cmMakefileTargetGenerator.cxx 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111
  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 = this->Convert(source.GetFullPath(),
  549. cmLocalGenerator::NONE,
  550. cmLocalGenerator::SHELL);
  551. // Construct the build message.
  552. std::vector<std::string> no_commands;
  553. std::vector<std::string> commands;
  554. // add in a progress call if needed
  555. this->NumberOfProgressActions++;
  556. if(!this->NoRuleMessages)
  557. {
  558. cmLocalUnixMakefileGenerator3::EchoProgress progress;
  559. this->MakeEchoProgress(progress);
  560. std::string buildEcho = "Building ";
  561. buildEcho += lang;
  562. buildEcho += " object ";
  563. buildEcho += relativeObj;
  564. this->LocalGenerator->AppendEcho
  565. (commands, buildEcho.c_str(), cmLocalUnixMakefileGenerator3::EchoBuild,
  566. &progress);
  567. }
  568. std::string targetOutPathReal;
  569. std::string targetOutPathPDB;
  570. std::string targetOutPathCompilePDB;
  571. {
  572. std::string targetFullPathReal;
  573. std::string targetFullPathPDB;
  574. std::string targetFullPathCompilePDB;
  575. if(this->Target->GetType() == cmTarget::EXECUTABLE ||
  576. this->Target->GetType() == cmTarget::STATIC_LIBRARY ||
  577. this->Target->GetType() == cmTarget::SHARED_LIBRARY ||
  578. this->Target->GetType() == cmTarget::MODULE_LIBRARY)
  579. {
  580. targetFullPathReal =
  581. this->Target->GetFullPath(this->ConfigName, false, true);
  582. targetFullPathPDB = this->Target->GetPDBDirectory(this->ConfigName);
  583. targetFullPathPDB += "/";
  584. targetFullPathPDB += this->Target->GetPDBName(this->ConfigName);
  585. }
  586. if(this->Target->GetType() <= cmTarget::OBJECT_LIBRARY)
  587. {
  588. targetFullPathCompilePDB =
  589. this->Target->GetCompilePDBPath(this->ConfigName);
  590. if(targetFullPathCompilePDB.empty())
  591. {
  592. targetFullPathCompilePDB = this->Target->GetSupportDirectory() + "/";
  593. }
  594. }
  595. targetOutPathReal = this->Convert(targetFullPathReal,
  596. cmLocalGenerator::START_OUTPUT,
  597. cmLocalGenerator::SHELL);
  598. targetOutPathPDB =
  599. this->Convert(targetFullPathPDB,cmLocalGenerator::NONE,
  600. cmLocalGenerator::SHELL);
  601. targetOutPathCompilePDB =
  602. this->Convert(targetFullPathCompilePDB,
  603. cmLocalGenerator::START_OUTPUT,
  604. cmLocalGenerator::SHELL);
  605. if (this->LocalGenerator->IsMinGWMake() &&
  606. cmHasLiteralSuffix(targetOutPathCompilePDB, "\\"))
  607. {
  608. // mingw32-make incorrectly interprets 'a\ b c' as 'a b' and 'c'
  609. // (but 'a\ b "c"' as 'a\', 'b', and 'c'!). Workaround this by
  610. // avoiding a trailing backslash in the argument.
  611. targetOutPathCompilePDB[targetOutPathCompilePDB.size()-1] = '/';
  612. }
  613. }
  614. cmLocalGenerator::RuleVariables vars;
  615. vars.RuleLauncher = "RULE_LAUNCH_COMPILE";
  616. vars.CMTarget = this->Target;
  617. vars.Language = lang.c_str();
  618. vars.Target = targetOutPathReal.c_str();
  619. vars.TargetPDB = targetOutPathPDB.c_str();
  620. vars.TargetCompilePDB = targetOutPathCompilePDB.c_str();
  621. vars.Source = sourceFile.c_str();
  622. std::string shellObj =
  623. this->Convert(obj,
  624. cmLocalGenerator::NONE,
  625. cmLocalGenerator::SHELL);
  626. vars.Object = shellObj.c_str();
  627. std::string objectDir = this->Target->GetSupportDirectory();
  628. objectDir = this->Convert(objectDir,
  629. cmLocalGenerator::START_OUTPUT,
  630. cmLocalGenerator::SHELL);
  631. vars.ObjectDir = objectDir.c_str();
  632. std::string objectFileDir = cmSystemTools::GetFilenamePath(obj);
  633. objectFileDir = this->Convert(objectFileDir,
  634. cmLocalGenerator::START_OUTPUT,
  635. cmLocalGenerator::SHELL);
  636. vars.ObjectFileDir = objectFileDir.c_str();
  637. vars.Flags = flags.c_str();
  638. std::string definesString = "$(";
  639. definesString += lang;
  640. definesString += "_DEFINES)";
  641. this->LocalGenerator->JoinDefines(defines, definesString, lang);
  642. vars.Defines = definesString.c_str();
  643. // At the moment, it is assumed that C, C++, and Fortran have both
  644. // assembly and preprocessor capabilities. The same is true for the
  645. // ability to export compile commands
  646. bool lang_has_preprocessor = ((lang == "C") ||
  647. (lang == "CXX") ||
  648. (lang == "Fortran"));
  649. bool const lang_has_assembly = lang_has_preprocessor;
  650. bool const lang_can_export_cmds = lang_has_preprocessor;
  651. // Construct the compile rules.
  652. {
  653. std::string compileRuleVar = "CMAKE_";
  654. compileRuleVar += lang;
  655. compileRuleVar += "_COMPILE_OBJECT";
  656. std::string compileRule =
  657. this->Makefile->GetRequiredDefinition(compileRuleVar);
  658. std::vector<std::string> compileCommands;
  659. cmSystemTools::ExpandListArgument(compileRule, compileCommands);
  660. if (this->Makefile->IsOn("CMAKE_EXPORT_COMPILE_COMMANDS") &&
  661. lang_can_export_cmds && compileCommands.size() == 1)
  662. {
  663. std::string compileCommand = compileCommands[0];
  664. this->LocalGenerator->ExpandRuleVariables(compileCommand, vars);
  665. std::string workingDirectory =
  666. this->LocalGenerator->Convert(
  667. this->Makefile->GetCurrentBinaryDirectory(), cmLocalGenerator::FULL);
  668. compileCommand.replace(compileCommand.find(langFlags),
  669. langFlags.size(), this->GetFlags(lang));
  670. std::string langDefines = std::string("$(") + lang + "_DEFINES)";
  671. compileCommand.replace(compileCommand.find(langDefines),
  672. langDefines.size(), this->GetDefines(lang));
  673. this->GlobalGenerator->AddCXXCompileCommand(
  674. source.GetFullPath(), workingDirectory, compileCommand);
  675. }
  676. // Maybe insert an include-what-you-use runner.
  677. if (!compileCommands.empty() && (lang == "C" || lang == "CXX"))
  678. {
  679. std::string const iwyu_prop = lang + "_INCLUDE_WHAT_YOU_USE";
  680. const char *iwyu = this->Target->GetProperty(iwyu_prop);
  681. if (iwyu && *iwyu)
  682. {
  683. std::string run_iwyu = "$(CMAKE_COMMAND) -E __run_iwyu --iwyu=";
  684. run_iwyu += this->LocalGenerator->EscapeForShell(iwyu);
  685. run_iwyu += " -- ";
  686. compileCommands.front().insert(0, run_iwyu);
  687. }
  688. }
  689. // Expand placeholders in the commands.
  690. for(std::vector<std::string>::iterator i = compileCommands.begin();
  691. i != compileCommands.end(); ++i)
  692. {
  693. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  694. }
  695. // Change the command working directory to the local build tree.
  696. this->LocalGenerator->CreateCDCommand
  697. (compileCommands,
  698. this->Makefile->GetCurrentBinaryDirectory(),
  699. cmLocalGenerator::HOME_OUTPUT);
  700. commands.insert(commands.end(),
  701. compileCommands.begin(), compileCommands.end());
  702. }
  703. // Check for extra outputs created by the compilation.
  704. std::vector<std::string> outputs(1, relativeObj);
  705. if(const char* extra_outputs_str =
  706. source.GetProperty("OBJECT_OUTPUTS"))
  707. {
  708. // Register these as extra files to clean.
  709. cmSystemTools::ExpandListArgument(extra_outputs_str, outputs);
  710. this->CleanFiles.insert(this->CleanFiles.end(),
  711. outputs.begin() + 1, outputs.end());
  712. }
  713. // Write the rule.
  714. this->WriteMakeRule(*this->BuildFileStream, 0, outputs,
  715. depends, commands);
  716. bool do_preprocess_rules = lang_has_preprocessor &&
  717. this->LocalGenerator->GetCreatePreprocessedSourceRules();
  718. bool do_assembly_rules = lang_has_assembly &&
  719. this->LocalGenerator->GetCreateAssemblySourceRules();
  720. if(do_preprocess_rules || do_assembly_rules)
  721. {
  722. std::vector<std::string> force_depends;
  723. force_depends.push_back("cmake_force");
  724. std::string::size_type dot_pos = relativeObj.rfind(".");
  725. std::string relativeObjBase = relativeObj.substr(0, dot_pos);
  726. dot_pos = obj.rfind(".");
  727. std::string objBase = obj.substr(0, dot_pos);
  728. if(do_preprocess_rules)
  729. {
  730. commands.clear();
  731. std::string relativeObjI = relativeObjBase + ".i";
  732. std::string objI = objBase + ".i";
  733. std::string preprocessEcho = "Preprocessing ";
  734. preprocessEcho += lang;
  735. preprocessEcho += " source to ";
  736. preprocessEcho += objI;
  737. this->LocalGenerator->AppendEcho(
  738. commands, preprocessEcho.c_str(),
  739. cmLocalUnixMakefileGenerator3::EchoBuild
  740. );
  741. std::string preprocessRuleVar = "CMAKE_";
  742. preprocessRuleVar += lang;
  743. preprocessRuleVar += "_CREATE_PREPROCESSED_SOURCE";
  744. if(const char* preprocessRule =
  745. this->Makefile->GetDefinition(preprocessRuleVar))
  746. {
  747. std::vector<std::string> preprocessCommands;
  748. cmSystemTools::ExpandListArgument(preprocessRule, preprocessCommands);
  749. std::string shellObjI =
  750. this->Convert(objI,
  751. cmLocalGenerator::NONE,
  752. cmLocalGenerator::SHELL);
  753. vars.PreprocessedSource = shellObjI.c_str();
  754. // Expand placeholders in the commands.
  755. for(std::vector<std::string>::iterator i = preprocessCommands.begin();
  756. i != preprocessCommands.end(); ++i)
  757. {
  758. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  759. }
  760. this->LocalGenerator->CreateCDCommand
  761. (preprocessCommands,
  762. this->Makefile->GetCurrentBinaryDirectory(),
  763. cmLocalGenerator::HOME_OUTPUT);
  764. commands.insert(commands.end(),
  765. preprocessCommands.begin(),
  766. preprocessCommands.end());
  767. }
  768. else
  769. {
  770. std::string cmd = "$(CMAKE_COMMAND) -E cmake_unimplemented_variable ";
  771. cmd += preprocessRuleVar;
  772. commands.push_back(cmd);
  773. }
  774. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  775. relativeObjI,
  776. force_depends, commands, false);
  777. }
  778. if(do_assembly_rules)
  779. {
  780. commands.clear();
  781. std::string relativeObjS = relativeObjBase + ".s";
  782. std::string objS = objBase + ".s";
  783. std::string assemblyEcho = "Compiling ";
  784. assemblyEcho += lang;
  785. assemblyEcho += " source to assembly ";
  786. assemblyEcho += objS;
  787. this->LocalGenerator->AppendEcho(
  788. commands, assemblyEcho.c_str(),
  789. cmLocalUnixMakefileGenerator3::EchoBuild
  790. );
  791. std::string assemblyRuleVar = "CMAKE_";
  792. assemblyRuleVar += lang;
  793. assemblyRuleVar += "_CREATE_ASSEMBLY_SOURCE";
  794. if(const char* assemblyRule =
  795. this->Makefile->GetDefinition(assemblyRuleVar))
  796. {
  797. std::vector<std::string> assemblyCommands;
  798. cmSystemTools::ExpandListArgument(assemblyRule, assemblyCommands);
  799. std::string shellObjS =
  800. this->Convert(objS,
  801. cmLocalGenerator::NONE,
  802. cmLocalGenerator::SHELL);
  803. vars.AssemblySource = shellObjS.c_str();
  804. // Expand placeholders in the commands.
  805. for(std::vector<std::string>::iterator i = assemblyCommands.begin();
  806. i != assemblyCommands.end(); ++i)
  807. {
  808. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  809. }
  810. this->LocalGenerator->CreateCDCommand
  811. (assemblyCommands,
  812. this->Makefile->GetCurrentBinaryDirectory(),
  813. cmLocalGenerator::HOME_OUTPUT);
  814. commands.insert(commands.end(),
  815. assemblyCommands.begin(),
  816. assemblyCommands.end());
  817. }
  818. else
  819. {
  820. std::string cmd = "$(CMAKE_COMMAND) -E cmake_unimplemented_variable ";
  821. cmd += assemblyRuleVar;
  822. commands.push_back(cmd);
  823. }
  824. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  825. relativeObjS,
  826. force_depends, commands, false);
  827. }
  828. }
  829. // If the language needs provides-requires mode, create the
  830. // corresponding targets.
  831. std::string objectRequires = relativeObj;
  832. objectRequires += ".requires";
  833. std::vector<std::string> p_depends;
  834. // always provide an empty requires target
  835. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  836. objectRequires, p_depends,
  837. no_commands, true);
  838. // write a build rule to recursively build what this obj provides
  839. std::string objectProvides = relativeObj;
  840. objectProvides += ".provides";
  841. std::string temp = relativeObj;
  842. temp += ".provides.build";
  843. std::vector<std::string> r_commands;
  844. std::string tgtMakefileName =
  845. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  846. tgtMakefileName += "/build.make";
  847. r_commands.push_back
  848. (this->LocalGenerator->GetRecursiveMakeCall(tgtMakefileName.c_str(),
  849. temp));
  850. p_depends.clear();
  851. p_depends.push_back(objectRequires);
  852. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  853. objectProvides, p_depends,
  854. r_commands, true);
  855. // write the provides.build rule dependency on the obj file
  856. p_depends.clear();
  857. p_depends.push_back(relativeObj);
  858. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  859. temp, p_depends, no_commands,
  860. false);
  861. }
  862. //----------------------------------------------------------------------------
  863. void cmMakefileTargetGenerator::WriteTargetRequiresRules()
  864. {
  865. std::vector<std::string> depends;
  866. std::vector<std::string> no_commands;
  867. // Construct the name of the dependency generation target.
  868. std::string depTarget =
  869. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  870. depTarget += "/requires";
  871. // This target drives dependency generation for all object files.
  872. std::string relPath = this->LocalGenerator->GetHomeRelativeOutputPath();
  873. std::string objTarget;
  874. for(std::vector<std::string>::const_iterator obj = this->Objects.begin();
  875. obj != this->Objects.end(); ++obj)
  876. {
  877. objTarget = relPath;
  878. objTarget += *obj;
  879. objTarget += ".requires";
  880. depends.push_back(objTarget);
  881. }
  882. // Write the rule.
  883. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  884. depTarget,
  885. depends, no_commands, true);
  886. }
  887. //----------------------------------------------------------------------------
  888. void cmMakefileTargetGenerator::WriteTargetCleanRules()
  889. {
  890. std::vector<std::string> depends;
  891. std::vector<std::string> commands;
  892. // Construct the clean target name.
  893. std::string cleanTarget =
  894. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  895. cleanTarget += "/clean";
  896. // Construct the clean command.
  897. this->LocalGenerator->AppendCleanCommand(commands, this->CleanFiles,
  898. *this->Target);
  899. this->LocalGenerator->CreateCDCommand
  900. (commands,
  901. this->Makefile->GetCurrentBinaryDirectory(),
  902. cmLocalGenerator::HOME_OUTPUT);
  903. // Write the rule.
  904. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  905. cleanTarget,
  906. depends, commands, true);
  907. }
  908. //----------------------------------------------------------------------------
  909. bool cmMakefileTargetGenerator::WriteMakeRule(
  910. std::ostream& os,
  911. const char* comment,
  912. const std::vector<std::string>& outputs,
  913. const std::vector<std::string>& depends,
  914. const std::vector<std::string>& commands,
  915. bool in_help)
  916. {
  917. bool symbolic = false;
  918. if (outputs.size() == 0)
  919. {
  920. return symbolic;
  921. }
  922. // Check whether we need to bother checking for a symbolic output.
  923. bool need_symbolic = this->GlobalGenerator->GetNeedSymbolicMark();
  924. // Check whether the first output is marked as symbolic.
  925. if(need_symbolic)
  926. {
  927. if(cmSourceFile* sf = this->Makefile->GetSource(outputs[0]))
  928. {
  929. symbolic = sf->GetPropertyAsBool("SYMBOLIC");
  930. }
  931. }
  932. // We always attach the actual commands to the first output.
  933. this->LocalGenerator->WriteMakeRule(os, comment, outputs[0], depends,
  934. commands, symbolic, in_help);
  935. // For single outputs, we are done.
  936. if (outputs.size() == 1)
  937. {
  938. return symbolic;
  939. }
  940. // For multiple outputs, make the extra ones depend on the first one.
  941. std::vector<std::string> const output_depends(1, outputs[0]);
  942. for (std::vector<std::string>::const_iterator o = outputs.begin()+1;
  943. o != outputs.end(); ++o)
  944. {
  945. // Touch the extra output so "make" knows that it was updated,
  946. // but only if the output was acually created.
  947. std::string const out = this->Convert(*o, cmLocalGenerator::HOME_OUTPUT,
  948. cmLocalGenerator::SHELL);
  949. std::vector<std::string> output_commands;
  950. bool o_symbolic = false;
  951. if(need_symbolic)
  952. {
  953. if(cmSourceFile* sf = this->Makefile->GetSource(*o))
  954. {
  955. o_symbolic = sf->GetPropertyAsBool("SYMBOLIC");
  956. }
  957. }
  958. symbolic = symbolic && o_symbolic;
  959. if (!o_symbolic)
  960. {
  961. output_commands.push_back("@$(CMAKE_COMMAND) -E touch_nocreate " + out);
  962. }
  963. this->LocalGenerator->WriteMakeRule(os, 0, *o, output_depends,
  964. output_commands, o_symbolic, in_help);
  965. if (!o_symbolic)
  966. {
  967. // At build time, remove the first output if this one does not exist
  968. // so that "make" will rerun the real commands that create this one.
  969. MultipleOutputPairsType::value_type p(*o, outputs[0]);
  970. this->MultipleOutputPairs.insert(p);
  971. }
  972. }
  973. return symbolic;
  974. }
  975. //----------------------------------------------------------------------------
  976. void cmMakefileTargetGenerator::WriteTargetDependRules()
  977. {
  978. // must write the targets depend info file
  979. std::string dir = this->LocalGenerator->GetTargetDirectory(*this->Target);
  980. this->InfoFileNameFull = dir;
  981. this->InfoFileNameFull += "/DependInfo.cmake";
  982. this->InfoFileNameFull =
  983. this->LocalGenerator->ConvertToFullPath(this->InfoFileNameFull);
  984. this->InfoFileStream =
  985. new cmGeneratedFileStream(this->InfoFileNameFull.c_str());
  986. this->InfoFileStream->SetCopyIfDifferent(true);
  987. if(!*this->InfoFileStream)
  988. {
  989. return;
  990. }
  991. this->LocalGenerator->
  992. WriteDependLanguageInfo(*this->InfoFileStream,*this->Target);
  993. // Store multiple output pairs in the depend info file.
  994. if(!this->MultipleOutputPairs.empty())
  995. {
  996. *this->InfoFileStream
  997. << "\n"
  998. << "# Pairs of files generated by the same build rule.\n"
  999. << "set(CMAKE_MULTIPLE_OUTPUT_PAIRS\n";
  1000. for(MultipleOutputPairsType::const_iterator pi =
  1001. this->MultipleOutputPairs.begin();
  1002. pi != this->MultipleOutputPairs.end(); ++pi)
  1003. {
  1004. *this->InfoFileStream
  1005. << " " << cmLocalGenerator::EscapeForCMake(pi->first)
  1006. << " " << cmLocalGenerator::EscapeForCMake(pi->second)
  1007. << "\n";
  1008. }
  1009. *this->InfoFileStream << " )\n\n";
  1010. }
  1011. // Store list of targets linked directly or transitively.
  1012. {
  1013. *this->InfoFileStream
  1014. << "\n"
  1015. << "# Targets to which this target links.\n"
  1016. << "set(CMAKE_TARGET_LINKED_INFO_FILES\n";
  1017. std::set<cmTarget const*> emitted;
  1018. const char* cfg = this->LocalGenerator->ConfigurationName.c_str();
  1019. if(cmComputeLinkInformation* cli = this->Target->GetLinkInformation(cfg))
  1020. {
  1021. cmComputeLinkInformation::ItemVector const& items = cli->GetItems();
  1022. for(cmComputeLinkInformation::ItemVector::const_iterator
  1023. i = items.begin(); i != items.end(); ++i)
  1024. {
  1025. cmTarget const* linkee = i->Target;
  1026. if(linkee && !linkee->IsImported()
  1027. // We can ignore the INTERFACE_LIBRARY items because
  1028. // Target->GetLinkInformation already processed their
  1029. // link interface and they don't have any output themselves.
  1030. && linkee->GetType() != cmTarget::INTERFACE_LIBRARY
  1031. && emitted.insert(linkee).second)
  1032. {
  1033. cmMakefile* mf = linkee->GetMakefile();
  1034. cmLocalGenerator* lg = mf->GetLocalGenerator();
  1035. std::string di = mf->GetCurrentBinaryDirectory();
  1036. di += "/";
  1037. di += lg->GetTargetDirectory(*linkee);
  1038. di += "/DependInfo.cmake";
  1039. *this->InfoFileStream << " \"" << di << "\"\n";
  1040. }
  1041. }
  1042. }
  1043. *this->InfoFileStream
  1044. << " )\n";
  1045. }
  1046. // Check for a target-specific module output directory.
  1047. if(const char* mdir = this->GetFortranModuleDirectory())
  1048. {
  1049. *this->InfoFileStream
  1050. << "\n"
  1051. << "# Fortran module output directory.\n"
  1052. << "set(CMAKE_Fortran_TARGET_MODULE_DIR \"" << mdir << "\")\n";
  1053. }
  1054. // and now write the rule to use it
  1055. std::vector<std::string> depends;
  1056. std::vector<std::string> commands;
  1057. // Construct the name of the dependency generation target.
  1058. std::string depTarget =
  1059. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  1060. depTarget += "/depend";
  1061. // Add a command to call CMake to scan dependencies. CMake will
  1062. // touch the corresponding depends file after scanning dependencies.
  1063. std::ostringstream depCmd;
  1064. // TODO: Account for source file properties and directory-level
  1065. // definitions when scanning for dependencies.
  1066. #if !defined(_WIN32) || defined(__CYGWIN__)
  1067. // This platform supports symlinks, so cmSystemTools will translate
  1068. // paths. Make sure PWD is set to the original name of the home
  1069. // output directory to help cmSystemTools to create the same
  1070. // translation table for the dependency scanning process.
  1071. depCmd << "cd "
  1072. << (this->LocalGenerator->Convert(
  1073. this->Makefile->GetHomeOutputDirectory(),
  1074. cmLocalGenerator::FULL, cmLocalGenerator::SHELL))
  1075. << " && ";
  1076. #endif
  1077. // Generate a call this signature:
  1078. //
  1079. // cmake -E cmake_depends <generator>
  1080. // <home-src-dir> <start-src-dir>
  1081. // <home-out-dir> <start-out-dir>
  1082. // <dep-info> --color=$(COLOR)
  1083. //
  1084. // This gives the dependency scanner enough information to recreate
  1085. // the state of our local generator sufficiently for its needs.
  1086. depCmd << "$(CMAKE_COMMAND) -E cmake_depends \""
  1087. << this->GlobalGenerator->GetName() << "\" "
  1088. << this->Convert(this->Makefile->GetHomeDirectory(),
  1089. cmLocalGenerator::FULL, cmLocalGenerator::SHELL)
  1090. << " "
  1091. << this->Convert(this->Makefile->GetCurrentSourceDirectory(),
  1092. cmLocalGenerator::FULL, cmLocalGenerator::SHELL)
  1093. << " "
  1094. << this->Convert(this->Makefile->GetHomeOutputDirectory(),
  1095. cmLocalGenerator::FULL, cmLocalGenerator::SHELL)
  1096. << " "
  1097. << this->Convert(this->Makefile->GetCurrentBinaryDirectory(),
  1098. cmLocalGenerator::FULL, cmLocalGenerator::SHELL)
  1099. << " "
  1100. << this->Convert(this->InfoFileNameFull,
  1101. cmLocalGenerator::FULL, cmLocalGenerator::SHELL);
  1102. if(this->LocalGenerator->GetColorMakefile())
  1103. {
  1104. depCmd << " --color=$(COLOR)";
  1105. }
  1106. commands.push_back(depCmd.str());
  1107. // Make sure all custom command outputs in this target are built.
  1108. if(this->CustomCommandDriver == OnDepends)
  1109. {
  1110. this->DriveCustomCommands(depends);
  1111. }
  1112. // Write the rule.
  1113. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  1114. depTarget,
  1115. depends, commands, true);
  1116. }
  1117. //----------------------------------------------------------------------------
  1118. void
  1119. cmMakefileTargetGenerator
  1120. ::DriveCustomCommands(std::vector<std::string>& depends)
  1121. {
  1122. // Depend on all custom command outputs.
  1123. std::vector<cmSourceFile*> sources;
  1124. this->Target->GetSourceFiles(sources,
  1125. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE"));
  1126. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  1127. source != sources.end(); ++source)
  1128. {
  1129. if(cmCustomCommand* cc = (*source)->GetCustomCommand())
  1130. {
  1131. cmCustomCommandGenerator ccg(*cc, this->ConfigName, this->Makefile);
  1132. const std::vector<std::string>& outputs = ccg.GetOutputs();
  1133. depends.insert(depends.end(), outputs.begin(), outputs.end());
  1134. }
  1135. }
  1136. }
  1137. //----------------------------------------------------------------------------
  1138. void cmMakefileTargetGenerator
  1139. ::WriteObjectDependRules(cmSourceFile const& source,
  1140. std::vector<std::string>& depends)
  1141. {
  1142. // Create the list of dependencies known at cmake time. These are
  1143. // shared between the object file and dependency scanning rule.
  1144. depends.push_back(source.GetFullPath());
  1145. if(const char* objectDeps = source.GetProperty("OBJECT_DEPENDS"))
  1146. {
  1147. cmSystemTools::ExpandListArgument(objectDeps, depends);
  1148. }
  1149. }
  1150. //----------------------------------------------------------------------------
  1151. void cmMakefileTargetGenerator
  1152. ::GenerateCustomRuleFile(cmCustomCommandGenerator const& ccg)
  1153. {
  1154. // Collect the commands.
  1155. std::vector<std::string> commands;
  1156. std::string comment = this->LocalGenerator->ConstructComment(ccg);
  1157. if(!comment.empty())
  1158. {
  1159. // add in a progress call if needed
  1160. this->NumberOfProgressActions++;
  1161. if(!this->NoRuleMessages)
  1162. {
  1163. cmLocalUnixMakefileGenerator3::EchoProgress progress;
  1164. this->MakeEchoProgress(progress);
  1165. this->LocalGenerator
  1166. ->AppendEcho(commands, comment.c_str(),
  1167. cmLocalUnixMakefileGenerator3::EchoGenerate,
  1168. &progress);
  1169. }
  1170. }
  1171. // Now append the actual user-specified commands.
  1172. std::ostringstream content;
  1173. this->LocalGenerator->AppendCustomCommand(commands, ccg, this->Target, false,
  1174. cmLocalGenerator::HOME_OUTPUT,
  1175. &content);
  1176. // Collect the dependencies.
  1177. std::vector<std::string> depends;
  1178. this->LocalGenerator->AppendCustomDepend(depends, ccg);
  1179. // Write the rule.
  1180. const std::vector<std::string>& outputs = ccg.GetOutputs();
  1181. bool symbolic = this->WriteMakeRule(*this->BuildFileStream, 0,
  1182. outputs, depends, commands);
  1183. // If the rule has changed make sure the output is rebuilt.
  1184. if(!symbolic)
  1185. {
  1186. this->GlobalGenerator->AddRuleHash(ccg.GetOutputs(), content.str());
  1187. }
  1188. // Setup implicit dependency scanning.
  1189. for(cmCustomCommand::ImplicitDependsList::const_iterator
  1190. idi = ccg.GetCC().GetImplicitDepends().begin();
  1191. idi != ccg.GetCC().GetImplicitDepends().end(); ++idi)
  1192. {
  1193. std::string objFullPath =
  1194. this->Convert(outputs[0], cmLocalGenerator::FULL);
  1195. std::string srcFullPath =
  1196. this->Convert(idi->second, cmLocalGenerator::FULL);
  1197. this->LocalGenerator->
  1198. AddImplicitDepends(*this->Target, idi->first,
  1199. objFullPath.c_str(),
  1200. srcFullPath.c_str());
  1201. }
  1202. }
  1203. //----------------------------------------------------------------------------
  1204. void
  1205. cmMakefileTargetGenerator
  1206. ::MakeEchoProgress(cmLocalUnixMakefileGenerator3::EchoProgress& progress) const
  1207. {
  1208. progress.Dir = this->Makefile->GetHomeOutputDirectory();
  1209. progress.Dir += cmake::GetCMakeFilesDirectory();
  1210. std::ostringstream progressArg;
  1211. progressArg << "$(CMAKE_PROGRESS_" << this->NumberOfProgressActions << ")";
  1212. progress.Arg = progressArg.str();
  1213. }
  1214. //----------------------------------------------------------------------------
  1215. void
  1216. cmMakefileTargetGenerator
  1217. ::WriteObjectsVariable(std::string& variableName,
  1218. std::string& variableNameExternal,
  1219. bool useWatcomQuote)
  1220. {
  1221. // Write a make variable assignment that lists all objects for the
  1222. // target.
  1223. variableName =
  1224. this->LocalGenerator->CreateMakeVariable(this->Target->GetName(),
  1225. "_OBJECTS");
  1226. *this->BuildFileStream
  1227. << "# Object files for target " << this->Target->GetName() << "\n"
  1228. << variableName << " =";
  1229. std::string object;
  1230. const char* lineContinue =
  1231. this->Makefile->GetDefinition("CMAKE_MAKE_LINE_CONTINUE");
  1232. if(!lineContinue)
  1233. {
  1234. lineContinue = "\\";
  1235. }
  1236. for(std::vector<std::string>::const_iterator i = this->Objects.begin();
  1237. i != this->Objects.end(); ++i)
  1238. {
  1239. *this->BuildFileStream << " " << lineContinue << "\n";
  1240. *this->BuildFileStream <<
  1241. this->LocalGenerator->ConvertToQuotedOutputPath(i->c_str(),
  1242. useWatcomQuote);
  1243. }
  1244. *this->BuildFileStream << "\n";
  1245. // Write a make variable assignment that lists all external objects
  1246. // for the target.
  1247. variableNameExternal =
  1248. this->LocalGenerator->CreateMakeVariable(this->Target->GetName(),
  1249. "_EXTERNAL_OBJECTS");
  1250. *this->BuildFileStream
  1251. << "\n"
  1252. << "# External object files for target "
  1253. << this->Target->GetName() << "\n"
  1254. << variableNameExternal << " =";
  1255. for(std::vector<std::string>::const_iterator i =
  1256. this->ExternalObjects.begin();
  1257. i != this->ExternalObjects.end(); ++i)
  1258. {
  1259. object = this->Convert(*i,cmLocalGenerator::START_OUTPUT);
  1260. *this->BuildFileStream
  1261. << " " << lineContinue << "\n"
  1262. << this->Makefile->GetSafeDefinition("CMAKE_OBJECT_NAME");
  1263. *this->BuildFileStream <<
  1264. this->LocalGenerator->ConvertToQuotedOutputPath(i->c_str(),
  1265. useWatcomQuote);
  1266. }
  1267. *this->BuildFileStream << "\n" << "\n";
  1268. }
  1269. //----------------------------------------------------------------------------
  1270. void
  1271. cmMakefileTargetGenerator
  1272. ::WriteObjectsString(std::string& buildObjs)
  1273. {
  1274. std::vector<std::string> objStrings;
  1275. this->WriteObjectsStrings(objStrings);
  1276. buildObjs = objStrings[0];
  1277. }
  1278. //----------------------------------------------------------------------------
  1279. class cmMakefileTargetGeneratorObjectStrings
  1280. {
  1281. public:
  1282. cmMakefileTargetGeneratorObjectStrings(std::vector<std::string>& strings,
  1283. cmLocalUnixMakefileGenerator3* lg,
  1284. std::string::size_type limit):
  1285. Strings(strings), LocalGenerator(lg), LengthLimit(limit)
  1286. {
  1287. this->Space = "";
  1288. }
  1289. void Feed(std::string const& obj)
  1290. {
  1291. // Construct the name of the next object.
  1292. this->NextObject =
  1293. this->LocalGenerator->Convert(obj,
  1294. cmLocalGenerator::START_OUTPUT,
  1295. cmLocalGenerator::RESPONSE);
  1296. // Roll over to next string if the limit will be exceeded.
  1297. if(this->LengthLimit != std::string::npos &&
  1298. (this->CurrentString.length() + 1 + this->NextObject.length()
  1299. > this->LengthLimit))
  1300. {
  1301. this->Strings.push_back(this->CurrentString);
  1302. this->CurrentString = "";
  1303. this->Space = "";
  1304. }
  1305. // Separate from previous object.
  1306. this->CurrentString += this->Space;
  1307. this->Space = " ";
  1308. // Append this object.
  1309. this->CurrentString += this->NextObject;
  1310. }
  1311. void Done()
  1312. {
  1313. this->Strings.push_back(this->CurrentString);
  1314. }
  1315. private:
  1316. std::vector<std::string>& Strings;
  1317. cmLocalUnixMakefileGenerator3* LocalGenerator;
  1318. std::string::size_type LengthLimit;
  1319. std::string CurrentString;
  1320. std::string NextObject;
  1321. const char* Space;
  1322. };
  1323. //----------------------------------------------------------------------------
  1324. void
  1325. cmMakefileTargetGenerator
  1326. ::WriteObjectsStrings(std::vector<std::string>& objStrings,
  1327. std::string::size_type limit)
  1328. {
  1329. cmMakefileTargetGeneratorObjectStrings
  1330. helper(objStrings, this->LocalGenerator, limit);
  1331. for(std::vector<std::string>::const_iterator i = this->Objects.begin();
  1332. i != this->Objects.end(); ++i)
  1333. {
  1334. helper.Feed(*i);
  1335. }
  1336. for(std::vector<std::string>::const_iterator i =
  1337. this->ExternalObjects.begin();
  1338. i != this->ExternalObjects.end(); ++i)
  1339. {
  1340. helper.Feed(*i);
  1341. }
  1342. helper.Done();
  1343. }
  1344. //----------------------------------------------------------------------------
  1345. void cmMakefileTargetGenerator::WriteTargetDriverRule(
  1346. const std::string& main_output,
  1347. bool relink)
  1348. {
  1349. // Compute the name of the driver target.
  1350. std::string dir =
  1351. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  1352. std::string buildTargetRuleName = dir;
  1353. buildTargetRuleName += relink?"/preinstall":"/build";
  1354. buildTargetRuleName = this->Convert(buildTargetRuleName,
  1355. cmLocalGenerator::HOME_OUTPUT,
  1356. cmLocalGenerator::UNCHANGED);
  1357. // Build the list of target outputs to drive.
  1358. std::vector<std::string> depends;
  1359. depends.push_back(main_output);
  1360. const char* comment = 0;
  1361. if(relink)
  1362. {
  1363. // Setup the comment for the preinstall driver.
  1364. comment = "Rule to relink during preinstall.";
  1365. }
  1366. else
  1367. {
  1368. // Setup the comment for the main build driver.
  1369. comment = "Rule to build all files generated by this target.";
  1370. // Make sure all custom command outputs in this target are built.
  1371. if(this->CustomCommandDriver == OnBuild)
  1372. {
  1373. this->DriveCustomCommands(depends);
  1374. }
  1375. // Make sure the extra files are built.
  1376. depends.insert(depends.end(),
  1377. this->ExtraFiles.begin(), this->ExtraFiles.end());
  1378. }
  1379. // Write the driver rule.
  1380. std::vector<std::string> no_commands;
  1381. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, comment,
  1382. buildTargetRuleName,
  1383. depends, no_commands, true);
  1384. }
  1385. //----------------------------------------------------------------------------
  1386. std::string cmMakefileTargetGenerator::GetFrameworkFlags(std::string const& l)
  1387. {
  1388. if(!this->Makefile->IsOn("APPLE"))
  1389. {
  1390. return std::string();
  1391. }
  1392. std::string fwSearchFlagVar = "CMAKE_" + l + "_FRAMEWORK_SEARCH_FLAG";
  1393. const char* fwSearchFlag =
  1394. this->Makefile->GetDefinition(fwSearchFlagVar);
  1395. if(!(fwSearchFlag && *fwSearchFlag))
  1396. {
  1397. return std::string();
  1398. }
  1399. std::set<std::string> emitted;
  1400. #ifdef __APPLE__ /* don't insert this when crosscompiling e.g. to iphone */
  1401. emitted.insert("/System/Library/Frameworks");
  1402. #endif
  1403. std::vector<std::string> includes;
  1404. const std::string& config =
  1405. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  1406. this->LocalGenerator->GetIncludeDirectories(includes,
  1407. this->GeneratorTarget,
  1408. "C", config);
  1409. // check all include directories for frameworks as this
  1410. // will already have added a -F for the framework
  1411. for(std::vector<std::string>::iterator i = includes.begin();
  1412. i != includes.end(); ++i)
  1413. {
  1414. if(this->Target->NameResolvesToFramework(*i))
  1415. {
  1416. std::string frameworkDir = *i;
  1417. frameworkDir += "/../";
  1418. frameworkDir = cmSystemTools::CollapseFullPath(frameworkDir);
  1419. emitted.insert(frameworkDir);
  1420. }
  1421. }
  1422. std::string flags;
  1423. const char* cfg = this->LocalGenerator->ConfigurationName.c_str();
  1424. if(cmComputeLinkInformation* cli = this->Target->GetLinkInformation(cfg))
  1425. {
  1426. std::vector<std::string> const& frameworks = cli->GetFrameworkPaths();
  1427. for(std::vector<std::string>::const_iterator i = frameworks.begin();
  1428. i != frameworks.end(); ++i)
  1429. {
  1430. if(emitted.insert(*i).second)
  1431. {
  1432. flags += fwSearchFlag;
  1433. flags += this->Convert(*i,
  1434. cmLocalGenerator::START_OUTPUT,
  1435. cmLocalGenerator::SHELL, true);
  1436. flags += " ";
  1437. }
  1438. }
  1439. }
  1440. return flags;
  1441. }
  1442. //----------------------------------------------------------------------------
  1443. void cmMakefileTargetGenerator
  1444. ::AppendTargetDepends(std::vector<std::string>& depends)
  1445. {
  1446. // Static libraries never depend on anything for linking.
  1447. if(this->Target->GetType() == cmTarget::STATIC_LIBRARY)
  1448. {
  1449. return;
  1450. }
  1451. // Loop over all library dependencies.
  1452. const char* cfg = this->LocalGenerator->ConfigurationName.c_str();
  1453. if(cmComputeLinkInformation* cli = this->Target->GetLinkInformation(cfg))
  1454. {
  1455. std::vector<std::string> const& libDeps = cli->GetDepends();
  1456. depends.insert(depends.end(), libDeps.begin(), libDeps.end());
  1457. }
  1458. }
  1459. //----------------------------------------------------------------------------
  1460. void cmMakefileTargetGenerator
  1461. ::AppendObjectDepends(std::vector<std::string>& depends)
  1462. {
  1463. // Add dependencies on the compiled object files.
  1464. std::string relPath = this->LocalGenerator->GetHomeRelativeOutputPath();
  1465. std::string objTarget;
  1466. for(std::vector<std::string>::const_iterator obj = this->Objects.begin();
  1467. obj != this->Objects.end(); ++obj)
  1468. {
  1469. objTarget = relPath;
  1470. objTarget += *obj;
  1471. depends.push_back(objTarget);
  1472. }
  1473. // Add dependencies on the external object files.
  1474. depends.insert(depends.end(),
  1475. this->ExternalObjects.begin(), this->ExternalObjects.end());
  1476. // Add a dependency on the rule file itself.
  1477. this->LocalGenerator->AppendRuleDepend(depends,
  1478. this->BuildFileNameFull.c_str());
  1479. }
  1480. //----------------------------------------------------------------------------
  1481. void cmMakefileTargetGenerator
  1482. ::AppendLinkDepends(std::vector<std::string>& depends)
  1483. {
  1484. this->AppendObjectDepends(depends);
  1485. // Add dependencies on targets that must be built first.
  1486. this->AppendTargetDepends(depends);
  1487. // Add a dependency on the link definitions file, if any.
  1488. std::string def = this->GeneratorTarget->GetModuleDefinitionFile(
  1489. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE"));
  1490. if(!def.empty())
  1491. {
  1492. depends.push_back(def);
  1493. }
  1494. // Add user-specified dependencies.
  1495. if(const char* linkDepends =
  1496. this->Target->GetProperty("LINK_DEPENDS"))
  1497. {
  1498. cmSystemTools::ExpandListArgument(linkDepends, depends);
  1499. }
  1500. }
  1501. //----------------------------------------------------------------------------
  1502. std::string cmMakefileTargetGenerator::GetLinkRule(
  1503. const std::string& linkRuleVar)
  1504. {
  1505. std::string linkRule = this->Makefile->GetRequiredDefinition(linkRuleVar);
  1506. if(this->Target->HasImplibGNUtoMS())
  1507. {
  1508. std::string ruleVar = "CMAKE_";
  1509. ruleVar += this->Target->GetLinkerLanguage(this->ConfigName);
  1510. ruleVar += "_GNUtoMS_RULE";
  1511. if(const char* rule = this->Makefile->GetDefinition(ruleVar))
  1512. {
  1513. linkRule += rule;
  1514. }
  1515. }
  1516. return linkRule;
  1517. }
  1518. //----------------------------------------------------------------------------
  1519. void cmMakefileTargetGenerator
  1520. ::CloseFileStreams()
  1521. {
  1522. delete this->BuildFileStream;
  1523. delete this->InfoFileStream;
  1524. delete this->FlagFileStream;
  1525. }
  1526. void cmMakefileTargetGenerator::RemoveForbiddenFlags(const char* flagVar,
  1527. const std::string& linkLang,
  1528. std::string& linkFlags)
  1529. {
  1530. // check for language flags that are not allowed at link time, and
  1531. // remove them, -w on darwin for gcc -w -dynamiclib sends -w to libtool
  1532. // which fails, there may be more]
  1533. std::string removeFlags = "CMAKE_";
  1534. removeFlags += linkLang;
  1535. removeFlags += flagVar;
  1536. std::string removeflags =
  1537. this->Makefile->GetSafeDefinition(removeFlags);
  1538. std::vector<std::string> removeList;
  1539. cmSystemTools::ExpandListArgument(removeflags, removeList);
  1540. for(std::vector<std::string>::iterator i = removeList.begin();
  1541. i != removeList.end(); ++i)
  1542. {
  1543. std::string tmp;
  1544. std::string::size_type lastPosition = 0;
  1545. for(;;)
  1546. {
  1547. std::string::size_type position = linkFlags.find(*i, lastPosition);
  1548. if(position == std::string::npos)
  1549. {
  1550. tmp += linkFlags.substr(lastPosition);
  1551. break;
  1552. }
  1553. else
  1554. {
  1555. std::string::size_type prefixLength = position - lastPosition;
  1556. tmp += linkFlags.substr(lastPosition, prefixLength);
  1557. lastPosition = position + i->length();
  1558. bool validFlagStart = position == 0 ||
  1559. isspace(linkFlags[position - 1]);
  1560. bool validFlagEnd = lastPosition == linkFlags.size() ||
  1561. isspace(linkFlags[lastPosition]);
  1562. if(!validFlagStart || !validFlagEnd)
  1563. {
  1564. tmp += *i;
  1565. }
  1566. }
  1567. }
  1568. linkFlags = tmp;
  1569. }
  1570. }
  1571. //----------------------------------------------------------------------------
  1572. void
  1573. cmMakefileTargetGenerator
  1574. ::CreateLinkScript(const char* name,
  1575. std::vector<std::string> const& link_commands,
  1576. std::vector<std::string>& makefile_commands,
  1577. std::vector<std::string>& makefile_depends)
  1578. {
  1579. // Create the link script file.
  1580. std::string linkScriptName = this->TargetBuildDirectoryFull;
  1581. linkScriptName += "/";
  1582. linkScriptName += name;
  1583. cmGeneratedFileStream linkScriptStream(linkScriptName.c_str());
  1584. linkScriptStream.SetCopyIfDifferent(true);
  1585. for(std::vector<std::string>::const_iterator cmd = link_commands.begin();
  1586. cmd != link_commands.end(); ++cmd)
  1587. {
  1588. // Do not write out empty commands or commands beginning in the
  1589. // shell no-op ":".
  1590. if(!cmd->empty() && (*cmd)[0] != ':')
  1591. {
  1592. linkScriptStream << *cmd << "\n";
  1593. }
  1594. }
  1595. // Create the makefile command to invoke the link script.
  1596. std::string link_command = "$(CMAKE_COMMAND) -E cmake_link_script ";
  1597. link_command += this->Convert(linkScriptName,
  1598. cmLocalGenerator::START_OUTPUT,
  1599. cmLocalGenerator::SHELL);
  1600. link_command += " --verbose=$(VERBOSE)";
  1601. makefile_commands.push_back(link_command);
  1602. makefile_depends.push_back(linkScriptName);
  1603. }
  1604. //----------------------------------------------------------------------------
  1605. std::string
  1606. cmMakefileTargetGenerator
  1607. ::CreateResponseFile(const char* name, std::string const& options,
  1608. std::vector<std::string>& makefile_depends)
  1609. {
  1610. // Create the response file.
  1611. std::string responseFileNameFull = this->TargetBuildDirectoryFull;
  1612. responseFileNameFull += "/";
  1613. responseFileNameFull += name;
  1614. cmGeneratedFileStream responseStream(responseFileNameFull.c_str());
  1615. responseStream.SetCopyIfDifferent(true);
  1616. responseStream << options << "\n";
  1617. // Add a dependency so the target will rebuild when the set of
  1618. // objects changes.
  1619. makefile_depends.push_back(responseFileNameFull);
  1620. // Construct the name to be used on the command line.
  1621. std::string responseFileName = this->TargetBuildDirectory;
  1622. responseFileName += "/";
  1623. responseFileName += name;
  1624. return responseFileName;
  1625. }
  1626. //----------------------------------------------------------------------------
  1627. void
  1628. cmMakefileTargetGenerator
  1629. ::CreateLinkLibs(std::string& linkLibs, bool relink,
  1630. bool useResponseFile,
  1631. std::vector<std::string>& makefile_depends,
  1632. bool useWatcomQuote)
  1633. {
  1634. std::string frameworkPath;
  1635. std::string linkPath;
  1636. this->LocalGenerator
  1637. ->OutputLinkLibraries(linkLibs, frameworkPath, linkPath,
  1638. *this->GeneratorTarget, relink,
  1639. useResponseFile,
  1640. useWatcomQuote);
  1641. linkLibs = frameworkPath + linkPath + linkLibs;
  1642. if(useResponseFile && linkLibs.find_first_not_of(" ") != linkLibs.npos)
  1643. {
  1644. // Lookup the response file reference flag.
  1645. std::string responseFlagVar = "CMAKE_";
  1646. responseFlagVar += this->Target->GetLinkerLanguage(this->ConfigName);
  1647. responseFlagVar += "_RESPONSE_FILE_LINK_FLAG";
  1648. const char* responseFlag =
  1649. this->Makefile->GetDefinition(responseFlagVar);
  1650. if(!responseFlag)
  1651. {
  1652. responseFlag = "@";
  1653. }
  1654. // Create this response file.
  1655. std::string link_rsp =
  1656. this->CreateResponseFile("linklibs.rsp", linkLibs, makefile_depends);
  1657. // Reference the response file.
  1658. linkLibs = responseFlag;
  1659. linkLibs += this->Convert(link_rsp,
  1660. cmLocalGenerator::NONE,
  1661. cmLocalGenerator::SHELL);
  1662. }
  1663. }
  1664. //----------------------------------------------------------------------------
  1665. void
  1666. cmMakefileTargetGenerator
  1667. ::CreateObjectLists(bool useLinkScript, bool useArchiveRules,
  1668. bool useResponseFile, std::string& buildObjs,
  1669. std::vector<std::string>& makefile_depends,
  1670. bool useWatcomQuote)
  1671. {
  1672. std::string variableName;
  1673. std::string variableNameExternal;
  1674. this->WriteObjectsVariable(variableName, variableNameExternal,
  1675. useWatcomQuote);
  1676. if(useResponseFile)
  1677. {
  1678. // MSVC response files cannot exceed 128K.
  1679. std::string::size_type const responseFileLimit = 131000;
  1680. // Construct the individual object list strings.
  1681. std::vector<std::string> object_strings;
  1682. this->WriteObjectsStrings(object_strings, responseFileLimit);
  1683. // Lookup the response file reference flag.
  1684. std::string responseFlagVar = "CMAKE_";
  1685. responseFlagVar += this->Target->GetLinkerLanguage(this->ConfigName);
  1686. responseFlagVar += "_RESPONSE_FILE_LINK_FLAG";
  1687. const char* responseFlag =
  1688. this->Makefile->GetDefinition(responseFlagVar);
  1689. if(!responseFlag)
  1690. {
  1691. responseFlag = "@";
  1692. }
  1693. // Write a response file for each string.
  1694. const char* sep = "";
  1695. for(unsigned int i = 0; i < object_strings.size(); ++i)
  1696. {
  1697. // Number the response files.
  1698. char rsp[32];
  1699. sprintf(rsp, "objects%u.rsp", i+1);
  1700. // Create this response file.
  1701. std::string objects_rsp =
  1702. this->CreateResponseFile(rsp, object_strings[i], makefile_depends);
  1703. // Separate from previous response file references.
  1704. buildObjs += sep;
  1705. sep = " ";
  1706. // Reference the response file.
  1707. buildObjs += responseFlag;
  1708. buildObjs += this->Convert(objects_rsp,
  1709. cmLocalGenerator::NONE,
  1710. cmLocalGenerator::SHELL);
  1711. }
  1712. }
  1713. else if(useLinkScript)
  1714. {
  1715. if(!useArchiveRules)
  1716. {
  1717. this->WriteObjectsString(buildObjs);
  1718. }
  1719. }
  1720. else
  1721. {
  1722. buildObjs = "$(";
  1723. buildObjs += variableName;
  1724. buildObjs += ") $(";
  1725. buildObjs += variableNameExternal;
  1726. buildObjs += ")";
  1727. }
  1728. }
  1729. //----------------------------------------------------------------------------
  1730. void cmMakefileTargetGenerator::AddIncludeFlags(std::string& flags,
  1731. const std::string& lang)
  1732. {
  1733. std::string responseVar = "CMAKE_";
  1734. responseVar += lang;
  1735. responseVar += "_USE_RESPONSE_FILE_FOR_INCLUDES";
  1736. bool useResponseFile = this->Makefile->IsOn(responseVar);
  1737. std::vector<std::string> includes;
  1738. const std::string& config =
  1739. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  1740. this->LocalGenerator->GetIncludeDirectories(includes,
  1741. this->GeneratorTarget,
  1742. lang, config);
  1743. std::string includeFlags =
  1744. this->LocalGenerator->GetIncludeFlags(includes, this->GeneratorTarget,
  1745. lang, false, useResponseFile,
  1746. config);
  1747. if(includeFlags.empty())
  1748. {
  1749. return;
  1750. }
  1751. if(useResponseFile)
  1752. {
  1753. std::string name = "includes_";
  1754. name += lang;
  1755. name += ".rsp";
  1756. std::string arg = "@" +
  1757. this->CreateResponseFile(name.c_str(), includeFlags,
  1758. this->FlagFileDepends[lang]);
  1759. this->LocalGenerator->AppendFlags(flags, arg);
  1760. }
  1761. else
  1762. {
  1763. this->LocalGenerator->AppendFlags(flags, includeFlags);
  1764. }
  1765. }
  1766. //----------------------------------------------------------------------------
  1767. const char* cmMakefileTargetGenerator::GetFortranModuleDirectory()
  1768. {
  1769. // Compute the module directory.
  1770. if(!this->FortranModuleDirectoryComputed)
  1771. {
  1772. const char* target_mod_dir =
  1773. this->Target->GetProperty("Fortran_MODULE_DIRECTORY");
  1774. const char* moddir_flag =
  1775. this->Makefile->GetDefinition("CMAKE_Fortran_MODDIR_FLAG");
  1776. if(target_mod_dir && moddir_flag)
  1777. {
  1778. // Compute the full path to the module directory.
  1779. if(cmSystemTools::FileIsFullPath(target_mod_dir))
  1780. {
  1781. // Already a full path.
  1782. this->FortranModuleDirectory = target_mod_dir;
  1783. }
  1784. else
  1785. {
  1786. // Interpret relative to the current output directory.
  1787. this->FortranModuleDirectory =
  1788. this->Makefile->GetCurrentBinaryDirectory();
  1789. this->FortranModuleDirectory += "/";
  1790. this->FortranModuleDirectory += target_mod_dir;
  1791. }
  1792. // Make sure the module output directory exists.
  1793. cmSystemTools::MakeDirectory(this->FortranModuleDirectory.c_str());
  1794. }
  1795. this->FortranModuleDirectoryComputed = true;
  1796. }
  1797. // Return the computed directory.
  1798. if(this->FortranModuleDirectory.empty())
  1799. {
  1800. return 0;
  1801. }
  1802. else
  1803. {
  1804. return this->FortranModuleDirectory.c_str();
  1805. }
  1806. }
  1807. //----------------------------------------------------------------------------
  1808. void cmMakefileTargetGenerator::AddFortranFlags(std::string& flags)
  1809. {
  1810. // Enable module output if necessary.
  1811. if(const char* modout_flag =
  1812. this->Makefile->GetDefinition("CMAKE_Fortran_MODOUT_FLAG"))
  1813. {
  1814. this->LocalGenerator->AppendFlags(flags, modout_flag);
  1815. }
  1816. // Add a module output directory flag if necessary.
  1817. const char* mod_dir = this->GetFortranModuleDirectory();
  1818. if(!mod_dir)
  1819. {
  1820. mod_dir = this->Makefile->GetDefinition("CMAKE_Fortran_MODDIR_DEFAULT");
  1821. }
  1822. if(mod_dir)
  1823. {
  1824. const char* moddir_flag =
  1825. this->Makefile->GetRequiredDefinition("CMAKE_Fortran_MODDIR_FLAG");
  1826. std::string modflag = moddir_flag;
  1827. modflag += this->Convert(mod_dir,
  1828. cmLocalGenerator::START_OUTPUT,
  1829. cmLocalGenerator::SHELL);
  1830. this->LocalGenerator->AppendFlags(flags, modflag);
  1831. }
  1832. // If there is a separate module path flag then duplicate the
  1833. // include path with it. This compiler does not search the include
  1834. // path for modules.
  1835. if(const char* modpath_flag =
  1836. this->Makefile->GetDefinition("CMAKE_Fortran_MODPATH_FLAG"))
  1837. {
  1838. std::vector<std::string> includes;
  1839. const std::string& config =
  1840. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  1841. this->LocalGenerator->GetIncludeDirectories(includes,
  1842. this->GeneratorTarget,
  1843. "C", config);
  1844. for(std::vector<std::string>::const_iterator idi = includes.begin();
  1845. idi != includes.end(); ++idi)
  1846. {
  1847. std::string flg = modpath_flag;
  1848. flg += this->Convert(*idi,
  1849. cmLocalGenerator::NONE,
  1850. cmLocalGenerator::SHELL);
  1851. this->LocalGenerator->AppendFlags(flags, flg);
  1852. }
  1853. }
  1854. }
  1855. //----------------------------------------------------------------------------
  1856. void cmMakefileTargetGenerator::AddModuleDefinitionFlag(std::string& flags)
  1857. {
  1858. std::string def = this->GeneratorTarget->GetModuleDefinitionFile(
  1859. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE"));
  1860. if(def.empty())
  1861. {
  1862. return;
  1863. }
  1864. // TODO: Create a per-language flag variable.
  1865. const char* defFileFlag =
  1866. this->Makefile->GetDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  1867. if(!defFileFlag)
  1868. {
  1869. return;
  1870. }
  1871. // Append the flag and value. Use ConvertToLinkReference to help
  1872. // vs6's "cl -link" pass it to the linker.
  1873. std::string flag = defFileFlag;
  1874. flag += (this->LocalGenerator->ConvertToLinkReference(def));
  1875. this->LocalGenerator->AppendFlags(flags, flag);
  1876. }
  1877. //----------------------------------------------------------------------------
  1878. const char* cmMakefileTargetGenerator::GetFeature(const std::string& feature)
  1879. {
  1880. return this->Target->GetFeature(feature, this->ConfigName);
  1881. }
  1882. //----------------------------------------------------------------------------
  1883. bool cmMakefileTargetGenerator::GetFeatureAsBool(const std::string& feature)
  1884. {
  1885. return this->Target->GetFeatureAsBool(feature, this->ConfigName);
  1886. }
  1887. //----------------------------------------------------------------------------
  1888. void cmMakefileTargetGenerator::AddFeatureFlags(
  1889. std::string& flags, const std::string& lang
  1890. )
  1891. {
  1892. // Add language-specific flags.
  1893. this->LocalGenerator->AddLanguageFlags(flags, lang, this->ConfigName);
  1894. if(this->GetFeatureAsBool("INTERPROCEDURAL_OPTIMIZATION"))
  1895. {
  1896. this->LocalGenerator->AppendFeatureOptions(flags, lang, "IPO");
  1897. }
  1898. }