cmMakefileExecutableTargetGenerator.cxx 25 KB

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