cmMakefileLibraryTargetGenerator.cxx 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  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 "cmMakefileLibraryTargetGenerator.h"
  4. #include <cstddef>
  5. #include <set>
  6. #include <sstream>
  7. #include <utility>
  8. #include <vector>
  9. #include <cm/memory>
  10. #include <cmext/algorithm>
  11. #include "cmGeneratedFileStream.h"
  12. #include "cmGeneratorTarget.h"
  13. #include "cmGlobalUnixMakefileGenerator3.h"
  14. #include "cmLinkLineComputer.h"
  15. #include "cmLinkLineDeviceComputer.h"
  16. #include "cmLocalGenerator.h"
  17. #include "cmLocalUnixMakefileGenerator3.h"
  18. #include "cmMakefile.h"
  19. #include "cmOSXBundleGenerator.h"
  20. #include "cmOutputConverter.h"
  21. #include "cmProperty.h"
  22. #include "cmRulePlaceholderExpander.h"
  23. #include "cmState.h"
  24. #include "cmStateDirectory.h"
  25. #include "cmStateSnapshot.h"
  26. #include "cmStateTypes.h"
  27. #include "cmStringAlgorithms.h"
  28. #include "cmSystemTools.h"
  29. cmMakefileLibraryTargetGenerator::cmMakefileLibraryTargetGenerator(
  30. cmGeneratorTarget* target)
  31. : cmMakefileTargetGenerator(target)
  32. {
  33. this->CustomCommandDriver = OnDepends;
  34. if (this->GeneratorTarget->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  35. this->TargetNames =
  36. this->GeneratorTarget->GetLibraryNames(this->GetConfigName());
  37. }
  38. this->OSXBundleGenerator = cm::make_unique<cmOSXBundleGenerator>(target);
  39. this->OSXBundleGenerator->SetMacContentFolders(&this->MacContentFolders);
  40. }
  41. cmMakefileLibraryTargetGenerator::~cmMakefileLibraryTargetGenerator() =
  42. default;
  43. void cmMakefileLibraryTargetGenerator::WriteRuleFiles()
  44. {
  45. // create the build.make file and directory, put in the common blocks
  46. this->CreateRuleFile();
  47. // write rules used to help build object files
  48. this->WriteCommonCodeRules();
  49. // write the per-target per-language flags
  50. this->WriteTargetLanguageFlags();
  51. // write in rules for object files and custom commands
  52. this->WriteTargetBuildRules();
  53. // write the link rules
  54. // Write the rule for this target type.
  55. switch (this->GeneratorTarget->GetType()) {
  56. case cmStateEnums::STATIC_LIBRARY:
  57. this->WriteStaticLibraryRules();
  58. break;
  59. case cmStateEnums::SHARED_LIBRARY:
  60. this->WriteSharedLibraryRules(false);
  61. if (this->GeneratorTarget->NeedRelinkBeforeInstall(
  62. this->GetConfigName())) {
  63. // Write rules to link an installable version of the target.
  64. this->WriteSharedLibraryRules(true);
  65. }
  66. break;
  67. case cmStateEnums::MODULE_LIBRARY:
  68. this->WriteModuleLibraryRules(false);
  69. if (this->GeneratorTarget->NeedRelinkBeforeInstall(
  70. this->GetConfigName())) {
  71. // Write rules to link an installable version of the target.
  72. this->WriteModuleLibraryRules(true);
  73. }
  74. break;
  75. case cmStateEnums::OBJECT_LIBRARY:
  76. this->WriteObjectLibraryRules();
  77. break;
  78. default:
  79. // If language is not known, this is an error.
  80. cmSystemTools::Error("Unknown Library Type");
  81. break;
  82. }
  83. // Write clean target
  84. this->WriteTargetCleanRules();
  85. // Write the dependency generation rule. This must be done last so
  86. // that multiple output pair information is available.
  87. this->WriteTargetDependRules();
  88. // close the streams
  89. this->CloseFileStreams();
  90. }
  91. void cmMakefileLibraryTargetGenerator::WriteObjectLibraryRules()
  92. {
  93. std::vector<std::string> commands;
  94. std::vector<std::string> depends;
  95. // Add post-build rules.
  96. this->LocalGenerator->AppendCustomCommands(
  97. commands, this->GeneratorTarget->GetPostBuildCommands(),
  98. this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
  99. // Depend on the object files.
  100. this->AppendObjectDepends(depends);
  101. // Write the rule.
  102. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, nullptr,
  103. this->GeneratorTarget->GetName(),
  104. depends, commands, true);
  105. // Write the main driver rule to build everything in this target.
  106. this->WriteTargetDriverRule(this->GeneratorTarget->GetName(), false);
  107. }
  108. void cmMakefileLibraryTargetGenerator::WriteStaticLibraryRules()
  109. {
  110. const bool requiresDeviceLinking = requireDeviceLinking(
  111. *this->GeneratorTarget, *this->LocalGenerator, this->GetConfigName());
  112. if (requiresDeviceLinking) {
  113. this->WriteDeviceLibraryRules("CMAKE_CUDA_DEVICE_LINK_LIBRARY", false);
  114. }
  115. std::string linkLanguage =
  116. this->GeneratorTarget->GetLinkerLanguage(this->GetConfigName());
  117. std::string linkRuleVar = this->GeneratorTarget->GetCreateRuleVariable(
  118. linkLanguage, this->GetConfigName());
  119. std::string extraFlags;
  120. this->LocalGenerator->GetStaticLibraryFlags(
  121. extraFlags, this->GetConfigName(), linkLanguage, this->GeneratorTarget);
  122. this->WriteLibraryRules(linkRuleVar, extraFlags, false);
  123. }
  124. void cmMakefileLibraryTargetGenerator::WriteSharedLibraryRules(bool relink)
  125. {
  126. if (this->GeneratorTarget->IsFrameworkOnApple()) {
  127. this->WriteFrameworkRules(relink);
  128. return;
  129. }
  130. if (!relink) {
  131. const bool requiresDeviceLinking = requireDeviceLinking(
  132. *this->GeneratorTarget, *this->LocalGenerator, this->GetConfigName());
  133. if (requiresDeviceLinking) {
  134. this->WriteDeviceLibraryRules("CMAKE_CUDA_DEVICE_LINK_LIBRARY", relink);
  135. }
  136. }
  137. std::string linkLanguage =
  138. this->GeneratorTarget->GetLinkerLanguage(this->GetConfigName());
  139. std::string linkRuleVar =
  140. cmStrCat("CMAKE_", linkLanguage, "_CREATE_SHARED_LIBRARY");
  141. std::string extraFlags;
  142. this->GetTargetLinkFlags(extraFlags, linkLanguage);
  143. this->LocalGenerator->AddConfigVariableFlags(
  144. extraFlags, "CMAKE_SHARED_LINKER_FLAGS", this->GetConfigName());
  145. std::unique_ptr<cmLinkLineComputer> linkLineComputer =
  146. this->CreateLinkLineComputer(
  147. this->LocalGenerator,
  148. this->LocalGenerator->GetStateSnapshot().GetDirectory());
  149. this->AddModuleDefinitionFlag(linkLineComputer.get(), extraFlags,
  150. this->GetConfigName());
  151. if (this->GeneratorTarget->GetPropertyAsBool("LINK_WHAT_YOU_USE")) {
  152. this->LocalGenerator->AppendFlags(extraFlags, " -Wl,--no-as-needed");
  153. }
  154. this->WriteLibraryRules(linkRuleVar, extraFlags, relink);
  155. }
  156. void cmMakefileLibraryTargetGenerator::WriteModuleLibraryRules(bool relink)
  157. {
  158. if (!relink) {
  159. const bool requiresDeviceLinking = requireDeviceLinking(
  160. *this->GeneratorTarget, *this->LocalGenerator, this->GetConfigName());
  161. if (requiresDeviceLinking) {
  162. this->WriteDeviceLibraryRules("CMAKE_CUDA_DEVICE_LINK_LIBRARY", relink);
  163. }
  164. }
  165. std::string linkLanguage =
  166. this->GeneratorTarget->GetLinkerLanguage(this->GetConfigName());
  167. std::string linkRuleVar =
  168. cmStrCat("CMAKE_", linkLanguage, "_CREATE_SHARED_MODULE");
  169. std::string extraFlags;
  170. this->GetTargetLinkFlags(extraFlags, linkLanguage);
  171. this->LocalGenerator->AddConfigVariableFlags(
  172. extraFlags, "CMAKE_MODULE_LINKER_FLAGS", this->GetConfigName());
  173. std::unique_ptr<cmLinkLineComputer> linkLineComputer =
  174. this->CreateLinkLineComputer(
  175. this->LocalGenerator,
  176. this->LocalGenerator->GetStateSnapshot().GetDirectory());
  177. this->AddModuleDefinitionFlag(linkLineComputer.get(), extraFlags,
  178. this->GetConfigName());
  179. this->WriteLibraryRules(linkRuleVar, extraFlags, relink);
  180. }
  181. void cmMakefileLibraryTargetGenerator::WriteFrameworkRules(bool relink)
  182. {
  183. std::string linkLanguage =
  184. this->GeneratorTarget->GetLinkerLanguage(this->GetConfigName());
  185. std::string linkRuleVar =
  186. cmStrCat("CMAKE_", linkLanguage, "_CREATE_MACOSX_FRAMEWORK");
  187. std::string extraFlags;
  188. this->GetTargetLinkFlags(extraFlags, linkLanguage);
  189. this->LocalGenerator->AddConfigVariableFlags(
  190. extraFlags, "CMAKE_MACOSX_FRAMEWORK_LINKER_FLAGS", this->GetConfigName());
  191. this->WriteLibraryRules(linkRuleVar, extraFlags, relink);
  192. }
  193. void cmMakefileLibraryTargetGenerator::WriteDeviceLibraryRules(
  194. const std::string& linkRuleVar, bool relink)
  195. {
  196. #ifndef CMAKE_BOOTSTRAP
  197. // TODO: Merge the methods that call this method to avoid
  198. // code duplication.
  199. std::vector<std::string> commands;
  200. std::string const objExt =
  201. this->Makefile->GetSafeDefinition("CMAKE_CUDA_OUTPUT_EXTENSION");
  202. // Get the name of the device object to generate.
  203. std::string const targetOutput =
  204. this->GeneratorTarget->ObjectDirectory + "cmake_device_link" + objExt;
  205. this->DeviceLinkObject = targetOutput;
  206. this->NumberOfProgressActions++;
  207. if (!this->NoRuleMessages) {
  208. cmLocalUnixMakefileGenerator3::EchoProgress progress;
  209. this->MakeEchoProgress(progress);
  210. // Add the link message.
  211. std::string buildEcho = cmStrCat(
  212. "Linking CUDA device code ",
  213. this->LocalGenerator->ConvertToOutputFormat(
  214. this->LocalGenerator->MaybeRelativeToCurBinDir(this->DeviceLinkObject),
  215. cmOutputConverter::SHELL));
  216. this->LocalGenerator->AppendEcho(
  217. commands, buildEcho, cmLocalUnixMakefileGenerator3::EchoLink, &progress);
  218. }
  219. if (this->Makefile->GetSafeDefinition("CMAKE_CUDA_COMPILER_ID") == "Clang") {
  220. this->WriteDeviceLinkRule(commands, targetOutput);
  221. } else {
  222. this->WriteNvidiaDeviceLibraryRules(linkRuleVar, relink, commands,
  223. targetOutput);
  224. }
  225. // Write the main driver rule to build everything in this target.
  226. this->WriteTargetDriverRule(targetOutput, relink);
  227. }
  228. void cmMakefileLibraryTargetGenerator::WriteNvidiaDeviceLibraryRules(
  229. const std::string& linkRuleVar, bool relink,
  230. std::vector<std::string>& commands, const std::string& targetOutput)
  231. {
  232. std::string linkLanguage = "CUDA";
  233. // Build list of dependencies.
  234. std::vector<std::string> depends;
  235. this->AppendLinkDepends(depends, linkLanguage);
  236. // Add language-specific flags.
  237. std::string langFlags;
  238. this->LocalGenerator->AddLanguageFlagsForLinking(
  239. langFlags, this->GeneratorTarget, linkLanguage, this->GetConfigName());
  240. // Create set of linking flags.
  241. std::string linkFlags;
  242. this->GetDeviceLinkFlags(linkFlags, linkLanguage);
  243. // Clean files associated with this library.
  244. std::set<std::string> libCleanFiles;
  245. libCleanFiles.insert(
  246. this->LocalGenerator->MaybeRelativeToCurBinDir(targetOutput));
  247. // Determine whether a link script will be used.
  248. bool useLinkScript = this->GlobalGenerator->GetUseLinkScript();
  249. bool useResponseFileForObjects =
  250. this->CheckUseResponseFileForObjects(linkLanguage);
  251. bool const useResponseFileForLibs =
  252. this->CheckUseResponseFileForLibraries(linkLanguage);
  253. cmRulePlaceholderExpander::RuleVariables vars;
  254. vars.Language = linkLanguage.c_str();
  255. // Expand the rule variables.
  256. std::vector<std::string> real_link_commands;
  257. {
  258. bool useWatcomQuote =
  259. this->Makefile->IsOn(linkRuleVar + "_USE_WATCOM_QUOTE");
  260. // Set path conversion for link script shells.
  261. this->LocalGenerator->SetLinkScriptShell(useLinkScript);
  262. // Collect up flags to link in needed libraries.
  263. std::string linkLibs;
  264. std::unique_ptr<cmLinkLineComputer> linkLineComputer(
  265. new cmLinkLineDeviceComputer(
  266. this->LocalGenerator,
  267. this->LocalGenerator->GetStateSnapshot().GetDirectory()));
  268. linkLineComputer->SetForResponse(useResponseFileForLibs);
  269. linkLineComputer->SetUseWatcomQuote(useWatcomQuote);
  270. linkLineComputer->SetRelink(relink);
  271. this->CreateLinkLibs(linkLineComputer.get(), linkLibs,
  272. useResponseFileForLibs, depends);
  273. // Construct object file lists that may be needed to expand the
  274. // rule.
  275. std::string buildObjs;
  276. this->CreateObjectLists(useLinkScript, false, // useArchiveRules
  277. useResponseFileForObjects, buildObjs, depends,
  278. useWatcomQuote);
  279. cmOutputConverter::OutputFormat output = (useWatcomQuote)
  280. ? cmOutputConverter::WATCOMQUOTE
  281. : cmOutputConverter::SHELL;
  282. std::string objectDir = this->GeneratorTarget->GetSupportDirectory();
  283. objectDir = this->LocalGenerator->ConvertToOutputFormat(
  284. this->LocalGenerator->MaybeRelativeToCurBinDir(objectDir),
  285. cmOutputConverter::SHELL);
  286. std::string target = this->LocalGenerator->ConvertToOutputFormat(
  287. this->LocalGenerator->MaybeRelativeToCurBinDir(targetOutput), output);
  288. std::string targetFullPathCompilePDB =
  289. this->ComputeTargetCompilePDB(this->GetConfigName());
  290. std::string targetOutPathCompilePDB =
  291. this->LocalGenerator->ConvertToOutputFormat(targetFullPathCompilePDB,
  292. cmOutputConverter::SHELL);
  293. vars.Objects = buildObjs.c_str();
  294. vars.ObjectDir = objectDir.c_str();
  295. vars.Target = target.c_str();
  296. vars.LinkLibraries = linkLibs.c_str();
  297. vars.ObjectsQuoted = buildObjs.c_str();
  298. vars.LanguageCompileFlags = langFlags.c_str();
  299. vars.LinkFlags = linkFlags.c_str();
  300. vars.TargetCompilePDB = targetOutPathCompilePDB.c_str();
  301. std::string launcher;
  302. cmProp val = this->LocalGenerator->GetRuleLauncher(this->GeneratorTarget,
  303. "RULE_LAUNCH_LINK");
  304. if (cmNonempty(val)) {
  305. launcher = cmStrCat(*val, ' ');
  306. }
  307. std::unique_ptr<cmRulePlaceholderExpander> rulePlaceholderExpander(
  308. this->LocalGenerator->CreateRulePlaceholderExpander());
  309. // Construct the main link rule and expand placeholders.
  310. rulePlaceholderExpander->SetTargetImpLib(targetOutput);
  311. std::string linkRule = this->GetLinkRule(linkRuleVar);
  312. cmExpandList(linkRule, real_link_commands);
  313. // Expand placeholders.
  314. for (std::string& real_link_command : real_link_commands) {
  315. real_link_command = cmStrCat(launcher, real_link_command);
  316. rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator,
  317. real_link_command, vars);
  318. }
  319. // Restore path conversion to normal shells.
  320. this->LocalGenerator->SetLinkScriptShell(false);
  321. // Clean all the possible library names and symlinks.
  322. this->CleanFiles.insert(libCleanFiles.begin(), libCleanFiles.end());
  323. }
  324. std::vector<std::string> commands1;
  325. // Optionally convert the build rule to use a script to avoid long
  326. // command lines in the make shell.
  327. if (useLinkScript) {
  328. // Use a link script.
  329. const char* name = (relink ? "drelink.txt" : "dlink.txt");
  330. this->CreateLinkScript(name, real_link_commands, commands1, depends);
  331. } else {
  332. // No link script. Just use the link rule directly.
  333. commands1 = real_link_commands;
  334. }
  335. this->LocalGenerator->CreateCDCommand(
  336. commands1, this->Makefile->GetCurrentBinaryDirectory(),
  337. this->LocalGenerator->GetBinaryDirectory());
  338. cm::append(commands, commands1);
  339. commands1.clear();
  340. // Compute the list of outputs.
  341. std::vector<std::string> outputs(1, targetOutput);
  342. // Write the build rule.
  343. this->WriteMakeRule(*this->BuildFileStream, nullptr, outputs, depends,
  344. commands, false);
  345. #else
  346. static_cast<void>(linkRuleVar);
  347. static_cast<void>(relink);
  348. #endif
  349. }
  350. void cmMakefileLibraryTargetGenerator::WriteLibraryRules(
  351. const std::string& linkRuleVar, const std::string& extraFlags, bool relink)
  352. {
  353. // TODO: Merge the methods that call this method to avoid
  354. // code duplication.
  355. std::vector<std::string> commands;
  356. // Get the language to use for linking this library.
  357. std::string linkLanguage =
  358. this->GeneratorTarget->GetLinkerLanguage(this->GetConfigName());
  359. // Make sure we have a link language.
  360. if (linkLanguage.empty()) {
  361. cmSystemTools::Error("Cannot determine link language for target \"" +
  362. this->GeneratorTarget->GetName() + "\".");
  363. return;
  364. }
  365. // Build list of dependencies.
  366. std::vector<std::string> depends;
  367. this->AppendLinkDepends(depends, linkLanguage);
  368. if (!this->DeviceLinkObject.empty()) {
  369. depends.push_back(this->DeviceLinkObject);
  370. }
  371. // Create set of linking flags.
  372. std::string linkFlags;
  373. this->LocalGenerator->AppendFlags(linkFlags, extraFlags);
  374. this->LocalGenerator->AppendIPOLinkerFlags(
  375. linkFlags, this->GeneratorTarget, this->GetConfigName(), linkLanguage);
  376. // Add OSX version flags, if any.
  377. if (this->GeneratorTarget->GetType() == cmStateEnums::SHARED_LIBRARY ||
  378. this->GeneratorTarget->GetType() == cmStateEnums::MODULE_LIBRARY) {
  379. this->AppendOSXVerFlag(linkFlags, linkLanguage, "COMPATIBILITY", true);
  380. this->AppendOSXVerFlag(linkFlags, linkLanguage, "CURRENT", false);
  381. }
  382. // Construct the name of the library.
  383. this->GeneratorTarget->GetLibraryNames(this->GetConfigName());
  384. // Construct the full path version of the names.
  385. std::string outpath;
  386. std::string outpathImp;
  387. if (this->GeneratorTarget->IsFrameworkOnApple()) {
  388. outpath = this->GeneratorTarget->GetDirectory(this->GetConfigName());
  389. this->OSXBundleGenerator->CreateFramework(this->TargetNames.Output,
  390. outpath, this->GetConfigName());
  391. outpath += '/';
  392. } else if (this->GeneratorTarget->IsCFBundleOnApple()) {
  393. outpath = this->GeneratorTarget->GetDirectory(this->GetConfigName());
  394. this->OSXBundleGenerator->CreateCFBundle(this->TargetNames.Output, outpath,
  395. this->GetConfigName());
  396. outpath += '/';
  397. } else if (relink) {
  398. outpath = cmStrCat(this->Makefile->GetCurrentBinaryDirectory(),
  399. "/CMakeFiles/CMakeRelink.dir");
  400. cmSystemTools::MakeDirectory(outpath);
  401. outpath += '/';
  402. if (!this->TargetNames.ImportLibrary.empty()) {
  403. outpathImp = outpath;
  404. }
  405. } else {
  406. outpath = this->GeneratorTarget->GetDirectory(this->GetConfigName());
  407. cmSystemTools::MakeDirectory(outpath);
  408. outpath += '/';
  409. if (!this->TargetNames.ImportLibrary.empty()) {
  410. outpathImp = this->GeneratorTarget->GetDirectory(
  411. this->GetConfigName(), cmStateEnums::ImportLibraryArtifact);
  412. cmSystemTools::MakeDirectory(outpathImp);
  413. outpathImp += '/';
  414. }
  415. }
  416. std::string compilePdbOutputPath =
  417. this->GeneratorTarget->GetCompilePDBDirectory(this->GetConfigName());
  418. cmSystemTools::MakeDirectory(compilePdbOutputPath);
  419. std::string pdbOutputPath =
  420. this->GeneratorTarget->GetPDBDirectory(this->GetConfigName());
  421. cmSystemTools::MakeDirectory(pdbOutputPath);
  422. pdbOutputPath += "/";
  423. std::string targetFullPath = outpath + this->TargetNames.Output;
  424. std::string targetFullPathPDB = pdbOutputPath + this->TargetNames.PDB;
  425. std::string targetFullPathSO = outpath + this->TargetNames.SharedObject;
  426. std::string targetFullPathReal = outpath + this->TargetNames.Real;
  427. std::string targetFullPathImport =
  428. outpathImp + this->TargetNames.ImportLibrary;
  429. // Construct the output path version of the names for use in command
  430. // arguments.
  431. std::string targetOutPathPDB = this->LocalGenerator->ConvertToOutputFormat(
  432. targetFullPathPDB, cmOutputConverter::SHELL);
  433. std::string targetOutPath = this->LocalGenerator->ConvertToOutputFormat(
  434. this->LocalGenerator->MaybeRelativeToCurBinDir(targetFullPath),
  435. cmOutputConverter::SHELL);
  436. std::string targetOutPathSO = this->LocalGenerator->ConvertToOutputFormat(
  437. this->LocalGenerator->MaybeRelativeToCurBinDir(targetFullPathSO),
  438. cmOutputConverter::SHELL);
  439. std::string targetOutPathReal = this->LocalGenerator->ConvertToOutputFormat(
  440. this->LocalGenerator->MaybeRelativeToCurBinDir(targetFullPathReal),
  441. cmOutputConverter::SHELL);
  442. std::string targetOutPathImport =
  443. this->LocalGenerator->ConvertToOutputFormat(
  444. this->LocalGenerator->MaybeRelativeToCurBinDir(targetFullPathImport),
  445. cmOutputConverter::SHELL);
  446. this->NumberOfProgressActions++;
  447. if (!this->NoRuleMessages) {
  448. cmLocalUnixMakefileGenerator3::EchoProgress progress;
  449. this->MakeEchoProgress(progress);
  450. // Add the link message.
  451. std::string buildEcho = cmStrCat("Linking ", linkLanguage);
  452. switch (this->GeneratorTarget->GetType()) {
  453. case cmStateEnums::STATIC_LIBRARY:
  454. buildEcho += " static library ";
  455. break;
  456. case cmStateEnums::SHARED_LIBRARY:
  457. buildEcho += " shared library ";
  458. break;
  459. case cmStateEnums::MODULE_LIBRARY:
  460. if (this->GeneratorTarget->IsCFBundleOnApple()) {
  461. buildEcho += " CFBundle";
  462. }
  463. buildEcho += " shared module ";
  464. break;
  465. default:
  466. buildEcho += " library ";
  467. break;
  468. }
  469. buildEcho += targetOutPath;
  470. this->LocalGenerator->AppendEcho(
  471. commands, buildEcho, cmLocalUnixMakefileGenerator3::EchoLink, &progress);
  472. }
  473. // Clean files associated with this library.
  474. std::set<std::string> libCleanFiles;
  475. libCleanFiles.insert(
  476. this->LocalGenerator->MaybeRelativeToCurBinDir(targetFullPathReal));
  477. std::vector<std::string> commands1;
  478. // Add a command to remove any existing files for this library.
  479. // for static libs only
  480. if (this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY) {
  481. this->LocalGenerator->AppendCleanCommand(commands1, libCleanFiles,
  482. this->GeneratorTarget, "target");
  483. this->LocalGenerator->CreateCDCommand(
  484. commands1, this->Makefile->GetCurrentBinaryDirectory(),
  485. this->LocalGenerator->GetBinaryDirectory());
  486. cm::append(commands, commands1);
  487. commands1.clear();
  488. }
  489. if (this->TargetNames.Output != this->TargetNames.Real) {
  490. libCleanFiles.insert(
  491. this->LocalGenerator->MaybeRelativeToCurBinDir(targetFullPath));
  492. }
  493. if (this->TargetNames.SharedObject != this->TargetNames.Real &&
  494. this->TargetNames.SharedObject != this->TargetNames.Output) {
  495. libCleanFiles.insert(
  496. this->LocalGenerator->MaybeRelativeToCurBinDir(targetFullPathSO));
  497. }
  498. if (!this->TargetNames.ImportLibrary.empty()) {
  499. libCleanFiles.insert(
  500. this->LocalGenerator->MaybeRelativeToCurBinDir(targetFullPathImport));
  501. std::string implib;
  502. if (this->GeneratorTarget->GetImplibGNUtoMS(
  503. this->GetConfigName(), targetFullPathImport, implib)) {
  504. libCleanFiles.insert(
  505. this->LocalGenerator->MaybeRelativeToCurBinDir(implib));
  506. }
  507. }
  508. // List the PDB for cleaning only when the whole target is
  509. // cleaned. We do not want to delete the .pdb file just before
  510. // linking the target.
  511. this->CleanFiles.insert(
  512. this->LocalGenerator->MaybeRelativeToCurBinDir(targetFullPathPDB));
  513. #ifdef _WIN32
  514. // There may be a manifest file for this target. Add it to the
  515. // clean set just in case.
  516. if (this->GeneratorTarget->GetType() != cmStateEnums::STATIC_LIBRARY) {
  517. libCleanFiles.insert(this->LocalGenerator->MaybeRelativeToCurBinDir(
  518. targetFullPath + ".manifest"));
  519. }
  520. #endif
  521. // Add the pre-build and pre-link rules building but not when relinking.
  522. if (!relink) {
  523. this->LocalGenerator->AppendCustomCommands(
  524. commands, this->GeneratorTarget->GetPreBuildCommands(),
  525. this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
  526. this->LocalGenerator->AppendCustomCommands(
  527. commands, this->GeneratorTarget->GetPreLinkCommands(),
  528. this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
  529. }
  530. // Determine whether a link script will be used.
  531. bool useLinkScript = this->GlobalGenerator->GetUseLinkScript();
  532. bool useResponseFileForObjects =
  533. this->CheckUseResponseFileForObjects(linkLanguage);
  534. bool const useResponseFileForLibs =
  535. this->CheckUseResponseFileForLibraries(linkLanguage);
  536. // For static libraries there might be archiving rules.
  537. bool haveStaticLibraryRule = false;
  538. std::vector<std::string> archiveCreateCommands;
  539. std::vector<std::string> archiveAppendCommands;
  540. std::vector<std::string> archiveFinishCommands;
  541. std::string::size_type archiveCommandLimit = std::string::npos;
  542. if (this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY) {
  543. haveStaticLibraryRule = this->Makefile->IsDefinitionSet(linkRuleVar);
  544. std::string arCreateVar =
  545. cmStrCat("CMAKE_", linkLanguage, "_ARCHIVE_CREATE");
  546. arCreateVar = this->GeneratorTarget->GetFeatureSpecificLinkRuleVariable(
  547. arCreateVar, linkLanguage, this->GetConfigName());
  548. this->Makefile->GetDefExpandList(arCreateVar, archiveCreateCommands);
  549. std::string arAppendVar =
  550. cmStrCat("CMAKE_", linkLanguage, "_ARCHIVE_APPEND");
  551. arAppendVar = this->GeneratorTarget->GetFeatureSpecificLinkRuleVariable(
  552. arAppendVar, linkLanguage, this->GetConfigName());
  553. this->Makefile->GetDefExpandList(arAppendVar, archiveAppendCommands);
  554. std::string arFinishVar =
  555. cmStrCat("CMAKE_", linkLanguage, "_ARCHIVE_FINISH");
  556. arFinishVar = this->GeneratorTarget->GetFeatureSpecificLinkRuleVariable(
  557. arFinishVar, linkLanguage, this->GetConfigName());
  558. this->Makefile->GetDefExpandList(arFinishVar, archiveFinishCommands);
  559. }
  560. // Decide whether to use archiving rules.
  561. bool useArchiveRules = !haveStaticLibraryRule &&
  562. !archiveCreateCommands.empty() && !archiveAppendCommands.empty();
  563. if (useArchiveRules) {
  564. // Archiving rules are always run with a link script.
  565. useLinkScript = true;
  566. // Archiving rules never use a response file.
  567. useResponseFileForObjects = false;
  568. // Limit the length of individual object lists to less than half of
  569. // the command line length limit (leaving half for other flags).
  570. // This may result in several calls to the archiver.
  571. if (size_t limit = cmSystemTools::CalculateCommandLineLengthLimit()) {
  572. archiveCommandLimit = limit / 2;
  573. } else {
  574. archiveCommandLimit = 8000;
  575. }
  576. }
  577. // Expand the rule variables.
  578. std::vector<std::string> real_link_commands;
  579. {
  580. bool useWatcomQuote =
  581. this->Makefile->IsOn(linkRuleVar + "_USE_WATCOM_QUOTE");
  582. // Set path conversion for link script shells.
  583. this->LocalGenerator->SetLinkScriptShell(useLinkScript);
  584. // Collect up flags to link in needed libraries.
  585. std::string linkLibs;
  586. if (this->GeneratorTarget->GetType() != cmStateEnums::STATIC_LIBRARY) {
  587. std::unique_ptr<cmLinkLineComputer> linkLineComputer =
  588. this->CreateLinkLineComputer(
  589. this->LocalGenerator,
  590. this->LocalGenerator->GetStateSnapshot().GetDirectory());
  591. linkLineComputer->SetForResponse(useResponseFileForLibs);
  592. linkLineComputer->SetUseWatcomQuote(useWatcomQuote);
  593. linkLineComputer->SetRelink(relink);
  594. this->CreateLinkLibs(linkLineComputer.get(), linkLibs,
  595. useResponseFileForLibs, depends);
  596. }
  597. // Construct object file lists that may be needed to expand the
  598. // rule.
  599. std::string buildObjs;
  600. this->CreateObjectLists(useLinkScript, useArchiveRules,
  601. useResponseFileForObjects, buildObjs, depends,
  602. useWatcomQuote);
  603. if (!this->DeviceLinkObject.empty()) {
  604. buildObjs += " " +
  605. this->LocalGenerator->ConvertToOutputFormat(
  606. this->LocalGenerator->MaybeRelativeToCurBinDir(
  607. this->DeviceLinkObject),
  608. cmOutputConverter::SHELL);
  609. }
  610. std::string const& aixExports = this->GetAIXExports(this->GetConfigName());
  611. // maybe create .def file from list of objects
  612. this->GenDefFile(real_link_commands);
  613. std::string manifests = this->GetManifests(this->GetConfigName());
  614. cmRulePlaceholderExpander::RuleVariables vars;
  615. vars.TargetPDB = targetOutPathPDB.c_str();
  616. // Setup the target version.
  617. std::string targetVersionMajor;
  618. std::string targetVersionMinor;
  619. {
  620. std::ostringstream majorStream;
  621. std::ostringstream minorStream;
  622. int major;
  623. int minor;
  624. this->GeneratorTarget->GetTargetVersion(major, minor);
  625. majorStream << major;
  626. minorStream << minor;
  627. targetVersionMajor = majorStream.str();
  628. targetVersionMinor = minorStream.str();
  629. }
  630. vars.TargetVersionMajor = targetVersionMajor.c_str();
  631. vars.TargetVersionMinor = targetVersionMinor.c_str();
  632. vars.CMTargetName = this->GeneratorTarget->GetName().c_str();
  633. vars.CMTargetType =
  634. cmState::GetTargetTypeName(this->GeneratorTarget->GetType()).c_str();
  635. vars.Language = linkLanguage.c_str();
  636. vars.AIXExports = aixExports.c_str();
  637. vars.Objects = buildObjs.c_str();
  638. std::string objectDir = this->GeneratorTarget->GetSupportDirectory();
  639. objectDir = this->LocalGenerator->ConvertToOutputFormat(
  640. this->LocalGenerator->MaybeRelativeToCurBinDir(objectDir),
  641. cmOutputConverter::SHELL);
  642. vars.ObjectDir = objectDir.c_str();
  643. cmOutputConverter::OutputFormat output = (useWatcomQuote)
  644. ? cmOutputConverter::WATCOMQUOTE
  645. : cmOutputConverter::SHELL;
  646. std::string target = this->LocalGenerator->ConvertToOutputFormat(
  647. this->LocalGenerator->MaybeRelativeToCurBinDir(targetFullPathReal),
  648. output);
  649. vars.Target = target.c_str();
  650. vars.LinkLibraries = linkLibs.c_str();
  651. vars.ObjectsQuoted = buildObjs.c_str();
  652. std::string targetOutSOName;
  653. if (this->GeneratorTarget->HasSOName(this->GetConfigName())) {
  654. vars.SONameFlag = this->Makefile->GetSONameFlag(linkLanguage);
  655. targetOutSOName = this->LocalGenerator->ConvertToOutputFormat(
  656. this->TargetNames.SharedObject.c_str(), cmOutputConverter::SHELL);
  657. vars.TargetSOName = targetOutSOName.c_str();
  658. }
  659. vars.LinkFlags = linkFlags.c_str();
  660. vars.Manifests = manifests.c_str();
  661. // Compute the directory portion of the install_name setting.
  662. std::string install_name_dir;
  663. if (this->GeneratorTarget->GetType() == cmStateEnums::SHARED_LIBRARY) {
  664. // Get the install_name directory for the build tree.
  665. install_name_dir = this->GeneratorTarget->GetInstallNameDirForBuildTree(
  666. this->GetConfigName());
  667. // Set the rule variable replacement value.
  668. if (install_name_dir.empty()) {
  669. vars.TargetInstallNameDir = "";
  670. } else {
  671. // Convert to a path for the native build tool.
  672. install_name_dir = this->LocalGenerator->ConvertToOutputFormat(
  673. install_name_dir, cmOutputConverter::SHELL);
  674. vars.TargetInstallNameDir = install_name_dir.c_str();
  675. }
  676. }
  677. // Add language-specific flags.
  678. std::string langFlags;
  679. this->LocalGenerator->AddLanguageFlagsForLinking(
  680. langFlags, this->GeneratorTarget, linkLanguage, this->GetConfigName());
  681. this->LocalGenerator->AddArchitectureFlags(
  682. langFlags, this->GeneratorTarget, linkLanguage, this->GetConfigName());
  683. vars.LanguageCompileFlags = langFlags.c_str();
  684. std::string launcher;
  685. cmProp val = this->LocalGenerator->GetRuleLauncher(this->GeneratorTarget,
  686. "RULE_LAUNCH_LINK");
  687. if (cmNonempty(val)) {
  688. launcher = cmStrCat(*val, ' ');
  689. }
  690. std::unique_ptr<cmRulePlaceholderExpander> rulePlaceholderExpander(
  691. this->LocalGenerator->CreateRulePlaceholderExpander());
  692. // Construct the main link rule and expand placeholders.
  693. rulePlaceholderExpander->SetTargetImpLib(targetOutPathImport);
  694. if (useArchiveRules) {
  695. // Construct the individual object list strings.
  696. std::vector<std::string> object_strings;
  697. this->WriteObjectsStrings(object_strings, archiveCommandLimit);
  698. // Add the cuda device object to the list of archive files. This will
  699. // only occur on archives which have CUDA_RESOLVE_DEVICE_SYMBOLS enabled
  700. if (!this->DeviceLinkObject.empty()) {
  701. object_strings.push_back(this->LocalGenerator->ConvertToOutputFormat(
  702. this->LocalGenerator->MaybeRelativeToCurBinDir(
  703. this->DeviceLinkObject),
  704. cmOutputConverter::SHELL));
  705. }
  706. // Create the archive with the first set of objects.
  707. auto osi = object_strings.begin();
  708. {
  709. vars.Objects = osi->c_str();
  710. for (std::string const& acc : archiveCreateCommands) {
  711. std::string cmd = launcher + acc;
  712. rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator,
  713. cmd, vars);
  714. real_link_commands.push_back(std::move(cmd));
  715. }
  716. }
  717. // Append to the archive with the other object sets.
  718. for (++osi; osi != object_strings.end(); ++osi) {
  719. vars.Objects = osi->c_str();
  720. for (std::string const& aac : archiveAppendCommands) {
  721. std::string cmd = launcher + aac;
  722. rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator,
  723. cmd, vars);
  724. real_link_commands.push_back(std::move(cmd));
  725. }
  726. }
  727. // Finish the archive.
  728. vars.Objects = "";
  729. for (std::string const& afc : archiveFinishCommands) {
  730. std::string cmd = launcher + afc;
  731. rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator, cmd,
  732. vars);
  733. // If there is no ranlib the command will be ":". Skip it.
  734. if (!cmd.empty() && cmd[0] != ':') {
  735. real_link_commands.push_back(std::move(cmd));
  736. }
  737. }
  738. } else {
  739. // Get the set of commands.
  740. std::string linkRule = this->GetLinkRule(linkRuleVar);
  741. cmExpandList(linkRule, real_link_commands);
  742. if (this->GeneratorTarget->GetPropertyAsBool("LINK_WHAT_YOU_USE") &&
  743. (this->GeneratorTarget->GetType() == cmStateEnums::SHARED_LIBRARY)) {
  744. std::string cmakeCommand = cmStrCat(
  745. this->LocalGenerator->ConvertToOutputFormat(
  746. cmSystemTools::GetCMakeCommand(), cmLocalGenerator::SHELL),
  747. " -E __run_co_compile --lwyu=", targetOutPathReal);
  748. real_link_commands.push_back(std::move(cmakeCommand));
  749. }
  750. // Expand placeholders.
  751. for (std::string& real_link_command : real_link_commands) {
  752. real_link_command = cmStrCat(launcher, real_link_command);
  753. rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator,
  754. real_link_command, vars);
  755. }
  756. }
  757. // Restore path conversion to normal shells.
  758. this->LocalGenerator->SetLinkScriptShell(false);
  759. }
  760. // Optionally convert the build rule to use a script to avoid long
  761. // command lines in the make shell.
  762. if (useLinkScript) {
  763. // Use a link script.
  764. const char* name = (relink ? "relink.txt" : "link.txt");
  765. this->CreateLinkScript(name, real_link_commands, commands1, depends);
  766. } else {
  767. // No link script. Just use the link rule directly.
  768. commands1 = real_link_commands;
  769. }
  770. this->LocalGenerator->CreateCDCommand(
  771. commands1, this->Makefile->GetCurrentBinaryDirectory(),
  772. this->LocalGenerator->GetBinaryDirectory());
  773. cm::append(commands, commands1);
  774. commands1.clear();
  775. // Add a rule to create necessary symlinks for the library.
  776. // Frameworks are handled by cmOSXBundleGenerator.
  777. if (targetOutPath != targetOutPathReal &&
  778. !this->GeneratorTarget->IsFrameworkOnApple()) {
  779. std::string symlink =
  780. cmStrCat("$(CMAKE_COMMAND) -E cmake_symlink_library ", targetOutPathReal,
  781. ' ', targetOutPathSO, ' ', targetOutPath);
  782. commands1.push_back(std::move(symlink));
  783. this->LocalGenerator->CreateCDCommand(
  784. commands1, this->Makefile->GetCurrentBinaryDirectory(),
  785. this->LocalGenerator->GetBinaryDirectory());
  786. cm::append(commands, commands1);
  787. commands1.clear();
  788. }
  789. // Add the post-build rules when building but not when relinking.
  790. if (!relink) {
  791. this->LocalGenerator->AppendCustomCommands(
  792. commands, this->GeneratorTarget->GetPostBuildCommands(),
  793. this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
  794. }
  795. // Compute the list of outputs.
  796. std::vector<std::string> outputs(1, targetFullPathReal);
  797. if (this->TargetNames.SharedObject != this->TargetNames.Real) {
  798. outputs.push_back(targetFullPathSO);
  799. }
  800. if (this->TargetNames.Output != this->TargetNames.SharedObject &&
  801. this->TargetNames.Output != this->TargetNames.Real) {
  802. outputs.push_back(targetFullPath);
  803. }
  804. // Write the build rule.
  805. this->WriteMakeRule(*this->BuildFileStream, nullptr, outputs, depends,
  806. commands, false);
  807. // Write the main driver rule to build everything in this target.
  808. this->WriteTargetDriverRule(targetFullPath, relink);
  809. // Clean all the possible library names and symlinks.
  810. this->CleanFiles.insert(libCleanFiles.begin(), libCleanFiles.end());
  811. }