cmMakefileTargetGenerator.cxx 69 KB

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