cmMakefileTargetGenerator.cxx 72 KB

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