cmMakefileLibraryTargetGenerator.cxx 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077
  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 <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->GetTargetLinkFlags(extraFlags, linkLanguage);
  151. this->LocalGenerator->AddConfigVariableFlags(
  152. extraFlags, "CMAKE_SHARED_LINKER_FLAGS", this->GeneratorTarget,
  153. cmBuildStep::Link, linkLanguage, this->GetConfigName());
  154. std::unique_ptr<cmLinkLineComputer> linkLineComputer =
  155. this->CreateLinkLineComputer(
  156. this->LocalGenerator,
  157. this->LocalGenerator->GetStateSnapshot().GetDirectory());
  158. this->LocalGenerator->AppendModuleDefinitionFlag(
  159. extraFlags, this->GeneratorTarget, linkLineComputer.get(),
  160. this->GetConfigName());
  161. this->UseLWYU = this->LocalGenerator->AppendLWYUFlags(
  162. extraFlags, this->GeneratorTarget, linkLanguage);
  163. this->WriteLibraryRules(linkRuleVar, extraFlags, relink);
  164. }
  165. void cmMakefileLibraryTargetGenerator::WriteModuleLibraryRules(bool relink)
  166. {
  167. if (!relink) {
  168. bool const requiresDeviceLinking = requireDeviceLinking(
  169. *this->GeneratorTarget, *this->LocalGenerator, this->GetConfigName());
  170. if (requiresDeviceLinking) {
  171. this->WriteDeviceLibraryRules("CMAKE_CUDA_DEVICE_LINK_LIBRARY", relink);
  172. }
  173. }
  174. std::string linkLanguage =
  175. this->GeneratorTarget->GetLinkerLanguage(this->GetConfigName());
  176. std::string linkRuleVar =
  177. cmStrCat("CMAKE_", linkLanguage, "_CREATE_SHARED_MODULE");
  178. std::string extraFlags;
  179. this->GetTargetLinkFlags(extraFlags, linkLanguage);
  180. this->LocalGenerator->AddConfigVariableFlags(
  181. extraFlags, "CMAKE_MODULE_LINKER_FLAGS", this->GeneratorTarget,
  182. cmBuildStep::Link, linkLanguage, this->GetConfigName());
  183. std::unique_ptr<cmLinkLineComputer> linkLineComputer =
  184. this->CreateLinkLineComputer(
  185. this->LocalGenerator,
  186. this->LocalGenerator->GetStateSnapshot().GetDirectory());
  187. this->LocalGenerator->AppendModuleDefinitionFlag(
  188. extraFlags, this->GeneratorTarget, linkLineComputer.get(),
  189. this->GetConfigName());
  190. this->UseLWYU = this->LocalGenerator->AppendLWYUFlags(
  191. extraFlags, this->GeneratorTarget, linkLanguage);
  192. this->WriteLibraryRules(linkRuleVar, extraFlags, relink);
  193. }
  194. void cmMakefileLibraryTargetGenerator::WriteFrameworkRules(bool relink)
  195. {
  196. std::string linkLanguage =
  197. this->GeneratorTarget->GetLinkerLanguage(this->GetConfigName());
  198. std::string linkRuleVar =
  199. cmStrCat("CMAKE_", linkLanguage, "_CREATE_MACOSX_FRAMEWORK");
  200. std::string extraFlags;
  201. this->GetTargetLinkFlags(extraFlags, linkLanguage);
  202. this->LocalGenerator->AddConfigVariableFlags(
  203. extraFlags, "CMAKE_MACOSX_FRAMEWORK_LINKER_FLAGS", this->GeneratorTarget,
  204. cmBuildStep::Link, linkLanguage, this->GetConfigName());
  205. this->WriteLibraryRules(linkRuleVar, extraFlags, relink);
  206. }
  207. void cmMakefileLibraryTargetGenerator::WriteDeviceLibraryRules(
  208. std::string const& linkRuleVar, bool relink)
  209. {
  210. #ifndef CMAKE_BOOTSTRAP
  211. // TODO: Merge the methods that call this method to avoid
  212. // code duplication.
  213. std::vector<std::string> commands;
  214. std::string const objExt =
  215. this->Makefile->GetSafeDefinition("CMAKE_CUDA_OUTPUT_EXTENSION");
  216. // Get the name of the device object to generate.
  217. std::string const targetOutput =
  218. this->GeneratorTarget->ObjectDirectory + "cmake_device_link" + objExt;
  219. this->DeviceLinkObject = targetOutput;
  220. this->NumberOfProgressActions++;
  221. if (!this->NoRuleMessages) {
  222. cmLocalUnixMakefileGenerator3::EchoProgress progress;
  223. this->MakeEchoProgress(progress);
  224. // Add the link message.
  225. std::string buildEcho = cmStrCat(
  226. "Linking CUDA device code ",
  227. this->LocalGenerator->ConvertToOutputFormat(
  228. this->LocalGenerator->MaybeRelativeToCurBinDir(this->DeviceLinkObject),
  229. cmOutputConverter::SHELL));
  230. this->LocalGenerator->AppendEcho(
  231. commands, buildEcho, cmLocalUnixMakefileGenerator3::EchoLink, &progress);
  232. }
  233. if (this->Makefile->GetSafeDefinition("CMAKE_CUDA_COMPILER_ID") == "Clang") {
  234. this->WriteDeviceLinkRule(commands, targetOutput);
  235. } else {
  236. this->WriteNvidiaDeviceLibraryRules(linkRuleVar, relink, commands,
  237. targetOutput);
  238. }
  239. // Write the main driver rule to build everything in this target.
  240. this->WriteTargetDriverRule(targetOutput, relink);
  241. }
  242. void cmMakefileLibraryTargetGenerator::WriteNvidiaDeviceLibraryRules(
  243. std::string const& linkRuleVar, bool relink,
  244. std::vector<std::string>& commands, std::string const& targetOutput)
  245. {
  246. std::string linkLanguage = "CUDA";
  247. // Build list of dependencies.
  248. std::vector<std::string> depends;
  249. this->AppendLinkDepends(depends, linkLanguage);
  250. // Add language-specific flags.
  251. std::string langFlags;
  252. this->LocalGenerator->AddLanguageFlagsForLinking(
  253. langFlags, this->GeneratorTarget, linkLanguage, this->GetConfigName());
  254. // Clean files associated with this library.
  255. std::set<std::string> libCleanFiles;
  256. libCleanFiles.insert(
  257. this->LocalGenerator->MaybeRelativeToCurBinDir(targetOutput));
  258. // Determine whether a link script will be used.
  259. bool useLinkScript = this->GlobalGenerator->GetUseLinkScript();
  260. bool useResponseFileForObjects =
  261. this->CheckUseResponseFileForObjects(linkLanguage);
  262. bool const useResponseFileForLibs =
  263. this->CheckUseResponseFileForLibraries(linkLanguage);
  264. cmRulePlaceholderExpander::RuleVariables vars;
  265. vars.Language = linkLanguage.c_str();
  266. // Expand the rule variables.
  267. cmList real_link_commands;
  268. {
  269. // Set path conversion for link script shells.
  270. this->LocalGenerator->SetLinkScriptShell(useLinkScript);
  271. // Collect up flags to link in needed libraries.
  272. std::string linkLibs;
  273. std::unique_ptr<cmLinkLineDeviceComputer> linkLineComputer(
  274. new cmLinkLineDeviceComputer(
  275. this->LocalGenerator,
  276. this->LocalGenerator->GetStateSnapshot().GetDirectory()));
  277. linkLineComputer->SetForResponse(useResponseFileForLibs);
  278. linkLineComputer->SetRelink(relink);
  279. // Create set of linking flags.
  280. std::string linkFlags;
  281. std::string ignored_;
  282. this->LocalGenerator->GetDeviceLinkFlags(
  283. *linkLineComputer, this->GetConfigName(), ignored_, linkFlags, ignored_,
  284. ignored_, this->GeneratorTarget);
  285. this->CreateLinkLibs(
  286. linkLineComputer.get(), linkLibs, useResponseFileForLibs, depends,
  287. linkLanguage, cmMakefileTargetGenerator::ResponseFlagFor::DeviceLink);
  288. // Construct object file lists that may be needed to expand the
  289. // rule.
  290. std::string buildObjs;
  291. this->CreateObjectLists(
  292. useLinkScript, false, // useArchiveRules
  293. useResponseFileForObjects, buildObjs, depends, false, linkLanguage,
  294. cmMakefileTargetGenerator::ResponseFlagFor::DeviceLink);
  295. std::string objectDir = this->GeneratorTarget->GetSupportDirectory();
  296. objectDir = this->LocalGenerator->ConvertToOutputFormat(
  297. this->LocalGenerator->MaybeRelativeToCurBinDir(objectDir),
  298. cmOutputConverter::SHELL);
  299. std::string target = this->LocalGenerator->ConvertToOutputFormat(
  300. this->LocalGenerator->MaybeRelativeToCurBinDir(targetOutput),
  301. cmOutputConverter::SHELL);
  302. std::string targetFullPathCompilePDB =
  303. this->ComputeTargetCompilePDB(this->GetConfigName());
  304. std::string targetOutPathCompilePDB =
  305. this->LocalGenerator->ConvertToOutputFormat(targetFullPathCompilePDB,
  306. cmOutputConverter::SHELL);
  307. vars.Objects = buildObjs.c_str();
  308. vars.ObjectDir = objectDir.c_str();
  309. vars.Target = target.c_str();
  310. vars.LinkLibraries = linkLibs.c_str();
  311. vars.ObjectsQuoted = buildObjs.c_str();
  312. vars.LanguageCompileFlags = langFlags.c_str();
  313. vars.LinkFlags = linkFlags.c_str();
  314. vars.TargetCompilePDB = targetOutPathCompilePDB.c_str();
  315. std::string launcher;
  316. std::string val = this->LocalGenerator->GetRuleLauncher(
  317. this->GeneratorTarget, "RULE_LAUNCH_LINK",
  318. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE"));
  319. if (cmNonempty(val)) {
  320. launcher = cmStrCat(val, ' ');
  321. }
  322. auto rulePlaceholderExpander =
  323. this->LocalGenerator->CreateRulePlaceholderExpander(
  324. cmBuildStep::Link, this->GeneratorTarget, linkLanguage);
  325. // Construct the main link rule and expand placeholders.
  326. rulePlaceholderExpander->SetTargetImpLib(targetOutput);
  327. std::string linkRule = this->GetLinkRule(linkRuleVar);
  328. real_link_commands.append(linkRule);
  329. // Expand placeholders.
  330. for (auto& real_link_command : real_link_commands) {
  331. real_link_command = cmStrCat(launcher, real_link_command);
  332. rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator,
  333. real_link_command, vars);
  334. }
  335. // Restore path conversion to normal shells.
  336. this->LocalGenerator->SetLinkScriptShell(false);
  337. // Clean all the possible library names and symlinks.
  338. this->CleanFiles.insert(libCleanFiles.begin(), libCleanFiles.end());
  339. }
  340. std::vector<std::string> commands1;
  341. // Optionally convert the build rule to use a script to avoid long
  342. // command lines in the make shell.
  343. if (useLinkScript) {
  344. // Use a link script.
  345. char const* name = (relink ? "drelink.txt" : "dlink.txt");
  346. this->CreateLinkScript(name, real_link_commands, commands1, depends);
  347. } else {
  348. // No link script. Just use the link rule directly.
  349. commands1 = real_link_commands;
  350. }
  351. this->LocalGenerator->CreateCDCommand(
  352. commands1, this->Makefile->GetCurrentBinaryDirectory(),
  353. this->LocalGenerator->GetBinaryDirectory());
  354. cm::append(commands, commands1);
  355. commands1.clear();
  356. // Compute the list of outputs.
  357. std::vector<std::string> outputs(1, targetOutput);
  358. // Write the build rule.
  359. this->WriteMakeRule(*this->BuildFileStream, nullptr, outputs, depends,
  360. commands, false);
  361. #else
  362. static_cast<void>(linkRuleVar);
  363. static_cast<void>(relink);
  364. #endif
  365. }
  366. void cmMakefileLibraryTargetGenerator::WriteLibraryRules(
  367. std::string const& linkRuleVar, std::string const& extraFlags, bool relink)
  368. {
  369. // TODO: Merge the methods that call this method to avoid
  370. // code duplication.
  371. std::vector<std::string> commands;
  372. // Get the language to use for linking this library.
  373. std::string linkLanguage =
  374. this->GeneratorTarget->GetLinkerLanguage(this->GetConfigName());
  375. // Make sure we have a link language.
  376. if (linkLanguage.empty()) {
  377. cmSystemTools::Error("Cannot determine link language for target \"" +
  378. this->GeneratorTarget->GetName() + "\".");
  379. return;
  380. }
  381. auto linker = this->GeneratorTarget->GetLinkerTool(this->GetConfigName());
  382. // Build list of dependencies.
  383. std::vector<std::string> depends;
  384. this->AppendLinkDepends(depends, linkLanguage);
  385. if (!this->DeviceLinkObject.empty()) {
  386. depends.push_back(this->DeviceLinkObject);
  387. }
  388. // Create set of linking flags.
  389. std::string linkFlags;
  390. this->LocalGenerator->AppendFlags(linkFlags, extraFlags);
  391. this->LocalGenerator->AppendIPOLinkerFlags(
  392. linkFlags, this->GeneratorTarget, this->GetConfigName(), linkLanguage);
  393. // Add OSX version flags, if any.
  394. if (this->GeneratorTarget->GetType() == cmStateEnums::SHARED_LIBRARY ||
  395. this->GeneratorTarget->GetType() == cmStateEnums::MODULE_LIBRARY) {
  396. this->AppendOSXVerFlag(linkFlags, linkLanguage, "COMPATIBILITY", true);
  397. this->AppendOSXVerFlag(linkFlags, linkLanguage, "CURRENT", false);
  398. }
  399. // Construct the name of the library.
  400. this->GeneratorTarget->GetLibraryNames(this->GetConfigName());
  401. // Construct the full path version of the names.
  402. std::string outpath;
  403. std::string outpathImp;
  404. if (this->GeneratorTarget->IsFrameworkOnApple()) {
  405. outpath = this->GeneratorTarget->GetDirectory(this->GetConfigName());
  406. cmOSXBundleGenerator::SkipParts bundleSkipParts;
  407. if (this->GeneratorTarget->HasImportLibrary(this->GetConfigName())) {
  408. bundleSkipParts.TextStubs = false;
  409. }
  410. this->OSXBundleGenerator->CreateFramework(this->TargetNames.Output,
  411. outpath, this->GetConfigName(),
  412. bundleSkipParts);
  413. outpath += '/';
  414. if (!this->TargetNames.ImportLibrary.empty()) {
  415. outpathImp = this->GeneratorTarget->GetDirectory(
  416. this->GetConfigName(), cmStateEnums::ImportLibraryArtifact);
  417. cmSystemTools::MakeDirectory(outpathImp);
  418. outpathImp += '/';
  419. }
  420. } else if (this->GeneratorTarget->IsCFBundleOnApple()) {
  421. outpath = this->GeneratorTarget->GetDirectory(this->GetConfigName());
  422. this->OSXBundleGenerator->CreateCFBundle(this->TargetNames.Output, outpath,
  423. this->GetConfigName());
  424. outpath += '/';
  425. } else if (relink) {
  426. outpath = cmStrCat(this->Makefile->GetCurrentBinaryDirectory(),
  427. "/CMakeFiles/CMakeRelink.dir");
  428. cmSystemTools::MakeDirectory(outpath);
  429. outpath += '/';
  430. if (!this->TargetNames.ImportLibrary.empty()) {
  431. outpathImp = outpath;
  432. }
  433. } else {
  434. outpath = this->GeneratorTarget->GetDirectory(this->GetConfigName());
  435. cmSystemTools::MakeDirectory(outpath);
  436. outpath += '/';
  437. if (!this->TargetNames.ImportLibrary.empty()) {
  438. outpathImp = this->GeneratorTarget->GetDirectory(
  439. this->GetConfigName(), cmStateEnums::ImportLibraryArtifact);
  440. cmSystemTools::MakeDirectory(outpathImp);
  441. outpathImp += '/';
  442. }
  443. }
  444. std::string compilePdbOutputPath =
  445. this->GeneratorTarget->GetCompilePDBDirectory(this->GetConfigName());
  446. cmSystemTools::MakeDirectory(compilePdbOutputPath);
  447. std::string pdbOutputPath =
  448. this->GeneratorTarget->GetPDBDirectory(this->GetConfigName());
  449. cmSystemTools::MakeDirectory(pdbOutputPath);
  450. pdbOutputPath += "/";
  451. std::string targetFullPath = outpath + this->TargetNames.Output;
  452. std::string targetFullPathPDB = pdbOutputPath + this->TargetNames.PDB;
  453. std::string targetFullPathSO = outpath + this->TargetNames.SharedObject;
  454. std::string targetFullPathReal = outpath + this->TargetNames.Real;
  455. std::string targetFullPathImport =
  456. outpathImp + this->TargetNames.ImportLibrary;
  457. // Construct the output path version of the names for use in command
  458. // arguments.
  459. std::string targetOutPathPDB = this->LocalGenerator->ConvertToOutputFormat(
  460. targetFullPathPDB, cmOutputConverter::SHELL);
  461. std::string targetOutPath = this->LocalGenerator->ConvertToOutputFormat(
  462. this->LocalGenerator->MaybeRelativeToCurBinDir(targetFullPath),
  463. cmOutputConverter::SHELL);
  464. std::string targetOutPathSO = this->LocalGenerator->ConvertToOutputFormat(
  465. this->LocalGenerator->MaybeRelativeToCurBinDir(targetFullPathSO),
  466. cmOutputConverter::SHELL);
  467. std::string targetOutPathReal = this->LocalGenerator->ConvertToOutputFormat(
  468. this->LocalGenerator->MaybeRelativeToCurBinDir(targetFullPathReal),
  469. cmOutputConverter::SHELL);
  470. std::string targetOutPathImport =
  471. this->LocalGenerator->ConvertToOutputFormat(
  472. this->LocalGenerator->MaybeRelativeToCurBinDir(targetFullPathImport),
  473. cmOutputConverter::SHELL);
  474. this->NumberOfProgressActions++;
  475. if (!this->NoRuleMessages) {
  476. cmLocalUnixMakefileGenerator3::EchoProgress progress;
  477. this->MakeEchoProgress(progress);
  478. // Add the link message.
  479. std::string buildEcho = cmStrCat("Linking ", linkLanguage);
  480. switch (this->GeneratorTarget->GetType()) {
  481. case cmStateEnums::STATIC_LIBRARY:
  482. buildEcho += " static library ";
  483. break;
  484. case cmStateEnums::SHARED_LIBRARY:
  485. buildEcho += " shared library ";
  486. break;
  487. case cmStateEnums::MODULE_LIBRARY:
  488. if (this->GeneratorTarget->IsCFBundleOnApple()) {
  489. buildEcho += " CFBundle";
  490. }
  491. buildEcho += " shared module ";
  492. break;
  493. default:
  494. buildEcho += " library ";
  495. break;
  496. }
  497. buildEcho += targetOutPath;
  498. this->LocalGenerator->AppendEcho(
  499. commands, buildEcho, cmLocalUnixMakefileGenerator3::EchoLink, &progress);
  500. }
  501. // Clean files associated with this library.
  502. std::set<std::string> libCleanFiles;
  503. libCleanFiles.insert(
  504. this->LocalGenerator->MaybeRelativeToCurBinDir(targetFullPathReal));
  505. std::vector<std::string> commands1;
  506. // Add a command to remove any existing files for this library.
  507. // for static libs only
  508. if (this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY) {
  509. this->LocalGenerator->AppendCleanCommand(commands1, libCleanFiles,
  510. this->GeneratorTarget, "target");
  511. this->LocalGenerator->CreateCDCommand(
  512. commands1, this->Makefile->GetCurrentBinaryDirectory(),
  513. this->LocalGenerator->GetBinaryDirectory());
  514. cm::append(commands, commands1);
  515. commands1.clear();
  516. }
  517. if (this->TargetNames.Output != this->TargetNames.Real) {
  518. libCleanFiles.insert(
  519. this->LocalGenerator->MaybeRelativeToCurBinDir(targetFullPath));
  520. }
  521. if (this->TargetNames.SharedObject != this->TargetNames.Real &&
  522. this->TargetNames.SharedObject != this->TargetNames.Output) {
  523. libCleanFiles.insert(
  524. this->LocalGenerator->MaybeRelativeToCurBinDir(targetFullPathSO));
  525. }
  526. if (!this->TargetNames.ImportLibrary.empty()) {
  527. libCleanFiles.insert(
  528. this->LocalGenerator->MaybeRelativeToCurBinDir(targetFullPathImport));
  529. std::string implib;
  530. if (this->GeneratorTarget->GetImplibGNUtoMS(
  531. this->GetConfigName(), targetFullPathImport, implib)) {
  532. libCleanFiles.insert(
  533. this->LocalGenerator->MaybeRelativeToCurBinDir(implib));
  534. }
  535. }
  536. // List the PDB for cleaning only when the whole target is
  537. // cleaned. We do not want to delete the .pdb file just before
  538. // linking the target.
  539. this->CleanFiles.insert(
  540. this->LocalGenerator->MaybeRelativeToCurBinDir(targetFullPathPDB));
  541. #ifdef _WIN32
  542. // There may be a manifest file for this target. Add it to the
  543. // clean set just in case.
  544. if (this->GeneratorTarget->GetType() != cmStateEnums::STATIC_LIBRARY) {
  545. libCleanFiles.insert(this->LocalGenerator->MaybeRelativeToCurBinDir(
  546. targetFullPath + ".manifest"));
  547. }
  548. #endif
  549. // Add the pre-build and pre-link rules building but not when relinking.
  550. if (!relink) {
  551. this->LocalGenerator->AppendCustomCommands(
  552. commands, this->GeneratorTarget->GetPreBuildCommands(),
  553. this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
  554. this->LocalGenerator->AppendCustomCommands(
  555. commands, this->GeneratorTarget->GetPreLinkCommands(),
  556. this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
  557. }
  558. // Determine whether a link script will be used.
  559. bool useLinkScript = this->GlobalGenerator->GetUseLinkScript();
  560. bool useResponseFileForObjects =
  561. this->CheckUseResponseFileForObjects(linkLanguage);
  562. bool const useResponseFileForLibs =
  563. this->CheckUseResponseFileForLibraries(linkLanguage);
  564. // For static libraries there might be archiving rules.
  565. bool haveStaticLibraryRule = false;
  566. cmList archiveCreateCommands;
  567. cmList archiveAppendCommands;
  568. cmList archiveFinishCommands;
  569. std::string::size_type archiveCommandLimit = std::string::npos;
  570. if (this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY) {
  571. haveStaticLibraryRule = this->Makefile->IsDefinitionSet(linkRuleVar);
  572. std::string arCreateVar =
  573. cmStrCat("CMAKE_", linkLanguage, "_ARCHIVE_CREATE");
  574. arCreateVar = this->GeneratorTarget->GetFeatureSpecificLinkRuleVariable(
  575. arCreateVar, linkLanguage, this->GetConfigName());
  576. archiveCreateCommands.assign(this->Makefile->GetDefinition(arCreateVar));
  577. std::string arAppendVar =
  578. cmStrCat("CMAKE_", linkLanguage, "_ARCHIVE_APPEND");
  579. arAppendVar = this->GeneratorTarget->GetFeatureSpecificLinkRuleVariable(
  580. arAppendVar, linkLanguage, this->GetConfigName());
  581. archiveAppendCommands.assign(this->Makefile->GetDefinition(arAppendVar));
  582. std::string arFinishVar =
  583. cmStrCat("CMAKE_", linkLanguage, "_ARCHIVE_FINISH");
  584. arFinishVar = this->GeneratorTarget->GetFeatureSpecificLinkRuleVariable(
  585. arFinishVar, linkLanguage, this->GetConfigName());
  586. archiveFinishCommands.assign(this->Makefile->GetDefinition(arFinishVar));
  587. }
  588. // Decide whether to use archiving rules.
  589. bool useArchiveRules = !haveStaticLibraryRule &&
  590. !archiveCreateCommands.empty() && !archiveAppendCommands.empty();
  591. if (useArchiveRules) {
  592. // Archiving rules are always run with a link script.
  593. useLinkScript = true;
  594. // Archiving rules never use a response file.
  595. useResponseFileForObjects = false;
  596. // Limit the length of individual object lists to less than half of
  597. // the command line length limit (leaving half for other flags).
  598. // This may result in several calls to the archiver.
  599. if (size_t limit = cmSystemTools::CalculateCommandLineLengthLimit()) {
  600. archiveCommandLimit = limit / 2;
  601. } else {
  602. archiveCommandLimit = 8000;
  603. }
  604. }
  605. // Expand the rule variables.
  606. auto rulePlaceholderExpander =
  607. this->LocalGenerator->CreateRulePlaceholderExpander(
  608. cmBuildStep::Link, this->GeneratorTarget, linkLanguage);
  609. bool useWatcomQuote =
  610. this->Makefile->IsOn(linkRuleVar + "_USE_WATCOM_QUOTE");
  611. cmList real_link_commands;
  612. {
  613. // Set path conversion for link script shells.
  614. this->LocalGenerator->SetLinkScriptShell(useLinkScript);
  615. // Collect up flags to link in needed libraries.
  616. std::string linkLibs;
  617. if (this->GeneratorTarget->GetType() != cmStateEnums::STATIC_LIBRARY) {
  618. std::unique_ptr<cmLinkLineComputer> linkLineComputer =
  619. this->CreateLinkLineComputer(
  620. this->LocalGenerator,
  621. this->LocalGenerator->GetStateSnapshot().GetDirectory());
  622. linkLineComputer->SetForResponse(useResponseFileForLibs);
  623. linkLineComputer->SetUseWatcomQuote(useWatcomQuote);
  624. linkLineComputer->SetRelink(relink);
  625. this->CreateLinkLibs(linkLineComputer.get(), linkLibs,
  626. useResponseFileForLibs, depends, linkLanguage);
  627. }
  628. // Construct object file lists that may be needed to expand the
  629. // rule.
  630. std::string buildObjs;
  631. this->CreateObjectLists(useLinkScript, useArchiveRules,
  632. useResponseFileForObjects, buildObjs, depends,
  633. useWatcomQuote, linkLanguage);
  634. if (!this->DeviceLinkObject.empty()) {
  635. buildObjs += " " +
  636. this->LocalGenerator->ConvertToOutputFormat(
  637. this->LocalGenerator->MaybeRelativeToCurBinDir(
  638. this->DeviceLinkObject),
  639. cmOutputConverter::SHELL);
  640. }
  641. std::string const& aixExports = this->GetAIXExports(this->GetConfigName());
  642. // maybe create .def file from list of objects
  643. this->GenDefFile(real_link_commands);
  644. std::string manifests = this->GetManifests(this->GetConfigName());
  645. cmRulePlaceholderExpander::RuleVariables vars;
  646. vars.TargetPDB = targetOutPathPDB.c_str();
  647. // Setup the target version.
  648. std::string targetVersionMajor;
  649. std::string targetVersionMinor;
  650. {
  651. std::ostringstream majorStream;
  652. std::ostringstream minorStream;
  653. int major;
  654. int minor;
  655. this->GeneratorTarget->GetTargetVersion(major, minor);
  656. majorStream << major;
  657. minorStream << minor;
  658. targetVersionMajor = majorStream.str();
  659. targetVersionMinor = minorStream.str();
  660. }
  661. vars.TargetVersionMajor = targetVersionMajor.c_str();
  662. vars.TargetVersionMinor = targetVersionMinor.c_str();
  663. vars.CMTargetName = this->GeneratorTarget->GetName().c_str();
  664. vars.CMTargetType =
  665. cmState::GetTargetTypeName(this->GeneratorTarget->GetType()).c_str();
  666. vars.CMTargetLabels =
  667. this->GeneratorTarget->GetTargetLabelsString().c_str();
  668. vars.Language = linkLanguage.c_str();
  669. vars.Linker = linker.c_str();
  670. vars.AIXExports = aixExports.c_str();
  671. vars.Objects = buildObjs.c_str();
  672. std::string objectDir = this->GeneratorTarget->GetSupportDirectory();
  673. objectDir = this->LocalGenerator->ConvertToOutputFormat(
  674. this->LocalGenerator->MaybeRelativeToCurBinDir(objectDir),
  675. cmOutputConverter::SHELL);
  676. vars.ObjectDir = objectDir.c_str();
  677. std::string target = this->LocalGenerator->ConvertToOutputFormat(
  678. this->LocalGenerator->MaybeRelativeToCurBinDir(targetFullPathReal),
  679. cmOutputConverter::SHELL, useWatcomQuote);
  680. vars.Target = target.c_str();
  681. vars.LinkLibraries = linkLibs.c_str();
  682. vars.ObjectsQuoted = buildObjs.c_str();
  683. std::string targetOutSOName;
  684. if (this->GeneratorTarget->HasSOName(this->GetConfigName()) ||
  685. this->GeneratorTarget->IsArchivedAIXSharedLibrary()) {
  686. vars.SONameFlag = this->Makefile->GetSONameFlag(linkLanguage);
  687. targetOutSOName = this->LocalGenerator->ConvertToOutputFormat(
  688. this->TargetNames.SharedObject, cmOutputConverter::SHELL);
  689. vars.TargetSOName = targetOutSOName.c_str();
  690. }
  691. vars.LinkFlags = linkFlags.c_str();
  692. vars.Manifests = manifests.c_str();
  693. vars.Config = this->GetConfigName().c_str();
  694. // Compute the directory portion of the install_name setting.
  695. std::string install_name_dir;
  696. if (this->GeneratorTarget->GetType() == cmStateEnums::SHARED_LIBRARY) {
  697. // Get the install_name directory for the build tree.
  698. install_name_dir = this->GeneratorTarget->GetInstallNameDirForBuildTree(
  699. this->GetConfigName());
  700. // Set the rule variable replacement value.
  701. if (install_name_dir.empty()) {
  702. vars.TargetInstallNameDir = "";
  703. } else {
  704. // Convert to a path for the native build tool.
  705. install_name_dir = this->LocalGenerator->ConvertToOutputFormat(
  706. install_name_dir, cmOutputConverter::SHELL);
  707. vars.TargetInstallNameDir = install_name_dir.c_str();
  708. }
  709. }
  710. // Add language-specific flags.
  711. std::string langFlags;
  712. this->LocalGenerator->AddLanguageFlagsForLinking(
  713. langFlags, this->GeneratorTarget, linkLanguage, this->GetConfigName());
  714. this->LocalGenerator->AddArchitectureFlags(
  715. langFlags, this->GeneratorTarget, linkLanguage, this->GetConfigName());
  716. vars.LanguageCompileFlags = langFlags.c_str();
  717. std::string linkerLauncher =
  718. this->GetLinkerLauncher(this->GetConfigName());
  719. if (cmNonempty(linkerLauncher)) {
  720. vars.Launcher = linkerLauncher.c_str();
  721. }
  722. std::string launcher;
  723. std::string val = this->LocalGenerator->GetRuleLauncher(
  724. this->GeneratorTarget, "RULE_LAUNCH_LINK",
  725. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE"));
  726. if (cmNonempty(val)) {
  727. launcher = cmStrCat(val, ' ');
  728. }
  729. // Construct the main link rule and expand placeholders.
  730. rulePlaceholderExpander->SetTargetImpLib(targetOutPathImport);
  731. if (useArchiveRules) {
  732. // Construct the individual object list strings.
  733. std::vector<std::string> object_strings;
  734. this->WriteObjectsStrings(object_strings, false, archiveCommandLimit);
  735. // Add the cuda device object to the list of archive files. This will
  736. // only occur on archives which have CUDA_RESOLVE_DEVICE_SYMBOLS enabled
  737. if (!this->DeviceLinkObject.empty()) {
  738. object_strings.push_back(this->LocalGenerator->ConvertToOutputFormat(
  739. this->LocalGenerator->MaybeRelativeToCurBinDir(
  740. this->DeviceLinkObject),
  741. cmOutputConverter::SHELL));
  742. }
  743. // Create the archive with the first set of objects.
  744. auto osi = object_strings.begin();
  745. {
  746. vars.Objects = osi->c_str();
  747. for (std::string const& acc : archiveCreateCommands) {
  748. std::string cmd = launcher + acc;
  749. rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator,
  750. cmd, vars);
  751. real_link_commands.push_back(std::move(cmd));
  752. }
  753. }
  754. // Append to the archive with the other object sets.
  755. for (++osi; osi != object_strings.end(); ++osi) {
  756. vars.Objects = osi->c_str();
  757. for (std::string const& aac : archiveAppendCommands) {
  758. std::string cmd = launcher + aac;
  759. rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator,
  760. cmd, vars);
  761. real_link_commands.push_back(std::move(cmd));
  762. }
  763. }
  764. // Finish the archive.
  765. vars.Objects = "";
  766. for (std::string const& afc : archiveFinishCommands) {
  767. std::string cmd = launcher + afc;
  768. rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator, cmd,
  769. vars);
  770. // If there is no ranlib the command will be ":". Skip it.
  771. if (!cmd.empty() && cmd[0] != ':') {
  772. real_link_commands.push_back(std::move(cmd));
  773. }
  774. }
  775. } else {
  776. // Get the set of commands.
  777. std::string linkRule = this->GetLinkRule(linkRuleVar);
  778. real_link_commands.append(linkRule);
  779. if (this->UseLWYU) {
  780. cmValue lwyuCheck =
  781. this->Makefile->GetDefinition("CMAKE_LINK_WHAT_YOU_USE_CHECK");
  782. if (lwyuCheck) {
  783. std::string cmakeCommand = cmStrCat(
  784. this->LocalGenerator->ConvertToOutputFormat(
  785. cmSystemTools::GetCMakeCommand(), cmLocalGenerator::SHELL),
  786. " -E __run_co_compile --lwyu=");
  787. cmakeCommand += this->LocalGenerator->EscapeForShell(*lwyuCheck);
  788. cmakeCommand += cmStrCat(" --source=", targetOutPathReal);
  789. real_link_commands.push_back(std::move(cmakeCommand));
  790. }
  791. }
  792. // Expand placeholders.
  793. for (auto& real_link_command : real_link_commands) {
  794. real_link_command = cmStrCat(launcher, real_link_command);
  795. rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator,
  796. real_link_command, vars);
  797. }
  798. }
  799. // Restore path conversion to normal shells.
  800. this->LocalGenerator->SetLinkScriptShell(false);
  801. }
  802. // Optionally convert the build rule to use a script to avoid long
  803. // command lines in the make shell.
  804. if (useLinkScript) {
  805. // Use a link script.
  806. char const* name = (relink ? "relink.txt" : "link.txt");
  807. this->CreateLinkScript(name, real_link_commands, commands1, depends);
  808. } else {
  809. // No link script. Just use the link rule directly.
  810. commands1 = real_link_commands;
  811. }
  812. this->LocalGenerator->CreateCDCommand(
  813. commands1, this->Makefile->GetCurrentBinaryDirectory(),
  814. this->LocalGenerator->GetBinaryDirectory());
  815. cm::append(commands, commands1);
  816. commands1.clear();
  817. // Add a rule to create necessary symlinks for the library.
  818. // Frameworks are handled by cmOSXBundleGenerator.
  819. if (targetOutPath != targetOutPathReal &&
  820. !this->GeneratorTarget->IsFrameworkOnApple()) {
  821. std::string symlink =
  822. cmStrCat("$(CMAKE_COMMAND) -E cmake_symlink_library ", targetOutPathReal,
  823. ' ', targetOutPathSO, ' ', targetOutPath);
  824. commands1.push_back(std::move(symlink));
  825. this->LocalGenerator->CreateCDCommand(
  826. commands1, this->Makefile->GetCurrentBinaryDirectory(),
  827. this->LocalGenerator->GetBinaryDirectory());
  828. cm::append(commands, commands1);
  829. commands1.clear();
  830. }
  831. // Add the post-build rules when building but not when relinking.
  832. if (!relink) {
  833. this->LocalGenerator->AppendCustomCommands(
  834. commands, this->GeneratorTarget->GetPostBuildCommands(),
  835. this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
  836. }
  837. // Compute the list of outputs.
  838. std::vector<std::string> outputs;
  839. outputs.reserve(3);
  840. outputs.push_back(targetFullPathReal);
  841. if (this->TargetNames.SharedObject != this->TargetNames.Real) {
  842. outputs.push_back(targetFullPathSO);
  843. }
  844. if (this->TargetNames.Output != this->TargetNames.SharedObject &&
  845. this->TargetNames.Output != this->TargetNames.Real) {
  846. outputs.push_back(targetFullPath);
  847. }
  848. // Write the build rule.
  849. this->WriteMakeRule(*this->BuildFileStream, nullptr, outputs, depends,
  850. commands, false);
  851. // Add rule to generate text-based stubs, if required
  852. if (this->GeneratorTarget->IsApple() &&
  853. this->GeneratorTarget->HasImportLibrary(this->GetConfigName())) {
  854. auto genStubsRule =
  855. this->Makefile->GetDefinition("CMAKE_CREATE_TEXT_STUBS");
  856. cmList genStubs_commands{ genStubsRule };
  857. this->LocalGenerator->CreateCDCommand(
  858. genStubs_commands, this->Makefile->GetCurrentBinaryDirectory(),
  859. this->LocalGenerator->GetBinaryDirectory());
  860. std::string TBDFullPath =
  861. cmStrCat(outpathImp, this->TargetNames.ImportOutput);
  862. std::string TBDFullPathReal =
  863. cmStrCat(outpathImp, this->TargetNames.ImportReal);
  864. std::string TBDFullPathSO =
  865. cmStrCat(outpathImp, this->TargetNames.ImportLibrary);
  866. // Expand placeholders.
  867. cmRulePlaceholderExpander::RuleVariables vars;
  868. std::string target = this->LocalGenerator->ConvertToOutputFormat(
  869. this->LocalGenerator->MaybeRelativeToCurBinDir(targetFullPathReal),
  870. cmOutputConverter::SHELL, useWatcomQuote);
  871. vars.Target = target.c_str();
  872. std::string TBDOutPathReal = this->LocalGenerator->ConvertToOutputFormat(
  873. this->LocalGenerator->MaybeRelativeToCurBinDir(TBDFullPathReal),
  874. cmOutputConverter::SHELL, useWatcomQuote);
  875. rulePlaceholderExpander->SetTargetImpLib(TBDOutPathReal);
  876. for (std::string& command : genStubs_commands) {
  877. rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator,
  878. command, vars);
  879. }
  880. outputs.clear();
  881. outputs.push_back(TBDFullPathReal);
  882. if (this->TargetNames.ImportLibrary != this->TargetNames.ImportReal) {
  883. outputs.push_back(TBDFullPathSO);
  884. }
  885. if (this->TargetNames.ImportOutput != this->TargetNames.ImportLibrary &&
  886. this->TargetNames.ImportOutput != this->TargetNames.ImportReal) {
  887. outputs.push_back(TBDFullPath);
  888. }
  889. this->ExtraFiles.insert(TBDFullPath);
  890. depends.clear();
  891. depends.push_back(targetFullPathReal);
  892. // Add a rule to create necessary symlinks for the library.
  893. // Frameworks are handled by cmOSXBundleGenerator.
  894. if (TBDFullPath != TBDFullPathReal &&
  895. !this->GeneratorTarget->IsFrameworkOnApple()) {
  896. auto TBDOutPathSO = this->LocalGenerator->ConvertToOutputFormat(
  897. this->LocalGenerator->MaybeRelativeToCurBinDir(TBDFullPathSO),
  898. cmOutputConverter::SHELL, useWatcomQuote);
  899. auto TBDOutPath = this->LocalGenerator->ConvertToOutputFormat(
  900. this->LocalGenerator->MaybeRelativeToCurBinDir(TBDFullPath),
  901. cmOutputConverter::SHELL, useWatcomQuote);
  902. std::string symlink =
  903. cmStrCat("$(CMAKE_COMMAND) -E cmake_symlink_library ", TBDOutPathReal,
  904. ' ', TBDOutPathSO, ' ', TBDOutPath);
  905. commands1.push_back(std::move(symlink));
  906. this->LocalGenerator->CreateCDCommand(
  907. commands1, this->Makefile->GetCurrentBinaryDirectory(),
  908. this->LocalGenerator->GetBinaryDirectory());
  909. cm::append(genStubs_commands, commands1);
  910. commands1.clear();
  911. }
  912. this->WriteMakeRule(*this->BuildFileStream, nullptr, outputs, depends,
  913. genStubs_commands, false);
  914. // clean actions for apple specific outputs
  915. // clean actions for ImportLibrary are already specified
  916. if (this->TargetNames.ImportReal != this->TargetNames.ImportLibrary) {
  917. libCleanFiles.insert(
  918. this->LocalGenerator->MaybeRelativeToCurBinDir(TBDFullPathReal));
  919. }
  920. if (this->TargetNames.ImportOutput != this->TargetNames.ImportReal &&
  921. this->TargetNames.ImportOutput != this->TargetNames.ImportLibrary) {
  922. libCleanFiles.insert(
  923. this->LocalGenerator->MaybeRelativeToCurBinDir(TBDFullPath));
  924. }
  925. }
  926. // Write the main driver rule to build everything in this target.
  927. this->WriteTargetDriverRule(targetFullPath, relink);
  928. // Clean all the possible library names and symlinks.
  929. this->CleanFiles.insert(libCleanFiles.begin(), libCleanFiles.end());
  930. }