cmMakefileExecutableTargetGenerator.cxx 25 KB

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