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. : OSXBundleGenerator(0)
  31. , MacOSXContentGenerator(0)
  32. {
  33. this->BuildFileStream = 0;
  34. this->InfoFileStream = 0;
  35. this->FlagFileStream = 0;
  36. this->CustomCommandDriver = OnBuild;
  37. this->FortranModuleDirectoryComputed = false;
  38. this->Target = target->Target;
  39. this->Makefile = this->Target->GetMakefile();
  40. this->LocalGenerator =
  41. static_cast<cmLocalUnixMakefileGenerator3*>(target->GetLocalGenerator());
  42. this->ConfigName = this->LocalGenerator->ConfigurationName.c_str();
  43. this->GlobalGenerator =
  44. static_cast<cmGlobalUnixMakefileGenerator3*>(
  45. this->LocalGenerator->GetGlobalGenerator());
  46. this->GeneratorTarget = target;
  47. cmake* cm = this->GlobalGenerator->GetCMakeInstance();
  48. this->NoRuleMessages = false;
  49. if(const char* ruleStatus = cm->GetState()
  50. ->GetGlobalProperty("RULE_MESSAGES"))
  51. {
  52. this->NoRuleMessages = cmSystemTools::IsOff(ruleStatus);
  53. }
  54. MacOSXContentGenerator = new MacOSXContentGeneratorType(this);
  55. }
  56. cmMakefileTargetGenerator::~cmMakefileTargetGenerator()
  57. {
  58. delete MacOSXContentGenerator;
  59. }
  60. cmMakefileTargetGenerator *
  61. cmMakefileTargetGenerator::New(cmGeneratorTarget *tgt)
  62. {
  63. cmMakefileTargetGenerator *result = 0;
  64. switch (tgt->GetType())
  65. {
  66. case cmTarget::EXECUTABLE:
  67. result = new cmMakefileExecutableTargetGenerator(tgt);
  68. break;
  69. case cmTarget::STATIC_LIBRARY:
  70. case cmTarget::SHARED_LIBRARY:
  71. case cmTarget::MODULE_LIBRARY:
  72. case cmTarget::OBJECT_LIBRARY:
  73. result = new cmMakefileLibraryTargetGenerator(tgt);
  74. break;
  75. case cmTarget::UTILITY:
  76. result = new cmMakefileUtilityTargetGenerator(tgt);
  77. break;
  78. default:
  79. return result;
  80. // break; /* unreachable */
  81. }
  82. return result;
  83. }
  84. //----------------------------------------------------------------------------
  85. void cmMakefileTargetGenerator::CreateRuleFile()
  86. {
  87. // Create a directory for this target.
  88. this->TargetBuildDirectory =
  89. this->LocalGenerator->GetTargetDirectory(*this->Target);
  90. this->TargetBuildDirectoryFull =
  91. this->LocalGenerator->ConvertToFullPath(this->TargetBuildDirectory);
  92. cmSystemTools::MakeDirectory(this->TargetBuildDirectoryFull.c_str());
  93. // Construct the rule file name.
  94. this->BuildFileName = this->TargetBuildDirectory;
  95. this->BuildFileName += "/build.make";
  96. this->BuildFileNameFull = this->TargetBuildDirectoryFull;
  97. this->BuildFileNameFull += "/build.make";
  98. // Construct the rule file name.
  99. this->ProgressFileNameFull = this->TargetBuildDirectoryFull;
  100. this->ProgressFileNameFull += "/progress.make";
  101. // reset the progress count
  102. this->NumberOfProgressActions = 0;
  103. // Open the rule file. This should be copy-if-different because the
  104. // rules may depend on this file itself.
  105. this->BuildFileStream =
  106. new cmGeneratedFileStream(this->BuildFileNameFull.c_str());
  107. this->BuildFileStream->SetCopyIfDifferent(true);
  108. if(!this->BuildFileStream)
  109. {
  110. return;
  111. }
  112. this->LocalGenerator->WriteDisclaimer(*this->BuildFileStream);
  113. if (this->GlobalGenerator->AllowDeleteOnError())
  114. {
  115. std::vector<std::string> no_depends;
  116. std::vector<std::string> no_commands;
  117. this->LocalGenerator->WriteMakeRule(
  118. *this->BuildFileStream, "Delete rule output on recipe failure.",
  119. ".DELETE_ON_ERROR", no_depends, no_commands, false);
  120. }
  121. this->LocalGenerator->WriteSpecialTargetsTop(*this->BuildFileStream);
  122. }
  123. //----------------------------------------------------------------------------
  124. void cmMakefileTargetGenerator::WriteTargetBuildRules()
  125. {
  126. const std::string& config =
  127. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  128. // write the custom commands for this target
  129. // Look for files registered for cleaning in this directory.
  130. if(const char* additional_clean_files =
  131. this->Makefile->GetProperty
  132. ("ADDITIONAL_MAKE_CLEAN_FILES"))
  133. {
  134. cmGeneratorExpression ge;
  135. cmsys::auto_ptr<cmCompiledGeneratorExpression> cge =
  136. ge.Parse(additional_clean_files);
  137. cmSystemTools::ExpandListArgument(cge->Evaluate(this->Makefile, config,
  138. false, this->Target, 0, 0),
  139. this->CleanFiles);
  140. }
  141. // add custom commands to the clean rules?
  142. const char* clean_no_custom =
  143. this->Makefile->GetProperty("CLEAN_NO_CUSTOM");
  144. bool clean = cmSystemTools::IsOff(clean_no_custom);
  145. // First generate the object rule files. Save a list of all object
  146. // files for this target.
  147. std::vector<cmSourceFile const*> customCommands;
  148. this->GeneratorTarget->GetCustomCommands(customCommands, config);
  149. for(std::vector<cmSourceFile const*>::const_iterator
  150. si = customCommands.begin();
  151. si != customCommands.end(); ++si)
  152. {
  153. cmCustomCommandGenerator ccg(*(*si)->GetCustomCommand(),
  154. this->ConfigName,
  155. this->Makefile);
  156. this->GenerateCustomRuleFile(ccg);
  157. if (clean)
  158. {
  159. const std::vector<std::string>& outputs = ccg.GetOutputs();
  160. for(std::vector<std::string>::const_iterator o = outputs.begin();
  161. o != outputs.end(); ++o)
  162. {
  163. this->CleanFiles.push_back
  164. (this->Convert(*o,
  165. cmLocalGenerator::START_OUTPUT,
  166. cmLocalGenerator::UNCHANGED));
  167. }
  168. }
  169. }
  170. std::vector<cmSourceFile const*> headerSources;
  171. this->GeneratorTarget->GetHeaderSources(headerSources, config);
  172. this->OSXBundleGenerator->GenerateMacOSXContentStatements(
  173. headerSources,
  174. this->MacOSXContentGenerator);
  175. std::vector<cmSourceFile const*> extraSources;
  176. this->GeneratorTarget->GetExtraSources(extraSources, config);
  177. this->OSXBundleGenerator->GenerateMacOSXContentStatements(
  178. extraSources,
  179. this->MacOSXContentGenerator);
  180. std::vector<cmSourceFile const*> externalObjects;
  181. this->GeneratorTarget->GetExternalObjects(externalObjects, config);
  182. for(std::vector<cmSourceFile const*>::const_iterator
  183. si = externalObjects.begin();
  184. si != externalObjects.end(); ++si)
  185. {
  186. this->ExternalObjects.push_back((*si)->GetFullPath());
  187. }
  188. std::vector<cmSourceFile const*> objectSources;
  189. this->GeneratorTarget->GetObjectSources(objectSources, config);
  190. for(std::vector<cmSourceFile const*>::const_iterator
  191. si = objectSources.begin(); si != objectSources.end(); ++si)
  192. {
  193. // Generate this object file's rule file.
  194. this->WriteObjectRuleFiles(**si);
  195. }
  196. }
  197. //----------------------------------------------------------------------------
  198. void cmMakefileTargetGenerator::WriteCommonCodeRules()
  199. {
  200. const char* root = (this->Makefile->IsOn("CMAKE_MAKE_INCLUDE_FROM_ROOT")?
  201. "$(CMAKE_BINARY_DIR)/" : "");
  202. // Include the dependencies for the target.
  203. std::string dependFileNameFull = this->TargetBuildDirectoryFull;
  204. dependFileNameFull += "/depend.make";
  205. *this->BuildFileStream
  206. << "# Include any dependencies generated for this target.\n"
  207. << this->GlobalGenerator->IncludeDirective << " " << root
  208. << this->Convert(dependFileNameFull,
  209. cmLocalGenerator::HOME_OUTPUT,
  210. cmLocalGenerator::MAKERULE)
  211. << "\n\n";
  212. if(!this->NoRuleMessages)
  213. {
  214. // Include the progress variables for the target.
  215. *this->BuildFileStream
  216. << "# Include the progress variables for this target.\n"
  217. << this->GlobalGenerator->IncludeDirective << " " << root
  218. << this->Convert(this->ProgressFileNameFull,
  219. cmLocalGenerator::HOME_OUTPUT,
  220. cmLocalGenerator::MAKERULE)
  221. << "\n\n";
  222. }
  223. // make sure the depend file exists
  224. if (!cmSystemTools::FileExists(dependFileNameFull.c_str()))
  225. {
  226. // Write an empty dependency file.
  227. cmGeneratedFileStream depFileStream(dependFileNameFull.c_str());
  228. depFileStream
  229. << "# Empty dependencies file for " << this->Target->GetName() << ".\n"
  230. << "# This may be replaced when dependencies are built." << std::endl;
  231. }
  232. // Open the flags file. This should be copy-if-different because the
  233. // rules may depend on this file itself.
  234. this->FlagFileNameFull = this->TargetBuildDirectoryFull;
  235. this->FlagFileNameFull += "/flags.make";
  236. this->FlagFileStream =
  237. new cmGeneratedFileStream(this->FlagFileNameFull.c_str());
  238. this->FlagFileStream->SetCopyIfDifferent(true);
  239. if(!this->FlagFileStream)
  240. {
  241. return;
  242. }
  243. this->LocalGenerator->WriteDisclaimer(*this->FlagFileStream);
  244. // Include the flags for the target.
  245. *this->BuildFileStream
  246. << "# Include the compile flags for this target's objects.\n"
  247. << this->GlobalGenerator->IncludeDirective << " " << root
  248. << this->Convert(this->FlagFileNameFull,
  249. cmLocalGenerator::HOME_OUTPUT,
  250. cmLocalGenerator::MAKERULE)
  251. << "\n\n";
  252. }
  253. //----------------------------------------------------------------------------
  254. std::string cmMakefileTargetGenerator::GetFlags(const std::string &l)
  255. {
  256. ByLanguageMap::iterator i = this->FlagsByLanguage.find(l);
  257. if (i == this->FlagsByLanguage.end())
  258. {
  259. std::string flags;
  260. const char *lang = l.c_str();
  261. // Add language feature flags.
  262. this->AddFeatureFlags(flags, lang);
  263. this->LocalGenerator->AddArchitectureFlags(flags, this->GeneratorTarget,
  264. lang, this->ConfigName);
  265. // Fortran-specific flags computed for this target.
  266. if(l == "Fortran")
  267. {
  268. this->AddFortranFlags(flags);
  269. }
  270. this->LocalGenerator->AddCMP0018Flags(flags, this->Target,
  271. lang, this->ConfigName);
  272. this->LocalGenerator->AddVisibilityPresetFlags(flags, this->Target,
  273. lang);
  274. // Add include directory flags.
  275. this->AddIncludeFlags(flags, lang);
  276. // Append old-style preprocessor definition flags.
  277. this->LocalGenerator->
  278. AppendFlags(flags, this->Makefile->GetDefineFlags());
  279. // Add include directory flags.
  280. this->LocalGenerator->
  281. AppendFlags(flags,this->GetFrameworkFlags(l));
  282. // Add target-specific flags.
  283. this->LocalGenerator->AddCompileOptions(flags, this->Target,
  284. lang, this->ConfigName);
  285. ByLanguageMap::value_type entry(l, flags);
  286. i = this->FlagsByLanguage.insert(entry).first;
  287. }
  288. return i->second;
  289. }
  290. std::string cmMakefileTargetGenerator::GetDefines(const std::string &l)
  291. {
  292. ByLanguageMap::iterator i = this->DefinesByLanguage.find(l);
  293. if (i == this->DefinesByLanguage.end())
  294. {
  295. std::set<std::string> defines;
  296. const char *lang = l.c_str();
  297. // Add the export symbol definition for shared library objects.
  298. if(const char* exportMacro = this->Target->GetExportMacro())
  299. {
  300. this->LocalGenerator->AppendDefines(defines, exportMacro);
  301. }
  302. // Add preprocessor definitions for this target and configuration.
  303. this->LocalGenerator->AddCompileDefinitions(defines, this->Target,
  304. this->LocalGenerator->ConfigurationName, l);
  305. std::string definesString;
  306. this->LocalGenerator->JoinDefines(defines, definesString, lang);
  307. ByLanguageMap::value_type entry(l, definesString);
  308. i = this->DefinesByLanguage.insert(entry).first;
  309. }
  310. return i->second;
  311. }
  312. void cmMakefileTargetGenerator::WriteTargetLanguageFlags()
  313. {
  314. // write language flags for target
  315. std::set<std::string> languages;
  316. this->Target->GetLanguages(languages,
  317. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE"));
  318. // put the compiler in the rules.make file so that if it changes
  319. // things rebuild
  320. for(std::set<std::string>::const_iterator l = languages.begin();
  321. l != languages.end(); ++l)
  322. {
  323. std::string compiler = "CMAKE_";
  324. compiler += *l;
  325. compiler += "_COMPILER";
  326. *this->FlagFileStream << "# compile " << *l << " with " <<
  327. this->Makefile->GetSafeDefinition(compiler) << "\n";
  328. }
  329. for(std::set<std::string>::const_iterator l = languages.begin();
  330. l != languages.end(); ++l)
  331. {
  332. std::string flags = this->GetFlags(*l);
  333. std::string defines = this->GetDefines(*l);
  334. // Escape comment characters so they do not terminate assignment.
  335. cmSystemTools::ReplaceString(flags, "#", "\\#");
  336. cmSystemTools::ReplaceString(defines, "#", "\\#");
  337. *this->FlagFileStream << *l << "_FLAGS = " << flags << "\n\n";
  338. *this->FlagFileStream << *l << "_DEFINES = " << defines << "\n\n";
  339. }
  340. }
  341. //----------------------------------------------------------------------------
  342. void
  343. cmMakefileTargetGenerator::MacOSXContentGeneratorType::operator()
  344. (cmSourceFile const& source, const char* pkgloc)
  345. {
  346. // Skip OS X content when not building a Framework or Bundle.
  347. if(!this->Generator->GetTarget()->IsBundleOnApple())
  348. {
  349. return;
  350. }
  351. std::string macdir =
  352. this->Generator->OSXBundleGenerator->InitMacOSXContentDirectory(pkgloc);
  353. // Get the input file location.
  354. std::string input = source.GetFullPath();
  355. // Get the output file location.
  356. std::string output = macdir;
  357. output += "/";
  358. output += cmSystemTools::GetFilenameName(input);
  359. this->Generator->CleanFiles.push_back(
  360. this->Generator->Convert(output,
  361. cmLocalGenerator::START_OUTPUT));
  362. output = this->Generator->Convert(output,
  363. cmLocalGenerator::HOME_OUTPUT);
  364. // Create a rule to copy the content into the bundle.
  365. std::vector<std::string> depends;
  366. std::vector<std::string> commands;
  367. depends.push_back(input);
  368. std::string copyEcho = "Copying OS X content ";
  369. copyEcho += output;
  370. this->Generator->LocalGenerator->AppendEcho(
  371. commands, copyEcho.c_str(),
  372. cmLocalUnixMakefileGenerator3::EchoBuild);
  373. std::string copyCommand = "$(CMAKE_COMMAND) -E copy ";
  374. copyCommand += this->Generator->Convert(input,
  375. cmLocalGenerator::NONE,
  376. cmLocalGenerator::SHELL);
  377. copyCommand += " ";
  378. copyCommand += this->Generator->Convert(output,
  379. cmLocalGenerator::NONE,
  380. cmLocalGenerator::SHELL);
  381. commands.push_back(copyCommand);
  382. this->Generator->LocalGenerator->WriteMakeRule(
  383. *this->Generator->BuildFileStream, 0,
  384. output,
  385. depends, commands, false);
  386. this->Generator->ExtraFiles.insert(output);
  387. }
  388. //----------------------------------------------------------------------------
  389. void cmMakefileTargetGenerator
  390. ::WriteObjectRuleFiles(cmSourceFile const& source)
  391. {
  392. // Identify the language of the source file.
  393. const std::string& lang =
  394. this->LocalGenerator->GetSourceFileLanguage(source);
  395. if(lang.empty())
  396. {
  397. // don't know anything about this file so skip it
  398. return;
  399. }
  400. // Get the full path name of the object file.
  401. std::string const& objectName = this->GeneratorTarget
  402. ->GetObjectName(&source);
  403. std::string obj = this->LocalGenerator->GetTargetDirectory(*this->Target);
  404. obj += "/";
  405. obj += objectName;
  406. // Avoid generating duplicate rules.
  407. if(this->ObjectFiles.find(obj) == this->ObjectFiles.end())
  408. {
  409. this->ObjectFiles.insert(obj);
  410. }
  411. else
  412. {
  413. std::ostringstream err;
  414. err << "Warning: Source file \""
  415. << source.GetFullPath()
  416. << "\" is listed multiple times for target \""
  417. << this->Target->GetName()
  418. << "\".";
  419. cmSystemTools::Message(err.str().c_str(), "Warning");
  420. return;
  421. }
  422. // Create the directory containing the object file. This may be a
  423. // subdirectory under the target's directory.
  424. std::string dir = cmSystemTools::GetFilenamePath(obj);
  425. cmSystemTools::MakeDirectory
  426. (this->LocalGenerator->ConvertToFullPath(dir).c_str());
  427. // Save this in the target's list of object files.
  428. this->Objects.push_back(obj);
  429. this->CleanFiles.push_back(obj);
  430. // TODO: Remove
  431. //std::string relativeObj
  432. //= this->LocalGenerator->GetHomeRelativeOutputPath();
  433. //relativeObj += obj;
  434. // we compute some depends when writing the depend.make that we will also
  435. // use in the build.make, same with depMakeFile
  436. std::vector<std::string> depends;
  437. std::string depMakeFile;
  438. // generate the build rule file
  439. this->WriteObjectBuildFile(obj, lang, source, depends);
  440. // The object file should be checked for dependency integrity.
  441. std::string objFullPath = this->Makefile->GetCurrentBinaryDirectory();
  442. objFullPath += "/";
  443. objFullPath += obj;
  444. objFullPath =
  445. this->Convert(objFullPath, cmLocalGenerator::FULL);
  446. std::string srcFullPath =
  447. this->Convert(source.GetFullPath(), cmLocalGenerator::FULL);
  448. this->LocalGenerator->
  449. AddImplicitDepends(*this->Target, lang,
  450. objFullPath.c_str(),
  451. srcFullPath.c_str());
  452. }
  453. //----------------------------------------------------------------------------
  454. void
  455. cmMakefileTargetGenerator
  456. ::AppendFortranFormatFlags(std::string& flags, cmSourceFile const& source)
  457. {
  458. const char* srcfmt = source.GetProperty("Fortran_FORMAT");
  459. cmLocalGenerator::FortranFormat format =
  460. this->LocalGenerator->GetFortranFormat(srcfmt);
  461. if(format == cmLocalGenerator::FortranFormatNone)
  462. {
  463. const char* tgtfmt = this->Target->GetProperty("Fortran_FORMAT");
  464. format = this->LocalGenerator->GetFortranFormat(tgtfmt);
  465. }
  466. const char* var = 0;
  467. switch (format)
  468. {
  469. case cmLocalGenerator::FortranFormatFixed:
  470. var = "CMAKE_Fortran_FORMAT_FIXED_FLAG"; break;
  471. case cmLocalGenerator::FortranFormatFree:
  472. var = "CMAKE_Fortran_FORMAT_FREE_FLAG"; break;
  473. default: break;
  474. }
  475. if(var)
  476. {
  477. this->LocalGenerator->AppendFlags(
  478. flags, this->Makefile->GetDefinition(var));
  479. }
  480. }
  481. //----------------------------------------------------------------------------
  482. void
  483. cmMakefileTargetGenerator
  484. ::WriteObjectBuildFile(std::string &obj,
  485. const std::string& lang,
  486. cmSourceFile const& source,
  487. std::vector<std::string>& depends)
  488. {
  489. this->LocalGenerator->AppendRuleDepend(depends,
  490. this->FlagFileNameFull.c_str());
  491. this->LocalGenerator->AppendRuleDepends(depends,
  492. this->FlagFileDepends[lang]);
  493. // generate the depend scanning rule
  494. this->WriteObjectDependRules(source, depends);
  495. std::string relativeObj = this->LocalGenerator->GetHomeRelativeOutputPath();
  496. relativeObj += obj;
  497. // Write the build rule.
  498. // Build the set of compiler flags.
  499. std::string flags;
  500. // Add language-specific flags.
  501. std::string langFlags = "$(";
  502. langFlags += lang;
  503. langFlags += "_FLAGS)";
  504. this->LocalGenerator->AppendFlags(flags, langFlags);
  505. std::string configUpper =
  506. cmSystemTools::UpperCase(this->LocalGenerator->ConfigurationName);
  507. // Add Fortran format flags.
  508. if(lang == "Fortran")
  509. {
  510. this->AppendFortranFormatFlags(flags, source);
  511. }
  512. // Add flags from source file properties.
  513. if (source.GetProperty("COMPILE_FLAGS"))
  514. {
  515. this->LocalGenerator->AppendFlags
  516. (flags, source.GetProperty("COMPILE_FLAGS"));
  517. *this->FlagFileStream << "# Custom flags: "
  518. << relativeObj << "_FLAGS = "
  519. << source.GetProperty("COMPILE_FLAGS")
  520. << "\n"
  521. << "\n";
  522. }
  523. // Add language-specific defines.
  524. std::set<std::string> defines;
  525. // Add source-sepcific preprocessor definitions.
  526. if(const char* compile_defs = source.GetProperty("COMPILE_DEFINITIONS"))
  527. {
  528. this->LocalGenerator->AppendDefines(defines, compile_defs);
  529. *this->FlagFileStream << "# Custom defines: "
  530. << relativeObj << "_DEFINES = "
  531. << compile_defs << "\n"
  532. << "\n";
  533. }
  534. std::string defPropName = "COMPILE_DEFINITIONS_";
  535. defPropName += configUpper;
  536. if(const char* config_compile_defs =
  537. source.GetProperty(defPropName))
  538. {
  539. this->LocalGenerator->AppendDefines(defines, config_compile_defs);
  540. *this->FlagFileStream
  541. << "# Custom defines: "
  542. << relativeObj << "_DEFINES_" << configUpper
  543. << " = " << config_compile_defs << "\n"
  544. << "\n";
  545. }
  546. // Get the output paths for source and object files.
  547. std::string sourceFile = this->Convert(source.GetFullPath(),
  548. cmLocalGenerator::NONE,
  549. cmLocalGenerator::SHELL);
  550. // Construct the build message.
  551. std::vector<std::string> no_commands;
  552. std::vector<std::string> commands;
  553. // add in a progress call if needed
  554. this->NumberOfProgressActions++;
  555. if(!this->NoRuleMessages)
  556. {
  557. cmLocalUnixMakefileGenerator3::EchoProgress progress;
  558. this->MakeEchoProgress(progress);
  559. std::string buildEcho = "Building ";
  560. buildEcho += lang;
  561. buildEcho += " object ";
  562. buildEcho += relativeObj;
  563. this->LocalGenerator->AppendEcho
  564. (commands, buildEcho.c_str(), cmLocalUnixMakefileGenerator3::EchoBuild,
  565. &progress);
  566. }
  567. std::string targetOutPathReal;
  568. std::string targetOutPathPDB;
  569. std::string targetOutPathCompilePDB;
  570. {
  571. std::string targetFullPathReal;
  572. std::string targetFullPathPDB;
  573. std::string targetFullPathCompilePDB;
  574. if(this->Target->GetType() == cmTarget::EXECUTABLE ||
  575. this->Target->GetType() == cmTarget::STATIC_LIBRARY ||
  576. this->Target->GetType() == cmTarget::SHARED_LIBRARY ||
  577. this->Target->GetType() == cmTarget::MODULE_LIBRARY)
  578. {
  579. targetFullPathReal =
  580. this->Target->GetFullPath(this->ConfigName, false, true);
  581. targetFullPathPDB = this->Target->GetPDBDirectory(this->ConfigName);
  582. targetFullPathPDB += "/";
  583. targetFullPathPDB += this->Target->GetPDBName(this->ConfigName);
  584. }
  585. if(this->Target->GetType() <= cmTarget::OBJECT_LIBRARY)
  586. {
  587. targetFullPathCompilePDB =
  588. this->Target->GetCompilePDBPath(this->ConfigName);
  589. if(targetFullPathCompilePDB.empty())
  590. {
  591. targetFullPathCompilePDB = this->Target->GetSupportDirectory() + "/";
  592. }
  593. }
  594. targetOutPathReal = this->Convert(targetFullPathReal,
  595. cmLocalGenerator::START_OUTPUT,
  596. cmLocalGenerator::SHELL);
  597. targetOutPathPDB =
  598. this->Convert(targetFullPathPDB,cmLocalGenerator::NONE,
  599. cmLocalGenerator::SHELL);
  600. targetOutPathCompilePDB =
  601. this->Convert(targetFullPathCompilePDB,
  602. cmLocalGenerator::START_OUTPUT,
  603. cmLocalGenerator::SHELL);
  604. if (this->LocalGenerator->IsMinGWMake() &&
  605. cmHasLiteralSuffix(targetOutPathCompilePDB, "\\"))
  606. {
  607. // mingw32-make incorrectly interprets 'a\ b c' as 'a b' and 'c'
  608. // (but 'a\ b "c"' as 'a\', 'b', and 'c'!). Workaround this by
  609. // avoiding a trailing backslash in the argument.
  610. targetOutPathCompilePDB[targetOutPathCompilePDB.size()-1] = '/';
  611. }
  612. }
  613. cmLocalGenerator::RuleVariables vars;
  614. vars.RuleLauncher = "RULE_LAUNCH_COMPILE";
  615. vars.CMTarget = this->Target;
  616. vars.Language = lang.c_str();
  617. vars.Target = targetOutPathReal.c_str();
  618. vars.TargetPDB = targetOutPathPDB.c_str();
  619. vars.TargetCompilePDB = targetOutPathCompilePDB.c_str();
  620. vars.Source = sourceFile.c_str();
  621. std::string shellObj =
  622. this->Convert(obj,
  623. cmLocalGenerator::NONE,
  624. cmLocalGenerator::SHELL);
  625. vars.Object = shellObj.c_str();
  626. std::string objectDir = this->Target->GetSupportDirectory();
  627. objectDir = this->Convert(objectDir,
  628. cmLocalGenerator::START_OUTPUT,
  629. cmLocalGenerator::SHELL);
  630. vars.ObjectDir = objectDir.c_str();
  631. std::string objectFileDir = cmSystemTools::GetFilenamePath(obj);
  632. objectFileDir = this->Convert(objectFileDir,
  633. cmLocalGenerator::START_OUTPUT,
  634. cmLocalGenerator::SHELL);
  635. vars.ObjectFileDir = objectFileDir.c_str();
  636. vars.Flags = flags.c_str();
  637. std::string definesString = "$(";
  638. definesString += lang;
  639. definesString += "_DEFINES)";
  640. this->LocalGenerator->JoinDefines(defines, definesString, lang);
  641. vars.Defines = definesString.c_str();
  642. // At the moment, it is assumed that C, C++, and Fortran have both
  643. // assembly and preprocessor capabilities. The same is true for the
  644. // ability to export compile commands
  645. bool lang_has_preprocessor = ((lang == "C") ||
  646. (lang == "CXX") ||
  647. (lang == "Fortran"));
  648. bool const lang_has_assembly = lang_has_preprocessor;
  649. bool const lang_can_export_cmds = lang_has_preprocessor;
  650. // Construct the compile rules.
  651. {
  652. std::string compileRuleVar = "CMAKE_";
  653. compileRuleVar += lang;
  654. compileRuleVar += "_COMPILE_OBJECT";
  655. std::string compileRule =
  656. this->Makefile->GetRequiredDefinition(compileRuleVar);
  657. std::vector<std::string> compileCommands;
  658. cmSystemTools::ExpandListArgument(compileRule, compileCommands);
  659. if (this->Makefile->IsOn("CMAKE_EXPORT_COMPILE_COMMANDS") &&
  660. lang_can_export_cmds && compileCommands.size() == 1)
  661. {
  662. std::string compileCommand = compileCommands[0];
  663. this->LocalGenerator->ExpandRuleVariables(compileCommand, vars);
  664. std::string workingDirectory =
  665. this->LocalGenerator->Convert(
  666. this->Makefile->GetCurrentBinaryDirectory(), cmLocalGenerator::FULL);
  667. compileCommand.replace(compileCommand.find(langFlags),
  668. langFlags.size(), this->GetFlags(lang));
  669. std::string langDefines = std::string("$(") + lang + "_DEFINES)";
  670. compileCommand.replace(compileCommand.find(langDefines),
  671. langDefines.size(), this->GetDefines(lang));
  672. this->GlobalGenerator->AddCXXCompileCommand(
  673. source.GetFullPath(), workingDirectory, compileCommand);
  674. }
  675. // Maybe insert an include-what-you-use runner.
  676. if (!compileCommands.empty() && (lang == "C" || lang == "CXX"))
  677. {
  678. std::string const iwyu_prop = lang + "_INCLUDE_WHAT_YOU_USE";
  679. const char *iwyu = this->Target->GetProperty(iwyu_prop);
  680. if (iwyu && *iwyu)
  681. {
  682. std::string run_iwyu = "$(CMAKE_COMMAND) -E __run_iwyu --iwyu=";
  683. run_iwyu += this->LocalGenerator->EscapeForShell(iwyu);
  684. run_iwyu += " -- ";
  685. compileCommands.front().insert(0, run_iwyu);
  686. }
  687. }
  688. // Maybe insert a compiler launcher like ccache or distcc
  689. if (!compileCommands.empty() && (lang == "C" || lang == "CXX"))
  690. {
  691. std::string const clauncher_prop = lang + "_COMPILER_LAUNCHER";
  692. const char *clauncher = this->Target->GetProperty(clauncher_prop);
  693. if (clauncher && *clauncher)
  694. {
  695. std::vector<std::string> launcher_cmd;
  696. cmSystemTools::ExpandListArgument(clauncher, launcher_cmd, true);
  697. for (std::vector<std::string>::iterator i = launcher_cmd.begin(),
  698. e = launcher_cmd.end(); i != e; ++i)
  699. {
  700. *i = this->LocalGenerator->EscapeForShell(*i);
  701. }
  702. std::string const& run_launcher = cmJoin(launcher_cmd, " ") + " ";
  703. compileCommands.front().insert(0, run_launcher);
  704. }
  705. }
  706. // Expand placeholders in the commands.
  707. for(std::vector<std::string>::iterator i = compileCommands.begin();
  708. i != compileCommands.end(); ++i)
  709. {
  710. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  711. }
  712. // Change the command working directory to the local build tree.
  713. this->LocalGenerator->CreateCDCommand
  714. (compileCommands,
  715. this->Makefile->GetCurrentBinaryDirectory(),
  716. cmLocalGenerator::HOME_OUTPUT);
  717. commands.insert(commands.end(),
  718. compileCommands.begin(), compileCommands.end());
  719. }
  720. // Check for extra outputs created by the compilation.
  721. std::vector<std::string> outputs(1, relativeObj);
  722. if(const char* extra_outputs_str =
  723. source.GetProperty("OBJECT_OUTPUTS"))
  724. {
  725. // Register these as extra files to clean.
  726. cmSystemTools::ExpandListArgument(extra_outputs_str, outputs);
  727. this->CleanFiles.insert(this->CleanFiles.end(),
  728. outputs.begin() + 1, outputs.end());
  729. }
  730. // Write the rule.
  731. this->WriteMakeRule(*this->BuildFileStream, 0, outputs,
  732. depends, commands);
  733. bool do_preprocess_rules = lang_has_preprocessor &&
  734. this->LocalGenerator->GetCreatePreprocessedSourceRules();
  735. bool do_assembly_rules = lang_has_assembly &&
  736. this->LocalGenerator->GetCreateAssemblySourceRules();
  737. if(do_preprocess_rules || do_assembly_rules)
  738. {
  739. std::vector<std::string> force_depends;
  740. force_depends.push_back("cmake_force");
  741. std::string::size_type dot_pos = relativeObj.rfind(".");
  742. std::string relativeObjBase = relativeObj.substr(0, dot_pos);
  743. dot_pos = obj.rfind(".");
  744. std::string objBase = obj.substr(0, dot_pos);
  745. if(do_preprocess_rules)
  746. {
  747. commands.clear();
  748. std::string relativeObjI = relativeObjBase + ".i";
  749. std::string objI = objBase + ".i";
  750. std::string preprocessEcho = "Preprocessing ";
  751. preprocessEcho += lang;
  752. preprocessEcho += " source to ";
  753. preprocessEcho += objI;
  754. this->LocalGenerator->AppendEcho(
  755. commands, preprocessEcho.c_str(),
  756. cmLocalUnixMakefileGenerator3::EchoBuild
  757. );
  758. std::string preprocessRuleVar = "CMAKE_";
  759. preprocessRuleVar += lang;
  760. preprocessRuleVar += "_CREATE_PREPROCESSED_SOURCE";
  761. if(const char* preprocessRule =
  762. this->Makefile->GetDefinition(preprocessRuleVar))
  763. {
  764. std::vector<std::string> preprocessCommands;
  765. cmSystemTools::ExpandListArgument(preprocessRule, preprocessCommands);
  766. std::string shellObjI =
  767. this->Convert(objI,
  768. cmLocalGenerator::NONE,
  769. cmLocalGenerator::SHELL);
  770. vars.PreprocessedSource = shellObjI.c_str();
  771. // Expand placeholders in the commands.
  772. for(std::vector<std::string>::iterator i = preprocessCommands.begin();
  773. i != preprocessCommands.end(); ++i)
  774. {
  775. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  776. }
  777. this->LocalGenerator->CreateCDCommand
  778. (preprocessCommands,
  779. this->Makefile->GetCurrentBinaryDirectory(),
  780. cmLocalGenerator::HOME_OUTPUT);
  781. commands.insert(commands.end(),
  782. preprocessCommands.begin(),
  783. preprocessCommands.end());
  784. }
  785. else
  786. {
  787. std::string cmd = "$(CMAKE_COMMAND) -E cmake_unimplemented_variable ";
  788. cmd += preprocessRuleVar;
  789. commands.push_back(cmd);
  790. }
  791. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  792. relativeObjI,
  793. force_depends, commands, false);
  794. }
  795. if(do_assembly_rules)
  796. {
  797. commands.clear();
  798. std::string relativeObjS = relativeObjBase + ".s";
  799. std::string objS = objBase + ".s";
  800. std::string assemblyEcho = "Compiling ";
  801. assemblyEcho += lang;
  802. assemblyEcho += " source to assembly ";
  803. assemblyEcho += objS;
  804. this->LocalGenerator->AppendEcho(
  805. commands, assemblyEcho.c_str(),
  806. cmLocalUnixMakefileGenerator3::EchoBuild
  807. );
  808. std::string assemblyRuleVar = "CMAKE_";
  809. assemblyRuleVar += lang;
  810. assemblyRuleVar += "_CREATE_ASSEMBLY_SOURCE";
  811. if(const char* assemblyRule =
  812. this->Makefile->GetDefinition(assemblyRuleVar))
  813. {
  814. std::vector<std::string> assemblyCommands;
  815. cmSystemTools::ExpandListArgument(assemblyRule, assemblyCommands);
  816. std::string shellObjS =
  817. this->Convert(objS,
  818. cmLocalGenerator::NONE,
  819. cmLocalGenerator::SHELL);
  820. vars.AssemblySource = shellObjS.c_str();
  821. // Expand placeholders in the commands.
  822. for(std::vector<std::string>::iterator i = assemblyCommands.begin();
  823. i != assemblyCommands.end(); ++i)
  824. {
  825. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  826. }
  827. this->LocalGenerator->CreateCDCommand
  828. (assemblyCommands,
  829. this->Makefile->GetCurrentBinaryDirectory(),
  830. cmLocalGenerator::HOME_OUTPUT);
  831. commands.insert(commands.end(),
  832. assemblyCommands.begin(),
  833. assemblyCommands.end());
  834. }
  835. else
  836. {
  837. std::string cmd = "$(CMAKE_COMMAND) -E cmake_unimplemented_variable ";
  838. cmd += assemblyRuleVar;
  839. commands.push_back(cmd);
  840. }
  841. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  842. relativeObjS,
  843. force_depends, commands, false);
  844. }
  845. }
  846. // If the language needs provides-requires mode, create the
  847. // corresponding targets.
  848. std::string objectRequires = relativeObj;
  849. objectRequires += ".requires";
  850. std::vector<std::string> p_depends;
  851. // always provide an empty requires target
  852. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  853. objectRequires, p_depends,
  854. no_commands, true);
  855. // write a build rule to recursively build what this obj provides
  856. std::string objectProvides = relativeObj;
  857. objectProvides += ".provides";
  858. std::string temp = relativeObj;
  859. temp += ".provides.build";
  860. std::vector<std::string> r_commands;
  861. std::string tgtMakefileName =
  862. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  863. tgtMakefileName += "/build.make";
  864. r_commands.push_back
  865. (this->LocalGenerator->GetRecursiveMakeCall(tgtMakefileName.c_str(),
  866. temp));
  867. p_depends.clear();
  868. p_depends.push_back(objectRequires);
  869. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  870. objectProvides, p_depends,
  871. r_commands, true);
  872. // write the provides.build rule dependency on the obj file
  873. p_depends.clear();
  874. p_depends.push_back(relativeObj);
  875. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  876. temp, p_depends, no_commands,
  877. false);
  878. }
  879. //----------------------------------------------------------------------------
  880. void cmMakefileTargetGenerator::WriteTargetRequiresRules()
  881. {
  882. std::vector<std::string> depends;
  883. std::vector<std::string> no_commands;
  884. // Construct the name of the dependency generation target.
  885. std::string depTarget =
  886. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  887. depTarget += "/requires";
  888. // This target drives dependency generation for all object files.
  889. std::string relPath = this->LocalGenerator->GetHomeRelativeOutputPath();
  890. std::string objTarget;
  891. for(std::vector<std::string>::const_iterator obj = this->Objects.begin();
  892. obj != this->Objects.end(); ++obj)
  893. {
  894. objTarget = relPath;
  895. objTarget += *obj;
  896. objTarget += ".requires";
  897. depends.push_back(objTarget);
  898. }
  899. // Write the rule.
  900. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  901. depTarget,
  902. depends, no_commands, true);
  903. }
  904. //----------------------------------------------------------------------------
  905. void cmMakefileTargetGenerator::WriteTargetCleanRules()
  906. {
  907. std::vector<std::string> depends;
  908. std::vector<std::string> commands;
  909. // Construct the clean target name.
  910. std::string cleanTarget =
  911. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  912. cleanTarget += "/clean";
  913. // Construct the clean command.
  914. this->LocalGenerator->AppendCleanCommand(commands, this->CleanFiles,
  915. *this->Target);
  916. this->LocalGenerator->CreateCDCommand
  917. (commands,
  918. this->Makefile->GetCurrentBinaryDirectory(),
  919. cmLocalGenerator::HOME_OUTPUT);
  920. // Write the rule.
  921. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  922. cleanTarget,
  923. depends, commands, true);
  924. }
  925. //----------------------------------------------------------------------------
  926. bool cmMakefileTargetGenerator::WriteMakeRule(
  927. std::ostream& os,
  928. const char* comment,
  929. const std::vector<std::string>& outputs,
  930. const std::vector<std::string>& depends,
  931. const std::vector<std::string>& commands,
  932. bool in_help)
  933. {
  934. bool symbolic = false;
  935. if (outputs.size() == 0)
  936. {
  937. return symbolic;
  938. }
  939. // Check whether we need to bother checking for a symbolic output.
  940. bool need_symbolic = this->GlobalGenerator->GetNeedSymbolicMark();
  941. // Check whether the first output is marked as symbolic.
  942. if(need_symbolic)
  943. {
  944. if(cmSourceFile* sf = this->Makefile->GetSource(outputs[0]))
  945. {
  946. symbolic = sf->GetPropertyAsBool("SYMBOLIC");
  947. }
  948. }
  949. // We always attach the actual commands to the first output.
  950. this->LocalGenerator->WriteMakeRule(os, comment, outputs[0], depends,
  951. commands, symbolic, in_help);
  952. // For single outputs, we are done.
  953. if (outputs.size() == 1)
  954. {
  955. return symbolic;
  956. }
  957. // For multiple outputs, make the extra ones depend on the first one.
  958. std::vector<std::string> const output_depends(1, outputs[0]);
  959. for (std::vector<std::string>::const_iterator o = outputs.begin()+1;
  960. o != outputs.end(); ++o)
  961. {
  962. // Touch the extra output so "make" knows that it was updated,
  963. // but only if the output was acually created.
  964. std::string const out = this->Convert(*o, cmLocalGenerator::HOME_OUTPUT,
  965. cmLocalGenerator::SHELL);
  966. std::vector<std::string> output_commands;
  967. bool o_symbolic = false;
  968. if(need_symbolic)
  969. {
  970. if(cmSourceFile* sf = this->Makefile->GetSource(*o))
  971. {
  972. o_symbolic = sf->GetPropertyAsBool("SYMBOLIC");
  973. }
  974. }
  975. symbolic = symbolic && o_symbolic;
  976. if (!o_symbolic)
  977. {
  978. output_commands.push_back("@$(CMAKE_COMMAND) -E touch_nocreate " + out);
  979. }
  980. this->LocalGenerator->WriteMakeRule(os, 0, *o, output_depends,
  981. output_commands, o_symbolic, in_help);
  982. if (!o_symbolic)
  983. {
  984. // At build time, remove the first output if this one does not exist
  985. // so that "make" will rerun the real commands that create this one.
  986. MultipleOutputPairsType::value_type p(*o, outputs[0]);
  987. this->MultipleOutputPairs.insert(p);
  988. }
  989. }
  990. return symbolic;
  991. }
  992. //----------------------------------------------------------------------------
  993. void cmMakefileTargetGenerator::WriteTargetDependRules()
  994. {
  995. // must write the targets depend info file
  996. std::string dir = this->LocalGenerator->GetTargetDirectory(*this->Target);
  997. this->InfoFileNameFull = dir;
  998. this->InfoFileNameFull += "/DependInfo.cmake";
  999. this->InfoFileNameFull =
  1000. this->LocalGenerator->ConvertToFullPath(this->InfoFileNameFull);
  1001. this->InfoFileStream =
  1002. new cmGeneratedFileStream(this->InfoFileNameFull.c_str());
  1003. this->InfoFileStream->SetCopyIfDifferent(true);
  1004. if(!*this->InfoFileStream)
  1005. {
  1006. return;
  1007. }
  1008. this->LocalGenerator->
  1009. WriteDependLanguageInfo(*this->InfoFileStream,*this->Target);
  1010. // Store multiple output pairs in the depend info file.
  1011. if(!this->MultipleOutputPairs.empty())
  1012. {
  1013. *this->InfoFileStream
  1014. << "\n"
  1015. << "# Pairs of files generated by the same build rule.\n"
  1016. << "set(CMAKE_MULTIPLE_OUTPUT_PAIRS\n";
  1017. for(MultipleOutputPairsType::const_iterator pi =
  1018. this->MultipleOutputPairs.begin();
  1019. pi != this->MultipleOutputPairs.end(); ++pi)
  1020. {
  1021. *this->InfoFileStream
  1022. << " " << cmOutputConverter::EscapeForCMake(pi->first)
  1023. << " " << cmOutputConverter::EscapeForCMake(pi->second)
  1024. << "\n";
  1025. }
  1026. *this->InfoFileStream << " )\n\n";
  1027. }
  1028. // Store list of targets linked directly or transitively.
  1029. {
  1030. *this->InfoFileStream
  1031. << "\n"
  1032. << "# Targets to which this target links.\n"
  1033. << "set(CMAKE_TARGET_LINKED_INFO_FILES\n";
  1034. std::set<cmTarget const*> emitted;
  1035. const char* cfg = this->LocalGenerator->ConfigurationName.c_str();
  1036. if(cmComputeLinkInformation* cli = this->Target->GetLinkInformation(cfg))
  1037. {
  1038. cmComputeLinkInformation::ItemVector const& items = cli->GetItems();
  1039. for(cmComputeLinkInformation::ItemVector::const_iterator
  1040. i = items.begin(); i != items.end(); ++i)
  1041. {
  1042. cmTarget const* linkee = i->Target;
  1043. if(linkee && !linkee->IsImported()
  1044. // We can ignore the INTERFACE_LIBRARY items because
  1045. // Target->GetLinkInformation already processed their
  1046. // link interface and they don't have any output themselves.
  1047. && linkee->GetType() != cmTarget::INTERFACE_LIBRARY
  1048. && emitted.insert(linkee).second)
  1049. {
  1050. cmMakefile* mf = linkee->GetMakefile();
  1051. cmLocalGenerator* lg = mf->GetLocalGenerator();
  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->ConfigurationName.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->ConfigurationName.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. }