cmMakefileTargetGenerator.cxx 70 KB

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