cmMakefileExecutableTargetGenerator.cxx 25 KB

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