cmMakefileExecutableTargetGenerator.cxx 25 KB

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