cmMakefileLibraryTargetGenerator.cxx 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst or https://cmake.org/licensing for details. */
  3. #include "cmMakefileLibraryTargetGenerator.h"
  4. #include <cstddef>
  5. #include <set>
  6. #include <sstream>
  7. #include <utility>
  8. #include <vector>
  9. #include <cm/memory>
  10. #include <cmext/algorithm>
  11. #include "cmGeneratedFileStream.h"
  12. #include "cmGeneratorOptions.h"
  13. #include "cmGeneratorTarget.h"
  14. #include "cmGlobalUnixMakefileGenerator3.h"
  15. #include "cmLinkLineComputer.h"
  16. #include "cmLinkLineDeviceComputer.h"
  17. #include "cmList.h"
  18. #include "cmLocalGenerator.h"
  19. #include "cmLocalUnixMakefileGenerator3.h"
  20. #include "cmMakefile.h"
  21. #include "cmOSXBundleGenerator.h"
  22. #include "cmOutputConverter.h"
  23. #include "cmRulePlaceholderExpander.h"
  24. #include "cmState.h"
  25. #include "cmStateDirectory.h"
  26. #include "cmStateSnapshot.h"
  27. #include "cmStateTypes.h"
  28. #include "cmStringAlgorithms.h"
  29. #include "cmSystemTools.h"
  30. #include "cmValue.h"
  31. cmMakefileLibraryTargetGenerator::cmMakefileLibraryTargetGenerator(
  32. cmGeneratorTarget* target)
  33. : cmMakefileTargetGenerator(target)
  34. {
  35. this->CustomCommandDriver = OnDepends;
  36. if (this->GeneratorTarget->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  37. this->TargetNames =
  38. this->GeneratorTarget->GetLibraryNames(this->GetConfigName());
  39. }
  40. this->OSXBundleGenerator = cm::make_unique<cmOSXBundleGenerator>(target);
  41. this->OSXBundleGenerator->SetMacContentFolders(&this->MacContentFolders);
  42. }
  43. cmMakefileLibraryTargetGenerator::~cmMakefileLibraryTargetGenerator() =
  44. default;
  45. void cmMakefileLibraryTargetGenerator::WriteRuleFiles()
  46. {
  47. // create the build.make file and directory, put in the common blocks
  48. this->CreateRuleFile();
  49. // write rules used to help build object files
  50. this->WriteCommonCodeRules();
  51. // write the per-target per-language flags
  52. this->WriteTargetLanguageFlags();
  53. // write in rules for object files and custom commands
  54. this->WriteTargetBuildRules();
  55. // Write in the rules for the link dependency file
  56. this->WriteTargetLinkDependRules();
  57. // write the link rules
  58. // Write the rule for this target type.
  59. switch (this->GeneratorTarget->GetType()) {
  60. case cmStateEnums::STATIC_LIBRARY:
  61. this->WriteStaticLibraryRules();
  62. break;
  63. case cmStateEnums::SHARED_LIBRARY:
  64. this->WriteSharedLibraryRules(false);
  65. if (this->GeneratorTarget->NeedRelinkBeforeInstall(
  66. this->GetConfigName())) {
  67. // Write rules to link an installable version of the target.
  68. this->WriteSharedLibraryRules(true);
  69. }
  70. break;
  71. case cmStateEnums::MODULE_LIBRARY:
  72. this->WriteModuleLibraryRules(false);
  73. if (this->GeneratorTarget->NeedRelinkBeforeInstall(
  74. this->GetConfigName())) {
  75. // Write rules to link an installable version of the target.
  76. this->WriteModuleLibraryRules(true);
  77. }
  78. break;
  79. case cmStateEnums::OBJECT_LIBRARY:
  80. this->WriteObjectLibraryRules();
  81. break;
  82. default:
  83. // If language is not known, this is an error.
  84. cmSystemTools::Error("Unknown Library Type");
  85. break;
  86. }
  87. // Write clean target
  88. this->WriteTargetCleanRules();
  89. // Write the dependency generation rule. This must be done last so
  90. // that multiple output pair information is available.
  91. this->WriteTargetDependRules();
  92. // close the streams
  93. this->CloseFileStreams();
  94. }
  95. void cmMakefileLibraryTargetGenerator::WriteObjectLibraryRules()
  96. {
  97. std::vector<std::string> commands;
  98. std::vector<std::string> depends;
  99. // Add post-build rules.
  100. this->LocalGenerator->AppendCustomCommands(
  101. commands, this->GeneratorTarget->GetPostBuildCommands(),
  102. this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
  103. // Depend on the object files.
  104. this->AppendObjectDepends(depends);
  105. // Write the rule.
  106. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, nullptr,
  107. this->GeneratorTarget->GetName(),
  108. depends, commands, true);
  109. // Write the main driver rule to build everything in this target.
  110. this->WriteTargetDriverRule(this->GeneratorTarget->GetName(), false);
  111. }
  112. void cmMakefileLibraryTargetGenerator::WriteStaticLibraryRules()
  113. {
  114. bool const requiresDeviceLinking = requireDeviceLinking(
  115. *this->GeneratorTarget, *this->LocalGenerator, this->GetConfigName());
  116. if (requiresDeviceLinking) {
  117. this->WriteDeviceLibraryRules("CMAKE_CUDA_DEVICE_LINK_LIBRARY", false);
  118. }
  119. std::string linkLanguage =
  120. this->GeneratorTarget->GetLinkerLanguage(this->GetConfigName());
  121. std::string linkRuleVar = this->GeneratorTarget->GetCreateRuleVariable(
  122. linkLanguage, this->GetConfigName());
  123. std::string extraFlags;
  124. this->LocalGenerator->GetStaticLibraryFlags(
  125. extraFlags, this->GetConfigName(), linkLanguage, this->GeneratorTarget);
  126. this->WriteLibraryRules(linkRuleVar, extraFlags, false);
  127. }
  128. void cmMakefileLibraryTargetGenerator::WriteSharedLibraryRules(bool relink)
  129. {
  130. if (this->GeneratorTarget->IsFrameworkOnApple()) {
  131. this->WriteFrameworkRules(relink);
  132. return;
  133. }
  134. if (!relink) {
  135. bool const requiresDeviceLinking = requireDeviceLinking(
  136. *this->GeneratorTarget, *this->LocalGenerator, this->GetConfigName());
  137. if (requiresDeviceLinking) {
  138. this->WriteDeviceLibraryRules("CMAKE_CUDA_DEVICE_LINK_LIBRARY", relink);
  139. }
  140. }
  141. std::string linkLanguage =
  142. this->GeneratorTarget->GetLinkerLanguage(this->GetConfigName());
  143. std::string linkRuleVar =
  144. cmStrCat("CMAKE_", linkLanguage, "_CREATE_SHARED_LIBRARY");
  145. if (this->GeneratorTarget->IsArchivedAIXSharedLibrary()) {
  146. linkRuleVar =
  147. cmStrCat("CMAKE_", linkLanguage, "_CREATE_SHARED_LIBRARY_ARCHIVE");
  148. }
  149. std::string extraFlags;
  150. this->LocalGenerator->AppendTargetCreationLinkFlags(
  151. extraFlags, this->GeneratorTarget, linkLanguage);
  152. this->LocalGenerator->AddConfigVariableFlags(
  153. extraFlags, "CMAKE_SHARED_LINKER_FLAGS", this->GeneratorTarget,
  154. cmBuildStep::Link, linkLanguage, this->GetConfigName());
  155. std::unique_ptr<cmLinkLineComputer> linkLineComputer =
  156. this->CreateLinkLineComputer(
  157. this->LocalGenerator,
  158. this->LocalGenerator->GetStateSnapshot().GetDirectory());
  159. this->LocalGenerator->AppendModuleDefinitionFlag(
  160. extraFlags, this->GeneratorTarget, linkLineComputer.get(),
  161. this->GetConfigName(), linkLanguage);
  162. this->UseLWYU = this->LocalGenerator->AppendLWYUFlags(
  163. extraFlags, this->GeneratorTarget, linkLanguage);
  164. this->GetTargetLinkFlags(extraFlags, linkLanguage);
  165. this->WriteLibraryRules(linkRuleVar, extraFlags, relink);
  166. }
  167. void cmMakefileLibraryTargetGenerator::WriteModuleLibraryRules(bool relink)
  168. {
  169. if (!relink) {
  170. bool const requiresDeviceLinking = requireDeviceLinking(
  171. *this->GeneratorTarget, *this->LocalGenerator, this->GetConfigName());
  172. if (requiresDeviceLinking) {
  173. this->WriteDeviceLibraryRules("CMAKE_CUDA_DEVICE_LINK_LIBRARY", relink);
  174. }
  175. }
  176. std::string linkLanguage =
  177. this->GeneratorTarget->GetLinkerLanguage(this->GetConfigName());
  178. std::string linkRuleVar =
  179. cmStrCat("CMAKE_", linkLanguage, "_CREATE_SHARED_MODULE");
  180. std::string extraFlags;
  181. this->LocalGenerator->AppendTargetCreationLinkFlags(
  182. extraFlags, this->GeneratorTarget, linkLanguage);
  183. this->LocalGenerator->AddConfigVariableFlags(
  184. extraFlags, "CMAKE_MODULE_LINKER_FLAGS", this->GeneratorTarget,
  185. cmBuildStep::Link, linkLanguage, this->GetConfigName());
  186. std::unique_ptr<cmLinkLineComputer> linkLineComputer =
  187. this->CreateLinkLineComputer(
  188. this->LocalGenerator,
  189. this->LocalGenerator->GetStateSnapshot().GetDirectory());
  190. this->LocalGenerator->AppendModuleDefinitionFlag(
  191. extraFlags, this->GeneratorTarget, linkLineComputer.get(),
  192. this->GetConfigName(), linkLanguage);
  193. this->UseLWYU = this->LocalGenerator->AppendLWYUFlags(
  194. extraFlags, this->GeneratorTarget, linkLanguage);
  195. this->GetTargetLinkFlags(extraFlags, linkLanguage);
  196. this->WriteLibraryRules(linkRuleVar, extraFlags, relink);
  197. }
  198. void cmMakefileLibraryTargetGenerator::WriteFrameworkRules(bool relink)
  199. {
  200. std::string linkLanguage =
  201. this->GeneratorTarget->GetLinkerLanguage(this->GetConfigName());
  202. std::string linkRuleVar =
  203. cmStrCat("CMAKE_", linkLanguage, "_CREATE_MACOSX_FRAMEWORK");
  204. std::string extraFlags;
  205. this->LocalGenerator->AppendTargetCreationLinkFlags(
  206. extraFlags, this->GeneratorTarget, linkLanguage);
  207. this->LocalGenerator->AddConfigVariableFlags(
  208. extraFlags, "CMAKE_MACOSX_FRAMEWORK_LINKER_FLAGS", this->GeneratorTarget,
  209. cmBuildStep::Link, linkLanguage, this->GetConfigName());
  210. this->GetTargetLinkFlags(extraFlags, linkLanguage);
  211. this->WriteLibraryRules(linkRuleVar, extraFlags, relink);
  212. }
  213. void cmMakefileLibraryTargetGenerator::WriteDeviceLibraryRules(
  214. std::string const& linkRuleVar, bool relink)
  215. {
  216. #ifndef CMAKE_BOOTSTRAP
  217. // TODO: Merge the methods that call this method to avoid
  218. // code duplication.
  219. std::vector<std::string> commands;
  220. std::string const objExt =
  221. this->Makefile->GetSafeDefinition("CMAKE_CUDA_OUTPUT_EXTENSION");
  222. // Get the name of the device object to generate.
  223. std::string const targetOutput =
  224. this->GeneratorTarget->ObjectDirectory + "cmake_device_link" + objExt;
  225. this->DeviceLinkObject = targetOutput;
  226. this->NumberOfProgressActions++;
  227. if (!this->NoRuleMessages) {
  228. cmLocalUnixMakefileGenerator3::EchoProgress progress;
  229. this->MakeEchoProgress(progress);
  230. // Add the link message.
  231. std::string buildEcho = cmStrCat(
  232. "Linking CUDA device code ",
  233. this->LocalGenerator->ConvertToOutputFormat(
  234. this->LocalGenerator->MaybeRelativeToCurBinDir(this->DeviceLinkObject),
  235. cmOutputConverter::SHELL));
  236. this->LocalGenerator->AppendEcho(
  237. commands, buildEcho, cmLocalUnixMakefileGenerator3::EchoLink, &progress);
  238. }
  239. if (this->Makefile->GetSafeDefinition("CMAKE_CUDA_COMPILER_ID") == "Clang") {
  240. this->WriteDeviceLinkRule(commands, targetOutput);
  241. } else {
  242. this->WriteNvidiaDeviceLibraryRules(linkRuleVar, relink, commands,
  243. targetOutput);
  244. }
  245. // Write the main driver rule to build everything in this target.
  246. this->WriteTargetDriverRule(targetOutput, relink);
  247. }
  248. void cmMakefileLibraryTargetGenerator::WriteNvidiaDeviceLibraryRules(
  249. std::string const& linkRuleVar, bool relink,
  250. std::vector<std::string>& commands, std::string const& targetOutput)
  251. {
  252. std::string linkLanguage = "CUDA";
  253. // Build list of dependencies.
  254. std::vector<std::string> depends;
  255. this->AppendLinkDepends(depends, linkLanguage);
  256. // Add language-specific flags.
  257. std::string langFlags;
  258. this->LocalGenerator->AddLanguageFlagsForLinking(
  259. langFlags, this->GeneratorTarget, linkLanguage, this->GetConfigName());
  260. // Clean files associated with this library.
  261. std::set<std::string> libCleanFiles;
  262. libCleanFiles.insert(
  263. this->LocalGenerator->MaybeRelativeToCurBinDir(targetOutput));
  264. // Determine whether a link script will be used.
  265. bool useLinkScript = this->GlobalGenerator->GetUseLinkScript();
  266. bool useResponseFileForObjects =
  267. this->CheckUseResponseFileForObjects(linkLanguage);
  268. bool const useResponseFileForLibs =
  269. this->CheckUseResponseFileForLibraries(linkLanguage);
  270. cmRulePlaceholderExpander::RuleVariables vars;
  271. vars.Language = linkLanguage.c_str();
  272. // Expand the rule variables.
  273. cmList real_link_commands;
  274. {
  275. // Set path conversion for link script shells.
  276. this->LocalGenerator->SetLinkScriptShell(useLinkScript);
  277. // Collect up flags to link in needed libraries.
  278. std::string linkLibs;
  279. std::unique_ptr<cmLinkLineDeviceComputer> linkLineComputer(
  280. new cmLinkLineDeviceComputer(
  281. this->LocalGenerator,
  282. this->LocalGenerator->GetStateSnapshot().GetDirectory()));
  283. linkLineComputer->SetForResponse(useResponseFileForLibs);
  284. linkLineComputer->SetRelink(relink);
  285. // Create set of linking flags.
  286. std::string linkFlags;
  287. std::string ignored_;
  288. this->LocalGenerator->GetDeviceLinkFlags(
  289. *linkLineComputer, this->GetConfigName(), ignored_, linkFlags, ignored_,
  290. ignored_, this->GeneratorTarget);
  291. this->CreateLinkLibs(
  292. linkLineComputer.get(), linkLibs, useResponseFileForLibs, depends,
  293. linkLanguage, cmMakefileTargetGenerator::ResponseFlagFor::DeviceLink);
  294. // Construct object file lists that may be needed to expand the
  295. // rule.
  296. std::string buildObjs;
  297. this->CreateObjectLists(
  298. useLinkScript, false, // useArchiveRules
  299. useResponseFileForObjects, buildObjs, depends, false, linkLanguage,
  300. cmMakefileTargetGenerator::ResponseFlagFor::DeviceLink);
  301. std::string objectDir = this->GeneratorTarget->GetSupportDirectory();
  302. objectDir = this->LocalGenerator->ConvertToOutputFormat(
  303. this->LocalGenerator->MaybeRelativeToCurBinDir(objectDir),
  304. cmOutputConverter::SHELL);
  305. std::string targetSupportDir =
  306. this->GeneratorTarget->GetCMFSupportDirectory();
  307. targetSupportDir = this->LocalGenerator->ConvertToOutputFormat(
  308. this->LocalGenerator->MaybeRelativeToTopBinDir(targetSupportDir),
  309. cmOutputConverter::SHELL);
  310. std::string target = this->LocalGenerator->ConvertToOutputFormat(
  311. this->LocalGenerator->MaybeRelativeToCurBinDir(targetOutput),
  312. cmOutputConverter::SHELL);
  313. std::string targetFullPathCompilePDB =
  314. this->ComputeTargetCompilePDB(this->GetConfigName());
  315. std::string targetOutPathCompilePDB =
  316. this->LocalGenerator->ConvertToOutputFormat(targetFullPathCompilePDB,
  317. cmOutputConverter::SHELL);
  318. vars.Objects = buildObjs.c_str();
  319. vars.ObjectDir = objectDir.c_str();
  320. vars.TargetSupportDir = targetSupportDir.c_str();
  321. vars.Target = target.c_str();
  322. vars.LinkLibraries = linkLibs.c_str();
  323. vars.ObjectsQuoted = buildObjs.c_str();
  324. vars.LanguageCompileFlags = langFlags.c_str();
  325. vars.LinkFlags = linkFlags.c_str();
  326. vars.TargetCompilePDB = targetOutPathCompilePDB.c_str();
  327. std::string launcher;
  328. std::string val = this->LocalGenerator->GetRuleLauncher(
  329. this->GeneratorTarget, "RULE_LAUNCH_LINK",
  330. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE"));
  331. if (cmNonempty(val)) {
  332. launcher = cmStrCat(val, ' ');
  333. }
  334. auto rulePlaceholderExpander =
  335. this->LocalGenerator->CreateRulePlaceholderExpander(cmBuildStep::Link);
  336. // Construct the main link rule and expand placeholders.
  337. rulePlaceholderExpander->SetTargetImpLib(targetOutput);
  338. std::string linkRule = this->GetLinkRule(linkRuleVar);
  339. real_link_commands.append(linkRule);
  340. // Expand placeholders.
  341. for (auto& real_link_command : real_link_commands) {
  342. real_link_command = cmStrCat(launcher, real_link_command);
  343. rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator,
  344. real_link_command, vars);
  345. }
  346. // Restore path conversion to normal shells.
  347. this->LocalGenerator->SetLinkScriptShell(false);
  348. // Clean all the possible library names and symlinks.
  349. this->CleanFiles.insert(libCleanFiles.begin(), libCleanFiles.end());
  350. }
  351. std::vector<std::string> commands1;
  352. // Optionally convert the build rule to use a script to avoid long
  353. // command lines in the make shell.
  354. if (useLinkScript) {
  355. // Use a link script.
  356. char const* name = (relink ? "drelink.txt" : "dlink.txt");
  357. this->CreateLinkScript(name, real_link_commands, commands1, depends);
  358. } else {
  359. // No link script. Just use the link rule directly.
  360. commands1 = real_link_commands;
  361. }
  362. this->LocalGenerator->CreateCDCommand(
  363. commands1, this->Makefile->GetCurrentBinaryDirectory(),
  364. this->LocalGenerator->GetBinaryDirectory());
  365. cm::append(commands, commands1);
  366. commands1.clear();
  367. // Compute the list of outputs.
  368. std::vector<std::string> outputs(1, targetOutput);
  369. // Write the build rule.
  370. this->WriteMakeRule(*this->BuildFileStream, nullptr, outputs, depends,
  371. commands, false);
  372. #else
  373. static_cast<void>(linkRuleVar);
  374. static_cast<void>(relink);
  375. #endif
  376. }
  377. void cmMakefileLibraryTargetGenerator::WriteLibraryRules(
  378. std::string const& linkRuleVar, std::string const& extraFlags, bool relink)
  379. {
  380. // TODO: Merge the methods that call this method to avoid
  381. // code duplication.
  382. std::vector<std::string> commands;
  383. // Get the language to use for linking this library.
  384. std::string linkLanguage =
  385. this->GeneratorTarget->GetLinkerLanguage(this->GetConfigName());
  386. // Make sure we have a link language.
  387. if (linkLanguage.empty()) {
  388. cmSystemTools::Error("Cannot determine link language for target \"" +
  389. this->GeneratorTarget->GetName() + "\".");
  390. return;
  391. }
  392. auto linker = this->GeneratorTarget->GetLinkerTool(this->GetConfigName());
  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(
  403. linkFlags, this->GeneratorTarget, this->GetConfigName(), 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. this->GeneratorTarget->GetLibraryNames(this->GetConfigName());
  412. // Construct the full path version of the names.
  413. std::string outpath;
  414. std::string outpathImp;
  415. if (this->GeneratorTarget->IsFrameworkOnApple()) {
  416. outpath = this->GeneratorTarget->GetDirectory(this->GetConfigName());
  417. cmOSXBundleGenerator::SkipParts bundleSkipParts;
  418. if (this->GeneratorTarget->HasImportLibrary(this->GetConfigName())) {
  419. bundleSkipParts.TextStubs = false;
  420. }
  421. this->OSXBundleGenerator->CreateFramework(this->TargetNames.Output,
  422. outpath, this->GetConfigName(),
  423. bundleSkipParts);
  424. outpath += '/';
  425. if (!this->TargetNames.ImportLibrary.empty()) {
  426. outpathImp = this->GeneratorTarget->GetDirectory(
  427. this->GetConfigName(), cmStateEnums::ImportLibraryArtifact);
  428. cmSystemTools::MakeDirectory(outpathImp);
  429. outpathImp += '/';
  430. }
  431. } else if (this->GeneratorTarget->IsCFBundleOnApple()) {
  432. outpath = this->GeneratorTarget->GetDirectory(this->GetConfigName());
  433. this->OSXBundleGenerator->CreateCFBundle(this->TargetNames.Output, outpath,
  434. this->GetConfigName());
  435. outpath += '/';
  436. } else if (relink) {
  437. outpath = cmStrCat(this->Makefile->GetCurrentBinaryDirectory(),
  438. "/CMakeFiles/CMakeRelink.dir");
  439. cmSystemTools::MakeDirectory(outpath);
  440. outpath += '/';
  441. if (!this->TargetNames.ImportLibrary.empty()) {
  442. outpathImp = outpath;
  443. }
  444. } else {
  445. outpath = this->GeneratorTarget->GetDirectory(this->GetConfigName());
  446. cmSystemTools::MakeDirectory(outpath);
  447. outpath += '/';
  448. if (!this->TargetNames.ImportLibrary.empty()) {
  449. outpathImp = this->GeneratorTarget->GetDirectory(
  450. this->GetConfigName(), cmStateEnums::ImportLibraryArtifact);
  451. cmSystemTools::MakeDirectory(outpathImp);
  452. outpathImp += '/';
  453. }
  454. }
  455. std::string compilePdbOutputPath =
  456. this->GeneratorTarget->GetCompilePDBDirectory(this->GetConfigName());
  457. cmSystemTools::MakeDirectory(compilePdbOutputPath);
  458. std::string pdbOutputPath =
  459. this->GeneratorTarget->GetPDBDirectory(this->GetConfigName());
  460. cmSystemTools::MakeDirectory(pdbOutputPath);
  461. pdbOutputPath += "/";
  462. std::string targetFullPath = outpath + this->TargetNames.Output;
  463. std::string targetFullPathPDB = pdbOutputPath + this->TargetNames.PDB;
  464. std::string targetFullPathSO = outpath + this->TargetNames.SharedObject;
  465. std::string targetFullPathReal = outpath + this->TargetNames.Real;
  466. std::string targetFullPathImport =
  467. outpathImp + this->TargetNames.ImportLibrary;
  468. // Construct the output path version of the names for use in command
  469. // arguments.
  470. std::string targetOutPathPDB = this->LocalGenerator->ConvertToOutputFormat(
  471. targetFullPathPDB, cmOutputConverter::SHELL);
  472. std::string targetOutPath = this->LocalGenerator->ConvertToOutputFormat(
  473. this->LocalGenerator->MaybeRelativeToCurBinDir(targetFullPath),
  474. cmOutputConverter::SHELL);
  475. std::string targetOutPathSO = this->LocalGenerator->ConvertToOutputFormat(
  476. this->LocalGenerator->MaybeRelativeToCurBinDir(targetFullPathSO),
  477. cmOutputConverter::SHELL);
  478. std::string targetOutPathReal = this->LocalGenerator->ConvertToOutputFormat(
  479. this->LocalGenerator->MaybeRelativeToCurBinDir(targetFullPathReal),
  480. cmOutputConverter::SHELL);
  481. std::string targetOutPathImport =
  482. this->LocalGenerator->ConvertToOutputFormat(
  483. this->LocalGenerator->MaybeRelativeToCurBinDir(targetFullPathImport),
  484. cmOutputConverter::SHELL);
  485. this->NumberOfProgressActions++;
  486. if (!this->NoRuleMessages) {
  487. cmLocalUnixMakefileGenerator3::EchoProgress progress;
  488. this->MakeEchoProgress(progress);
  489. // Add the link message.
  490. std::string buildEcho = cmStrCat("Linking ", 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::set<std::string> libCleanFiles;
  514. libCleanFiles.insert(
  515. this->LocalGenerator->MaybeRelativeToCurBinDir(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. cm::append(commands, commands1);
  526. commands1.clear();
  527. }
  528. if (this->TargetNames.Output != this->TargetNames.Real) {
  529. libCleanFiles.insert(
  530. this->LocalGenerator->MaybeRelativeToCurBinDir(targetFullPath));
  531. }
  532. if (this->TargetNames.SharedObject != this->TargetNames.Real &&
  533. this->TargetNames.SharedObject != this->TargetNames.Output) {
  534. libCleanFiles.insert(
  535. this->LocalGenerator->MaybeRelativeToCurBinDir(targetFullPathSO));
  536. }
  537. if (!this->TargetNames.ImportLibrary.empty()) {
  538. libCleanFiles.insert(
  539. this->LocalGenerator->MaybeRelativeToCurBinDir(targetFullPathImport));
  540. std::string implib;
  541. if (this->GeneratorTarget->GetImplibGNUtoMS(
  542. this->GetConfigName(), targetFullPathImport, implib)) {
  543. libCleanFiles.insert(
  544. this->LocalGenerator->MaybeRelativeToCurBinDir(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.insert(
  551. this->LocalGenerator->MaybeRelativeToCurBinDir(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.insert(this->LocalGenerator->MaybeRelativeToCurBinDir(
  557. targetFullPath + ".manifest"));
  558. }
  559. #endif
  560. // Add the pre-build and pre-link rules building but not when relinking.
  561. if (!relink) {
  562. this->LocalGenerator->AppendCustomCommands(
  563. commands, this->GeneratorTarget->GetPreBuildCommands(),
  564. this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
  565. this->LocalGenerator->AppendCustomCommands(
  566. commands, this->GeneratorTarget->GetPreLinkCommands(),
  567. this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
  568. }
  569. // Determine whether a link script will be used.
  570. bool useLinkScript = this->GlobalGenerator->GetUseLinkScript();
  571. bool useResponseFileForObjects =
  572. this->CheckUseResponseFileForObjects(linkLanguage);
  573. bool const useResponseFileForLibs =
  574. this->CheckUseResponseFileForLibraries(linkLanguage);
  575. // For static libraries there might be archiving rules.
  576. bool haveStaticLibraryRule = false;
  577. cmList archiveCreateCommands;
  578. cmList archiveAppendCommands;
  579. cmList archiveFinishCommands;
  580. std::string::size_type archiveCommandLimit = std::string::npos;
  581. if (this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY) {
  582. haveStaticLibraryRule = this->Makefile->IsDefinitionSet(linkRuleVar);
  583. std::string arCreateVar =
  584. cmStrCat("CMAKE_", linkLanguage, "_ARCHIVE_CREATE");
  585. arCreateVar = this->GeneratorTarget->GetFeatureSpecificLinkRuleVariable(
  586. arCreateVar, linkLanguage, this->GetConfigName());
  587. archiveCreateCommands.assign(this->Makefile->GetDefinition(arCreateVar));
  588. std::string arAppendVar =
  589. cmStrCat("CMAKE_", linkLanguage, "_ARCHIVE_APPEND");
  590. arAppendVar = this->GeneratorTarget->GetFeatureSpecificLinkRuleVariable(
  591. arAppendVar, linkLanguage, this->GetConfigName());
  592. archiveAppendCommands.assign(this->Makefile->GetDefinition(arAppendVar));
  593. std::string arFinishVar =
  594. cmStrCat("CMAKE_", linkLanguage, "_ARCHIVE_FINISH");
  595. arFinishVar = this->GeneratorTarget->GetFeatureSpecificLinkRuleVariable(
  596. arFinishVar, linkLanguage, this->GetConfigName());
  597. archiveFinishCommands.assign(this->Makefile->GetDefinition(arFinishVar));
  598. }
  599. // Decide whether to use archiving rules.
  600. bool useArchiveRules = !haveStaticLibraryRule &&
  601. !archiveCreateCommands.empty() && !archiveAppendCommands.empty();
  602. if (useArchiveRules) {
  603. // Archiving rules are always run with a link script.
  604. useLinkScript = true;
  605. // Archiving rules never use a response file.
  606. useResponseFileForObjects = false;
  607. // Limit the length of individual object lists to less than half of
  608. // the command line length limit (leaving half for other flags).
  609. // This may result in several calls to the archiver.
  610. if (size_t limit = cmSystemTools::CalculateCommandLineLengthLimit()) {
  611. archiveCommandLimit = limit / 2;
  612. } else {
  613. archiveCommandLimit = 8000;
  614. }
  615. }
  616. // Expand the rule variables.
  617. auto rulePlaceholderExpander =
  618. this->LocalGenerator->CreateRulePlaceholderExpander(cmBuildStep::Link);
  619. bool useWatcomQuote =
  620. this->Makefile->IsOn(linkRuleVar + "_USE_WATCOM_QUOTE");
  621. cmList real_link_commands;
  622. {
  623. // Set path conversion for link script shells.
  624. this->LocalGenerator->SetLinkScriptShell(useLinkScript);
  625. // Collect up flags to link in needed libraries.
  626. std::string linkLibs;
  627. if (this->GeneratorTarget->GetType() != cmStateEnums::STATIC_LIBRARY) {
  628. std::unique_ptr<cmLinkLineComputer> linkLineComputer =
  629. this->CreateLinkLineComputer(
  630. this->LocalGenerator,
  631. this->LocalGenerator->GetStateSnapshot().GetDirectory());
  632. linkLineComputer->SetForResponse(useResponseFileForLibs);
  633. linkLineComputer->SetUseWatcomQuote(useWatcomQuote);
  634. linkLineComputer->SetRelink(relink);
  635. this->CreateLinkLibs(linkLineComputer.get(), linkLibs,
  636. useResponseFileForLibs, depends, linkLanguage);
  637. }
  638. // Construct object file lists that may be needed to expand the
  639. // rule.
  640. std::string buildObjs;
  641. this->CreateObjectLists(useLinkScript, useArchiveRules,
  642. useResponseFileForObjects, buildObjs, depends,
  643. useWatcomQuote, linkLanguage);
  644. if (!this->DeviceLinkObject.empty()) {
  645. buildObjs += " " +
  646. this->LocalGenerator->ConvertToOutputFormat(
  647. this->LocalGenerator->MaybeRelativeToCurBinDir(
  648. this->DeviceLinkObject),
  649. cmOutputConverter::SHELL);
  650. }
  651. std::string const& aixExports = this->GetAIXExports(this->GetConfigName());
  652. // maybe create .def file from list of objects
  653. this->GenDefFile(real_link_commands);
  654. std::string manifests = this->GetManifests(this->GetConfigName());
  655. cmRulePlaceholderExpander::RuleVariables vars;
  656. vars.TargetPDB = targetOutPathPDB.c_str();
  657. // Setup the target version.
  658. std::string targetVersionMajor;
  659. std::string targetVersionMinor;
  660. {
  661. std::ostringstream majorStream;
  662. std::ostringstream minorStream;
  663. int major;
  664. int minor;
  665. this->GeneratorTarget->GetTargetVersion(major, minor);
  666. majorStream << major;
  667. minorStream << minor;
  668. targetVersionMajor = majorStream.str();
  669. targetVersionMinor = minorStream.str();
  670. }
  671. vars.TargetVersionMajor = targetVersionMajor.c_str();
  672. vars.TargetVersionMinor = targetVersionMinor.c_str();
  673. vars.CMTargetName = this->GeneratorTarget->GetName().c_str();
  674. vars.CMTargetType =
  675. cmState::GetTargetTypeName(this->GeneratorTarget->GetType()).c_str();
  676. vars.Language = linkLanguage.c_str();
  677. vars.Linker = linker.c_str();
  678. vars.AIXExports = aixExports.c_str();
  679. vars.Objects = buildObjs.c_str();
  680. std::string objectDir = this->GeneratorTarget->GetSupportDirectory();
  681. objectDir = this->LocalGenerator->ConvertToOutputFormat(
  682. this->LocalGenerator->MaybeRelativeToCurBinDir(objectDir),
  683. cmOutputConverter::SHELL);
  684. vars.ObjectDir = objectDir.c_str();
  685. std::string targetSupportDir =
  686. this->GeneratorTarget->GetCMFSupportDirectory();
  687. targetSupportDir = this->LocalGenerator->ConvertToOutputFormat(
  688. this->LocalGenerator->MaybeRelativeToTopBinDir(targetSupportDir),
  689. cmOutputConverter::SHELL);
  690. vars.TargetSupportDir = targetSupportDir.c_str();
  691. std::string target = this->LocalGenerator->ConvertToOutputFormat(
  692. this->LocalGenerator->MaybeRelativeToCurBinDir(targetFullPathReal),
  693. cmOutputConverter::SHELL, useWatcomQuote);
  694. vars.Target = target.c_str();
  695. vars.LinkLibraries = linkLibs.c_str();
  696. vars.ObjectsQuoted = buildObjs.c_str();
  697. std::string targetOutSOName;
  698. if (this->GeneratorTarget->HasSOName(this->GetConfigName()) ||
  699. this->GeneratorTarget->IsArchivedAIXSharedLibrary()) {
  700. vars.SONameFlag = this->Makefile->GetSONameFlag(linkLanguage);
  701. targetOutSOName = this->LocalGenerator->ConvertToOutputFormat(
  702. this->TargetNames.SharedObject, cmOutputConverter::SHELL);
  703. vars.TargetSOName = targetOutSOName.c_str();
  704. }
  705. vars.LinkFlags = linkFlags.c_str();
  706. vars.Manifests = manifests.c_str();
  707. vars.Config = this->GetConfigName().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 = this->GeneratorTarget->GetInstallNameDirForBuildTree(
  713. this->GetConfigName());
  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->GetConfigName());
  728. this->LocalGenerator->AddArchitectureFlags(
  729. langFlags, this->GeneratorTarget, linkLanguage, this->GetConfigName());
  730. vars.LanguageCompileFlags = langFlags.c_str();
  731. std::string linkerLauncher =
  732. this->GetLinkerLauncher(this->GetConfigName());
  733. if (cmNonempty(linkerLauncher)) {
  734. vars.Launcher = linkerLauncher.c_str();
  735. }
  736. std::string launcher;
  737. std::string val = this->LocalGenerator->GetRuleLauncher(
  738. this->GeneratorTarget, "RULE_LAUNCH_LINK",
  739. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE"));
  740. if (cmNonempty(val)) {
  741. launcher = cmStrCat(val, ' ');
  742. }
  743. // Construct the main link rule and expand placeholders.
  744. rulePlaceholderExpander->SetTargetImpLib(targetOutPathImport);
  745. if (useArchiveRules) {
  746. // Construct the individual object list strings.
  747. std::vector<std::string> object_strings;
  748. this->WriteObjectsStrings(object_strings, false, archiveCommandLimit);
  749. // Add the cuda device object to the list of archive files. This will
  750. // only occur on archives which have CUDA_RESOLVE_DEVICE_SYMBOLS enabled
  751. if (!this->DeviceLinkObject.empty()) {
  752. object_strings.push_back(this->LocalGenerator->ConvertToOutputFormat(
  753. this->LocalGenerator->MaybeRelativeToCurBinDir(
  754. this->DeviceLinkObject),
  755. cmOutputConverter::SHELL));
  756. }
  757. // Create the archive with the first set of objects.
  758. auto osi = object_strings.begin();
  759. {
  760. vars.Objects = osi->c_str();
  761. for (std::string const& acc : archiveCreateCommands) {
  762. std::string cmd = launcher + acc;
  763. rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator,
  764. cmd, vars);
  765. real_link_commands.push_back(std::move(cmd));
  766. }
  767. }
  768. // Append to the archive with the other object sets.
  769. for (++osi; osi != object_strings.end(); ++osi) {
  770. vars.Objects = osi->c_str();
  771. for (std::string const& aac : archiveAppendCommands) {
  772. std::string cmd = launcher + aac;
  773. rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator,
  774. cmd, vars);
  775. real_link_commands.push_back(std::move(cmd));
  776. }
  777. }
  778. // Finish the archive.
  779. vars.Objects = "";
  780. for (std::string const& afc : archiveFinishCommands) {
  781. std::string cmd = launcher + afc;
  782. rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator, cmd,
  783. vars);
  784. // If there is no ranlib the command will be ":". Skip it.
  785. if (!cmd.empty() && cmd[0] != ':') {
  786. real_link_commands.push_back(std::move(cmd));
  787. }
  788. }
  789. } else {
  790. // Get the set of commands.
  791. std::string linkRule = this->GetLinkRule(linkRuleVar);
  792. real_link_commands.append(linkRule);
  793. if (this->UseLWYU) {
  794. cmValue lwyuCheck =
  795. this->Makefile->GetDefinition("CMAKE_LINK_WHAT_YOU_USE_CHECK");
  796. if (lwyuCheck) {
  797. std::string cmakeCommand = cmStrCat(
  798. this->LocalGenerator->ConvertToOutputFormat(
  799. cmSystemTools::GetCMakeCommand(), cmLocalGenerator::SHELL),
  800. " -E __run_co_compile --lwyu=");
  801. cmakeCommand += this->LocalGenerator->EscapeForShell(*lwyuCheck);
  802. cmakeCommand += cmStrCat(" --source=", targetOutPathReal);
  803. real_link_commands.push_back(std::move(cmakeCommand));
  804. }
  805. }
  806. // Expand placeholders.
  807. for (auto& real_link_command : real_link_commands) {
  808. real_link_command = cmStrCat(launcher, real_link_command);
  809. rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator,
  810. real_link_command, vars);
  811. }
  812. }
  813. // Restore path conversion to normal shells.
  814. this->LocalGenerator->SetLinkScriptShell(false);
  815. }
  816. // Optionally convert the build rule to use a script to avoid long
  817. // command lines in the make shell.
  818. if (useLinkScript) {
  819. // Use a link script.
  820. char const* name = (relink ? "relink.txt" : "link.txt");
  821. this->CreateLinkScript(name, real_link_commands, commands1, depends);
  822. } else {
  823. // No link script. Just use the link rule directly.
  824. commands1 = real_link_commands;
  825. }
  826. this->LocalGenerator->CreateCDCommand(
  827. commands1, this->Makefile->GetCurrentBinaryDirectory(),
  828. this->LocalGenerator->GetBinaryDirectory());
  829. cm::append(commands, commands1);
  830. commands1.clear();
  831. // Add a rule to create necessary symlinks for the library.
  832. // Frameworks are handled by cmOSXBundleGenerator.
  833. if (targetOutPath != targetOutPathReal &&
  834. !this->GeneratorTarget->IsFrameworkOnApple()) {
  835. std::string symlink =
  836. cmStrCat("$(CMAKE_COMMAND) -E cmake_symlink_library ", targetOutPathReal,
  837. ' ', targetOutPathSO, ' ', targetOutPath);
  838. commands1.push_back(std::move(symlink));
  839. this->LocalGenerator->CreateCDCommand(
  840. commands1, this->Makefile->GetCurrentBinaryDirectory(),
  841. this->LocalGenerator->GetBinaryDirectory());
  842. cm::append(commands, commands1);
  843. commands1.clear();
  844. }
  845. // Add the post-build rules when building but not when relinking.
  846. if (!relink) {
  847. this->LocalGenerator->AppendCustomCommands(
  848. commands, this->GeneratorTarget->GetPostBuildCommands(),
  849. this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
  850. }
  851. // Compute the list of outputs.
  852. std::vector<std::string> outputs;
  853. outputs.reserve(3);
  854. outputs.push_back(targetFullPathReal);
  855. if (this->TargetNames.SharedObject != this->TargetNames.Real) {
  856. outputs.push_back(targetFullPathSO);
  857. }
  858. if (this->TargetNames.Output != this->TargetNames.SharedObject &&
  859. this->TargetNames.Output != this->TargetNames.Real) {
  860. outputs.push_back(targetFullPath);
  861. }
  862. // Write the build rule.
  863. this->WriteMakeRule(*this->BuildFileStream, nullptr, outputs, depends,
  864. commands, false);
  865. // Add rule to generate text-based stubs, if required
  866. if (this->GeneratorTarget->IsApple() &&
  867. this->GeneratorTarget->HasImportLibrary(this->GetConfigName())) {
  868. auto genStubsRule =
  869. this->Makefile->GetDefinition("CMAKE_CREATE_TEXT_STUBS");
  870. cmList genStubs_commands{ genStubsRule };
  871. this->LocalGenerator->CreateCDCommand(
  872. genStubs_commands, this->Makefile->GetCurrentBinaryDirectory(),
  873. this->LocalGenerator->GetBinaryDirectory());
  874. std::string TBDFullPath =
  875. cmStrCat(outpathImp, this->TargetNames.ImportOutput);
  876. std::string TBDFullPathReal =
  877. cmStrCat(outpathImp, this->TargetNames.ImportReal);
  878. std::string TBDFullPathSO =
  879. cmStrCat(outpathImp, this->TargetNames.ImportLibrary);
  880. // Expand placeholders.
  881. cmRulePlaceholderExpander::RuleVariables vars;
  882. std::string target = this->LocalGenerator->ConvertToOutputFormat(
  883. this->LocalGenerator->MaybeRelativeToCurBinDir(targetFullPathReal),
  884. cmOutputConverter::SHELL, useWatcomQuote);
  885. vars.Target = target.c_str();
  886. std::string TBDOutPathReal = this->LocalGenerator->ConvertToOutputFormat(
  887. this->LocalGenerator->MaybeRelativeToCurBinDir(TBDFullPathReal),
  888. cmOutputConverter::SHELL, useWatcomQuote);
  889. rulePlaceholderExpander->SetTargetImpLib(TBDOutPathReal);
  890. for (std::string& command : genStubs_commands) {
  891. rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator,
  892. command, vars);
  893. }
  894. outputs.clear();
  895. outputs.push_back(TBDFullPathReal);
  896. if (this->TargetNames.ImportLibrary != this->TargetNames.ImportReal) {
  897. outputs.push_back(TBDFullPathSO);
  898. }
  899. if (this->TargetNames.ImportOutput != this->TargetNames.ImportLibrary &&
  900. this->TargetNames.ImportOutput != this->TargetNames.ImportReal) {
  901. outputs.push_back(TBDFullPath);
  902. }
  903. this->ExtraFiles.insert(TBDFullPath);
  904. depends.clear();
  905. depends.push_back(targetFullPathReal);
  906. // Add a rule to create necessary symlinks for the library.
  907. // Frameworks are handled by cmOSXBundleGenerator.
  908. if (TBDFullPath != TBDFullPathReal &&
  909. !this->GeneratorTarget->IsFrameworkOnApple()) {
  910. auto TBDOutPathSO = this->LocalGenerator->ConvertToOutputFormat(
  911. this->LocalGenerator->MaybeRelativeToCurBinDir(TBDFullPathSO),
  912. cmOutputConverter::SHELL, useWatcomQuote);
  913. auto TBDOutPath = this->LocalGenerator->ConvertToOutputFormat(
  914. this->LocalGenerator->MaybeRelativeToCurBinDir(TBDFullPath),
  915. cmOutputConverter::SHELL, useWatcomQuote);
  916. std::string symlink =
  917. cmStrCat("$(CMAKE_COMMAND) -E cmake_symlink_library ", TBDOutPathReal,
  918. ' ', TBDOutPathSO, ' ', TBDOutPath);
  919. commands1.push_back(std::move(symlink));
  920. this->LocalGenerator->CreateCDCommand(
  921. commands1, this->Makefile->GetCurrentBinaryDirectory(),
  922. this->LocalGenerator->GetBinaryDirectory());
  923. cm::append(genStubs_commands, commands1);
  924. commands1.clear();
  925. }
  926. this->WriteMakeRule(*this->BuildFileStream, nullptr, outputs, depends,
  927. genStubs_commands, false);
  928. // clean actions for apple specific outputs
  929. // clean actions for ImportLibrary are already specified
  930. if (this->TargetNames.ImportReal != this->TargetNames.ImportLibrary) {
  931. libCleanFiles.insert(
  932. this->LocalGenerator->MaybeRelativeToCurBinDir(TBDFullPathReal));
  933. }
  934. if (this->TargetNames.ImportOutput != this->TargetNames.ImportReal &&
  935. this->TargetNames.ImportOutput != this->TargetNames.ImportLibrary) {
  936. libCleanFiles.insert(
  937. this->LocalGenerator->MaybeRelativeToCurBinDir(TBDFullPath));
  938. }
  939. }
  940. // Write the main driver rule to build everything in this target.
  941. this->WriteTargetDriverRule(targetFullPath, relink);
  942. // Clean all the possible library names and symlinks.
  943. this->CleanFiles.insert(libCleanFiles.begin(), libCleanFiles.end());
  944. }