cmMakefileTargetGenerator.cxx 72 KB

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