cmMakefileExecutableTargetGenerator.cxx 27 KB

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