cmMakefileExecutableTargetGenerator.cxx 27 KB

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