cmMakefileExecutableTargetGenerator.cxx 26 KB

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