cmMakefileTargetGenerator.cxx 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122
  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. const std::string& config =
  117. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  118. // write the custom commands for this target
  119. // Look for files registered for cleaning in this directory.
  120. if(const char* additional_clean_files =
  121. this->Makefile->GetProperty
  122. ("ADDITIONAL_MAKE_CLEAN_FILES"))
  123. {
  124. cmGeneratorExpression ge;
  125. cmsys::auto_ptr<cmCompiledGeneratorExpression> cge =
  126. ge.Parse(additional_clean_files);
  127. cmSystemTools::ExpandListArgument(cge->Evaluate(this->Makefile, config,
  128. false, this->Target, 0, 0),
  129. this->CleanFiles);
  130. }
  131. // add custom commands to the clean rules?
  132. const char* clean_no_custom =
  133. this->Makefile->GetProperty("CLEAN_NO_CUSTOM");
  134. bool clean = cmSystemTools::IsOff(clean_no_custom);
  135. // First generate the object rule files. Save a list of all object
  136. // files for this target.
  137. std::vector<cmSourceFile const*> customCommands;
  138. this->GeneratorTarget->GetCustomCommands(customCommands, config);
  139. for(std::vector<cmSourceFile const*>::const_iterator
  140. si = customCommands.begin();
  141. si != customCommands.end(); ++si)
  142. {
  143. cmCustomCommandGenerator ccg(*(*si)->GetCustomCommand(),
  144. this->ConfigName,
  145. this->Makefile);
  146. this->GenerateCustomRuleFile(ccg);
  147. if (clean)
  148. {
  149. const std::vector<std::string>& outputs = ccg.GetOutputs();
  150. for(std::vector<std::string>::const_iterator o = outputs.begin();
  151. o != outputs.end(); ++o)
  152. {
  153. this->CleanFiles.push_back
  154. (this->Convert(*o,
  155. cmLocalGenerator::START_OUTPUT,
  156. cmLocalGenerator::UNCHANGED));
  157. }
  158. }
  159. }
  160. std::vector<cmSourceFile const*> headerSources;
  161. this->GeneratorTarget->GetHeaderSources(headerSources, config);
  162. this->OSXBundleGenerator->GenerateMacOSXContentStatements(
  163. headerSources,
  164. this->MacOSXContentGenerator);
  165. std::vector<cmSourceFile const*> extraSources;
  166. this->GeneratorTarget->GetExtraSources(extraSources, config);
  167. this->OSXBundleGenerator->GenerateMacOSXContentStatements(
  168. extraSources,
  169. this->MacOSXContentGenerator);
  170. std::vector<cmSourceFile const*> externalObjects;
  171. this->GeneratorTarget->GetExternalObjects(externalObjects, config);
  172. for(std::vector<cmSourceFile const*>::const_iterator
  173. si = externalObjects.begin();
  174. si != externalObjects.end(); ++si)
  175. {
  176. this->ExternalObjects.push_back((*si)->GetFullPath());
  177. }
  178. std::vector<cmSourceFile const*> objectSources;
  179. this->GeneratorTarget->GetObjectSources(objectSources, config);
  180. for(std::vector<cmSourceFile const*>::const_iterator
  181. si = objectSources.begin(); si != objectSources.end(); ++si)
  182. {
  183. // Generate this object file's rule file.
  184. this->WriteObjectRuleFiles(**si);
  185. }
  186. }
  187. //----------------------------------------------------------------------------
  188. void cmMakefileTargetGenerator::WriteCommonCodeRules()
  189. {
  190. const char* root = (this->Makefile->IsOn("CMAKE_MAKE_INCLUDE_FROM_ROOT")?
  191. "$(CMAKE_BINARY_DIR)/" : "");
  192. // Include the dependencies for the target.
  193. std::string dependFileNameFull = this->TargetBuildDirectoryFull;
  194. dependFileNameFull += "/depend.make";
  195. *this->BuildFileStream
  196. << "# Include any dependencies generated for this target.\n"
  197. << this->LocalGenerator->IncludeDirective << " " << root
  198. << this->Convert(dependFileNameFull,
  199. cmLocalGenerator::HOME_OUTPUT,
  200. cmLocalGenerator::MAKEFILE)
  201. << "\n\n";
  202. if(!this->NoRuleMessages)
  203. {
  204. // Include the progress variables for the target.
  205. *this->BuildFileStream
  206. << "# Include the progress variables for this target.\n"
  207. << this->LocalGenerator->IncludeDirective << " " << root
  208. << this->Convert(this->ProgressFileNameFull,
  209. cmLocalGenerator::HOME_OUTPUT,
  210. cmLocalGenerator::MAKEFILE)
  211. << "\n\n";
  212. }
  213. // make sure the depend file exists
  214. if (!cmSystemTools::FileExists(dependFileNameFull.c_str()))
  215. {
  216. // Write an empty dependency file.
  217. cmGeneratedFileStream depFileStream(dependFileNameFull.c_str());
  218. depFileStream
  219. << "# Empty dependencies file for " << this->Target->GetName() << ".\n"
  220. << "# This may be replaced when dependencies are built." << std::endl;
  221. }
  222. // Open the flags file. This should be copy-if-different because the
  223. // rules may depend on this file itself.
  224. this->FlagFileNameFull = this->TargetBuildDirectoryFull;
  225. this->FlagFileNameFull += "/flags.make";
  226. this->FlagFileStream =
  227. new cmGeneratedFileStream(this->FlagFileNameFull.c_str());
  228. this->FlagFileStream->SetCopyIfDifferent(true);
  229. if(!this->FlagFileStream)
  230. {
  231. return;
  232. }
  233. this->LocalGenerator->WriteDisclaimer(*this->FlagFileStream);
  234. // Include the flags for the target.
  235. *this->BuildFileStream
  236. << "# Include the compile flags for this target's objects.\n"
  237. << this->LocalGenerator->IncludeDirective << " " << root
  238. << this->Convert(this->FlagFileNameFull,
  239. cmLocalGenerator::HOME_OUTPUT,
  240. cmLocalGenerator::MAKEFILE)
  241. << "\n\n";
  242. }
  243. //----------------------------------------------------------------------------
  244. std::string cmMakefileTargetGenerator::GetFlags(const std::string &l)
  245. {
  246. ByLanguageMap::iterator i = this->FlagsByLanguage.find(l);
  247. if (i == this->FlagsByLanguage.end())
  248. {
  249. std::string flags;
  250. const char *lang = l.c_str();
  251. // Add language feature flags.
  252. this->AddFeatureFlags(flags, lang);
  253. this->LocalGenerator->AddArchitectureFlags(flags, this->GeneratorTarget,
  254. lang, this->ConfigName);
  255. // Fortran-specific flags computed for this target.
  256. if(l == "Fortran")
  257. {
  258. this->AddFortranFlags(flags);
  259. }
  260. this->LocalGenerator->AddCMP0018Flags(flags, this->Target,
  261. lang, this->ConfigName);
  262. this->LocalGenerator->AddVisibilityPresetFlags(flags, this->Target,
  263. lang);
  264. // Add include directory flags.
  265. this->AddIncludeFlags(flags, lang);
  266. // Append old-style preprocessor definition flags.
  267. this->LocalGenerator->
  268. AppendFlags(flags, this->Makefile->GetDefineFlags());
  269. // Add include directory flags.
  270. this->LocalGenerator->
  271. AppendFlags(flags,this->GetFrameworkFlags(l));
  272. // Add target-specific flags.
  273. this->LocalGenerator->AddCompileOptions(flags, this->Target,
  274. lang, this->ConfigName);
  275. ByLanguageMap::value_type entry(l, flags);
  276. i = this->FlagsByLanguage.insert(entry).first;
  277. }
  278. return i->second;
  279. }
  280. std::string cmMakefileTargetGenerator::GetDefines(const std::string &l)
  281. {
  282. ByLanguageMap::iterator i = this->DefinesByLanguage.find(l);
  283. if (i == this->DefinesByLanguage.end())
  284. {
  285. std::set<std::string> defines;
  286. const char *lang = l.c_str();
  287. // Add the export symbol definition for shared library objects.
  288. if(const char* exportMacro = this->Target->GetExportMacro())
  289. {
  290. this->LocalGenerator->AppendDefines(defines, exportMacro);
  291. }
  292. // Add preprocessor definitions for this target and configuration.
  293. this->LocalGenerator->AddCompileDefinitions(defines, this->Target,
  294. this->LocalGenerator->ConfigurationName);
  295. std::string definesString;
  296. this->LocalGenerator->JoinDefines(defines, definesString, lang);
  297. ByLanguageMap::value_type entry(l, definesString);
  298. i = this->DefinesByLanguage.insert(entry).first;
  299. }
  300. return i->second;
  301. }
  302. void cmMakefileTargetGenerator::WriteTargetLanguageFlags()
  303. {
  304. // write language flags for target
  305. std::set<std::string> languages;
  306. this->Target->GetLanguages(languages,
  307. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE"));
  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);
  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. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE"));
  1054. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  1055. source != sources.end(); ++source)
  1056. {
  1057. if(cmCustomCommand* cc = (*source)->GetCustomCommand())
  1058. {
  1059. cmCustomCommandGenerator ccg(*cc, this->ConfigName, this->Makefile);
  1060. const std::vector<std::string>& outputs = ccg.GetOutputs();
  1061. for(std::vector<std::string>::const_iterator o = outputs.begin();
  1062. o != outputs.end(); ++o)
  1063. {
  1064. depends.push_back(*o);
  1065. }
  1066. }
  1067. }
  1068. }
  1069. //----------------------------------------------------------------------------
  1070. void cmMakefileTargetGenerator
  1071. ::WriteObjectDependRules(cmSourceFile const& source,
  1072. std::vector<std::string>& depends)
  1073. {
  1074. // Create the list of dependencies known at cmake time. These are
  1075. // shared between the object file and dependency scanning rule.
  1076. depends.push_back(source.GetFullPath());
  1077. if(const char* objectDeps = source.GetProperty("OBJECT_DEPENDS"))
  1078. {
  1079. std::vector<std::string> deps;
  1080. cmSystemTools::ExpandListArgument(objectDeps, deps);
  1081. for(std::vector<std::string>::iterator i = deps.begin();
  1082. i != deps.end(); ++i)
  1083. {
  1084. depends.push_back(*i);
  1085. }
  1086. }
  1087. }
  1088. //----------------------------------------------------------------------------
  1089. void cmMakefileTargetGenerator
  1090. ::GenerateCustomRuleFile(cmCustomCommandGenerator const& ccg)
  1091. {
  1092. // Collect the commands.
  1093. std::vector<std::string> commands;
  1094. std::string comment = this->LocalGenerator->ConstructComment(ccg);
  1095. if(!comment.empty())
  1096. {
  1097. // add in a progress call if needed
  1098. this->AppendProgress(commands);
  1099. if(!this->NoRuleMessages)
  1100. {
  1101. this->LocalGenerator
  1102. ->AppendEcho(commands, comment.c_str(),
  1103. cmLocalUnixMakefileGenerator3::EchoGenerate);
  1104. }
  1105. }
  1106. // Now append the actual user-specified commands.
  1107. cmOStringStream content;
  1108. this->LocalGenerator->AppendCustomCommand(commands, ccg, this->Target, false,
  1109. cmLocalGenerator::HOME_OUTPUT,
  1110. &content);
  1111. // Collect the dependencies.
  1112. std::vector<std::string> depends;
  1113. this->LocalGenerator->AppendCustomDepend(depends, ccg);
  1114. // Check whether we need to bother checking for a symbolic output.
  1115. bool need_symbolic = this->GlobalGenerator->GetNeedSymbolicMark();
  1116. // Write the rule.
  1117. const std::vector<std::string>& outputs = ccg.GetOutputs();
  1118. std::vector<std::string>::const_iterator o = outputs.begin();
  1119. {
  1120. bool symbolic = false;
  1121. if(need_symbolic)
  1122. {
  1123. if(cmSourceFile* sf = this->Makefile->GetSource(*o))
  1124. {
  1125. symbolic = sf->GetPropertyAsBool("SYMBOLIC");
  1126. }
  1127. }
  1128. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  1129. *o, depends, commands,
  1130. symbolic);
  1131. // If the rule has changed make sure the output is rebuilt.
  1132. if(!symbolic)
  1133. {
  1134. this->GlobalGenerator->AddRuleHash(ccg.GetOutputs(), content.str());
  1135. }
  1136. }
  1137. // Write rules to drive building any outputs beyond the first.
  1138. const char* in = o->c_str();
  1139. for(++o; o != outputs.end(); ++o)
  1140. {
  1141. bool symbolic = false;
  1142. if(need_symbolic)
  1143. {
  1144. if(cmSourceFile* sf = this->Makefile->GetSource(*o))
  1145. {
  1146. symbolic = sf->GetPropertyAsBool("SYMBOLIC");
  1147. }
  1148. }
  1149. this->GenerateExtraOutput(o->c_str(), in, symbolic);
  1150. }
  1151. // Setup implicit dependency scanning.
  1152. for(cmCustomCommand::ImplicitDependsList::const_iterator
  1153. idi = ccg.GetCC().GetImplicitDepends().begin();
  1154. idi != ccg.GetCC().GetImplicitDepends().end(); ++idi)
  1155. {
  1156. std::string objFullPath =
  1157. this->Convert(outputs[0], cmLocalGenerator::FULL);
  1158. std::string srcFullPath =
  1159. this->Convert(idi->second, cmLocalGenerator::FULL);
  1160. this->LocalGenerator->
  1161. AddImplicitDepends(*this->Target, idi->first,
  1162. objFullPath.c_str(),
  1163. srcFullPath.c_str());
  1164. }
  1165. }
  1166. //----------------------------------------------------------------------------
  1167. void
  1168. cmMakefileTargetGenerator
  1169. ::GenerateExtraOutput(const char* out, const char* in, bool symbolic)
  1170. {
  1171. // Add a rule to build the primary output if the extra output needs
  1172. // to be created.
  1173. std::vector<std::string> commands;
  1174. std::vector<std::string> depends;
  1175. std::string emptyCommand = this->GlobalGenerator->GetEmptyRuleHackCommand();
  1176. if(!emptyCommand.empty())
  1177. {
  1178. commands.push_back(emptyCommand);
  1179. }
  1180. depends.push_back(in);
  1181. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  1182. out, depends, commands,
  1183. symbolic);
  1184. // Register the extra output as paired with the first output so that
  1185. // the check-build-system step will remove the primary output if any
  1186. // extra outputs are missing. This forces the rule to regenerate
  1187. // all outputs.
  1188. this->AddMultipleOutputPair(out, in);
  1189. }
  1190. //----------------------------------------------------------------------------
  1191. void
  1192. cmMakefileTargetGenerator::AppendProgress(std::vector<std::string>& commands)
  1193. {
  1194. this->NumberOfProgressActions++;
  1195. if(this->NoRuleMessages)
  1196. {
  1197. return;
  1198. }
  1199. std::string progressDir = this->Makefile->GetHomeOutputDirectory();
  1200. progressDir += cmake::GetCMakeFilesDirectory();
  1201. cmOStringStream progCmd;
  1202. progCmd << "$(CMAKE_COMMAND) -E cmake_progress_report ";
  1203. progCmd << this->LocalGenerator->Convert(progressDir,
  1204. cmLocalGenerator::FULL,
  1205. cmLocalGenerator::SHELL);
  1206. progCmd << " $(CMAKE_PROGRESS_" << this->NumberOfProgressActions << ")";
  1207. commands.push_back(progCmd.str());
  1208. }
  1209. //----------------------------------------------------------------------------
  1210. void
  1211. cmMakefileTargetGenerator
  1212. ::WriteObjectsVariable(std::string& variableName,
  1213. std::string& variableNameExternal,
  1214. bool useWatcomQuote)
  1215. {
  1216. // Write a make variable assignment that lists all objects for the
  1217. // target.
  1218. variableName =
  1219. this->LocalGenerator->CreateMakeVariable(this->Target->GetName(),
  1220. "_OBJECTS");
  1221. *this->BuildFileStream
  1222. << "# Object files for target " << this->Target->GetName() << "\n"
  1223. << variableName << " =";
  1224. std::string object;
  1225. const char* lineContinue =
  1226. this->Makefile->GetDefinition("CMAKE_MAKE_LINE_CONTINUE");
  1227. if(!lineContinue)
  1228. {
  1229. lineContinue = "\\";
  1230. }
  1231. for(std::vector<std::string>::const_iterator i = this->Objects.begin();
  1232. i != this->Objects.end(); ++i)
  1233. {
  1234. *this->BuildFileStream << " " << lineContinue << "\n";
  1235. *this->BuildFileStream <<
  1236. this->LocalGenerator->ConvertToQuotedOutputPath(i->c_str(),
  1237. useWatcomQuote);
  1238. }
  1239. *this->BuildFileStream << "\n";
  1240. // Write a make variable assignment that lists all external objects
  1241. // for the target.
  1242. variableNameExternal =
  1243. this->LocalGenerator->CreateMakeVariable(this->Target->GetName(),
  1244. "_EXTERNAL_OBJECTS");
  1245. *this->BuildFileStream
  1246. << "\n"
  1247. << "# External object files for target "
  1248. << this->Target->GetName() << "\n"
  1249. << variableNameExternal << " =";
  1250. for(std::vector<std::string>::const_iterator i =
  1251. this->ExternalObjects.begin();
  1252. i != this->ExternalObjects.end(); ++i)
  1253. {
  1254. object = this->Convert(*i,cmLocalGenerator::START_OUTPUT);
  1255. *this->BuildFileStream
  1256. << " " << lineContinue << "\n"
  1257. << this->Makefile->GetSafeDefinition("CMAKE_OBJECT_NAME");
  1258. *this->BuildFileStream <<
  1259. this->LocalGenerator->ConvertToQuotedOutputPath(i->c_str(),
  1260. useWatcomQuote);
  1261. }
  1262. *this->BuildFileStream << "\n" << "\n";
  1263. }
  1264. //----------------------------------------------------------------------------
  1265. void
  1266. cmMakefileTargetGenerator
  1267. ::WriteObjectsString(std::string& buildObjs)
  1268. {
  1269. std::vector<std::string> objStrings;
  1270. this->WriteObjectsStrings(objStrings);
  1271. buildObjs = objStrings[0];
  1272. }
  1273. //----------------------------------------------------------------------------
  1274. class cmMakefileTargetGeneratorObjectStrings
  1275. {
  1276. public:
  1277. cmMakefileTargetGeneratorObjectStrings(std::vector<std::string>& strings,
  1278. cmLocalUnixMakefileGenerator3* lg,
  1279. std::string::size_type limit):
  1280. Strings(strings), LocalGenerator(lg), LengthLimit(limit)
  1281. {
  1282. this->Space = "";
  1283. }
  1284. void Feed(std::string const& obj)
  1285. {
  1286. // Construct the name of the next object.
  1287. this->NextObject =
  1288. this->LocalGenerator->Convert(obj,
  1289. cmLocalGenerator::START_OUTPUT,
  1290. cmLocalGenerator::RESPONSE);
  1291. // Roll over to next string if the limit will be exceeded.
  1292. if(this->LengthLimit != std::string::npos &&
  1293. (this->CurrentString.length() + 1 + this->NextObject.length()
  1294. > this->LengthLimit))
  1295. {
  1296. this->Strings.push_back(this->CurrentString);
  1297. this->CurrentString = "";
  1298. this->Space = "";
  1299. }
  1300. // Separate from previous object.
  1301. this->CurrentString += this->Space;
  1302. this->Space = " ";
  1303. // Append this object.
  1304. this->CurrentString += this->NextObject;
  1305. }
  1306. void Done()
  1307. {
  1308. this->Strings.push_back(this->CurrentString);
  1309. }
  1310. private:
  1311. std::vector<std::string>& Strings;
  1312. cmLocalUnixMakefileGenerator3* LocalGenerator;
  1313. std::string::size_type LengthLimit;
  1314. std::string CurrentString;
  1315. std::string NextObject;
  1316. const char* Space;
  1317. };
  1318. //----------------------------------------------------------------------------
  1319. void
  1320. cmMakefileTargetGenerator
  1321. ::WriteObjectsStrings(std::vector<std::string>& objStrings,
  1322. std::string::size_type limit)
  1323. {
  1324. cmMakefileTargetGeneratorObjectStrings
  1325. helper(objStrings, this->LocalGenerator, limit);
  1326. for(std::vector<std::string>::const_iterator i = this->Objects.begin();
  1327. i != this->Objects.end(); ++i)
  1328. {
  1329. helper.Feed(*i);
  1330. }
  1331. for(std::vector<std::string>::const_iterator i =
  1332. this->ExternalObjects.begin();
  1333. i != this->ExternalObjects.end(); ++i)
  1334. {
  1335. helper.Feed(*i);
  1336. }
  1337. helper.Done();
  1338. }
  1339. //----------------------------------------------------------------------------
  1340. void cmMakefileTargetGenerator::WriteTargetDriverRule(
  1341. const std::string& main_output,
  1342. bool relink)
  1343. {
  1344. // Compute the name of the driver target.
  1345. std::string dir =
  1346. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  1347. std::string buildTargetRuleName = dir;
  1348. buildTargetRuleName += relink?"/preinstall":"/build";
  1349. buildTargetRuleName = this->Convert(buildTargetRuleName,
  1350. cmLocalGenerator::HOME_OUTPUT,
  1351. cmLocalGenerator::UNCHANGED);
  1352. // Build the list of target outputs to drive.
  1353. std::vector<std::string> depends;
  1354. depends.push_back(main_output);
  1355. const char* comment = 0;
  1356. if(relink)
  1357. {
  1358. // Setup the comment for the preinstall driver.
  1359. comment = "Rule to relink during preinstall.";
  1360. }
  1361. else
  1362. {
  1363. // Setup the comment for the main build driver.
  1364. comment = "Rule to build all files generated by this target.";
  1365. // Make sure all custom command outputs in this target are built.
  1366. if(this->CustomCommandDriver == OnBuild)
  1367. {
  1368. this->DriveCustomCommands(depends);
  1369. }
  1370. // Make sure the extra files are built.
  1371. for(std::set<std::string>::const_iterator i = this->ExtraFiles.begin();
  1372. i != this->ExtraFiles.end(); ++i)
  1373. {
  1374. depends.push_back(*i);
  1375. }
  1376. }
  1377. // Write the driver rule.
  1378. std::vector<std::string> no_commands;
  1379. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, comment,
  1380. buildTargetRuleName,
  1381. depends, no_commands, true);
  1382. }
  1383. //----------------------------------------------------------------------------
  1384. std::string cmMakefileTargetGenerator::GetFrameworkFlags(std::string const& l)
  1385. {
  1386. if(!this->Makefile->IsOn("APPLE"))
  1387. {
  1388. return std::string();
  1389. }
  1390. std::string fwSearchFlagVar = "CMAKE_" + l + "_FRAMEWORK_SEARCH_FLAG";
  1391. const char* fwSearchFlag =
  1392. this->Makefile->GetDefinition(fwSearchFlagVar);
  1393. if(!(fwSearchFlag && *fwSearchFlag))
  1394. {
  1395. return std::string();
  1396. }
  1397. std::set<std::string> emitted;
  1398. #ifdef __APPLE__ /* don't insert this when crosscompiling e.g. to iphone */
  1399. emitted.insert("/System/Library/Frameworks");
  1400. #endif
  1401. std::vector<std::string> includes;
  1402. const std::string& config =
  1403. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  1404. this->LocalGenerator->GetIncludeDirectories(includes,
  1405. this->GeneratorTarget,
  1406. "C", config);
  1407. // check all include directories for frameworks as this
  1408. // will already have added a -F for the framework
  1409. for(std::vector<std::string>::iterator i = includes.begin();
  1410. i != includes.end(); ++i)
  1411. {
  1412. if(this->Target->NameResolvesToFramework(*i))
  1413. {
  1414. std::string frameworkDir = *i;
  1415. frameworkDir += "/../";
  1416. frameworkDir = cmSystemTools::CollapseFullPath(frameworkDir.c_str());
  1417. emitted.insert(frameworkDir);
  1418. }
  1419. }
  1420. std::string flags;
  1421. const char* cfg = this->LocalGenerator->ConfigurationName.c_str();
  1422. if(cmComputeLinkInformation* cli = this->Target->GetLinkInformation(cfg))
  1423. {
  1424. std::vector<std::string> const& frameworks = cli->GetFrameworkPaths();
  1425. for(std::vector<std::string>::const_iterator i = frameworks.begin();
  1426. i != frameworks.end(); ++i)
  1427. {
  1428. if(emitted.insert(*i).second)
  1429. {
  1430. flags += fwSearchFlag;
  1431. flags += this->Convert(*i,
  1432. cmLocalGenerator::START_OUTPUT,
  1433. cmLocalGenerator::SHELL, true);
  1434. flags += " ";
  1435. }
  1436. }
  1437. }
  1438. return flags;
  1439. }
  1440. //----------------------------------------------------------------------------
  1441. void cmMakefileTargetGenerator
  1442. ::AppendTargetDepends(std::vector<std::string>& depends)
  1443. {
  1444. // Static libraries never depend on anything for linking.
  1445. if(this->Target->GetType() == cmTarget::STATIC_LIBRARY)
  1446. {
  1447. return;
  1448. }
  1449. // Loop over all library dependencies.
  1450. const char* cfg = this->LocalGenerator->ConfigurationName.c_str();
  1451. if(cmComputeLinkInformation* cli = this->Target->GetLinkInformation(cfg))
  1452. {
  1453. std::vector<std::string> const& libDeps = cli->GetDepends();
  1454. for(std::vector<std::string>::const_iterator j = libDeps.begin();
  1455. j != libDeps.end(); ++j)
  1456. {
  1457. depends.push_back(*j);
  1458. }
  1459. }
  1460. }
  1461. //----------------------------------------------------------------------------
  1462. void cmMakefileTargetGenerator
  1463. ::AppendObjectDepends(std::vector<std::string>& depends)
  1464. {
  1465. // Add dependencies on the compiled object files.
  1466. std::string relPath = this->LocalGenerator->GetHomeRelativeOutputPath();
  1467. std::string objTarget;
  1468. for(std::vector<std::string>::const_iterator obj = this->Objects.begin();
  1469. obj != this->Objects.end(); ++obj)
  1470. {
  1471. objTarget = relPath;
  1472. objTarget += *obj;
  1473. depends.push_back(objTarget);
  1474. }
  1475. // Add dependencies on the external object files.
  1476. for(std::vector<std::string>::const_iterator obj
  1477. = this->ExternalObjects.begin();
  1478. obj != this->ExternalObjects.end(); ++obj)
  1479. {
  1480. depends.push_back(*obj);
  1481. }
  1482. // Add a dependency on the rule file itself.
  1483. this->LocalGenerator->AppendRuleDepend(depends,
  1484. this->BuildFileNameFull.c_str());
  1485. }
  1486. //----------------------------------------------------------------------------
  1487. void cmMakefileTargetGenerator
  1488. ::AppendLinkDepends(std::vector<std::string>& depends)
  1489. {
  1490. this->AppendObjectDepends(depends);
  1491. // Add dependencies on targets that must be built first.
  1492. this->AppendTargetDepends(depends);
  1493. // Add a dependency on the link definitions file, if any.
  1494. std::string def = this->GeneratorTarget->GetModuleDefinitionFile(
  1495. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE"));
  1496. if(!def.empty())
  1497. {
  1498. depends.push_back(def);
  1499. }
  1500. // Add user-specified dependencies.
  1501. if(const char* linkDepends =
  1502. this->Target->GetProperty("LINK_DEPENDS"))
  1503. {
  1504. cmSystemTools::ExpandListArgument(linkDepends, depends);
  1505. }
  1506. }
  1507. //----------------------------------------------------------------------------
  1508. std::string cmMakefileTargetGenerator::GetLinkRule(
  1509. const std::string& linkRuleVar)
  1510. {
  1511. std::string linkRule = this->Makefile->GetRequiredDefinition(linkRuleVar);
  1512. if(this->Target->HasImplibGNUtoMS())
  1513. {
  1514. std::string ruleVar = "CMAKE_";
  1515. ruleVar += this->Target->GetLinkerLanguage(this->ConfigName);
  1516. ruleVar += "_GNUtoMS_RULE";
  1517. if(const char* rule = this->Makefile->GetDefinition(ruleVar))
  1518. {
  1519. linkRule += rule;
  1520. }
  1521. }
  1522. return linkRule;
  1523. }
  1524. //----------------------------------------------------------------------------
  1525. void cmMakefileTargetGenerator
  1526. ::CloseFileStreams()
  1527. {
  1528. delete this->BuildFileStream;
  1529. delete this->InfoFileStream;
  1530. delete this->FlagFileStream;
  1531. }
  1532. void cmMakefileTargetGenerator::RemoveForbiddenFlags(const char* flagVar,
  1533. const std::string& linkLang,
  1534. std::string& linkFlags)
  1535. {
  1536. // check for language flags that are not allowed at link time, and
  1537. // remove them, -w on darwin for gcc -w -dynamiclib sends -w to libtool
  1538. // which fails, there may be more]
  1539. std::string removeFlags = "CMAKE_";
  1540. removeFlags += linkLang;
  1541. removeFlags += flagVar;
  1542. std::string removeflags =
  1543. this->Makefile->GetSafeDefinition(removeFlags);
  1544. std::vector<std::string> removeList;
  1545. cmSystemTools::ExpandListArgument(removeflags, removeList);
  1546. for(std::vector<std::string>::iterator i = removeList.begin();
  1547. i != removeList.end(); ++i)
  1548. {
  1549. std::string tmp;
  1550. std::string::size_type lastPosition = 0;
  1551. for(;;)
  1552. {
  1553. std::string::size_type position = linkFlags.find(*i, lastPosition);
  1554. if(position == std::string::npos)
  1555. {
  1556. tmp += linkFlags.substr(lastPosition);
  1557. break;
  1558. }
  1559. else
  1560. {
  1561. std::string::size_type prefixLength = position - lastPosition;
  1562. tmp += linkFlags.substr(lastPosition, prefixLength);
  1563. lastPosition = position + i->length();
  1564. bool validFlagStart = position == 0 ||
  1565. isspace(linkFlags[position - 1]);
  1566. bool validFlagEnd = lastPosition == linkFlags.size() ||
  1567. isspace(linkFlags[lastPosition]);
  1568. if(!validFlagStart || !validFlagEnd)
  1569. {
  1570. tmp += *i;
  1571. }
  1572. }
  1573. }
  1574. linkFlags = tmp;
  1575. }
  1576. }
  1577. //----------------------------------------------------------------------------
  1578. void
  1579. cmMakefileTargetGenerator
  1580. ::AddMultipleOutputPair(const char* depender, const char* dependee)
  1581. {
  1582. MultipleOutputPairsType::value_type p(depender, dependee);
  1583. this->MultipleOutputPairs.insert(p);
  1584. }
  1585. //----------------------------------------------------------------------------
  1586. void
  1587. cmMakefileTargetGenerator
  1588. ::CreateLinkScript(const char* name,
  1589. std::vector<std::string> const& link_commands,
  1590. std::vector<std::string>& makefile_commands,
  1591. std::vector<std::string>& makefile_depends)
  1592. {
  1593. // Create the link script file.
  1594. std::string linkScriptName = this->TargetBuildDirectoryFull;
  1595. linkScriptName += "/";
  1596. linkScriptName += name;
  1597. cmGeneratedFileStream linkScriptStream(linkScriptName.c_str());
  1598. linkScriptStream.SetCopyIfDifferent(true);
  1599. for(std::vector<std::string>::const_iterator cmd = link_commands.begin();
  1600. cmd != link_commands.end(); ++cmd)
  1601. {
  1602. // Do not write out empty commands or commands beginning in the
  1603. // shell no-op ":".
  1604. if(!cmd->empty() && (*cmd)[0] != ':')
  1605. {
  1606. linkScriptStream << *cmd << "\n";
  1607. }
  1608. }
  1609. // Create the makefile command to invoke the link script.
  1610. std::string link_command = "$(CMAKE_COMMAND) -E cmake_link_script ";
  1611. link_command += this->Convert(linkScriptName,
  1612. cmLocalGenerator::START_OUTPUT,
  1613. cmLocalGenerator::SHELL);
  1614. link_command += " --verbose=$(VERBOSE)";
  1615. makefile_commands.push_back(link_command);
  1616. makefile_depends.push_back(linkScriptName);
  1617. }
  1618. //----------------------------------------------------------------------------
  1619. std::string
  1620. cmMakefileTargetGenerator
  1621. ::CreateResponseFile(const char* name, std::string const& options,
  1622. std::vector<std::string>& makefile_depends)
  1623. {
  1624. // Create the response file.
  1625. std::string responseFileNameFull = this->TargetBuildDirectoryFull;
  1626. responseFileNameFull += "/";
  1627. responseFileNameFull += name;
  1628. cmGeneratedFileStream responseStream(responseFileNameFull.c_str());
  1629. responseStream.SetCopyIfDifferent(true);
  1630. responseStream << options << "\n";
  1631. // Add a dependency so the target will rebuild when the set of
  1632. // objects changes.
  1633. makefile_depends.push_back(responseFileNameFull);
  1634. // Construct the name to be used on the command line.
  1635. std::string responseFileName = this->TargetBuildDirectory;
  1636. responseFileName += "/";
  1637. responseFileName += name;
  1638. return responseFileName;
  1639. }
  1640. //----------------------------------------------------------------------------
  1641. void
  1642. cmMakefileTargetGenerator
  1643. ::CreateLinkLibs(std::string& linkLibs, bool relink,
  1644. bool useResponseFile,
  1645. std::vector<std::string>& makefile_depends,
  1646. bool useWatcomQuote)
  1647. {
  1648. std::string frameworkPath;
  1649. std::string linkPath;
  1650. this->LocalGenerator
  1651. ->OutputLinkLibraries(linkLibs, frameworkPath, linkPath,
  1652. *this->GeneratorTarget, relink,
  1653. useResponseFile,
  1654. useWatcomQuote);
  1655. linkLibs = frameworkPath + linkPath + linkLibs;
  1656. if(useResponseFile)
  1657. {
  1658. // Lookup the response file reference flag.
  1659. std::string responseFlagVar = "CMAKE_";
  1660. responseFlagVar += this->Target->GetLinkerLanguage(this->ConfigName);
  1661. responseFlagVar += "_RESPONSE_FILE_LINK_FLAG";
  1662. const char* responseFlag =
  1663. this->Makefile->GetDefinition(responseFlagVar);
  1664. if(!responseFlag)
  1665. {
  1666. responseFlag = "@";
  1667. }
  1668. // Create this response file.
  1669. std::string link_rsp =
  1670. this->CreateResponseFile("linklibs.rsp", linkLibs, makefile_depends);
  1671. // Reference the response file.
  1672. linkLibs = responseFlag;
  1673. linkLibs += this->Convert(link_rsp,
  1674. cmLocalGenerator::NONE,
  1675. cmLocalGenerator::SHELL);
  1676. }
  1677. }
  1678. //----------------------------------------------------------------------------
  1679. void
  1680. cmMakefileTargetGenerator
  1681. ::CreateObjectLists(bool useLinkScript, bool useArchiveRules,
  1682. bool useResponseFile, std::string& buildObjs,
  1683. std::vector<std::string>& makefile_depends,
  1684. bool useWatcomQuote)
  1685. {
  1686. std::string variableName;
  1687. std::string variableNameExternal;
  1688. this->WriteObjectsVariable(variableName, variableNameExternal,
  1689. useWatcomQuote);
  1690. if(useResponseFile)
  1691. {
  1692. // MSVC response files cannot exceed 128K.
  1693. std::string::size_type const responseFileLimit = 131000;
  1694. // Construct the individual object list strings.
  1695. std::vector<std::string> object_strings;
  1696. this->WriteObjectsStrings(object_strings, responseFileLimit);
  1697. // Lookup the response file reference flag.
  1698. std::string responseFlagVar = "CMAKE_";
  1699. responseFlagVar += this->Target->GetLinkerLanguage(this->ConfigName);
  1700. responseFlagVar += "_RESPONSE_FILE_LINK_FLAG";
  1701. const char* responseFlag =
  1702. this->Makefile->GetDefinition(responseFlagVar);
  1703. if(!responseFlag)
  1704. {
  1705. responseFlag = "@";
  1706. }
  1707. // Write a response file for each string.
  1708. const char* sep = "";
  1709. for(unsigned int i = 0; i < object_strings.size(); ++i)
  1710. {
  1711. // Number the response files.
  1712. char rsp[32];
  1713. sprintf(rsp, "objects%u.rsp", i+1);
  1714. // Create this response file.
  1715. std::string objects_rsp =
  1716. this->CreateResponseFile(rsp, object_strings[i], makefile_depends);
  1717. // Separate from previous response file references.
  1718. buildObjs += sep;
  1719. sep = " ";
  1720. // Reference the response file.
  1721. buildObjs += responseFlag;
  1722. buildObjs += this->Convert(objects_rsp,
  1723. cmLocalGenerator::NONE,
  1724. cmLocalGenerator::SHELL);
  1725. }
  1726. }
  1727. else if(useLinkScript)
  1728. {
  1729. if(!useArchiveRules)
  1730. {
  1731. this->WriteObjectsString(buildObjs);
  1732. }
  1733. }
  1734. else
  1735. {
  1736. buildObjs = "$(";
  1737. buildObjs += variableName;
  1738. buildObjs += ") $(";
  1739. buildObjs += variableNameExternal;
  1740. buildObjs += ")";
  1741. }
  1742. }
  1743. //----------------------------------------------------------------------------
  1744. void cmMakefileTargetGenerator::AddIncludeFlags(std::string& flags,
  1745. const std::string& lang)
  1746. {
  1747. std::string responseVar = "CMAKE_";
  1748. responseVar += lang;
  1749. responseVar += "_USE_RESPONSE_FILE_FOR_INCLUDES";
  1750. bool useResponseFile = this->Makefile->IsOn(responseVar);
  1751. std::vector<std::string> includes;
  1752. const std::string& config =
  1753. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  1754. this->LocalGenerator->GetIncludeDirectories(includes,
  1755. this->GeneratorTarget,
  1756. lang, config);
  1757. std::string includeFlags =
  1758. this->LocalGenerator->GetIncludeFlags(includes, this->GeneratorTarget,
  1759. lang, useResponseFile);
  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->GetCurrentOutputDirectory();
  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. }
  1868. //----------------------------------------------------------------------------
  1869. void cmMakefileTargetGenerator::AddModuleDefinitionFlag(std::string& flags)
  1870. {
  1871. std::string def = this->GeneratorTarget->GetModuleDefinitionFile(
  1872. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE"));
  1873. if(def.empty())
  1874. {
  1875. return;
  1876. }
  1877. // TODO: Create a per-language flag variable.
  1878. const char* defFileFlag =
  1879. this->Makefile->GetDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  1880. if(!defFileFlag)
  1881. {
  1882. return;
  1883. }
  1884. // Append the flag and value. Use ConvertToLinkReference to help
  1885. // vs6's "cl -link" pass it to the linker.
  1886. std::string flag = defFileFlag;
  1887. flag += (this->LocalGenerator->ConvertToLinkReference(def));
  1888. this->LocalGenerator->AppendFlags(flags, flag);
  1889. }
  1890. //----------------------------------------------------------------------------
  1891. const char* cmMakefileTargetGenerator::GetFeature(const std::string& feature)
  1892. {
  1893. return this->Target->GetFeature(feature, this->ConfigName);
  1894. }
  1895. //----------------------------------------------------------------------------
  1896. bool cmMakefileTargetGenerator::GetFeatureAsBool(const std::string& feature)
  1897. {
  1898. return this->Target->GetFeatureAsBool(feature, this->ConfigName);
  1899. }
  1900. //----------------------------------------------------------------------------
  1901. void cmMakefileTargetGenerator::AddFeatureFlags(
  1902. std::string& flags, const std::string& lang
  1903. )
  1904. {
  1905. // Add language-specific flags.
  1906. this->LocalGenerator->AddLanguageFlags(flags, lang, this->ConfigName);
  1907. if(this->GetFeatureAsBool("INTERPROCEDURAL_OPTIMIZATION"))
  1908. {
  1909. this->LocalGenerator->AppendFeatureOptions(flags, lang, "IPO");
  1910. }
  1911. }