cmMakefileTargetGenerator.cxx 73 KB

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