cmMakefileTargetGenerator.cxx 73 KB

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