cmMakefileExecutableTargetGenerator.cxx 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  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 "cmList.h"
  17. #include "cmLocalGenerator.h"
  18. #include "cmLocalUnixMakefileGenerator3.h"
  19. #include "cmMakefile.h"
  20. #include "cmOSXBundleGenerator.h"
  21. #include "cmOutputConverter.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. #include "cmValue.h"
  30. cmMakefileExecutableTargetGenerator::cmMakefileExecutableTargetGenerator(
  31. cmGeneratorTarget* target)
  32. : cmMakefileTargetGenerator(target)
  33. {
  34. this->CustomCommandDriver = OnDepends;
  35. this->TargetNames =
  36. this->GeneratorTarget->GetExecutableNames(this->GetConfigName());
  37. this->OSXBundleGenerator = cm::make_unique<cmOSXBundleGenerator>(target);
  38. this->OSXBundleGenerator->SetMacContentFolders(&this->MacContentFolders);
  39. }
  40. cmMakefileExecutableTargetGenerator::~cmMakefileExecutableTargetGenerator() =
  41. default;
  42. void cmMakefileExecutableTargetGenerator::WriteRuleFiles()
  43. {
  44. // create the build.make file and directory, put in the common blocks
  45. this->CreateRuleFile();
  46. // write rules used to help build object files
  47. this->WriteCommonCodeRules();
  48. // write the per-target per-language flags
  49. this->WriteTargetLanguageFlags();
  50. // write in rules for object files and custom commands
  51. this->WriteTargetBuildRules();
  52. // write the device link rules
  53. this->WriteDeviceExecutableRule(false);
  54. // write the link rules
  55. this->WriteExecutableRule(false);
  56. if (this->GeneratorTarget->NeedRelinkBeforeInstall(this->GetConfigName())) {
  57. // Write rules to link an installable version of the target.
  58. this->WriteExecutableRule(true);
  59. }
  60. // Write clean target
  61. this->WriteTargetCleanRules();
  62. // Write the dependency generation rule. This must be done last so
  63. // that multiple output pair information is available.
  64. this->WriteTargetDependRules();
  65. // close the streams
  66. this->CloseFileStreams();
  67. }
  68. void cmMakefileExecutableTargetGenerator::WriteDeviceExecutableRule(
  69. bool relink)
  70. {
  71. #ifndef CMAKE_BOOTSTRAP
  72. const bool requiresDeviceLinking = requireDeviceLinking(
  73. *this->GeneratorTarget, *this->LocalGenerator, this->GetConfigName());
  74. if (!requiresDeviceLinking) {
  75. return;
  76. }
  77. std::vector<std::string> commands;
  78. // Get the name of the device object to generate.
  79. std::string const& objExt =
  80. this->Makefile->GetSafeDefinition("CMAKE_CUDA_OUTPUT_EXTENSION");
  81. std::string const targetOutput =
  82. this->GeneratorTarget->ObjectDirectory + "cmake_device_link" + objExt;
  83. this->DeviceLinkObject = targetOutput;
  84. this->NumberOfProgressActions++;
  85. if (!this->NoRuleMessages) {
  86. cmLocalUnixMakefileGenerator3::EchoProgress progress;
  87. this->MakeEchoProgress(progress);
  88. // Add the link message.
  89. std::string buildEcho = cmStrCat(
  90. "Linking CUDA device code ",
  91. this->LocalGenerator->ConvertToOutputFormat(
  92. this->LocalGenerator->MaybeRelativeToCurBinDir(this->DeviceLinkObject),
  93. cmOutputConverter::SHELL));
  94. this->LocalGenerator->AppendEcho(
  95. commands, buildEcho, cmLocalUnixMakefileGenerator3::EchoLink, &progress);
  96. }
  97. if (this->Makefile->GetSafeDefinition("CMAKE_CUDA_COMPILER_ID") == "Clang") {
  98. this->WriteDeviceLinkRule(commands, targetOutput);
  99. } else {
  100. this->WriteNvidiaDeviceExecutableRule(relink, commands, targetOutput);
  101. }
  102. // Write the main driver rule to build everything in this target.
  103. this->WriteTargetDriverRule(targetOutput, relink);
  104. #else
  105. static_cast<void>(relink);
  106. #endif
  107. }
  108. void cmMakefileExecutableTargetGenerator::WriteNvidiaDeviceExecutableRule(
  109. bool relink, std::vector<std::string>& commands,
  110. const std::string& targetOutput)
  111. {
  112. const std::string linkLanguage = "CUDA";
  113. // Build list of dependencies.
  114. std::vector<std::string> depends;
  115. this->AppendLinkDepends(depends, linkLanguage);
  116. // Add language feature flags.
  117. std::string langFlags;
  118. this->LocalGenerator->AddLanguageFlagsForLinking(
  119. langFlags, this->GeneratorTarget, linkLanguage, this->GetConfigName());
  120. // Construct a list of files associated with this executable that
  121. // may need to be cleaned.
  122. std::vector<std::string> exeCleanFiles;
  123. exeCleanFiles.push_back(
  124. this->LocalGenerator->MaybeRelativeToCurBinDir(targetOutput));
  125. // Determine whether a link script will be used.
  126. bool useLinkScript = this->GlobalGenerator->GetUseLinkScript();
  127. // Construct the main link rule.
  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. cmList real_link_commands(linkRule);
  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. linkLanguage, 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, linkLanguage,
  163. cmMakefileTargetGenerator::ResponseFlagFor::DeviceLink);
  164. cmRulePlaceholderExpander::RuleVariables vars;
  165. std::string objectDir = this->GeneratorTarget->GetSupportDirectory();
  166. objectDir = this->LocalGenerator->ConvertToOutputFormat(
  167. this->LocalGenerator->MaybeRelativeToCurBinDir(objectDir),
  168. cmOutputConverter::SHELL);
  169. std::string target = this->LocalGenerator->ConvertToOutputFormat(
  170. this->LocalGenerator->MaybeRelativeToCurBinDir(targetOutput),
  171. cmOutputConverter::SHELL);
  172. std::string targetFullPathCompilePDB =
  173. this->ComputeTargetCompilePDB(this->GetConfigName());
  174. std::string targetOutPathCompilePDB =
  175. this->LocalGenerator->ConvertToOutputFormat(targetFullPathCompilePDB,
  176. cmOutputConverter::SHELL);
  177. vars.Language = linkLanguage.c_str();
  178. vars.Objects = buildObjs.c_str();
  179. vars.ObjectDir = objectDir.c_str();
  180. vars.Target = target.c_str();
  181. vars.LinkLibraries = linkLibs.c_str();
  182. vars.LanguageCompileFlags = langFlags.c_str();
  183. vars.LinkFlags = linkFlags.c_str();
  184. vars.TargetCompilePDB = targetOutPathCompilePDB.c_str();
  185. std::string launcher;
  186. std::string val = this->LocalGenerator->GetRuleLauncher(
  187. this->GeneratorTarget, "RULE_LAUNCH_LINK",
  188. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE"));
  189. if (cmNonempty(val)) {
  190. launcher = cmStrCat(val, ' ');
  191. }
  192. std::unique_ptr<cmRulePlaceholderExpander> rulePlaceholderExpander(
  193. this->LocalGenerator->CreateRulePlaceholderExpander());
  194. // Expand placeholders in the commands.
  195. rulePlaceholderExpander->SetTargetImpLib(targetOutput);
  196. for (auto& real_link_command : real_link_commands) {
  197. real_link_command = cmStrCat(launcher, real_link_command);
  198. rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator,
  199. real_link_command, vars);
  200. }
  201. // Restore path conversion to normal shells.
  202. this->LocalGenerator->SetLinkScriptShell(false);
  203. }
  204. // Optionally convert the build rule to use a script to avoid long
  205. // command lines in the make shell.
  206. if (useLinkScript) {
  207. // Use a link script.
  208. const char* name = (relink ? "drelink.txt" : "dlink.txt");
  209. this->CreateLinkScript(name, real_link_commands, commands1, depends);
  210. } else {
  211. // No link script. Just use the link rule directly.
  212. commands1 = real_link_commands;
  213. }
  214. this->LocalGenerator->CreateCDCommand(
  215. commands1, this->Makefile->GetCurrentBinaryDirectory(),
  216. this->LocalGenerator->GetBinaryDirectory());
  217. cm::append(commands, commands1);
  218. commands1.clear();
  219. // Write the build rule.
  220. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, nullptr,
  221. targetOutput, depends, commands, false);
  222. // Clean all the possible executable names and symlinks.
  223. this->CleanFiles.insert(exeCleanFiles.begin(), exeCleanFiles.end());
  224. }
  225. void cmMakefileExecutableTargetGenerator::WriteExecutableRule(bool relink)
  226. {
  227. std::vector<std::string> commands;
  228. // Get the name of the executable to generate.
  229. cmGeneratorTarget::Names targetNames =
  230. this->GeneratorTarget->GetExecutableNames(this->GetConfigName());
  231. // Construct the full path version of the names.
  232. std::string outpath =
  233. this->GeneratorTarget->GetDirectory(this->GetConfigName());
  234. if (this->GeneratorTarget->IsAppBundleOnApple()) {
  235. this->OSXBundleGenerator->CreateAppBundle(targetNames.Output, outpath,
  236. this->GetConfigName());
  237. }
  238. outpath += '/';
  239. std::string outpathImp;
  240. if (relink) {
  241. outpath = cmStrCat(this->Makefile->GetCurrentBinaryDirectory(),
  242. "/CMakeFiles/CMakeRelink.dir");
  243. cmSystemTools::MakeDirectory(outpath);
  244. outpath += '/';
  245. if (!targetNames.ImportLibrary.empty()) {
  246. outpathImp = outpath;
  247. }
  248. } else {
  249. cmSystemTools::MakeDirectory(outpath);
  250. if (!targetNames.ImportLibrary.empty()) {
  251. outpathImp = this->GeneratorTarget->GetDirectory(
  252. this->GetConfigName(), cmStateEnums::ImportLibraryArtifact);
  253. cmSystemTools::MakeDirectory(outpathImp);
  254. outpathImp += '/';
  255. }
  256. }
  257. std::string compilePdbOutputPath =
  258. this->GeneratorTarget->GetCompilePDBDirectory(this->GetConfigName());
  259. cmSystemTools::MakeDirectory(compilePdbOutputPath);
  260. std::string pdbOutputPath =
  261. this->GeneratorTarget->GetPDBDirectory(this->GetConfigName());
  262. cmSystemTools::MakeDirectory(pdbOutputPath);
  263. pdbOutputPath += '/';
  264. std::string targetFullPath = outpath + targetNames.Output;
  265. std::string targetFullPathReal = outpath + targetNames.Real;
  266. std::string targetFullPathPDB = pdbOutputPath + targetNames.PDB;
  267. std::string targetFullPathImport = outpathImp + targetNames.ImportLibrary;
  268. std::string targetOutPathPDB = this->LocalGenerator->ConvertToOutputFormat(
  269. targetFullPathPDB, cmOutputConverter::SHELL);
  270. // Convert to the output path to use in constructing commands.
  271. std::string targetOutPath = this->LocalGenerator->ConvertToOutputFormat(
  272. this->LocalGenerator->MaybeRelativeToCurBinDir(targetFullPath),
  273. cmOutputConverter::SHELL);
  274. std::string targetOutPathReal = this->LocalGenerator->ConvertToOutputFormat(
  275. this->LocalGenerator->MaybeRelativeToCurBinDir(targetFullPathReal),
  276. cmOutputConverter::SHELL);
  277. std::string targetOutPathImport =
  278. this->LocalGenerator->ConvertToOutputFormat(
  279. this->LocalGenerator->MaybeRelativeToCurBinDir(targetFullPathImport),
  280. cmOutputConverter::SHELL);
  281. // Get the language to use for linking this executable.
  282. std::string linkLanguage =
  283. this->GeneratorTarget->GetLinkerLanguage(this->GetConfigName());
  284. // Make sure we have a link language.
  285. if (linkLanguage.empty()) {
  286. cmSystemTools::Error("Cannot determine link language for target \"" +
  287. this->GeneratorTarget->GetName() + "\".");
  288. return;
  289. }
  290. // Build list of dependencies.
  291. std::vector<std::string> depends;
  292. this->AppendLinkDepends(depends, linkLanguage);
  293. if (!this->DeviceLinkObject.empty()) {
  294. depends.push_back(this->DeviceLinkObject);
  295. }
  296. this->NumberOfProgressActions++;
  297. if (!this->NoRuleMessages) {
  298. cmLocalUnixMakefileGenerator3::EchoProgress progress;
  299. this->MakeEchoProgress(progress);
  300. // Add the link message.
  301. std::string buildEcho =
  302. cmStrCat("Linking ", linkLanguage, " executable ", targetOutPath);
  303. this->LocalGenerator->AppendEcho(
  304. commands, buildEcho, cmLocalUnixMakefileGenerator3::EchoLink, &progress);
  305. }
  306. // Build a list of compiler flags and linker flags.
  307. std::string flags;
  308. std::string linkFlags;
  309. // Add flags to create an executable.
  310. this->LocalGenerator->AddConfigVariableFlags(
  311. linkFlags, "CMAKE_EXE_LINKER_FLAGS", this->GetConfigName());
  312. if (this->GeneratorTarget->IsWin32Executable(
  313. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE"))) {
  314. this->LocalGenerator->AppendFlags(
  315. linkFlags,
  316. this->Makefile->GetSafeDefinition(
  317. cmStrCat("CMAKE_", linkLanguage, "_CREATE_WIN32_EXE")));
  318. } else {
  319. this->LocalGenerator->AppendFlags(
  320. linkFlags,
  321. this->Makefile->GetSafeDefinition(
  322. cmStrCat("CMAKE_", linkLanguage, "_CREATE_CONSOLE_EXE")));
  323. }
  324. // Add symbol export flags if necessary.
  325. if (this->GeneratorTarget->IsExecutableWithExports()) {
  326. std::string export_flag_var =
  327. cmStrCat("CMAKE_EXE_EXPORTS_", linkLanguage, "_FLAG");
  328. this->LocalGenerator->AppendFlags(
  329. linkFlags, this->Makefile->GetSafeDefinition(export_flag_var));
  330. }
  331. this->LocalGenerator->AppendFlags(linkFlags,
  332. this->LocalGenerator->GetLinkLibsCMP0065(
  333. linkLanguage, *this->GeneratorTarget));
  334. this->UseLWYU = this->LocalGenerator->AppendLWYUFlags(
  335. linkFlags, this->GeneratorTarget, linkLanguage);
  336. // Add language feature flags.
  337. this->LocalGenerator->AddLanguageFlagsForLinking(
  338. flags, this->GeneratorTarget, linkLanguage, this->GetConfigName());
  339. this->LocalGenerator->AddArchitectureFlags(
  340. flags, this->GeneratorTarget, linkLanguage, this->GetConfigName());
  341. // Add target-specific linker flags.
  342. this->GetTargetLinkFlags(linkFlags, linkLanguage);
  343. {
  344. std::unique_ptr<cmLinkLineComputer> linkLineComputer =
  345. this->CreateLinkLineComputer(
  346. this->LocalGenerator,
  347. this->LocalGenerator->GetStateSnapshot().GetDirectory());
  348. this->LocalGenerator->AppendModuleDefinitionFlag(
  349. linkFlags, this->GeneratorTarget, linkLineComputer.get(),
  350. this->GetConfigName());
  351. }
  352. this->LocalGenerator->AppendIPOLinkerFlags(
  353. linkFlags, this->GeneratorTarget, this->GetConfigName(), linkLanguage);
  354. // Construct a list of files associated with this executable that
  355. // may need to be cleaned.
  356. std::vector<std::string> exeCleanFiles;
  357. exeCleanFiles.push_back(
  358. this->LocalGenerator->MaybeRelativeToCurBinDir(targetFullPath));
  359. #ifdef _WIN32
  360. // There may be a manifest file for this target. Add it to the
  361. // clean set just in case.
  362. exeCleanFiles.push_back(this->LocalGenerator->MaybeRelativeToCurBinDir(
  363. targetFullPath + ".manifest"));
  364. #endif
  365. if (this->TargetNames.Real != this->TargetNames.Output) {
  366. exeCleanFiles.push_back(
  367. this->LocalGenerator->MaybeRelativeToCurBinDir(targetFullPathReal));
  368. }
  369. if (!this->TargetNames.ImportLibrary.empty()) {
  370. exeCleanFiles.push_back(
  371. this->LocalGenerator->MaybeRelativeToCurBinDir(targetFullPathImport));
  372. std::string implib;
  373. if (this->GeneratorTarget->GetImplibGNUtoMS(
  374. this->GetConfigName(), targetFullPathImport, implib)) {
  375. exeCleanFiles.push_back(
  376. this->LocalGenerator->MaybeRelativeToCurBinDir(implib));
  377. }
  378. }
  379. // List the PDB for cleaning only when the whole target is
  380. // cleaned. We do not want to delete the .pdb file just before
  381. // linking the target.
  382. this->CleanFiles.insert(
  383. this->LocalGenerator->MaybeRelativeToCurBinDir(targetFullPathPDB));
  384. // Add the pre-build and pre-link rules building but not when relinking.
  385. if (!relink) {
  386. this->LocalGenerator->AppendCustomCommands(
  387. commands, this->GeneratorTarget->GetPreBuildCommands(),
  388. this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
  389. this->LocalGenerator->AppendCustomCommands(
  390. commands, this->GeneratorTarget->GetPreLinkCommands(),
  391. this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
  392. }
  393. // Determine whether a link script will be used.
  394. bool useLinkScript = this->GlobalGenerator->GetUseLinkScript();
  395. // Construct the main link rule.
  396. std::string linkRuleVar = this->GeneratorTarget->GetCreateRuleVariable(
  397. linkLanguage, this->GetConfigName());
  398. std::string linkRule = this->GetLinkRule(linkRuleVar);
  399. std::vector<std::string> commands1;
  400. cmList real_link_commands(linkRule);
  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. real_link_commands.append(this->Makefile->GetDefinition(implibRuleVar));
  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, linkLanguage);
  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, linkLanguage);
  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 (auto& 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. }