cmMakefileExecutableTargetGenerator.cxx 26 KB

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