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 <memory>
  5. #include <set>
  6. #include <sstream>
  7. #include <string>
  8. #include <utility>
  9. #include <vector>
  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 "cmSystemTools.h"
  27. cmMakefileExecutableTargetGenerator::cmMakefileExecutableTargetGenerator(
  28. cmGeneratorTarget* target)
  29. : cmMakefileTargetGenerator(target)
  30. {
  31. this->CustomCommandDriver = OnDepends;
  32. this->TargetNames =
  33. this->GeneratorTarget->GetExecutableNames(this->ConfigName);
  34. this->OSXBundleGenerator =
  35. cm::make_unique<cmOSXBundleGenerator>(target, this->ConfigName);
  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->ConfigName)) {
  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. #ifdef CMAKE_BUILD_WITH_CMAKE
  70. const bool requiresDeviceLinking = requireDeviceLinking(
  71. *this->GeneratorTarget, *this->LocalGenerator, this->ConfigName);
  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 = "Linking ";
  93. buildEcho += linkLanguage;
  94. buildEcho += " device code ";
  95. buildEcho += this->LocalGenerator->ConvertToOutputFormat(
  96. this->LocalGenerator->MaybeConvertToRelativePath(
  97. this->LocalGenerator->GetCurrentBinaryDirectory(),
  98. this->DeviceLinkObject),
  99. cmOutputConverter::SHELL);
  100. this->LocalGenerator->AppendEcho(
  101. commands, buildEcho, cmLocalUnixMakefileGenerator3::EchoLink, &progress);
  102. }
  103. // Build a list of compiler flags and linker flags.
  104. std::string flags;
  105. std::string linkFlags;
  106. // Add flags to create an executable.
  107. // Add symbol export flags if necessary.
  108. if (this->GeneratorTarget->IsExecutableWithExports()) {
  109. std::string export_flag_var = "CMAKE_EXE_EXPORTS_";
  110. export_flag_var += linkLanguage;
  111. export_flag_var += "_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. cmSystemTools::ExpandListArgument(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 = val;
  194. launcher += " ";
  195. }
  196. std::unique_ptr<cmRulePlaceholderExpander> rulePlaceholderExpander(
  197. this->LocalGenerator->CreateRulePlaceholderExpander());
  198. // Expand placeholders in the commands.
  199. rulePlaceholderExpander->SetTargetImpLib(targetOutputReal);
  200. for (std::string& real_link_command : real_link_commands) {
  201. real_link_command = launcher + real_link_command;
  202. rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator,
  203. real_link_command, vars);
  204. }
  205. // Restore path conversion to normal shells.
  206. this->LocalGenerator->SetLinkScriptShell(false);
  207. }
  208. // Optionally convert the build rule to use a script to avoid long
  209. // command lines in the make shell.
  210. if (useLinkScript) {
  211. // Use a link script.
  212. const char* name = (relink ? "drelink.txt" : "dlink.txt");
  213. this->CreateLinkScript(name, real_link_commands, commands1, depends);
  214. } else {
  215. // No link script. Just use the link rule directly.
  216. commands1 = real_link_commands;
  217. }
  218. this->LocalGenerator->CreateCDCommand(
  219. commands1, this->Makefile->GetCurrentBinaryDirectory(),
  220. this->LocalGenerator->GetBinaryDirectory());
  221. cmAppend(commands, commands1);
  222. commands1.clear();
  223. // Write the build rule.
  224. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, nullptr,
  225. targetOutputReal, depends, commands,
  226. false);
  227. // Write the main driver rule to build everything in this target.
  228. this->WriteTargetDriverRule(targetOutputReal, relink);
  229. // Clean all the possible executable names and symlinks.
  230. this->CleanFiles.insert(exeCleanFiles.begin(), exeCleanFiles.end());
  231. #else
  232. static_cast<void>(relink);
  233. #endif
  234. }
  235. void cmMakefileExecutableTargetGenerator::WriteExecutableRule(bool relink)
  236. {
  237. std::vector<std::string> commands;
  238. // Get the name of the executable to generate.
  239. cmGeneratorTarget::Names targetNames =
  240. this->GeneratorTarget->GetExecutableNames(this->ConfigName);
  241. // Construct the full path version of the names.
  242. std::string outpath = this->GeneratorTarget->GetDirectory(this->ConfigName);
  243. if (this->GeneratorTarget->IsAppBundleOnApple()) {
  244. this->OSXBundleGenerator->CreateAppBundle(targetNames.Output, outpath);
  245. }
  246. outpath += "/";
  247. std::string outpathImp;
  248. if (relink) {
  249. outpath = this->Makefile->GetCurrentBinaryDirectory();
  250. outpath += "/CMakeFiles";
  251. outpath += "/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 = "CMAKE_";
  410. linkRuleVar += linkLanguage;
  411. linkRuleVar += "_LINK_EXECUTABLE";
  412. std::string linkRule = this->GetLinkRule(linkRuleVar);
  413. std::vector<std::string> commands1;
  414. cmSystemTools::ExpandListArgument(linkRule, real_link_commands);
  415. if (this->GeneratorTarget->IsExecutableWithExports()) {
  416. // If a separate rule for creating an import library is specified
  417. // add it now.
  418. std::string implibRuleVar = "CMAKE_";
  419. implibRuleVar += linkLanguage;
  420. implibRuleVar += "_CREATE_IMPORT_LIBRARY";
  421. if (const char* rule = this->Makefile->GetDefinition(implibRuleVar)) {
  422. cmSystemTools::ExpandListArgument(rule, real_link_commands);
  423. }
  424. }
  425. bool useResponseFileForObjects =
  426. this->CheckUseResponseFileForObjects(linkLanguage);
  427. bool const useResponseFileForLibs =
  428. this->CheckUseResponseFileForLibraries(linkLanguage);
  429. // Expand the rule variables.
  430. {
  431. bool useWatcomQuote =
  432. this->Makefile->IsOn(linkRuleVar + "_USE_WATCOM_QUOTE");
  433. // Set path conversion for link script shells.
  434. this->LocalGenerator->SetLinkScriptShell(useLinkScript);
  435. std::unique_ptr<cmLinkLineComputer> linkLineComputer(
  436. this->CreateLinkLineComputer(
  437. this->LocalGenerator,
  438. this->LocalGenerator->GetStateSnapshot().GetDirectory()));
  439. linkLineComputer->SetForResponse(useResponseFileForLibs);
  440. linkLineComputer->SetUseWatcomQuote(useWatcomQuote);
  441. linkLineComputer->SetRelink(relink);
  442. // Collect up flags to link in needed libraries.
  443. std::string linkLibs;
  444. this->CreateLinkLibs(linkLineComputer.get(), linkLibs,
  445. useResponseFileForLibs, depends);
  446. // Construct object file lists that may be needed to expand the
  447. // rule.
  448. std::string buildObjs;
  449. this->CreateObjectLists(useLinkScript, false, useResponseFileForObjects,
  450. buildObjs, depends, useWatcomQuote);
  451. if (!this->DeviceLinkObject.empty()) {
  452. buildObjs += " " +
  453. this->LocalGenerator->ConvertToOutputFormat(
  454. this->LocalGenerator->MaybeConvertToRelativePath(
  455. this->LocalGenerator->GetCurrentBinaryDirectory(),
  456. this->DeviceLinkObject),
  457. cmOutputConverter::SHELL);
  458. }
  459. // maybe create .def file from list of objects
  460. this->GenDefFile(real_link_commands);
  461. std::string manifests = this->GetManifests();
  462. cmRulePlaceholderExpander::RuleVariables vars;
  463. vars.CMTargetName = this->GeneratorTarget->GetName().c_str();
  464. vars.CMTargetType =
  465. cmState::GetTargetTypeName(this->GeneratorTarget->GetType());
  466. vars.Language = linkLanguage.c_str();
  467. vars.Objects = buildObjs.c_str();
  468. std::string objectDir = this->GeneratorTarget->GetSupportDirectory();
  469. objectDir = this->LocalGenerator->ConvertToOutputFormat(
  470. this->LocalGenerator->MaybeConvertToRelativePath(
  471. this->LocalGenerator->GetCurrentBinaryDirectory(), objectDir),
  472. cmOutputConverter::SHELL);
  473. vars.ObjectDir = objectDir.c_str();
  474. cmOutputConverter::OutputFormat output = (useWatcomQuote)
  475. ? cmOutputConverter::WATCOMQUOTE
  476. : cmOutputConverter::SHELL;
  477. std::string target = this->LocalGenerator->ConvertToOutputFormat(
  478. this->LocalGenerator->MaybeConvertToRelativePath(
  479. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPathReal),
  480. output);
  481. vars.Target = target.c_str();
  482. vars.TargetPDB = targetOutPathPDB.c_str();
  483. // Setup the target version.
  484. std::string targetVersionMajor;
  485. std::string targetVersionMinor;
  486. {
  487. std::ostringstream majorStream;
  488. std::ostringstream minorStream;
  489. int major;
  490. int minor;
  491. this->GeneratorTarget->GetTargetVersion(major, minor);
  492. majorStream << major;
  493. minorStream << minor;
  494. targetVersionMajor = majorStream.str();
  495. targetVersionMinor = minorStream.str();
  496. }
  497. vars.TargetVersionMajor = targetVersionMajor.c_str();
  498. vars.TargetVersionMinor = targetVersionMinor.c_str();
  499. vars.LinkLibraries = linkLibs.c_str();
  500. vars.Flags = flags.c_str();
  501. vars.LinkFlags = linkFlags.c_str();
  502. vars.Manifests = manifests.c_str();
  503. if (this->GeneratorTarget->GetPropertyAsBool("LINK_WHAT_YOU_USE")) {
  504. std::string cmakeCommand = this->LocalGenerator->ConvertToOutputFormat(
  505. cmSystemTools::GetCMakeCommand(), cmLocalGenerator::SHELL);
  506. cmakeCommand += " -E __run_co_compile --lwyu=";
  507. cmakeCommand += targetOutPathReal;
  508. real_link_commands.push_back(std::move(cmakeCommand));
  509. }
  510. std::string launcher;
  511. const char* val = this->LocalGenerator->GetRuleLauncher(
  512. this->GeneratorTarget, "RULE_LAUNCH_LINK");
  513. if (val && *val) {
  514. launcher = val;
  515. launcher += " ";
  516. }
  517. std::unique_ptr<cmRulePlaceholderExpander> rulePlaceholderExpander(
  518. this->LocalGenerator->CreateRulePlaceholderExpander());
  519. // Expand placeholders in the commands.
  520. rulePlaceholderExpander->SetTargetImpLib(targetOutPathImport);
  521. for (std::string& real_link_command : real_link_commands) {
  522. real_link_command = launcher + real_link_command;
  523. rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator,
  524. real_link_command, vars);
  525. }
  526. // Restore path conversion to normal shells.
  527. this->LocalGenerator->SetLinkScriptShell(false);
  528. }
  529. // Optionally convert the build rule to use a script to avoid long
  530. // command lines in the make shell.
  531. if (useLinkScript) {
  532. // Use a link script.
  533. const char* name = (relink ? "relink.txt" : "link.txt");
  534. this->CreateLinkScript(name, real_link_commands, commands1, depends);
  535. } else {
  536. // No link script. Just use the link rule directly.
  537. commands1 = real_link_commands;
  538. }
  539. this->LocalGenerator->CreateCDCommand(
  540. commands1, this->Makefile->GetCurrentBinaryDirectory(),
  541. this->LocalGenerator->GetBinaryDirectory());
  542. cmAppend(commands, commands1);
  543. commands1.clear();
  544. // Add a rule to create necessary symlinks for the library.
  545. if (targetOutPath != targetOutPathReal) {
  546. std::string symlink = "$(CMAKE_COMMAND) -E cmake_symlink_executable ";
  547. symlink += targetOutPathReal;
  548. symlink += " ";
  549. symlink += targetOutPath;
  550. commands1.push_back(std::move(symlink));
  551. this->LocalGenerator->CreateCDCommand(
  552. commands1, this->Makefile->GetCurrentBinaryDirectory(),
  553. this->LocalGenerator->GetBinaryDirectory());
  554. cmAppend(commands, commands1);
  555. commands1.clear();
  556. }
  557. // Add the post-build rules when building but not when relinking.
  558. if (!relink) {
  559. this->LocalGenerator->AppendCustomCommands(
  560. commands, this->GeneratorTarget->GetPostBuildCommands(),
  561. this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
  562. }
  563. // Write the build rule.
  564. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, nullptr,
  565. targetFullPathReal, depends, commands,
  566. false);
  567. // The symlink name for the target should depend on the real target
  568. // so if the target version changes it rebuilds and recreates the
  569. // symlink.
  570. if (targetFullPath != targetFullPathReal) {
  571. depends.clear();
  572. commands.clear();
  573. depends.push_back(targetFullPathReal);
  574. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, nullptr,
  575. targetFullPath, depends, commands,
  576. false);
  577. }
  578. // Write the main driver rule to build everything in this target.
  579. this->WriteTargetDriverRule(targetFullPath, relink);
  580. // Clean all the possible executable names and symlinks.
  581. this->CleanFiles.insert(exeCleanFiles.begin(), exeCleanFiles.end());
  582. }