cmMakefileExecutableTargetGenerator.cxx 24 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 "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. if (this->GeneratorTarget->GetPropertyAsBool("LINK_WHAT_YOU_USE")) {
  336. this->LocalGenerator->AppendFlags(linkFlags, " -Wl,--no-as-needed");
  337. }
  338. // Add language feature flags.
  339. this->LocalGenerator->AddLanguageFlagsForLinking(
  340. flags, this->GeneratorTarget, linkLanguage, this->GetConfigName());
  341. this->LocalGenerator->AddArchitectureFlags(
  342. flags, this->GeneratorTarget, linkLanguage, this->GetConfigName());
  343. // Add target-specific linker flags.
  344. this->GetTargetLinkFlags(linkFlags, linkLanguage);
  345. {
  346. std::unique_ptr<cmLinkLineComputer> linkLineComputer =
  347. this->CreateLinkLineComputer(
  348. this->LocalGenerator,
  349. this->LocalGenerator->GetStateSnapshot().GetDirectory());
  350. this->AddModuleDefinitionFlag(linkLineComputer.get(), linkFlags,
  351. this->GetConfigName());
  352. }
  353. this->LocalGenerator->AppendIPOLinkerFlags(
  354. linkFlags, this->GeneratorTarget, this->GetConfigName(), linkLanguage);
  355. // Construct a list of files associated with this executable that
  356. // may need to be cleaned.
  357. std::vector<std::string> exeCleanFiles;
  358. exeCleanFiles.push_back(
  359. this->LocalGenerator->MaybeRelativeToCurBinDir(targetFullPath));
  360. #ifdef _WIN32
  361. // There may be a manifest file for this target. Add it to the
  362. // clean set just in case.
  363. exeCleanFiles.push_back(this->LocalGenerator->MaybeRelativeToCurBinDir(
  364. targetFullPath + ".manifest"));
  365. #endif
  366. if (this->TargetNames.Real != this->TargetNames.Output) {
  367. exeCleanFiles.push_back(
  368. this->LocalGenerator->MaybeRelativeToCurBinDir(targetFullPathReal));
  369. }
  370. if (!this->TargetNames.ImportLibrary.empty()) {
  371. exeCleanFiles.push_back(
  372. this->LocalGenerator->MaybeRelativeToCurBinDir(targetFullPathImport));
  373. std::string implib;
  374. if (this->GeneratorTarget->GetImplibGNUtoMS(
  375. this->GetConfigName(), targetFullPathImport, implib)) {
  376. exeCleanFiles.push_back(
  377. this->LocalGenerator->MaybeRelativeToCurBinDir(implib));
  378. }
  379. }
  380. // List the PDB for cleaning only when the whole target is
  381. // cleaned. We do not want to delete the .pdb file just before
  382. // linking the target.
  383. this->CleanFiles.insert(
  384. this->LocalGenerator->MaybeRelativeToCurBinDir(targetFullPathPDB));
  385. // Add the pre-build and pre-link rules building but not when relinking.
  386. if (!relink) {
  387. this->LocalGenerator->AppendCustomCommands(
  388. commands, this->GeneratorTarget->GetPreBuildCommands(),
  389. this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
  390. this->LocalGenerator->AppendCustomCommands(
  391. commands, this->GeneratorTarget->GetPreLinkCommands(),
  392. this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
  393. }
  394. // Determine whether a link script will be used.
  395. bool useLinkScript = this->GlobalGenerator->GetUseLinkScript();
  396. // Construct the main link rule.
  397. std::vector<std::string> real_link_commands;
  398. std::string linkRuleVar = this->GeneratorTarget->GetCreateRuleVariable(
  399. linkLanguage, this->GetConfigName());
  400. std::string linkRule = this->GetLinkRule(linkRuleVar);
  401. std::vector<std::string> commands1;
  402. cmExpandList(linkRule, real_link_commands);
  403. if (this->GeneratorTarget->IsExecutableWithExports()) {
  404. // If a separate rule for creating an import library is specified
  405. // add it now.
  406. std::string implibRuleVar =
  407. cmStrCat("CMAKE_", linkLanguage, "_CREATE_IMPORT_LIBRARY");
  408. this->Makefile->GetDefExpandList(implibRuleVar, real_link_commands);
  409. }
  410. bool useResponseFileForObjects =
  411. this->CheckUseResponseFileForObjects(linkLanguage);
  412. bool const useResponseFileForLibs =
  413. this->CheckUseResponseFileForLibraries(linkLanguage);
  414. // Expand the rule variables.
  415. {
  416. bool useWatcomQuote =
  417. this->Makefile->IsOn(linkRuleVar + "_USE_WATCOM_QUOTE");
  418. // Set path conversion for link script shells.
  419. this->LocalGenerator->SetLinkScriptShell(useLinkScript);
  420. std::unique_ptr<cmLinkLineComputer> linkLineComputer =
  421. this->CreateLinkLineComputer(
  422. this->LocalGenerator,
  423. this->LocalGenerator->GetStateSnapshot().GetDirectory());
  424. linkLineComputer->SetForResponse(useResponseFileForLibs);
  425. linkLineComputer->SetUseWatcomQuote(useWatcomQuote);
  426. linkLineComputer->SetRelink(relink);
  427. // Collect up flags to link in needed libraries.
  428. std::string linkLibs;
  429. this->CreateLinkLibs(linkLineComputer.get(), linkLibs,
  430. useResponseFileForLibs, depends);
  431. // Construct object file lists that may be needed to expand the
  432. // rule.
  433. std::string buildObjs;
  434. this->CreateObjectLists(useLinkScript, false, useResponseFileForObjects,
  435. buildObjs, depends, useWatcomQuote);
  436. if (!this->DeviceLinkObject.empty()) {
  437. buildObjs += " " +
  438. this->LocalGenerator->ConvertToOutputFormat(
  439. this->LocalGenerator->MaybeRelativeToCurBinDir(
  440. this->DeviceLinkObject),
  441. cmOutputConverter::SHELL);
  442. }
  443. // maybe create .def file from list of objects
  444. this->GenDefFile(real_link_commands);
  445. std::string manifests = this->GetManifests(this->GetConfigName());
  446. cmRulePlaceholderExpander::RuleVariables vars;
  447. vars.CMTargetName = this->GeneratorTarget->GetName().c_str();
  448. vars.CMTargetType =
  449. cmState::GetTargetTypeName(this->GeneratorTarget->GetType()).c_str();
  450. vars.Language = linkLanguage.c_str();
  451. vars.Objects = buildObjs.c_str();
  452. std::string objectDir = this->GeneratorTarget->GetSupportDirectory();
  453. objectDir = this->LocalGenerator->ConvertToOutputFormat(
  454. this->LocalGenerator->MaybeRelativeToCurBinDir(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->MaybeRelativeToCurBinDir(targetFullPathReal),
  462. output);
  463. vars.Target = target.c_str();
  464. vars.TargetPDB = targetOutPathPDB.c_str();
  465. // Setup the target version.
  466. std::string targetVersionMajor;
  467. std::string targetVersionMinor;
  468. {
  469. std::ostringstream majorStream;
  470. std::ostringstream minorStream;
  471. int major;
  472. int minor;
  473. this->GeneratorTarget->GetTargetVersion(major, minor);
  474. majorStream << major;
  475. minorStream << minor;
  476. targetVersionMajor = majorStream.str();
  477. targetVersionMinor = minorStream.str();
  478. }
  479. vars.TargetVersionMajor = targetVersionMajor.c_str();
  480. vars.TargetVersionMinor = targetVersionMinor.c_str();
  481. vars.LinkLibraries = linkLibs.c_str();
  482. vars.Flags = flags.c_str();
  483. vars.LinkFlags = linkFlags.c_str();
  484. vars.Manifests = manifests.c_str();
  485. if (this->GeneratorTarget->GetPropertyAsBool("LINK_WHAT_YOU_USE")) {
  486. std::string cmakeCommand =
  487. cmStrCat(this->LocalGenerator->ConvertToOutputFormat(
  488. cmSystemTools::GetCMakeCommand(), cmLocalGenerator::SHELL),
  489. " -E __run_co_compile --lwyu=", targetOutPathReal);
  490. real_link_commands.push_back(std::move(cmakeCommand));
  491. }
  492. std::string launcher;
  493. cmProp val = this->LocalGenerator->GetRuleLauncher(this->GeneratorTarget,
  494. "RULE_LAUNCH_LINK");
  495. if (cmNonempty(val)) {
  496. launcher = cmStrCat(*val, ' ');
  497. }
  498. std::unique_ptr<cmRulePlaceholderExpander> rulePlaceholderExpander(
  499. this->LocalGenerator->CreateRulePlaceholderExpander());
  500. // Expand placeholders in the commands.
  501. rulePlaceholderExpander->SetTargetImpLib(targetOutPathImport);
  502. for (std::string& real_link_command : real_link_commands) {
  503. real_link_command = cmStrCat(launcher, real_link_command);
  504. rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator,
  505. real_link_command, vars);
  506. }
  507. // Restore path conversion to normal shells.
  508. this->LocalGenerator->SetLinkScriptShell(false);
  509. }
  510. // Optionally convert the build rule to use a script to avoid long
  511. // command lines in the make shell.
  512. if (useLinkScript) {
  513. // Use a link script.
  514. const char* name = (relink ? "relink.txt" : "link.txt");
  515. this->CreateLinkScript(name, real_link_commands, commands1, depends);
  516. } else {
  517. // No link script. Just use the link rule directly.
  518. commands1 = real_link_commands;
  519. }
  520. this->LocalGenerator->CreateCDCommand(
  521. commands1, this->Makefile->GetCurrentBinaryDirectory(),
  522. this->LocalGenerator->GetBinaryDirectory());
  523. cm::append(commands, commands1);
  524. commands1.clear();
  525. // Add a rule to create necessary symlinks for the library.
  526. if (targetOutPath != targetOutPathReal) {
  527. std::string symlink =
  528. cmStrCat("$(CMAKE_COMMAND) -E cmake_symlink_executable ",
  529. targetOutPathReal, ' ', targetOutPath);
  530. commands1.push_back(std::move(symlink));
  531. this->LocalGenerator->CreateCDCommand(
  532. commands1, this->Makefile->GetCurrentBinaryDirectory(),
  533. this->LocalGenerator->GetBinaryDirectory());
  534. cm::append(commands, commands1);
  535. commands1.clear();
  536. }
  537. // Add the post-build rules when building but not when relinking.
  538. if (!relink) {
  539. this->LocalGenerator->AppendCustomCommands(
  540. commands, this->GeneratorTarget->GetPostBuildCommands(),
  541. this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
  542. }
  543. // Write the build rule.
  544. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, nullptr,
  545. targetFullPathReal, depends, commands,
  546. false);
  547. // The symlink name for the target should depend on the real target
  548. // so if the target version changes it rebuilds and recreates the
  549. // symlink.
  550. if (targetFullPath != targetFullPathReal) {
  551. depends.clear();
  552. commands.clear();
  553. depends.push_back(targetFullPathReal);
  554. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, nullptr,
  555. targetFullPath, depends, commands,
  556. false);
  557. }
  558. // Write the main driver rule to build everything in this target.
  559. this->WriteTargetDriverRule(targetFullPath, relink);
  560. // Clean all the possible executable names and symlinks.
  561. this->CleanFiles.insert(exeCleanFiles.begin(), exeCleanFiles.end());
  562. }