cmMakefileTargetGenerator.cxx 66 KB

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