cmMakefileExecutableTargetGenerator.cxx 26 KB

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