cmMakefileTargetGenerator.cxx 72 KB

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