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