cmMakefileTargetGenerator.cxx 73 KB

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