cmMakefileLibraryTargetGenerator.cxx 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  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 <sstream>
  6. #include <vector>
  7. #include "cmGeneratedFileStream.h"
  8. #include "cmGeneratorTarget.h"
  9. #include "cmGlobalUnixMakefileGenerator3.h"
  10. #include "cmLinkLineComputer.h"
  11. #include "cmLinkLineDeviceComputer.h"
  12. #include "cmLocalGenerator.h"
  13. #include "cmLocalUnixMakefileGenerator3.h"
  14. #include "cmMakefile.h"
  15. #include "cmOSXBundleGenerator.h"
  16. #include "cmOutputConverter.h"
  17. #include "cmRulePlaceholderExpander.h"
  18. #include "cmState.h"
  19. #include "cmStateDirectory.h"
  20. #include "cmStateSnapshot.h"
  21. #include "cmStateTypes.h"
  22. #include "cmSystemTools.h"
  23. #include "cm_auto_ptr.hxx"
  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 the requires target.
  82. this->WriteTargetRequiresRules();
  83. // Write clean target
  84. this->WriteTargetCleanRules();
  85. // Write the dependency generation rule. This must be done last so
  86. // that multiple output pair information is available.
  87. this->WriteTargetDependRules();
  88. // close the streams
  89. this->CloseFileStreams();
  90. }
  91. void cmMakefileLibraryTargetGenerator::WriteObjectLibraryRules()
  92. {
  93. std::vector<std::string> commands;
  94. std::vector<std::string> depends;
  95. // Add post-build rules.
  96. this->LocalGenerator->AppendCustomCommands(
  97. commands, this->GeneratorTarget->GetPostBuildCommands(),
  98. this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
  99. // Depend on the object files.
  100. this->AppendObjectDepends(depends);
  101. // Write the rule.
  102. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, CM_NULLPTR,
  103. this->GeneratorTarget->GetName(),
  104. depends, commands, true);
  105. // Write the main driver rule to build everything in this target.
  106. this->WriteTargetDriverRule(this->GeneratorTarget->GetName(), false);
  107. }
  108. void cmMakefileLibraryTargetGenerator::WriteStaticLibraryRules()
  109. {
  110. const std::string cuda_lang("CUDA");
  111. cmGeneratorTarget::LinkClosure const* closure =
  112. this->GeneratorTarget->GetLinkClosure(this->ConfigName);
  113. const bool hasCUDA =
  114. (std::find(closure->Languages.begin(), closure->Languages.end(),
  115. cuda_lang) != closure->Languages.end());
  116. const bool resolveDeviceSymbols =
  117. this->GeneratorTarget->GetPropertyAsBool("CUDA_RESOLVE_DEVICE_SYMBOLS");
  118. if (hasCUDA && resolveDeviceSymbols) {
  119. std::string linkRuleVar = "CMAKE_CUDA_DEVICE_LINK_LIBRARY";
  120. std::string extraFlags;
  121. this->LocalGenerator->AppendFlags(
  122. extraFlags, this->GeneratorTarget->GetProperty("LINK_FLAGS"));
  123. this->WriteDeviceLibraryRules(linkRuleVar, extraFlags, false);
  124. }
  125. std::string linkLanguage =
  126. this->GeneratorTarget->GetLinkerLanguage(this->ConfigName);
  127. std::string linkRuleVar = this->GeneratorTarget->GetCreateRuleVariable(
  128. linkLanguage, this->ConfigName);
  129. std::string extraFlags;
  130. this->LocalGenerator->GetStaticLibraryFlags(
  131. extraFlags, cmSystemTools::UpperCase(this->ConfigName),
  132. this->GeneratorTarget);
  133. this->WriteLibraryRules(linkRuleVar, extraFlags, false);
  134. }
  135. void cmMakefileLibraryTargetGenerator::WriteSharedLibraryRules(bool relink)
  136. {
  137. if (this->GeneratorTarget->IsFrameworkOnApple()) {
  138. this->WriteFrameworkRules(relink);
  139. return;
  140. }
  141. if (!relink) {
  142. const std::string cuda_lang("CUDA");
  143. cmGeneratorTarget::LinkClosure const* closure =
  144. this->GeneratorTarget->GetLinkClosure(this->ConfigName);
  145. const bool hasCUDA =
  146. (std::find(closure->Languages.begin(), closure->Languages.end(),
  147. cuda_lang) != closure->Languages.end());
  148. if (hasCUDA) {
  149. std::string linkRuleVar = "CMAKE_CUDA_DEVICE_LINK_LIBRARY";
  150. std::string extraFlags;
  151. this->LocalGenerator->AppendFlags(
  152. extraFlags, this->GeneratorTarget->GetProperty("LINK_FLAGS"));
  153. this->WriteDeviceLibraryRules(linkRuleVar, extraFlags, relink);
  154. }
  155. }
  156. std::string linkLanguage =
  157. this->GeneratorTarget->GetLinkerLanguage(this->ConfigName);
  158. std::string linkRuleVar = "CMAKE_";
  159. linkRuleVar += linkLanguage;
  160. linkRuleVar += "_CREATE_SHARED_LIBRARY";
  161. std::string extraFlags;
  162. this->LocalGenerator->AppendFlags(
  163. extraFlags, this->GeneratorTarget->GetProperty("LINK_FLAGS"));
  164. std::string linkFlagsConfig = "LINK_FLAGS_";
  165. linkFlagsConfig += cmSystemTools::UpperCase(this->ConfigName);
  166. this->LocalGenerator->AppendFlags(
  167. extraFlags, this->GeneratorTarget->GetProperty(linkFlagsConfig));
  168. this->LocalGenerator->AddConfigVariableFlags(
  169. extraFlags, "CMAKE_SHARED_LINKER_FLAGS", this->ConfigName);
  170. CM_AUTO_PTR<cmLinkLineComputer> linkLineComputer(
  171. this->CreateLinkLineComputer(
  172. this->LocalGenerator,
  173. this->LocalGenerator->GetStateSnapshot().GetDirectory()));
  174. this->AddModuleDefinitionFlag(linkLineComputer.get(), extraFlags);
  175. if (this->GeneratorTarget->GetPropertyAsBool("LINK_WHAT_YOU_USE")) {
  176. this->LocalGenerator->AppendFlags(extraFlags, " -Wl,--no-as-needed");
  177. }
  178. this->WriteLibraryRules(linkRuleVar, extraFlags, relink);
  179. }
  180. void cmMakefileLibraryTargetGenerator::WriteModuleLibraryRules(bool relink)
  181. {
  182. if (!relink) {
  183. const std::string cuda_lang("CUDA");
  184. cmGeneratorTarget::LinkClosure const* closure =
  185. this->GeneratorTarget->GetLinkClosure(this->ConfigName);
  186. const bool hasCUDA =
  187. (std::find(closure->Languages.begin(), closure->Languages.end(),
  188. cuda_lang) != closure->Languages.end());
  189. if (hasCUDA) {
  190. std::string linkRuleVar = "CMAKE_CUDA_DEVICE_LINK_LIBRARY";
  191. std::string extraFlags;
  192. this->LocalGenerator->AppendFlags(
  193. extraFlags, this->GeneratorTarget->GetProperty("LINK_FLAGS"));
  194. this->WriteDeviceLibraryRules(linkRuleVar, extraFlags, relink);
  195. }
  196. }
  197. std::string linkLanguage =
  198. this->GeneratorTarget->GetLinkerLanguage(this->ConfigName);
  199. std::string linkRuleVar = "CMAKE_";
  200. linkRuleVar += linkLanguage;
  201. linkRuleVar += "_CREATE_SHARED_MODULE";
  202. std::string extraFlags;
  203. this->LocalGenerator->AppendFlags(
  204. extraFlags, this->GeneratorTarget->GetProperty("LINK_FLAGS"));
  205. std::string linkFlagsConfig = "LINK_FLAGS_";
  206. linkFlagsConfig += cmSystemTools::UpperCase(this->ConfigName);
  207. this->LocalGenerator->AppendFlags(
  208. extraFlags, this->GeneratorTarget->GetProperty(linkFlagsConfig));
  209. this->LocalGenerator->AddConfigVariableFlags(
  210. extraFlags, "CMAKE_MODULE_LINKER_FLAGS", this->ConfigName);
  211. CM_AUTO_PTR<cmLinkLineComputer> linkLineComputer(
  212. this->CreateLinkLineComputer(
  213. this->LocalGenerator,
  214. this->LocalGenerator->GetStateSnapshot().GetDirectory()));
  215. this->AddModuleDefinitionFlag(linkLineComputer.get(), extraFlags);
  216. this->WriteLibraryRules(linkRuleVar, extraFlags, relink);
  217. }
  218. void cmMakefileLibraryTargetGenerator::WriteFrameworkRules(bool relink)
  219. {
  220. std::string linkLanguage =
  221. this->GeneratorTarget->GetLinkerLanguage(this->ConfigName);
  222. std::string linkRuleVar = "CMAKE_";
  223. linkRuleVar += linkLanguage;
  224. linkRuleVar += "_CREATE_MACOSX_FRAMEWORK";
  225. std::string extraFlags;
  226. this->LocalGenerator->AppendFlags(
  227. extraFlags, this->GeneratorTarget->GetProperty("LINK_FLAGS"));
  228. std::string linkFlagsConfig = "LINK_FLAGS_";
  229. linkFlagsConfig += cmSystemTools::UpperCase(this->ConfigName);
  230. this->LocalGenerator->AppendFlags(
  231. extraFlags, this->GeneratorTarget->GetProperty(linkFlagsConfig));
  232. this->LocalGenerator->AddConfigVariableFlags(
  233. extraFlags, "CMAKE_MACOSX_FRAMEWORK_LINKER_FLAGS", this->ConfigName);
  234. this->WriteLibraryRules(linkRuleVar, extraFlags, relink);
  235. }
  236. void cmMakefileLibraryTargetGenerator::WriteDeviceLibraryRules(
  237. const std::string& linkRuleVar, const std::string& extraFlags, bool relink)
  238. {
  239. #ifdef CMAKE_BUILD_WITH_CMAKE
  240. // TODO: Merge the methods that call this method to avoid
  241. // code duplication.
  242. std::vector<std::string> commands;
  243. // Build list of dependencies.
  244. std::vector<std::string> depends;
  245. this->AppendLinkDepends(depends);
  246. // Get the language to use for linking this library.
  247. std::string linkLanguage = "CUDA";
  248. std::string const objExt =
  249. this->Makefile->GetSafeDefinition("CMAKE_CUDA_OUTPUT_EXTENSION");
  250. // Create set of linking flags.
  251. std::string linkFlags;
  252. this->LocalGenerator->AppendFlags(linkFlags, extraFlags);
  253. // Get the name of the device object to generate.
  254. std::string const targetOutputReal =
  255. this->GeneratorTarget->ObjectDirectory + "cmake_device_link" + objExt;
  256. this->DeviceLinkObject = targetOutputReal;
  257. this->NumberOfProgressActions++;
  258. if (!this->NoRuleMessages) {
  259. cmLocalUnixMakefileGenerator3::EchoProgress progress;
  260. this->MakeEchoProgress(progress);
  261. // Add the link message.
  262. std::string buildEcho = "Linking " + linkLanguage + " device code ";
  263. buildEcho += this->LocalGenerator->ConvertToOutputFormat(
  264. this->LocalGenerator->MaybeConvertToRelativePath(
  265. this->LocalGenerator->GetCurrentBinaryDirectory(),
  266. this->DeviceLinkObject),
  267. cmOutputConverter::SHELL);
  268. this->LocalGenerator->AppendEcho(
  269. commands, buildEcho, cmLocalUnixMakefileGenerator3::EchoLink, &progress);
  270. }
  271. // Clean files associated with this library.
  272. std::vector<std::string> libCleanFiles;
  273. libCleanFiles.push_back(this->LocalGenerator->MaybeConvertToRelativePath(
  274. this->LocalGenerator->GetCurrentBinaryDirectory(), targetOutputReal));
  275. // Determine whether a link script will be used.
  276. bool useLinkScript = this->GlobalGenerator->GetUseLinkScript();
  277. bool useResponseFileForObjects =
  278. this->CheckUseResponseFileForObjects(linkLanguage);
  279. bool const useResponseFileForLibs =
  280. this->CheckUseResponseFileForLibraries(linkLanguage);
  281. cmRulePlaceholderExpander::RuleVariables vars;
  282. vars.Language = linkLanguage.c_str();
  283. // Expand the rule variables.
  284. std::vector<std::string> real_link_commands;
  285. {
  286. bool useWatcomQuote =
  287. this->Makefile->IsOn(linkRuleVar + "_USE_WATCOM_QUOTE");
  288. // Set path conversion for link script shells.
  289. this->LocalGenerator->SetLinkScriptShell(useLinkScript);
  290. // Collect up flags to link in needed libraries.
  291. std::string linkLibs;
  292. if (this->GeneratorTarget->GetType() != cmStateEnums::STATIC_LIBRARY) {
  293. CM_AUTO_PTR<cmLinkLineComputer> linkLineComputer(
  294. new cmLinkLineDeviceComputer(
  295. this->LocalGenerator,
  296. this->LocalGenerator->GetStateSnapshot().GetDirectory()));
  297. linkLineComputer->SetForResponse(useResponseFileForLibs);
  298. linkLineComputer->SetUseWatcomQuote(useWatcomQuote);
  299. linkLineComputer->SetRelink(relink);
  300. this->CreateLinkLibs(linkLineComputer.get(), linkLibs,
  301. useResponseFileForLibs, depends);
  302. }
  303. // Construct object file lists that may be needed to expand the
  304. // rule.
  305. std::string buildObjs;
  306. this->CreateObjectLists(useLinkScript, false, // useArchiveRules
  307. useResponseFileForObjects, buildObjs, depends,
  308. useWatcomQuote);
  309. cmOutputConverter::OutputFormat output = (useWatcomQuote)
  310. ? cmOutputConverter::WATCOMQUOTE
  311. : cmOutputConverter::SHELL;
  312. std::string objectDir = this->GeneratorTarget->GetSupportDirectory();
  313. objectDir = this->LocalGenerator->ConvertToOutputFormat(
  314. this->LocalGenerator->MaybeConvertToRelativePath(
  315. this->LocalGenerator->GetCurrentBinaryDirectory(), objectDir),
  316. cmOutputConverter::SHELL);
  317. std::string target = this->LocalGenerator->ConvertToOutputFormat(
  318. this->LocalGenerator->MaybeConvertToRelativePath(
  319. this->LocalGenerator->GetCurrentBinaryDirectory(), targetOutputReal),
  320. output);
  321. std::string targetFullPathCompilePDB = this->ComputeTargetCompilePDB();
  322. std::string targetOutPathCompilePDB =
  323. this->LocalGenerator->ConvertToOutputFormat(targetFullPathCompilePDB,
  324. cmOutputConverter::SHELL);
  325. vars.Objects = buildObjs.c_str();
  326. vars.ObjectDir = objectDir.c_str();
  327. vars.Target = target.c_str();
  328. vars.LinkLibraries = linkLibs.c_str();
  329. vars.ObjectsQuoted = buildObjs.c_str();
  330. vars.LinkFlags = linkFlags.c_str();
  331. vars.TargetCompilePDB = targetOutPathCompilePDB.c_str();
  332. // Add language feature flags.
  333. std::string langFlags;
  334. this->AddFeatureFlags(langFlags, linkLanguage);
  335. vars.LanguageCompileFlags = langFlags.c_str();
  336. std::string launcher;
  337. const char* val = this->LocalGenerator->GetRuleLauncher(
  338. this->GeneratorTarget, "RULE_LAUNCH_LINK");
  339. if (val && *val) {
  340. launcher = val;
  341. launcher += " ";
  342. }
  343. CM_AUTO_PTR<cmRulePlaceholderExpander> rulePlaceholderExpander(
  344. this->LocalGenerator->CreateRulePlaceholderExpander());
  345. // Construct the main link rule and expand placeholders.
  346. rulePlaceholderExpander->SetTargetImpLib(targetOutputReal);
  347. std::string linkRule = this->GetLinkRule(linkRuleVar);
  348. cmSystemTools::ExpandListArgument(linkRule, real_link_commands);
  349. // Expand placeholders.
  350. for (std::vector<std::string>::iterator i = real_link_commands.begin();
  351. i != real_link_commands.end(); ++i) {
  352. *i = launcher + *i;
  353. rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator, *i,
  354. vars);
  355. }
  356. // Restore path conversion to normal shells.
  357. this->LocalGenerator->SetLinkScriptShell(false);
  358. // Clean all the possible library names and symlinks.
  359. this->CleanFiles.insert(this->CleanFiles.end(), libCleanFiles.begin(),
  360. libCleanFiles.end());
  361. }
  362. std::vector<std::string> commands1;
  363. // Optionally convert the build rule to use a script to avoid long
  364. // command lines in the make shell.
  365. if (useLinkScript) {
  366. // Use a link script.
  367. const char* name = (relink ? "drelink.txt" : "dlink.txt");
  368. this->CreateLinkScript(name, real_link_commands, commands1, depends);
  369. } else {
  370. // No link script. Just use the link rule directly.
  371. commands1 = real_link_commands;
  372. }
  373. this->LocalGenerator->CreateCDCommand(
  374. commands1, this->Makefile->GetCurrentBinaryDirectory(),
  375. this->LocalGenerator->GetBinaryDirectory());
  376. commands.insert(commands.end(), commands1.begin(), commands1.end());
  377. commands1.clear();
  378. // Compute the list of outputs.
  379. std::vector<std::string> outputs(1, targetOutputReal);
  380. // Write the build rule.
  381. this->WriteMakeRule(*this->BuildFileStream, CM_NULLPTR, outputs, depends,
  382. commands, false);
  383. // Write the main driver rule to build everything in this target.
  384. this->WriteTargetDriverRule(targetOutputReal, relink);
  385. #else
  386. static_cast<void>(linkRuleVar);
  387. static_cast<void>(extraFlags);
  388. static_cast<void>(relink);
  389. #endif
  390. }
  391. void cmMakefileLibraryTargetGenerator::WriteLibraryRules(
  392. const std::string& linkRuleVar, const std::string& extraFlags, bool relink)
  393. {
  394. // TODO: Merge the methods that call this method to avoid
  395. // code duplication.
  396. std::vector<std::string> commands;
  397. // Build list of dependencies.
  398. std::vector<std::string> depends;
  399. this->AppendLinkDepends(depends);
  400. if (!this->DeviceLinkObject.empty()) {
  401. depends.push_back(this->DeviceLinkObject);
  402. }
  403. // Get the language to use for linking this library.
  404. std::string linkLanguage =
  405. this->GeneratorTarget->GetLinkerLanguage(this->ConfigName);
  406. // Make sure we have a link language.
  407. if (linkLanguage.empty()) {
  408. cmSystemTools::Error("Cannot determine link language for target \"",
  409. this->GeneratorTarget->GetName().c_str(), "\".");
  410. return;
  411. }
  412. // Create set of linking flags.
  413. std::string linkFlags;
  414. this->LocalGenerator->AppendFlags(linkFlags, extraFlags);
  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.c_str());
  446. outpath += "/";
  447. if (!targetNameImport.empty()) {
  448. outpathImp = outpath;
  449. }
  450. } else {
  451. outpath = this->GeneratorTarget->GetDirectory(this->ConfigName);
  452. cmSystemTools::MakeDirectory(outpath.c_str());
  453. outpath += "/";
  454. if (!targetNameImport.empty()) {
  455. outpathImp = this->GeneratorTarget->GetDirectory(
  456. this->ConfigName, cmStateEnums::ImportLibraryArtifact);
  457. cmSystemTools::MakeDirectory(outpathImp.c_str());
  458. outpathImp += "/";
  459. }
  460. }
  461. std::string compilePdbOutputPath =
  462. this->GeneratorTarget->GetCompilePDBDirectory(this->ConfigName);
  463. cmSystemTools::MakeDirectory(compilePdbOutputPath.c_str());
  464. std::string pdbOutputPath =
  465. this->GeneratorTarget->GetPDBDirectory(this->ConfigName);
  466. cmSystemTools::MakeDirectory(pdbOutputPath.c_str());
  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, 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, 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, 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. CM_AUTO_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 feature flags.
  732. std::string langFlags;
  733. this->AddFeatureFlags(langFlags, linkLanguage);
  734. this->LocalGenerator->AddArchitectureFlags(
  735. langFlags, this->GeneratorTarget, linkLanguage, this->ConfigName);
  736. vars.LanguageCompileFlags = langFlags.c_str();
  737. std::string launcher;
  738. const char* val = this->LocalGenerator->GetRuleLauncher(
  739. this->GeneratorTarget, "RULE_LAUNCH_LINK");
  740. if (val && *val) {
  741. launcher = val;
  742. launcher += " ";
  743. }
  744. CM_AUTO_PTR<cmRulePlaceholderExpander> rulePlaceholderExpander(
  745. this->LocalGenerator->CreateRulePlaceholderExpander());
  746. // Construct the main link rule and expand placeholders.
  747. rulePlaceholderExpander->SetTargetImpLib(targetOutPathImport);
  748. if (useArchiveRules) {
  749. // Construct the individual object list strings.
  750. std::vector<std::string> object_strings;
  751. this->WriteObjectsStrings(object_strings, archiveCommandLimit);
  752. // Add the cuda device object to the list of archive files. This will
  753. // only occur on archives which have CUDA_RESOLVE_DEVICE_SYMBOLS enabled
  754. if (!this->DeviceLinkObject.empty()) {
  755. object_strings.push_back(this->LocalGenerator->ConvertToOutputFormat(
  756. this->LocalGenerator->MaybeConvertToRelativePath(
  757. this->LocalGenerator->GetCurrentBinaryDirectory(),
  758. this->DeviceLinkObject),
  759. cmOutputConverter::SHELL));
  760. }
  761. // Create the archive with the first set of objects.
  762. std::vector<std::string>::iterator osi = object_strings.begin();
  763. {
  764. vars.Objects = osi->c_str();
  765. for (std::vector<std::string>::const_iterator i =
  766. archiveCreateCommands.begin();
  767. i != archiveCreateCommands.end(); ++i) {
  768. std::string cmd = launcher + *i;
  769. rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator,
  770. cmd, vars);
  771. real_link_commands.push_back(cmd);
  772. }
  773. }
  774. // Append to the archive with the other object sets.
  775. for (++osi; osi != object_strings.end(); ++osi) {
  776. vars.Objects = osi->c_str();
  777. for (std::vector<std::string>::const_iterator i =
  778. archiveAppendCommands.begin();
  779. i != archiveAppendCommands.end(); ++i) {
  780. std::string cmd = launcher + *i;
  781. rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator,
  782. cmd, vars);
  783. real_link_commands.push_back(cmd);
  784. }
  785. }
  786. // Finish the archive.
  787. vars.Objects = "";
  788. for (std::vector<std::string>::const_iterator i =
  789. archiveFinishCommands.begin();
  790. i != archiveFinishCommands.end(); ++i) {
  791. std::string cmd = launcher + *i;
  792. rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator, cmd,
  793. vars);
  794. // If there is no ranlib the command will be ":". Skip it.
  795. if (!cmd.empty() && cmd[0] != ':') {
  796. real_link_commands.push_back(cmd);
  797. }
  798. }
  799. } else {
  800. // Get the set of commands.
  801. std::string linkRule = this->GetLinkRule(linkRuleVar);
  802. cmSystemTools::ExpandListArgument(linkRule, real_link_commands);
  803. if (this->GeneratorTarget->GetPropertyAsBool("LINK_WHAT_YOU_USE") &&
  804. (this->GeneratorTarget->GetType() == cmStateEnums::SHARED_LIBRARY)) {
  805. std::string cmakeCommand = this->LocalGenerator->ConvertToOutputFormat(
  806. cmSystemTools::GetCMakeCommand(), cmLocalGenerator::SHELL);
  807. cmakeCommand += " -E __run_iwyu --lwyu=";
  808. cmakeCommand += targetOutPathReal;
  809. real_link_commands.push_back(cmakeCommand);
  810. }
  811. // Expand placeholders.
  812. for (std::vector<std::string>::iterator i = real_link_commands.begin();
  813. i != real_link_commands.end(); ++i) {
  814. *i = launcher + *i;
  815. rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator, *i,
  816. vars);
  817. }
  818. }
  819. // Restore path conversion to normal shells.
  820. this->LocalGenerator->SetLinkScriptShell(false);
  821. }
  822. // Optionally convert the build rule to use a script to avoid long
  823. // command lines in the make shell.
  824. if (useLinkScript) {
  825. // Use a link script.
  826. const char* name = (relink ? "relink.txt" : "link.txt");
  827. this->CreateLinkScript(name, real_link_commands, commands1, depends);
  828. } else {
  829. // No link script. Just use the link rule directly.
  830. commands1 = real_link_commands;
  831. }
  832. this->LocalGenerator->CreateCDCommand(
  833. commands1, this->Makefile->GetCurrentBinaryDirectory(),
  834. this->LocalGenerator->GetBinaryDirectory());
  835. commands.insert(commands.end(), commands1.begin(), commands1.end());
  836. commands1.clear();
  837. // Add a rule to create necessary symlinks for the library.
  838. // Frameworks are handled by cmOSXBundleGenerator.
  839. if (targetOutPath != targetOutPathReal &&
  840. !this->GeneratorTarget->IsFrameworkOnApple()) {
  841. std::string symlink = "$(CMAKE_COMMAND) -E cmake_symlink_library ";
  842. symlink += targetOutPathReal;
  843. symlink += " ";
  844. symlink += targetOutPathSO;
  845. symlink += " ";
  846. symlink += targetOutPath;
  847. commands1.push_back(symlink);
  848. this->LocalGenerator->CreateCDCommand(
  849. commands1, this->Makefile->GetCurrentBinaryDirectory(),
  850. this->LocalGenerator->GetBinaryDirectory());
  851. commands.insert(commands.end(), commands1.begin(), commands1.end());
  852. commands1.clear();
  853. }
  854. // Add the post-build rules when building but not when relinking.
  855. if (!relink) {
  856. this->LocalGenerator->AppendCustomCommands(
  857. commands, this->GeneratorTarget->GetPostBuildCommands(),
  858. this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
  859. }
  860. // Compute the list of outputs.
  861. std::vector<std::string> outputs(1, targetFullPathReal);
  862. if (targetNameSO != targetNameReal) {
  863. outputs.push_back(targetFullPathSO);
  864. }
  865. if (targetName != targetNameSO && targetName != targetNameReal) {
  866. outputs.push_back(targetFullPath);
  867. }
  868. // Write the build rule.
  869. this->WriteMakeRule(*this->BuildFileStream, CM_NULLPTR, outputs, depends,
  870. commands, false);
  871. // Write the main driver rule to build everything in this target.
  872. this->WriteTargetDriverRule(targetFullPath, relink);
  873. // Clean all the possible library names and symlinks.
  874. this->CleanFiles.insert(this->CleanFiles.end(), libCleanFiles.begin(),
  875. libCleanFiles.end());
  876. }