cmMakefileExecutableTargetGenerator.cxx 25 KB

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