cmMakefileLibraryTargetGenerator.cxx 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  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, 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-specific flags.
  333. std::string langFlags;
  334. this->LocalGenerator->AddLanguageFlagsForLinking(
  335. langFlags, this->GeneratorTarget, linkLanguage, this->ConfigName);
  336. vars.LanguageCompileFlags = langFlags.c_str();
  337. std::string launcher;
  338. const char* val = this->LocalGenerator->GetRuleLauncher(
  339. this->GeneratorTarget, "RULE_LAUNCH_LINK");
  340. if (val && *val) {
  341. launcher = val;
  342. launcher += " ";
  343. }
  344. CM_AUTO_PTR<cmRulePlaceholderExpander> rulePlaceholderExpander(
  345. this->LocalGenerator->CreateRulePlaceholderExpander());
  346. // Construct the main link rule and expand placeholders.
  347. rulePlaceholderExpander->SetTargetImpLib(targetOutputReal);
  348. std::string linkRule = this->GetLinkRule(linkRuleVar);
  349. cmSystemTools::ExpandListArgument(linkRule, real_link_commands);
  350. // Expand placeholders.
  351. for (std::vector<std::string>::iterator i = real_link_commands.begin();
  352. i != real_link_commands.end(); ++i) {
  353. *i = launcher + *i;
  354. rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator, *i,
  355. vars);
  356. }
  357. // Restore path conversion to normal shells.
  358. this->LocalGenerator->SetLinkScriptShell(false);
  359. // Clean all the possible library names and symlinks.
  360. this->CleanFiles.insert(this->CleanFiles.end(), libCleanFiles.begin(),
  361. libCleanFiles.end());
  362. }
  363. std::vector<std::string> commands1;
  364. // Optionally convert the build rule to use a script to avoid long
  365. // command lines in the make shell.
  366. if (useLinkScript) {
  367. // Use a link script.
  368. const char* name = (relink ? "drelink.txt" : "dlink.txt");
  369. this->CreateLinkScript(name, real_link_commands, commands1, depends);
  370. } else {
  371. // No link script. Just use the link rule directly.
  372. commands1 = real_link_commands;
  373. }
  374. this->LocalGenerator->CreateCDCommand(
  375. commands1, this->Makefile->GetCurrentBinaryDirectory(),
  376. this->LocalGenerator->GetBinaryDirectory());
  377. commands.insert(commands.end(), commands1.begin(), commands1.end());
  378. commands1.clear();
  379. // Compute the list of outputs.
  380. std::vector<std::string> outputs(1, targetOutputReal);
  381. // Write the build rule.
  382. this->WriteMakeRule(*this->BuildFileStream, nullptr, outputs, depends,
  383. commands, false);
  384. // Write the main driver rule to build everything in this target.
  385. this->WriteTargetDriverRule(targetOutputReal, relink);
  386. #else
  387. static_cast<void>(linkRuleVar);
  388. static_cast<void>(extraFlags);
  389. static_cast<void>(relink);
  390. #endif
  391. }
  392. void cmMakefileLibraryTargetGenerator::WriteLibraryRules(
  393. const std::string& linkRuleVar, const std::string& extraFlags, bool relink)
  394. {
  395. // TODO: Merge the methods that call this method to avoid
  396. // code duplication.
  397. std::vector<std::string> commands;
  398. // Build list of dependencies.
  399. std::vector<std::string> depends;
  400. this->AppendLinkDepends(depends);
  401. if (!this->DeviceLinkObject.empty()) {
  402. depends.push_back(this->DeviceLinkObject);
  403. }
  404. // Get the language to use for linking this library.
  405. std::string linkLanguage =
  406. this->GeneratorTarget->GetLinkerLanguage(this->ConfigName);
  407. // Make sure we have a link language.
  408. if (linkLanguage.empty()) {
  409. cmSystemTools::Error("Cannot determine link language for target \"",
  410. this->GeneratorTarget->GetName().c_str(), "\".");
  411. return;
  412. }
  413. // Create set of linking flags.
  414. std::string linkFlags;
  415. this->LocalGenerator->AppendFlags(linkFlags, extraFlags);
  416. this->LocalGenerator->AppendIPOLinkerFlags(linkFlags, this->GeneratorTarget,
  417. this->ConfigName, linkLanguage);
  418. // Add OSX version flags, if any.
  419. if (this->GeneratorTarget->GetType() == cmStateEnums::SHARED_LIBRARY ||
  420. this->GeneratorTarget->GetType() == cmStateEnums::MODULE_LIBRARY) {
  421. this->AppendOSXVerFlag(linkFlags, linkLanguage, "COMPATIBILITY", true);
  422. this->AppendOSXVerFlag(linkFlags, linkLanguage, "CURRENT", false);
  423. }
  424. // Construct the name of the library.
  425. std::string targetName;
  426. std::string targetNameSO;
  427. std::string targetNameReal;
  428. std::string targetNameImport;
  429. std::string targetNamePDB;
  430. this->GeneratorTarget->GetLibraryNames(targetName, targetNameSO,
  431. targetNameReal, targetNameImport,
  432. targetNamePDB, this->ConfigName);
  433. // Construct the full path version of the names.
  434. std::string outpath;
  435. std::string outpathImp;
  436. if (this->GeneratorTarget->IsFrameworkOnApple()) {
  437. outpath = this->GeneratorTarget->GetDirectory(this->ConfigName);
  438. this->OSXBundleGenerator->CreateFramework(targetName, outpath);
  439. outpath += "/";
  440. } else if (this->GeneratorTarget->IsCFBundleOnApple()) {
  441. outpath = this->GeneratorTarget->GetDirectory(this->ConfigName);
  442. this->OSXBundleGenerator->CreateCFBundle(targetName, outpath);
  443. outpath += "/";
  444. } else if (relink) {
  445. outpath = this->Makefile->GetCurrentBinaryDirectory();
  446. outpath += cmake::GetCMakeFilesDirectory();
  447. outpath += "/CMakeRelink.dir";
  448. cmSystemTools::MakeDirectory(outpath.c_str());
  449. outpath += "/";
  450. if (!targetNameImport.empty()) {
  451. outpathImp = outpath;
  452. }
  453. } else {
  454. outpath = this->GeneratorTarget->GetDirectory(this->ConfigName);
  455. cmSystemTools::MakeDirectory(outpath.c_str());
  456. outpath += "/";
  457. if (!targetNameImport.empty()) {
  458. outpathImp = this->GeneratorTarget->GetDirectory(
  459. this->ConfigName, cmStateEnums::ImportLibraryArtifact);
  460. cmSystemTools::MakeDirectory(outpathImp.c_str());
  461. outpathImp += "/";
  462. }
  463. }
  464. std::string compilePdbOutputPath =
  465. this->GeneratorTarget->GetCompilePDBDirectory(this->ConfigName);
  466. cmSystemTools::MakeDirectory(compilePdbOutputPath.c_str());
  467. std::string pdbOutputPath =
  468. this->GeneratorTarget->GetPDBDirectory(this->ConfigName);
  469. cmSystemTools::MakeDirectory(pdbOutputPath.c_str());
  470. pdbOutputPath += "/";
  471. std::string targetFullPath = outpath + targetName;
  472. std::string targetFullPathPDB = pdbOutputPath + targetNamePDB;
  473. std::string targetFullPathSO = outpath + targetNameSO;
  474. std::string targetFullPathReal = outpath + targetNameReal;
  475. std::string targetFullPathImport = outpathImp + targetNameImport;
  476. // Construct the output path version of the names for use in command
  477. // arguments.
  478. std::string targetOutPathPDB = this->LocalGenerator->ConvertToOutputFormat(
  479. targetFullPathPDB, cmOutputConverter::SHELL);
  480. std::string targetOutPath = this->LocalGenerator->ConvertToOutputFormat(
  481. this->LocalGenerator->MaybeConvertToRelativePath(
  482. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPath),
  483. cmOutputConverter::SHELL);
  484. std::string targetOutPathSO = this->LocalGenerator->ConvertToOutputFormat(
  485. this->LocalGenerator->MaybeConvertToRelativePath(
  486. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPathSO),
  487. cmOutputConverter::SHELL);
  488. std::string targetOutPathReal = this->LocalGenerator->ConvertToOutputFormat(
  489. this->LocalGenerator->MaybeConvertToRelativePath(
  490. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPathReal),
  491. cmOutputConverter::SHELL);
  492. std::string targetOutPathImport =
  493. this->LocalGenerator->ConvertToOutputFormat(
  494. this->LocalGenerator->MaybeConvertToRelativePath(
  495. this->LocalGenerator->GetCurrentBinaryDirectory(),
  496. targetFullPathImport),
  497. cmOutputConverter::SHELL);
  498. this->NumberOfProgressActions++;
  499. if (!this->NoRuleMessages) {
  500. cmLocalUnixMakefileGenerator3::EchoProgress progress;
  501. this->MakeEchoProgress(progress);
  502. // Add the link message.
  503. std::string buildEcho = "Linking ";
  504. buildEcho += linkLanguage;
  505. switch (this->GeneratorTarget->GetType()) {
  506. case cmStateEnums::STATIC_LIBRARY:
  507. buildEcho += " static library ";
  508. break;
  509. case cmStateEnums::SHARED_LIBRARY:
  510. buildEcho += " shared library ";
  511. break;
  512. case cmStateEnums::MODULE_LIBRARY:
  513. if (this->GeneratorTarget->IsCFBundleOnApple()) {
  514. buildEcho += " CFBundle";
  515. }
  516. buildEcho += " shared module ";
  517. break;
  518. default:
  519. buildEcho += " library ";
  520. break;
  521. }
  522. buildEcho += targetOutPath;
  523. this->LocalGenerator->AppendEcho(
  524. commands, buildEcho, cmLocalUnixMakefileGenerator3::EchoLink, &progress);
  525. }
  526. // Clean files associated with this library.
  527. std::vector<std::string> libCleanFiles;
  528. libCleanFiles.push_back(this->LocalGenerator->MaybeConvertToRelativePath(
  529. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPathReal));
  530. std::vector<std::string> commands1;
  531. // Add a command to remove any existing files for this library.
  532. // for static libs only
  533. if (this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY) {
  534. this->LocalGenerator->AppendCleanCommand(commands1, libCleanFiles,
  535. this->GeneratorTarget, "target");
  536. this->LocalGenerator->CreateCDCommand(
  537. commands1, this->Makefile->GetCurrentBinaryDirectory(),
  538. this->LocalGenerator->GetBinaryDirectory());
  539. commands.insert(commands.end(), commands1.begin(), commands1.end());
  540. commands1.clear();
  541. }
  542. if (targetName != targetNameReal) {
  543. libCleanFiles.push_back(this->LocalGenerator->MaybeConvertToRelativePath(
  544. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPath));
  545. }
  546. if (targetNameSO != targetNameReal && targetNameSO != targetName) {
  547. libCleanFiles.push_back(this->LocalGenerator->MaybeConvertToRelativePath(
  548. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPathSO));
  549. }
  550. if (!targetNameImport.empty()) {
  551. libCleanFiles.push_back(this->LocalGenerator->MaybeConvertToRelativePath(
  552. this->LocalGenerator->GetCurrentBinaryDirectory(),
  553. targetFullPathImport));
  554. std::string implib;
  555. if (this->GeneratorTarget->GetImplibGNUtoMS(targetFullPathImport,
  556. implib)) {
  557. libCleanFiles.push_back(this->LocalGenerator->MaybeConvertToRelativePath(
  558. this->LocalGenerator->GetCurrentBinaryDirectory(), implib));
  559. }
  560. }
  561. // List the PDB for cleaning only when the whole target is
  562. // cleaned. We do not want to delete the .pdb file just before
  563. // linking the target.
  564. this->CleanFiles.push_back(this->LocalGenerator->MaybeConvertToRelativePath(
  565. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPathPDB));
  566. #ifdef _WIN32
  567. // There may be a manifest file for this target. Add it to the
  568. // clean set just in case.
  569. if (this->GeneratorTarget->GetType() != cmStateEnums::STATIC_LIBRARY) {
  570. libCleanFiles.push_back(this->LocalGenerator->MaybeConvertToRelativePath(
  571. this->LocalGenerator->GetCurrentBinaryDirectory(),
  572. (targetFullPath + ".manifest").c_str()));
  573. }
  574. #endif
  575. // Add the pre-build and pre-link rules building but not when relinking.
  576. if (!relink) {
  577. this->LocalGenerator->AppendCustomCommands(
  578. commands, this->GeneratorTarget->GetPreBuildCommands(),
  579. this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
  580. this->LocalGenerator->AppendCustomCommands(
  581. commands, this->GeneratorTarget->GetPreLinkCommands(),
  582. this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
  583. }
  584. // Determine whether a link script will be used.
  585. bool useLinkScript = this->GlobalGenerator->GetUseLinkScript();
  586. bool useResponseFileForObjects =
  587. this->CheckUseResponseFileForObjects(linkLanguage);
  588. bool const useResponseFileForLibs =
  589. this->CheckUseResponseFileForLibraries(linkLanguage);
  590. // For static libraries there might be archiving rules.
  591. bool haveStaticLibraryRule = false;
  592. std::vector<std::string> archiveCreateCommands;
  593. std::vector<std::string> archiveAppendCommands;
  594. std::vector<std::string> archiveFinishCommands;
  595. std::string::size_type archiveCommandLimit = std::string::npos;
  596. if (this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY) {
  597. haveStaticLibraryRule = this->Makefile->IsDefinitionSet(linkRuleVar);
  598. std::string arCreateVar = "CMAKE_";
  599. arCreateVar += linkLanguage;
  600. arCreateVar += "_ARCHIVE_CREATE";
  601. arCreateVar = this->GeneratorTarget->GetFeatureSpecificLinkRuleVariable(
  602. arCreateVar, linkLanguage, this->ConfigName);
  603. if (const char* rule = this->Makefile->GetDefinition(arCreateVar)) {
  604. cmSystemTools::ExpandListArgument(rule, archiveCreateCommands);
  605. }
  606. std::string arAppendVar = "CMAKE_";
  607. arAppendVar += linkLanguage;
  608. arAppendVar += "_ARCHIVE_APPEND";
  609. arAppendVar = this->GeneratorTarget->GetFeatureSpecificLinkRuleVariable(
  610. arAppendVar, linkLanguage, this->ConfigName);
  611. if (const char* rule = this->Makefile->GetDefinition(arAppendVar)) {
  612. cmSystemTools::ExpandListArgument(rule, archiveAppendCommands);
  613. }
  614. std::string arFinishVar = "CMAKE_";
  615. arFinishVar += linkLanguage;
  616. arFinishVar += "_ARCHIVE_FINISH";
  617. arFinishVar = this->GeneratorTarget->GetFeatureSpecificLinkRuleVariable(
  618. arFinishVar, linkLanguage, this->ConfigName);
  619. if (const char* rule = this->Makefile->GetDefinition(arFinishVar)) {
  620. cmSystemTools::ExpandListArgument(rule, archiveFinishCommands);
  621. }
  622. }
  623. // Decide whether to use archiving rules.
  624. bool useArchiveRules = !haveStaticLibraryRule &&
  625. !archiveCreateCommands.empty() && !archiveAppendCommands.empty();
  626. if (useArchiveRules) {
  627. // Archiving rules are always run with a link script.
  628. useLinkScript = true;
  629. // Archiving rules never use a response file.
  630. useResponseFileForObjects = false;
  631. // Limit the length of individual object lists to less than the
  632. // 32K command line length limit on Windows. We could make this a
  633. // platform file variable but this should work everywhere.
  634. archiveCommandLimit = 30000;
  635. }
  636. // Expand the rule variables.
  637. std::vector<std::string> real_link_commands;
  638. {
  639. bool useWatcomQuote =
  640. this->Makefile->IsOn(linkRuleVar + "_USE_WATCOM_QUOTE");
  641. // Set path conversion for link script shells.
  642. this->LocalGenerator->SetLinkScriptShell(useLinkScript);
  643. // Collect up flags to link in needed libraries.
  644. std::string linkLibs;
  645. if (this->GeneratorTarget->GetType() != cmStateEnums::STATIC_LIBRARY) {
  646. CM_AUTO_PTR<cmLinkLineComputer> linkLineComputer(
  647. this->CreateLinkLineComputer(
  648. this->LocalGenerator,
  649. this->LocalGenerator->GetStateSnapshot().GetDirectory()));
  650. linkLineComputer->SetForResponse(useResponseFileForLibs);
  651. linkLineComputer->SetUseWatcomQuote(useWatcomQuote);
  652. linkLineComputer->SetRelink(relink);
  653. this->CreateLinkLibs(linkLineComputer.get(), linkLibs,
  654. useResponseFileForLibs, depends);
  655. }
  656. // Construct object file lists that may be needed to expand the
  657. // rule.
  658. std::string buildObjs;
  659. this->CreateObjectLists(useLinkScript, useArchiveRules,
  660. useResponseFileForObjects, buildObjs, depends,
  661. useWatcomQuote);
  662. if (!this->DeviceLinkObject.empty()) {
  663. buildObjs += " " +
  664. this->LocalGenerator->ConvertToOutputFormat(
  665. this->LocalGenerator->MaybeConvertToRelativePath(
  666. this->LocalGenerator->GetCurrentBinaryDirectory(),
  667. this->DeviceLinkObject),
  668. cmOutputConverter::SHELL);
  669. }
  670. // maybe create .def file from list of objects
  671. this->GenDefFile(real_link_commands);
  672. std::string manifests = this->GetManifests();
  673. cmRulePlaceholderExpander::RuleVariables vars;
  674. vars.TargetPDB = targetOutPathPDB.c_str();
  675. // Setup the target version.
  676. std::string targetVersionMajor;
  677. std::string targetVersionMinor;
  678. {
  679. std::ostringstream majorStream;
  680. std::ostringstream minorStream;
  681. int major;
  682. int minor;
  683. this->GeneratorTarget->GetTargetVersion(major, minor);
  684. majorStream << major;
  685. minorStream << minor;
  686. targetVersionMajor = majorStream.str();
  687. targetVersionMinor = minorStream.str();
  688. }
  689. vars.TargetVersionMajor = targetVersionMajor.c_str();
  690. vars.TargetVersionMinor = targetVersionMinor.c_str();
  691. vars.CMTargetName = this->GeneratorTarget->GetName().c_str();
  692. vars.CMTargetType =
  693. cmState::GetTargetTypeName(this->GeneratorTarget->GetType());
  694. vars.Language = linkLanguage.c_str();
  695. vars.Objects = buildObjs.c_str();
  696. std::string objectDir = this->GeneratorTarget->GetSupportDirectory();
  697. objectDir = this->LocalGenerator->ConvertToOutputFormat(
  698. this->LocalGenerator->MaybeConvertToRelativePath(
  699. this->LocalGenerator->GetCurrentBinaryDirectory(), objectDir),
  700. cmOutputConverter::SHELL);
  701. vars.ObjectDir = objectDir.c_str();
  702. cmOutputConverter::OutputFormat output = (useWatcomQuote)
  703. ? cmOutputConverter::WATCOMQUOTE
  704. : cmOutputConverter::SHELL;
  705. std::string target = this->LocalGenerator->ConvertToOutputFormat(
  706. this->LocalGenerator->MaybeConvertToRelativePath(
  707. this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPathReal),
  708. output);
  709. vars.Target = target.c_str();
  710. vars.LinkLibraries = linkLibs.c_str();
  711. vars.ObjectsQuoted = buildObjs.c_str();
  712. if (this->GeneratorTarget->HasSOName(this->ConfigName)) {
  713. vars.SONameFlag = this->Makefile->GetSONameFlag(linkLanguage);
  714. vars.TargetSOName = targetNameSO.c_str();
  715. }
  716. vars.LinkFlags = linkFlags.c_str();
  717. vars.Manifests = manifests.c_str();
  718. // Compute the directory portion of the install_name setting.
  719. std::string install_name_dir;
  720. if (this->GeneratorTarget->GetType() == cmStateEnums::SHARED_LIBRARY) {
  721. // Get the install_name directory for the build tree.
  722. install_name_dir =
  723. this->GeneratorTarget->GetInstallNameDirForBuildTree(this->ConfigName);
  724. // Set the rule variable replacement value.
  725. if (install_name_dir.empty()) {
  726. vars.TargetInstallNameDir = "";
  727. } else {
  728. // Convert to a path for the native build tool.
  729. install_name_dir = this->LocalGenerator->ConvertToOutputFormat(
  730. install_name_dir, cmOutputConverter::SHELL);
  731. vars.TargetInstallNameDir = install_name_dir.c_str();
  732. }
  733. }
  734. // Add language-specific flags.
  735. std::string langFlags;
  736. this->LocalGenerator->AddLanguageFlagsForLinking(
  737. langFlags, this->GeneratorTarget, linkLanguage, this->ConfigName);
  738. this->LocalGenerator->AddArchitectureFlags(
  739. langFlags, this->GeneratorTarget, linkLanguage, this->ConfigName);
  740. vars.LanguageCompileFlags = langFlags.c_str();
  741. std::string launcher;
  742. const char* val = this->LocalGenerator->GetRuleLauncher(
  743. this->GeneratorTarget, "RULE_LAUNCH_LINK");
  744. if (val && *val) {
  745. launcher = val;
  746. launcher += " ";
  747. }
  748. CM_AUTO_PTR<cmRulePlaceholderExpander> rulePlaceholderExpander(
  749. this->LocalGenerator->CreateRulePlaceholderExpander());
  750. // Construct the main link rule and expand placeholders.
  751. rulePlaceholderExpander->SetTargetImpLib(targetOutPathImport);
  752. if (useArchiveRules) {
  753. // Construct the individual object list strings.
  754. std::vector<std::string> object_strings;
  755. this->WriteObjectsStrings(object_strings, archiveCommandLimit);
  756. // Add the cuda device object to the list of archive files. This will
  757. // only occur on archives which have CUDA_RESOLVE_DEVICE_SYMBOLS enabled
  758. if (!this->DeviceLinkObject.empty()) {
  759. object_strings.push_back(this->LocalGenerator->ConvertToOutputFormat(
  760. this->LocalGenerator->MaybeConvertToRelativePath(
  761. this->LocalGenerator->GetCurrentBinaryDirectory(),
  762. this->DeviceLinkObject),
  763. cmOutputConverter::SHELL));
  764. }
  765. // Create the archive with the first set of objects.
  766. std::vector<std::string>::iterator osi = object_strings.begin();
  767. {
  768. vars.Objects = osi->c_str();
  769. for (std::vector<std::string>::const_iterator i =
  770. archiveCreateCommands.begin();
  771. i != archiveCreateCommands.end(); ++i) {
  772. std::string cmd = launcher + *i;
  773. rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator,
  774. cmd, vars);
  775. real_link_commands.push_back(cmd);
  776. }
  777. }
  778. // Append to the archive with the other object sets.
  779. for (++osi; osi != object_strings.end(); ++osi) {
  780. vars.Objects = osi->c_str();
  781. for (std::vector<std::string>::const_iterator i =
  782. archiveAppendCommands.begin();
  783. i != archiveAppendCommands.end(); ++i) {
  784. std::string cmd = launcher + *i;
  785. rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator,
  786. cmd, vars);
  787. real_link_commands.push_back(cmd);
  788. }
  789. }
  790. // Finish the archive.
  791. vars.Objects = "";
  792. for (std::vector<std::string>::const_iterator i =
  793. archiveFinishCommands.begin();
  794. i != archiveFinishCommands.end(); ++i) {
  795. std::string cmd = launcher + *i;
  796. rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator, cmd,
  797. vars);
  798. // If there is no ranlib the command will be ":". Skip it.
  799. if (!cmd.empty() && cmd[0] != ':') {
  800. real_link_commands.push_back(cmd);
  801. }
  802. }
  803. } else {
  804. // Get the set of commands.
  805. std::string linkRule = this->GetLinkRule(linkRuleVar);
  806. cmSystemTools::ExpandListArgument(linkRule, real_link_commands);
  807. if (this->GeneratorTarget->GetPropertyAsBool("LINK_WHAT_YOU_USE") &&
  808. (this->GeneratorTarget->GetType() == cmStateEnums::SHARED_LIBRARY)) {
  809. std::string cmakeCommand = this->LocalGenerator->ConvertToOutputFormat(
  810. cmSystemTools::GetCMakeCommand(), cmLocalGenerator::SHELL);
  811. cmakeCommand += " -E __run_co_compile --lwyu=";
  812. cmakeCommand += targetOutPathReal;
  813. real_link_commands.push_back(cmakeCommand);
  814. }
  815. // Expand placeholders.
  816. for (std::vector<std::string>::iterator i = real_link_commands.begin();
  817. i != real_link_commands.end(); ++i) {
  818. *i = launcher + *i;
  819. rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator, *i,
  820. vars);
  821. }
  822. }
  823. // Restore path conversion to normal shells.
  824. this->LocalGenerator->SetLinkScriptShell(false);
  825. }
  826. // Optionally convert the build rule to use a script to avoid long
  827. // command lines in the make shell.
  828. if (useLinkScript) {
  829. // Use a link script.
  830. const char* name = (relink ? "relink.txt" : "link.txt");
  831. this->CreateLinkScript(name, real_link_commands, commands1, depends);
  832. } else {
  833. // No link script. Just use the link rule directly.
  834. commands1 = real_link_commands;
  835. }
  836. this->LocalGenerator->CreateCDCommand(
  837. commands1, this->Makefile->GetCurrentBinaryDirectory(),
  838. this->LocalGenerator->GetBinaryDirectory());
  839. commands.insert(commands.end(), commands1.begin(), commands1.end());
  840. commands1.clear();
  841. // Add a rule to create necessary symlinks for the library.
  842. // Frameworks are handled by cmOSXBundleGenerator.
  843. if (targetOutPath != targetOutPathReal &&
  844. !this->GeneratorTarget->IsFrameworkOnApple()) {
  845. std::string symlink = "$(CMAKE_COMMAND) -E cmake_symlink_library ";
  846. symlink += targetOutPathReal;
  847. symlink += " ";
  848. symlink += targetOutPathSO;
  849. symlink += " ";
  850. symlink += targetOutPath;
  851. commands1.push_back(symlink);
  852. this->LocalGenerator->CreateCDCommand(
  853. commands1, this->Makefile->GetCurrentBinaryDirectory(),
  854. this->LocalGenerator->GetBinaryDirectory());
  855. commands.insert(commands.end(), commands1.begin(), commands1.end());
  856. commands1.clear();
  857. }
  858. // Add the post-build rules when building but not when relinking.
  859. if (!relink) {
  860. this->LocalGenerator->AppendCustomCommands(
  861. commands, this->GeneratorTarget->GetPostBuildCommands(),
  862. this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
  863. }
  864. // Compute the list of outputs.
  865. std::vector<std::string> outputs(1, targetFullPathReal);
  866. if (targetNameSO != targetNameReal) {
  867. outputs.push_back(targetFullPathSO);
  868. }
  869. if (targetName != targetNameSO && targetName != targetNameReal) {
  870. outputs.push_back(targetFullPath);
  871. }
  872. // Write the build rule.
  873. this->WriteMakeRule(*this->BuildFileStream, nullptr, outputs, depends,
  874. commands, false);
  875. // Write the main driver rule to build everything in this target.
  876. this->WriteTargetDriverRule(targetFullPath, relink);
  877. // Clean all the possible library names and symlinks.
  878. this->CleanFiles.insert(this->CleanFiles.end(), libCleanFiles.begin(),
  879. libCleanFiles.end());
  880. }