cmMakefileExecutableTargetGenerator.cxx 27 KB

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