cmMakefileExecutableTargetGenerator.cxx 26 KB

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