cmMakefileLibraryTargetGenerator.cxx 36 KB

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