cmMakefileLibraryTargetGenerator.cxx 38 KB

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