cmMakefileExecutableTargetGenerator.cxx 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  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 "cmMakefileExecutableTargetGenerator.h"
  4. #include <set>
  5. #include <sstream>
  6. #include <string>
  7. #include <utility>
  8. #include <vector>
  9. #include "cm_memory.hxx"
  10. #include "cmAlgorithms.h"
  11. #include "cmGeneratedFileStream.h"
  12. #include "cmGeneratorTarget.h"
  13. #include "cmGlobalUnixMakefileGenerator3.h"
  14. #include "cmLinkLineComputer.h"
  15. #include "cmLinkLineDeviceComputer.h"
  16. #include "cmLocalGenerator.h"
  17. #include "cmLocalUnixMakefileGenerator3.h"
  18. #include "cmMakefile.h"
  19. #include "cmOSXBundleGenerator.h"
  20. #include "cmOutputConverter.h"
  21. #include "cmRulePlaceholderExpander.h"
  22. #include "cmState.h"
  23. #include "cmStateDirectory.h"
  24. #include "cmStateSnapshot.h"
  25. #include "cmStateTypes.h"
  26. #include "cmStringAlgorithms.h"
  27. #include "cmSystemTools.h"
  28. cmMakefileExecutableTargetGenerator::cmMakefileExecutableTargetGenerator(
  29. cmGeneratorTarget* target)
  30. : cmMakefileTargetGenerator(target)
  31. {
  32. this->CustomCommandDriver = OnDepends;
  33. this->TargetNames =
  34. this->GeneratorTarget->GetExecutableNames(this->ConfigName);
  35. this->OSXBundleGenerator =
  36. cm::make_unique<cmOSXBundleGenerator>(target, this->ConfigName);
  37. this->OSXBundleGenerator->SetMacContentFolders(&this->MacContentFolders);
  38. this->LocalGenerator->AddUnityBuild(target, this->ConfigName);
  39. this->LocalGenerator->AddPchDependencies(target, this->ConfigName);
  40. }
  41. cmMakefileExecutableTargetGenerator::~cmMakefileExecutableTargetGenerator() =
  42. default;
  43. void cmMakefileExecutableTargetGenerator::WriteRuleFiles()
  44. {
  45. // create the build.make file and directory, put in the common blocks
  46. this->CreateRuleFile();
  47. // write rules used to help build object files
  48. this->WriteCommonCodeRules();
  49. // write the per-target per-language flags
  50. this->WriteTargetLanguageFlags();
  51. // write in rules for object files and custom commands
  52. this->WriteTargetBuildRules();
  53. // write the device link rules
  54. this->WriteDeviceExecutableRule(false);
  55. // write the link rules
  56. this->WriteExecutableRule(false);
  57. if (this->GeneratorTarget->NeedRelinkBeforeInstall(this->ConfigName)) {
  58. // Write rules to link an installable version of the target.
  59. this->WriteExecutableRule(true);
  60. }
  61. // Write clean target
  62. this->WriteTargetCleanRules();
  63. // Write the dependency generation rule. This must be done last so
  64. // that multiple output pair information is available.
  65. this->WriteTargetDependRules();
  66. // close the streams
  67. this->CloseFileStreams();
  68. }
  69. void cmMakefileExecutableTargetGenerator::WriteDeviceExecutableRule(
  70. bool relink)
  71. {
  72. #ifndef CMAKE_BOOTSTRAP
  73. const bool requiresDeviceLinking = requireDeviceLinking(
  74. *this->GeneratorTarget, *this->LocalGenerator, this->ConfigName);
  75. if (!requiresDeviceLinking) {
  76. return;
  77. }
  78. std::vector<std::string> commands;
  79. // Get the language to use for linking this library.
  80. std::string linkLanguage = "CUDA";
  81. std::string const& objExt =
  82. this->Makefile->GetSafeDefinition("CMAKE_CUDA_OUTPUT_EXTENSION");
  83. // Build list of dependencies.
  84. std::vector<std::string> depends;
  85. this->AppendLinkDepends(depends, linkLanguage);
  86. // Get the name of the device object to generate.
  87. std::string const targetOutputReal =
  88. this->GeneratorTarget->ObjectDirectory + "cmake_device_link" + objExt;
  89. this->DeviceLinkObject = targetOutputReal;
  90. this->NumberOfProgressActions++;
  91. if (!this->NoRuleMessages) {
  92. cmLocalUnixMakefileGenerator3::EchoProgress progress;
  93. this->MakeEchoProgress(progress);
  94. // Add the link message.
  95. std::string buildEcho =
  96. cmStrCat("Linking ", linkLanguage, " device code ",
  97. this->LocalGenerator->ConvertToOutputFormat(
  98. this->LocalGenerator->MaybeConvertToRelativePath(
  99. this->LocalGenerator->GetCurrentBinaryDirectory(),
  100. this->DeviceLinkObject),
  101. cmOutputConverter::SHELL));
  102. this->LocalGenerator->AppendEcho(
  103. commands, buildEcho, cmLocalUnixMakefileGenerator3::EchoLink, &progress);
  104. }
  105. // Build a list of compiler flags and linker flags.
  106. std::string flags;
  107. std::string linkFlags;
  108. // Add flags to create an executable.
  109. // Add symbol export flags if necessary.
  110. if (this->GeneratorTarget->IsExecutableWithExports()) {
  111. std::string export_flag_var =
  112. cmStrCat("CMAKE_EXE_EXPORTS_", linkLanguage, "_FLAG");
  113. this->LocalGenerator->AppendFlags(
  114. linkFlags, this->Makefile->GetSafeDefinition(export_flag_var));
  115. }
  116. this->LocalGenerator->AppendFlags(linkFlags,
  117. this->LocalGenerator->GetLinkLibsCMP0065(
  118. linkLanguage, *this->GeneratorTarget));
  119. // Add language feature flags.
  120. this->LocalGenerator->AddLanguageFlagsForLinking(
  121. flags, this->GeneratorTarget, linkLanguage, this->ConfigName);
  122. this->LocalGenerator->AddArchitectureFlags(flags, this->GeneratorTarget,
  123. linkLanguage, this->ConfigName);
  124. // Add target-specific linker flags.
  125. this->GetTargetLinkFlags(linkFlags, linkLanguage);
  126. // Construct a list of files associated with this executable that
  127. // may need to be cleaned.
  128. std::vector<std::string> exeCleanFiles;
  129. exeCleanFiles.push_back(this->LocalGenerator->MaybeConvertToRelativePath(
  130. this->LocalGenerator->GetCurrentBinaryDirectory(), targetOutputReal));
  131. // Determine whether a link script will be used.
  132. bool useLinkScript = this->GlobalGenerator->GetUseLinkScript();
  133. // Construct the main link rule.
  134. std::vector<std::string> real_link_commands;
  135. const std::string linkRuleVar = "CMAKE_CUDA_DEVICE_LINK_EXECUTABLE";
  136. const std::string linkRule = this->GetLinkRule(linkRuleVar);
  137. std::vector<std::string> commands1;
  138. cmExpandList(linkRule, real_link_commands);
  139. bool useResponseFileForObjects =
  140. this->CheckUseResponseFileForObjects(linkLanguage);
  141. bool const useResponseFileForLibs =
  142. this->CheckUseResponseFileForLibraries(linkLanguage);
  143. // Expand the rule variables.
  144. {
  145. bool useWatcomQuote =
  146. this->Makefile->IsOn(linkRuleVar + "_USE_WATCOM_QUOTE");
  147. // Set path conversion for link script shells.
  148. this->LocalGenerator->SetLinkScriptShell(useLinkScript);
  149. std::unique_ptr<cmLinkLineComputer> linkLineComputer(
  150. new cmLinkLineDeviceComputer(
  151. this->LocalGenerator,
  152. this->LocalGenerator->GetStateSnapshot().GetDirectory()));
  153. linkLineComputer->SetForResponse(useResponseFileForLibs);
  154. linkLineComputer->SetUseWatcomQuote(useWatcomQuote);
  155. linkLineComputer->SetRelink(relink);
  156. // Collect up flags to link in needed libraries.
  157. std::string linkLibs;
  158. this->CreateLinkLibs(linkLineComputer.get(), linkLibs,
  159. useResponseFileForLibs, depends);
  160. // Construct object file lists that may be needed to expand the
  161. // rule.
  162. std::string buildObjs;
  163. this->CreateObjectLists(useLinkScript, false, useResponseFileForObjects,
  164. buildObjs, depends, useWatcomQuote);
  165. cmRulePlaceholderExpander::RuleVariables vars;
  166. std::string objectDir = this->GeneratorTarget->GetSupportDirectory();
  167. objectDir = this->LocalGenerator->ConvertToOutputFormat(
  168. this->LocalGenerator->MaybeConvertToRelativePath(
  169. this->LocalGenerator->GetCurrentBinaryDirectory(), objectDir),
  170. cmOutputConverter::SHELL);
  171. cmOutputConverter::OutputFormat output = (useWatcomQuote)
  172. ? cmOutputConverter::WATCOMQUOTE
  173. : cmOutputConverter::SHELL;
  174. std::string target = this->LocalGenerator->ConvertToOutputFormat(
  175. this->LocalGenerator->MaybeConvertToRelativePath(
  176. this->LocalGenerator->GetCurrentBinaryDirectory(), targetOutputReal),
  177. output);
  178. std::string targetFullPathCompilePDB = this->ComputeTargetCompilePDB();
  179. std::string targetOutPathCompilePDB =
  180. this->LocalGenerator->ConvertToOutputFormat(targetFullPathCompilePDB,
  181. cmOutputConverter::SHELL);
  182. vars.Language = linkLanguage.c_str();
  183. vars.Objects = buildObjs.c_str();
  184. vars.ObjectDir = objectDir.c_str();
  185. vars.Target = target.c_str();
  186. vars.LinkLibraries = linkLibs.c_str();
  187. vars.Flags = flags.c_str();
  188. vars.LinkFlags = linkFlags.c_str();
  189. vars.TargetCompilePDB = targetOutPathCompilePDB.c_str();
  190. std::string launcher;
  191. const char* val = this->LocalGenerator->GetRuleLauncher(
  192. this->GeneratorTarget, "RULE_LAUNCH_LINK");
  193. if (val && *val) {
  194. launcher = cmStrCat(val, ' ');
  195. }
  196. std::unique_ptr<cmRulePlaceholderExpander> rulePlaceholderExpander(
  197. this->LocalGenerator->CreateRulePlaceholderExpander());
  198. // Expand placeholders in the commands.
  199. rulePlaceholderExpander->SetTargetImpLib(targetOutputReal);
  200. for (std::string& real_link_command : real_link_commands) {
  201. real_link_command = cmStrCat(launcher, real_link_command);
  202. rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator,
  203. real_link_command, vars);
  204. }
  205. // Restore path conversion to normal shells.
  206. this->LocalGenerator->SetLinkScriptShell(false);
  207. }
  208. // Optionally convert the build rule to use a script to avoid long
  209. // command lines in the make shell.
  210. if (useLinkScript) {
  211. // Use a link script.
  212. const char* name = (relink ? "drelink.txt" : "dlink.txt");
  213. this->CreateLinkScript(name, real_link_commands, commands1, depends);
  214. } else {
  215. // No link script. Just use the link rule directly.
  216. commands1 = real_link_commands;
  217. }
  218. this->LocalGenerator->CreateCDCommand(
  219. commands1, this->Makefile->GetCurrentBinaryDirectory(),
  220. this->LocalGenerator->GetBinaryDirectory());
  221. cmAppend(commands, commands1);
  222. commands1.clear();
  223. // Write the build rule.
  224. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, nullptr,
  225. targetOutputReal, depends, commands,
  226. false);
  227. // Write the main driver rule to build everything in this target.
  228. this->WriteTargetDriverRule(targetOutputReal, relink);
  229. // Clean all the possible executable names and symlinks.
  230. this->CleanFiles.insert(exeCleanFiles.begin(), exeCleanFiles.end());
  231. #else
  232. static_cast<void>(relink);
  233. #endif
  234. }
  235. void cmMakefileExecutableTargetGenerator::WriteExecutableRule(bool relink)
  236. {
  237. std::vector<std::string> commands;
  238. // Get the name of the executable to generate.
  239. cmGeneratorTarget::Names targetNames =
  240. this->GeneratorTarget->GetExecutableNames(this->ConfigName);
  241. // Construct the full path version of the names.
  242. std::string outpath = this->GeneratorTarget->GetDirectory(this->ConfigName);
  243. if (this->GeneratorTarget->IsAppBundleOnApple()) {
  244. this->OSXBundleGenerator->CreateAppBundle(targetNames.Output, outpath);
  245. }
  246. outpath += '/';
  247. std::string outpathImp;
  248. if (relink) {
  249. outpath = cmStrCat(this->Makefile->GetCurrentBinaryDirectory(),
  250. "/CMakeFiles/CMakeRelink.dir");
  251. cmSystemTools::MakeDirectory(outpath);
  252. outpath += '/';
  253. if (!targetNames.ImportLibrary.empty()) {
  254. outpathImp = outpath;
  255. }
  256. } else {
  257. cmSystemTools::MakeDirectory(outpath);
  258. if (!targetNames.ImportLibrary.empty()) {
  259. outpathImp = this->GeneratorTarget->GetDirectory(
  260. this->ConfigName, cmStateEnums::ImportLibraryArtifact);
  261. cmSystemTools::MakeDirectory(outpathImp);
  262. outpathImp += '/';
  263. }
  264. }
  265. std::string compilePdbOutputPath =
  266. this->GeneratorTarget->GetCompilePDBDirectory(this->ConfigName);
  267. cmSystemTools::MakeDirectory(compilePdbOutputPath);
  268. std::string pdbOutputPath =
  269. this->GeneratorTarget->GetPDBDirectory(this->ConfigName);
  270. cmSystemTools::MakeDirectory(pdbOutputPath);
  271. pdbOutputPath += '/';
  272. std::string targetFullPath = outpath + targetNames.Output;
  273. std::string targetFullPathReal = outpath + targetNames.Real;
  274. std::string targetFullPathPDB = pdbOutputPath + targetNames.PDB;
  275. std::string targetFullPathImport = outpathImp + targetNames.ImportLibrary;
  276. std::string targetOutPathPDB = this->LocalGenerator->ConvertToOutputFormat(
  277. targetFullPathPDB, cmOutputConverter::SHELL);
  278. // Convert to the output path to use in constructing commands.
  279. std::string targetOutPath = this->LocalGenerator->ConvertToOutputFormat(
  280. this->LocalGenerator->MaybeConvertToRelativePath(
  281. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPath),
  282. cmOutputConverter::SHELL);
  283. std::string targetOutPathReal = this->LocalGenerator->ConvertToOutputFormat(
  284. this->LocalGenerator->MaybeConvertToRelativePath(
  285. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPathReal),
  286. cmOutputConverter::SHELL);
  287. std::string targetOutPathImport =
  288. this->LocalGenerator->ConvertToOutputFormat(
  289. this->LocalGenerator->MaybeConvertToRelativePath(
  290. this->LocalGenerator->GetCurrentBinaryDirectory(),
  291. targetFullPathImport),
  292. cmOutputConverter::SHELL);
  293. // Get the language to use for linking this executable.
  294. std::string linkLanguage =
  295. this->GeneratorTarget->GetLinkerLanguage(this->ConfigName);
  296. // Make sure we have a link language.
  297. if (linkLanguage.empty()) {
  298. cmSystemTools::Error("Cannot determine link language for target \"" +
  299. this->GeneratorTarget->GetName() + "\".");
  300. return;
  301. }
  302. // Build list of dependencies.
  303. std::vector<std::string> depends;
  304. this->AppendLinkDepends(depends, linkLanguage);
  305. if (!this->DeviceLinkObject.empty()) {
  306. depends.push_back(this->DeviceLinkObject);
  307. }
  308. this->NumberOfProgressActions++;
  309. if (!this->NoRuleMessages) {
  310. cmLocalUnixMakefileGenerator3::EchoProgress progress;
  311. this->MakeEchoProgress(progress);
  312. // Add the link message.
  313. std::string buildEcho =
  314. cmStrCat("Linking ", linkLanguage, " executable ", targetOutPath);
  315. this->LocalGenerator->AppendEcho(
  316. commands, buildEcho, cmLocalUnixMakefileGenerator3::EchoLink, &progress);
  317. }
  318. // Build a list of compiler flags and linker flags.
  319. std::string flags;
  320. std::string linkFlags;
  321. // Add flags to create an executable.
  322. this->LocalGenerator->AddConfigVariableFlags(
  323. linkFlags, "CMAKE_EXE_LINKER_FLAGS", this->ConfigName);
  324. if (this->GeneratorTarget->GetPropertyAsBool("WIN32_EXECUTABLE")) {
  325. this->LocalGenerator->AppendFlags(
  326. linkFlags, this->Makefile->GetSafeDefinition("CMAKE_CREATE_WIN32_EXE"));
  327. } else {
  328. this->LocalGenerator->AppendFlags(
  329. linkFlags,
  330. this->Makefile->GetSafeDefinition("CMAKE_CREATE_CONSOLE_EXE"));
  331. }
  332. // Add symbol export flags if necessary.
  333. if (this->GeneratorTarget->IsExecutableWithExports()) {
  334. std::string export_flag_var =
  335. cmStrCat("CMAKE_EXE_EXPORTS_", linkLanguage, "_FLAG");
  336. this->LocalGenerator->AppendFlags(
  337. linkFlags, this->Makefile->GetSafeDefinition(export_flag_var));
  338. }
  339. this->LocalGenerator->AppendFlags(linkFlags,
  340. this->LocalGenerator->GetLinkLibsCMP0065(
  341. linkLanguage, *this->GeneratorTarget));
  342. if (this->GeneratorTarget->GetPropertyAsBool("LINK_WHAT_YOU_USE")) {
  343. this->LocalGenerator->AppendFlags(linkFlags, " -Wl,--no-as-needed");
  344. }
  345. // Add language feature flags.
  346. this->LocalGenerator->AddLanguageFlagsForLinking(
  347. flags, this->GeneratorTarget, linkLanguage, this->ConfigName);
  348. this->LocalGenerator->AddArchitectureFlags(flags, this->GeneratorTarget,
  349. linkLanguage, this->ConfigName);
  350. // Add target-specific linker flags.
  351. this->GetTargetLinkFlags(linkFlags, linkLanguage);
  352. {
  353. std::unique_ptr<cmLinkLineComputer> linkLineComputer(
  354. this->CreateLinkLineComputer(
  355. this->LocalGenerator,
  356. this->LocalGenerator->GetStateSnapshot().GetDirectory()));
  357. this->AddModuleDefinitionFlag(linkLineComputer.get(), linkFlags);
  358. }
  359. this->LocalGenerator->AppendIPOLinkerFlags(linkFlags, this->GeneratorTarget,
  360. this->ConfigName, linkLanguage);
  361. // Construct a list of files associated with this executable that
  362. // may need to be cleaned.
  363. std::vector<std::string> exeCleanFiles;
  364. exeCleanFiles.push_back(this->LocalGenerator->MaybeConvertToRelativePath(
  365. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPath));
  366. #ifdef _WIN32
  367. // There may be a manifest file for this target. Add it to the
  368. // clean set just in case.
  369. exeCleanFiles.push_back(this->LocalGenerator->MaybeConvertToRelativePath(
  370. this->LocalGenerator->GetCurrentBinaryDirectory(),
  371. targetFullPath + ".manifest"));
  372. #endif
  373. if (this->TargetNames.Real != this->TargetNames.Output) {
  374. exeCleanFiles.push_back(this->LocalGenerator->MaybeConvertToRelativePath(
  375. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPathReal));
  376. }
  377. if (!this->TargetNames.ImportLibrary.empty()) {
  378. exeCleanFiles.push_back(this->LocalGenerator->MaybeConvertToRelativePath(
  379. this->LocalGenerator->GetCurrentBinaryDirectory(),
  380. targetFullPathImport));
  381. std::string implib;
  382. if (this->GeneratorTarget->GetImplibGNUtoMS(
  383. this->ConfigName, targetFullPathImport, implib)) {
  384. exeCleanFiles.push_back(this->LocalGenerator->MaybeConvertToRelativePath(
  385. this->LocalGenerator->GetCurrentBinaryDirectory(), implib));
  386. }
  387. }
  388. // List the PDB for cleaning only when the whole target is
  389. // cleaned. We do not want to delete the .pdb file just before
  390. // linking the target.
  391. this->CleanFiles.insert(this->LocalGenerator->MaybeConvertToRelativePath(
  392. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPathPDB));
  393. // Add the pre-build and pre-link rules building but not when relinking.
  394. if (!relink) {
  395. this->LocalGenerator->AppendCustomCommands(
  396. commands, this->GeneratorTarget->GetPreBuildCommands(),
  397. this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
  398. this->LocalGenerator->AppendCustomCommands(
  399. commands, this->GeneratorTarget->GetPreLinkCommands(),
  400. this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
  401. }
  402. // Determine whether a link script will be used.
  403. bool useLinkScript = this->GlobalGenerator->GetUseLinkScript();
  404. // Construct the main link rule.
  405. std::vector<std::string> real_link_commands;
  406. std::string linkRuleVar = this->GeneratorTarget->GetCreateRuleVariable(
  407. linkLanguage, this->ConfigName);
  408. std::string linkRule = this->GetLinkRule(linkRuleVar);
  409. std::vector<std::string> commands1;
  410. cmExpandList(linkRule, real_link_commands);
  411. if (this->GeneratorTarget->IsExecutableWithExports()) {
  412. // If a separate rule for creating an import library is specified
  413. // add it now.
  414. std::string implibRuleVar =
  415. cmStrCat("CMAKE_", linkLanguage, "_CREATE_IMPORT_LIBRARY");
  416. if (const char* rule = this->Makefile->GetDefinition(implibRuleVar)) {
  417. cmExpandList(rule, real_link_commands);
  418. }
  419. }
  420. bool useResponseFileForObjects =
  421. this->CheckUseResponseFileForObjects(linkLanguage);
  422. bool const useResponseFileForLibs =
  423. this->CheckUseResponseFileForLibraries(linkLanguage);
  424. // Expand the rule variables.
  425. {
  426. bool useWatcomQuote =
  427. this->Makefile->IsOn(linkRuleVar + "_USE_WATCOM_QUOTE");
  428. // Set path conversion for link script shells.
  429. this->LocalGenerator->SetLinkScriptShell(useLinkScript);
  430. std::unique_ptr<cmLinkLineComputer> linkLineComputer(
  431. this->CreateLinkLineComputer(
  432. this->LocalGenerator,
  433. this->LocalGenerator->GetStateSnapshot().GetDirectory()));
  434. linkLineComputer->SetForResponse(useResponseFileForLibs);
  435. linkLineComputer->SetUseWatcomQuote(useWatcomQuote);
  436. linkLineComputer->SetRelink(relink);
  437. // Collect up flags to link in needed libraries.
  438. std::string linkLibs;
  439. this->CreateLinkLibs(linkLineComputer.get(), linkLibs,
  440. useResponseFileForLibs, depends);
  441. // Construct object file lists that may be needed to expand the
  442. // rule.
  443. std::string buildObjs;
  444. this->CreateObjectLists(useLinkScript, false, useResponseFileForObjects,
  445. buildObjs, depends, useWatcomQuote);
  446. if (!this->DeviceLinkObject.empty()) {
  447. buildObjs += " " +
  448. this->LocalGenerator->ConvertToOutputFormat(
  449. this->LocalGenerator->MaybeConvertToRelativePath(
  450. this->LocalGenerator->GetCurrentBinaryDirectory(),
  451. this->DeviceLinkObject),
  452. cmOutputConverter::SHELL);
  453. }
  454. // maybe create .def file from list of objects
  455. this->GenDefFile(real_link_commands);
  456. std::string manifests = this->GetManifests();
  457. cmRulePlaceholderExpander::RuleVariables vars;
  458. vars.CMTargetName = this->GeneratorTarget->GetName().c_str();
  459. vars.CMTargetType =
  460. cmState::GetTargetTypeName(this->GeneratorTarget->GetType());
  461. vars.Language = linkLanguage.c_str();
  462. vars.Objects = buildObjs.c_str();
  463. std::string objectDir = this->GeneratorTarget->GetSupportDirectory();
  464. objectDir = this->LocalGenerator->ConvertToOutputFormat(
  465. this->LocalGenerator->MaybeConvertToRelativePath(
  466. this->LocalGenerator->GetCurrentBinaryDirectory(), objectDir),
  467. cmOutputConverter::SHELL);
  468. vars.ObjectDir = objectDir.c_str();
  469. cmOutputConverter::OutputFormat output = (useWatcomQuote)
  470. ? cmOutputConverter::WATCOMQUOTE
  471. : cmOutputConverter::SHELL;
  472. std::string target = this->LocalGenerator->ConvertToOutputFormat(
  473. this->LocalGenerator->MaybeConvertToRelativePath(
  474. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPathReal),
  475. output);
  476. vars.Target = target.c_str();
  477. vars.TargetPDB = targetOutPathPDB.c_str();
  478. // Setup the target version.
  479. std::string targetVersionMajor;
  480. std::string targetVersionMinor;
  481. {
  482. std::ostringstream majorStream;
  483. std::ostringstream minorStream;
  484. int major;
  485. int minor;
  486. this->GeneratorTarget->GetTargetVersion(major, minor);
  487. majorStream << major;
  488. minorStream << minor;
  489. targetVersionMajor = majorStream.str();
  490. targetVersionMinor = minorStream.str();
  491. }
  492. vars.TargetVersionMajor = targetVersionMajor.c_str();
  493. vars.TargetVersionMinor = targetVersionMinor.c_str();
  494. vars.LinkLibraries = linkLibs.c_str();
  495. vars.Flags = flags.c_str();
  496. vars.LinkFlags = linkFlags.c_str();
  497. vars.Manifests = manifests.c_str();
  498. if (this->GeneratorTarget->GetPropertyAsBool("LINK_WHAT_YOU_USE")) {
  499. std::string cmakeCommand =
  500. cmStrCat(this->LocalGenerator->ConvertToOutputFormat(
  501. cmSystemTools::GetCMakeCommand(), cmLocalGenerator::SHELL),
  502. " -E __run_co_compile --lwyu=", targetOutPathReal);
  503. real_link_commands.push_back(std::move(cmakeCommand));
  504. }
  505. std::string launcher;
  506. const char* val = this->LocalGenerator->GetRuleLauncher(
  507. this->GeneratorTarget, "RULE_LAUNCH_LINK");
  508. if (val && *val) {
  509. launcher = cmStrCat(val, ' ');
  510. }
  511. std::unique_ptr<cmRulePlaceholderExpander> rulePlaceholderExpander(
  512. this->LocalGenerator->CreateRulePlaceholderExpander());
  513. // Expand placeholders in the commands.
  514. rulePlaceholderExpander->SetTargetImpLib(targetOutPathImport);
  515. for (std::string& real_link_command : real_link_commands) {
  516. real_link_command = cmStrCat(launcher, real_link_command);
  517. rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator,
  518. real_link_command, vars);
  519. }
  520. // Restore path conversion to normal shells.
  521. this->LocalGenerator->SetLinkScriptShell(false);
  522. }
  523. // Optionally convert the build rule to use a script to avoid long
  524. // command lines in the make shell.
  525. if (useLinkScript) {
  526. // Use a link script.
  527. const char* name = (relink ? "relink.txt" : "link.txt");
  528. this->CreateLinkScript(name, real_link_commands, commands1, depends);
  529. } else {
  530. // No link script. Just use the link rule directly.
  531. commands1 = real_link_commands;
  532. }
  533. this->LocalGenerator->CreateCDCommand(
  534. commands1, this->Makefile->GetCurrentBinaryDirectory(),
  535. this->LocalGenerator->GetBinaryDirectory());
  536. cmAppend(commands, commands1);
  537. commands1.clear();
  538. // Add a rule to create necessary symlinks for the library.
  539. if (targetOutPath != targetOutPathReal) {
  540. std::string symlink =
  541. cmStrCat("$(CMAKE_COMMAND) -E cmake_symlink_executable ",
  542. targetOutPathReal, ' ', targetOutPath);
  543. commands1.push_back(std::move(symlink));
  544. this->LocalGenerator->CreateCDCommand(
  545. commands1, this->Makefile->GetCurrentBinaryDirectory(),
  546. this->LocalGenerator->GetBinaryDirectory());
  547. cmAppend(commands, commands1);
  548. commands1.clear();
  549. }
  550. // Add the post-build rules when building but not when relinking.
  551. if (!relink) {
  552. this->LocalGenerator->AppendCustomCommands(
  553. commands, this->GeneratorTarget->GetPostBuildCommands(),
  554. this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
  555. }
  556. // Write the build rule.
  557. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, nullptr,
  558. targetFullPathReal, depends, commands,
  559. false);
  560. // The symlink name for the target should depend on the real target
  561. // so if the target version changes it rebuilds and recreates the
  562. // symlink.
  563. if (targetFullPath != targetFullPathReal) {
  564. depends.clear();
  565. commands.clear();
  566. depends.push_back(targetFullPathReal);
  567. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, nullptr,
  568. targetFullPath, depends, commands,
  569. false);
  570. }
  571. // Write the main driver rule to build everything in this target.
  572. this->WriteTargetDriverRule(targetFullPath, relink);
  573. // Clean all the possible executable names and symlinks.
  574. this->CleanFiles.insert(exeCleanFiles.begin(), exeCleanFiles.end());
  575. }