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