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 <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. std::string const objExt =
  88. this->Makefile->GetSafeDefinition("CMAKE_CUDA_OUTPUT_EXTENSION");
  89. // Get the name of the device object to generate.
  90. std::string const targetOutputReal =
  91. this->GeneratorTarget->ObjectDirectory + "cmake_device_link" + objExt;
  92. this->DeviceLinkObject = targetOutputReal;
  93. this->NumberOfProgressActions++;
  94. if (!this->NoRuleMessages) {
  95. cmLocalUnixMakefileGenerator3::EchoProgress progress;
  96. this->MakeEchoProgress(progress);
  97. // Add the link message.
  98. std::string buildEcho = "Linking ";
  99. buildEcho += linkLanguage;
  100. buildEcho += " device code ";
  101. buildEcho += targetOutputReal;
  102. this->LocalGenerator->AppendEcho(
  103. commands, buildEcho, cmLocalUnixMakefileGenerator3::EchoLink, &progress);
  104. }
  105. // Build a list of compiler flags and linker flags.
  106. std::string flags;
  107. std::string linkFlags;
  108. // Add flags to create an executable.
  109. // Add symbol export flags if necessary.
  110. if (this->GeneratorTarget->IsExecutableWithExports()) {
  111. std::string export_flag_var = "CMAKE_EXE_EXPORTS_";
  112. export_flag_var += linkLanguage;
  113. export_flag_var += "_FLAG";
  114. this->LocalGenerator->AppendFlags(
  115. linkFlags, this->Makefile->GetDefinition(export_flag_var));
  116. }
  117. this->LocalGenerator->AppendFlags(linkFlags,
  118. this->LocalGenerator->GetLinkLibsCMP0065(
  119. linkLanguage, *this->GeneratorTarget));
  120. // Add language feature flags.
  121. this->AddFeatureFlags(flags, linkLanguage);
  122. this->LocalGenerator->AddArchitectureFlags(flags, this->GeneratorTarget,
  123. linkLanguage, this->ConfigName);
  124. // Add target-specific linker flags.
  125. this->LocalGenerator->AppendFlags(
  126. linkFlags, this->GeneratorTarget->GetProperty("LINK_FLAGS"));
  127. std::string linkFlagsConfig = "LINK_FLAGS_";
  128. linkFlagsConfig += cmSystemTools::UpperCase(this->ConfigName);
  129. this->LocalGenerator->AppendFlags(
  130. linkFlags, this->GeneratorTarget->GetProperty(linkFlagsConfig));
  131. {
  132. CM_AUTO_PTR<cmLinkLineComputer> linkLineComputer(
  133. this->CreateLinkLineComputer(
  134. this->LocalGenerator,
  135. this->LocalGenerator->GetStateSnapshot().GetDirectory()));
  136. this->AddModuleDefinitionFlag(linkLineComputer.get(), linkFlags);
  137. }
  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(this->ConfigName, true);
  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->AddFeatureFlags(flags, linkLanguage);
  369. this->LocalGenerator->AddArchitectureFlags(flags, this->GeneratorTarget,
  370. linkLanguage, this->ConfigName);
  371. // Add target-specific linker flags.
  372. this->LocalGenerator->AppendFlags(
  373. linkFlags, this->GeneratorTarget->GetProperty("LINK_FLAGS"));
  374. std::string linkFlagsConfig = "LINK_FLAGS_";
  375. linkFlagsConfig += cmSystemTools::UpperCase(this->ConfigName);
  376. this->LocalGenerator->AppendFlags(
  377. linkFlags, this->GeneratorTarget->GetProperty(linkFlagsConfig));
  378. {
  379. CM_AUTO_PTR<cmLinkLineComputer> linkLineComputer(
  380. this->CreateLinkLineComputer(
  381. this->LocalGenerator,
  382. this->LocalGenerator->GetStateSnapshot().GetDirectory()));
  383. this->AddModuleDefinitionFlag(linkLineComputer.get(), linkFlags);
  384. }
  385. // Construct a list of files associated with this executable that
  386. // may need to be cleaned.
  387. std::vector<std::string> exeCleanFiles;
  388. exeCleanFiles.push_back(this->LocalGenerator->MaybeConvertToRelativePath(
  389. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPath));
  390. #ifdef _WIN32
  391. // There may be a manifest file for this target. Add it to the
  392. // clean set just in case.
  393. exeCleanFiles.push_back(this->LocalGenerator->MaybeConvertToRelativePath(
  394. this->LocalGenerator->GetCurrentBinaryDirectory(),
  395. (targetFullPath + ".manifest").c_str()));
  396. #endif
  397. if (targetNameReal != targetName) {
  398. exeCleanFiles.push_back(this->LocalGenerator->MaybeConvertToRelativePath(
  399. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPathReal));
  400. }
  401. if (!targetNameImport.empty()) {
  402. exeCleanFiles.push_back(this->LocalGenerator->MaybeConvertToRelativePath(
  403. this->LocalGenerator->GetCurrentBinaryDirectory(),
  404. targetFullPathImport));
  405. std::string implib;
  406. if (this->GeneratorTarget->GetImplibGNUtoMS(targetFullPathImport,
  407. implib)) {
  408. exeCleanFiles.push_back(this->LocalGenerator->MaybeConvertToRelativePath(
  409. this->LocalGenerator->GetCurrentBinaryDirectory(), implib));
  410. }
  411. }
  412. // List the PDB for cleaning only when the whole target is
  413. // cleaned. We do not want to delete the .pdb file just before
  414. // linking the target.
  415. this->CleanFiles.push_back(this->LocalGenerator->MaybeConvertToRelativePath(
  416. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPathPDB));
  417. // Add the pre-build and pre-link rules building but not when relinking.
  418. if (!relink) {
  419. this->LocalGenerator->AppendCustomCommands(
  420. commands, this->GeneratorTarget->GetPreBuildCommands(),
  421. this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
  422. this->LocalGenerator->AppendCustomCommands(
  423. commands, this->GeneratorTarget->GetPreLinkCommands(),
  424. this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
  425. }
  426. // Determine whether a link script will be used.
  427. bool useLinkScript = this->GlobalGenerator->GetUseLinkScript();
  428. // Construct the main link rule.
  429. std::vector<std::string> real_link_commands;
  430. std::string linkRuleVar = "CMAKE_";
  431. linkRuleVar += linkLanguage;
  432. linkRuleVar += "_LINK_EXECUTABLE";
  433. std::string linkRule = this->GetLinkRule(linkRuleVar);
  434. std::vector<std::string> commands1;
  435. cmSystemTools::ExpandListArgument(linkRule, real_link_commands);
  436. if (this->GeneratorTarget->IsExecutableWithExports()) {
  437. // If a separate rule for creating an import library is specified
  438. // add it now.
  439. std::string implibRuleVar = "CMAKE_";
  440. implibRuleVar += linkLanguage;
  441. implibRuleVar += "_CREATE_IMPORT_LIBRARY";
  442. if (const char* rule = this->Makefile->GetDefinition(implibRuleVar)) {
  443. cmSystemTools::ExpandListArgument(rule, real_link_commands);
  444. }
  445. }
  446. bool useResponseFileForObjects =
  447. this->CheckUseResponseFileForObjects(linkLanguage);
  448. bool const useResponseFileForLibs =
  449. this->CheckUseResponseFileForLibraries(linkLanguage);
  450. // Expand the rule variables.
  451. {
  452. bool useWatcomQuote =
  453. this->Makefile->IsOn(linkRuleVar + "_USE_WATCOM_QUOTE");
  454. // Set path conversion for link script shells.
  455. this->LocalGenerator->SetLinkScriptShell(useLinkScript);
  456. CM_AUTO_PTR<cmLinkLineComputer> linkLineComputer(
  457. this->CreateLinkLineComputer(
  458. this->LocalGenerator,
  459. this->LocalGenerator->GetStateSnapshot().GetDirectory()));
  460. linkLineComputer->SetForResponse(useResponseFileForLibs);
  461. linkLineComputer->SetUseWatcomQuote(useWatcomQuote);
  462. linkLineComputer->SetRelink(relink);
  463. // Collect up flags to link in needed libraries.
  464. std::string linkLibs;
  465. this->CreateLinkLibs(linkLineComputer.get(), linkLibs,
  466. useResponseFileForLibs, depends);
  467. // Construct object file lists that may be needed to expand the
  468. // rule.
  469. std::string buildObjs;
  470. this->CreateObjectLists(useLinkScript, false, useResponseFileForObjects,
  471. buildObjs, depends, useWatcomQuote);
  472. if (!this->DeviceLinkObject.empty()) {
  473. buildObjs += " " +
  474. this->LocalGenerator->ConvertToOutputFormat(
  475. this->LocalGenerator->MaybeConvertToRelativePath(
  476. this->LocalGenerator->GetCurrentBinaryDirectory(),
  477. this->DeviceLinkObject),
  478. cmOutputConverter::SHELL);
  479. }
  480. // maybe create .def file from list of objects
  481. if (this->GeneratorTarget->IsExecutableWithExports() &&
  482. this->Makefile->IsOn("CMAKE_SUPPORT_WINDOWS_EXPORT_ALL_SYMBOLS")) {
  483. this->GenDefFile(real_link_commands, linkFlags);
  484. }
  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_iwyu --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::vector<std::string>::iterator i = real_link_commands.begin();
  546. i != real_link_commands.end(); ++i) {
  547. *i = launcher + *i;
  548. rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator, *i,
  549. vars);
  550. }
  551. // Restore path conversion to normal shells.
  552. this->LocalGenerator->SetLinkScriptShell(false);
  553. }
  554. // Optionally convert the build rule to use a script to avoid long
  555. // command lines in the make shell.
  556. if (useLinkScript) {
  557. // Use a link script.
  558. const char* name = (relink ? "relink.txt" : "link.txt");
  559. this->CreateLinkScript(name, real_link_commands, commands1, depends);
  560. } else {
  561. // No link script. Just use the link rule directly.
  562. commands1 = real_link_commands;
  563. }
  564. this->LocalGenerator->CreateCDCommand(
  565. commands1, this->Makefile->GetCurrentBinaryDirectory(),
  566. this->LocalGenerator->GetBinaryDirectory());
  567. commands.insert(commands.end(), commands1.begin(), commands1.end());
  568. commands1.clear();
  569. // Add a rule to create necessary symlinks for the library.
  570. if (targetOutPath != targetOutPathReal) {
  571. std::string symlink = "$(CMAKE_COMMAND) -E cmake_symlink_executable ";
  572. symlink += targetOutPathReal;
  573. symlink += " ";
  574. symlink += targetOutPath;
  575. commands1.push_back(symlink);
  576. this->LocalGenerator->CreateCDCommand(
  577. commands1, this->Makefile->GetCurrentBinaryDirectory(),
  578. this->LocalGenerator->GetBinaryDirectory());
  579. commands.insert(commands.end(), commands1.begin(), commands1.end());
  580. commands1.clear();
  581. }
  582. // Add the post-build rules when building but not when relinking.
  583. if (!relink) {
  584. this->LocalGenerator->AppendCustomCommands(
  585. commands, this->GeneratorTarget->GetPostBuildCommands(),
  586. this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
  587. }
  588. // Write the build rule.
  589. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, CM_NULLPTR,
  590. targetFullPathReal, depends, commands,
  591. false);
  592. // The symlink name for the target should depend on the real target
  593. // so if the target version changes it rebuilds and recreates the
  594. // symlink.
  595. if (targetFullPath != targetFullPathReal) {
  596. depends.clear();
  597. commands.clear();
  598. depends.push_back(targetFullPathReal);
  599. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, CM_NULLPTR,
  600. targetFullPath, depends, commands,
  601. false);
  602. }
  603. // Write the main driver rule to build everything in this target.
  604. this->WriteTargetDriverRule(targetFullPath, relink);
  605. // Clean all the possible executable names and symlinks.
  606. this->CleanFiles.insert(this->CleanFiles.end(), exeCleanFiles.begin(),
  607. exeCleanFiles.end());
  608. }