cmMakefileTargetGenerator.cxx 73 KB

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