cmMakefileTargetGenerator.cxx 69 KB

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