cmMakefileExecutableTargetGenerator.cxx 25 KB

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