cmMakefileTargetGenerator.cxx 72 KB

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