cmMakefileTargetGenerator.cxx 72 KB

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