cmMakefileLibraryTargetGenerator.cxx 38 KB

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