cmMakefileTargetGenerator.cxx 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030
  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, lang);
  243. // Add include directory flags.
  244. this->AddIncludeFlags(flags, lang);
  245. // Append old-style preprocessor definition flags.
  246. this->LocalGenerator->
  247. AppendFlags(flags, this->Makefile->GetDefineFlags());
  248. // Add include directory flags.
  249. this->LocalGenerator->
  250. AppendFlags(flags,this->GetFrameworkFlags().c_str());
  251. ByLanguageMap::value_type entry(l, flags);
  252. i = this->FlagsByLanguage.insert(entry).first;
  253. }
  254. return i->second;
  255. }
  256. std::string cmMakefileTargetGenerator::GetDefines(const std::string &l)
  257. {
  258. ByLanguageMap::iterator i = this->DefinesByLanguage.find(l);
  259. if (i == this->DefinesByLanguage.end())
  260. {
  261. std::set<std::string> defines;
  262. const char *lang = l.c_str();
  263. // Add the export symbol definition for shared library objects.
  264. if(const char* exportMacro = this->Target->GetExportMacro())
  265. {
  266. this->LocalGenerator->AppendDefines(defines, exportMacro);
  267. }
  268. // Add preprocessor definitions for this target and configuration.
  269. this->LocalGenerator->AppendDefines
  270. (defines, this->GeneratorTarget->GetCompileDefinitions());
  271. this->LocalGenerator->AppendDefines
  272. (defines, this->GeneratorTarget->GetCompileDefinitions(
  273. this->LocalGenerator->ConfigurationName.c_str()));
  274. std::string definesString;
  275. this->LocalGenerator->JoinDefines(defines, definesString, lang);
  276. ByLanguageMap::value_type entry(l, definesString);
  277. i = this->DefinesByLanguage.insert(entry).first;
  278. }
  279. return i->second;
  280. }
  281. void cmMakefileTargetGenerator::WriteTargetLanguageFlags()
  282. {
  283. // write language flags for target
  284. std::set<cmStdString> languages;
  285. this->Target->GetLanguages(languages);
  286. // put the compiler in the rules.make file so that if it changes
  287. // things rebuild
  288. for(std::set<cmStdString>::const_iterator l = languages.begin();
  289. l != languages.end(); ++l)
  290. {
  291. cmStdString compiler = "CMAKE_";
  292. compiler += *l;
  293. compiler += "_COMPILER";
  294. *this->FlagFileStream << "# compile " << l->c_str() << " with " <<
  295. this->Makefile->GetSafeDefinition(compiler.c_str()) << "\n";
  296. }
  297. for(std::set<cmStdString>::const_iterator l = languages.begin();
  298. l != languages.end(); ++l)
  299. {
  300. *this->FlagFileStream << *l << "_FLAGS = " << this->GetFlags(*l) << "\n\n";
  301. *this->FlagFileStream << *l << "_DEFINES = " << this->GetDefines(*l) <<
  302. "\n\n";
  303. }
  304. // Add target-specific flags.
  305. if(this->Target->GetProperty("COMPILE_FLAGS"))
  306. {
  307. std::string flags;
  308. this->LocalGenerator->AppendFlags
  309. (flags, this->Target->GetProperty("COMPILE_FLAGS"));
  310. *this->FlagFileStream << "# TARGET_FLAGS = " << flags << "\n\n";
  311. }
  312. }
  313. //----------------------------------------------------------------------------
  314. void
  315. cmMakefileTargetGenerator::MacOSXContentGeneratorType::operator()
  316. (cmSourceFile& source, const char* pkgloc)
  317. {
  318. // Skip OS X content when not building a Framework or Bundle.
  319. if(this->Generator->MacContentDirectory.empty())
  320. {
  321. return;
  322. }
  323. std::string macdir =
  324. this->Generator->OSXBundleGenerator->InitMacOSXContentDirectory(pkgloc);
  325. // Get the input file location.
  326. std::string input = source.GetFullPath();
  327. // Get the output file location.
  328. std::string output = macdir;
  329. output += "/";
  330. output += cmSystemTools::GetFilenameName(input);
  331. this->Generator->CleanFiles.push_back(
  332. this->Generator->Convert(output.c_str(),
  333. cmLocalGenerator::START_OUTPUT));
  334. output = this->Generator->Convert(output.c_str(),
  335. cmLocalGenerator::HOME_OUTPUT);
  336. // Create a rule to copy the content into the bundle.
  337. std::vector<std::string> depends;
  338. std::vector<std::string> commands;
  339. depends.push_back(input);
  340. std::string copyEcho = "Copying OS X content ";
  341. copyEcho += output;
  342. this->Generator->LocalGenerator->AppendEcho(
  343. commands, copyEcho.c_str(),
  344. cmLocalUnixMakefileGenerator3::EchoBuild);
  345. std::string copyCommand = "$(CMAKE_COMMAND) -E copy ";
  346. copyCommand += this->Generator->Convert(input.c_str(),
  347. cmLocalGenerator::NONE,
  348. cmLocalGenerator::SHELL);
  349. copyCommand += " ";
  350. copyCommand += this->Generator->Convert(output.c_str(),
  351. cmLocalGenerator::NONE,
  352. cmLocalGenerator::SHELL);
  353. commands.push_back(copyCommand);
  354. this->Generator->LocalGenerator->WriteMakeRule(
  355. *this->Generator->BuildFileStream, 0,
  356. output.c_str(),
  357. depends, commands, false);
  358. this->Generator->ExtraFiles.insert(output);
  359. }
  360. //----------------------------------------------------------------------------
  361. void cmMakefileTargetGenerator::WriteObjectRuleFiles(cmSourceFile& source)
  362. {
  363. // Identify the language of the source file.
  364. const char* lang = this->LocalGenerator->GetSourceFileLanguage(source);
  365. if(!lang)
  366. {
  367. // don't know anything about this file so skip it
  368. return;
  369. }
  370. // Get the full path name of the object file.
  371. std::string const& objectName = this->GeneratorTarget->Objects[&source];
  372. std::string obj = this->LocalGenerator->GetTargetDirectory(*this->Target);
  373. obj += "/";
  374. obj += objectName;
  375. // Avoid generating duplicate rules.
  376. if(this->ObjectFiles.find(obj) == this->ObjectFiles.end())
  377. {
  378. this->ObjectFiles.insert(obj);
  379. }
  380. else
  381. {
  382. cmOStringStream err;
  383. err << "Warning: Source file \""
  384. << source.GetFullPath()
  385. << "\" is listed multiple times for target \""
  386. << this->Target->GetName()
  387. << "\".";
  388. cmSystemTools::Message(err.str().c_str(), "Warning");
  389. return;
  390. }
  391. // Create the directory containing the object file. This may be a
  392. // subdirectory under the target's directory.
  393. std::string dir = cmSystemTools::GetFilenamePath(obj.c_str());
  394. cmSystemTools::MakeDirectory
  395. (this->LocalGenerator->ConvertToFullPath(dir).c_str());
  396. // Save this in the target's list of object files.
  397. this->Objects.push_back(obj);
  398. this->CleanFiles.push_back(obj);
  399. // TODO: Remove
  400. //std::string relativeObj
  401. //= this->LocalGenerator->GetHomeRelativeOutputPath();
  402. //relativeObj += obj;
  403. // we compute some depends when writing the depend.make that we will also
  404. // use in the build.make, same with depMakeFile
  405. std::vector<std::string> depends;
  406. std::string depMakeFile;
  407. // generate the build rule file
  408. this->WriteObjectBuildFile(obj, lang, source, depends);
  409. // The object file should be checked for dependency integrity.
  410. std::string objFullPath = this->Makefile->GetCurrentOutputDirectory();
  411. objFullPath += "/";
  412. objFullPath += obj;
  413. objFullPath =
  414. this->Convert(objFullPath.c_str(), cmLocalGenerator::FULL);
  415. std::string srcFullPath =
  416. this->Convert(source.GetFullPath().c_str(), cmLocalGenerator::FULL);
  417. this->LocalGenerator->
  418. AddImplicitDepends(*this->Target, lang,
  419. objFullPath.c_str(),
  420. srcFullPath.c_str());
  421. }
  422. //----------------------------------------------------------------------------
  423. void
  424. cmMakefileTargetGenerator
  425. ::AppendFortranFormatFlags(std::string& flags, cmSourceFile& source)
  426. {
  427. const char* srcfmt = source.GetProperty("Fortran_FORMAT");
  428. cmLocalGenerator::FortranFormat format =
  429. this->LocalGenerator->GetFortranFormat(srcfmt);
  430. if(format == cmLocalGenerator::FortranFormatNone)
  431. {
  432. const char* tgtfmt = this->Target->GetProperty("Fortran_FORMAT");
  433. format = this->LocalGenerator->GetFortranFormat(tgtfmt);
  434. }
  435. const char* var = 0;
  436. switch (format)
  437. {
  438. case cmLocalGenerator::FortranFormatFixed:
  439. var = "CMAKE_Fortran_FORMAT_FIXED_FLAG"; break;
  440. case cmLocalGenerator::FortranFormatFree:
  441. var = "CMAKE_Fortran_FORMAT_FREE_FLAG"; break;
  442. default: break;
  443. }
  444. if(var)
  445. {
  446. this->LocalGenerator->AppendFlags(
  447. flags, this->Makefile->GetDefinition(var));
  448. }
  449. }
  450. //----------------------------------------------------------------------------
  451. void
  452. cmMakefileTargetGenerator
  453. ::WriteObjectBuildFile(std::string &obj,
  454. const char *lang,
  455. cmSourceFile& source,
  456. std::vector<std::string>& depends)
  457. {
  458. this->LocalGenerator->AppendRuleDepend(depends,
  459. this->FlagFileNameFull.c_str());
  460. this->LocalGenerator->AppendRuleDepends(depends,
  461. this->FlagFileDepends[lang]);
  462. // generate the depend scanning rule
  463. this->WriteObjectDependRules(source, depends);
  464. std::string relativeObj = this->LocalGenerator->GetHomeRelativeOutputPath();
  465. relativeObj += obj;
  466. // Write the build rule.
  467. // Build the set of compiler flags.
  468. std::string flags;
  469. // Add language-specific flags.
  470. std::string langFlags = "$(";
  471. langFlags += lang;
  472. langFlags += "_FLAGS)";
  473. this->LocalGenerator->AppendFlags(flags, langFlags.c_str());
  474. // Add target-specific flags.
  475. if(this->Target->GetProperty("COMPILE_FLAGS"))
  476. {
  477. std::string langIncludeExpr = "CMAKE_";
  478. langIncludeExpr += lang;
  479. langIncludeExpr += "_FLAG_REGEX";
  480. const char* regex = this->Makefile->
  481. GetDefinition(langIncludeExpr.c_str());
  482. if(regex)
  483. {
  484. cmsys::RegularExpression r(regex);
  485. std::vector<std::string> args;
  486. cmSystemTools::ParseWindowsCommandLine(
  487. this->Target->GetProperty("COMPILE_FLAGS"),
  488. args);
  489. for(std::vector<std::string>::iterator i = args.begin();
  490. i != args.end(); ++i)
  491. {
  492. if(r.find(i->c_str()))
  493. {
  494. this->LocalGenerator->AppendFlags
  495. (flags, i->c_str());
  496. }
  497. }
  498. }
  499. else
  500. {
  501. this->LocalGenerator->AppendFlags
  502. (flags, this->Target->GetProperty("COMPILE_FLAGS"));
  503. }
  504. }
  505. // Add Fortran format flags.
  506. if(strcmp(lang, "Fortran") == 0)
  507. {
  508. this->AppendFortranFormatFlags(flags, source);
  509. }
  510. // Add flags from source file properties.
  511. if (source.GetProperty("COMPILE_FLAGS"))
  512. {
  513. this->LocalGenerator->AppendFlags
  514. (flags, source.GetProperty("COMPILE_FLAGS"));
  515. *this->FlagFileStream << "# Custom flags: "
  516. << relativeObj << "_FLAGS = "
  517. << source.GetProperty("COMPILE_FLAGS")
  518. << "\n"
  519. << "\n";
  520. }
  521. // Add language-specific defines.
  522. std::set<std::string> defines;
  523. // Add source-sepcific preprocessor definitions.
  524. if(const char* compile_defs = source.GetProperty("COMPILE_DEFINITIONS"))
  525. {
  526. this->LocalGenerator->AppendDefines(defines, compile_defs);
  527. *this->FlagFileStream << "# Custom defines: "
  528. << relativeObj << "_DEFINES = "
  529. << compile_defs << "\n"
  530. << "\n";
  531. }
  532. std::string configUpper =
  533. cmSystemTools::UpperCase(this->LocalGenerator->ConfigurationName);
  534. std::string defPropName = "COMPILE_DEFINITIONS_";
  535. defPropName += configUpper;
  536. if(const char* config_compile_defs =
  537. source.GetProperty(defPropName.c_str()))
  538. {
  539. this->LocalGenerator->AppendDefines(defines, config_compile_defs);
  540. *this->FlagFileStream
  541. << "# Custom defines: "
  542. << relativeObj << "_DEFINES_" << configUpper
  543. << " = " << config_compile_defs << "\n"
  544. << "\n";
  545. }
  546. // Get the output paths for source and object files.
  547. std::string sourceFile = source.GetFullPath();
  548. if(this->LocalGenerator->UseRelativePaths)
  549. {
  550. sourceFile = this->Convert(sourceFile.c_str(),
  551. cmLocalGenerator::START_OUTPUT);
  552. }
  553. sourceFile = this->Convert(sourceFile.c_str(),
  554. cmLocalGenerator::NONE,
  555. cmLocalGenerator::SHELL);
  556. // Construct the build message.
  557. std::vector<std::string> no_commands;
  558. std::vector<std::string> commands;
  559. // add in a progress call if needed
  560. this->AppendProgress(commands);
  561. if(!this->NoRuleMessages)
  562. {
  563. std::string buildEcho = "Building ";
  564. buildEcho += lang;
  565. buildEcho += " object ";
  566. buildEcho += relativeObj;
  567. this->LocalGenerator->AppendEcho
  568. (commands, buildEcho.c_str(), cmLocalUnixMakefileGenerator3::EchoBuild);
  569. }
  570. std::string targetOutPathPDB;
  571. {
  572. std::string targetFullPathPDB;
  573. if(this->Target->GetType() == cmTarget::EXECUTABLE ||
  574. this->Target->GetType() == cmTarget::STATIC_LIBRARY ||
  575. this->Target->GetType() == cmTarget::SHARED_LIBRARY ||
  576. this->Target->GetType() == cmTarget::MODULE_LIBRARY)
  577. {
  578. targetFullPathPDB = this->Target->GetPDBDirectory(this->ConfigName);
  579. targetFullPathPDB += "/";
  580. targetFullPathPDB += this->Target->GetPDBName(this->ConfigName);
  581. }
  582. targetOutPathPDB =
  583. this->Convert(targetFullPathPDB.c_str(),cmLocalGenerator::NONE,
  584. cmLocalGenerator::SHELL);
  585. }
  586. cmLocalGenerator::RuleVariables vars;
  587. vars.RuleLauncher = "RULE_LAUNCH_COMPILE";
  588. vars.CMTarget = this->Target;
  589. vars.Language = lang;
  590. vars.TargetPDB = targetOutPathPDB.c_str();
  591. vars.Source = sourceFile.c_str();
  592. std::string shellObj =
  593. this->Convert(obj.c_str(),
  594. cmLocalGenerator::NONE,
  595. cmLocalGenerator::SHELL).c_str();
  596. vars.Object = shellObj.c_str();
  597. std::string objectDir = cmSystemTools::GetFilenamePath(obj);
  598. objectDir = this->Convert(objectDir.c_str(),
  599. cmLocalGenerator::START_OUTPUT,
  600. cmLocalGenerator::SHELL);
  601. vars.ObjectDir = objectDir.c_str();
  602. vars.Flags = flags.c_str();
  603. std::string definesString = "$(";
  604. definesString += lang;
  605. definesString += "_DEFINES)";
  606. this->LocalGenerator->JoinDefines(defines, definesString, lang);
  607. vars.Defines = definesString.c_str();
  608. bool lang_is_c_or_cxx = ((strcmp(lang, "C") == 0) ||
  609. (strcmp(lang, "CXX") == 0));
  610. // Construct the compile rules.
  611. {
  612. std::string compileRuleVar = "CMAKE_";
  613. compileRuleVar += lang;
  614. compileRuleVar += "_COMPILE_OBJECT";
  615. std::string compileRule =
  616. this->Makefile->GetRequiredDefinition(compileRuleVar.c_str());
  617. std::vector<std::string> compileCommands;
  618. cmSystemTools::ExpandListArgument(compileRule, compileCommands);
  619. if (this->Makefile->IsOn("CMAKE_EXPORT_COMPILE_COMMANDS") &&
  620. lang_is_c_or_cxx && compileCommands.size() == 1)
  621. {
  622. std::string compileCommand = compileCommands[0];
  623. this->LocalGenerator->ExpandRuleVariables(compileCommand, vars);
  624. std::string workingDirectory =
  625. this->LocalGenerator->Convert(
  626. this->Makefile->GetStartOutputDirectory(), cmLocalGenerator::FULL);
  627. compileCommand.replace(compileCommand.find(langFlags),
  628. langFlags.size(), this->GetFlags(lang));
  629. std::string langDefines = std::string("$(") + lang + "_DEFINES)";
  630. compileCommand.replace(compileCommand.find(langDefines),
  631. langDefines.size(), this->GetDefines(lang));
  632. this->GlobalGenerator->AddCXXCompileCommand(
  633. source.GetFullPath(), workingDirectory, compileCommand);
  634. }
  635. // Expand placeholders in the commands.
  636. for(std::vector<std::string>::iterator i = compileCommands.begin();
  637. i != compileCommands.end(); ++i)
  638. {
  639. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  640. }
  641. // Change the command working directory to the local build tree.
  642. this->LocalGenerator->CreateCDCommand
  643. (compileCommands,
  644. this->Makefile->GetStartOutputDirectory(),
  645. cmLocalGenerator::HOME_OUTPUT);
  646. commands.insert(commands.end(),
  647. compileCommands.begin(), compileCommands.end());
  648. }
  649. // Write the rule.
  650. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  651. relativeObj.c_str(),
  652. depends, commands, false);
  653. // Check for extra outputs created by the compilation.
  654. if(const char* extra_outputs_str =
  655. source.GetProperty("OBJECT_OUTPUTS"))
  656. {
  657. std::vector<std::string> extra_outputs;
  658. cmSystemTools::ExpandListArgument(extra_outputs_str, extra_outputs);
  659. for(std::vector<std::string>::const_iterator eoi = extra_outputs.begin();
  660. eoi != extra_outputs.end(); ++eoi)
  661. {
  662. // Register this as an extra output for the object file rule.
  663. // This will cause the object file to be rebuilt if the extra
  664. // output is missing.
  665. this->GenerateExtraOutput(eoi->c_str(), relativeObj.c_str(), false);
  666. // Register this as an extra file to clean.
  667. this->CleanFiles.push_back(eoi->c_str());
  668. }
  669. }
  670. bool do_preprocess_rules = lang_is_c_or_cxx &&
  671. this->LocalGenerator->GetCreatePreprocessedSourceRules();
  672. bool do_assembly_rules = lang_is_c_or_cxx &&
  673. this->LocalGenerator->GetCreateAssemblySourceRules();
  674. if(do_preprocess_rules || do_assembly_rules)
  675. {
  676. std::vector<std::string> force_depends;
  677. force_depends.push_back("cmake_force");
  678. std::string::size_type dot_pos = relativeObj.rfind(".");
  679. std::string relativeObjBase = relativeObj.substr(0, dot_pos);
  680. dot_pos = obj.rfind(".");
  681. std::string objBase = obj.substr(0, dot_pos);
  682. if(do_preprocess_rules)
  683. {
  684. commands.clear();
  685. std::string relativeObjI = relativeObjBase + ".i";
  686. std::string objI = objBase + ".i";
  687. std::string preprocessEcho = "Preprocessing ";
  688. preprocessEcho += lang;
  689. preprocessEcho += " source to ";
  690. preprocessEcho += objI;
  691. this->LocalGenerator->AppendEcho(
  692. commands, preprocessEcho.c_str(),
  693. cmLocalUnixMakefileGenerator3::EchoBuild
  694. );
  695. std::string preprocessRuleVar = "CMAKE_";
  696. preprocessRuleVar += lang;
  697. preprocessRuleVar += "_CREATE_PREPROCESSED_SOURCE";
  698. if(const char* preprocessRule =
  699. this->Makefile->GetDefinition(preprocessRuleVar.c_str()))
  700. {
  701. std::vector<std::string> preprocessCommands;
  702. cmSystemTools::ExpandListArgument(preprocessRule, preprocessCommands);
  703. std::string shellObjI =
  704. this->Convert(objI.c_str(),
  705. cmLocalGenerator::NONE,
  706. cmLocalGenerator::SHELL).c_str();
  707. vars.PreprocessedSource = shellObjI.c_str();
  708. // Expand placeholders in the commands.
  709. for(std::vector<std::string>::iterator i = preprocessCommands.begin();
  710. i != preprocessCommands.end(); ++i)
  711. {
  712. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  713. }
  714. this->LocalGenerator->CreateCDCommand
  715. (preprocessCommands,
  716. this->Makefile->GetStartOutputDirectory(),
  717. cmLocalGenerator::HOME_OUTPUT);
  718. commands.insert(commands.end(),
  719. preprocessCommands.begin(),
  720. preprocessCommands.end());
  721. }
  722. else
  723. {
  724. std::string cmd = "$(CMAKE_COMMAND) -E cmake_unimplemented_variable ";
  725. cmd += preprocessRuleVar;
  726. commands.push_back(cmd);
  727. }
  728. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  729. relativeObjI.c_str(),
  730. force_depends, commands, false);
  731. }
  732. if(do_assembly_rules)
  733. {
  734. commands.clear();
  735. std::string relativeObjS = relativeObjBase + ".s";
  736. std::string objS = objBase + ".s";
  737. std::string assemblyEcho = "Compiling ";
  738. assemblyEcho += lang;
  739. assemblyEcho += " source to assembly ";
  740. assemblyEcho += objS;
  741. this->LocalGenerator->AppendEcho(
  742. commands, assemblyEcho.c_str(),
  743. cmLocalUnixMakefileGenerator3::EchoBuild
  744. );
  745. std::string assemblyRuleVar = "CMAKE_";
  746. assemblyRuleVar += lang;
  747. assemblyRuleVar += "_CREATE_ASSEMBLY_SOURCE";
  748. if(const char* assemblyRule =
  749. this->Makefile->GetDefinition(assemblyRuleVar.c_str()))
  750. {
  751. std::vector<std::string> assemblyCommands;
  752. cmSystemTools::ExpandListArgument(assemblyRule, assemblyCommands);
  753. std::string shellObjS =
  754. this->Convert(objS.c_str(),
  755. cmLocalGenerator::NONE,
  756. cmLocalGenerator::SHELL).c_str();
  757. vars.AssemblySource = shellObjS.c_str();
  758. // Expand placeholders in the commands.
  759. for(std::vector<std::string>::iterator i = assemblyCommands.begin();
  760. i != assemblyCommands.end(); ++i)
  761. {
  762. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  763. }
  764. this->LocalGenerator->CreateCDCommand
  765. (assemblyCommands,
  766. this->Makefile->GetStartOutputDirectory(),
  767. cmLocalGenerator::HOME_OUTPUT);
  768. commands.insert(commands.end(),
  769. assemblyCommands.begin(),
  770. assemblyCommands.end());
  771. }
  772. else
  773. {
  774. std::string cmd = "$(CMAKE_COMMAND) -E cmake_unimplemented_variable ";
  775. cmd += assemblyRuleVar;
  776. commands.push_back(cmd);
  777. }
  778. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  779. relativeObjS.c_str(),
  780. force_depends, commands, false);
  781. }
  782. }
  783. // If the language needs provides-requires mode, create the
  784. // corresponding targets.
  785. std::string objectRequires = relativeObj;
  786. objectRequires += ".requires";
  787. std::vector<std::string> p_depends;
  788. // always provide an empty requires target
  789. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  790. objectRequires.c_str(), p_depends,
  791. no_commands, true);
  792. // write a build rule to recursively build what this obj provides
  793. std::string objectProvides = relativeObj;
  794. objectProvides += ".provides";
  795. std::string temp = relativeObj;
  796. temp += ".provides.build";
  797. std::vector<std::string> r_commands;
  798. std::string tgtMakefileName =
  799. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  800. tgtMakefileName += "/build.make";
  801. r_commands.push_back
  802. (this->LocalGenerator->GetRecursiveMakeCall(tgtMakefileName.c_str(),
  803. temp.c_str()));
  804. p_depends.clear();
  805. p_depends.push_back(objectRequires);
  806. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  807. objectProvides.c_str(), p_depends,
  808. r_commands, true);
  809. // write the provides.build rule dependency on the obj file
  810. p_depends.clear();
  811. p_depends.push_back(relativeObj);
  812. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  813. temp.c_str(), p_depends, no_commands,
  814. false);
  815. }
  816. //----------------------------------------------------------------------------
  817. void cmMakefileTargetGenerator::WriteTargetRequiresRules()
  818. {
  819. std::vector<std::string> depends;
  820. std::vector<std::string> no_commands;
  821. // Construct the name of the dependency generation target.
  822. std::string depTarget =
  823. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  824. depTarget += "/requires";
  825. // This target drives dependency generation for all object files.
  826. std::string relPath = this->LocalGenerator->GetHomeRelativeOutputPath();
  827. std::string objTarget;
  828. for(std::vector<std::string>::const_iterator obj = this->Objects.begin();
  829. obj != this->Objects.end(); ++obj)
  830. {
  831. objTarget = relPath;
  832. objTarget += *obj;
  833. objTarget += ".requires";
  834. depends.push_back(objTarget);
  835. }
  836. // Write the rule.
  837. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  838. depTarget.c_str(),
  839. depends, no_commands, true);
  840. }
  841. //----------------------------------------------------------------------------
  842. void cmMakefileTargetGenerator::WriteTargetCleanRules()
  843. {
  844. std::vector<std::string> depends;
  845. std::vector<std::string> commands;
  846. // Construct the clean target name.
  847. std::string cleanTarget =
  848. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  849. cleanTarget += "/clean";
  850. // Construct the clean command.
  851. this->LocalGenerator->AppendCleanCommand(commands, this->CleanFiles,
  852. *this->Target);
  853. this->LocalGenerator->CreateCDCommand
  854. (commands,
  855. this->Makefile->GetStartOutputDirectory(),
  856. cmLocalGenerator::HOME_OUTPUT);
  857. // Write the rule.
  858. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  859. cleanTarget.c_str(),
  860. depends, commands, true);
  861. }
  862. //----------------------------------------------------------------------------
  863. void cmMakefileTargetGenerator::WriteTargetDependRules()
  864. {
  865. // must write the targets depend info file
  866. std::string dir = this->LocalGenerator->GetTargetDirectory(*this->Target);
  867. this->InfoFileNameFull = dir;
  868. this->InfoFileNameFull += "/DependInfo.cmake";
  869. this->InfoFileNameFull =
  870. this->LocalGenerator->ConvertToFullPath(this->InfoFileNameFull);
  871. this->InfoFileStream =
  872. new cmGeneratedFileStream(this->InfoFileNameFull.c_str());
  873. this->InfoFileStream->SetCopyIfDifferent(true);
  874. if(!*this->InfoFileStream)
  875. {
  876. return;
  877. }
  878. this->LocalGenerator->
  879. WriteDependLanguageInfo(*this->InfoFileStream,*this->Target);
  880. // Store multiple output pairs in the depend info file.
  881. if(!this->MultipleOutputPairs.empty())
  882. {
  883. *this->InfoFileStream
  884. << "\n"
  885. << "# Pairs of files generated by the same build rule.\n"
  886. << "SET(CMAKE_MULTIPLE_OUTPUT_PAIRS\n";
  887. for(MultipleOutputPairsType::const_iterator pi =
  888. this->MultipleOutputPairs.begin();
  889. pi != this->MultipleOutputPairs.end(); ++pi)
  890. {
  891. *this->InfoFileStream
  892. << " " << this->LocalGenerator->EscapeForCMake(pi->first.c_str())
  893. << " " << this->LocalGenerator->EscapeForCMake(pi->second.c_str())
  894. << "\n";
  895. }
  896. *this->InfoFileStream << " )\n\n";
  897. }
  898. // Store list of targets linked directly or transitively.
  899. {
  900. *this->InfoFileStream
  901. << "\n"
  902. << "# Targets to which this target links.\n"
  903. << "SET(CMAKE_TARGET_LINKED_INFO_FILES\n";
  904. std::set<cmTarget const*> emitted;
  905. const char* cfg = this->LocalGenerator->ConfigurationName.c_str();
  906. if(cmComputeLinkInformation* cli = this->Target->GetLinkInformation(cfg))
  907. {
  908. cmComputeLinkInformation::ItemVector const& items = cli->GetItems();
  909. for(cmComputeLinkInformation::ItemVector::const_iterator
  910. i = items.begin(); i != items.end(); ++i)
  911. {
  912. cmTarget const* linkee = i->Target;
  913. if(linkee && !linkee->IsImported() && emitted.insert(linkee).second)
  914. {
  915. cmMakefile* mf = linkee->GetMakefile();
  916. cmLocalGenerator* lg = mf->GetLocalGenerator();
  917. std::string di = mf->GetStartOutputDirectory();
  918. di += "/";
  919. di += lg->GetTargetDirectory(*linkee);
  920. di += "/DependInfo.cmake";
  921. *this->InfoFileStream << " \"" << di << "\"\n";
  922. }
  923. }
  924. }
  925. *this->InfoFileStream
  926. << " )\n";
  927. }
  928. // Check for a target-specific module output directory.
  929. if(const char* mdir = this->GetFortranModuleDirectory())
  930. {
  931. *this->InfoFileStream
  932. << "\n"
  933. << "# Fortran module output directory.\n"
  934. << "SET(CMAKE_Fortran_TARGET_MODULE_DIR \"" << mdir << "\")\n";
  935. }
  936. // Target-specific include directories:
  937. *this->InfoFileStream
  938. << "\n"
  939. << "# The include file search paths:\n";
  940. *this->InfoFileStream
  941. << "SET(CMAKE_C_TARGET_INCLUDE_PATH\n";
  942. std::vector<std::string> includes;
  943. const char *config = this->Makefile->GetDefinition("CMAKE_BUILD_TYPE");
  944. this->LocalGenerator->GetIncludeDirectories(includes,
  945. this->GeneratorTarget,
  946. "C", config);
  947. for(std::vector<std::string>::iterator i = includes.begin();
  948. i != includes.end(); ++i)
  949. {
  950. *this->InfoFileStream
  951. << " \""
  952. << this->LocalGenerator->Convert(i->c_str(),
  953. cmLocalGenerator::HOME_OUTPUT)
  954. << "\"\n";
  955. }
  956. *this->InfoFileStream
  957. << " )\n";
  958. *this->InfoFileStream
  959. << "SET(CMAKE_CXX_TARGET_INCLUDE_PATH "
  960. << "${CMAKE_C_TARGET_INCLUDE_PATH})\n";
  961. *this->InfoFileStream
  962. << "SET(CMAKE_Fortran_TARGET_INCLUDE_PATH "
  963. << "${CMAKE_C_TARGET_INCLUDE_PATH})\n";
  964. *this->InfoFileStream
  965. << "SET(CMAKE_ASM_TARGET_INCLUDE_PATH "
  966. << "${CMAKE_C_TARGET_INCLUDE_PATH})\n";
  967. // and now write the rule to use it
  968. std::vector<std::string> depends;
  969. std::vector<std::string> commands;
  970. // Construct the name of the dependency generation target.
  971. std::string depTarget =
  972. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  973. depTarget += "/depend";
  974. // Add a command to call CMake to scan dependencies. CMake will
  975. // touch the corresponding depends file after scanning dependencies.
  976. cmOStringStream depCmd;
  977. // TODO: Account for source file properties and directory-level
  978. // definitions when scanning for dependencies.
  979. #if !defined(_WIN32) || defined(__CYGWIN__)
  980. // This platform supports symlinks, so cmSystemTools will translate
  981. // paths. Make sure PWD is set to the original name of the home
  982. // output directory to help cmSystemTools to create the same
  983. // translation table for the dependency scanning process.
  984. depCmd << "cd "
  985. << (this->LocalGenerator->Convert(
  986. this->Makefile->GetHomeOutputDirectory(),
  987. cmLocalGenerator::FULL, cmLocalGenerator::SHELL))
  988. << " && ";
  989. #endif
  990. // Generate a call this signature:
  991. //
  992. // cmake -E cmake_depends <generator>
  993. // <home-src-dir> <start-src-dir>
  994. // <home-out-dir> <start-out-dir>
  995. // <dep-info> --color=$(COLOR)
  996. //
  997. // This gives the dependency scanner enough information to recreate
  998. // the state of our local generator sufficiently for its needs.
  999. depCmd << "$(CMAKE_COMMAND) -E cmake_depends \""
  1000. << this->GlobalGenerator->GetName() << "\" "
  1001. << this->Convert(this->Makefile->GetHomeDirectory(),
  1002. cmLocalGenerator::FULL, cmLocalGenerator::SHELL)
  1003. << " "
  1004. << this->Convert(this->Makefile->GetStartDirectory(),
  1005. cmLocalGenerator::FULL, cmLocalGenerator::SHELL)
  1006. << " "
  1007. << this->Convert(this->Makefile->GetHomeOutputDirectory(),
  1008. cmLocalGenerator::FULL, cmLocalGenerator::SHELL)
  1009. << " "
  1010. << this->Convert(this->Makefile->GetStartOutputDirectory(),
  1011. cmLocalGenerator::FULL, cmLocalGenerator::SHELL)
  1012. << " "
  1013. << this->Convert(this->InfoFileNameFull.c_str(),
  1014. cmLocalGenerator::FULL, cmLocalGenerator::SHELL);
  1015. if(this->LocalGenerator->GetColorMakefile())
  1016. {
  1017. depCmd << " --color=$(COLOR)";
  1018. }
  1019. commands.push_back(depCmd.str());
  1020. // Make sure all custom command outputs in this target are built.
  1021. if(this->CustomCommandDriver == OnDepends)
  1022. {
  1023. this->DriveCustomCommands(depends);
  1024. }
  1025. // Write the rule.
  1026. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  1027. depTarget.c_str(),
  1028. depends, commands, true);
  1029. }
  1030. //----------------------------------------------------------------------------
  1031. void
  1032. cmMakefileTargetGenerator
  1033. ::DriveCustomCommands(std::vector<std::string>& depends)
  1034. {
  1035. // Depend on all custom command outputs.
  1036. const std::vector<cmSourceFile*>& sources =
  1037. this->Target->GetSourceFiles();
  1038. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  1039. source != sources.end(); ++source)
  1040. {
  1041. if(cmCustomCommand* cc = (*source)->GetCustomCommand())
  1042. {
  1043. const std::vector<std::string>& outputs = cc->GetOutputs();
  1044. for(std::vector<std::string>::const_iterator o = outputs.begin();
  1045. o != outputs.end(); ++o)
  1046. {
  1047. depends.push_back(*o);
  1048. }
  1049. }
  1050. }
  1051. }
  1052. //----------------------------------------------------------------------------
  1053. void cmMakefileTargetGenerator
  1054. ::WriteObjectDependRules(cmSourceFile& source,
  1055. std::vector<std::string>& depends)
  1056. {
  1057. // Create the list of dependencies known at cmake time. These are
  1058. // shared between the object file and dependency scanning rule.
  1059. depends.push_back(source.GetFullPath());
  1060. if(const char* objectDeps = source.GetProperty("OBJECT_DEPENDS"))
  1061. {
  1062. std::vector<std::string> deps;
  1063. cmSystemTools::ExpandListArgument(objectDeps, deps);
  1064. for(std::vector<std::string>::iterator i = deps.begin();
  1065. i != deps.end(); ++i)
  1066. {
  1067. depends.push_back(i->c_str());
  1068. }
  1069. }
  1070. }
  1071. //----------------------------------------------------------------------------
  1072. void cmMakefileTargetGenerator
  1073. ::GenerateCustomRuleFile(const cmCustomCommand& cc)
  1074. {
  1075. // Collect the commands.
  1076. std::vector<std::string> commands;
  1077. std::string comment = this->LocalGenerator->ConstructComment(cc);
  1078. if(!comment.empty())
  1079. {
  1080. // add in a progress call if needed
  1081. this->AppendProgress(commands);
  1082. if(!this->NoRuleMessages)
  1083. {
  1084. this->LocalGenerator
  1085. ->AppendEcho(commands, comment.c_str(),
  1086. cmLocalUnixMakefileGenerator3::EchoGenerate);
  1087. }
  1088. }
  1089. // Now append the actual user-specified commands.
  1090. cmOStringStream content;
  1091. this->LocalGenerator->AppendCustomCommand(commands, cc, this->Target, false,
  1092. cmLocalGenerator::HOME_OUTPUT,
  1093. &content);
  1094. // Collect the dependencies.
  1095. std::vector<std::string> depends;
  1096. this->LocalGenerator->AppendCustomDepend(depends, cc);
  1097. // Check whether we need to bother checking for a symbolic output.
  1098. bool need_symbolic = this->GlobalGenerator->GetNeedSymbolicMark();
  1099. // Write the rule.
  1100. const std::vector<std::string>& outputs = cc.GetOutputs();
  1101. std::vector<std::string>::const_iterator o = outputs.begin();
  1102. {
  1103. bool symbolic = false;
  1104. if(need_symbolic)
  1105. {
  1106. if(cmSourceFile* sf = this->Makefile->GetSource(o->c_str()))
  1107. {
  1108. symbolic = sf->GetPropertyAsBool("SYMBOLIC");
  1109. }
  1110. }
  1111. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  1112. o->c_str(), depends, commands,
  1113. symbolic);
  1114. // If the rule has changed make sure the output is rebuilt.
  1115. if(!symbolic)
  1116. {
  1117. this->GlobalGenerator->AddRuleHash(cc.GetOutputs(), content.str());
  1118. }
  1119. }
  1120. // Write rules to drive building any outputs beyond the first.
  1121. const char* in = o->c_str();
  1122. for(++o; o != outputs.end(); ++o)
  1123. {
  1124. bool symbolic = false;
  1125. if(need_symbolic)
  1126. {
  1127. if(cmSourceFile* sf = this->Makefile->GetSource(o->c_str()))
  1128. {
  1129. symbolic = sf->GetPropertyAsBool("SYMBOLIC");
  1130. }
  1131. }
  1132. this->GenerateExtraOutput(o->c_str(), in, symbolic);
  1133. }
  1134. // Setup implicit dependency scanning.
  1135. for(cmCustomCommand::ImplicitDependsList::const_iterator
  1136. idi = cc.GetImplicitDepends().begin();
  1137. idi != cc.GetImplicitDepends().end(); ++idi)
  1138. {
  1139. std::string objFullPath =
  1140. this->Convert(outputs[0].c_str(), cmLocalGenerator::FULL);
  1141. std::string srcFullPath =
  1142. this->Convert(idi->second.c_str(), cmLocalGenerator::FULL);
  1143. this->LocalGenerator->
  1144. AddImplicitDepends(*this->Target, idi->first.c_str(),
  1145. objFullPath.c_str(),
  1146. srcFullPath.c_str());
  1147. }
  1148. }
  1149. //----------------------------------------------------------------------------
  1150. void
  1151. cmMakefileTargetGenerator
  1152. ::GenerateExtraOutput(const char* out, const char* in, bool symbolic)
  1153. {
  1154. // Add a rule to build the primary output if the extra output needs
  1155. // to be created.
  1156. std::vector<std::string> commands;
  1157. std::vector<std::string> depends;
  1158. std::string emptyCommand = this->GlobalGenerator->GetEmptyRuleHackCommand();
  1159. if(!emptyCommand.empty())
  1160. {
  1161. commands.push_back(emptyCommand);
  1162. }
  1163. depends.push_back(in);
  1164. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  1165. out, depends, commands,
  1166. symbolic);
  1167. // Register the extra output as paired with the first output so that
  1168. // the check-build-system step will remove the primary output if any
  1169. // extra outputs are missing. This forces the rule to regenerate
  1170. // all outputs.
  1171. this->AddMultipleOutputPair(out, in);
  1172. }
  1173. //----------------------------------------------------------------------------
  1174. void
  1175. cmMakefileTargetGenerator::AppendProgress(std::vector<std::string>& commands)
  1176. {
  1177. this->NumberOfProgressActions++;
  1178. if(this->NoRuleMessages)
  1179. {
  1180. return;
  1181. }
  1182. std::string progressDir = this->Makefile->GetHomeOutputDirectory();
  1183. progressDir += cmake::GetCMakeFilesDirectory();
  1184. cmOStringStream progCmd;
  1185. progCmd << "$(CMAKE_COMMAND) -E cmake_progress_report ";
  1186. progCmd << this->LocalGenerator->Convert(progressDir.c_str(),
  1187. cmLocalGenerator::FULL,
  1188. cmLocalGenerator::SHELL);
  1189. progCmd << " $(CMAKE_PROGRESS_" << this->NumberOfProgressActions << ")";
  1190. commands.push_back(progCmd.str());
  1191. }
  1192. //----------------------------------------------------------------------------
  1193. void
  1194. cmMakefileTargetGenerator
  1195. ::WriteObjectsVariable(std::string& variableName,
  1196. std::string& variableNameExternal)
  1197. {
  1198. // Write a make variable assignment that lists all objects for the
  1199. // target.
  1200. variableName =
  1201. this->LocalGenerator->CreateMakeVariable(this->Target->GetName(),
  1202. "_OBJECTS");
  1203. *this->BuildFileStream
  1204. << "# Object files for target " << this->Target->GetName() << "\n"
  1205. << variableName.c_str() << " =";
  1206. std::string object;
  1207. const char* objName =
  1208. this->Makefile->GetDefinition("CMAKE_NO_QUOTED_OBJECTS");
  1209. const char* lineContinue =
  1210. this->Makefile->GetDefinition("CMAKE_MAKE_LINE_CONTINUE");
  1211. if(!lineContinue)
  1212. {
  1213. lineContinue = "\\";
  1214. }
  1215. for(std::vector<std::string>::const_iterator i = this->Objects.begin();
  1216. i != this->Objects.end(); ++i)
  1217. {
  1218. *this->BuildFileStream << " " << lineContinue << "\n";
  1219. if(objName)
  1220. {
  1221. *this->BuildFileStream <<
  1222. this->Convert(i->c_str(), cmLocalGenerator::START_OUTPUT,
  1223. cmLocalGenerator::MAKEFILE);
  1224. }
  1225. else
  1226. {
  1227. *this->BuildFileStream <<
  1228. this->LocalGenerator->ConvertToQuotedOutputPath(i->c_str());
  1229. }
  1230. }
  1231. *this->BuildFileStream << "\n";
  1232. // Write a make variable assignment that lists all external objects
  1233. // for the target.
  1234. variableNameExternal =
  1235. this->LocalGenerator->CreateMakeVariable(this->Target->GetName(),
  1236. "_EXTERNAL_OBJECTS");
  1237. *this->BuildFileStream
  1238. << "\n"
  1239. << "# External object files for target "
  1240. << this->Target->GetName() << "\n"
  1241. << variableNameExternal.c_str() << " =";
  1242. for(std::vector<std::string>::const_iterator i =
  1243. this->ExternalObjects.begin();
  1244. i != this->ExternalObjects.end(); ++i)
  1245. {
  1246. object = this->Convert(i->c_str(),cmLocalGenerator::START_OUTPUT);
  1247. *this->BuildFileStream
  1248. << " " << lineContinue << "\n"
  1249. << this->Makefile->GetSafeDefinition("CMAKE_OBJECT_NAME");
  1250. if(objName)
  1251. {
  1252. *this->BuildFileStream <<
  1253. this->Convert(i->c_str(), cmLocalGenerator::START_OUTPUT,
  1254. cmLocalGenerator::MAKEFILE);
  1255. }
  1256. else
  1257. {
  1258. *this->BuildFileStream <<
  1259. this->LocalGenerator->ConvertToQuotedOutputPath(i->c_str());
  1260. }
  1261. }
  1262. *this->BuildFileStream << "\n" << "\n";
  1263. }
  1264. //----------------------------------------------------------------------------
  1265. void
  1266. cmMakefileTargetGenerator
  1267. ::WriteObjectsString(std::string& buildObjs)
  1268. {
  1269. std::vector<std::string> objStrings;
  1270. this->WriteObjectsStrings(objStrings);
  1271. buildObjs = objStrings[0];
  1272. }
  1273. //----------------------------------------------------------------------------
  1274. class cmMakefileTargetGeneratorObjectStrings
  1275. {
  1276. public:
  1277. cmMakefileTargetGeneratorObjectStrings(std::vector<std::string>& strings,
  1278. cmLocalUnixMakefileGenerator3* lg,
  1279. std::string::size_type limit):
  1280. Strings(strings), LocalGenerator(lg), LengthLimit(limit)
  1281. {
  1282. this->Space = "";
  1283. }
  1284. void Feed(std::string const& obj)
  1285. {
  1286. // Construct the name of the next object.
  1287. this->NextObject =
  1288. this->LocalGenerator->Convert(obj.c_str(),
  1289. cmLocalGenerator::START_OUTPUT,
  1290. cmLocalGenerator::RESPONSE);
  1291. // Roll over to next string if the limit will be exceeded.
  1292. if(this->LengthLimit != std::string::npos &&
  1293. (this->CurrentString.length() + 1 + this->NextObject.length()
  1294. > this->LengthLimit))
  1295. {
  1296. this->Strings.push_back(this->CurrentString);
  1297. this->CurrentString = "";
  1298. this->Space = "";
  1299. }
  1300. // Separate from previous object.
  1301. this->CurrentString += this->Space;
  1302. this->Space = " ";
  1303. // Append this object.
  1304. this->CurrentString += this->NextObject;
  1305. }
  1306. void Done()
  1307. {
  1308. this->Strings.push_back(this->CurrentString);
  1309. }
  1310. private:
  1311. std::vector<std::string>& Strings;
  1312. cmLocalUnixMakefileGenerator3* LocalGenerator;
  1313. std::string::size_type LengthLimit;
  1314. std::string CurrentString;
  1315. std::string NextObject;
  1316. const char* Space;
  1317. };
  1318. //----------------------------------------------------------------------------
  1319. void
  1320. cmMakefileTargetGenerator
  1321. ::WriteObjectsStrings(std::vector<std::string>& objStrings,
  1322. std::string::size_type limit)
  1323. {
  1324. cmMakefileTargetGeneratorObjectStrings
  1325. helper(objStrings, this->LocalGenerator, limit);
  1326. for(std::vector<std::string>::const_iterator i = this->Objects.begin();
  1327. i != this->Objects.end(); ++i)
  1328. {
  1329. helper.Feed(*i);
  1330. }
  1331. for(std::vector<std::string>::const_iterator i =
  1332. this->ExternalObjects.begin();
  1333. i != this->ExternalObjects.end(); ++i)
  1334. {
  1335. helper.Feed(*i);
  1336. }
  1337. helper.Done();
  1338. }
  1339. //----------------------------------------------------------------------------
  1340. void cmMakefileTargetGenerator::WriteTargetDriverRule(const char* main_output,
  1341. bool relink)
  1342. {
  1343. // Compute the name of the driver target.
  1344. std::string dir =
  1345. this->LocalGenerator->GetRelativeTargetDirectory(*this->Target);
  1346. std::string buildTargetRuleName = dir;
  1347. buildTargetRuleName += relink?"/preinstall":"/build";
  1348. buildTargetRuleName = this->Convert(buildTargetRuleName.c_str(),
  1349. cmLocalGenerator::HOME_OUTPUT,
  1350. cmLocalGenerator::UNCHANGED);
  1351. // Build the list of target outputs to drive.
  1352. std::vector<std::string> depends;
  1353. if(main_output)
  1354. {
  1355. depends.push_back(main_output);
  1356. }
  1357. const char* comment = 0;
  1358. if(relink)
  1359. {
  1360. // Setup the comment for the preinstall driver.
  1361. comment = "Rule to relink during preinstall.";
  1362. }
  1363. else
  1364. {
  1365. // Setup the comment for the main build driver.
  1366. comment = "Rule to build all files generated by this target.";
  1367. // Make sure all custom command outputs in this target are built.
  1368. if(this->CustomCommandDriver == OnBuild)
  1369. {
  1370. this->DriveCustomCommands(depends);
  1371. }
  1372. // Make sure the extra files are built.
  1373. for(std::set<cmStdString>::const_iterator i = this->ExtraFiles.begin();
  1374. i != this->ExtraFiles.end(); ++i)
  1375. {
  1376. depends.push_back(*i);
  1377. }
  1378. }
  1379. // Write the driver rule.
  1380. std::vector<std::string> no_commands;
  1381. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, comment,
  1382. buildTargetRuleName.c_str(),
  1383. depends, no_commands, true);
  1384. }
  1385. //----------------------------------------------------------------------------
  1386. std::string cmMakefileTargetGenerator::GetFrameworkFlags()
  1387. {
  1388. if(!this->Makefile->IsOn("APPLE"))
  1389. {
  1390. return std::string();
  1391. }
  1392. std::set<cmStdString> emitted;
  1393. #ifdef __APPLE__ /* don't insert this when crosscompiling e.g. to iphone */
  1394. emitted.insert("/System/Library/Frameworks");
  1395. #endif
  1396. std::vector<std::string> includes;
  1397. const char *config = this->Makefile->GetDefinition("CMAKE_BUILD_TYPE");
  1398. this->LocalGenerator->GetIncludeDirectories(includes,
  1399. this->GeneratorTarget,
  1400. "C", config);
  1401. // check all include directories for frameworks as this
  1402. // will already have added a -F for the framework
  1403. for(std::vector<std::string>::iterator i = includes.begin();
  1404. i != includes.end(); ++i)
  1405. {
  1406. if(this->Target->NameResolvesToFramework(i->c_str()))
  1407. {
  1408. std::string frameworkDir = *i;
  1409. frameworkDir += "/../";
  1410. frameworkDir = cmSystemTools::CollapseFullPath(frameworkDir.c_str());
  1411. emitted.insert(frameworkDir);
  1412. }
  1413. }
  1414. std::string flags;
  1415. const char* cfg = this->LocalGenerator->ConfigurationName.c_str();
  1416. if(cmComputeLinkInformation* cli = this->Target->GetLinkInformation(cfg))
  1417. {
  1418. std::vector<std::string> const& frameworks = cli->GetFrameworkPaths();
  1419. for(std::vector<std::string>::const_iterator i = frameworks.begin();
  1420. i != frameworks.end(); ++i)
  1421. {
  1422. if(emitted.insert(*i).second)
  1423. {
  1424. flags += "-F";
  1425. flags += this->Convert(i->c_str(),
  1426. cmLocalGenerator::START_OUTPUT,
  1427. cmLocalGenerator::SHELL, true);
  1428. flags += " ";
  1429. }
  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. const char *config = this->Makefile->GetDefinition("CMAKE_BUILD_TYPE");
  1679. this->LocalGenerator->GetIncludeDirectories(includes,
  1680. this->GeneratorTarget,
  1681. lang, config);
  1682. std::string includeFlags =
  1683. this->LocalGenerator->GetIncludeFlags(includes, lang, useResponseFile);
  1684. if(includeFlags.empty())
  1685. {
  1686. return;
  1687. }
  1688. if(useResponseFile)
  1689. {
  1690. std::string name = "includes_";
  1691. name += lang;
  1692. name += ".rsp";
  1693. std::string arg = "@" +
  1694. this->CreateResponseFile(name.c_str(), includeFlags,
  1695. this->FlagFileDepends[lang]);
  1696. this->LocalGenerator->AppendFlags(flags, arg.c_str());
  1697. }
  1698. else
  1699. {
  1700. this->LocalGenerator->AppendFlags(flags, includeFlags.c_str());
  1701. }
  1702. }
  1703. //----------------------------------------------------------------------------
  1704. const char* cmMakefileTargetGenerator::GetFortranModuleDirectory()
  1705. {
  1706. // Compute the module directory.
  1707. if(!this->FortranModuleDirectoryComputed)
  1708. {
  1709. const char* target_mod_dir =
  1710. this->Target->GetProperty("Fortran_MODULE_DIRECTORY");
  1711. const char* moddir_flag =
  1712. this->Makefile->GetDefinition("CMAKE_Fortran_MODDIR_FLAG");
  1713. if(target_mod_dir && moddir_flag)
  1714. {
  1715. // Compute the full path to the module directory.
  1716. if(cmSystemTools::FileIsFullPath(target_mod_dir))
  1717. {
  1718. // Already a full path.
  1719. this->FortranModuleDirectory = target_mod_dir;
  1720. }
  1721. else
  1722. {
  1723. // Interpret relative to the current output directory.
  1724. this->FortranModuleDirectory =
  1725. this->Makefile->GetCurrentOutputDirectory();
  1726. this->FortranModuleDirectory += "/";
  1727. this->FortranModuleDirectory += target_mod_dir;
  1728. }
  1729. // Make sure the module output directory exists.
  1730. cmSystemTools::MakeDirectory(this->FortranModuleDirectory.c_str());
  1731. }
  1732. this->FortranModuleDirectoryComputed = true;
  1733. }
  1734. // Return the computed directory.
  1735. if(this->FortranModuleDirectory.empty())
  1736. {
  1737. return 0;
  1738. }
  1739. else
  1740. {
  1741. return this->FortranModuleDirectory.c_str();
  1742. }
  1743. }
  1744. //----------------------------------------------------------------------------
  1745. void cmMakefileTargetGenerator::AddFortranFlags(std::string& flags)
  1746. {
  1747. // Enable module output if necessary.
  1748. if(const char* modout_flag =
  1749. this->Makefile->GetDefinition("CMAKE_Fortran_MODOUT_FLAG"))
  1750. {
  1751. this->LocalGenerator->AppendFlags(flags, modout_flag);
  1752. }
  1753. // Add a module output directory flag if necessary.
  1754. const char* mod_dir = this->GetFortranModuleDirectory();
  1755. if(!mod_dir)
  1756. {
  1757. mod_dir = this->Makefile->GetDefinition("CMAKE_Fortran_MODDIR_DEFAULT");
  1758. }
  1759. if(mod_dir)
  1760. {
  1761. const char* moddir_flag =
  1762. this->Makefile->GetRequiredDefinition("CMAKE_Fortran_MODDIR_FLAG");
  1763. std::string modflag = moddir_flag;
  1764. modflag += this->Convert(mod_dir,
  1765. cmLocalGenerator::START_OUTPUT,
  1766. cmLocalGenerator::SHELL);
  1767. this->LocalGenerator->AppendFlags(flags, modflag.c_str());
  1768. }
  1769. // If there is a separate module path flag then duplicate the
  1770. // include path with it. This compiler does not search the include
  1771. // path for modules.
  1772. if(const char* modpath_flag =
  1773. this->Makefile->GetDefinition("CMAKE_Fortran_MODPATH_FLAG"))
  1774. {
  1775. std::vector<std::string> includes;
  1776. const char *config = this->Makefile->GetDefinition("CMAKE_BUILD_TYPE");
  1777. this->LocalGenerator->GetIncludeDirectories(includes,
  1778. this->GeneratorTarget,
  1779. "C", config);
  1780. for(std::vector<std::string>::const_iterator idi = includes.begin();
  1781. idi != includes.end(); ++idi)
  1782. {
  1783. std::string flg = modpath_flag;
  1784. flg += this->Convert(idi->c_str(),
  1785. cmLocalGenerator::NONE,
  1786. cmLocalGenerator::SHELL);
  1787. this->LocalGenerator->AppendFlags(flags, flg.c_str());
  1788. }
  1789. }
  1790. }
  1791. //----------------------------------------------------------------------------
  1792. void cmMakefileTargetGenerator::AddModuleDefinitionFlag(std::string& flags)
  1793. {
  1794. if(this->GeneratorTarget->ModuleDefinitionFile.empty())
  1795. {
  1796. return;
  1797. }
  1798. // TODO: Create a per-language flag variable.
  1799. const char* defFileFlag =
  1800. this->Makefile->GetDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  1801. if(!defFileFlag)
  1802. {
  1803. return;
  1804. }
  1805. // Append the flag and value. Use ConvertToLinkReference to help
  1806. // vs6's "cl -link" pass it to the linker.
  1807. std::string flag = defFileFlag;
  1808. flag += (this->LocalGenerator->ConvertToLinkReference(
  1809. this->GeneratorTarget->ModuleDefinitionFile.c_str()));
  1810. this->LocalGenerator->AppendFlags(flags, flag.c_str());
  1811. }
  1812. //----------------------------------------------------------------------------
  1813. const char* cmMakefileTargetGenerator::GetFeature(const char* feature)
  1814. {
  1815. return this->Target->GetFeature(feature, this->ConfigName);
  1816. }
  1817. //----------------------------------------------------------------------------
  1818. bool cmMakefileTargetGenerator::GetFeatureAsBool(const char* feature)
  1819. {
  1820. return cmSystemTools::IsOn(this->GetFeature(feature));
  1821. }
  1822. //----------------------------------------------------------------------------
  1823. void cmMakefileTargetGenerator::AddFeatureFlags(
  1824. std::string& flags, const char* lang
  1825. )
  1826. {
  1827. // Add language-specific flags.
  1828. this->LocalGenerator->AddLanguageFlags(flags, lang, this->ConfigName);
  1829. if(this->GetFeatureAsBool("INTERPROCEDURAL_OPTIMIZATION"))
  1830. {
  1831. this->LocalGenerator->AppendFeatureOptions(flags, lang, "IPO");
  1832. }
  1833. }