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