cmMakefileExecutableTargetGenerator.cxx 24 KB

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