cmMakefileExecutableTargetGenerator.cxx 25 KB

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