cmMakefileExecutableTargetGenerator.cxx 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  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. this->WriteTargetLinkDependRules();
  61. // Write clean target
  62. this->WriteTargetCleanRules();
  63. // Write the dependency generation rule. This must be done last so
  64. // that multiple output pair information is available.
  65. this->WriteTargetDependRules();
  66. // close the streams
  67. this->CloseFileStreams();
  68. }
  69. void cmMakefileExecutableTargetGenerator::WriteDeviceExecutableRule(
  70. bool relink)
  71. {
  72. #ifndef CMAKE_BOOTSTRAP
  73. const bool requiresDeviceLinking = requireDeviceLinking(
  74. *this->GeneratorTarget, *this->LocalGenerator, this->GetConfigName());
  75. if (!requiresDeviceLinking) {
  76. return;
  77. }
  78. std::vector<std::string> commands;
  79. // Get the name of the device object to generate.
  80. std::string const& objExt =
  81. this->Makefile->GetSafeDefinition("CMAKE_CUDA_OUTPUT_EXTENSION");
  82. std::string const targetOutput =
  83. this->GeneratorTarget->ObjectDirectory + "cmake_device_link" + objExt;
  84. this->DeviceLinkObject = targetOutput;
  85. this->NumberOfProgressActions++;
  86. if (!this->NoRuleMessages) {
  87. cmLocalUnixMakefileGenerator3::EchoProgress progress;
  88. this->MakeEchoProgress(progress);
  89. // Add the link message.
  90. std::string buildEcho = cmStrCat(
  91. "Linking CUDA device code ",
  92. this->LocalGenerator->ConvertToOutputFormat(
  93. this->LocalGenerator->MaybeRelativeToCurBinDir(this->DeviceLinkObject),
  94. cmOutputConverter::SHELL));
  95. this->LocalGenerator->AppendEcho(
  96. commands, buildEcho, cmLocalUnixMakefileGenerator3::EchoLink, &progress);
  97. }
  98. if (this->Makefile->GetSafeDefinition("CMAKE_CUDA_COMPILER_ID") == "Clang") {
  99. this->WriteDeviceLinkRule(commands, targetOutput);
  100. } else {
  101. this->WriteNvidiaDeviceExecutableRule(relink, commands, targetOutput);
  102. }
  103. // Write the main driver rule to build everything in this target.
  104. this->WriteTargetDriverRule(targetOutput, relink);
  105. #else
  106. static_cast<void>(relink);
  107. #endif
  108. }
  109. void cmMakefileExecutableTargetGenerator::WriteNvidiaDeviceExecutableRule(
  110. bool relink, std::vector<std::string>& commands,
  111. const std::string& targetOutput)
  112. {
  113. const std::string linkLanguage = "CUDA";
  114. // Build list of dependencies.
  115. std::vector<std::string> depends;
  116. this->AppendLinkDepends(depends, linkLanguage);
  117. // Add language feature flags.
  118. std::string langFlags;
  119. this->LocalGenerator->AddLanguageFlagsForLinking(
  120. langFlags, this->GeneratorTarget, linkLanguage, this->GetConfigName());
  121. // Construct a list of files associated with this executable that
  122. // may need to be cleaned.
  123. std::vector<std::string> exeCleanFiles;
  124. exeCleanFiles.push_back(
  125. this->LocalGenerator->MaybeRelativeToCurBinDir(targetOutput));
  126. // Determine whether a link script will be used.
  127. bool useLinkScript = this->GlobalGenerator->GetUseLinkScript();
  128. // Construct the main link rule.
  129. const std::string linkRuleVar = "CMAKE_CUDA_DEVICE_LINK_EXECUTABLE";
  130. const std::string linkRule = this->GetLinkRule(linkRuleVar);
  131. std::vector<std::string> commands1;
  132. cmList real_link_commands(linkRule);
  133. bool useResponseFileForObjects =
  134. this->CheckUseResponseFileForObjects(linkLanguage);
  135. bool const useResponseFileForLibs =
  136. this->CheckUseResponseFileForLibraries(linkLanguage);
  137. // Expand the rule variables.
  138. {
  139. // Set path conversion for link script shells.
  140. this->LocalGenerator->SetLinkScriptShell(useLinkScript);
  141. std::unique_ptr<cmLinkLineDeviceComputer> linkLineComputer(
  142. new cmLinkLineDeviceComputer(
  143. this->LocalGenerator,
  144. this->LocalGenerator->GetStateSnapshot().GetDirectory()));
  145. linkLineComputer->SetForResponse(useResponseFileForLibs);
  146. linkLineComputer->SetRelink(relink);
  147. // Create set of linking flags.
  148. std::string linkFlags;
  149. std::string ignored_;
  150. this->LocalGenerator->GetDeviceLinkFlags(
  151. *linkLineComputer, this->GetConfigName(), ignored_, linkFlags, ignored_,
  152. ignored_, this->GeneratorTarget);
  153. // Collect up flags to link in needed libraries.
  154. std::string linkLibs;
  155. this->CreateLinkLibs(
  156. linkLineComputer.get(), linkLibs, useResponseFileForLibs, depends,
  157. linkLanguage, cmMakefileTargetGenerator::ResponseFlagFor::DeviceLink);
  158. // Construct object file lists that may be needed to expand the
  159. // rule.
  160. std::string buildObjs;
  161. this->CreateObjectLists(
  162. useLinkScript, false, useResponseFileForObjects, buildObjs, depends,
  163. false, linkLanguage,
  164. cmMakefileTargetGenerator::ResponseFlagFor::DeviceLink);
  165. cmRulePlaceholderExpander::RuleVariables vars;
  166. std::string objectDir = this->GeneratorTarget->GetSupportDirectory();
  167. objectDir = this->LocalGenerator->ConvertToOutputFormat(
  168. this->LocalGenerator->MaybeRelativeToCurBinDir(objectDir),
  169. cmOutputConverter::SHELL);
  170. std::string target = this->LocalGenerator->ConvertToOutputFormat(
  171. this->LocalGenerator->MaybeRelativeToCurBinDir(targetOutput),
  172. cmOutputConverter::SHELL);
  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.Objects = buildObjs.c_str();
  180. vars.ObjectDir = objectDir.c_str();
  181. vars.Target = target.c_str();
  182. vars.LinkLibraries = linkLibs.c_str();
  183. vars.LanguageCompileFlags = langFlags.c_str();
  184. vars.LinkFlags = linkFlags.c_str();
  185. vars.TargetCompilePDB = targetOutPathCompilePDB.c_str();
  186. std::string launcher;
  187. std::string val = this->LocalGenerator->GetRuleLauncher(
  188. this->GeneratorTarget, "RULE_LAUNCH_LINK",
  189. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE"));
  190. if (cmNonempty(val)) {
  191. launcher = cmStrCat(val, ' ');
  192. }
  193. auto rulePlaceholderExpander =
  194. this->LocalGenerator->CreateRulePlaceholderExpander();
  195. // Expand placeholders in the commands.
  196. rulePlaceholderExpander->SetTargetImpLib(targetOutput);
  197. for (auto& 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. this->UseLWYU = this->LocalGenerator->AppendLWYUFlags(
  336. linkFlags, this->GeneratorTarget, linkLanguage);
  337. // Add language feature flags.
  338. this->LocalGenerator->AddLanguageFlagsForLinking(
  339. flags, this->GeneratorTarget, linkLanguage, this->GetConfigName());
  340. this->LocalGenerator->AddArchitectureFlags(
  341. flags, this->GeneratorTarget, linkLanguage, this->GetConfigName());
  342. // Add target-specific linker flags.
  343. this->GetTargetLinkFlags(linkFlags, linkLanguage);
  344. {
  345. std::unique_ptr<cmLinkLineComputer> linkLineComputer =
  346. this->CreateLinkLineComputer(
  347. this->LocalGenerator,
  348. this->LocalGenerator->GetStateSnapshot().GetDirectory());
  349. this->LocalGenerator->AppendModuleDefinitionFlag(
  350. linkFlags, this->GeneratorTarget, linkLineComputer.get(),
  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::string linkRuleVar = this->GeneratorTarget->GetCreateRuleVariable(
  398. linkLanguage, this->GetConfigName());
  399. std::string linkRule = this->GetLinkRule(linkRuleVar);
  400. std::vector<std::string> commands1;
  401. cmList real_link_commands(linkRule);
  402. if (this->GeneratorTarget->IsExecutableWithExports()) {
  403. // If a separate rule for creating an import library is specified
  404. // add it now.
  405. std::string implibRuleVar =
  406. cmStrCat("CMAKE_", linkLanguage, "_CREATE_IMPORT_LIBRARY");
  407. real_link_commands.append(this->Makefile->GetDefinition(implibRuleVar));
  408. }
  409. bool useResponseFileForObjects =
  410. this->CheckUseResponseFileForObjects(linkLanguage);
  411. bool const useResponseFileForLibs =
  412. this->CheckUseResponseFileForLibraries(linkLanguage);
  413. // Expand the rule variables.
  414. {
  415. bool useWatcomQuote =
  416. this->Makefile->IsOn(linkRuleVar + "_USE_WATCOM_QUOTE");
  417. // Set path conversion for link script shells.
  418. this->LocalGenerator->SetLinkScriptShell(useLinkScript);
  419. std::unique_ptr<cmLinkLineComputer> linkLineComputer =
  420. this->CreateLinkLineComputer(
  421. this->LocalGenerator,
  422. this->LocalGenerator->GetStateSnapshot().GetDirectory());
  423. linkLineComputer->SetForResponse(useResponseFileForLibs);
  424. linkLineComputer->SetUseWatcomQuote(useWatcomQuote);
  425. linkLineComputer->SetRelink(relink);
  426. // Collect up flags to link in needed libraries.
  427. std::string linkLibs;
  428. this->CreateLinkLibs(linkLineComputer.get(), linkLibs,
  429. useResponseFileForLibs, depends, linkLanguage);
  430. // Construct object file lists that may be needed to expand the
  431. // rule.
  432. std::string buildObjs;
  433. this->CreateObjectLists(useLinkScript, false, useResponseFileForObjects,
  434. buildObjs, depends, useWatcomQuote, linkLanguage);
  435. if (!this->DeviceLinkObject.empty()) {
  436. buildObjs += " " +
  437. this->LocalGenerator->ConvertToOutputFormat(
  438. this->LocalGenerator->MaybeRelativeToCurBinDir(
  439. this->DeviceLinkObject),
  440. cmOutputConverter::SHELL);
  441. }
  442. // maybe create .def file from list of objects
  443. this->GenDefFile(real_link_commands);
  444. std::string manifests = this->GetManifests(this->GetConfigName());
  445. std::string const& aixExports = this->GetAIXExports(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.AIXExports = aixExports.c_str();
  452. vars.Objects = buildObjs.c_str();
  453. std::string objectDir = this->GeneratorTarget->GetSupportDirectory();
  454. objectDir = this->LocalGenerator->ConvertToOutputFormat(
  455. this->LocalGenerator->MaybeRelativeToCurBinDir(objectDir),
  456. cmOutputConverter::SHELL);
  457. vars.ObjectDir = objectDir.c_str();
  458. std::string target = this->LocalGenerator->ConvertToOutputFormat(
  459. this->LocalGenerator->MaybeRelativeToCurBinDir(targetFullPathReal),
  460. cmOutputConverter::SHELL, useWatcomQuote);
  461. vars.Target = target.c_str();
  462. vars.TargetPDB = targetOutPathPDB.c_str();
  463. // Setup the target version.
  464. std::string targetVersionMajor;
  465. std::string targetVersionMinor;
  466. {
  467. std::ostringstream majorStream;
  468. std::ostringstream minorStream;
  469. int major;
  470. int minor;
  471. this->GeneratorTarget->GetTargetVersion(major, minor);
  472. majorStream << major;
  473. minorStream << minor;
  474. targetVersionMajor = majorStream.str();
  475. targetVersionMinor = minorStream.str();
  476. }
  477. vars.TargetVersionMajor = targetVersionMajor.c_str();
  478. vars.TargetVersionMinor = targetVersionMinor.c_str();
  479. vars.LinkLibraries = linkLibs.c_str();
  480. vars.Flags = flags.c_str();
  481. vars.LinkFlags = linkFlags.c_str();
  482. vars.Manifests = manifests.c_str();
  483. std::string linkerLauncher =
  484. this->GetLinkerLauncher(this->GetConfigName());
  485. if (cmNonempty(linkerLauncher)) {
  486. vars.Launcher = linkerLauncher.c_str();
  487. }
  488. if (this->UseLWYU) {
  489. cmValue lwyuCheck =
  490. this->Makefile->GetDefinition("CMAKE_LINK_WHAT_YOU_USE_CHECK");
  491. if (lwyuCheck) {
  492. std::string cmakeCommand = cmStrCat(
  493. this->LocalGenerator->ConvertToOutputFormat(
  494. cmSystemTools::GetCMakeCommand(), cmLocalGenerator::SHELL),
  495. " -E __run_co_compile --lwyu=");
  496. cmakeCommand += this->LocalGenerator->EscapeForShell(*lwyuCheck);
  497. cmakeCommand += cmStrCat(" --source=", targetOutPathReal);
  498. real_link_commands.push_back(std::move(cmakeCommand));
  499. }
  500. }
  501. std::string launcher;
  502. std::string val = this->LocalGenerator->GetRuleLauncher(
  503. this->GeneratorTarget, "RULE_LAUNCH_LINK",
  504. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE"));
  505. if (cmNonempty(val)) {
  506. launcher = cmStrCat(val, ' ');
  507. }
  508. auto rulePlaceholderExpander =
  509. this->LocalGenerator->CreateRulePlaceholderExpander();
  510. // Expand placeholders in the commands.
  511. rulePlaceholderExpander->SetTargetImpLib(targetOutPathImport);
  512. for (auto& real_link_command : real_link_commands) {
  513. real_link_command = cmStrCat(launcher, real_link_command);
  514. rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator,
  515. real_link_command, vars);
  516. }
  517. // Restore path conversion to normal shells.
  518. this->LocalGenerator->SetLinkScriptShell(false);
  519. }
  520. // Optionally convert the build rule to use a script to avoid long
  521. // command lines in the make shell.
  522. if (useLinkScript) {
  523. // Use a link script.
  524. const char* name = (relink ? "relink.txt" : "link.txt");
  525. this->CreateLinkScript(name, real_link_commands, commands1, depends);
  526. } else {
  527. // No link script. Just use the link rule directly.
  528. commands1 = real_link_commands;
  529. }
  530. this->LocalGenerator->CreateCDCommand(
  531. commands1, this->Makefile->GetCurrentBinaryDirectory(),
  532. this->LocalGenerator->GetBinaryDirectory());
  533. cm::append(commands, commands1);
  534. commands1.clear();
  535. // Add a rule to create necessary symlinks for the library.
  536. if (targetOutPath != targetOutPathReal) {
  537. std::string symlink =
  538. cmStrCat("$(CMAKE_COMMAND) -E cmake_symlink_executable ",
  539. targetOutPathReal, ' ', targetOutPath);
  540. commands1.push_back(std::move(symlink));
  541. this->LocalGenerator->CreateCDCommand(
  542. commands1, this->Makefile->GetCurrentBinaryDirectory(),
  543. this->LocalGenerator->GetBinaryDirectory());
  544. cm::append(commands, commands1);
  545. commands1.clear();
  546. }
  547. // Add the post-build rules when building but not when relinking.
  548. if (!relink) {
  549. this->LocalGenerator->AppendCustomCommands(
  550. commands, this->GeneratorTarget->GetPostBuildCommands(),
  551. this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
  552. }
  553. // Write the build rule.
  554. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, nullptr,
  555. targetFullPathReal, depends, commands,
  556. false);
  557. // The symlink name for the target should depend on the real target
  558. // so if the target version changes it rebuilds and recreates the
  559. // symlink.
  560. if (targetFullPath != targetFullPathReal) {
  561. depends.clear();
  562. commands.clear();
  563. depends.push_back(targetFullPathReal);
  564. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, nullptr,
  565. targetFullPath, depends, commands,
  566. false);
  567. }
  568. // Write the main driver rule to build everything in this target.
  569. this->WriteTargetDriverRule(targetFullPath, relink);
  570. // Clean all the possible executable names and symlinks.
  571. this->CleanFiles.insert(exeCleanFiles.begin(), exeCleanFiles.end());
  572. }