cmMakefileExecutableTargetGenerator.cxx 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst 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 "cmGeneratorOptions.h"
  13. #include "cmGeneratorTarget.h"
  14. #include "cmGlobalUnixMakefileGenerator3.h"
  15. #include "cmLinkLineComputer.h"
  16. #include "cmLinkLineDeviceComputer.h"
  17. #include "cmList.h"
  18. #include "cmLocalGenerator.h"
  19. #include "cmLocalUnixMakefileGenerator3.h"
  20. #include "cmMakefile.h"
  21. #include "cmOSXBundleGenerator.h"
  22. #include "cmOutputConverter.h"
  23. #include "cmRulePlaceholderExpander.h"
  24. #include "cmState.h"
  25. #include "cmStateDirectory.h"
  26. #include "cmStateSnapshot.h"
  27. #include "cmStateTypes.h"
  28. #include "cmStringAlgorithms.h"
  29. #include "cmSystemTools.h"
  30. #include "cmValue.h"
  31. cmMakefileExecutableTargetGenerator::cmMakefileExecutableTargetGenerator(
  32. cmGeneratorTarget* target)
  33. : cmMakefileTargetGenerator(target)
  34. {
  35. this->CustomCommandDriver = OnDepends;
  36. this->TargetNames =
  37. this->GeneratorTarget->GetExecutableNames(this->GetConfigName());
  38. this->OSXBundleGenerator = cm::make_unique<cmOSXBundleGenerator>(target);
  39. this->OSXBundleGenerator->SetMacContentFolders(&this->MacContentFolders);
  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->GetConfigName())) {
  58. // Write rules to link an installable version of the target.
  59. this->WriteExecutableRule(true);
  60. }
  61. this->WriteTargetLinkDependRules();
  62. // Write clean target
  63. this->WriteTargetCleanRules();
  64. // Write the dependency generation rule. This must be done last so
  65. // that multiple output pair information is available.
  66. this->WriteTargetDependRules();
  67. // close the streams
  68. this->CloseFileStreams();
  69. }
  70. void cmMakefileExecutableTargetGenerator::WriteDeviceExecutableRule(
  71. bool relink)
  72. {
  73. #ifndef CMAKE_BOOTSTRAP
  74. bool const requiresDeviceLinking = requireDeviceLinking(
  75. *this->GeneratorTarget, *this->LocalGenerator, this->GetConfigName());
  76. if (!requiresDeviceLinking) {
  77. return;
  78. }
  79. std::vector<std::string> commands;
  80. // Get the name of the device object to generate.
  81. std::string const& objExt =
  82. this->Makefile->GetSafeDefinition("CMAKE_CUDA_OUTPUT_EXTENSION");
  83. std::string const targetOutput =
  84. this->GeneratorTarget->ObjectDirectory + "cmake_device_link" + objExt;
  85. this->DeviceLinkObject = targetOutput;
  86. this->NumberOfProgressActions++;
  87. if (!this->NoRuleMessages) {
  88. cmLocalUnixMakefileGenerator3::EchoProgress progress;
  89. this->MakeEchoProgress(progress);
  90. // Add the link message.
  91. std::string buildEcho = cmStrCat(
  92. "Linking CUDA device code ",
  93. this->LocalGenerator->ConvertToOutputFormat(
  94. this->LocalGenerator->MaybeRelativeToCurBinDir(this->DeviceLinkObject),
  95. cmOutputConverter::SHELL));
  96. this->LocalGenerator->AppendEcho(
  97. commands, buildEcho, cmLocalUnixMakefileGenerator3::EchoLink, &progress);
  98. }
  99. if (this->Makefile->GetSafeDefinition("CMAKE_CUDA_COMPILER_ID") == "Clang") {
  100. this->WriteDeviceLinkRule(commands, targetOutput);
  101. } else {
  102. this->WriteNvidiaDeviceExecutableRule(relink, commands, targetOutput);
  103. }
  104. // Write the main driver rule to build everything in this target.
  105. this->WriteTargetDriverRule(targetOutput, relink);
  106. #else
  107. static_cast<void>(relink);
  108. #endif
  109. }
  110. void cmMakefileExecutableTargetGenerator::WriteNvidiaDeviceExecutableRule(
  111. bool relink, std::vector<std::string>& commands,
  112. std::string const& targetOutput)
  113. {
  114. std::string const linkLanguage = "CUDA";
  115. // Build list of dependencies.
  116. std::vector<std::string> depends;
  117. this->AppendLinkDepends(depends, linkLanguage);
  118. // Add language feature flags.
  119. std::string langFlags;
  120. this->LocalGenerator->AddLanguageFlagsForLinking(
  121. langFlags, this->GeneratorTarget, linkLanguage, this->GetConfigName());
  122. // Construct a list of files associated with this executable that
  123. // may need to be cleaned.
  124. std::vector<std::string> exeCleanFiles;
  125. exeCleanFiles.push_back(
  126. this->LocalGenerator->MaybeRelativeToCurBinDir(targetOutput));
  127. // Determine whether a link script will be used.
  128. bool useLinkScript = this->GlobalGenerator->GetUseLinkScript();
  129. // Construct the main link rule.
  130. std::string const linkRuleVar = "CMAKE_CUDA_DEVICE_LINK_EXECUTABLE";
  131. std::string const linkRule = this->GetLinkRule(linkRuleVar);
  132. std::vector<std::string> commands1;
  133. cmList real_link_commands(linkRule);
  134. bool useResponseFileForObjects =
  135. this->CheckUseResponseFileForObjects(linkLanguage);
  136. bool const useResponseFileForLibs =
  137. this->CheckUseResponseFileForLibraries(linkLanguage);
  138. // Expand the rule variables.
  139. {
  140. // Set path conversion for link script shells.
  141. this->LocalGenerator->SetLinkScriptShell(useLinkScript);
  142. std::unique_ptr<cmLinkLineDeviceComputer> linkLineComputer(
  143. new cmLinkLineDeviceComputer(
  144. this->LocalGenerator,
  145. this->LocalGenerator->GetStateSnapshot().GetDirectory()));
  146. linkLineComputer->SetForResponse(useResponseFileForLibs);
  147. linkLineComputer->SetRelink(relink);
  148. // Create set of linking flags.
  149. std::string linkFlags;
  150. std::string ignored_;
  151. this->LocalGenerator->GetDeviceLinkFlags(
  152. *linkLineComputer, this->GetConfigName(), ignored_, linkFlags, ignored_,
  153. ignored_, this->GeneratorTarget);
  154. // Collect up flags to link in needed libraries.
  155. std::string linkLibs;
  156. this->CreateLinkLibs(
  157. linkLineComputer.get(), linkLibs, useResponseFileForLibs, depends,
  158. linkLanguage, cmMakefileTargetGenerator::ResponseFlagFor::DeviceLink);
  159. // Construct object file lists that may be needed to expand the
  160. // rule.
  161. std::string buildObjs;
  162. this->CreateObjectLists(
  163. useLinkScript, false, useResponseFileForObjects, buildObjs, depends,
  164. false, linkLanguage,
  165. cmMakefileTargetGenerator::ResponseFlagFor::DeviceLink);
  166. cmRulePlaceholderExpander::RuleVariables vars;
  167. std::string objectDir = this->GeneratorTarget->GetSupportDirectory();
  168. objectDir = this->LocalGenerator->ConvertToOutputFormat(
  169. this->LocalGenerator->MaybeRelativeToCurBinDir(objectDir),
  170. cmOutputConverter::SHELL);
  171. std::string targetSupportDir =
  172. this->GeneratorTarget->GetCMFSupportDirectory();
  173. targetSupportDir = this->LocalGenerator->ConvertToOutputFormat(
  174. this->LocalGenerator->MaybeRelativeToTopBinDir(targetSupportDir),
  175. cmOutputConverter::SHELL);
  176. std::string target = this->LocalGenerator->ConvertToOutputFormat(
  177. this->LocalGenerator->MaybeRelativeToCurBinDir(targetOutput),
  178. cmOutputConverter::SHELL);
  179. std::string targetFullPathCompilePDB =
  180. this->ComputeTargetCompilePDB(this->GetConfigName());
  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.TargetSupportDir = targetSupportDir.c_str();
  189. vars.LinkLibraries = linkLibs.c_str();
  190. vars.LanguageCompileFlags = langFlags.c_str();
  191. vars.LinkFlags = linkFlags.c_str();
  192. vars.TargetCompilePDB = targetOutPathCompilePDB.c_str();
  193. std::string launcher;
  194. std::string val = this->LocalGenerator->GetRuleLauncher(
  195. this->GeneratorTarget, "RULE_LAUNCH_LINK",
  196. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE"));
  197. if (cmNonempty(val)) {
  198. launcher = cmStrCat(val, ' ');
  199. }
  200. auto rulePlaceholderExpander =
  201. this->LocalGenerator->CreateRulePlaceholderExpander(cmBuildStep::Link);
  202. // Expand placeholders in the commands.
  203. rulePlaceholderExpander->SetTargetImpLib(targetOutput);
  204. for (auto& real_link_command : real_link_commands) {
  205. real_link_command = cmStrCat(launcher, real_link_command);
  206. rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator,
  207. real_link_command, vars);
  208. }
  209. // Restore path conversion to normal shells.
  210. this->LocalGenerator->SetLinkScriptShell(false);
  211. }
  212. // Optionally convert the build rule to use a script to avoid long
  213. // command lines in the make shell.
  214. if (useLinkScript) {
  215. // Use a link script.
  216. char const* name = (relink ? "drelink.txt" : "dlink.txt");
  217. this->CreateLinkScript(name, real_link_commands, commands1, depends);
  218. } else {
  219. // No link script. Just use the link rule directly.
  220. commands1 = real_link_commands;
  221. }
  222. this->LocalGenerator->CreateCDCommand(
  223. commands1, this->Makefile->GetCurrentBinaryDirectory(),
  224. this->LocalGenerator->GetBinaryDirectory());
  225. cm::append(commands, commands1);
  226. commands1.clear();
  227. // Write the build rule.
  228. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, nullptr,
  229. targetOutput, depends, commands, false);
  230. // Clean all the possible executable names and symlinks.
  231. this->CleanFiles.insert(exeCleanFiles.begin(), exeCleanFiles.end());
  232. }
  233. void cmMakefileExecutableTargetGenerator::WriteExecutableRule(bool relink)
  234. {
  235. std::vector<std::string> commands;
  236. // Get the name of the executable to generate.
  237. cmGeneratorTarget::Names targetNames =
  238. this->GeneratorTarget->GetExecutableNames(this->GetConfigName());
  239. // Construct the full path version of the names.
  240. std::string outpath =
  241. this->GeneratorTarget->GetDirectory(this->GetConfigName());
  242. if (this->GeneratorTarget->IsAppBundleOnApple()) {
  243. this->OSXBundleGenerator->CreateAppBundle(targetNames.Output, outpath,
  244. this->GetConfigName());
  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->GetConfigName(), cmStateEnums::ImportLibraryArtifact);
  261. cmSystemTools::MakeDirectory(outpathImp);
  262. outpathImp += '/';
  263. }
  264. }
  265. std::string compilePdbOutputPath =
  266. this->GeneratorTarget->GetCompilePDBDirectory(this->GetConfigName());
  267. cmSystemTools::MakeDirectory(compilePdbOutputPath);
  268. std::string pdbOutputPath =
  269. this->GeneratorTarget->GetPDBDirectory(this->GetConfigName());
  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->MaybeRelativeToCurBinDir(targetFullPath),
  281. cmOutputConverter::SHELL);
  282. std::string targetOutPathReal = this->LocalGenerator->ConvertToOutputFormat(
  283. this->LocalGenerator->MaybeRelativeToCurBinDir(targetFullPathReal),
  284. cmOutputConverter::SHELL);
  285. std::string targetOutPathImport =
  286. this->LocalGenerator->ConvertToOutputFormat(
  287. this->LocalGenerator->MaybeRelativeToCurBinDir(targetFullPathImport),
  288. cmOutputConverter::SHELL);
  289. // Get the language to use for linking this executable.
  290. std::string linkLanguage =
  291. this->GeneratorTarget->GetLinkerLanguage(this->GetConfigName());
  292. // Make sure we have a link language.
  293. if (linkLanguage.empty()) {
  294. cmSystemTools::Error("Cannot determine link language for target \"" +
  295. this->GeneratorTarget->GetName() + "\".");
  296. return;
  297. }
  298. auto linker = this->GeneratorTarget->GetLinkerTool(this->GetConfigName());
  299. // Build list of dependencies.
  300. std::vector<std::string> depends;
  301. this->AppendLinkDepends(depends, linkLanguage);
  302. if (!this->DeviceLinkObject.empty()) {
  303. depends.push_back(this->DeviceLinkObject);
  304. }
  305. this->NumberOfProgressActions++;
  306. if (!this->NoRuleMessages) {
  307. cmLocalUnixMakefileGenerator3::EchoProgress progress;
  308. this->MakeEchoProgress(progress);
  309. // Add the link message.
  310. std::string buildEcho =
  311. cmStrCat("Linking ", linkLanguage, " executable ", targetOutPath);
  312. this->LocalGenerator->AppendEcho(
  313. commands, buildEcho, cmLocalUnixMakefileGenerator3::EchoLink, &progress);
  314. }
  315. // Build a list of compiler flags and linker flags.
  316. std::string flags;
  317. std::string linkFlags;
  318. // Add flags to create an executable.
  319. this->LocalGenerator->AppendTargetCreationLinkFlags(
  320. linkFlags, this->GeneratorTarget, linkLanguage);
  321. this->LocalGenerator->AddConfigVariableFlags(
  322. linkFlags, "CMAKE_EXE_LINKER_FLAGS", this->GeneratorTarget,
  323. cmBuildStep::Link, linkLanguage, this->GetConfigName());
  324. {
  325. auto exeType =
  326. cmStrCat("CMAKE_", linkLanguage, "_CREATE_",
  327. (this->GeneratorTarget->IsWin32Executable(
  328. this->Makefile->GetDefinition("CMAKE_BUILD_TYPE"))
  329. ? "WIN32"
  330. : "CONSOLE"),
  331. "_EXE");
  332. this->LocalGenerator->AppendFlags(
  333. linkFlags, this->Makefile->GetDefinition(exeType), exeType,
  334. this->GeneratorTarget, cmBuildStep::Link, linkLanguage);
  335. }
  336. // Add symbol export flags if necessary.
  337. if (this->GeneratorTarget->IsExecutableWithExports()) {
  338. std::string export_flag_var =
  339. cmStrCat("CMAKE_EXE_EXPORTS_", linkLanguage, "_FLAG");
  340. this->LocalGenerator->AppendFlags(
  341. linkFlags, this->Makefile->GetSafeDefinition(export_flag_var));
  342. }
  343. this->LocalGenerator->AppendFlags(linkFlags,
  344. this->LocalGenerator->GetExeExportFlags(
  345. linkLanguage, *this->GeneratorTarget));
  346. this->UseLWYU = this->LocalGenerator->AppendLWYUFlags(
  347. linkFlags, this->GeneratorTarget, linkLanguage);
  348. // Add language feature flags.
  349. this->LocalGenerator->AddLanguageFlagsForLinking(
  350. flags, this->GeneratorTarget, linkLanguage, this->GetConfigName());
  351. this->LocalGenerator->AddArchitectureFlags(
  352. flags, this->GeneratorTarget, linkLanguage, this->GetConfigName());
  353. // Add target-specific linker flags.
  354. this->GetTargetLinkFlags(linkFlags, linkLanguage);
  355. {
  356. std::unique_ptr<cmLinkLineComputer> linkLineComputer =
  357. this->CreateLinkLineComputer(
  358. this->LocalGenerator,
  359. this->LocalGenerator->GetStateSnapshot().GetDirectory());
  360. this->LocalGenerator->AppendModuleDefinitionFlag(
  361. linkFlags, this->GeneratorTarget, linkLineComputer.get(),
  362. this->GetConfigName(), linkLanguage);
  363. }
  364. this->LocalGenerator->AppendIPOLinkerFlags(
  365. linkFlags, this->GeneratorTarget, this->GetConfigName(), linkLanguage);
  366. // Construct a list of files associated with this executable that
  367. // may need to be cleaned.
  368. std::vector<std::string> exeCleanFiles;
  369. exeCleanFiles.push_back(
  370. this->LocalGenerator->MaybeRelativeToCurBinDir(targetFullPath));
  371. #ifdef _WIN32
  372. // There may be a manifest file for this target. Add it to the
  373. // clean set just in case.
  374. exeCleanFiles.push_back(this->LocalGenerator->MaybeRelativeToCurBinDir(
  375. targetFullPath + ".manifest"));
  376. #endif
  377. if (this->TargetNames.Real != this->TargetNames.Output) {
  378. exeCleanFiles.push_back(
  379. this->LocalGenerator->MaybeRelativeToCurBinDir(targetFullPathReal));
  380. }
  381. if (!this->TargetNames.ImportLibrary.empty()) {
  382. exeCleanFiles.push_back(
  383. this->LocalGenerator->MaybeRelativeToCurBinDir(targetFullPathImport));
  384. std::string implib;
  385. if (this->GeneratorTarget->GetImplibGNUtoMS(
  386. this->GetConfigName(), targetFullPathImport, implib)) {
  387. exeCleanFiles.push_back(
  388. this->LocalGenerator->MaybeRelativeToCurBinDir(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(
  395. this->LocalGenerator->MaybeRelativeToCurBinDir(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::string linkRuleVar = this->GeneratorTarget->GetCreateRuleVariable(
  409. linkLanguage, this->GetConfigName());
  410. std::string linkRule = this->GetLinkRule(linkRuleVar);
  411. std::vector<std::string> commands1;
  412. cmList real_link_commands(linkRule);
  413. if (this->GeneratorTarget->IsExecutableWithExports()) {
  414. // If a separate rule for creating an import library is specified
  415. // add it now.
  416. std::string implibRuleVar =
  417. cmStrCat("CMAKE_", linkLanguage, "_CREATE_IMPORT_LIBRARY");
  418. real_link_commands.append(this->Makefile->GetDefinition(implibRuleVar));
  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, linkLanguage);
  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, linkLanguage);
  446. if (!this->DeviceLinkObject.empty()) {
  447. buildObjs += " " +
  448. this->LocalGenerator->ConvertToOutputFormat(
  449. this->LocalGenerator->MaybeRelativeToCurBinDir(
  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(this->GetConfigName());
  456. std::string const& aixExports = this->GetAIXExports(this->GetConfigName());
  457. cmRulePlaceholderExpander::RuleVariables vars;
  458. vars.CMTargetName = this->GeneratorTarget->GetName().c_str();
  459. vars.CMTargetType =
  460. cmState::GetTargetTypeName(this->GeneratorTarget->GetType()).c_str();
  461. vars.Language = linkLanguage.c_str();
  462. vars.Linker = linker.c_str();
  463. vars.AIXExports = aixExports.c_str();
  464. vars.Objects = buildObjs.c_str();
  465. std::string objectDir = this->GeneratorTarget->GetSupportDirectory();
  466. objectDir = this->LocalGenerator->ConvertToOutputFormat(
  467. this->LocalGenerator->MaybeRelativeToCurBinDir(objectDir),
  468. cmOutputConverter::SHELL);
  469. vars.ObjectDir = objectDir.c_str();
  470. std::string targetSupportDir =
  471. this->GeneratorTarget->GetCMFSupportDirectory();
  472. targetSupportDir = this->LocalGenerator->ConvertToOutputFormat(
  473. this->LocalGenerator->MaybeRelativeToTopBinDir(targetSupportDir),
  474. cmOutputConverter::SHELL);
  475. vars.TargetSupportDir = targetSupportDir.c_str();
  476. std::string target = this->LocalGenerator->ConvertToOutputFormat(
  477. this->LocalGenerator->MaybeRelativeToCurBinDir(targetFullPathReal),
  478. cmOutputConverter::SHELL, useWatcomQuote);
  479. vars.Target = target.c_str();
  480. vars.TargetPDB = targetOutPathPDB.c_str();
  481. vars.Config = this->GetConfigName().c_str();
  482. // Setup the target version.
  483. std::string targetVersionMajor;
  484. std::string targetVersionMinor;
  485. {
  486. std::ostringstream majorStream;
  487. std::ostringstream minorStream;
  488. int major;
  489. int minor;
  490. this->GeneratorTarget->GetTargetVersion(major, minor);
  491. majorStream << major;
  492. minorStream << minor;
  493. targetVersionMajor = majorStream.str();
  494. targetVersionMinor = minorStream.str();
  495. }
  496. vars.TargetVersionMajor = targetVersionMajor.c_str();
  497. vars.TargetVersionMinor = targetVersionMinor.c_str();
  498. vars.LinkLibraries = linkLibs.c_str();
  499. vars.Flags = flags.c_str();
  500. vars.LinkFlags = linkFlags.c_str();
  501. vars.Manifests = manifests.c_str();
  502. std::string linkerLauncher =
  503. this->GetLinkerLauncher(this->GetConfigName());
  504. if (cmNonempty(linkerLauncher)) {
  505. vars.Launcher = linkerLauncher.c_str();
  506. }
  507. if (this->UseLWYU) {
  508. cmValue lwyuCheck =
  509. this->Makefile->GetDefinition("CMAKE_LINK_WHAT_YOU_USE_CHECK");
  510. if (lwyuCheck) {
  511. std::string cmakeCommand = cmStrCat(
  512. this->LocalGenerator->ConvertToOutputFormat(
  513. cmSystemTools::GetCMakeCommand(), cmLocalGenerator::SHELL),
  514. " -E __run_co_compile --lwyu=");
  515. cmakeCommand += this->LocalGenerator->EscapeForShell(*lwyuCheck);
  516. cmakeCommand += cmStrCat(" --source=", targetOutPathReal);
  517. real_link_commands.push_back(std::move(cmakeCommand));
  518. }
  519. }
  520. std::string launcher;
  521. std::string val = this->LocalGenerator->GetRuleLauncher(
  522. this->GeneratorTarget, "RULE_LAUNCH_LINK",
  523. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE"));
  524. if (cmNonempty(val)) {
  525. launcher = cmStrCat(val, ' ');
  526. }
  527. auto rulePlaceholderExpander =
  528. this->LocalGenerator->CreateRulePlaceholderExpander(cmBuildStep::Link);
  529. // Expand placeholders in the commands.
  530. rulePlaceholderExpander->SetTargetImpLib(targetOutPathImport);
  531. for (auto& real_link_command : real_link_commands) {
  532. real_link_command = cmStrCat(launcher, real_link_command);
  533. rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator,
  534. real_link_command, vars);
  535. }
  536. // Restore path conversion to normal shells.
  537. this->LocalGenerator->SetLinkScriptShell(false);
  538. }
  539. // Optionally convert the build rule to use a script to avoid long
  540. // command lines in the make shell.
  541. if (useLinkScript) {
  542. // Use a link script.
  543. char const* name = (relink ? "relink.txt" : "link.txt");
  544. this->CreateLinkScript(name, real_link_commands, commands1, depends);
  545. } else {
  546. // No link script. Just use the link rule directly.
  547. commands1 = real_link_commands;
  548. }
  549. this->LocalGenerator->CreateCDCommand(
  550. commands1, this->Makefile->GetCurrentBinaryDirectory(),
  551. this->LocalGenerator->GetBinaryDirectory());
  552. cm::append(commands, commands1);
  553. commands1.clear();
  554. // Add a rule to create necessary symlinks for the library.
  555. if (targetOutPath != targetOutPathReal) {
  556. std::string symlink =
  557. cmStrCat("$(CMAKE_COMMAND) -E cmake_symlink_executable ",
  558. targetOutPathReal, ' ', targetOutPath);
  559. commands1.push_back(std::move(symlink));
  560. this->LocalGenerator->CreateCDCommand(
  561. commands1, this->Makefile->GetCurrentBinaryDirectory(),
  562. this->LocalGenerator->GetBinaryDirectory());
  563. cm::append(commands, commands1);
  564. commands1.clear();
  565. }
  566. // Add the post-build rules when building but not when relinking.
  567. if (!relink) {
  568. this->LocalGenerator->AppendCustomCommands(
  569. commands, this->GeneratorTarget->GetPostBuildCommands(),
  570. this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
  571. }
  572. // Write the build rule.
  573. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, nullptr,
  574. targetFullPathReal, depends, commands,
  575. false);
  576. // The symlink name for the target should depend on the real target
  577. // so if the target version changes it rebuilds and recreates the
  578. // symlink.
  579. if (targetFullPath != targetFullPathReal) {
  580. depends.clear();
  581. commands.clear();
  582. depends.push_back(targetFullPathReal);
  583. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, nullptr,
  584. targetFullPath, depends, commands,
  585. false);
  586. }
  587. // Write the main driver rule to build everything in this target.
  588. this->WriteTargetDriverRule(targetFullPath, relink);
  589. // Clean all the possible executable names and symlinks.
  590. this->CleanFiles.insert(exeCleanFiles.begin(), exeCleanFiles.end());
  591. }