cmMakefileExecutableTargetGenerator.cxx 25 KB

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