cmMakefileExecutableTargetGenerator.cxx 25 KB

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