cmMakefileLibraryTargetGenerator.cxx 36 KB

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