cmMakefileExecutableTargetGenerator.cxx 26 KB

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