cmMakefileTargetGenerator.cxx 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmMakefileTargetGenerator.h"
  4. #include "cmAlgorithms.h"
  5. #include "cmComputeLinkInformation.h"
  6. #include "cmCustomCommand.h"
  7. #include "cmCustomCommandGenerator.h"
  8. #include "cmGeneratedFileStream.h"
  9. #include "cmGeneratorExpression.h"
  10. #include "cmGeneratorTarget.h"
  11. #include "cmGlobalUnixMakefileGenerator3.h"
  12. #include "cmLinkLineComputer.h"
  13. #include "cmLocalGenerator.h"
  14. #include "cmLocalUnixMakefileGenerator3.h"
  15. #include "cmMakefile.h"
  16. #include "cmMakefileExecutableTargetGenerator.h"
  17. #include "cmMakefileLibraryTargetGenerator.h"
  18. #include "cmMakefileUtilityTargetGenerator.h"
  19. #include "cmOutputConverter.h"
  20. #include "cmRulePlaceholderExpander.h"
  21. #include "cmSourceFile.h"
  22. #include "cmState.h"
  23. #include "cmStateTypes.h"
  24. #include "cmSystemTools.h"
  25. #include "cm_auto_ptr.hxx"
  26. #include "cmake.h"
  27. #include <algorithm>
  28. #include <ctype.h>
  29. #include <sstream>
  30. #include <stdio.h>
  31. #include <utility>
  32. #ifndef _WIN32
  33. #include <unistd.h>
  34. #endif
  35. cmMakefileTargetGenerator::cmMakefileTargetGenerator(cmGeneratorTarget* target)
  36. : cmCommonTargetGenerator(target)
  37. , OSXBundleGenerator(CM_NULLPTR)
  38. , MacOSXContentGenerator(CM_NULLPTR)
  39. {
  40. this->BuildFileStream = CM_NULLPTR;
  41. this->InfoFileStream = CM_NULLPTR;
  42. this->FlagFileStream = CM_NULLPTR;
  43. this->CustomCommandDriver = OnBuild;
  44. this->LocalGenerator =
  45. static_cast<cmLocalUnixMakefileGenerator3*>(target->GetLocalGenerator());
  46. this->GlobalGenerator = static_cast<cmGlobalUnixMakefileGenerator3*>(
  47. this->LocalGenerator->GetGlobalGenerator());
  48. cmake* cm = this->GlobalGenerator->GetCMakeInstance();
  49. this->NoRuleMessages = false;
  50. if (const char* ruleStatus =
  51. cm->GetState()->GetGlobalProperty("RULE_MESSAGES")) {
  52. this->NoRuleMessages = cmSystemTools::IsOff(ruleStatus);
  53. }
  54. MacOSXContentGenerator = new MacOSXContentGeneratorType(this);
  55. }
  56. cmMakefileTargetGenerator::~cmMakefileTargetGenerator()
  57. {
  58. delete MacOSXContentGenerator;
  59. }
  60. cmMakefileTargetGenerator* cmMakefileTargetGenerator::New(
  61. cmGeneratorTarget* tgt)
  62. {
  63. cmMakefileTargetGenerator* result = CM_NULLPTR;
  64. switch (tgt->GetType()) {
  65. case cmStateEnums::EXECUTABLE:
  66. result = new cmMakefileExecutableTargetGenerator(tgt);
  67. break;
  68. case cmStateEnums::STATIC_LIBRARY:
  69. case cmStateEnums::SHARED_LIBRARY:
  70. case cmStateEnums::MODULE_LIBRARY:
  71. case cmStateEnums::OBJECT_LIBRARY:
  72. result = new cmMakefileLibraryTargetGenerator(tgt);
  73. break;
  74. case cmStateEnums::UTILITY:
  75. result = new cmMakefileUtilityTargetGenerator(tgt);
  76. break;
  77. default:
  78. return result;
  79. // break; /* unreachable */
  80. }
  81. return result;
  82. }
  83. void cmMakefileTargetGenerator::CreateRuleFile()
  84. {
  85. // Create a directory for this target.
  86. this->TargetBuildDirectory =
  87. this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  88. this->TargetBuildDirectoryFull =
  89. this->LocalGenerator->ConvertToFullPath(this->TargetBuildDirectory);
  90. cmSystemTools::MakeDirectory(this->TargetBuildDirectoryFull.c_str());
  91. // Construct the rule file name.
  92. this->BuildFileName = this->TargetBuildDirectory;
  93. this->BuildFileName += "/build.make";
  94. this->BuildFileNameFull = this->TargetBuildDirectoryFull;
  95. this->BuildFileNameFull += "/build.make";
  96. // Construct the rule file name.
  97. this->ProgressFileNameFull = this->TargetBuildDirectoryFull;
  98. this->ProgressFileNameFull += "/progress.make";
  99. // reset the progress count
  100. this->NumberOfProgressActions = 0;
  101. // Open the rule file. This should be copy-if-different because the
  102. // rules may depend on this file itself.
  103. this->BuildFileStream =
  104. new cmGeneratedFileStream(this->BuildFileNameFull.c_str(), false,
  105. this->GlobalGenerator->GetMakefileEncoding());
  106. this->BuildFileStream->SetCopyIfDifferent(true);
  107. if (!this->BuildFileStream) {
  108. return;
  109. }
  110. this->LocalGenerator->WriteDisclaimer(*this->BuildFileStream);
  111. if (this->GlobalGenerator->AllowDeleteOnError()) {
  112. std::vector<std::string> no_depends;
  113. std::vector<std::string> no_commands;
  114. this->LocalGenerator->WriteMakeRule(
  115. *this->BuildFileStream, "Delete rule output on recipe failure.",
  116. ".DELETE_ON_ERROR", no_depends, no_commands, false);
  117. }
  118. this->LocalGenerator->WriteSpecialTargetsTop(*this->BuildFileStream);
  119. }
  120. void cmMakefileTargetGenerator::WriteTargetBuildRules()
  121. {
  122. const std::string& config =
  123. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  124. // write the custom commands for this target
  125. // Look for files registered for cleaning in this directory.
  126. if (const char* additional_clean_files =
  127. this->Makefile->GetProperty("ADDITIONAL_MAKE_CLEAN_FILES")) {
  128. cmGeneratorExpression ge;
  129. CM_AUTO_PTR<cmCompiledGeneratorExpression> cge =
  130. ge.Parse(additional_clean_files);
  131. cmSystemTools::ExpandListArgument(
  132. cge->Evaluate(this->LocalGenerator, config, false, this->GeneratorTarget,
  133. CM_NULLPTR, CM_NULLPTR),
  134. this->CleanFiles);
  135. }
  136. // add custom commands to the clean rules?
  137. const char* clean_no_custom = this->Makefile->GetProperty("CLEAN_NO_CUSTOM");
  138. bool clean = cmSystemTools::IsOff(clean_no_custom);
  139. // First generate the object rule files. Save a list of all object
  140. // files for this target.
  141. std::vector<cmSourceFile const*> customCommands;
  142. this->GeneratorTarget->GetCustomCommands(customCommands, config);
  143. std::string currentBinDir =
  144. this->LocalGenerator->GetCurrentBinaryDirectory();
  145. for (std::vector<cmSourceFile const*>::const_iterator si =
  146. customCommands.begin();
  147. si != customCommands.end(); ++si) {
  148. cmCustomCommandGenerator ccg(*(*si)->GetCustomCommand(), this->ConfigName,
  149. this->LocalGenerator);
  150. this->GenerateCustomRuleFile(ccg);
  151. if (clean) {
  152. const std::vector<std::string>& outputs = ccg.GetOutputs();
  153. for (std::vector<std::string>::const_iterator o = outputs.begin();
  154. o != outputs.end(); ++o) {
  155. this->CleanFiles.push_back(
  156. this->LocalGenerator->MaybeConvertToRelativePath(currentBinDir, *o));
  157. }
  158. }
  159. }
  160. std::vector<cmSourceFile const*> headerSources;
  161. this->GeneratorTarget->GetHeaderSources(headerSources, config);
  162. this->OSXBundleGenerator->GenerateMacOSXContentStatements(
  163. headerSources, this->MacOSXContentGenerator);
  164. std::vector<cmSourceFile const*> extraSources;
  165. this->GeneratorTarget->GetExtraSources(extraSources, config);
  166. this->OSXBundleGenerator->GenerateMacOSXContentStatements(
  167. extraSources, this->MacOSXContentGenerator);
  168. std::vector<cmSourceFile const*> externalObjects;
  169. this->GeneratorTarget->GetExternalObjects(externalObjects, config);
  170. for (std::vector<cmSourceFile const*>::const_iterator si =
  171. externalObjects.begin();
  172. si != externalObjects.end(); ++si) {
  173. this->ExternalObjects.push_back((*si)->GetFullPath());
  174. }
  175. std::vector<cmSourceFile const*> objectSources;
  176. this->GeneratorTarget->GetObjectSources(objectSources, config);
  177. for (std::vector<cmSourceFile const*>::const_iterator si =
  178. objectSources.begin();
  179. si != objectSources.end(); ++si) {
  180. // Generate this object file's rule file.
  181. this->WriteObjectRuleFiles(**si);
  182. }
  183. }
  184. void cmMakefileTargetGenerator::WriteCommonCodeRules()
  185. {
  186. const char* root = (this->Makefile->IsOn("CMAKE_MAKE_INCLUDE_FROM_ROOT")
  187. ? "$(CMAKE_BINARY_DIR)/"
  188. : "");
  189. // Include the dependencies for the target.
  190. std::string dependFileNameFull = this->TargetBuildDirectoryFull;
  191. dependFileNameFull += "/depend.make";
  192. *this->BuildFileStream
  193. << "# Include any dependencies generated for this target.\n"
  194. << this->GlobalGenerator->IncludeDirective << " " << root
  195. << cmSystemTools::ConvertToOutputPath(
  196. this->LocalGenerator
  197. ->MaybeConvertToRelativePath(
  198. this->LocalGenerator->GetBinaryDirectory(), dependFileNameFull)
  199. .c_str())
  200. << "\n\n";
  201. if (!this->NoRuleMessages) {
  202. // Include the progress variables for the target.
  203. *this->BuildFileStream
  204. << "# Include the progress variables for this target.\n"
  205. << this->GlobalGenerator->IncludeDirective << " " << root
  206. << cmSystemTools::ConvertToOutputPath(
  207. this->LocalGenerator
  208. ->MaybeConvertToRelativePath(
  209. this->LocalGenerator->GetBinaryDirectory(),
  210. this->ProgressFileNameFull)
  211. .c_str())
  212. << "\n\n";
  213. }
  214. // make sure the depend file exists
  215. if (!cmSystemTools::FileExists(dependFileNameFull.c_str())) {
  216. // Write an empty dependency file.
  217. cmGeneratedFileStream depFileStream(
  218. dependFileNameFull.c_str(), false,
  219. this->GlobalGenerator->GetMakefileEncoding());
  220. depFileStream << "# Empty dependencies file for "
  221. << this->GeneratorTarget->GetName() << ".\n"
  222. << "# This may be replaced when dependencies are built."
  223. << std::endl;
  224. }
  225. // Open the flags file. This should be copy-if-different because the
  226. // rules may depend on this file itself.
  227. this->FlagFileNameFull = this->TargetBuildDirectoryFull;
  228. this->FlagFileNameFull += "/flags.make";
  229. this->FlagFileStream =
  230. new cmGeneratedFileStream(this->FlagFileNameFull.c_str(), false,
  231. this->GlobalGenerator->GetMakefileEncoding());
  232. this->FlagFileStream->SetCopyIfDifferent(true);
  233. if (!this->FlagFileStream) {
  234. return;
  235. }
  236. this->LocalGenerator->WriteDisclaimer(*this->FlagFileStream);
  237. // Include the flags for the target.
  238. *this->BuildFileStream
  239. << "# Include the compile flags for this target's objects.\n"
  240. << this->GlobalGenerator->IncludeDirective << " " << root
  241. << cmSystemTools::ConvertToOutputPath(
  242. this->LocalGenerator
  243. ->MaybeConvertToRelativePath(
  244. this->LocalGenerator->GetBinaryDirectory(),
  245. this->FlagFileNameFull)
  246. .c_str())
  247. << "\n\n";
  248. }
  249. void cmMakefileTargetGenerator::WriteTargetLanguageFlags()
  250. {
  251. // write language flags for target
  252. std::set<std::string> languages;
  253. this->GeneratorTarget->GetLanguages(
  254. languages, this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE"));
  255. // put the compiler in the rules.make file so that if it changes
  256. // things rebuild
  257. for (std::set<std::string>::const_iterator l = languages.begin();
  258. l != languages.end(); ++l) {
  259. std::string compiler = "CMAKE_";
  260. compiler += *l;
  261. compiler += "_COMPILER";
  262. *this->FlagFileStream << "# compile " << *l << " with "
  263. << this->Makefile->GetSafeDefinition(compiler)
  264. << "\n";
  265. }
  266. for (std::set<std::string>::const_iterator l = languages.begin();
  267. l != languages.end(); ++l) {
  268. std::string flags = this->GetFlags(*l);
  269. std::string defines = this->GetDefines(*l);
  270. std::string includes = this->GetIncludes(*l);
  271. // Escape comment characters so they do not terminate assignment.
  272. cmSystemTools::ReplaceString(flags, "#", "\\#");
  273. cmSystemTools::ReplaceString(defines, "#", "\\#");
  274. cmSystemTools::ReplaceString(includes, "#", "\\#");
  275. *this->FlagFileStream << *l << "_FLAGS = " << flags << "\n\n";
  276. *this->FlagFileStream << *l << "_DEFINES = " << defines << "\n\n";
  277. *this->FlagFileStream << *l << "_INCLUDES = " << includes << "\n\n";
  278. }
  279. }
  280. void cmMakefileTargetGenerator::MacOSXContentGeneratorType::operator()(
  281. cmSourceFile const& source, const char* pkgloc)
  282. {
  283. // Skip OS X content when not building a Framework or Bundle.
  284. if (!this->Generator->GetGeneratorTarget()->IsBundleOnApple()) {
  285. return;
  286. }
  287. std::string macdir =
  288. this->Generator->OSXBundleGenerator->InitMacOSXContentDirectory(pkgloc);
  289. // Get the input file location.
  290. std::string const& input = source.GetFullPath();
  291. // Get the output file location.
  292. std::string output = macdir;
  293. output += "/";
  294. output += cmSystemTools::GetFilenameName(input);
  295. this->Generator->CleanFiles.push_back(
  296. this->Generator->LocalGenerator->MaybeConvertToRelativePath(
  297. this->Generator->LocalGenerator->GetCurrentBinaryDirectory(), output));
  298. output = this->Generator->LocalGenerator->MaybeConvertToRelativePath(
  299. this->Generator->LocalGenerator->GetBinaryDirectory(), output);
  300. // Create a rule to copy the content into the bundle.
  301. std::vector<std::string> depends;
  302. std::vector<std::string> commands;
  303. depends.push_back(input);
  304. std::string copyEcho = "Copying OS X content ";
  305. copyEcho += output;
  306. this->Generator->LocalGenerator->AppendEcho(
  307. commands, copyEcho, cmLocalUnixMakefileGenerator3::EchoBuild);
  308. std::string copyCommand = "$(CMAKE_COMMAND) -E copy ";
  309. copyCommand += this->Generator->LocalGenerator->ConvertToOutputFormat(
  310. input, cmOutputConverter::SHELL);
  311. copyCommand += " ";
  312. copyCommand += this->Generator->LocalGenerator->ConvertToOutputFormat(
  313. output, cmOutputConverter::SHELL);
  314. commands.push_back(copyCommand);
  315. this->Generator->LocalGenerator->WriteMakeRule(
  316. *this->Generator->BuildFileStream, CM_NULLPTR, output, depends, commands,
  317. false);
  318. this->Generator->ExtraFiles.insert(output);
  319. }
  320. void cmMakefileTargetGenerator::WriteObjectRuleFiles(
  321. cmSourceFile const& source)
  322. {
  323. // Identify the language of the source file.
  324. const std::string& lang =
  325. this->LocalGenerator->GetSourceFileLanguage(source);
  326. if (lang.empty()) {
  327. // don't know anything about this file so skip it
  328. return;
  329. }
  330. // Get the full path name of the object file.
  331. std::string const& objectName =
  332. this->GeneratorTarget->GetObjectName(&source);
  333. std::string obj =
  334. this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  335. obj += "/";
  336. obj += objectName;
  337. // Avoid generating duplicate rules.
  338. if (this->ObjectFiles.find(obj) == this->ObjectFiles.end()) {
  339. this->ObjectFiles.insert(obj);
  340. } else {
  341. std::ostringstream err;
  342. err << "Warning: Source file \"" << source.GetFullPath()
  343. << "\" is listed multiple times for target \""
  344. << this->GeneratorTarget->GetName() << "\".";
  345. cmSystemTools::Message(err.str().c_str(), "Warning");
  346. return;
  347. }
  348. // Create the directory containing the object file. This may be a
  349. // subdirectory under the target's directory.
  350. std::string dir = cmSystemTools::GetFilenamePath(obj);
  351. cmSystemTools::MakeDirectory(
  352. this->LocalGenerator->ConvertToFullPath(dir).c_str());
  353. // Save this in the target's list of object files.
  354. this->Objects.push_back(obj);
  355. this->CleanFiles.push_back(obj);
  356. // TODO: Remove
  357. // std::string relativeObj
  358. //= this->LocalGenerator->GetHomeRelativeOutputPath();
  359. // relativeObj += obj;
  360. // we compute some depends when writing the depend.make that we will also
  361. // use in the build.make, same with depMakeFile
  362. std::vector<std::string> depends;
  363. // generate the build rule file
  364. this->WriteObjectBuildFile(obj, lang, source, depends);
  365. // The object file should be checked for dependency integrity.
  366. std::string objFullPath = this->LocalGenerator->GetCurrentBinaryDirectory();
  367. objFullPath += "/";
  368. objFullPath += obj;
  369. objFullPath = cmSystemTools::CollapseFullPath(objFullPath);
  370. std::string srcFullPath =
  371. cmSystemTools::CollapseFullPath(source.GetFullPath());
  372. this->LocalGenerator->AddImplicitDepends(
  373. this->GeneratorTarget, lang, objFullPath.c_str(), srcFullPath.c_str());
  374. }
  375. void cmMakefileTargetGenerator::WriteObjectBuildFile(
  376. std::string& obj, const std::string& lang, cmSourceFile const& source,
  377. std::vector<std::string>& depends)
  378. {
  379. this->LocalGenerator->AppendRuleDepend(depends,
  380. this->FlagFileNameFull.c_str());
  381. this->LocalGenerator->AppendRuleDepends(depends,
  382. this->FlagFileDepends[lang]);
  383. // generate the depend scanning rule
  384. this->WriteObjectDependRules(source, depends);
  385. std::string relativeObj = this->LocalGenerator->GetHomeRelativeOutputPath();
  386. relativeObj += obj;
  387. // Write the build rule.
  388. // Build the set of compiler flags.
  389. std::string flags;
  390. // Add language-specific flags.
  391. std::string langFlags = "$(";
  392. langFlags += lang;
  393. langFlags += "_FLAGS)";
  394. this->LocalGenerator->AppendFlags(flags, langFlags);
  395. std::string config = this->LocalGenerator->GetConfigName();
  396. std::string configUpper = cmSystemTools::UpperCase(config);
  397. // Add Fortran format flags.
  398. if (lang == "Fortran") {
  399. this->AppendFortranFormatFlags(flags, source);
  400. }
  401. // Add flags from source file properties.
  402. if (const char* cflags = source.GetProperty("COMPILE_FLAGS")) {
  403. cmGeneratorExpression ge;
  404. CM_AUTO_PTR<cmCompiledGeneratorExpression> cge = ge.Parse(cflags);
  405. const char* evaluatedFlags = cge->Evaluate(this->LocalGenerator, config,
  406. false, this->GeneratorTarget);
  407. this->LocalGenerator->AppendFlags(flags, evaluatedFlags);
  408. *this->FlagFileStream << "# Custom flags: " << relativeObj
  409. << "_FLAGS = " << evaluatedFlags << "\n"
  410. << "\n";
  411. }
  412. // Add language-specific defines.
  413. std::set<std::string> defines;
  414. // Add source-sepcific preprocessor definitions.
  415. if (const char* compile_defs = source.GetProperty("COMPILE_DEFINITIONS")) {
  416. this->LocalGenerator->AppendDefines(defines, compile_defs);
  417. *this->FlagFileStream << "# Custom defines: " << relativeObj
  418. << "_DEFINES = " << compile_defs << "\n"
  419. << "\n";
  420. }
  421. std::string defPropName = "COMPILE_DEFINITIONS_";
  422. defPropName += configUpper;
  423. if (const char* config_compile_defs = source.GetProperty(defPropName)) {
  424. this->LocalGenerator->AppendDefines(defines, config_compile_defs);
  425. *this->FlagFileStream << "# Custom defines: " << relativeObj << "_DEFINES_"
  426. << configUpper << " = " << config_compile_defs
  427. << "\n"
  428. << "\n";
  429. }
  430. // Get the output paths for source and object files.
  431. std::string sourceFile = this->LocalGenerator->ConvertToOutputFormat(
  432. source.GetFullPath(), cmOutputConverter::SHELL);
  433. // Construct the build message.
  434. std::vector<std::string> no_commands;
  435. std::vector<std::string> commands;
  436. // add in a progress call if needed
  437. this->NumberOfProgressActions++;
  438. if (!this->NoRuleMessages) {
  439. cmLocalUnixMakefileGenerator3::EchoProgress progress;
  440. this->MakeEchoProgress(progress);
  441. std::string buildEcho = "Building ";
  442. buildEcho += lang;
  443. buildEcho += " object ";
  444. buildEcho += relativeObj;
  445. this->LocalGenerator->AppendEcho(commands, buildEcho,
  446. cmLocalUnixMakefileGenerator3::EchoBuild,
  447. &progress);
  448. }
  449. std::string targetOutPathReal;
  450. std::string targetOutPathPDB;
  451. std::string targetOutPathCompilePDB;
  452. {
  453. std::string targetFullPathReal;
  454. std::string targetFullPathPDB;
  455. std::string targetFullPathCompilePDB;
  456. if (this->GeneratorTarget->GetType() == cmStateEnums::EXECUTABLE ||
  457. this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY ||
  458. this->GeneratorTarget->GetType() == cmStateEnums::SHARED_LIBRARY ||
  459. this->GeneratorTarget->GetType() == cmStateEnums::MODULE_LIBRARY) {
  460. targetFullPathReal =
  461. this->GeneratorTarget->GetFullPath(this->ConfigName, false, true);
  462. targetFullPathPDB =
  463. this->GeneratorTarget->GetPDBDirectory(this->ConfigName);
  464. targetFullPathPDB += "/";
  465. targetFullPathPDB += this->GeneratorTarget->GetPDBName(this->ConfigName);
  466. }
  467. if (this->GeneratorTarget->GetType() <= cmStateEnums::OBJECT_LIBRARY) {
  468. targetFullPathCompilePDB =
  469. this->GeneratorTarget->GetCompilePDBPath(this->ConfigName);
  470. if (targetFullPathCompilePDB.empty()) {
  471. targetFullPathCompilePDB =
  472. this->GeneratorTarget->GetSupportDirectory() + "/";
  473. }
  474. }
  475. targetOutPathReal = this->LocalGenerator->ConvertToOutputFormat(
  476. this->LocalGenerator->MaybeConvertToRelativePath(
  477. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPathReal),
  478. cmOutputConverter::SHELL);
  479. targetOutPathPDB = this->LocalGenerator->ConvertToOutputFormat(
  480. targetFullPathPDB, cmOutputConverter::SHELL);
  481. targetOutPathCompilePDB = this->LocalGenerator->ConvertToOutputFormat(
  482. this->LocalGenerator->MaybeConvertToRelativePath(
  483. this->LocalGenerator->GetCurrentBinaryDirectory(),
  484. targetFullPathCompilePDB),
  485. cmOutputConverter::SHELL);
  486. if (this->LocalGenerator->IsMinGWMake() &&
  487. cmHasLiteralSuffix(targetOutPathCompilePDB, "\\")) {
  488. // mingw32-make incorrectly interprets 'a\ b c' as 'a b' and 'c'
  489. // (but 'a\ b "c"' as 'a\', 'b', and 'c'!). Workaround this by
  490. // avoiding a trailing backslash in the argument.
  491. targetOutPathCompilePDB[targetOutPathCompilePDB.size() - 1] = '/';
  492. }
  493. }
  494. cmRulePlaceholderExpander::RuleVariables vars;
  495. vars.RuleLauncher = "RULE_LAUNCH_COMPILE";
  496. vars.CMTargetName = this->GeneratorTarget->GetName().c_str();
  497. vars.CMTargetType =
  498. cmState::GetTargetTypeName(this->GeneratorTarget->GetType());
  499. vars.Language = lang.c_str();
  500. vars.Target = targetOutPathReal.c_str();
  501. vars.TargetPDB = targetOutPathPDB.c_str();
  502. vars.TargetCompilePDB = targetOutPathCompilePDB.c_str();
  503. vars.Source = sourceFile.c_str();
  504. std::string shellObj =
  505. this->LocalGenerator->ConvertToOutputFormat(obj, cmOutputConverter::SHELL);
  506. vars.Object = shellObj.c_str();
  507. std::string objectDir = this->GeneratorTarget->GetSupportDirectory();
  508. objectDir = this->LocalGenerator->ConvertToOutputFormat(
  509. this->LocalGenerator->MaybeConvertToRelativePath(
  510. this->LocalGenerator->GetCurrentBinaryDirectory(), objectDir),
  511. cmOutputConverter::SHELL);
  512. vars.ObjectDir = objectDir.c_str();
  513. std::string objectFileDir = cmSystemTools::GetFilenamePath(obj);
  514. objectFileDir = this->LocalGenerator->ConvertToOutputFormat(
  515. this->LocalGenerator->MaybeConvertToRelativePath(
  516. this->LocalGenerator->GetCurrentBinaryDirectory(), objectFileDir),
  517. cmOutputConverter::SHELL);
  518. vars.ObjectFileDir = objectFileDir.c_str();
  519. vars.Flags = flags.c_str();
  520. std::string definesString = "$(";
  521. definesString += lang;
  522. definesString += "_DEFINES)";
  523. this->LocalGenerator->JoinDefines(defines, definesString, lang);
  524. vars.Defines = definesString.c_str();
  525. std::string const includesString = "$(" + lang + "_INCLUDES)";
  526. vars.Includes = includesString.c_str();
  527. // At the moment, it is assumed that C, C++, and Fortran have both
  528. // assembly and preprocessor capabilities. The same is true for the
  529. // ability to export compile commands
  530. bool lang_has_preprocessor =
  531. ((lang == "C") || (lang == "CXX") || (lang == "Fortran"));
  532. bool const lang_has_assembly = lang_has_preprocessor;
  533. bool const lang_can_export_cmds = lang_has_preprocessor;
  534. CM_AUTO_PTR<cmRulePlaceholderExpander> rulePlaceholderExpander(
  535. this->LocalGenerator->CreateRulePlaceholderExpander());
  536. // Construct the compile rules.
  537. {
  538. std::string compileRuleVar = "CMAKE_";
  539. compileRuleVar += lang;
  540. compileRuleVar += "_COMPILE_OBJECT";
  541. std::string compileRule =
  542. this->Makefile->GetRequiredDefinition(compileRuleVar);
  543. std::vector<std::string> compileCommands;
  544. cmSystemTools::ExpandListArgument(compileRule, compileCommands);
  545. if (this->Makefile->IsOn("CMAKE_EXPORT_COMPILE_COMMANDS") &&
  546. lang_can_export_cmds && compileCommands.size() == 1) {
  547. std::string compileCommand = compileCommands[0];
  548. rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator,
  549. compileCommand, vars);
  550. std::string workingDirectory = cmSystemTools::CollapseFullPath(
  551. this->LocalGenerator->GetCurrentBinaryDirectory());
  552. compileCommand.replace(compileCommand.find(langFlags), langFlags.size(),
  553. this->GetFlags(lang));
  554. std::string langDefines = std::string("$(") + lang + "_DEFINES)";
  555. compileCommand.replace(compileCommand.find(langDefines),
  556. langDefines.size(), this->GetDefines(lang));
  557. std::string langIncludes = std::string("$(") + lang + "_INCLUDES)";
  558. compileCommand.replace(compileCommand.find(langIncludes),
  559. langIncludes.size(), this->GetIncludes(lang));
  560. this->GlobalGenerator->AddCXXCompileCommand(
  561. source.GetFullPath(), workingDirectory, compileCommand);
  562. }
  563. // Maybe insert an include-what-you-use runner.
  564. if (!compileCommands.empty() && (lang == "C" || lang == "CXX")) {
  565. std::string const iwyu_prop = lang + "_INCLUDE_WHAT_YOU_USE";
  566. const char* iwyu = this->GeneratorTarget->GetProperty(iwyu_prop);
  567. std::string const tidy_prop = lang + "_CLANG_TIDY";
  568. const char* tidy = this->GeneratorTarget->GetProperty(tidy_prop);
  569. if ((iwyu && *iwyu) || (tidy && *tidy)) {
  570. std::string run_iwyu = "$(CMAKE_COMMAND) -E __run_iwyu";
  571. if (iwyu && *iwyu) {
  572. run_iwyu += " --iwyu=";
  573. run_iwyu += this->LocalGenerator->EscapeForShell(iwyu);
  574. }
  575. if (tidy && *tidy) {
  576. run_iwyu += " --tidy=";
  577. run_iwyu += this->LocalGenerator->EscapeForShell(tidy);
  578. run_iwyu += " --source=";
  579. run_iwyu += sourceFile;
  580. }
  581. run_iwyu += " -- ";
  582. compileCommands.front().insert(0, run_iwyu);
  583. }
  584. }
  585. // Maybe insert a compiler launcher like ccache or distcc
  586. if (!compileCommands.empty() && (lang == "C" || lang == "CXX")) {
  587. std::string const clauncher_prop = lang + "_COMPILER_LAUNCHER";
  588. const char* clauncher =
  589. this->GeneratorTarget->GetProperty(clauncher_prop);
  590. if (clauncher && *clauncher) {
  591. std::vector<std::string> launcher_cmd;
  592. cmSystemTools::ExpandListArgument(clauncher, launcher_cmd, true);
  593. for (std::vector<std::string>::iterator i = launcher_cmd.begin(),
  594. e = launcher_cmd.end();
  595. i != e; ++i) {
  596. *i = this->LocalGenerator->EscapeForShell(*i);
  597. }
  598. std::string const& run_launcher = cmJoin(launcher_cmd, " ") + " ";
  599. compileCommands.front().insert(0, run_launcher);
  600. }
  601. }
  602. // Expand placeholders in the commands.
  603. for (std::vector<std::string>::iterator i = compileCommands.begin();
  604. i != compileCommands.end(); ++i) {
  605. rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator, *i,
  606. vars);
  607. }
  608. // Change the command working directory to the local build tree.
  609. this->LocalGenerator->CreateCDCommand(
  610. compileCommands, this->LocalGenerator->GetCurrentBinaryDirectory(),
  611. this->LocalGenerator->GetBinaryDirectory());
  612. commands.insert(commands.end(), compileCommands.begin(),
  613. compileCommands.end());
  614. }
  615. // Check for extra outputs created by the compilation.
  616. std::vector<std::string> outputs(1, relativeObj);
  617. if (const char* extra_outputs_str = source.GetProperty("OBJECT_OUTPUTS")) {
  618. // Register these as extra files to clean.
  619. cmSystemTools::ExpandListArgument(extra_outputs_str, outputs);
  620. this->CleanFiles.insert(this->CleanFiles.end(), outputs.begin() + 1,
  621. outputs.end());
  622. }
  623. // Write the rule.
  624. this->WriteMakeRule(*this->BuildFileStream, CM_NULLPTR, outputs, depends,
  625. commands);
  626. bool do_preprocess_rules = lang_has_preprocessor &&
  627. this->LocalGenerator->GetCreatePreprocessedSourceRules();
  628. bool do_assembly_rules =
  629. lang_has_assembly && this->LocalGenerator->GetCreateAssemblySourceRules();
  630. if (do_preprocess_rules || do_assembly_rules) {
  631. std::vector<std::string> force_depends;
  632. force_depends.push_back("cmake_force");
  633. std::string::size_type dot_pos = relativeObj.rfind('.');
  634. std::string relativeObjBase = relativeObj.substr(0, dot_pos);
  635. dot_pos = obj.rfind('.');
  636. std::string objBase = obj.substr(0, dot_pos);
  637. if (do_preprocess_rules) {
  638. commands.clear();
  639. std::string relativeObjI = relativeObjBase + ".i";
  640. std::string objI = objBase + ".i";
  641. std::string preprocessEcho = "Preprocessing ";
  642. preprocessEcho += lang;
  643. preprocessEcho += " source to ";
  644. preprocessEcho += objI;
  645. this->LocalGenerator->AppendEcho(
  646. commands, preprocessEcho, cmLocalUnixMakefileGenerator3::EchoBuild);
  647. std::string preprocessRuleVar = "CMAKE_";
  648. preprocessRuleVar += lang;
  649. preprocessRuleVar += "_CREATE_PREPROCESSED_SOURCE";
  650. if (const char* preprocessRule =
  651. this->Makefile->GetDefinition(preprocessRuleVar)) {
  652. std::vector<std::string> preprocessCommands;
  653. cmSystemTools::ExpandListArgument(preprocessRule, preprocessCommands);
  654. std::string shellObjI = this->LocalGenerator->ConvertToOutputFormat(
  655. objI, cmOutputConverter::SHELL);
  656. vars.PreprocessedSource = shellObjI.c_str();
  657. // Expand placeholders in the commands.
  658. for (std::vector<std::string>::iterator i = preprocessCommands.begin();
  659. i != preprocessCommands.end(); ++i) {
  660. rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator,
  661. *i, vars);
  662. }
  663. this->LocalGenerator->CreateCDCommand(
  664. preprocessCommands,
  665. this->LocalGenerator->GetCurrentBinaryDirectory(),
  666. this->LocalGenerator->GetBinaryDirectory());
  667. commands.insert(commands.end(), preprocessCommands.begin(),
  668. preprocessCommands.end());
  669. } else {
  670. std::string cmd = "$(CMAKE_COMMAND) -E cmake_unimplemented_variable ";
  671. cmd += preprocessRuleVar;
  672. commands.push_back(cmd);
  673. }
  674. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, CM_NULLPTR,
  675. relativeObjI, force_depends,
  676. commands, false);
  677. }
  678. if (do_assembly_rules) {
  679. commands.clear();
  680. std::string relativeObjS = relativeObjBase + ".s";
  681. std::string objS = objBase + ".s";
  682. std::string assemblyEcho = "Compiling ";
  683. assemblyEcho += lang;
  684. assemblyEcho += " source to assembly ";
  685. assemblyEcho += objS;
  686. this->LocalGenerator->AppendEcho(
  687. commands, assemblyEcho, cmLocalUnixMakefileGenerator3::EchoBuild);
  688. std::string assemblyRuleVar = "CMAKE_";
  689. assemblyRuleVar += lang;
  690. assemblyRuleVar += "_CREATE_ASSEMBLY_SOURCE";
  691. if (const char* assemblyRule =
  692. this->Makefile->GetDefinition(assemblyRuleVar)) {
  693. std::vector<std::string> assemblyCommands;
  694. cmSystemTools::ExpandListArgument(assemblyRule, assemblyCommands);
  695. std::string shellObjS = this->LocalGenerator->ConvertToOutputFormat(
  696. objS, cmOutputConverter::SHELL);
  697. vars.AssemblySource = shellObjS.c_str();
  698. // Expand placeholders in the commands.
  699. for (std::vector<std::string>::iterator i = assemblyCommands.begin();
  700. i != assemblyCommands.end(); ++i) {
  701. rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator,
  702. *i, vars);
  703. }
  704. this->LocalGenerator->CreateCDCommand(
  705. assemblyCommands, this->LocalGenerator->GetCurrentBinaryDirectory(),
  706. this->LocalGenerator->GetBinaryDirectory());
  707. commands.insert(commands.end(), assemblyCommands.begin(),
  708. assemblyCommands.end());
  709. } else {
  710. std::string cmd = "$(CMAKE_COMMAND) -E cmake_unimplemented_variable ";
  711. cmd += assemblyRuleVar;
  712. commands.push_back(cmd);
  713. }
  714. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, CM_NULLPTR,
  715. relativeObjS, force_depends,
  716. commands, false);
  717. }
  718. }
  719. // If the language needs provides-requires mode, create the
  720. // corresponding targets.
  721. std::string objectRequires = relativeObj;
  722. objectRequires += ".requires";
  723. std::vector<std::string> p_depends;
  724. // always provide an empty requires target
  725. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, CM_NULLPTR,
  726. objectRequires, p_depends, no_commands,
  727. true);
  728. // write a build rule to recursively build what this obj provides
  729. std::string objectProvides = relativeObj;
  730. objectProvides += ".provides";
  731. std::string temp = relativeObj;
  732. temp += ".provides.build";
  733. std::vector<std::string> r_commands;
  734. std::string tgtMakefileName =
  735. this->LocalGenerator->GetRelativeTargetDirectory(this->GeneratorTarget);
  736. tgtMakefileName += "/build.make";
  737. r_commands.push_back(
  738. this->LocalGenerator->GetRecursiveMakeCall(tgtMakefileName.c_str(), temp));
  739. p_depends.clear();
  740. p_depends.push_back(objectRequires);
  741. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, CM_NULLPTR,
  742. objectProvides, p_depends, r_commands,
  743. true);
  744. // write the provides.build rule dependency on the obj file
  745. p_depends.clear();
  746. p_depends.push_back(relativeObj);
  747. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, CM_NULLPTR, temp,
  748. p_depends, no_commands, false);
  749. }
  750. void cmMakefileTargetGenerator::WriteTargetRequiresRules()
  751. {
  752. std::vector<std::string> depends;
  753. std::vector<std::string> no_commands;
  754. // Construct the name of the dependency generation target.
  755. std::string depTarget =
  756. this->LocalGenerator->GetRelativeTargetDirectory(this->GeneratorTarget);
  757. depTarget += "/requires";
  758. // This target drives dependency generation for all object files.
  759. std::string relPath = this->LocalGenerator->GetHomeRelativeOutputPath();
  760. std::string objTarget;
  761. for (std::vector<std::string>::const_iterator obj = this->Objects.begin();
  762. obj != this->Objects.end(); ++obj) {
  763. objTarget = relPath;
  764. objTarget += *obj;
  765. objTarget += ".requires";
  766. depends.push_back(objTarget);
  767. }
  768. // Write the rule.
  769. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, CM_NULLPTR,
  770. depTarget, depends, no_commands, true);
  771. }
  772. void cmMakefileTargetGenerator::WriteTargetCleanRules()
  773. {
  774. std::vector<std::string> depends;
  775. std::vector<std::string> commands;
  776. // Construct the clean target name.
  777. std::string cleanTarget =
  778. this->LocalGenerator->GetRelativeTargetDirectory(this->GeneratorTarget);
  779. cleanTarget += "/clean";
  780. // Construct the clean command.
  781. this->LocalGenerator->AppendCleanCommand(commands, this->CleanFiles,
  782. this->GeneratorTarget);
  783. this->LocalGenerator->CreateCDCommand(
  784. commands, this->LocalGenerator->GetCurrentBinaryDirectory(),
  785. this->LocalGenerator->GetBinaryDirectory());
  786. // Write the rule.
  787. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, CM_NULLPTR,
  788. cleanTarget, depends, commands, true);
  789. }
  790. bool cmMakefileTargetGenerator::WriteMakeRule(
  791. std::ostream& os, const char* comment,
  792. const std::vector<std::string>& outputs,
  793. const std::vector<std::string>& depends,
  794. const std::vector<std::string>& commands, bool in_help)
  795. {
  796. bool symbolic = false;
  797. if (outputs.empty()) {
  798. return symbolic;
  799. }
  800. // Check whether we need to bother checking for a symbolic output.
  801. bool need_symbolic = this->GlobalGenerator->GetNeedSymbolicMark();
  802. // Check whether the first output is marked as symbolic.
  803. if (need_symbolic) {
  804. if (cmSourceFile* sf = this->Makefile->GetSource(outputs[0])) {
  805. symbolic = sf->GetPropertyAsBool("SYMBOLIC");
  806. }
  807. }
  808. // We always attach the actual commands to the first output.
  809. this->LocalGenerator->WriteMakeRule(os, comment, outputs[0], depends,
  810. commands, symbolic, in_help);
  811. // For single outputs, we are done.
  812. if (outputs.size() == 1) {
  813. return symbolic;
  814. }
  815. // For multiple outputs, make the extra ones depend on the first one.
  816. std::vector<std::string> const output_depends(1, outputs[0]);
  817. std::string binDir = this->LocalGenerator->GetBinaryDirectory();
  818. for (std::vector<std::string>::const_iterator o = outputs.begin() + 1;
  819. o != outputs.end(); ++o) {
  820. // Touch the extra output so "make" knows that it was updated,
  821. // but only if the output was acually created.
  822. std::string const out = this->LocalGenerator->ConvertToOutputFormat(
  823. this->LocalGenerator->MaybeConvertToRelativePath(binDir, *o),
  824. cmOutputConverter::SHELL);
  825. std::vector<std::string> output_commands;
  826. bool o_symbolic = false;
  827. if (need_symbolic) {
  828. if (cmSourceFile* sf = this->Makefile->GetSource(*o)) {
  829. o_symbolic = sf->GetPropertyAsBool("SYMBOLIC");
  830. }
  831. }
  832. symbolic = symbolic && o_symbolic;
  833. if (!o_symbolic) {
  834. output_commands.push_back("@$(CMAKE_COMMAND) -E touch_nocreate " + out);
  835. }
  836. this->LocalGenerator->WriteMakeRule(os, CM_NULLPTR, *o, output_depends,
  837. output_commands, o_symbolic, in_help);
  838. if (!o_symbolic) {
  839. // At build time, remove the first output if this one does not exist
  840. // so that "make" will rerun the real commands that create this one.
  841. MultipleOutputPairsType::value_type p(*o, outputs[0]);
  842. this->MultipleOutputPairs.insert(p);
  843. }
  844. }
  845. return symbolic;
  846. }
  847. void cmMakefileTargetGenerator::WriteTargetDependRules()
  848. {
  849. // must write the targets depend info file
  850. std::string dir =
  851. this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  852. this->InfoFileNameFull = dir;
  853. this->InfoFileNameFull += "/DependInfo.cmake";
  854. this->InfoFileNameFull =
  855. this->LocalGenerator->ConvertToFullPath(this->InfoFileNameFull);
  856. this->InfoFileStream =
  857. new cmGeneratedFileStream(this->InfoFileNameFull.c_str());
  858. this->InfoFileStream->SetCopyIfDifferent(true);
  859. if (!*this->InfoFileStream) {
  860. return;
  861. }
  862. this->LocalGenerator->WriteDependLanguageInfo(*this->InfoFileStream,
  863. this->GeneratorTarget);
  864. // Store multiple output pairs in the depend info file.
  865. if (!this->MultipleOutputPairs.empty()) {
  866. /* clang-format off */
  867. *this->InfoFileStream
  868. << "\n"
  869. << "# Pairs of files generated by the same build rule.\n"
  870. << "set(CMAKE_MULTIPLE_OUTPUT_PAIRS\n";
  871. /* clang-format on */
  872. for (MultipleOutputPairsType::const_iterator pi =
  873. this->MultipleOutputPairs.begin();
  874. pi != this->MultipleOutputPairs.end(); ++pi) {
  875. *this->InfoFileStream
  876. << " " << cmOutputConverter::EscapeForCMake(pi->first) << " "
  877. << cmOutputConverter::EscapeForCMake(pi->second) << "\n";
  878. }
  879. *this->InfoFileStream << " )\n\n";
  880. }
  881. // Store list of targets linked directly or transitively.
  882. {
  883. /* clang-format off */
  884. *this->InfoFileStream
  885. << "\n"
  886. << "# Targets to which this target links.\n"
  887. << "set(CMAKE_TARGET_LINKED_INFO_FILES\n";
  888. /* clang-format on */
  889. std::vector<std::string> dirs = this->GetLinkedTargetDirectories();
  890. for (std::vector<std::string>::iterator i = dirs.begin(); i != dirs.end();
  891. ++i) {
  892. *this->InfoFileStream << " \"" << *i << "/DependInfo.cmake\"\n";
  893. }
  894. *this->InfoFileStream << " )\n";
  895. }
  896. std::string const& working_dir =
  897. this->LocalGenerator->GetCurrentBinaryDirectory();
  898. /* clang-format off */
  899. *this->InfoFileStream
  900. << "\n"
  901. << "# Fortran module output directory.\n"
  902. << "set(CMAKE_Fortran_TARGET_MODULE_DIR \""
  903. << this->GeneratorTarget->GetFortranModuleDirectory(working_dir)
  904. << "\")\n";
  905. /* clang-format on */
  906. // and now write the rule to use it
  907. std::vector<std::string> depends;
  908. std::vector<std::string> commands;
  909. // Construct the name of the dependency generation target.
  910. std::string depTarget =
  911. this->LocalGenerator->GetRelativeTargetDirectory(this->GeneratorTarget);
  912. depTarget += "/depend";
  913. // Add a command to call CMake to scan dependencies. CMake will
  914. // touch the corresponding depends file after scanning dependencies.
  915. std::ostringstream depCmd;
  916. // TODO: Account for source file properties and directory-level
  917. // definitions when scanning for dependencies.
  918. #if !defined(_WIN32) || defined(__CYGWIN__)
  919. // This platform supports symlinks, so cmSystemTools will translate
  920. // paths. Make sure PWD is set to the original name of the home
  921. // output directory to help cmSystemTools to create the same
  922. // translation table for the dependency scanning process.
  923. depCmd << "cd " << (this->LocalGenerator->ConvertToOutputFormat(
  924. cmSystemTools::CollapseFullPath(
  925. this->LocalGenerator->GetBinaryDirectory()),
  926. cmOutputConverter::SHELL))
  927. << " && ";
  928. #endif
  929. // Generate a call this signature:
  930. //
  931. // cmake -E cmake_depends <generator>
  932. // <home-src-dir> <start-src-dir>
  933. // <home-out-dir> <start-out-dir>
  934. // <dep-info> --color=$(COLOR)
  935. //
  936. // This gives the dependency scanner enough information to recreate
  937. // the state of our local generator sufficiently for its needs.
  938. depCmd << "$(CMAKE_COMMAND) -E cmake_depends \""
  939. << this->GlobalGenerator->GetName() << "\" "
  940. << this->LocalGenerator->ConvertToOutputFormat(
  941. cmSystemTools::CollapseFullPath(
  942. this->LocalGenerator->GetSourceDirectory()),
  943. cmOutputConverter::SHELL)
  944. << " "
  945. << this->LocalGenerator->ConvertToOutputFormat(
  946. cmSystemTools::CollapseFullPath(
  947. this->LocalGenerator->GetCurrentSourceDirectory()),
  948. cmOutputConverter::SHELL)
  949. << " "
  950. << this->LocalGenerator->ConvertToOutputFormat(
  951. cmSystemTools::CollapseFullPath(
  952. this->LocalGenerator->GetBinaryDirectory()),
  953. cmOutputConverter::SHELL)
  954. << " "
  955. << this->LocalGenerator->ConvertToOutputFormat(
  956. cmSystemTools::CollapseFullPath(
  957. this->LocalGenerator->GetCurrentBinaryDirectory()),
  958. cmOutputConverter::SHELL)
  959. << " "
  960. << this->LocalGenerator->ConvertToOutputFormat(
  961. cmSystemTools::CollapseFullPath(this->InfoFileNameFull),
  962. cmOutputConverter::SHELL);
  963. if (this->LocalGenerator->GetColorMakefile()) {
  964. depCmd << " --color=$(COLOR)";
  965. }
  966. commands.push_back(depCmd.str());
  967. // Make sure all custom command outputs in this target are built.
  968. if (this->CustomCommandDriver == OnDepends) {
  969. this->DriveCustomCommands(depends);
  970. }
  971. // Write the rule.
  972. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, CM_NULLPTR,
  973. depTarget, depends, commands, true);
  974. }
  975. void cmMakefileTargetGenerator::DriveCustomCommands(
  976. std::vector<std::string>& depends)
  977. {
  978. // Depend on all custom command outputs.
  979. std::vector<cmSourceFile*> sources;
  980. this->GeneratorTarget->GetSourceFiles(
  981. sources, this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE"));
  982. for (std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  983. source != sources.end(); ++source) {
  984. if (cmCustomCommand* cc = (*source)->GetCustomCommand()) {
  985. cmCustomCommandGenerator ccg(*cc, this->ConfigName,
  986. this->LocalGenerator);
  987. const std::vector<std::string>& outputs = ccg.GetOutputs();
  988. depends.insert(depends.end(), outputs.begin(), outputs.end());
  989. }
  990. }
  991. }
  992. void cmMakefileTargetGenerator::WriteObjectDependRules(
  993. cmSourceFile const& source, std::vector<std::string>& depends)
  994. {
  995. // Create the list of dependencies known at cmake time. These are
  996. // shared between the object file and dependency scanning rule.
  997. depends.push_back(source.GetFullPath());
  998. if (const char* objectDeps = source.GetProperty("OBJECT_DEPENDS")) {
  999. cmSystemTools::ExpandListArgument(objectDeps, depends);
  1000. }
  1001. }
  1002. void cmMakefileTargetGenerator::GenerateCustomRuleFile(
  1003. cmCustomCommandGenerator const& ccg)
  1004. {
  1005. // Collect the commands.
  1006. std::vector<std::string> commands;
  1007. std::string comment = this->LocalGenerator->ConstructComment(ccg);
  1008. if (!comment.empty()) {
  1009. // add in a progress call if needed
  1010. this->NumberOfProgressActions++;
  1011. if (!this->NoRuleMessages) {
  1012. cmLocalUnixMakefileGenerator3::EchoProgress progress;
  1013. this->MakeEchoProgress(progress);
  1014. this->LocalGenerator->AppendEcho(
  1015. commands, comment, cmLocalUnixMakefileGenerator3::EchoGenerate,
  1016. &progress);
  1017. }
  1018. }
  1019. // Now append the actual user-specified commands.
  1020. std::ostringstream content;
  1021. this->LocalGenerator->AppendCustomCommand(
  1022. commands, ccg, this->GeneratorTarget,
  1023. this->LocalGenerator->GetBinaryDirectory(), false, &content);
  1024. // Collect the dependencies.
  1025. std::vector<std::string> depends;
  1026. this->LocalGenerator->AppendCustomDepend(depends, ccg);
  1027. // Write the rule.
  1028. const std::vector<std::string>& outputs = ccg.GetOutputs();
  1029. bool symbolic = this->WriteMakeRule(*this->BuildFileStream, CM_NULLPTR,
  1030. outputs, depends, commands);
  1031. // If the rule has changed make sure the output is rebuilt.
  1032. if (!symbolic) {
  1033. this->GlobalGenerator->AddRuleHash(ccg.GetOutputs(), content.str());
  1034. }
  1035. // Setup implicit dependency scanning.
  1036. for (cmCustomCommand::ImplicitDependsList::const_iterator idi =
  1037. ccg.GetCC().GetImplicitDepends().begin();
  1038. idi != ccg.GetCC().GetImplicitDepends().end(); ++idi) {
  1039. std::string objFullPath = cmSystemTools::CollapseFullPath(outputs[0]);
  1040. std::string srcFullPath = cmSystemTools::CollapseFullPath(idi->second);
  1041. this->LocalGenerator->AddImplicitDepends(this->GeneratorTarget, idi->first,
  1042. objFullPath.c_str(),
  1043. srcFullPath.c_str());
  1044. }
  1045. }
  1046. void cmMakefileTargetGenerator::MakeEchoProgress(
  1047. cmLocalUnixMakefileGenerator3::EchoProgress& progress) const
  1048. {
  1049. progress.Dir = this->LocalGenerator->GetBinaryDirectory();
  1050. progress.Dir += cmake::GetCMakeFilesDirectory();
  1051. std::ostringstream progressArg;
  1052. progressArg << "$(CMAKE_PROGRESS_" << this->NumberOfProgressActions << ")";
  1053. progress.Arg = progressArg.str();
  1054. }
  1055. void cmMakefileTargetGenerator::WriteObjectsVariable(
  1056. std::string& variableName, std::string& variableNameExternal,
  1057. bool useWatcomQuote)
  1058. {
  1059. // Write a make variable assignment that lists all objects for the
  1060. // target.
  1061. variableName = this->LocalGenerator->CreateMakeVariable(
  1062. this->GeneratorTarget->GetName(), "_OBJECTS");
  1063. *this->BuildFileStream << "# Object files for target "
  1064. << this->GeneratorTarget->GetName() << "\n"
  1065. << variableName << " =";
  1066. std::string object;
  1067. const char* lineContinue =
  1068. this->Makefile->GetDefinition("CMAKE_MAKE_LINE_CONTINUE");
  1069. if (!lineContinue) {
  1070. lineContinue = "\\";
  1071. }
  1072. for (std::vector<std::string>::const_iterator i = this->Objects.begin();
  1073. i != this->Objects.end(); ++i) {
  1074. *this->BuildFileStream << " " << lineContinue << "\n";
  1075. *this->BuildFileStream << this->LocalGenerator->ConvertToQuotedOutputPath(
  1076. i->c_str(), useWatcomQuote);
  1077. }
  1078. *this->BuildFileStream << "\n";
  1079. // Write a make variable assignment that lists all external objects
  1080. // for the target.
  1081. variableNameExternal = this->LocalGenerator->CreateMakeVariable(
  1082. this->GeneratorTarget->GetName(), "_EXTERNAL_OBJECTS");
  1083. /* clang-format off */
  1084. *this->BuildFileStream
  1085. << "\n"
  1086. << "# External object files for target "
  1087. << this->GeneratorTarget->GetName() << "\n"
  1088. << variableNameExternal << " =";
  1089. /* clang-format on */
  1090. std::string currentBinDir =
  1091. this->LocalGenerator->GetCurrentBinaryDirectory();
  1092. for (std::vector<std::string>::const_iterator i =
  1093. this->ExternalObjects.begin();
  1094. i != this->ExternalObjects.end(); ++i) {
  1095. object =
  1096. this->LocalGenerator->MaybeConvertToRelativePath(currentBinDir, *i);
  1097. *this->BuildFileStream << " " << lineContinue << "\n";
  1098. *this->BuildFileStream << this->LocalGenerator->ConvertToQuotedOutputPath(
  1099. i->c_str(), useWatcomQuote);
  1100. }
  1101. *this->BuildFileStream << "\n"
  1102. << "\n";
  1103. }
  1104. class cmMakefileTargetGeneratorObjectStrings
  1105. {
  1106. public:
  1107. cmMakefileTargetGeneratorObjectStrings(std::vector<std::string>& strings,
  1108. cmOutputConverter* outputConverter,
  1109. cmStateDirectory stateDir,
  1110. std::string::size_type limit)
  1111. : Strings(strings)
  1112. , OutputConverter(outputConverter)
  1113. , StateDir(stateDir)
  1114. , LengthLimit(limit)
  1115. {
  1116. this->Space = "";
  1117. }
  1118. void Feed(std::string const& obj)
  1119. {
  1120. // Construct the name of the next object.
  1121. this->NextObject = this->OutputConverter->ConvertToOutputFormat(
  1122. this->MaybeConvertToRelativePath(obj), cmOutputConverter::RESPONSE);
  1123. // Roll over to next string if the limit will be exceeded.
  1124. if (this->LengthLimit != std::string::npos &&
  1125. (this->CurrentString.length() + 1 + this->NextObject.length() >
  1126. this->LengthLimit)) {
  1127. this->Strings.push_back(this->CurrentString);
  1128. this->CurrentString = "";
  1129. this->Space = "";
  1130. }
  1131. // Separate from previous object.
  1132. this->CurrentString += this->Space;
  1133. this->Space = " ";
  1134. // Append this object.
  1135. this->CurrentString += this->NextObject;
  1136. }
  1137. void Done() { this->Strings.push_back(this->CurrentString); }
  1138. private:
  1139. std::string MaybeConvertToRelativePath(std::string const& obj)
  1140. {
  1141. if (!cmOutputConverter::ContainedInDirectory(
  1142. this->StateDir.GetCurrentBinary(), obj, this->StateDir)) {
  1143. return obj;
  1144. }
  1145. return cmOutputConverter::ForceToRelativePath(
  1146. this->StateDir.GetCurrentBinary(), obj);
  1147. }
  1148. std::vector<std::string>& Strings;
  1149. cmOutputConverter* OutputConverter;
  1150. cmStateDirectory StateDir;
  1151. std::string::size_type LengthLimit;
  1152. std::string CurrentString;
  1153. std::string NextObject;
  1154. const char* Space;
  1155. };
  1156. void cmMakefileTargetGenerator::WriteObjectsStrings(
  1157. std::vector<std::string>& objStrings, std::string::size_type limit)
  1158. {
  1159. cmMakefileTargetGeneratorObjectStrings helper(
  1160. objStrings, this->LocalGenerator,
  1161. this->LocalGenerator->GetStateSnapshot().GetDirectory(), limit);
  1162. for (std::vector<std::string>::const_iterator i = this->Objects.begin();
  1163. i != this->Objects.end(); ++i) {
  1164. helper.Feed(*i);
  1165. }
  1166. for (std::vector<std::string>::const_iterator i =
  1167. this->ExternalObjects.begin();
  1168. i != this->ExternalObjects.end(); ++i) {
  1169. helper.Feed(*i);
  1170. }
  1171. helper.Done();
  1172. }
  1173. void cmMakefileTargetGenerator::WriteTargetDriverRule(
  1174. const std::string& main_output, bool relink)
  1175. {
  1176. // Compute the name of the driver target.
  1177. std::string dir =
  1178. this->LocalGenerator->GetRelativeTargetDirectory(this->GeneratorTarget);
  1179. std::string buildTargetRuleName = dir;
  1180. buildTargetRuleName += relink ? "/preinstall" : "/build";
  1181. buildTargetRuleName = this->LocalGenerator->MaybeConvertToRelativePath(
  1182. this->LocalGenerator->GetBinaryDirectory(), buildTargetRuleName);
  1183. // Build the list of target outputs to drive.
  1184. std::vector<std::string> depends;
  1185. depends.push_back(main_output);
  1186. const char* comment = CM_NULLPTR;
  1187. if (relink) {
  1188. // Setup the comment for the preinstall driver.
  1189. comment = "Rule to relink during preinstall.";
  1190. } else {
  1191. // Setup the comment for the main build driver.
  1192. comment = "Rule to build all files generated by this target.";
  1193. // Make sure all custom command outputs in this target are built.
  1194. if (this->CustomCommandDriver == OnBuild) {
  1195. this->DriveCustomCommands(depends);
  1196. }
  1197. // Make sure the extra files are built.
  1198. depends.insert(depends.end(), this->ExtraFiles.begin(),
  1199. this->ExtraFiles.end());
  1200. }
  1201. // Write the driver rule.
  1202. std::vector<std::string> no_commands;
  1203. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, comment,
  1204. buildTargetRuleName, depends,
  1205. no_commands, true);
  1206. }
  1207. void cmMakefileTargetGenerator::AppendTargetDepends(
  1208. std::vector<std::string>& depends)
  1209. {
  1210. // Static libraries never depend on anything for linking.
  1211. if (this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY) {
  1212. return;
  1213. }
  1214. // Loop over all library dependencies.
  1215. const char* cfg = this->LocalGenerator->GetConfigName().c_str();
  1216. if (cmComputeLinkInformation* cli =
  1217. this->GeneratorTarget->GetLinkInformation(cfg)) {
  1218. std::vector<std::string> const& libDeps = cli->GetDepends();
  1219. depends.insert(depends.end(), libDeps.begin(), libDeps.end());
  1220. }
  1221. }
  1222. void cmMakefileTargetGenerator::AppendObjectDepends(
  1223. std::vector<std::string>& depends)
  1224. {
  1225. // Add dependencies on the compiled object files.
  1226. std::string relPath = this->LocalGenerator->GetHomeRelativeOutputPath();
  1227. std::string objTarget;
  1228. for (std::vector<std::string>::const_iterator obj = this->Objects.begin();
  1229. obj != this->Objects.end(); ++obj) {
  1230. objTarget = relPath;
  1231. objTarget += *obj;
  1232. depends.push_back(objTarget);
  1233. }
  1234. // Add dependencies on the external object files.
  1235. depends.insert(depends.end(), this->ExternalObjects.begin(),
  1236. this->ExternalObjects.end());
  1237. // Add a dependency on the rule file itself.
  1238. this->LocalGenerator->AppendRuleDepend(depends,
  1239. this->BuildFileNameFull.c_str());
  1240. }
  1241. void cmMakefileTargetGenerator::AppendLinkDepends(
  1242. std::vector<std::string>& depends)
  1243. {
  1244. this->AppendObjectDepends(depends);
  1245. // Add dependencies on targets that must be built first.
  1246. this->AppendTargetDepends(depends);
  1247. // Add a dependency on the link definitions file, if any.
  1248. if (this->ModuleDefinitionFile) {
  1249. depends.push_back(this->ModuleDefinitionFile->GetFullPath());
  1250. }
  1251. // Add a dependency on user-specified manifest files, if any.
  1252. std::vector<cmSourceFile const*> manifest_srcs;
  1253. this->GeneratorTarget->GetManifests(manifest_srcs, this->ConfigName);
  1254. for (std::vector<cmSourceFile const*>::iterator mi = manifest_srcs.begin();
  1255. mi != manifest_srcs.end(); ++mi) {
  1256. depends.push_back((*mi)->GetFullPath());
  1257. }
  1258. // Add user-specified dependencies.
  1259. if (const char* linkDepends =
  1260. this->GeneratorTarget->GetProperty("LINK_DEPENDS")) {
  1261. cmSystemTools::ExpandListArgument(linkDepends, depends);
  1262. }
  1263. }
  1264. std::string cmMakefileTargetGenerator::GetLinkRule(
  1265. const std::string& linkRuleVar)
  1266. {
  1267. std::string linkRule = this->Makefile->GetRequiredDefinition(linkRuleVar);
  1268. if (this->GeneratorTarget->HasImplibGNUtoMS()) {
  1269. std::string ruleVar = "CMAKE_";
  1270. ruleVar += this->GeneratorTarget->GetLinkerLanguage(this->ConfigName);
  1271. ruleVar += "_GNUtoMS_RULE";
  1272. if (const char* rule = this->Makefile->GetDefinition(ruleVar)) {
  1273. linkRule += rule;
  1274. }
  1275. }
  1276. return linkRule;
  1277. }
  1278. void cmMakefileTargetGenerator::CloseFileStreams()
  1279. {
  1280. delete this->BuildFileStream;
  1281. delete this->InfoFileStream;
  1282. delete this->FlagFileStream;
  1283. }
  1284. void cmMakefileTargetGenerator::CreateLinkScript(
  1285. const char* name, std::vector<std::string> const& link_commands,
  1286. std::vector<std::string>& makefile_commands,
  1287. std::vector<std::string>& makefile_depends)
  1288. {
  1289. // Create the link script file.
  1290. std::string linkScriptName = this->TargetBuildDirectoryFull;
  1291. linkScriptName += "/";
  1292. linkScriptName += name;
  1293. cmGeneratedFileStream linkScriptStream(linkScriptName.c_str());
  1294. linkScriptStream.SetCopyIfDifferent(true);
  1295. for (std::vector<std::string>::const_iterator cmd = link_commands.begin();
  1296. cmd != link_commands.end(); ++cmd) {
  1297. // Do not write out empty commands or commands beginning in the
  1298. // shell no-op ":".
  1299. if (!cmd->empty() && (*cmd)[0] != ':') {
  1300. linkScriptStream << *cmd << "\n";
  1301. }
  1302. }
  1303. // Create the makefile command to invoke the link script.
  1304. std::string link_command = "$(CMAKE_COMMAND) -E cmake_link_script ";
  1305. link_command += this->LocalGenerator->ConvertToOutputFormat(
  1306. this->LocalGenerator->MaybeConvertToRelativePath(
  1307. this->LocalGenerator->GetCurrentBinaryDirectory(), linkScriptName),
  1308. cmOutputConverter::SHELL);
  1309. link_command += " --verbose=$(VERBOSE)";
  1310. makefile_commands.push_back(link_command);
  1311. makefile_depends.push_back(linkScriptName);
  1312. }
  1313. static size_t calculateCommandLineLengthLimit()
  1314. {
  1315. #if defined(_SC_ARG_MAX)
  1316. return ((size_t)sysconf(_SC_ARG_MAX)) - 1000;
  1317. #else
  1318. return 0;
  1319. #endif
  1320. }
  1321. bool cmMakefileTargetGenerator::CheckUseResponseFileForObjects(
  1322. std::string const& l) const
  1323. {
  1324. // Check for an explicit setting one way or the other.
  1325. std::string const responseVar =
  1326. "CMAKE_" + l + "_USE_RESPONSE_FILE_FOR_OBJECTS";
  1327. if (const char* val = this->Makefile->GetDefinition(responseVar)) {
  1328. if (*val) {
  1329. return cmSystemTools::IsOn(val);
  1330. }
  1331. }
  1332. // Check for a system limit.
  1333. if (size_t const limit = calculateCommandLineLengthLimit()) {
  1334. // Compute the total length of our list of object files with room
  1335. // for argument separation and quoting. This does not convert paths
  1336. // relative to CMAKE_CURRENT_BINARY_DIR like the final list will be, so the
  1337. // actual list will likely be much shorter than this. However, in the
  1338. // worst case all objects will remain as absolute paths.
  1339. size_t length = 0;
  1340. for (std::vector<std::string>::const_iterator i = this->Objects.begin();
  1341. i != this->Objects.end(); ++i) {
  1342. length += i->size() + 3;
  1343. }
  1344. for (std::vector<std::string>::const_iterator i =
  1345. this->ExternalObjects.begin();
  1346. i != this->ExternalObjects.end(); ++i) {
  1347. length += i->size() + 3;
  1348. }
  1349. // We need to guarantee room for both objects and libraries, so
  1350. // if the objects take up more than half then use a response file
  1351. // for them.
  1352. if (length > (limit / 2)) {
  1353. return true;
  1354. }
  1355. }
  1356. // We do not need a response file for objects.
  1357. return false;
  1358. }
  1359. bool cmMakefileTargetGenerator::CheckUseResponseFileForLibraries(
  1360. std::string const& l) const
  1361. {
  1362. // Check for an explicit setting one way or the other.
  1363. std::string const responseVar =
  1364. "CMAKE_" + l + "_USE_RESPONSE_FILE_FOR_LIBRARIES";
  1365. if (const char* val = this->Makefile->GetDefinition(responseVar)) {
  1366. if (*val) {
  1367. return cmSystemTools::IsOn(val);
  1368. }
  1369. }
  1370. // We do not need a response file for libraries.
  1371. return false;
  1372. }
  1373. std::string cmMakefileTargetGenerator::CreateResponseFile(
  1374. const char* name, std::string const& options,
  1375. std::vector<std::string>& makefile_depends)
  1376. {
  1377. // Create the response file.
  1378. std::string responseFileNameFull = this->TargetBuildDirectoryFull;
  1379. responseFileNameFull += "/";
  1380. responseFileNameFull += name;
  1381. cmGeneratedFileStream responseStream(responseFileNameFull.c_str());
  1382. responseStream.SetCopyIfDifferent(true);
  1383. responseStream << options << "\n";
  1384. // Add a dependency so the target will rebuild when the set of
  1385. // objects changes.
  1386. makefile_depends.push_back(responseFileNameFull);
  1387. // Construct the name to be used on the command line.
  1388. std::string responseFileName = this->TargetBuildDirectory;
  1389. responseFileName += "/";
  1390. responseFileName += name;
  1391. return responseFileName;
  1392. }
  1393. cmLinkLineComputer* cmMakefileTargetGenerator::CreateLinkLineComputer(
  1394. cmOutputConverter* outputConverter, cmStateDirectory stateDir)
  1395. {
  1396. if (this->Makefile->IsOn("MSVC60")) {
  1397. return this->GlobalGenerator->CreateMSVC60LinkLineComputer(outputConverter,
  1398. stateDir);
  1399. }
  1400. return this->GlobalGenerator->CreateLinkLineComputer(outputConverter,
  1401. stateDir);
  1402. }
  1403. void cmMakefileTargetGenerator::CreateLinkLibs(
  1404. cmLinkLineComputer* linkLineComputer, std::string& linkLibs,
  1405. bool useResponseFile, std::vector<std::string>& makefile_depends)
  1406. {
  1407. std::string frameworkPath;
  1408. std::string linkPath;
  1409. std::string config = this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  1410. cmComputeLinkInformation* pcli =
  1411. this->GeneratorTarget->GetLinkInformation(config);
  1412. this->LocalGenerator->OutputLinkLibraries(pcli, linkLineComputer, linkLibs,
  1413. frameworkPath, linkPath);
  1414. linkLibs = frameworkPath + linkPath + linkLibs;
  1415. if (useResponseFile && linkLibs.find_first_not_of(' ') != linkLibs.npos) {
  1416. // Lookup the response file reference flag.
  1417. std::string responseFlagVar = "CMAKE_";
  1418. responseFlagVar +=
  1419. this->GeneratorTarget->GetLinkerLanguage(this->ConfigName);
  1420. responseFlagVar += "_RESPONSE_FILE_LINK_FLAG";
  1421. const char* responseFlag = this->Makefile->GetDefinition(responseFlagVar);
  1422. if (!responseFlag) {
  1423. responseFlag = "@";
  1424. }
  1425. // Create this response file.
  1426. std::string link_rsp =
  1427. this->CreateResponseFile("linklibs.rsp", linkLibs, makefile_depends);
  1428. // Reference the response file.
  1429. linkLibs = responseFlag;
  1430. linkLibs += this->LocalGenerator->ConvertToOutputFormat(
  1431. link_rsp, cmOutputConverter::SHELL);
  1432. }
  1433. }
  1434. void cmMakefileTargetGenerator::CreateObjectLists(
  1435. bool useLinkScript, bool useArchiveRules, bool useResponseFile,
  1436. std::string& buildObjs, std::vector<std::string>& makefile_depends,
  1437. bool useWatcomQuote)
  1438. {
  1439. std::string variableName;
  1440. std::string variableNameExternal;
  1441. this->WriteObjectsVariable(variableName, variableNameExternal,
  1442. useWatcomQuote);
  1443. if (useResponseFile) {
  1444. // MSVC response files cannot exceed 128K.
  1445. std::string::size_type const responseFileLimit = 131000;
  1446. // Construct the individual object list strings.
  1447. std::vector<std::string> object_strings;
  1448. this->WriteObjectsStrings(object_strings, responseFileLimit);
  1449. // Lookup the response file reference flag.
  1450. std::string responseFlagVar = "CMAKE_";
  1451. responseFlagVar +=
  1452. this->GeneratorTarget->GetLinkerLanguage(this->ConfigName);
  1453. responseFlagVar += "_RESPONSE_FILE_LINK_FLAG";
  1454. const char* responseFlag = this->Makefile->GetDefinition(responseFlagVar);
  1455. if (!responseFlag) {
  1456. responseFlag = "@";
  1457. }
  1458. // Write a response file for each string.
  1459. const char* sep = "";
  1460. for (unsigned int i = 0; i < object_strings.size(); ++i) {
  1461. // Number the response files.
  1462. char rsp[32];
  1463. sprintf(rsp, "objects%u.rsp", i + 1);
  1464. // Create this response file.
  1465. std::string objects_rsp =
  1466. this->CreateResponseFile(rsp, object_strings[i], makefile_depends);
  1467. // Separate from previous response file references.
  1468. buildObjs += sep;
  1469. sep = " ";
  1470. // Reference the response file.
  1471. buildObjs += responseFlag;
  1472. buildObjs += this->LocalGenerator->ConvertToOutputFormat(
  1473. objects_rsp, cmOutputConverter::SHELL);
  1474. }
  1475. } else if (useLinkScript) {
  1476. if (!useArchiveRules) {
  1477. std::vector<std::string> objStrings;
  1478. this->WriteObjectsStrings(objStrings);
  1479. buildObjs = objStrings[0];
  1480. }
  1481. } else {
  1482. buildObjs = "$(";
  1483. buildObjs += variableName;
  1484. buildObjs += ") $(";
  1485. buildObjs += variableNameExternal;
  1486. buildObjs += ")";
  1487. }
  1488. }
  1489. void cmMakefileTargetGenerator::AddIncludeFlags(std::string& flags,
  1490. const std::string& lang)
  1491. {
  1492. std::string responseVar = "CMAKE_";
  1493. responseVar += lang;
  1494. responseVar += "_USE_RESPONSE_FILE_FOR_INCLUDES";
  1495. bool useResponseFile = this->Makefile->IsOn(responseVar);
  1496. std::vector<std::string> includes;
  1497. const std::string& config =
  1498. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  1499. this->LocalGenerator->GetIncludeDirectories(includes, this->GeneratorTarget,
  1500. lang, config);
  1501. std::string includeFlags = this->LocalGenerator->GetIncludeFlags(
  1502. includes, this->GeneratorTarget, lang, false, useResponseFile, config);
  1503. if (includeFlags.empty()) {
  1504. return;
  1505. }
  1506. if (useResponseFile) {
  1507. std::string name = "includes_";
  1508. name += lang;
  1509. name += ".rsp";
  1510. std::string arg = "@" +
  1511. this->CreateResponseFile(name.c_str(), includeFlags,
  1512. this->FlagFileDepends[lang]);
  1513. this->LocalGenerator->AppendFlags(flags, arg);
  1514. } else {
  1515. this->LocalGenerator->AppendFlags(flags, includeFlags);
  1516. }
  1517. }
  1518. void cmMakefileTargetGenerator::GenDefFile(
  1519. std::vector<std::string>& real_link_commands, std::string& linkFlags)
  1520. {
  1521. if (this->GeneratorTarget->GetPropertyAsBool("WINDOWS_EXPORT_ALL_SYMBOLS")) {
  1522. std::string name_of_def_file =
  1523. this->GeneratorTarget->GetSupportDirectory();
  1524. name_of_def_file += std::string("/") + this->GeneratorTarget->GetName();
  1525. name_of_def_file += ".def";
  1526. std::string cmd = cmSystemTools::GetCMakeCommand();
  1527. cmd = this->LocalGenerator->ConvertToOutputFormat(
  1528. cmd, cmOutputConverter::SHELL);
  1529. cmd += " -E __create_def ";
  1530. cmd += this->LocalGenerator->ConvertToOutputFormat(
  1531. this->LocalGenerator->MaybeConvertToRelativePath(
  1532. this->LocalGenerator->GetCurrentBinaryDirectory(), name_of_def_file),
  1533. cmOutputConverter::SHELL);
  1534. cmd += " ";
  1535. std::string objlist_file = name_of_def_file;
  1536. objlist_file += ".objs";
  1537. cmd += this->LocalGenerator->ConvertToOutputFormat(
  1538. this->LocalGenerator->MaybeConvertToRelativePath(
  1539. this->LocalGenerator->GetCurrentBinaryDirectory(), objlist_file),
  1540. cmOutputConverter::SHELL);
  1541. real_link_commands.insert(real_link_commands.begin(), cmd);
  1542. // create a list of obj files for the -E __create_def to read
  1543. cmGeneratedFileStream fout(objlist_file.c_str());
  1544. for (std::vector<std::string>::const_iterator i = this->Objects.begin();
  1545. i != this->Objects.end(); ++i) {
  1546. if (cmHasLiteralSuffix(*i, ".obj")) {
  1547. fout << *i << "\n";
  1548. }
  1549. }
  1550. for (std::vector<std::string>::const_iterator i =
  1551. this->ExternalObjects.begin();
  1552. i != this->ExternalObjects.end(); ++i) {
  1553. fout << *i << "\n";
  1554. }
  1555. // now add the def file link flag
  1556. linkFlags += " ";
  1557. linkFlags += this->Makefile->GetSafeDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  1558. linkFlags += this->LocalGenerator->ConvertToOutputFormat(
  1559. this->LocalGenerator->MaybeConvertToRelativePath(
  1560. this->LocalGenerator->GetCurrentBinaryDirectory(), name_of_def_file),
  1561. cmOutputConverter::SHELL);
  1562. linkFlags += " ";
  1563. }
  1564. }