cmMakefileTargetGenerator.cxx 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmMakefileTargetGenerator.h"
  11. #include "cmGeneratorTarget.h"
  12. #include "cmGeneratedFileStream.h"
  13. #include "cmGlobalGenerator.h"
  14. #include "cmGlobalUnixMakefileGenerator3.h"
  15. #include "cmLocalUnixMakefileGenerator3.h"
  16. #include "cmMakefile.h"
  17. #include "cmSourceFile.h"
  18. #include "cmTarget.h"
  19. #include "cmake.h"
  20. #include "cmComputeLinkInformation.h"
  21. #include "cmGeneratorExpression.h"
  22. #include "cmMakefileExecutableTargetGenerator.h"
  23. #include "cmMakefileLibraryTargetGenerator.h"
  24. #include "cmMakefileUtilityTargetGenerator.h"
  25. #include <ctype.h>
  26. cmMakefileTargetGenerator::cmMakefileTargetGenerator(cmTarget* target)
  27. : OSXBundleGenerator(0)
  28. , MacOSXContentGenerator(0)
  29. {
  30. this->BuildFileStream = 0;
  31. this->InfoFileStream = 0;
  32. this->FlagFileStream = 0;
  33. this->CustomCommandDriver = OnBuild;
  34. this->FortranModuleDirectoryComputed = false;
  35. this->Target = target;
  36. this->Makefile = this->Target->GetMakefile();
  37. this->LocalGenerator =
  38. static_cast<cmLocalUnixMakefileGenerator3*>(
  39. this->Makefile->GetLocalGenerator());
  40. this->ConfigName = this->LocalGenerator->ConfigurationName.c_str();
  41. this->GlobalGenerator =
  42. static_cast<cmGlobalUnixMakefileGenerator3*>(
  43. this->LocalGenerator->GetGlobalGenerator());
  44. this->GeneratorTarget = this->GlobalGenerator->GetGeneratorTarget(target);
  45. cmake* cm = this->GlobalGenerator->GetCMakeInstance();
  46. this->NoRuleMessages = false;
  47. if(const char* ruleStatus = cm->GetProperty("RULE_MESSAGES"))
  48. {
  49. this->NoRuleMessages = cmSystemTools::IsOff(ruleStatus);
  50. }
  51. MacOSXContentGenerator = new MacOSXContentGeneratorType(this);
  52. }
  53. cmMakefileTargetGenerator::~cmMakefileTargetGenerator()
  54. {
  55. delete MacOSXContentGenerator;
  56. }
  57. cmMakefileTargetGenerator *
  58. cmMakefileTargetGenerator::New(cmTarget *tgt)
  59. {
  60. cmMakefileTargetGenerator *result = 0;
  61. switch (tgt->GetType())
  62. {
  63. case cmTarget::EXECUTABLE:
  64. result = new cmMakefileExecutableTargetGenerator(tgt);
  65. break;
  66. case cmTarget::STATIC_LIBRARY:
  67. case cmTarget::SHARED_LIBRARY:
  68. case cmTarget::MODULE_LIBRARY:
  69. case cmTarget::OBJECT_LIBRARY:
  70. case cmTarget::INTERFACE_LIBRARY:
  71. result = new cmMakefileLibraryTargetGenerator(tgt);
  72. break;
  73. case cmTarget::UTILITY:
  74. result = new cmMakefileUtilityTargetGenerator(tgt);
  75. break;
  76. default:
  77. return result;
  78. // break; /* unreachable */
  79. }
  80. return result;
  81. }
  82. //----------------------------------------------------------------------------
  83. void cmMakefileTargetGenerator::CreateRuleFile()
  84. {
  85. // Create a directory for this target.
  86. this->TargetBuildDirectory =
  87. this->LocalGenerator->GetTargetDirectory(*this->Target);
  88. this->TargetBuildDirectoryFull =
  89. this->LocalGenerator->ConvertToFullPath(this->TargetBuildDirectory);
  90. cmSystemTools::MakeDirectory(this->TargetBuildDirectoryFull.c_str());
  91. // Construct the rule file name.
  92. this->BuildFileName = this->TargetBuildDirectory;
  93. this->BuildFileName += "/build.make";
  94. this->BuildFileNameFull = this->TargetBuildDirectoryFull;
  95. this->BuildFileNameFull += "/build.make";
  96. // Construct the rule file name.
  97. this->ProgressFileNameFull = this->TargetBuildDirectoryFull;
  98. this->ProgressFileNameFull += "/progress.make";
  99. // reset the progress count
  100. this->NumberOfProgressActions = 0;
  101. // Open the rule file. This should be copy-if-different because the
  102. // rules may depend on this file itself.
  103. this->BuildFileStream =
  104. new cmGeneratedFileStream(this->BuildFileNameFull.c_str());
  105. this->BuildFileStream->SetCopyIfDifferent(true);
  106. if(!this->BuildFileStream)
  107. {
  108. return;
  109. }
  110. this->LocalGenerator->WriteDisclaimer(*this->BuildFileStream);
  111. this->LocalGenerator->WriteSpecialTargetsTop(*this->BuildFileStream);
  112. }
  113. //----------------------------------------------------------------------------
  114. void cmMakefileTargetGenerator::WriteTargetBuildRules()
  115. {
  116. // write the custom commands for this target
  117. // Look for files registered for cleaning in this directory.
  118. if(const char* additional_clean_files =
  119. this->Makefile->GetProperty
  120. ("ADDITIONAL_MAKE_CLEAN_FILES"))
  121. {
  122. const char *config = this->Makefile->GetDefinition("CMAKE_BUILD_TYPE");
  123. cmListFileBacktrace lfbt;
  124. cmGeneratorExpression ge(lfbt);
  125. cmsys::auto_ptr<cmCompiledGeneratorExpression> cge =
  126. ge.Parse(additional_clean_files);
  127. cmSystemTools::ExpandListArgument(cge->Evaluate(this->Makefile, config,
  128. false, this->Target, 0, 0),
  129. this->CleanFiles);
  130. }
  131. // add custom commands to the clean rules?
  132. const char* clean_no_custom =
  133. this->Makefile->GetProperty("CLEAN_NO_CUSTOM");
  134. bool clean = cmSystemTools::IsOff(clean_no_custom);
  135. // First generate the object rule files. Save a list of all object
  136. // files for this target.
  137. for(std::vector<cmSourceFile*>::const_iterator
  138. si = this->GeneratorTarget->CustomCommands.begin();
  139. si != this->GeneratorTarget->CustomCommands.end(); ++si)
  140. {
  141. cmCustomCommand const* cc = (*si)->GetCustomCommand();
  142. this->GenerateCustomRuleFile(*cc);
  143. if (clean)
  144. {
  145. const std::vector<std::string>& outputs = cc->GetOutputs();
  146. for(std::vector<std::string>::const_iterator o = outputs.begin();
  147. o != outputs.end(); ++o)
  148. {
  149. this->CleanFiles.push_back
  150. (this->Convert(o->c_str(),
  151. cmLocalGenerator::START_OUTPUT,
  152. cmLocalGenerator::UNCHANGED));
  153. }
  154. }
  155. }
  156. this->OSXBundleGenerator->GenerateMacOSXContentStatements(
  157. this->GeneratorTarget->HeaderSources,
  158. this->MacOSXContentGenerator);
  159. this->OSXBundleGenerator->GenerateMacOSXContentStatements(
  160. this->GeneratorTarget->ExtraSources,
  161. this->MacOSXContentGenerator);
  162. for(std::vector<cmSourceFile*>::const_iterator
  163. si = this->GeneratorTarget->ExternalObjects.begin();
  164. si != this->GeneratorTarget->ExternalObjects.end(); ++si)
  165. {
  166. this->ExternalObjects.push_back((*si)->GetFullPath());
  167. }
  168. for(std::vector<cmSourceFile*>::const_iterator
  169. si = this->GeneratorTarget->ObjectSources.begin();
  170. si != this->GeneratorTarget->ObjectSources.end(); ++si)
  171. {
  172. // Generate this object file's rule file.
  173. this->WriteObjectRuleFiles(**si);
  174. }
  175. // Add object library contents as external objects.
  176. this->GeneratorTarget->UseObjectLibraries(this->ExternalObjects);
  177. }
  178. //----------------------------------------------------------------------------
  179. void cmMakefileTargetGenerator::WriteCommonCodeRules()
  180. {
  181. const char* root = (this->Makefile->IsOn("CMAKE_MAKE_INCLUDE_FROM_ROOT")?
  182. "$(CMAKE_BINARY_DIR)/" : "");
  183. // Include the dependencies for the target.
  184. std::string dependFileNameFull = this->TargetBuildDirectoryFull;
  185. dependFileNameFull += "/depend.make";
  186. *this->BuildFileStream
  187. << "# Include any dependencies generated for this target.\n"
  188. << this->LocalGenerator->IncludeDirective << " " << root
  189. << this->Convert(dependFileNameFull.c_str(),
  190. cmLocalGenerator::HOME_OUTPUT,
  191. cmLocalGenerator::MAKEFILE)
  192. << "\n\n";
  193. if(!this->NoRuleMessages)
  194. {
  195. // Include the progress variables for the target.
  196. *this->BuildFileStream
  197. << "# Include the progress variables for this target.\n"
  198. << this->LocalGenerator->IncludeDirective << " " << root
  199. << this->Convert(this->ProgressFileNameFull.c_str(),
  200. cmLocalGenerator::HOME_OUTPUT,
  201. cmLocalGenerator::MAKEFILE)
  202. << "\n\n";
  203. }
  204. // make sure the depend file exists
  205. if (!cmSystemTools::FileExists(dependFileNameFull.c_str()))
  206. {
  207. // Write an empty dependency file.
  208. cmGeneratedFileStream depFileStream(dependFileNameFull.c_str());
  209. depFileStream
  210. << "# Empty dependencies file for " << this->Target->GetName() << ".\n"
  211. << "# This may be replaced when dependencies are built." << std::endl;
  212. }
  213. // Open the flags file. This should be copy-if-different because the
  214. // rules may depend on this file itself.
  215. this->FlagFileNameFull = this->TargetBuildDirectoryFull;
  216. this->FlagFileNameFull += "/flags.make";
  217. this->FlagFileStream =
  218. new cmGeneratedFileStream(this->FlagFileNameFull.c_str());
  219. this->FlagFileStream->SetCopyIfDifferent(true);
  220. if(!this->FlagFileStream)
  221. {
  222. return;
  223. }
  224. this->LocalGenerator->WriteDisclaimer(*this->FlagFileStream);
  225. // Include the flags for the target.
  226. *this->BuildFileStream
  227. << "# Include the compile flags for this target's objects.\n"
  228. << this->LocalGenerator->IncludeDirective << " " << root
  229. << this->Convert(this->FlagFileNameFull.c_str(),
  230. cmLocalGenerator::HOME_OUTPUT,
  231. cmLocalGenerator::MAKEFILE)
  232. << "\n\n";
  233. }
  234. //----------------------------------------------------------------------------
  235. std::string cmMakefileTargetGenerator::GetFlags(const std::string &l)
  236. {
  237. ByLanguageMap::iterator i = this->FlagsByLanguage.find(l);
  238. if (i == this->FlagsByLanguage.end())
  239. {
  240. std::string flags;
  241. const char *lang = l.c_str();
  242. // Add language feature flags.
  243. this->AddFeatureFlags(flags, lang);
  244. this->LocalGenerator->AddArchitectureFlags(flags, this->GeneratorTarget,
  245. lang, this->ConfigName);
  246. // Fortran-specific flags computed for this target.
  247. if(l == "Fortran")
  248. {
  249. this->AddFortranFlags(flags);
  250. }
  251. this->LocalGenerator->AddCMP0018Flags(flags, this->Target,
  252. lang, this->ConfigName);
  253. this->LocalGenerator->AddVisibilityPresetFlags(flags, this->Target,
  254. lang);
  255. // Add include directory flags.
  256. this->AddIncludeFlags(flags, lang);
  257. // Append old-style preprocessor definition flags.
  258. this->LocalGenerator->
  259. AppendFlags(flags, this->Makefile->GetDefineFlags());
  260. // Add include directory flags.
  261. this->LocalGenerator->
  262. AppendFlags(flags,this->GetFrameworkFlags(l).c_str());
  263. // Add target-specific flags.
  264. this->LocalGenerator->AddCompileOptions(flags, this->Target,
  265. lang, this->ConfigName);
  266. ByLanguageMap::value_type entry(l, flags);
  267. i = this->FlagsByLanguage.insert(entry).first;
  268. }
  269. return i->second;
  270. }
  271. std::string cmMakefileTargetGenerator::GetDefines(const std::string &l)
  272. {
  273. ByLanguageMap::iterator i = this->DefinesByLanguage.find(l);
  274. if (i == this->DefinesByLanguage.end())
  275. {
  276. std::set<std::string> defines;
  277. const char *lang = l.c_str();
  278. // Add the export symbol definition for shared library objects.
  279. if(const char* exportMacro = this->Target->GetExportMacro())
  280. {
  281. this->LocalGenerator->AppendDefines(defines, exportMacro);
  282. }
  283. // Add preprocessor definitions for this target and configuration.
  284. this->LocalGenerator->AddCompileDefinitions(defines, this->Target,
  285. this->LocalGenerator->ConfigurationName.c_str());
  286. std::string definesString;
  287. this->LocalGenerator->JoinDefines(defines, definesString, lang);
  288. ByLanguageMap::value_type entry(l, definesString);
  289. i = this->DefinesByLanguage.insert(entry).first;
  290. }
  291. return i->second;
  292. }
  293. void cmMakefileTargetGenerator::WriteTargetLanguageFlags()
  294. {
  295. // write language flags for target
  296. std::set<cmStdString> languages;
  297. this->Target->GetLanguages(languages);
  298. // put the compiler in the rules.make file so that if it changes
  299. // things rebuild
  300. for(std::set<cmStdString>::const_iterator l = languages.begin();
  301. l != languages.end(); ++l)
  302. {
  303. cmStdString compiler = "CMAKE_";
  304. compiler += *l;
  305. compiler += "_COMPILER";
  306. *this->FlagFileStream << "# compile " << l->c_str() << " with " <<
  307. this->Makefile->GetSafeDefinition(compiler.c_str()) << "\n";
  308. }
  309. for(std::set<cmStdString>::const_iterator l = languages.begin();
  310. l != languages.end(); ++l)
  311. {
  312. *this->FlagFileStream << *l << "_FLAGS = " << this->GetFlags(*l) << "\n\n";
  313. *this->FlagFileStream << *l << "_DEFINES = " << this->GetDefines(*l) <<
  314. "\n\n";
  315. }
  316. }
  317. //----------------------------------------------------------------------------
  318. void
  319. cmMakefileTargetGenerator::MacOSXContentGeneratorType::operator()
  320. (cmSourceFile& source, const char* pkgloc)
  321. {
  322. // Skip OS X content when not building a Framework or Bundle.
  323. if(!this->Generator->GetTarget()->IsBundleOnApple())
  324. {
  325. return;
  326. }
  327. std::string macdir =
  328. this->Generator->OSXBundleGenerator->InitMacOSXContentDirectory(pkgloc);
  329. // Get the input file location.
  330. std::string input = source.GetFullPath();
  331. // Get the output file location.
  332. std::string output = macdir;
  333. output += "/";
  334. output += cmSystemTools::GetFilenameName(input);
  335. this->Generator->CleanFiles.push_back(
  336. this->Generator->Convert(output.c_str(),
  337. cmLocalGenerator::START_OUTPUT));
  338. output = this->Generator->Convert(output.c_str(),
  339. cmLocalGenerator::HOME_OUTPUT);
  340. // Create a rule to copy the content into the bundle.
  341. std::vector<std::string> depends;
  342. std::vector<std::string> commands;
  343. depends.push_back(input);
  344. std::string copyEcho = "Copying OS X content ";
  345. copyEcho += output;
  346. this->Generator->LocalGenerator->AppendEcho(
  347. commands, copyEcho.c_str(),
  348. cmLocalUnixMakefileGenerator3::EchoBuild);
  349. std::string copyCommand = "$(CMAKE_COMMAND) -E copy ";
  350. copyCommand += this->Generator->Convert(input.c_str(),
  351. cmLocalGenerator::NONE,
  352. cmLocalGenerator::SHELL);
  353. copyCommand += " ";
  354. copyCommand += this->Generator->Convert(output.c_str(),
  355. cmLocalGenerator::NONE,
  356. cmLocalGenerator::SHELL);
  357. commands.push_back(copyCommand);
  358. this->Generator->LocalGenerator->WriteMakeRule(
  359. *this->Generator->BuildFileStream, 0,
  360. output.c_str(),
  361. depends, commands, false);
  362. this->Generator->ExtraFiles.insert(output);
  363. }
  364. //----------------------------------------------------------------------------
  365. void cmMakefileTargetGenerator::WriteObjectRuleFiles(cmSourceFile& source)
  366. {
  367. // Identify the language of the source file.
  368. const char* lang = this->LocalGenerator->GetSourceFileLanguage(source);
  369. if(!lang)
  370. {
  371. // don't know anything about this file so skip it
  372. return;
  373. }
  374. // Get the full path name of the object file.
  375. std::string const& objectName = this->GeneratorTarget->Objects[&source];
  376. std::string obj = this->LocalGenerator->GetTargetDirectory(*this->Target);
  377. obj += "/";
  378. obj += objectName;
  379. // Avoid generating duplicate rules.
  380. if(this->ObjectFiles.find(obj) == this->ObjectFiles.end())
  381. {
  382. this->ObjectFiles.insert(obj);
  383. }
  384. else
  385. {
  386. cmOStringStream err;
  387. err << "Warning: Source file \""
  388. << source.GetFullPath()
  389. << "\" is listed multiple times for target \""
  390. << this->Target->GetName()
  391. << "\".";
  392. cmSystemTools::Message(err.str().c_str(), "Warning");
  393. return;
  394. }
  395. // Create the directory containing the object file. This may be a
  396. // subdirectory under the target's directory.
  397. std::string dir = cmSystemTools::GetFilenamePath(obj.c_str());
  398. cmSystemTools::MakeDirectory
  399. (this->LocalGenerator->ConvertToFullPath(dir).c_str());
  400. // Save this in the target's list of object files.
  401. this->Objects.push_back(obj);
  402. this->CleanFiles.push_back(obj);
  403. // TODO: Remove
  404. //std::string relativeObj
  405. //= this->LocalGenerator->GetHomeRelativeOutputPath();
  406. //relativeObj += obj;
  407. // we compute some depends when writing the depend.make that we will also
  408. // use in the build.make, same with depMakeFile
  409. std::vector<std::string> depends;
  410. std::string depMakeFile;
  411. // generate the build rule file
  412. this->WriteObjectBuildFile(obj, lang, source, depends);
  413. // The object file should be checked for dependency integrity.
  414. std::string objFullPath = this->Makefile->GetCurrentOutputDirectory();
  415. objFullPath += "/";
  416. objFullPath += obj;
  417. objFullPath =
  418. this->Convert(objFullPath.c_str(), cmLocalGenerator::FULL);
  419. std::string srcFullPath =
  420. this->Convert(source.GetFullPath().c_str(), cmLocalGenerator::FULL);
  421. this->LocalGenerator->
  422. AddImplicitDepends(*this->Target, lang,
  423. objFullPath.c_str(),
  424. srcFullPath.c_str());
  425. }
  426. //----------------------------------------------------------------------------
  427. void
  428. cmMakefileTargetGenerator
  429. ::AppendFortranFormatFlags(std::string& flags, cmSourceFile& source)
  430. {
  431. const char* srcfmt = source.GetProperty("Fortran_FORMAT");
  432. cmLocalGenerator::FortranFormat format =
  433. this->LocalGenerator->GetFortranFormat(srcfmt);
  434. if(format == cmLocalGenerator::FortranFormatNone)
  435. {
  436. const char* tgtfmt = this->Target->GetProperty("Fortran_FORMAT");
  437. format = this->LocalGenerator->GetFortranFormat(tgtfmt);
  438. }
  439. const char* var = 0;
  440. switch (format)
  441. {
  442. case cmLocalGenerator::FortranFormatFixed:
  443. var = "CMAKE_Fortran_FORMAT_FIXED_FLAG"; break;
  444. case cmLocalGenerator::FortranFormatFree:
  445. var = "CMAKE_Fortran_FORMAT_FREE_FLAG"; break;
  446. default: break;
  447. }
  448. if(var)
  449. {
  450. this->LocalGenerator->AppendFlags(
  451. flags, this->Makefile->GetDefinition(var));
  452. }
  453. }
  454. //----------------------------------------------------------------------------
  455. void
  456. cmMakefileTargetGenerator
  457. ::WriteObjectBuildFile(std::string &obj,
  458. const char *lang,
  459. cmSourceFile& source,
  460. std::vector<std::string>& depends)
  461. {
  462. this->LocalGenerator->AppendRuleDepend(depends,
  463. this->FlagFileNameFull.c_str());
  464. this->LocalGenerator->AppendRuleDepends(depends,
  465. this->FlagFileDepends[lang]);
  466. // generate the depend scanning rule
  467. this->WriteObjectDependRules(source, depends);
  468. std::string relativeObj = this->LocalGenerator->GetHomeRelativeOutputPath();
  469. relativeObj += obj;
  470. // Write the build rule.
  471. // Build the set of compiler flags.
  472. std::string flags;
  473. // Add language-specific flags.
  474. std::string langFlags = "$(";
  475. langFlags += lang;
  476. langFlags += "_FLAGS)";
  477. this->LocalGenerator->AppendFlags(flags, langFlags.c_str());
  478. std::string configUpper =
  479. cmSystemTools::UpperCase(this->LocalGenerator->ConfigurationName);
  480. // Add Fortran format flags.
  481. if(strcmp(lang, "Fortran") == 0)
  482. {
  483. this->AppendFortranFormatFlags(flags, source);
  484. }
  485. // Add flags from source file properties.
  486. if (source.GetProperty("COMPILE_FLAGS"))
  487. {
  488. this->LocalGenerator->AppendFlags
  489. (flags, source.GetProperty("COMPILE_FLAGS"));
  490. *this->FlagFileStream << "# Custom flags: "
  491. << relativeObj << "_FLAGS = "
  492. << source.GetProperty("COMPILE_FLAGS")
  493. << "\n"
  494. << "\n";
  495. }
  496. // Add language-specific defines.
  497. std::set<std::string> defines;
  498. // Add source-sepcific preprocessor definitions.
  499. if(const char* compile_defs = source.GetProperty("COMPILE_DEFINITIONS"))
  500. {
  501. this->LocalGenerator->AppendDefines(defines, compile_defs);
  502. *this->FlagFileStream << "# Custom defines: "
  503. << relativeObj << "_DEFINES = "
  504. << compile_defs << "\n"
  505. << "\n";
  506. }
  507. std::string defPropName = "COMPILE_DEFINITIONS_";
  508. defPropName += configUpper;
  509. if(const char* config_compile_defs =
  510. source.GetProperty(defPropName.c_str()))
  511. {
  512. this->LocalGenerator->AppendDefines(defines, config_compile_defs);
  513. *this->FlagFileStream
  514. << "# Custom defines: "
  515. << relativeObj << "_DEFINES_" << configUpper
  516. << " = " << config_compile_defs << "\n"
  517. << "\n";
  518. }
  519. // Get the output paths for source and object files.
  520. std::string sourceFile = source.GetFullPath();
  521. if(this->LocalGenerator->UseRelativePaths)
  522. {
  523. sourceFile = this->Convert(sourceFile.c_str(),
  524. cmLocalGenerator::START_OUTPUT);
  525. }
  526. sourceFile = this->Convert(sourceFile.c_str(),
  527. cmLocalGenerator::NONE,
  528. cmLocalGenerator::SHELL);
  529. // Construct the build message.
  530. std::vector<std::string> no_commands;
  531. std::vector<std::string> commands;
  532. // add in a progress call if needed
  533. this->AppendProgress(commands);
  534. if(!this->NoRuleMessages)
  535. {
  536. std::string buildEcho = "Building ";
  537. buildEcho += lang;
  538. buildEcho += " object ";
  539. buildEcho += relativeObj;
  540. this->LocalGenerator->AppendEcho
  541. (commands, buildEcho.c_str(), cmLocalUnixMakefileGenerator3::EchoBuild);
  542. }
  543. std::string targetOutPathReal;
  544. std::string targetOutPathPDB;
  545. {
  546. std::string targetFullPathReal;
  547. std::string targetFullPathPDB;
  548. if(this->Target->GetType() == cmTarget::EXECUTABLE ||
  549. this->Target->GetType() == cmTarget::STATIC_LIBRARY ||
  550. this->Target->GetType() == cmTarget::SHARED_LIBRARY ||
  551. this->Target->GetType() == cmTarget::MODULE_LIBRARY)
  552. {
  553. targetFullPathReal =
  554. this->Target->GetFullPath(this->ConfigName, false, true);
  555. targetFullPathPDB = this->Target->GetPDBDirectory(this->ConfigName);
  556. targetFullPathPDB += "/";
  557. targetFullPathPDB += this->Target->GetPDBName(this->ConfigName);
  558. }
  559. targetOutPathReal = this->Convert(targetFullPathReal.c_str(),
  560. cmLocalGenerator::START_OUTPUT,
  561. cmLocalGenerator::SHELL);
  562. targetOutPathPDB =
  563. this->Convert(targetFullPathPDB.c_str(),cmLocalGenerator::NONE,
  564. cmLocalGenerator::SHELL);
  565. }
  566. cmLocalGenerator::RuleVariables vars;
  567. vars.RuleLauncher = "RULE_LAUNCH_COMPILE";
  568. vars.CMTarget = this->Target;
  569. vars.Language = lang;
  570. vars.Target = targetOutPathReal.c_str();
  571. vars.TargetPDB = targetOutPathPDB.c_str();
  572. vars.Source = sourceFile.c_str();
  573. std::string shellObj =
  574. this->Convert(obj.c_str(),
  575. cmLocalGenerator::NONE,
  576. cmLocalGenerator::SHELL).c_str();
  577. vars.Object = shellObj.c_str();
  578. std::string objectDir = cmSystemTools::GetFilenamePath(obj);
  579. objectDir = this->Convert(objectDir.c_str(),
  580. cmLocalGenerator::START_OUTPUT,
  581. cmLocalGenerator::SHELL);
  582. vars.ObjectDir = objectDir.c_str();
  583. vars.Flags = flags.c_str();
  584. std::string definesString = "$(";
  585. definesString += lang;
  586. definesString += "_DEFINES)";
  587. this->LocalGenerator->JoinDefines(defines, definesString, lang);
  588. vars.Defines = definesString.c_str();
  589. bool lang_is_c_or_cxx = ((strcmp(lang, "C") == 0) ||
  590. (strcmp(lang, "CXX") == 0));
  591. // Construct the compile rules.
  592. {
  593. std::string compileRuleVar = "CMAKE_";
  594. compileRuleVar += lang;
  595. compileRuleVar += "_COMPILE_OBJECT";
  596. std::string compileRule =
  597. this->Makefile->GetRequiredDefinition(compileRuleVar.c_str());
  598. std::vector<std::string> compileCommands;
  599. cmSystemTools::ExpandListArgument(compileRule, compileCommands);
  600. if (this->Makefile->IsOn("CMAKE_EXPORT_COMPILE_COMMANDS") &&
  601. lang_is_c_or_cxx && compileCommands.size() == 1)
  602. {
  603. std::string compileCommand = compileCommands[0];
  604. this->LocalGenerator->ExpandRuleVariables(compileCommand, vars);
  605. std::string workingDirectory =
  606. this->LocalGenerator->Convert(
  607. this->Makefile->GetStartOutputDirectory(), cmLocalGenerator::FULL);
  608. compileCommand.replace(compileCommand.find(langFlags),
  609. langFlags.size(), this->GetFlags(lang));
  610. std::string langDefines = std::string("$(") + lang + "_DEFINES)";
  611. compileCommand.replace(compileCommand.find(langDefines),
  612. langDefines.size(), this->GetDefines(lang));
  613. this->GlobalGenerator->AddCXXCompileCommand(
  614. source.GetFullPath(), workingDirectory, compileCommand);
  615. }
  616. // Expand placeholders in the commands.
  617. for(std::vector<std::string>::iterator i = compileCommands.begin();
  618. i != compileCommands.end(); ++i)
  619. {
  620. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  621. }
  622. // Change the command working directory to the local build tree.
  623. this->LocalGenerator->CreateCDCommand
  624. (compileCommands,
  625. this->Makefile->GetStartOutputDirectory(),
  626. cmLocalGenerator::HOME_OUTPUT);
  627. commands.insert(commands.end(),
  628. compileCommands.begin(), compileCommands.end());
  629. }
  630. // Write the rule.
  631. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  632. relativeObj.c_str(),
  633. depends, commands, false);
  634. // Check for extra outputs created by the compilation.
  635. if(const char* extra_outputs_str =
  636. source.GetProperty("OBJECT_OUTPUTS"))
  637. {
  638. std::vector<std::string> extra_outputs;
  639. cmSystemTools::ExpandListArgument(extra_outputs_str, extra_outputs);
  640. for(std::vector<std::string>::const_iterator eoi = extra_outputs.begin();
  641. eoi != extra_outputs.end(); ++eoi)
  642. {
  643. // Register this as an extra output for the object file rule.
  644. // This will cause the object file to be rebuilt if the extra
  645. // output is missing.
  646. this->GenerateExtraOutput(eoi->c_str(), relativeObj.c_str(), false);
  647. // Register this as an extra file to clean.
  648. this->CleanFiles.push_back(eoi->c_str());
  649. }
  650. }
  651. bool do_preprocess_rules = lang_is_c_or_cxx &&
  652. this->LocalGenerator->GetCreatePreprocessedSourceRules();
  653. bool do_assembly_rules = lang_is_c_or_cxx &&
  654. this->LocalGenerator->GetCreateAssemblySourceRules();
  655. if(do_preprocess_rules || do_assembly_rules)
  656. {
  657. std::vector<std::string> force_depends;
  658. force_depends.push_back("cmake_force");
  659. std::string::size_type dot_pos = relativeObj.rfind(".");
  660. std::string relativeObjBase = relativeObj.substr(0, dot_pos);
  661. dot_pos = obj.rfind(".");
  662. std::string objBase = obj.substr(0, dot_pos);
  663. if(do_preprocess_rules)
  664. {
  665. commands.clear();
  666. std::string relativeObjI = relativeObjBase + ".i";
  667. std::string objI = objBase + ".i";
  668. std::string preprocessEcho = "Preprocessing ";
  669. preprocessEcho += lang;
  670. preprocessEcho += " source to ";
  671. preprocessEcho += objI;
  672. this->LocalGenerator->AppendEcho(
  673. commands, preprocessEcho.c_str(),
  674. cmLocalUnixMakefileGenerator3::EchoBuild
  675. );
  676. std::string preprocessRuleVar = "CMAKE_";
  677. preprocessRuleVar += lang;
  678. preprocessRuleVar += "_CREATE_PREPROCESSED_SOURCE";
  679. if(const char* preprocessRule =
  680. this->Makefile->GetDefinition(preprocessRuleVar.c_str()))
  681. {
  682. std::vector<std::string> preprocessCommands;
  683. cmSystemTools::ExpandListArgument(preprocessRule, preprocessCommands);
  684. std::string shellObjI =
  685. this->Convert(objI.c_str(),
  686. cmLocalGenerator::NONE,
  687. cmLocalGenerator::SHELL).c_str();
  688. vars.PreprocessedSource = shellObjI.c_str();
  689. // Expand placeholders in the commands.
  690. for(std::vector<std::string>::iterator i = preprocessCommands.begin();
  691. i != preprocessCommands.end(); ++i)
  692. {
  693. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  694. }
  695. this->LocalGenerator->CreateCDCommand
  696. (preprocessCommands,
  697. this->Makefile->GetStartOutputDirectory(),
  698. cmLocalGenerator::HOME_OUTPUT);
  699. commands.insert(commands.end(),
  700. preprocessCommands.begin(),
  701. preprocessCommands.end());
  702. }
  703. else
  704. {
  705. std::string cmd = "$(CMAKE_COMMAND) -E cmake_unimplemented_variable ";
  706. cmd += preprocessRuleVar;
  707. commands.push_back(cmd);
  708. }
  709. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  710. relativeObjI.c_str(),
  711. force_depends, commands, false);
  712. }
  713. if(do_assembly_rules)
  714. {
  715. commands.clear();
  716. std::string relativeObjS = relativeObjBase + ".s";
  717. std::string objS = objBase + ".s";
  718. std::string assemblyEcho = "Compiling ";
  719. assemblyEcho += lang;
  720. assemblyEcho += " source to assembly ";
  721. assemblyEcho += objS;
  722. this->LocalGenerator->AppendEcho(
  723. commands, assemblyEcho.c_str(),
  724. cmLocalUnixMakefileGenerator3::EchoBuild
  725. );
  726. std::string assemblyRuleVar = "CMAKE_";
  727. assemblyRuleVar += lang;
  728. assemblyRuleVar += "_CREATE_ASSEMBLY_SOURCE";
  729. if(const char* assemblyRule =
  730. this->Makefile->GetDefinition(assemblyRuleVar.c_str()))
  731. {
  732. std::vector<std::string> assemblyCommands;
  733. cmSystemTools::ExpandListArgument(assemblyRule, assemblyCommands);
  734. std::string shellObjS =
  735. this->Convert(objS.c_str(),
  736. cmLocalGenerator::NONE,
  737. cmLocalGenerator::SHELL).c_str();
  738. vars.AssemblySource = shellObjS.c_str();
  739. // Expand placeholders in the commands.
  740. for(std::vector<std::string>::iterator i = assemblyCommands.begin();
  741. i != assemblyCommands.end(); ++i)
  742. {
  743. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  744. }
  745. this->LocalGenerator->CreateCDCommand
  746. (assemblyCommands,
  747. this->Makefile->GetStartOutputDirectory(),
  748. cmLocalGenerator::HOME_OUTPUT);
  749. commands.insert(commands.end(),
  750. assemblyCommands.begin(),
  751. assemblyCommands.end());
  752. }
  753. else
  754. {
  755. std::string cmd = "$(CMAKE_COMMAND) -E cmake_unimplemented_variable ";
  756. cmd += assemblyRuleVar;
  757. commands.push_back(cmd);
  758. }
  759. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  760. relativeObjS.c_str(),
  761. force_depends, commands, false);
  762. }
  763. }
  764. // If the language needs provides-requires mode, create the
  765. // corresponding targets.
  766. std::string objectRequires = relativeObj;
  767. objectRequires += ".requires";
  768. std::vector<std::string> p_depends;
  769. // always provide an empty requires target
  770. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  771. objectRequires.c_str(), p_depends,
  772. no_commands, true);
  773. // write a build rule to recursively build what this obj provides
  774. std::string objectProvides = relativeObj;
  775. objectProvides += ".provides";
  776. std::string temp = relativeObj;
  777. temp += ".provides.build";
  778. std::vector<std::string> r_commands;
  779. std::string tgtMakefileName =
  780. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  781. tgtMakefileName += "/build.make";
  782. r_commands.push_back
  783. (this->LocalGenerator->GetRecursiveMakeCall(tgtMakefileName.c_str(),
  784. temp.c_str()));
  785. p_depends.clear();
  786. p_depends.push_back(objectRequires);
  787. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  788. objectProvides.c_str(), p_depends,
  789. r_commands, true);
  790. // write the provides.build rule dependency on the obj file
  791. p_depends.clear();
  792. p_depends.push_back(relativeObj);
  793. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  794. temp.c_str(), p_depends, no_commands,
  795. false);
  796. }
  797. //----------------------------------------------------------------------------
  798. void cmMakefileTargetGenerator::WriteTargetRequiresRules()
  799. {
  800. std::vector<std::string> depends;
  801. std::vector<std::string> no_commands;
  802. // Construct the name of the dependency generation target.
  803. std::string depTarget =
  804. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  805. depTarget += "/requires";
  806. // This target drives dependency generation for all object files.
  807. std::string relPath = this->LocalGenerator->GetHomeRelativeOutputPath();
  808. std::string objTarget;
  809. for(std::vector<std::string>::const_iterator obj = this->Objects.begin();
  810. obj != this->Objects.end(); ++obj)
  811. {
  812. objTarget = relPath;
  813. objTarget += *obj;
  814. objTarget += ".requires";
  815. depends.push_back(objTarget);
  816. }
  817. // Write the rule.
  818. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  819. depTarget.c_str(),
  820. depends, no_commands, true);
  821. }
  822. //----------------------------------------------------------------------------
  823. void cmMakefileTargetGenerator::WriteTargetCleanRules()
  824. {
  825. std::vector<std::string> depends;
  826. std::vector<std::string> commands;
  827. // Construct the clean target name.
  828. std::string cleanTarget =
  829. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  830. cleanTarget += "/clean";
  831. // Construct the clean command.
  832. this->LocalGenerator->AppendCleanCommand(commands, this->CleanFiles,
  833. *this->Target);
  834. this->LocalGenerator->CreateCDCommand
  835. (commands,
  836. this->Makefile->GetStartOutputDirectory(),
  837. cmLocalGenerator::HOME_OUTPUT);
  838. // Write the rule.
  839. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  840. cleanTarget.c_str(),
  841. depends, commands, true);
  842. }
  843. //----------------------------------------------------------------------------
  844. void cmMakefileTargetGenerator::WriteTargetDependRules()
  845. {
  846. // must write the targets depend info file
  847. std::string dir = this->LocalGenerator->GetTargetDirectory(*this->Target);
  848. this->InfoFileNameFull = dir;
  849. this->InfoFileNameFull += "/DependInfo.cmake";
  850. this->InfoFileNameFull =
  851. this->LocalGenerator->ConvertToFullPath(this->InfoFileNameFull);
  852. this->InfoFileStream =
  853. new cmGeneratedFileStream(this->InfoFileNameFull.c_str());
  854. this->InfoFileStream->SetCopyIfDifferent(true);
  855. if(!*this->InfoFileStream)
  856. {
  857. return;
  858. }
  859. this->LocalGenerator->
  860. WriteDependLanguageInfo(*this->InfoFileStream,*this->Target);
  861. // Store multiple output pairs in the depend info file.
  862. if(!this->MultipleOutputPairs.empty())
  863. {
  864. *this->InfoFileStream
  865. << "\n"
  866. << "# Pairs of files generated by the same build rule.\n"
  867. << "set(CMAKE_MULTIPLE_OUTPUT_PAIRS\n";
  868. for(MultipleOutputPairsType::const_iterator pi =
  869. this->MultipleOutputPairs.begin();
  870. pi != this->MultipleOutputPairs.end(); ++pi)
  871. {
  872. *this->InfoFileStream
  873. << " " << this->LocalGenerator->EscapeForCMake(pi->first.c_str())
  874. << " " << this->LocalGenerator->EscapeForCMake(pi->second.c_str())
  875. << "\n";
  876. }
  877. *this->InfoFileStream << " )\n\n";
  878. }
  879. // Store list of targets linked directly or transitively.
  880. {
  881. *this->InfoFileStream
  882. << "\n"
  883. << "# Targets to which this target links.\n"
  884. << "set(CMAKE_TARGET_LINKED_INFO_FILES\n";
  885. std::set<cmTarget const*> emitted;
  886. const char* cfg = this->LocalGenerator->ConfigurationName.c_str();
  887. if(cmComputeLinkInformation* cli = this->Target->GetLinkInformation(cfg))
  888. {
  889. cmComputeLinkInformation::ItemVector const& items = cli->GetItems();
  890. for(cmComputeLinkInformation::ItemVector::const_iterator
  891. i = items.begin(); i != items.end(); ++i)
  892. {
  893. cmTarget const* linkee = i->Target;
  894. if(linkee && !linkee->IsImported()
  895. // We can ignore the INTERFACE_LIBRARY items because
  896. // Target->GetLinkInformation already processed their
  897. // link interface and they don't have any output themselves.
  898. && linkee->GetType() != cmTarget::INTERFACE_LIBRARY
  899. && emitted.insert(linkee).second)
  900. {
  901. cmMakefile* mf = linkee->GetMakefile();
  902. cmLocalGenerator* lg = mf->GetLocalGenerator();
  903. std::string di = mf->GetStartOutputDirectory();
  904. di += "/";
  905. di += lg->GetTargetDirectory(*linkee);
  906. di += "/DependInfo.cmake";
  907. *this->InfoFileStream << " \"" << di << "\"\n";
  908. }
  909. }
  910. }
  911. *this->InfoFileStream
  912. << " )\n";
  913. }
  914. // Check for a target-specific module output directory.
  915. if(const char* mdir = this->GetFortranModuleDirectory())
  916. {
  917. *this->InfoFileStream
  918. << "\n"
  919. << "# Fortran module output directory.\n"
  920. << "set(CMAKE_Fortran_TARGET_MODULE_DIR \"" << mdir << "\")\n";
  921. }
  922. // Target-specific include directories:
  923. *this->InfoFileStream
  924. << "\n"
  925. << "# The include file search paths:\n";
  926. *this->InfoFileStream
  927. << "set(CMAKE_C_TARGET_INCLUDE_PATH\n";
  928. std::vector<std::string> includes;
  929. const char *config = this->Makefile->GetDefinition("CMAKE_BUILD_TYPE");
  930. this->LocalGenerator->GetIncludeDirectories(includes,
  931. this->GeneratorTarget,
  932. "C", config);
  933. for(std::vector<std::string>::iterator i = includes.begin();
  934. i != includes.end(); ++i)
  935. {
  936. *this->InfoFileStream
  937. << " \""
  938. << this->LocalGenerator->Convert(i->c_str(),
  939. cmLocalGenerator::HOME_OUTPUT)
  940. << "\"\n";
  941. }
  942. *this->InfoFileStream
  943. << " )\n";
  944. *this->InfoFileStream
  945. << "set(CMAKE_CXX_TARGET_INCLUDE_PATH "
  946. << "${CMAKE_C_TARGET_INCLUDE_PATH})\n";
  947. *this->InfoFileStream
  948. << "set(CMAKE_Fortran_TARGET_INCLUDE_PATH "
  949. << "${CMAKE_C_TARGET_INCLUDE_PATH})\n";
  950. *this->InfoFileStream
  951. << "set(CMAKE_ASM_TARGET_INCLUDE_PATH "
  952. << "${CMAKE_C_TARGET_INCLUDE_PATH})\n";
  953. // and now write the rule to use it
  954. std::vector<std::string> depends;
  955. std::vector<std::string> commands;
  956. // Construct the name of the dependency generation target.
  957. std::string depTarget =
  958. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  959. depTarget += "/depend";
  960. // Add a command to call CMake to scan dependencies. CMake will
  961. // touch the corresponding depends file after scanning dependencies.
  962. cmOStringStream depCmd;
  963. // TODO: Account for source file properties and directory-level
  964. // definitions when scanning for dependencies.
  965. #if !defined(_WIN32) || defined(__CYGWIN__)
  966. // This platform supports symlinks, so cmSystemTools will translate
  967. // paths. Make sure PWD is set to the original name of the home
  968. // output directory to help cmSystemTools to create the same
  969. // translation table for the dependency scanning process.
  970. depCmd << "cd "
  971. << (this->LocalGenerator->Convert(
  972. this->Makefile->GetHomeOutputDirectory(),
  973. cmLocalGenerator::FULL, cmLocalGenerator::SHELL))
  974. << " && ";
  975. #endif
  976. // Generate a call this signature:
  977. //
  978. // cmake -E cmake_depends <generator>
  979. // <home-src-dir> <start-src-dir>
  980. // <home-out-dir> <start-out-dir>
  981. // <dep-info> --color=$(COLOR)
  982. //
  983. // This gives the dependency scanner enough information to recreate
  984. // the state of our local generator sufficiently for its needs.
  985. depCmd << "$(CMAKE_COMMAND) -E cmake_depends \""
  986. << this->GlobalGenerator->GetName() << "\" "
  987. << this->Convert(this->Makefile->GetHomeDirectory(),
  988. cmLocalGenerator::FULL, cmLocalGenerator::SHELL)
  989. << " "
  990. << this->Convert(this->Makefile->GetStartDirectory(),
  991. cmLocalGenerator::FULL, cmLocalGenerator::SHELL)
  992. << " "
  993. << this->Convert(this->Makefile->GetHomeOutputDirectory(),
  994. cmLocalGenerator::FULL, cmLocalGenerator::SHELL)
  995. << " "
  996. << this->Convert(this->Makefile->GetStartOutputDirectory(),
  997. cmLocalGenerator::FULL, cmLocalGenerator::SHELL)
  998. << " "
  999. << this->Convert(this->InfoFileNameFull.c_str(),
  1000. cmLocalGenerator::FULL, cmLocalGenerator::SHELL);
  1001. if(this->LocalGenerator->GetColorMakefile())
  1002. {
  1003. depCmd << " --color=$(COLOR)";
  1004. }
  1005. commands.push_back(depCmd.str());
  1006. // Make sure all custom command outputs in this target are built.
  1007. if(this->CustomCommandDriver == OnDepends)
  1008. {
  1009. this->DriveCustomCommands(depends);
  1010. }
  1011. // Write the rule.
  1012. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  1013. depTarget.c_str(),
  1014. depends, commands, true);
  1015. }
  1016. //----------------------------------------------------------------------------
  1017. void
  1018. cmMakefileTargetGenerator
  1019. ::DriveCustomCommands(std::vector<std::string>& depends)
  1020. {
  1021. // Depend on all custom command outputs.
  1022. const std::vector<cmSourceFile*>& sources =
  1023. this->Target->GetSourceFiles();
  1024. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  1025. source != sources.end(); ++source)
  1026. {
  1027. if(cmCustomCommand* cc = (*source)->GetCustomCommand())
  1028. {
  1029. const std::vector<std::string>& outputs = cc->GetOutputs();
  1030. for(std::vector<std::string>::const_iterator o = outputs.begin();
  1031. o != outputs.end(); ++o)
  1032. {
  1033. depends.push_back(*o);
  1034. }
  1035. }
  1036. }
  1037. }
  1038. //----------------------------------------------------------------------------
  1039. void cmMakefileTargetGenerator
  1040. ::WriteObjectDependRules(cmSourceFile& source,
  1041. std::vector<std::string>& depends)
  1042. {
  1043. // Create the list of dependencies known at cmake time. These are
  1044. // shared between the object file and dependency scanning rule.
  1045. depends.push_back(source.GetFullPath());
  1046. if(const char* objectDeps = source.GetProperty("OBJECT_DEPENDS"))
  1047. {
  1048. std::vector<std::string> deps;
  1049. cmSystemTools::ExpandListArgument(objectDeps, deps);
  1050. for(std::vector<std::string>::iterator i = deps.begin();
  1051. i != deps.end(); ++i)
  1052. {
  1053. depends.push_back(i->c_str());
  1054. }
  1055. }
  1056. }
  1057. //----------------------------------------------------------------------------
  1058. void cmMakefileTargetGenerator
  1059. ::GenerateCustomRuleFile(const cmCustomCommand& cc)
  1060. {
  1061. // Collect the commands.
  1062. std::vector<std::string> commands;
  1063. std::string comment = this->LocalGenerator->ConstructComment(cc);
  1064. if(!comment.empty())
  1065. {
  1066. // add in a progress call if needed
  1067. this->AppendProgress(commands);
  1068. if(!this->NoRuleMessages)
  1069. {
  1070. this->LocalGenerator
  1071. ->AppendEcho(commands, comment.c_str(),
  1072. cmLocalUnixMakefileGenerator3::EchoGenerate);
  1073. }
  1074. }
  1075. // Now append the actual user-specified commands.
  1076. cmOStringStream content;
  1077. this->LocalGenerator->AppendCustomCommand(commands, cc, this->Target, false,
  1078. cmLocalGenerator::HOME_OUTPUT,
  1079. &content);
  1080. // Collect the dependencies.
  1081. std::vector<std::string> depends;
  1082. this->LocalGenerator->AppendCustomDepend(depends, cc);
  1083. // Check whether we need to bother checking for a symbolic output.
  1084. bool need_symbolic = this->GlobalGenerator->GetNeedSymbolicMark();
  1085. // Write the rule.
  1086. const std::vector<std::string>& outputs = cc.GetOutputs();
  1087. std::vector<std::string>::const_iterator o = outputs.begin();
  1088. {
  1089. bool symbolic = false;
  1090. if(need_symbolic)
  1091. {
  1092. if(cmSourceFile* sf = this->Makefile->GetSource(o->c_str()))
  1093. {
  1094. symbolic = sf->GetPropertyAsBool("SYMBOLIC");
  1095. }
  1096. }
  1097. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  1098. o->c_str(), depends, commands,
  1099. symbolic);
  1100. // If the rule has changed make sure the output is rebuilt.
  1101. if(!symbolic)
  1102. {
  1103. this->GlobalGenerator->AddRuleHash(cc.GetOutputs(), content.str());
  1104. }
  1105. }
  1106. // Write rules to drive building any outputs beyond the first.
  1107. const char* in = o->c_str();
  1108. for(++o; o != outputs.end(); ++o)
  1109. {
  1110. bool symbolic = false;
  1111. if(need_symbolic)
  1112. {
  1113. if(cmSourceFile* sf = this->Makefile->GetSource(o->c_str()))
  1114. {
  1115. symbolic = sf->GetPropertyAsBool("SYMBOLIC");
  1116. }
  1117. }
  1118. this->GenerateExtraOutput(o->c_str(), in, symbolic);
  1119. }
  1120. // Setup implicit dependency scanning.
  1121. for(cmCustomCommand::ImplicitDependsList::const_iterator
  1122. idi = cc.GetImplicitDepends().begin();
  1123. idi != cc.GetImplicitDepends().end(); ++idi)
  1124. {
  1125. std::string objFullPath =
  1126. this->Convert(outputs[0].c_str(), cmLocalGenerator::FULL);
  1127. std::string srcFullPath =
  1128. this->Convert(idi->second.c_str(), cmLocalGenerator::FULL);
  1129. this->LocalGenerator->
  1130. AddImplicitDepends(*this->Target, idi->first.c_str(),
  1131. objFullPath.c_str(),
  1132. srcFullPath.c_str());
  1133. }
  1134. }
  1135. //----------------------------------------------------------------------------
  1136. void
  1137. cmMakefileTargetGenerator
  1138. ::GenerateExtraOutput(const char* out, const char* in, bool symbolic)
  1139. {
  1140. // Add a rule to build the primary output if the extra output needs
  1141. // to be created.
  1142. std::vector<std::string> commands;
  1143. std::vector<std::string> depends;
  1144. std::string emptyCommand = this->GlobalGenerator->GetEmptyRuleHackCommand();
  1145. if(!emptyCommand.empty())
  1146. {
  1147. commands.push_back(emptyCommand);
  1148. }
  1149. depends.push_back(in);
  1150. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  1151. out, depends, commands,
  1152. symbolic);
  1153. // Register the extra output as paired with the first output so that
  1154. // the check-build-system step will remove the primary output if any
  1155. // extra outputs are missing. This forces the rule to regenerate
  1156. // all outputs.
  1157. this->AddMultipleOutputPair(out, in);
  1158. }
  1159. //----------------------------------------------------------------------------
  1160. void
  1161. cmMakefileTargetGenerator::AppendProgress(std::vector<std::string>& commands)
  1162. {
  1163. this->NumberOfProgressActions++;
  1164. if(this->NoRuleMessages)
  1165. {
  1166. return;
  1167. }
  1168. std::string progressDir = this->Makefile->GetHomeOutputDirectory();
  1169. progressDir += cmake::GetCMakeFilesDirectory();
  1170. cmOStringStream progCmd;
  1171. progCmd << "$(CMAKE_COMMAND) -E cmake_progress_report ";
  1172. progCmd << this->LocalGenerator->Convert(progressDir.c_str(),
  1173. cmLocalGenerator::FULL,
  1174. cmLocalGenerator::SHELL);
  1175. progCmd << " $(CMAKE_PROGRESS_" << this->NumberOfProgressActions << ")";
  1176. commands.push_back(progCmd.str());
  1177. }
  1178. //----------------------------------------------------------------------------
  1179. void
  1180. cmMakefileTargetGenerator
  1181. ::WriteObjectsVariable(std::string& variableName,
  1182. std::string& variableNameExternal)
  1183. {
  1184. // Write a make variable assignment that lists all objects for the
  1185. // target.
  1186. variableName =
  1187. this->LocalGenerator->CreateMakeVariable(this->Target->GetName(),
  1188. "_OBJECTS");
  1189. *this->BuildFileStream
  1190. << "# Object files for target " << this->Target->GetName() << "\n"
  1191. << variableName.c_str() << " =";
  1192. std::string object;
  1193. const char* objName =
  1194. this->Makefile->GetDefinition("CMAKE_NO_QUOTED_OBJECTS");
  1195. const char* lineContinue =
  1196. this->Makefile->GetDefinition("CMAKE_MAKE_LINE_CONTINUE");
  1197. if(!lineContinue)
  1198. {
  1199. lineContinue = "\\";
  1200. }
  1201. for(std::vector<std::string>::const_iterator i = this->Objects.begin();
  1202. i != this->Objects.end(); ++i)
  1203. {
  1204. *this->BuildFileStream << " " << lineContinue << "\n";
  1205. if(objName)
  1206. {
  1207. *this->BuildFileStream <<
  1208. this->Convert(i->c_str(), cmLocalGenerator::START_OUTPUT,
  1209. cmLocalGenerator::MAKEFILE);
  1210. }
  1211. else
  1212. {
  1213. *this->BuildFileStream <<
  1214. this->LocalGenerator->ConvertToQuotedOutputPath(i->c_str());
  1215. }
  1216. }
  1217. *this->BuildFileStream << "\n";
  1218. // Write a make variable assignment that lists all external objects
  1219. // for the target.
  1220. variableNameExternal =
  1221. this->LocalGenerator->CreateMakeVariable(this->Target->GetName(),
  1222. "_EXTERNAL_OBJECTS");
  1223. *this->BuildFileStream
  1224. << "\n"
  1225. << "# External object files for target "
  1226. << this->Target->GetName() << "\n"
  1227. << variableNameExternal.c_str() << " =";
  1228. for(std::vector<std::string>::const_iterator i =
  1229. this->ExternalObjects.begin();
  1230. i != this->ExternalObjects.end(); ++i)
  1231. {
  1232. object = this->Convert(i->c_str(),cmLocalGenerator::START_OUTPUT);
  1233. *this->BuildFileStream
  1234. << " " << lineContinue << "\n"
  1235. << this->Makefile->GetSafeDefinition("CMAKE_OBJECT_NAME");
  1236. if(objName)
  1237. {
  1238. *this->BuildFileStream <<
  1239. this->Convert(i->c_str(), cmLocalGenerator::START_OUTPUT,
  1240. cmLocalGenerator::MAKEFILE);
  1241. }
  1242. else
  1243. {
  1244. *this->BuildFileStream <<
  1245. this->LocalGenerator->ConvertToQuotedOutputPath(i->c_str());
  1246. }
  1247. }
  1248. *this->BuildFileStream << "\n" << "\n";
  1249. }
  1250. //----------------------------------------------------------------------------
  1251. void
  1252. cmMakefileTargetGenerator
  1253. ::WriteObjectsString(std::string& buildObjs)
  1254. {
  1255. std::vector<std::string> objStrings;
  1256. this->WriteObjectsStrings(objStrings);
  1257. buildObjs = objStrings[0];
  1258. }
  1259. //----------------------------------------------------------------------------
  1260. class cmMakefileTargetGeneratorObjectStrings
  1261. {
  1262. public:
  1263. cmMakefileTargetGeneratorObjectStrings(std::vector<std::string>& strings,
  1264. cmLocalUnixMakefileGenerator3* lg,
  1265. std::string::size_type limit):
  1266. Strings(strings), LocalGenerator(lg), LengthLimit(limit)
  1267. {
  1268. this->Space = "";
  1269. }
  1270. void Feed(std::string const& obj)
  1271. {
  1272. // Construct the name of the next object.
  1273. this->NextObject =
  1274. this->LocalGenerator->Convert(obj.c_str(),
  1275. cmLocalGenerator::START_OUTPUT,
  1276. cmLocalGenerator::RESPONSE);
  1277. // Roll over to next string if the limit will be exceeded.
  1278. if(this->LengthLimit != std::string::npos &&
  1279. (this->CurrentString.length() + 1 + this->NextObject.length()
  1280. > this->LengthLimit))
  1281. {
  1282. this->Strings.push_back(this->CurrentString);
  1283. this->CurrentString = "";
  1284. this->Space = "";
  1285. }
  1286. // Separate from previous object.
  1287. this->CurrentString += this->Space;
  1288. this->Space = " ";
  1289. // Append this object.
  1290. this->CurrentString += this->NextObject;
  1291. }
  1292. void Done()
  1293. {
  1294. this->Strings.push_back(this->CurrentString);
  1295. }
  1296. private:
  1297. std::vector<std::string>& Strings;
  1298. cmLocalUnixMakefileGenerator3* LocalGenerator;
  1299. std::string::size_type LengthLimit;
  1300. std::string CurrentString;
  1301. std::string NextObject;
  1302. const char* Space;
  1303. };
  1304. //----------------------------------------------------------------------------
  1305. void
  1306. cmMakefileTargetGenerator
  1307. ::WriteObjectsStrings(std::vector<std::string>& objStrings,
  1308. std::string::size_type limit)
  1309. {
  1310. cmMakefileTargetGeneratorObjectStrings
  1311. helper(objStrings, this->LocalGenerator, limit);
  1312. for(std::vector<std::string>::const_iterator i = this->Objects.begin();
  1313. i != this->Objects.end(); ++i)
  1314. {
  1315. helper.Feed(*i);
  1316. }
  1317. for(std::vector<std::string>::const_iterator i =
  1318. this->ExternalObjects.begin();
  1319. i != this->ExternalObjects.end(); ++i)
  1320. {
  1321. helper.Feed(*i);
  1322. }
  1323. helper.Done();
  1324. }
  1325. //----------------------------------------------------------------------------
  1326. void cmMakefileTargetGenerator::WriteTargetDriverRule(const char* main_output,
  1327. bool relink)
  1328. {
  1329. // Compute the name of the driver target.
  1330. std::string dir =
  1331. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  1332. std::string buildTargetRuleName = dir;
  1333. buildTargetRuleName += relink?"/preinstall":"/build";
  1334. buildTargetRuleName = this->Convert(buildTargetRuleName.c_str(),
  1335. cmLocalGenerator::HOME_OUTPUT,
  1336. cmLocalGenerator::UNCHANGED);
  1337. // Build the list of target outputs to drive.
  1338. std::vector<std::string> depends;
  1339. if(main_output)
  1340. {
  1341. depends.push_back(main_output);
  1342. }
  1343. const char* comment = 0;
  1344. if(relink)
  1345. {
  1346. // Setup the comment for the preinstall driver.
  1347. comment = "Rule to relink during preinstall.";
  1348. }
  1349. else
  1350. {
  1351. // Setup the comment for the main build driver.
  1352. comment = "Rule to build all files generated by this target.";
  1353. // Make sure all custom command outputs in this target are built.
  1354. if(this->CustomCommandDriver == OnBuild)
  1355. {
  1356. this->DriveCustomCommands(depends);
  1357. }
  1358. // Make sure the extra files are built.
  1359. for(std::set<cmStdString>::const_iterator i = this->ExtraFiles.begin();
  1360. i != this->ExtraFiles.end(); ++i)
  1361. {
  1362. depends.push_back(*i);
  1363. }
  1364. }
  1365. // Write the driver rule.
  1366. std::vector<std::string> no_commands;
  1367. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, comment,
  1368. buildTargetRuleName.c_str(),
  1369. depends, no_commands, true);
  1370. }
  1371. //----------------------------------------------------------------------------
  1372. std::string cmMakefileTargetGenerator::GetFrameworkFlags(std::string const& l)
  1373. {
  1374. if(!this->Makefile->IsOn("APPLE"))
  1375. {
  1376. return std::string();
  1377. }
  1378. std::string fwSearchFlagVar = "CMAKE_" + l + "_FRAMEWORK_SEARCH_FLAG";
  1379. const char* fwSearchFlag =
  1380. this->Makefile->GetDefinition(fwSearchFlagVar.c_str());
  1381. if(!(fwSearchFlag && *fwSearchFlag))
  1382. {
  1383. return std::string();
  1384. }
  1385. std::set<cmStdString> emitted;
  1386. #ifdef __APPLE__ /* don't insert this when crosscompiling e.g. to iphone */
  1387. emitted.insert("/System/Library/Frameworks");
  1388. #endif
  1389. std::vector<std::string> includes;
  1390. const char *config = this->Makefile->GetDefinition("CMAKE_BUILD_TYPE");
  1391. this->LocalGenerator->GetIncludeDirectories(includes,
  1392. this->GeneratorTarget,
  1393. "C", config);
  1394. // check all include directories for frameworks as this
  1395. // will already have added a -F for the framework
  1396. for(std::vector<std::string>::iterator i = includes.begin();
  1397. i != includes.end(); ++i)
  1398. {
  1399. if(this->Target->NameResolvesToFramework(i->c_str()))
  1400. {
  1401. std::string frameworkDir = *i;
  1402. frameworkDir += "/../";
  1403. frameworkDir = cmSystemTools::CollapseFullPath(frameworkDir.c_str());
  1404. emitted.insert(frameworkDir);
  1405. }
  1406. }
  1407. std::string flags;
  1408. const char* cfg = this->LocalGenerator->ConfigurationName.c_str();
  1409. if(cmComputeLinkInformation* cli = this->Target->GetLinkInformation(cfg))
  1410. {
  1411. std::vector<std::string> const& frameworks = cli->GetFrameworkPaths();
  1412. for(std::vector<std::string>::const_iterator i = frameworks.begin();
  1413. i != frameworks.end(); ++i)
  1414. {
  1415. if(emitted.insert(*i).second)
  1416. {
  1417. flags += fwSearchFlag;
  1418. flags += this->Convert(i->c_str(),
  1419. cmLocalGenerator::START_OUTPUT,
  1420. cmLocalGenerator::SHELL, true);
  1421. flags += " ";
  1422. }
  1423. }
  1424. }
  1425. return flags;
  1426. }
  1427. //----------------------------------------------------------------------------
  1428. void cmMakefileTargetGenerator
  1429. ::AppendTargetDepends(std::vector<std::string>& depends)
  1430. {
  1431. // Static libraries never depend on anything for linking.
  1432. if(this->Target->GetType() == cmTarget::STATIC_LIBRARY)
  1433. {
  1434. return;
  1435. }
  1436. // Loop over all library dependencies.
  1437. const char* cfg = this->LocalGenerator->ConfigurationName.c_str();
  1438. if(cmComputeLinkInformation* cli = this->Target->GetLinkInformation(cfg))
  1439. {
  1440. std::vector<std::string> const& libDeps = cli->GetDepends();
  1441. for(std::vector<std::string>::const_iterator j = libDeps.begin();
  1442. j != libDeps.end(); ++j)
  1443. {
  1444. depends.push_back(*j);
  1445. }
  1446. }
  1447. }
  1448. //----------------------------------------------------------------------------
  1449. void cmMakefileTargetGenerator
  1450. ::AppendObjectDepends(std::vector<std::string>& depends)
  1451. {
  1452. // Add dependencies on the compiled object files.
  1453. std::string relPath = this->LocalGenerator->GetHomeRelativeOutputPath();
  1454. std::string objTarget;
  1455. for(std::vector<std::string>::const_iterator obj = this->Objects.begin();
  1456. obj != this->Objects.end(); ++obj)
  1457. {
  1458. objTarget = relPath;
  1459. objTarget += *obj;
  1460. depends.push_back(objTarget);
  1461. }
  1462. // Add dependencies on the external object files.
  1463. for(std::vector<std::string>::const_iterator obj
  1464. = this->ExternalObjects.begin();
  1465. obj != this->ExternalObjects.end(); ++obj)
  1466. {
  1467. depends.push_back(*obj);
  1468. }
  1469. // Add a dependency on the rule file itself.
  1470. this->LocalGenerator->AppendRuleDepend(depends,
  1471. this->BuildFileNameFull.c_str());
  1472. }
  1473. //----------------------------------------------------------------------------
  1474. void cmMakefileTargetGenerator
  1475. ::AppendLinkDepends(std::vector<std::string>& depends)
  1476. {
  1477. this->AppendObjectDepends(depends);
  1478. // Add dependencies on targets that must be built first.
  1479. this->AppendTargetDepends(depends);
  1480. // Add a dependency on the link definitions file, if any.
  1481. if(!this->GeneratorTarget->ModuleDefinitionFile.empty())
  1482. {
  1483. depends.push_back(this->GeneratorTarget->ModuleDefinitionFile);
  1484. }
  1485. // Add user-specified dependencies.
  1486. if(const char* linkDepends =
  1487. this->Target->GetProperty("LINK_DEPENDS"))
  1488. {
  1489. cmSystemTools::ExpandListArgument(linkDepends, depends);
  1490. }
  1491. }
  1492. //----------------------------------------------------------------------------
  1493. std::string cmMakefileTargetGenerator::GetLinkRule(const char* linkRuleVar)
  1494. {
  1495. std::string linkRule = this->Makefile->GetRequiredDefinition(linkRuleVar);
  1496. if(this->Target->HasImplibGNUtoMS())
  1497. {
  1498. std::string ruleVar = "CMAKE_";
  1499. ruleVar += this->Target->GetLinkerLanguage(this->ConfigName);
  1500. ruleVar += "_GNUtoMS_RULE";
  1501. if(const char* rule = this->Makefile->GetDefinition(ruleVar.c_str()))
  1502. {
  1503. linkRule += rule;
  1504. }
  1505. }
  1506. return linkRule;
  1507. }
  1508. //----------------------------------------------------------------------------
  1509. void cmMakefileTargetGenerator
  1510. ::CloseFileStreams()
  1511. {
  1512. delete this->BuildFileStream;
  1513. delete this->InfoFileStream;
  1514. delete this->FlagFileStream;
  1515. }
  1516. void cmMakefileTargetGenerator::RemoveForbiddenFlags(const char* flagVar,
  1517. const char* linkLang,
  1518. std::string& linkFlags)
  1519. {
  1520. // check for language flags that are not allowed at link time, and
  1521. // remove them, -w on darwin for gcc -w -dynamiclib sends -w to libtool
  1522. // which fails, there may be more]
  1523. std::string removeFlags = "CMAKE_";
  1524. removeFlags += linkLang;
  1525. removeFlags += flagVar;
  1526. std::string removeflags =
  1527. this->Makefile->GetSafeDefinition(removeFlags.c_str());
  1528. std::vector<std::string> removeList;
  1529. cmSystemTools::ExpandListArgument(removeflags, removeList);
  1530. for(std::vector<std::string>::iterator i = removeList.begin();
  1531. i != removeList.end(); ++i)
  1532. {
  1533. std::string tmp;
  1534. std::string::size_type lastPosition = 0;
  1535. for(;;)
  1536. {
  1537. std::string::size_type position = linkFlags.find(*i, lastPosition);
  1538. if(position == std::string::npos)
  1539. {
  1540. tmp += linkFlags.substr(lastPosition);
  1541. break;
  1542. }
  1543. else
  1544. {
  1545. std::string::size_type prefixLength = position - lastPosition;
  1546. tmp += linkFlags.substr(lastPosition, prefixLength);
  1547. lastPosition = position + i->length();
  1548. bool validFlagStart = position == 0 ||
  1549. isspace(linkFlags[position - 1]);
  1550. bool validFlagEnd = lastPosition == linkFlags.size() ||
  1551. isspace(linkFlags[lastPosition]);
  1552. if(!validFlagStart || !validFlagEnd)
  1553. {
  1554. tmp += *i;
  1555. }
  1556. }
  1557. }
  1558. linkFlags = tmp;
  1559. }
  1560. }
  1561. //----------------------------------------------------------------------------
  1562. void
  1563. cmMakefileTargetGenerator
  1564. ::AddMultipleOutputPair(const char* depender, const char* dependee)
  1565. {
  1566. MultipleOutputPairsType::value_type p(depender, dependee);
  1567. this->MultipleOutputPairs.insert(p);
  1568. }
  1569. //----------------------------------------------------------------------------
  1570. void
  1571. cmMakefileTargetGenerator
  1572. ::CreateLinkScript(const char* name,
  1573. std::vector<std::string> const& link_commands,
  1574. std::vector<std::string>& makefile_commands,
  1575. std::vector<std::string>& makefile_depends)
  1576. {
  1577. // Create the link script file.
  1578. std::string linkScriptName = this->TargetBuildDirectoryFull;
  1579. linkScriptName += "/";
  1580. linkScriptName += name;
  1581. cmGeneratedFileStream linkScriptStream(linkScriptName.c_str());
  1582. linkScriptStream.SetCopyIfDifferent(true);
  1583. for(std::vector<std::string>::const_iterator cmd = link_commands.begin();
  1584. cmd != link_commands.end(); ++cmd)
  1585. {
  1586. // Do not write out empty commands or commands beginning in the
  1587. // shell no-op ":".
  1588. if(!cmd->empty() && (*cmd)[0] != ':')
  1589. {
  1590. linkScriptStream << *cmd << "\n";
  1591. }
  1592. }
  1593. // Create the makefile command to invoke the link script.
  1594. std::string link_command = "$(CMAKE_COMMAND) -E cmake_link_script ";
  1595. link_command += this->Convert(linkScriptName.c_str(),
  1596. cmLocalGenerator::START_OUTPUT,
  1597. cmLocalGenerator::SHELL);
  1598. link_command += " --verbose=$(VERBOSE)";
  1599. makefile_commands.push_back(link_command);
  1600. makefile_depends.push_back(linkScriptName);
  1601. }
  1602. //----------------------------------------------------------------------------
  1603. std::string
  1604. cmMakefileTargetGenerator
  1605. ::CreateResponseFile(const char* name, std::string const& options,
  1606. std::vector<std::string>& makefile_depends)
  1607. {
  1608. // Create the response file.
  1609. std::string responseFileNameFull = this->TargetBuildDirectoryFull;
  1610. responseFileNameFull += "/";
  1611. responseFileNameFull += name;
  1612. cmGeneratedFileStream responseStream(responseFileNameFull.c_str());
  1613. responseStream.SetCopyIfDifferent(true);
  1614. responseStream << options << "\n";
  1615. // Add a dependency so the target will rebuild when the set of
  1616. // objects changes.
  1617. makefile_depends.push_back(responseFileNameFull);
  1618. // Construct the name to be used on the command line.
  1619. std::string responseFileName = this->TargetBuildDirectory;
  1620. responseFileName += "/";
  1621. responseFileName += name;
  1622. return responseFileName;
  1623. }
  1624. //----------------------------------------------------------------------------
  1625. void
  1626. cmMakefileTargetGenerator
  1627. ::CreateObjectLists(bool useLinkScript, bool useArchiveRules,
  1628. bool useResponseFile, std::string& buildObjs,
  1629. std::vector<std::string>& makefile_depends)
  1630. {
  1631. std::string variableName;
  1632. std::string variableNameExternal;
  1633. this->WriteObjectsVariable(variableName, variableNameExternal);
  1634. if(useResponseFile)
  1635. {
  1636. // MSVC response files cannot exceed 128K.
  1637. std::string::size_type const responseFileLimit = 131000;
  1638. // Construct the individual object list strings.
  1639. std::vector<std::string> object_strings;
  1640. this->WriteObjectsStrings(object_strings, responseFileLimit);
  1641. // Lookup the response file reference flag.
  1642. std::string responseFlagVar = "CMAKE_";
  1643. responseFlagVar += this->Target->GetLinkerLanguage(this->ConfigName);
  1644. responseFlagVar += "_RESPONSE_FILE_LINK_FLAG";
  1645. const char* responseFlag =
  1646. this->Makefile->GetDefinition(responseFlagVar.c_str());
  1647. if(!responseFlag)
  1648. {
  1649. responseFlag = "@";
  1650. }
  1651. // Write a response file for each string.
  1652. const char* sep = "";
  1653. for(unsigned int i = 0; i < object_strings.size(); ++i)
  1654. {
  1655. // Number the response files.
  1656. char rsp[32];
  1657. sprintf(rsp, "objects%u.rsp", i+1);
  1658. // Create this response file.
  1659. std::string objects_rsp =
  1660. this->CreateResponseFile(rsp, object_strings[i], makefile_depends);
  1661. // Separate from previous response file references.
  1662. buildObjs += sep;
  1663. sep = " ";
  1664. // Reference the response file.
  1665. buildObjs += responseFlag;
  1666. buildObjs += this->Convert(objects_rsp.c_str(),
  1667. cmLocalGenerator::NONE,
  1668. cmLocalGenerator::SHELL);
  1669. }
  1670. }
  1671. else if(useLinkScript)
  1672. {
  1673. if(!useArchiveRules)
  1674. {
  1675. this->WriteObjectsString(buildObjs);
  1676. }
  1677. }
  1678. else
  1679. {
  1680. buildObjs = "$(";
  1681. buildObjs += variableName;
  1682. buildObjs += ") $(";
  1683. buildObjs += variableNameExternal;
  1684. buildObjs += ")";
  1685. }
  1686. }
  1687. //----------------------------------------------------------------------------
  1688. void cmMakefileTargetGenerator::AddIncludeFlags(std::string& flags,
  1689. const char* lang)
  1690. {
  1691. std::string responseVar = "CMAKE_";
  1692. responseVar += lang;
  1693. responseVar += "_USE_RESPONSE_FILE_FOR_INCLUDES";
  1694. bool useResponseFile = this->Makefile->IsOn(responseVar.c_str());
  1695. std::vector<std::string> includes;
  1696. const char *config = this->Makefile->GetDefinition("CMAKE_BUILD_TYPE");
  1697. this->LocalGenerator->GetIncludeDirectories(includes,
  1698. this->GeneratorTarget,
  1699. lang, config);
  1700. std::string includeFlags =
  1701. this->LocalGenerator->GetIncludeFlags(includes, this->GeneratorTarget,
  1702. lang, useResponseFile);
  1703. if(includeFlags.empty())
  1704. {
  1705. return;
  1706. }
  1707. if(useResponseFile)
  1708. {
  1709. std::string name = "includes_";
  1710. name += lang;
  1711. name += ".rsp";
  1712. std::string arg = "@" +
  1713. this->CreateResponseFile(name.c_str(), includeFlags,
  1714. this->FlagFileDepends[lang]);
  1715. this->LocalGenerator->AppendFlags(flags, arg.c_str());
  1716. }
  1717. else
  1718. {
  1719. this->LocalGenerator->AppendFlags(flags, includeFlags.c_str());
  1720. }
  1721. }
  1722. //----------------------------------------------------------------------------
  1723. const char* cmMakefileTargetGenerator::GetFortranModuleDirectory()
  1724. {
  1725. // Compute the module directory.
  1726. if(!this->FortranModuleDirectoryComputed)
  1727. {
  1728. const char* target_mod_dir =
  1729. this->Target->GetProperty("Fortran_MODULE_DIRECTORY");
  1730. const char* moddir_flag =
  1731. this->Makefile->GetDefinition("CMAKE_Fortran_MODDIR_FLAG");
  1732. if(target_mod_dir && moddir_flag)
  1733. {
  1734. // Compute the full path to the module directory.
  1735. if(cmSystemTools::FileIsFullPath(target_mod_dir))
  1736. {
  1737. // Already a full path.
  1738. this->FortranModuleDirectory = target_mod_dir;
  1739. }
  1740. else
  1741. {
  1742. // Interpret relative to the current output directory.
  1743. this->FortranModuleDirectory =
  1744. this->Makefile->GetCurrentOutputDirectory();
  1745. this->FortranModuleDirectory += "/";
  1746. this->FortranModuleDirectory += target_mod_dir;
  1747. }
  1748. // Make sure the module output directory exists.
  1749. cmSystemTools::MakeDirectory(this->FortranModuleDirectory.c_str());
  1750. }
  1751. this->FortranModuleDirectoryComputed = true;
  1752. }
  1753. // Return the computed directory.
  1754. if(this->FortranModuleDirectory.empty())
  1755. {
  1756. return 0;
  1757. }
  1758. else
  1759. {
  1760. return this->FortranModuleDirectory.c_str();
  1761. }
  1762. }
  1763. //----------------------------------------------------------------------------
  1764. void cmMakefileTargetGenerator::AddFortranFlags(std::string& flags)
  1765. {
  1766. // Enable module output if necessary.
  1767. if(const char* modout_flag =
  1768. this->Makefile->GetDefinition("CMAKE_Fortran_MODOUT_FLAG"))
  1769. {
  1770. this->LocalGenerator->AppendFlags(flags, modout_flag);
  1771. }
  1772. // Add a module output directory flag if necessary.
  1773. const char* mod_dir = this->GetFortranModuleDirectory();
  1774. if(!mod_dir)
  1775. {
  1776. mod_dir = this->Makefile->GetDefinition("CMAKE_Fortran_MODDIR_DEFAULT");
  1777. }
  1778. if(mod_dir)
  1779. {
  1780. const char* moddir_flag =
  1781. this->Makefile->GetRequiredDefinition("CMAKE_Fortran_MODDIR_FLAG");
  1782. std::string modflag = moddir_flag;
  1783. modflag += this->Convert(mod_dir,
  1784. cmLocalGenerator::START_OUTPUT,
  1785. cmLocalGenerator::SHELL);
  1786. this->LocalGenerator->AppendFlags(flags, modflag.c_str());
  1787. }
  1788. // If there is a separate module path flag then duplicate the
  1789. // include path with it. This compiler does not search the include
  1790. // path for modules.
  1791. if(const char* modpath_flag =
  1792. this->Makefile->GetDefinition("CMAKE_Fortran_MODPATH_FLAG"))
  1793. {
  1794. std::vector<std::string> includes;
  1795. const char *config = this->Makefile->GetDefinition("CMAKE_BUILD_TYPE");
  1796. this->LocalGenerator->GetIncludeDirectories(includes,
  1797. this->GeneratorTarget,
  1798. "C", config);
  1799. for(std::vector<std::string>::const_iterator idi = includes.begin();
  1800. idi != includes.end(); ++idi)
  1801. {
  1802. std::string flg = modpath_flag;
  1803. flg += this->Convert(idi->c_str(),
  1804. cmLocalGenerator::NONE,
  1805. cmLocalGenerator::SHELL);
  1806. this->LocalGenerator->AppendFlags(flags, flg.c_str());
  1807. }
  1808. }
  1809. }
  1810. //----------------------------------------------------------------------------
  1811. void cmMakefileTargetGenerator::AddModuleDefinitionFlag(std::string& flags)
  1812. {
  1813. if(this->GeneratorTarget->ModuleDefinitionFile.empty())
  1814. {
  1815. return;
  1816. }
  1817. // TODO: Create a per-language flag variable.
  1818. const char* defFileFlag =
  1819. this->Makefile->GetDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  1820. if(!defFileFlag)
  1821. {
  1822. return;
  1823. }
  1824. // Append the flag and value. Use ConvertToLinkReference to help
  1825. // vs6's "cl -link" pass it to the linker.
  1826. std::string flag = defFileFlag;
  1827. flag += (this->LocalGenerator->ConvertToLinkReference(
  1828. this->GeneratorTarget->ModuleDefinitionFile.c_str()));
  1829. this->LocalGenerator->AppendFlags(flags, flag.c_str());
  1830. }
  1831. //----------------------------------------------------------------------------
  1832. const char* cmMakefileTargetGenerator::GetFeature(const char* feature)
  1833. {
  1834. return this->Target->GetFeature(feature, this->ConfigName);
  1835. }
  1836. //----------------------------------------------------------------------------
  1837. bool cmMakefileTargetGenerator::GetFeatureAsBool(const char* feature)
  1838. {
  1839. return cmSystemTools::IsOn(this->GetFeature(feature));
  1840. }
  1841. //----------------------------------------------------------------------------
  1842. void cmMakefileTargetGenerator::AddFeatureFlags(
  1843. std::string& flags, const char* lang
  1844. )
  1845. {
  1846. // Add language-specific flags.
  1847. this->LocalGenerator->AddLanguageFlags(flags, lang, this->ConfigName);
  1848. if(this->GetFeatureAsBool("INTERPROCEDURAL_OPTIMIZATION"))
  1849. {
  1850. this->LocalGenerator->AppendFeatureOptions(flags, lang, "IPO");
  1851. }
  1852. }