cmMakefileExecutableTargetGenerator.cxx 26 KB

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