cmInstallCommand.cxx 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494
  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 "cmInstallCommand.h"
  4. #include <algorithm>
  5. #include <cassert>
  6. #include <cstddef>
  7. #include <iterator>
  8. #include <map>
  9. #include <set>
  10. #include <sstream>
  11. #include <utility>
  12. #include <cm/memory>
  13. #include <cm/optional>
  14. #include <cm/string_view>
  15. #include <cmext/string_view>
  16. #include "cmsys/Glob.hxx"
  17. #include "cmArgumentParser.h"
  18. #include "cmExecutionStatus.h"
  19. #include "cmExperimental.h"
  20. #include "cmExportSet.h"
  21. #include "cmFileSet.h"
  22. #include "cmGeneratorExpression.h"
  23. #include "cmGlobalGenerator.h"
  24. #include "cmInstallCommandArguments.h"
  25. #include "cmInstallCxxModuleBmiGenerator.h"
  26. #include "cmInstallDirectoryGenerator.h"
  27. #include "cmInstallExportGenerator.h"
  28. #include "cmInstallFileSetGenerator.h"
  29. #include "cmInstallFilesGenerator.h"
  30. #include "cmInstallGenerator.h"
  31. #include "cmInstallGetRuntimeDependenciesGenerator.h"
  32. #include "cmInstallImportedRuntimeArtifactsGenerator.h"
  33. #include "cmInstallRuntimeDependencySet.h"
  34. #include "cmInstallRuntimeDependencySetGenerator.h"
  35. #include "cmInstallScriptGenerator.h"
  36. #include "cmInstallTargetGenerator.h"
  37. #include "cmMakefile.h"
  38. #include "cmMessageType.h"
  39. #include "cmPolicies.h"
  40. #include "cmRange.h"
  41. #include "cmRuntimeDependencyArchive.h"
  42. #include "cmStateTypes.h"
  43. #include "cmStringAlgorithms.h"
  44. #include "cmSubcommandTable.h"
  45. #include "cmSystemTools.h"
  46. #include "cmTarget.h"
  47. #include "cmTargetExport.h"
  48. #include "cmValue.h"
  49. class cmListFileBacktrace;
  50. namespace {
  51. struct RuntimeDependenciesArgs
  52. {
  53. std::vector<std::string> Directories;
  54. std::vector<std::string> PreIncludeRegexes;
  55. std::vector<std::string> PreExcludeRegexes;
  56. std::vector<std::string> PostIncludeRegexes;
  57. std::vector<std::string> PostExcludeRegexes;
  58. std::vector<std::string> PostIncludeFiles;
  59. std::vector<std::string> PostExcludeFiles;
  60. };
  61. auto const RuntimeDependenciesArgHelper =
  62. cmArgumentParser<RuntimeDependenciesArgs>{}
  63. .Bind("DIRECTORIES"_s, &RuntimeDependenciesArgs::Directories)
  64. .Bind("PRE_INCLUDE_REGEXES"_s, &RuntimeDependenciesArgs::PreIncludeRegexes)
  65. .Bind("PRE_EXCLUDE_REGEXES"_s, &RuntimeDependenciesArgs::PreExcludeRegexes)
  66. .Bind("POST_INCLUDE_REGEXES"_s,
  67. &RuntimeDependenciesArgs::PostIncludeRegexes)
  68. .Bind("POST_EXCLUDE_REGEXES"_s,
  69. &RuntimeDependenciesArgs::PostExcludeRegexes)
  70. .Bind("POST_INCLUDE_FILES"_s, &RuntimeDependenciesArgs::PostIncludeFiles)
  71. .Bind("POST_EXCLUDE_FILES"_s, &RuntimeDependenciesArgs::PostExcludeFiles);
  72. class Helper
  73. {
  74. public:
  75. Helper(cmExecutionStatus& status)
  76. : Status(status)
  77. , Makefile(&status.GetMakefile())
  78. {
  79. this->DefaultComponentName = this->Makefile->GetSafeDefinition(
  80. "CMAKE_INSTALL_DEFAULT_COMPONENT_NAME");
  81. if (this->DefaultComponentName.empty()) {
  82. this->DefaultComponentName = "Unspecified";
  83. }
  84. }
  85. void SetError(std::string const& err) { this->Status.SetError(err); }
  86. bool MakeFilesFullPath(const char* modeName,
  87. const std::vector<std::string>& relFiles,
  88. std::vector<std::string>& absFiles);
  89. bool MakeFilesFullPath(const char* modeName, const std::string& basePath,
  90. const std::vector<std::string>& relFiles,
  91. std::vector<std::string>& absFiles);
  92. bool CheckCMP0006(bool& failure) const;
  93. std::string GetDestination(const cmInstallCommandArguments* args,
  94. const std::string& varName,
  95. const std::string& guess) const;
  96. std::string GetRuntimeDestination(
  97. const cmInstallCommandArguments* args) const;
  98. std::string GetSbinDestination(const cmInstallCommandArguments* args) const;
  99. std::string GetArchiveDestination(
  100. const cmInstallCommandArguments* args) const;
  101. std::string GetLibraryDestination(
  102. const cmInstallCommandArguments* args) const;
  103. std::string GetCxxModulesBmiDestination(
  104. const cmInstallCommandArguments* args) const;
  105. std::string GetIncludeDestination(
  106. const cmInstallCommandArguments* args) const;
  107. std::string GetSysconfDestination(
  108. const cmInstallCommandArguments* args) const;
  109. std::string GetSharedStateDestination(
  110. const cmInstallCommandArguments* args) const;
  111. std::string GetLocalStateDestination(
  112. const cmInstallCommandArguments* args) const;
  113. std::string GetRunStateDestination(
  114. const cmInstallCommandArguments* args) const;
  115. std::string GetDataRootDestination(
  116. const cmInstallCommandArguments* args) const;
  117. std::string GetDataDestination(const cmInstallCommandArguments* args) const;
  118. std::string GetInfoDestination(const cmInstallCommandArguments* args) const;
  119. std::string GetLocaleDestination(
  120. const cmInstallCommandArguments* args) const;
  121. std::string GetManDestination(const cmInstallCommandArguments* args) const;
  122. std::string GetDocDestination(const cmInstallCommandArguments* args) const;
  123. std::string GetDestinationForType(const cmInstallCommandArguments* args,
  124. const std::string& type) const;
  125. cmExecutionStatus& Status;
  126. cmMakefile* Makefile;
  127. std::string DefaultComponentName;
  128. };
  129. std::unique_ptr<cmInstallTargetGenerator> CreateInstallTargetGenerator(
  130. cmTarget& target, const cmInstallCommandArguments& args, bool impLib,
  131. cmListFileBacktrace const& backtrace, const std::string& destination,
  132. bool forceOpt = false, bool namelink = false)
  133. {
  134. cmInstallGenerator::MessageLevel message =
  135. cmInstallGenerator::SelectMessageLevel(target.GetMakefile());
  136. target.SetHaveInstallRule(true);
  137. const std::string& component =
  138. namelink ? args.GetNamelinkComponent() : args.GetComponent();
  139. auto g = cm::make_unique<cmInstallTargetGenerator>(
  140. target.GetName(), destination, impLib, args.GetPermissions(),
  141. args.GetConfigurations(), component, message, args.GetExcludeFromAll(),
  142. args.GetOptional() || forceOpt, backtrace);
  143. target.AddInstallGenerator(g.get());
  144. return g;
  145. }
  146. std::unique_ptr<cmInstallTargetGenerator> CreateInstallTargetGenerator(
  147. cmTarget& target, const cmInstallCommandArguments& args, bool impLib,
  148. cmListFileBacktrace const& backtrace, bool forceOpt = false,
  149. bool namelink = false)
  150. {
  151. return CreateInstallTargetGenerator(target, args, impLib, backtrace,
  152. args.GetDestination(), forceOpt,
  153. namelink);
  154. }
  155. std::unique_ptr<cmInstallFilesGenerator> CreateInstallFilesGenerator(
  156. cmMakefile* mf, const std::vector<std::string>& absFiles,
  157. const cmInstallCommandArguments& args, bool programs,
  158. const std::string& destination)
  159. {
  160. cmInstallGenerator::MessageLevel message =
  161. cmInstallGenerator::SelectMessageLevel(mf);
  162. return cm::make_unique<cmInstallFilesGenerator>(
  163. absFiles, destination, programs, args.GetPermissions(),
  164. args.GetConfigurations(), args.GetComponent(), message,
  165. args.GetExcludeFromAll(), args.GetRename(), args.GetOptional(),
  166. mf->GetBacktrace());
  167. }
  168. std::unique_ptr<cmInstallFilesGenerator> CreateInstallFilesGenerator(
  169. cmMakefile* mf, const std::vector<std::string>& absFiles,
  170. const cmInstallCommandArguments& args, bool programs)
  171. {
  172. return CreateInstallFilesGenerator(mf, absFiles, args, programs,
  173. args.GetDestination());
  174. }
  175. std::unique_ptr<cmInstallFileSetGenerator> CreateInstallFileSetGenerator(
  176. Helper& helper, cmTarget& target, cmFileSet* fileSet,
  177. const std::string& destination, const cmInstallCommandArguments& args)
  178. {
  179. cmInstallGenerator::MessageLevel message =
  180. cmInstallGenerator::SelectMessageLevel(helper.Makefile);
  181. return cm::make_unique<cmInstallFileSetGenerator>(
  182. target.GetName(), fileSet, destination, args.GetPermissions(),
  183. args.GetConfigurations(), args.GetComponent(), message,
  184. args.GetExcludeFromAll(), args.GetOptional(),
  185. helper.Makefile->GetBacktrace());
  186. }
  187. void AddInstallRuntimeDependenciesGenerator(
  188. Helper& helper, cmInstallRuntimeDependencySet* runtimeDependencySet,
  189. const cmInstallCommandArguments& runtimeArgs,
  190. const cmInstallCommandArguments& libraryArgs,
  191. const cmInstallCommandArguments& frameworkArgs,
  192. RuntimeDependenciesArgs runtimeDependenciesArgs, bool& installsRuntime,
  193. bool& installsLibrary, bool& installsFramework)
  194. {
  195. bool dllPlatform =
  196. !helper.Makefile->GetSafeDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX").empty();
  197. bool apple =
  198. helper.Makefile->GetSafeDefinition("CMAKE_HOST_SYSTEM_NAME") == "Darwin";
  199. auto const& runtimeDependenciesArgsRef =
  200. dllPlatform ? runtimeArgs : libraryArgs;
  201. std::vector<std::string> configurations =
  202. runtimeDependenciesArgsRef.GetConfigurations();
  203. if (apple) {
  204. std::copy(frameworkArgs.GetConfigurations().begin(),
  205. frameworkArgs.GetConfigurations().end(),
  206. std::back_inserter(configurations));
  207. }
  208. // Create file(GET_RUNTIME_DEPENDENCIES) generator.
  209. auto getRuntimeDependenciesGenerator =
  210. cm::make_unique<cmInstallGetRuntimeDependenciesGenerator>(
  211. runtimeDependencySet, std::move(runtimeDependenciesArgs.Directories),
  212. std::move(runtimeDependenciesArgs.PreIncludeRegexes),
  213. std::move(runtimeDependenciesArgs.PreExcludeRegexes),
  214. std::move(runtimeDependenciesArgs.PostIncludeRegexes),
  215. std::move(runtimeDependenciesArgs.PostExcludeRegexes),
  216. std::move(runtimeDependenciesArgs.PostIncludeFiles),
  217. std::move(runtimeDependenciesArgs.PostExcludeFiles),
  218. runtimeDependenciesArgsRef.GetComponent(),
  219. apple ? frameworkArgs.GetComponent() : "", true, "_CMAKE_DEPS",
  220. "_CMAKE_RPATH", configurations,
  221. cmInstallGenerator::SelectMessageLevel(helper.Makefile),
  222. runtimeDependenciesArgsRef.GetExcludeFromAll() &&
  223. (apple ? frameworkArgs.GetExcludeFromAll() : true),
  224. helper.Makefile->GetBacktrace());
  225. helper.Makefile->AddInstallGenerator(
  226. std::move(getRuntimeDependenciesGenerator));
  227. // Create the library dependencies generator.
  228. auto libraryRuntimeDependenciesGenerator =
  229. cm::make_unique<cmInstallRuntimeDependencySetGenerator>(
  230. cmInstallRuntimeDependencySetGenerator::DependencyType::Library,
  231. runtimeDependencySet, std::vector<std::string>{}, true, std::string{},
  232. true, "_CMAKE_DEPS", "_CMAKE_RPATH", "_CMAKE_TMP",
  233. dllPlatform ? helper.GetRuntimeDestination(&runtimeArgs)
  234. : helper.GetLibraryDestination(&libraryArgs),
  235. runtimeDependenciesArgsRef.GetConfigurations(),
  236. runtimeDependenciesArgsRef.GetComponent(),
  237. runtimeDependenciesArgsRef.GetPermissions(),
  238. cmInstallGenerator::SelectMessageLevel(helper.Makefile),
  239. runtimeDependenciesArgsRef.GetExcludeFromAll(),
  240. helper.Makefile->GetBacktrace());
  241. helper.Makefile->AddInstallGenerator(
  242. std::move(libraryRuntimeDependenciesGenerator));
  243. if (dllPlatform) {
  244. installsRuntime = true;
  245. } else {
  246. installsLibrary = true;
  247. }
  248. if (apple) {
  249. // Create the framework dependencies generator.
  250. auto frameworkRuntimeDependenciesGenerator =
  251. cm::make_unique<cmInstallRuntimeDependencySetGenerator>(
  252. cmInstallRuntimeDependencySetGenerator::DependencyType::Framework,
  253. runtimeDependencySet, std::vector<std::string>{}, true, std::string{},
  254. true, "_CMAKE_DEPS", "_CMAKE_RPATH", "_CMAKE_TMP",
  255. frameworkArgs.GetDestination(), frameworkArgs.GetConfigurations(),
  256. frameworkArgs.GetComponent(), frameworkArgs.GetPermissions(),
  257. cmInstallGenerator::SelectMessageLevel(helper.Makefile),
  258. frameworkArgs.GetExcludeFromAll(), helper.Makefile->GetBacktrace());
  259. helper.Makefile->AddInstallGenerator(
  260. std::move(frameworkRuntimeDependenciesGenerator));
  261. installsFramework = true;
  262. }
  263. }
  264. std::set<std::string> const allowedTypes{
  265. "BIN", "SBIN", "LIB", "INCLUDE", "SYSCONF",
  266. "SHAREDSTATE", "LOCALSTATE", "RUNSTATE", "DATA", "INFO",
  267. "LOCALE", "MAN", "DOC",
  268. };
  269. template <typename T>
  270. bool AddBundleExecutable(Helper& helper,
  271. cmInstallRuntimeDependencySet* runtimeDependencySet,
  272. T&& bundleExecutable)
  273. {
  274. if (!runtimeDependencySet->AddBundleExecutable(bundleExecutable)) {
  275. helper.SetError(
  276. "A runtime dependency set may only have one bundle executable.");
  277. return false;
  278. }
  279. return true;
  280. }
  281. bool HandleScriptMode(std::vector<std::string> const& args,
  282. cmExecutionStatus& status)
  283. {
  284. Helper helper(status);
  285. std::string component = helper.DefaultComponentName;
  286. int componentCount = 0;
  287. bool doing_script = false;
  288. bool doing_code = false;
  289. bool exclude_from_all = false;
  290. bool all_components = false;
  291. // Scan the args once for COMPONENT. Only allow one.
  292. //
  293. for (size_t i = 0; i < args.size(); ++i) {
  294. if (args[i] == "COMPONENT" && i + 1 < args.size()) {
  295. ++componentCount;
  296. ++i;
  297. component = args[i];
  298. }
  299. if (args[i] == "EXCLUDE_FROM_ALL") {
  300. exclude_from_all = true;
  301. } else if (args[i] == "ALL_COMPONENTS") {
  302. all_components = true;
  303. }
  304. }
  305. if (componentCount > 1) {
  306. status.SetError("given more than one COMPONENT for the SCRIPT or CODE "
  307. "signature of the INSTALL command. "
  308. "Use multiple INSTALL commands with one COMPONENT each.");
  309. return false;
  310. }
  311. if (all_components && componentCount == 1) {
  312. status.SetError("ALL_COMPONENTS and COMPONENT are mutually exclusive");
  313. return false;
  314. }
  315. // Scan the args again, this time adding install generators each time we
  316. // encounter a SCRIPT or CODE arg:
  317. //
  318. for (std::string const& arg : args) {
  319. if (arg == "SCRIPT") {
  320. doing_script = true;
  321. doing_code = false;
  322. } else if (arg == "CODE") {
  323. doing_script = false;
  324. doing_code = true;
  325. } else if (arg == "COMPONENT") {
  326. doing_script = false;
  327. doing_code = false;
  328. } else if (doing_script) {
  329. doing_script = false;
  330. std::string script = arg;
  331. if (!cmSystemTools::FileIsFullPath(script)) {
  332. script =
  333. cmStrCat(helper.Makefile->GetCurrentSourceDirectory(), '/', arg);
  334. }
  335. if (cmSystemTools::FileIsDirectory(script)) {
  336. status.SetError("given a directory as value of SCRIPT argument.");
  337. return false;
  338. }
  339. helper.Makefile->AddInstallGenerator(
  340. cm::make_unique<cmInstallScriptGenerator>(
  341. script, false, component, exclude_from_all, all_components,
  342. helper.Makefile->GetBacktrace()));
  343. } else if (doing_code) {
  344. doing_code = false;
  345. std::string const& code = arg;
  346. helper.Makefile->AddInstallGenerator(
  347. cm::make_unique<cmInstallScriptGenerator>(
  348. code, true, component, exclude_from_all, all_components,
  349. helper.Makefile->GetBacktrace()));
  350. }
  351. }
  352. if (doing_script) {
  353. status.SetError("given no value for SCRIPT argument.");
  354. return false;
  355. }
  356. if (doing_code) {
  357. status.SetError("given no value for CODE argument.");
  358. return false;
  359. }
  360. // Tell the global generator about any installation component names
  361. // specified.
  362. helper.Makefile->GetGlobalGenerator()->AddInstallComponent(component);
  363. return true;
  364. }
  365. bool HandleTargetsMode(std::vector<std::string> const& args,
  366. cmExecutionStatus& status)
  367. {
  368. Helper helper(status);
  369. // This is the TARGETS mode.
  370. std::vector<cmTarget*> targets;
  371. struct ArgVectors
  372. {
  373. std::vector<std::string> Archive;
  374. std::vector<std::string> Library;
  375. std::vector<std::string> Runtime;
  376. std::vector<std::string> Object;
  377. std::vector<std::string> Framework;
  378. std::vector<std::string> Bundle;
  379. std::vector<std::string> Includes;
  380. std::vector<std::string> PrivateHeader;
  381. std::vector<std::string> PublicHeader;
  382. std::vector<std::string> Resource;
  383. std::vector<std::vector<std::string>> FileSets;
  384. std::vector<std::string> CxxModulesBmi;
  385. };
  386. static auto const argHelper =
  387. cmArgumentParser<ArgVectors>{}
  388. .Bind("ARCHIVE"_s, &ArgVectors::Archive)
  389. .Bind("LIBRARY"_s, &ArgVectors::Library)
  390. .Bind("RUNTIME"_s, &ArgVectors::Runtime)
  391. .Bind("OBJECTS"_s, &ArgVectors::Object)
  392. .Bind("FRAMEWORK"_s, &ArgVectors::Framework)
  393. .Bind("BUNDLE"_s, &ArgVectors::Bundle)
  394. .Bind("INCLUDES"_s, &ArgVectors::Includes)
  395. .Bind("PRIVATE_HEADER"_s, &ArgVectors::PrivateHeader)
  396. .Bind("PUBLIC_HEADER"_s, &ArgVectors::PublicHeader)
  397. .Bind("RESOURCE"_s, &ArgVectors::Resource)
  398. .Bind("FILE_SET"_s, &ArgVectors::FileSets)
  399. .Bind("CXX_MODULES_BMI"_s, &ArgVectors::CxxModulesBmi);
  400. std::vector<std::string> genericArgVector;
  401. ArgVectors const argVectors = argHelper.Parse(args, &genericArgVector);
  402. // now parse the generic args (i.e. the ones not specialized on LIBRARY/
  403. // ARCHIVE, RUNTIME etc. (see above)
  404. // These generic args also contain the targets and the export stuff
  405. std::vector<std::string> targetList;
  406. std::string exports;
  407. cm::optional<std::vector<std::string>> runtimeDependenciesArgVector;
  408. std::string runtimeDependencySetArg;
  409. std::vector<std::string> unknownArgs;
  410. cmInstallCommandArguments genericArgs(helper.DefaultComponentName);
  411. genericArgs.Bind("TARGETS"_s, targetList);
  412. genericArgs.Bind("EXPORT"_s, exports);
  413. genericArgs.Bind("RUNTIME_DEPENDENCIES"_s, runtimeDependenciesArgVector);
  414. genericArgs.Bind("RUNTIME_DEPENDENCY_SET"_s, runtimeDependencySetArg);
  415. genericArgs.Parse(genericArgVector, &unknownArgs);
  416. bool success = genericArgs.Finalize();
  417. RuntimeDependenciesArgs runtimeDependenciesArgs =
  418. runtimeDependenciesArgVector
  419. ? RuntimeDependenciesArgHelper.Parse(*runtimeDependenciesArgVector,
  420. &unknownArgs)
  421. : RuntimeDependenciesArgs();
  422. cmInstallCommandArguments archiveArgs(helper.DefaultComponentName);
  423. cmInstallCommandArguments libraryArgs(helper.DefaultComponentName);
  424. cmInstallCommandArguments runtimeArgs(helper.DefaultComponentName);
  425. cmInstallCommandArguments objectArgs(helper.DefaultComponentName);
  426. cmInstallCommandArguments frameworkArgs(helper.DefaultComponentName);
  427. cmInstallCommandArguments bundleArgs(helper.DefaultComponentName);
  428. cmInstallCommandArguments privateHeaderArgs(helper.DefaultComponentName);
  429. cmInstallCommandArguments publicHeaderArgs(helper.DefaultComponentName);
  430. cmInstallCommandArguments resourceArgs(helper.DefaultComponentName);
  431. cmInstallCommandIncludesArgument includesArgs;
  432. std::vector<cmInstallCommandFileSetArguments> fileSetArgs(
  433. argVectors.FileSets.size(), { helper.DefaultComponentName });
  434. cmInstallCommandArguments cxxModuleBmiArgs(helper.DefaultComponentName);
  435. // now parse the args for specific parts of the target (e.g. LIBRARY,
  436. // RUNTIME, ARCHIVE etc.
  437. archiveArgs.Parse(argVectors.Archive, &unknownArgs);
  438. libraryArgs.Parse(argVectors.Library, &unknownArgs);
  439. runtimeArgs.Parse(argVectors.Runtime, &unknownArgs);
  440. objectArgs.Parse(argVectors.Object, &unknownArgs);
  441. frameworkArgs.Parse(argVectors.Framework, &unknownArgs);
  442. bundleArgs.Parse(argVectors.Bundle, &unknownArgs);
  443. privateHeaderArgs.Parse(argVectors.PrivateHeader, &unknownArgs);
  444. publicHeaderArgs.Parse(argVectors.PublicHeader, &unknownArgs);
  445. resourceArgs.Parse(argVectors.Resource, &unknownArgs);
  446. includesArgs.Parse(&argVectors.Includes, &unknownArgs);
  447. for (std::size_t i = 0; i < argVectors.FileSets.size(); i++) {
  448. // We have to create a separate object for the parsing because
  449. // cmArgumentParser<void>::Bind() binds to a specific address, but the
  450. // objects in the vector can move around. So we parse in an object with a
  451. // fixed address and then copy the data into the vector.
  452. cmInstallCommandFileSetArguments fileSetArg(helper.DefaultComponentName);
  453. fileSetArg.Parse(argVectors.FileSets[i], &unknownArgs);
  454. fileSetArgs[i] = std::move(fileSetArg);
  455. }
  456. bool const supportCxx20FileSetTypes = cmExperimental::HasSupportEnabled(
  457. *helper.Makefile, cmExperimental::Feature::CxxModuleCMakeApi);
  458. if (!supportCxx20FileSetTypes) {
  459. std::copy(argVectors.CxxModulesBmi.begin(), argVectors.CxxModulesBmi.end(),
  460. std::back_inserter(unknownArgs));
  461. } else {
  462. cxxModuleBmiArgs.Parse(argVectors.CxxModulesBmi, &unknownArgs);
  463. }
  464. if (!unknownArgs.empty()) {
  465. // Unknown argument.
  466. status.SetError(
  467. cmStrCat("TARGETS given unknown argument \"", unknownArgs[0], "\"."));
  468. return false;
  469. }
  470. // apply generic args
  471. archiveArgs.SetGenericArguments(&genericArgs);
  472. libraryArgs.SetGenericArguments(&genericArgs);
  473. runtimeArgs.SetGenericArguments(&genericArgs);
  474. objectArgs.SetGenericArguments(&genericArgs);
  475. frameworkArgs.SetGenericArguments(&genericArgs);
  476. bundleArgs.SetGenericArguments(&genericArgs);
  477. privateHeaderArgs.SetGenericArguments(&genericArgs);
  478. publicHeaderArgs.SetGenericArguments(&genericArgs);
  479. resourceArgs.SetGenericArguments(&genericArgs);
  480. for (auto& fileSetArg : fileSetArgs) {
  481. fileSetArg.SetGenericArguments(&genericArgs);
  482. }
  483. cxxModuleBmiArgs.SetGenericArguments(&genericArgs);
  484. success = success && archiveArgs.Finalize();
  485. success = success && libraryArgs.Finalize();
  486. success = success && runtimeArgs.Finalize();
  487. success = success && objectArgs.Finalize();
  488. success = success && frameworkArgs.Finalize();
  489. success = success && bundleArgs.Finalize();
  490. success = success && privateHeaderArgs.Finalize();
  491. success = success && publicHeaderArgs.Finalize();
  492. success = success && resourceArgs.Finalize();
  493. for (auto& fileSetArg : fileSetArgs) {
  494. success = success && fileSetArg.Finalize();
  495. }
  496. if (supportCxx20FileSetTypes) {
  497. success = success && cxxModuleBmiArgs.Finalize();
  498. }
  499. if (!success) {
  500. return false;
  501. }
  502. // Enforce argument rules too complex to specify for the
  503. // general-purpose parser.
  504. if (archiveArgs.GetNamelinkOnly() || runtimeArgs.GetNamelinkOnly() ||
  505. objectArgs.GetNamelinkOnly() || frameworkArgs.GetNamelinkOnly() ||
  506. bundleArgs.GetNamelinkOnly() || privateHeaderArgs.GetNamelinkOnly() ||
  507. publicHeaderArgs.GetNamelinkOnly() || resourceArgs.GetNamelinkOnly() ||
  508. std::any_of(fileSetArgs.begin(), fileSetArgs.end(),
  509. [](const cmInstallCommandFileSetArguments& fileSetArg)
  510. -> bool { return fileSetArg.GetNamelinkOnly(); }) ||
  511. cxxModuleBmiArgs.GetNamelinkOnly()) {
  512. status.SetError(
  513. "TARGETS given NAMELINK_ONLY option not in LIBRARY group. "
  514. "The NAMELINK_ONLY option may be specified only following LIBRARY.");
  515. return false;
  516. }
  517. if (archiveArgs.GetNamelinkSkip() || runtimeArgs.GetNamelinkSkip() ||
  518. objectArgs.GetNamelinkSkip() || frameworkArgs.GetNamelinkSkip() ||
  519. bundleArgs.GetNamelinkSkip() || privateHeaderArgs.GetNamelinkSkip() ||
  520. publicHeaderArgs.GetNamelinkSkip() || resourceArgs.GetNamelinkSkip() ||
  521. std::any_of(fileSetArgs.begin(), fileSetArgs.end(),
  522. [](const cmInstallCommandFileSetArguments& fileSetArg)
  523. -> bool { return fileSetArg.GetNamelinkSkip(); }) ||
  524. cxxModuleBmiArgs.GetNamelinkSkip()) {
  525. status.SetError(
  526. "TARGETS given NAMELINK_SKIP option not in LIBRARY group. "
  527. "The NAMELINK_SKIP option may be specified only following LIBRARY.");
  528. return false;
  529. }
  530. if (archiveArgs.HasNamelinkComponent() ||
  531. runtimeArgs.HasNamelinkComponent() ||
  532. objectArgs.HasNamelinkComponent() ||
  533. frameworkArgs.HasNamelinkComponent() ||
  534. bundleArgs.HasNamelinkComponent() ||
  535. privateHeaderArgs.HasNamelinkComponent() ||
  536. publicHeaderArgs.HasNamelinkComponent() ||
  537. resourceArgs.HasNamelinkComponent() ||
  538. std::any_of(fileSetArgs.begin(), fileSetArgs.end(),
  539. [](const cmInstallCommandFileSetArguments& fileSetArg)
  540. -> bool { return fileSetArg.HasNamelinkComponent(); }) ||
  541. cxxModuleBmiArgs.HasNamelinkComponent()) {
  542. status.SetError(
  543. "TARGETS given NAMELINK_COMPONENT option not in LIBRARY group. "
  544. "The NAMELINK_COMPONENT option may be specified only following "
  545. "LIBRARY.");
  546. return false;
  547. }
  548. if (libraryArgs.GetNamelinkOnly() && libraryArgs.GetNamelinkSkip()) {
  549. status.SetError("TARGETS given NAMELINK_ONLY and NAMELINK_SKIP. "
  550. "At most one of these two options may be specified.");
  551. return false;
  552. }
  553. if (!genericArgs.GetType().empty() || !archiveArgs.GetType().empty() ||
  554. !libraryArgs.GetType().empty() || !runtimeArgs.GetType().empty() ||
  555. !objectArgs.GetType().empty() || !frameworkArgs.GetType().empty() ||
  556. !bundleArgs.GetType().empty() || !privateHeaderArgs.GetType().empty() ||
  557. !publicHeaderArgs.GetType().empty() || !resourceArgs.GetType().empty() ||
  558. std::any_of(fileSetArgs.begin(), fileSetArgs.end(),
  559. [](const cmInstallCommandFileSetArguments& fileSetArg)
  560. -> bool { return !fileSetArg.GetType().empty(); }) ||
  561. !cxxModuleBmiArgs.GetType().empty()) {
  562. status.SetError(
  563. "TARGETS given TYPE option. The TYPE option may only be specified in "
  564. " install(FILES) and install(DIRECTORIES).");
  565. return false;
  566. }
  567. if (std::any_of(fileSetArgs.begin(), fileSetArgs.end(),
  568. [](const cmInstallCommandFileSetArguments& fileSetArg)
  569. -> bool { return fileSetArg.GetFileSet().empty(); })) {
  570. status.SetError("TARGETS given FILE_SET option without file set name.");
  571. return false;
  572. }
  573. cmInstallRuntimeDependencySet* runtimeDependencySet = nullptr;
  574. if (runtimeDependenciesArgVector) {
  575. if (!runtimeDependencySetArg.empty()) {
  576. status.SetError("TARGETS cannot have both RUNTIME_DEPENDENCIES and "
  577. "RUNTIME_DEPENDENCY_SET.");
  578. return false;
  579. }
  580. auto system = helper.Makefile->GetSafeDefinition("CMAKE_HOST_SYSTEM_NAME");
  581. if (!cmRuntimeDependencyArchive::PlatformSupportsRuntimeDependencies(
  582. system)) {
  583. status.SetError(
  584. cmStrCat("TARGETS RUNTIME_DEPENDENCIES is not supported on system \"",
  585. system, '"'));
  586. return false;
  587. }
  588. if (helper.Makefile->IsOn("CMAKE_CROSSCOMPILING")) {
  589. status.SetError("TARGETS RUNTIME_DEPENDENCIES is not supported "
  590. "when cross-compiling.");
  591. return false;
  592. }
  593. if (helper.Makefile->GetSafeDefinition("CMAKE_HOST_SYSTEM_NAME") ==
  594. "Darwin" &&
  595. frameworkArgs.GetDestination().empty()) {
  596. status.SetError(
  597. "TARGETS RUNTIME_DEPENDENCIES given no FRAMEWORK DESTINATION");
  598. return false;
  599. }
  600. runtimeDependencySet = helper.Makefile->GetGlobalGenerator()
  601. ->CreateAnonymousRuntimeDependencySet();
  602. } else if (!runtimeDependencySetArg.empty()) {
  603. auto system = helper.Makefile->GetSafeDefinition("CMAKE_HOST_SYSTEM_NAME");
  604. if (!cmRuntimeDependencyArchive::PlatformSupportsRuntimeDependencies(
  605. system)) {
  606. status.SetError(cmStrCat(
  607. "TARGETS RUNTIME_DEPENDENCY_SET is not supported on system \"", system,
  608. '"'));
  609. return false;
  610. }
  611. runtimeDependencySet =
  612. helper.Makefile->GetGlobalGenerator()->GetNamedRuntimeDependencySet(
  613. runtimeDependencySetArg);
  614. }
  615. // Select the mode for installing symlinks to versioned shared libraries.
  616. cmInstallTargetGenerator::NamelinkModeType namelinkMode =
  617. cmInstallTargetGenerator::NamelinkModeNone;
  618. if (libraryArgs.GetNamelinkOnly()) {
  619. namelinkMode = cmInstallTargetGenerator::NamelinkModeOnly;
  620. } else if (libraryArgs.GetNamelinkSkip()) {
  621. namelinkMode = cmInstallTargetGenerator::NamelinkModeSkip;
  622. }
  623. // Check if there is something to do.
  624. if (targetList.empty()) {
  625. return true;
  626. }
  627. for (std::string const& tgt : targetList) {
  628. if (helper.Makefile->IsAlias(tgt)) {
  629. status.SetError(
  630. cmStrCat("TARGETS given target \"", tgt, "\" which is an alias."));
  631. return false;
  632. }
  633. // Lookup this target in the current directory.
  634. cmTarget* target = helper.Makefile->FindLocalNonAliasTarget(tgt);
  635. if (!target) {
  636. // If no local target has been found, find it in the global scope.
  637. cmTarget* const global_target =
  638. helper.Makefile->GetGlobalGenerator()->FindTarget(tgt, true);
  639. if (global_target && !global_target->IsImported()) {
  640. target = global_target;
  641. }
  642. }
  643. if (target) {
  644. // Found the target. Check its type.
  645. if (target->GetType() != cmStateEnums::EXECUTABLE &&
  646. target->GetType() != cmStateEnums::STATIC_LIBRARY &&
  647. target->GetType() != cmStateEnums::SHARED_LIBRARY &&
  648. target->GetType() != cmStateEnums::MODULE_LIBRARY &&
  649. target->GetType() != cmStateEnums::OBJECT_LIBRARY &&
  650. target->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  651. status.SetError(
  652. cmStrCat("TARGETS given target \"", tgt,
  653. "\" which is not an executable, library, or module."));
  654. return false;
  655. }
  656. // Store the target in the list to be installed.
  657. targets.push_back(target);
  658. } else {
  659. // Did not find the target.
  660. status.SetError(
  661. cmStrCat("TARGETS given target \"", tgt, "\" which does not exist."));
  662. return false;
  663. }
  664. }
  665. // Keep track of whether we will be performing an installation of
  666. // any files of the given type.
  667. bool installsArchive = false;
  668. bool installsLibrary = false;
  669. bool installsNamelink = false;
  670. bool installsRuntime = false;
  671. bool installsObject = false;
  672. bool installsFramework = false;
  673. bool installsBundle = false;
  674. bool installsPrivateHeader = false;
  675. bool installsPublicHeader = false;
  676. bool installsResource = false;
  677. std::vector<bool> installsFileSet(fileSetArgs.size(), false);
  678. bool installsCxxModuleBmi = false;
  679. // Generate install script code to install the given targets.
  680. for (cmTarget* ti : targets) {
  681. // Handle each target type.
  682. cmTarget& target = *ti;
  683. std::unique_ptr<cmInstallTargetGenerator> archiveGenerator;
  684. std::unique_ptr<cmInstallTargetGenerator> libraryGenerator;
  685. std::unique_ptr<cmInstallTargetGenerator> namelinkGenerator;
  686. std::unique_ptr<cmInstallTargetGenerator> runtimeGenerator;
  687. std::unique_ptr<cmInstallTargetGenerator> objectGenerator;
  688. std::unique_ptr<cmInstallTargetGenerator> frameworkGenerator;
  689. std::unique_ptr<cmInstallTargetGenerator> bundleGenerator;
  690. std::unique_ptr<cmInstallFilesGenerator> privateHeaderGenerator;
  691. std::unique_ptr<cmInstallFilesGenerator> publicHeaderGenerator;
  692. std::unique_ptr<cmInstallFilesGenerator> resourceGenerator;
  693. std::vector<std::unique_ptr<cmInstallFileSetGenerator>> fileSetGenerators;
  694. std::unique_ptr<cmInstallCxxModuleBmiGenerator> cxxModuleBmiGenerator;
  695. // Avoid selecting default destinations for PUBLIC_HEADER and
  696. // PRIVATE_HEADER if any artifacts are specified.
  697. bool artifactsSpecified = false;
  698. // Track whether this is a namelink-only rule.
  699. bool namelinkOnly = false;
  700. auto addTargetExport = [&]() -> bool {
  701. // Add this install rule to an export if one was specified.
  702. if (!exports.empty()) {
  703. auto interfaceFileSets = target.GetAllInterfaceFileSets();
  704. if (std::any_of(
  705. interfaceFileSets.begin(), interfaceFileSets.end(),
  706. [=](const std::string& name) -> bool {
  707. return !std::any_of(
  708. fileSetArgs.begin(), fileSetArgs.end(),
  709. [=](const cmInstallCommandFileSetArguments& fileSetArg)
  710. -> bool { return fileSetArg.GetFileSet() == name; });
  711. })) {
  712. status.SetError(cmStrCat("TARGETS target ", target.GetName(),
  713. " is exported but not all of its interface "
  714. "file sets are installed"));
  715. return false;
  716. }
  717. auto te = cm::make_unique<cmTargetExport>();
  718. te->TargetName = target.GetName();
  719. te->ArchiveGenerator = archiveGenerator.get();
  720. te->BundleGenerator = bundleGenerator.get();
  721. te->FrameworkGenerator = frameworkGenerator.get();
  722. te->HeaderGenerator = publicHeaderGenerator.get();
  723. te->LibraryGenerator = libraryGenerator.get();
  724. te->RuntimeGenerator = runtimeGenerator.get();
  725. te->ObjectsGenerator = objectGenerator.get();
  726. for (auto const& gen : fileSetGenerators) {
  727. te->FileSetGenerators[gen->GetFileSet()] = gen.get();
  728. }
  729. te->CxxModuleBmiGenerator = cxxModuleBmiGenerator.get();
  730. target.AddInstallIncludeDirectories(
  731. *te, cmMakeRange(includesArgs.GetIncludeDirs()));
  732. te->NamelinkOnly = namelinkOnly;
  733. helper.Makefile->GetGlobalGenerator()
  734. ->GetExportSets()[exports]
  735. .AddTargetExport(std::move(te));
  736. }
  737. return true;
  738. };
  739. switch (target.GetType()) {
  740. case cmStateEnums::SHARED_LIBRARY: {
  741. // Shared libraries are handled differently on DLL and non-DLL
  742. // platforms. All windows platforms are DLL platforms including
  743. // cygwin. Currently no other platform is a DLL platform.
  744. if (target.IsDLLPlatform()) {
  745. // When in namelink only mode skip all libraries on Windows.
  746. if (namelinkMode == cmInstallTargetGenerator::NamelinkModeOnly) {
  747. namelinkOnly = true;
  748. if (!addTargetExport()) {
  749. return false;
  750. }
  751. continue;
  752. }
  753. // This is a DLL platform.
  754. if (!archiveArgs.GetDestination().empty()) {
  755. // The import library uses the ARCHIVE properties.
  756. archiveGenerator = CreateInstallTargetGenerator(
  757. target, archiveArgs, true, helper.Makefile->GetBacktrace());
  758. artifactsSpecified = true;
  759. }
  760. if (!runtimeArgs.GetDestination().empty()) {
  761. // The DLL uses the RUNTIME properties.
  762. runtimeGenerator = CreateInstallTargetGenerator(
  763. target, runtimeArgs, false, helper.Makefile->GetBacktrace());
  764. artifactsSpecified = true;
  765. }
  766. if (!archiveGenerator && !runtimeGenerator) {
  767. archiveGenerator = CreateInstallTargetGenerator(
  768. target, archiveArgs, true, helper.Makefile->GetBacktrace(),
  769. helper.GetArchiveDestination(nullptr));
  770. runtimeGenerator = CreateInstallTargetGenerator(
  771. target, runtimeArgs, false, helper.Makefile->GetBacktrace(),
  772. helper.GetRuntimeDestination(nullptr));
  773. }
  774. if (runtimeDependencySet && runtimeGenerator) {
  775. runtimeDependencySet->AddLibrary(runtimeGenerator.get());
  776. }
  777. } else {
  778. // This is a non-DLL platform.
  779. // If it is marked with FRAMEWORK property use the FRAMEWORK set of
  780. // INSTALL properties. Otherwise, use the LIBRARY properties.
  781. if (target.IsFrameworkOnApple()) {
  782. // When in namelink only mode skip frameworks.
  783. if (namelinkMode == cmInstallTargetGenerator::NamelinkModeOnly) {
  784. namelinkOnly = true;
  785. if (!addTargetExport()) {
  786. return false;
  787. }
  788. continue;
  789. }
  790. // Use the FRAMEWORK properties.
  791. if (!frameworkArgs.GetDestination().empty()) {
  792. frameworkGenerator = CreateInstallTargetGenerator(
  793. target, frameworkArgs, false, helper.Makefile->GetBacktrace());
  794. } else {
  795. status.SetError(
  796. cmStrCat("TARGETS given no FRAMEWORK DESTINATION for shared "
  797. "library FRAMEWORK target \"",
  798. target.GetName(), "\"."));
  799. return false;
  800. }
  801. } else {
  802. // The shared library uses the LIBRARY properties.
  803. if (!libraryArgs.GetDestination().empty()) {
  804. artifactsSpecified = true;
  805. }
  806. if (namelinkMode != cmInstallTargetGenerator::NamelinkModeOnly) {
  807. libraryGenerator = CreateInstallTargetGenerator(
  808. target, libraryArgs, false, helper.Makefile->GetBacktrace(),
  809. helper.GetLibraryDestination(&libraryArgs));
  810. libraryGenerator->SetNamelinkMode(
  811. cmInstallTargetGenerator::NamelinkModeSkip);
  812. }
  813. if (namelinkMode != cmInstallTargetGenerator::NamelinkModeSkip) {
  814. namelinkGenerator = CreateInstallTargetGenerator(
  815. target, libraryArgs, false, helper.Makefile->GetBacktrace(),
  816. helper.GetLibraryDestination(&libraryArgs), false, true);
  817. namelinkGenerator->SetNamelinkMode(
  818. cmInstallTargetGenerator::NamelinkModeOnly);
  819. }
  820. namelinkOnly =
  821. (namelinkMode == cmInstallTargetGenerator::NamelinkModeOnly);
  822. }
  823. if (runtimeDependencySet && libraryGenerator) {
  824. runtimeDependencySet->AddLibrary(libraryGenerator.get());
  825. }
  826. }
  827. } break;
  828. case cmStateEnums::STATIC_LIBRARY: {
  829. // If it is marked with FRAMEWORK property use the FRAMEWORK set of
  830. // INSTALL properties. Otherwise, use the LIBRARY properties.
  831. if (target.IsFrameworkOnApple()) {
  832. // When in namelink only mode skip frameworks.
  833. if (namelinkMode == cmInstallTargetGenerator::NamelinkModeOnly) {
  834. namelinkOnly = true;
  835. if (!addTargetExport()) {
  836. return false;
  837. }
  838. continue;
  839. }
  840. // Use the FRAMEWORK properties.
  841. if (!frameworkArgs.GetDestination().empty()) {
  842. frameworkGenerator = CreateInstallTargetGenerator(
  843. target, frameworkArgs, false, helper.Makefile->GetBacktrace());
  844. } else {
  845. status.SetError(
  846. cmStrCat("TARGETS given no FRAMEWORK DESTINATION for static "
  847. "library FRAMEWORK target \"",
  848. target.GetName(), "\"."));
  849. return false;
  850. }
  851. } else {
  852. // Static libraries use ARCHIVE properties.
  853. if (!archiveArgs.GetDestination().empty()) {
  854. artifactsSpecified = true;
  855. }
  856. archiveGenerator = CreateInstallTargetGenerator(
  857. target, archiveArgs, false, helper.Makefile->GetBacktrace(),
  858. helper.GetArchiveDestination(&archiveArgs));
  859. }
  860. } break;
  861. case cmStateEnums::MODULE_LIBRARY: {
  862. // Modules use LIBRARY properties.
  863. if (!libraryArgs.GetDestination().empty()) {
  864. libraryGenerator = CreateInstallTargetGenerator(
  865. target, libraryArgs, false, helper.Makefile->GetBacktrace());
  866. libraryGenerator->SetNamelinkMode(namelinkMode);
  867. namelinkOnly =
  868. (namelinkMode == cmInstallTargetGenerator::NamelinkModeOnly);
  869. if (runtimeDependencySet) {
  870. runtimeDependencySet->AddModule(libraryGenerator.get());
  871. }
  872. } else {
  873. status.SetError(
  874. cmStrCat("TARGETS given no LIBRARY DESTINATION for module "
  875. "target \"",
  876. target.GetName(), "\"."));
  877. return false;
  878. }
  879. } break;
  880. case cmStateEnums::OBJECT_LIBRARY: {
  881. // Objects use OBJECT properties.
  882. if (!objectArgs.GetDestination().empty()) {
  883. // Verify that we know where the objects are to install them.
  884. std::string reason;
  885. if (!target.HasKnownObjectFileLocation(&reason)) {
  886. status.SetError(
  887. cmStrCat("TARGETS given OBJECT library \"", target.GetName(),
  888. "\" whose objects may not be installed", reason, "."));
  889. return false;
  890. }
  891. objectGenerator = CreateInstallTargetGenerator(
  892. target, objectArgs, false, helper.Makefile->GetBacktrace());
  893. } else {
  894. // Installing an OBJECT library without a destination transforms
  895. // it to an INTERFACE library. It installs no files but can be
  896. // exported.
  897. }
  898. } break;
  899. case cmStateEnums::EXECUTABLE: {
  900. if (target.IsAppBundleOnApple()) {
  901. // Application bundles use the BUNDLE properties.
  902. if (!bundleArgs.GetDestination().empty()) {
  903. bundleGenerator = CreateInstallTargetGenerator(
  904. target, bundleArgs, false, helper.Makefile->GetBacktrace());
  905. } else if (!runtimeArgs.GetDestination().empty()) {
  906. bool failure = false;
  907. if (helper.CheckCMP0006(failure)) {
  908. // For CMake 2.4 compatibility fallback to the RUNTIME
  909. // properties.
  910. bundleGenerator = CreateInstallTargetGenerator(
  911. target, runtimeArgs, false, helper.Makefile->GetBacktrace());
  912. } else if (failure) {
  913. return false;
  914. }
  915. }
  916. if (!bundleGenerator) {
  917. status.SetError(cmStrCat("TARGETS given no BUNDLE DESTINATION for "
  918. "MACOSX_BUNDLE executable target \"",
  919. target.GetName(), "\"."));
  920. return false;
  921. }
  922. if (runtimeDependencySet) {
  923. if (!AddBundleExecutable(helper, runtimeDependencySet,
  924. bundleGenerator.get())) {
  925. return false;
  926. }
  927. }
  928. } else {
  929. // Executables use the RUNTIME properties.
  930. if (!runtimeArgs.GetDestination().empty()) {
  931. artifactsSpecified = true;
  932. }
  933. runtimeGenerator = CreateInstallTargetGenerator(
  934. target, runtimeArgs, false, helper.Makefile->GetBacktrace(),
  935. helper.GetRuntimeDestination(&runtimeArgs));
  936. if (runtimeDependencySet) {
  937. runtimeDependencySet->AddExecutable(runtimeGenerator.get());
  938. }
  939. }
  940. // On DLL platforms an executable may also have an import
  941. // library. Install it to the archive destination if it
  942. // exists.
  943. if ((target.IsDLLPlatform() || target.IsAIX()) &&
  944. !archiveArgs.GetDestination().empty() &&
  945. target.IsExecutableWithExports()) {
  946. // The import library uses the ARCHIVE properties.
  947. artifactsSpecified = true;
  948. archiveGenerator = CreateInstallTargetGenerator(
  949. target, archiveArgs, true, helper.Makefile->GetBacktrace(), true);
  950. }
  951. } break;
  952. case cmStateEnums::INTERFACE_LIBRARY:
  953. // Nothing to do. An INTERFACE_LIBRARY can be installed, but the
  954. // only effect of that is to make it exportable. It installs no
  955. // other files itself.
  956. default:
  957. // This should never happen due to the above type check.
  958. // Ignore the case.
  959. break;
  960. }
  961. // These well-known sets of files are installed *automatically* for
  962. // FRAMEWORK SHARED library targets on the Mac as part of installing the
  963. // FRAMEWORK. For other target types or on other platforms, they are not
  964. // installed automatically and so we need to create install files
  965. // generators for them.
  966. bool createInstallGeneratorsForTargetFileSets = true;
  967. if (target.IsFrameworkOnApple()) {
  968. createInstallGeneratorsForTargetFileSets = false;
  969. }
  970. if (createInstallGeneratorsForTargetFileSets && !namelinkOnly) {
  971. cmValue files = target.GetProperty("PRIVATE_HEADER");
  972. if (cmNonempty(files)) {
  973. std::vector<std::string> relFiles = cmExpandedList(*files);
  974. std::vector<std::string> absFiles;
  975. if (!helper.MakeFilesFullPath("PRIVATE_HEADER", relFiles, absFiles)) {
  976. return false;
  977. }
  978. // Create the files install generator.
  979. if (!artifactsSpecified ||
  980. !privateHeaderArgs.GetDestination().empty()) {
  981. privateHeaderGenerator = CreateInstallFilesGenerator(
  982. helper.Makefile, absFiles, privateHeaderArgs, false,
  983. helper.GetIncludeDestination(&privateHeaderArgs));
  984. } else {
  985. std::ostringstream e;
  986. e << "Target " << target.GetName() << " has "
  987. << "PRIVATE_HEADER files but no PRIVATE_HEADER DESTINATION.";
  988. helper.Makefile->IssueMessage(MessageType::AUTHOR_WARNING, e.str());
  989. }
  990. }
  991. files = target.GetProperty("PUBLIC_HEADER");
  992. if (cmNonempty(files)) {
  993. std::vector<std::string> relFiles = cmExpandedList(*files);
  994. std::vector<std::string> absFiles;
  995. if (!helper.MakeFilesFullPath("PUBLIC_HEADER", relFiles, absFiles)) {
  996. return false;
  997. }
  998. // Create the files install generator.
  999. if (!artifactsSpecified ||
  1000. !publicHeaderArgs.GetDestination().empty()) {
  1001. publicHeaderGenerator = CreateInstallFilesGenerator(
  1002. helper.Makefile, absFiles, publicHeaderArgs, false,
  1003. helper.GetIncludeDestination(&publicHeaderArgs));
  1004. } else {
  1005. std::ostringstream e;
  1006. e << "Target " << target.GetName() << " has "
  1007. << "PUBLIC_HEADER files but no PUBLIC_HEADER DESTINATION.";
  1008. helper.Makefile->IssueMessage(MessageType::AUTHOR_WARNING, e.str());
  1009. }
  1010. }
  1011. files = target.GetProperty("RESOURCE");
  1012. if (cmNonempty(files)) {
  1013. std::vector<std::string> relFiles = cmExpandedList(*files);
  1014. std::vector<std::string> absFiles;
  1015. if (!helper.MakeFilesFullPath("RESOURCE", relFiles, absFiles)) {
  1016. return false;
  1017. }
  1018. // Create the files install generator.
  1019. if (!resourceArgs.GetDestination().empty()) {
  1020. resourceGenerator = CreateInstallFilesGenerator(
  1021. helper.Makefile, absFiles, resourceArgs, false);
  1022. } else if (!target.IsAppBundleOnApple()) {
  1023. helper.Makefile->IssueMessage(
  1024. MessageType::AUTHOR_WARNING,
  1025. cmStrCat("Target ", target.GetName(),
  1026. " has RESOURCE files but no RESOURCE DESTINATION."));
  1027. }
  1028. }
  1029. }
  1030. if (!namelinkOnly) {
  1031. for (std::size_t i = 0; i < fileSetArgs.size(); i++) {
  1032. if (auto* fileSet = target.GetFileSet(fileSetArgs[i].GetFileSet())) {
  1033. auto interfaceFileSetEntries = cmExpandedList(target.GetSafeProperty(
  1034. cmTarget::GetInterfaceFileSetsPropertyName(fileSet->GetType())));
  1035. if (std::find(interfaceFileSetEntries.begin(),
  1036. interfaceFileSetEntries.end(),
  1037. fileSetArgs[i].GetFileSet()) !=
  1038. interfaceFileSetEntries.end()) {
  1039. std::string destination;
  1040. if (fileSet->GetType() == "HEADERS"_s) {
  1041. destination = helper.GetIncludeDestination(&fileSetArgs[i]);
  1042. } else {
  1043. destination = fileSetArgs[i].GetDestination();
  1044. if (destination.empty()) {
  1045. status.SetError(cmStrCat(
  1046. "TARGETS given no FILE_SET DESTINATION for target \"",
  1047. target.GetName(), "\" file set \"", fileSet->GetName(),
  1048. "\"."));
  1049. return false;
  1050. }
  1051. }
  1052. fileSetGenerators.push_back(CreateInstallFileSetGenerator(
  1053. helper, target, fileSet, destination, fileSetArgs[i]));
  1054. installsFileSet[i] = true;
  1055. }
  1056. }
  1057. }
  1058. }
  1059. if (supportCxx20FileSetTypes &&
  1060. !cxxModuleBmiArgs.GetDestination().empty()) {
  1061. cxxModuleBmiGenerator = cm::make_unique<cmInstallCxxModuleBmiGenerator>(
  1062. target.GetName(),
  1063. helper.GetCxxModulesBmiDestination(&cxxModuleBmiArgs),
  1064. cxxModuleBmiArgs.GetPermissions(),
  1065. cxxModuleBmiArgs.GetConfigurations(), cxxModuleBmiArgs.GetComponent(),
  1066. cmInstallGenerator::SelectMessageLevel(target.GetMakefile()),
  1067. cxxModuleBmiArgs.GetExcludeFromAll(), cxxModuleBmiArgs.GetOptional(),
  1068. helper.Makefile->GetBacktrace());
  1069. target.SetHaveInstallRule(true);
  1070. }
  1071. // Add this install rule to an export if one was specified.
  1072. if (!addTargetExport()) {
  1073. return false;
  1074. }
  1075. // Keep track of whether we're installing anything in each category
  1076. installsArchive = installsArchive || archiveGenerator;
  1077. installsLibrary = installsLibrary || libraryGenerator;
  1078. installsNamelink = installsNamelink || namelinkGenerator;
  1079. installsRuntime = installsRuntime || runtimeGenerator;
  1080. installsObject = installsObject || objectGenerator;
  1081. installsFramework = installsFramework || frameworkGenerator;
  1082. installsBundle = installsBundle || bundleGenerator;
  1083. installsPrivateHeader = installsPrivateHeader || privateHeaderGenerator;
  1084. installsPublicHeader = installsPublicHeader || publicHeaderGenerator;
  1085. installsResource = installsResource || resourceGenerator;
  1086. installsCxxModuleBmi = installsCxxModuleBmi || cxxModuleBmiGenerator;
  1087. helper.Makefile->AddInstallGenerator(std::move(archiveGenerator));
  1088. helper.Makefile->AddInstallGenerator(std::move(libraryGenerator));
  1089. helper.Makefile->AddInstallGenerator(std::move(namelinkGenerator));
  1090. helper.Makefile->AddInstallGenerator(std::move(runtimeGenerator));
  1091. helper.Makefile->AddInstallGenerator(std::move(objectGenerator));
  1092. helper.Makefile->AddInstallGenerator(std::move(frameworkGenerator));
  1093. helper.Makefile->AddInstallGenerator(std::move(bundleGenerator));
  1094. helper.Makefile->AddInstallGenerator(std::move(privateHeaderGenerator));
  1095. helper.Makefile->AddInstallGenerator(std::move(publicHeaderGenerator));
  1096. helper.Makefile->AddInstallGenerator(std::move(resourceGenerator));
  1097. for (auto& gen : fileSetGenerators) {
  1098. helper.Makefile->AddInstallGenerator(std::move(gen));
  1099. }
  1100. helper.Makefile->AddInstallGenerator(std::move(cxxModuleBmiGenerator));
  1101. }
  1102. if (runtimeDependenciesArgVector && !runtimeDependencySet->Empty()) {
  1103. AddInstallRuntimeDependenciesGenerator(
  1104. helper, runtimeDependencySet, runtimeArgs, libraryArgs, frameworkArgs,
  1105. std::move(runtimeDependenciesArgs), installsRuntime, installsLibrary,
  1106. installsFramework);
  1107. }
  1108. // Tell the global generator about any installation component names
  1109. // specified
  1110. if (installsArchive) {
  1111. helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
  1112. archiveArgs.GetComponent());
  1113. }
  1114. if (installsLibrary) {
  1115. helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
  1116. libraryArgs.GetComponent());
  1117. }
  1118. if (installsNamelink) {
  1119. helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
  1120. libraryArgs.GetNamelinkComponent());
  1121. }
  1122. if (installsRuntime) {
  1123. helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
  1124. runtimeArgs.GetComponent());
  1125. }
  1126. if (installsObject) {
  1127. helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
  1128. objectArgs.GetComponent());
  1129. }
  1130. if (installsFramework) {
  1131. helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
  1132. frameworkArgs.GetComponent());
  1133. }
  1134. if (installsBundle) {
  1135. helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
  1136. bundleArgs.GetComponent());
  1137. }
  1138. if (installsPrivateHeader) {
  1139. helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
  1140. privateHeaderArgs.GetComponent());
  1141. }
  1142. if (installsPublicHeader) {
  1143. helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
  1144. publicHeaderArgs.GetComponent());
  1145. }
  1146. if (installsResource) {
  1147. helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
  1148. resourceArgs.GetComponent());
  1149. }
  1150. for (std::size_t i = 0; i < fileSetArgs.size(); i++) {
  1151. if (installsFileSet[i]) {
  1152. helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
  1153. fileSetArgs[i].GetComponent());
  1154. }
  1155. }
  1156. if (installsCxxModuleBmi) {
  1157. helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
  1158. cxxModuleBmiArgs.GetComponent());
  1159. }
  1160. return true;
  1161. }
  1162. bool HandleImportedRuntimeArtifactsMode(std::vector<std::string> const& args,
  1163. cmExecutionStatus& status)
  1164. {
  1165. Helper helper(status);
  1166. // This is the IMPORTED_RUNTIME_ARTIFACTS mode.
  1167. std::vector<cmTarget*> targets;
  1168. struct ArgVectors
  1169. {
  1170. std::vector<std::string> Library;
  1171. std::vector<std::string> Runtime;
  1172. std::vector<std::string> Framework;
  1173. std::vector<std::string> Bundle;
  1174. };
  1175. static auto const argHelper = cmArgumentParser<ArgVectors>{}
  1176. .Bind("LIBRARY"_s, &ArgVectors::Library)
  1177. .Bind("RUNTIME"_s, &ArgVectors::Runtime)
  1178. .Bind("FRAMEWORK"_s, &ArgVectors::Framework)
  1179. .Bind("BUNDLE"_s, &ArgVectors::Bundle);
  1180. std::vector<std::string> genericArgVector;
  1181. ArgVectors const argVectors = argHelper.Parse(args, &genericArgVector);
  1182. // now parse the generic args (i.e. the ones not specialized on LIBRARY,
  1183. // RUNTIME etc. (see above)
  1184. std::vector<std::string> targetList;
  1185. std::string runtimeDependencySetArg;
  1186. std::vector<std::string> unknownArgs;
  1187. cmInstallCommandArguments genericArgs(helper.DefaultComponentName);
  1188. genericArgs.Bind("IMPORTED_RUNTIME_ARTIFACTS"_s, targetList)
  1189. .Bind("RUNTIME_DEPENDENCY_SET"_s, runtimeDependencySetArg);
  1190. genericArgs.Parse(genericArgVector, &unknownArgs);
  1191. bool success = genericArgs.Finalize();
  1192. cmInstallCommandArguments libraryArgs(helper.DefaultComponentName);
  1193. cmInstallCommandArguments runtimeArgs(helper.DefaultComponentName);
  1194. cmInstallCommandArguments frameworkArgs(helper.DefaultComponentName);
  1195. cmInstallCommandArguments bundleArgs(helper.DefaultComponentName);
  1196. // now parse the args for specific parts of the target (e.g. LIBRARY,
  1197. // RUNTIME etc.
  1198. libraryArgs.Parse(argVectors.Library, &unknownArgs);
  1199. runtimeArgs.Parse(argVectors.Runtime, &unknownArgs);
  1200. frameworkArgs.Parse(argVectors.Framework, &unknownArgs);
  1201. bundleArgs.Parse(argVectors.Bundle, &unknownArgs);
  1202. if (!unknownArgs.empty()) {
  1203. // Unknown argument.
  1204. status.SetError(
  1205. cmStrCat("IMPORTED_RUNTIME_ARTIFACTS given unknown argument \"",
  1206. unknownArgs[0], "\"."));
  1207. return false;
  1208. }
  1209. // apply generic args
  1210. libraryArgs.SetGenericArguments(&genericArgs);
  1211. runtimeArgs.SetGenericArguments(&genericArgs);
  1212. frameworkArgs.SetGenericArguments(&genericArgs);
  1213. bundleArgs.SetGenericArguments(&genericArgs);
  1214. success = success && libraryArgs.Finalize();
  1215. success = success && runtimeArgs.Finalize();
  1216. success = success && frameworkArgs.Finalize();
  1217. success = success && bundleArgs.Finalize();
  1218. if (!success) {
  1219. return false;
  1220. }
  1221. cmInstallRuntimeDependencySet* runtimeDependencySet = nullptr;
  1222. if (!runtimeDependencySetArg.empty()) {
  1223. auto system = helper.Makefile->GetSafeDefinition("CMAKE_HOST_SYSTEM_NAME");
  1224. if (!cmRuntimeDependencyArchive::PlatformSupportsRuntimeDependencies(
  1225. system)) {
  1226. status.SetError(
  1227. cmStrCat("IMPORTED_RUNTIME_ARTIFACTS RUNTIME_DEPENDENCY_SET is not "
  1228. "supported on system \"",
  1229. system, '"'));
  1230. return false;
  1231. }
  1232. runtimeDependencySet =
  1233. helper.Makefile->GetGlobalGenerator()->GetNamedRuntimeDependencySet(
  1234. runtimeDependencySetArg);
  1235. }
  1236. // Check if there is something to do.
  1237. if (targetList.empty()) {
  1238. return true;
  1239. }
  1240. for (std::string const& tgt : targetList) {
  1241. if (helper.Makefile->IsAlias(tgt)) {
  1242. status.SetError(cmStrCat("IMPORTED_RUNTIME_ARTIFACTS given target \"",
  1243. tgt, "\" which is an alias."));
  1244. return false;
  1245. }
  1246. // Lookup this target in the current directory.
  1247. cmTarget* target = helper.Makefile->FindTargetToUse(tgt);
  1248. if (!target || !target->IsImported()) {
  1249. // If no local target has been found, find it in the global scope.
  1250. cmTarget* const global_target =
  1251. helper.Makefile->GetGlobalGenerator()->FindTarget(tgt, true);
  1252. if (global_target && global_target->IsImported()) {
  1253. target = global_target;
  1254. }
  1255. }
  1256. if (target) {
  1257. // Found the target. Check its type.
  1258. if (target->GetType() != cmStateEnums::EXECUTABLE &&
  1259. target->GetType() != cmStateEnums::SHARED_LIBRARY &&
  1260. target->GetType() != cmStateEnums::MODULE_LIBRARY) {
  1261. status.SetError(
  1262. cmStrCat("IMPORTED_RUNTIME_ARTIFACTS given target \"", tgt,
  1263. "\" which is not an executable, library, or module."));
  1264. return false;
  1265. }
  1266. // Store the target in the list to be installed.
  1267. targets.push_back(target);
  1268. } else {
  1269. // Did not find the target.
  1270. status.SetError(cmStrCat("IMPORTED_RUNTIME_ARTIFACTS given target \"",
  1271. tgt, "\" which does not exist."));
  1272. return false;
  1273. }
  1274. }
  1275. // Keep track of whether we will be performing an installation of
  1276. // any files of the given type.
  1277. bool installsLibrary = false;
  1278. bool installsRuntime = false;
  1279. bool installsFramework = false;
  1280. bool installsBundle = false;
  1281. auto const createInstallGenerator =
  1282. [helper](cmTarget& target, const cmInstallCommandArguments& typeArgs,
  1283. const std::string& destination)
  1284. -> std::unique_ptr<cmInstallImportedRuntimeArtifactsGenerator> {
  1285. return cm::make_unique<cmInstallImportedRuntimeArtifactsGenerator>(
  1286. target.GetName(), destination, typeArgs.GetPermissions(),
  1287. typeArgs.GetConfigurations(), typeArgs.GetComponent(),
  1288. cmInstallGenerator::SelectMessageLevel(helper.Makefile),
  1289. typeArgs.GetExcludeFromAll(), typeArgs.GetOptional(),
  1290. helper.Makefile->GetBacktrace());
  1291. };
  1292. // Generate install script code to install the given targets.
  1293. for (cmTarget* ti : targets) {
  1294. // Handle each target type.
  1295. cmTarget& target = *ti;
  1296. std::unique_ptr<cmInstallImportedRuntimeArtifactsGenerator>
  1297. libraryGenerator;
  1298. std::unique_ptr<cmInstallImportedRuntimeArtifactsGenerator>
  1299. runtimeGenerator;
  1300. std::unique_ptr<cmInstallImportedRuntimeArtifactsGenerator>
  1301. frameworkGenerator;
  1302. std::unique_ptr<cmInstallImportedRuntimeArtifactsGenerator>
  1303. bundleGenerator;
  1304. switch (target.GetType()) {
  1305. case cmStateEnums::SHARED_LIBRARY:
  1306. if (target.IsDLLPlatform()) {
  1307. runtimeGenerator = createInstallGenerator(
  1308. target, runtimeArgs, helper.GetRuntimeDestination(&runtimeArgs));
  1309. if (runtimeDependencySet) {
  1310. runtimeDependencySet->AddLibrary(runtimeGenerator.get());
  1311. }
  1312. } else if (target.IsFrameworkOnApple()) {
  1313. if (frameworkArgs.GetDestination().empty()) {
  1314. status.SetError(cmStrCat("IMPORTED_RUNTIME_ARTIFACTS given no "
  1315. "FRAMEWORK DESTINATION for shared "
  1316. "library FRAMEWORK target \"",
  1317. target.GetName(), "\"."));
  1318. return false;
  1319. }
  1320. frameworkGenerator = createInstallGenerator(
  1321. target, frameworkArgs, frameworkArgs.GetDestination());
  1322. if (runtimeDependencySet) {
  1323. runtimeDependencySet->AddLibrary(frameworkGenerator.get());
  1324. }
  1325. } else {
  1326. libraryGenerator = createInstallGenerator(
  1327. target, libraryArgs, helper.GetLibraryDestination(&libraryArgs));
  1328. if (runtimeDependencySet) {
  1329. runtimeDependencySet->AddLibrary(libraryGenerator.get());
  1330. }
  1331. }
  1332. break;
  1333. case cmStateEnums::MODULE_LIBRARY:
  1334. libraryGenerator = createInstallGenerator(
  1335. target, libraryArgs, helper.GetLibraryDestination(&libraryArgs));
  1336. if (runtimeDependencySet) {
  1337. runtimeDependencySet->AddModule(libraryGenerator.get());
  1338. }
  1339. break;
  1340. case cmStateEnums::EXECUTABLE:
  1341. if (target.IsAppBundleOnApple()) {
  1342. if (bundleArgs.GetDestination().empty()) {
  1343. status.SetError(
  1344. cmStrCat("IMPORTED_RUNTIME_ARTIFACTS given no BUNDLE "
  1345. "DESTINATION for MACOSX_BUNDLE executable target \"",
  1346. target.GetName(), "\"."));
  1347. return false;
  1348. }
  1349. bundleGenerator = createInstallGenerator(
  1350. target, bundleArgs, bundleArgs.GetDestination());
  1351. if (runtimeDependencySet) {
  1352. if (!AddBundleExecutable(helper, runtimeDependencySet,
  1353. bundleGenerator.get())) {
  1354. return false;
  1355. }
  1356. }
  1357. } else {
  1358. runtimeGenerator = createInstallGenerator(
  1359. target, runtimeArgs, helper.GetRuntimeDestination(&runtimeArgs));
  1360. if (runtimeDependencySet) {
  1361. runtimeDependencySet->AddExecutable(runtimeGenerator.get());
  1362. }
  1363. }
  1364. break;
  1365. default:
  1366. assert(false && "This should never happen");
  1367. break;
  1368. }
  1369. // Keep track of whether we're installing anything in each category
  1370. installsLibrary = installsLibrary || libraryGenerator;
  1371. installsRuntime = installsRuntime || runtimeGenerator;
  1372. installsFramework = installsFramework || frameworkGenerator;
  1373. installsBundle = installsBundle || bundleGenerator;
  1374. helper.Makefile->AddInstallGenerator(std::move(libraryGenerator));
  1375. helper.Makefile->AddInstallGenerator(std::move(runtimeGenerator));
  1376. helper.Makefile->AddInstallGenerator(std::move(frameworkGenerator));
  1377. helper.Makefile->AddInstallGenerator(std::move(bundleGenerator));
  1378. }
  1379. // Tell the global generator about any installation component names
  1380. // specified
  1381. if (installsLibrary) {
  1382. helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
  1383. libraryArgs.GetComponent());
  1384. }
  1385. if (installsRuntime) {
  1386. helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
  1387. runtimeArgs.GetComponent());
  1388. }
  1389. if (installsFramework) {
  1390. helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
  1391. frameworkArgs.GetComponent());
  1392. }
  1393. if (installsBundle) {
  1394. helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
  1395. bundleArgs.GetComponent());
  1396. }
  1397. return true;
  1398. }
  1399. bool HandleFilesMode(std::vector<std::string> const& args,
  1400. cmExecutionStatus& status)
  1401. {
  1402. Helper helper(status);
  1403. // This is the FILES mode.
  1404. bool programs = (args[0] == "PROGRAMS");
  1405. cmInstallCommandArguments ica(helper.DefaultComponentName);
  1406. std::vector<std::string> files;
  1407. ica.Bind(programs ? "PROGRAMS"_s : "FILES"_s, files);
  1408. std::vector<std::string> unknownArgs;
  1409. ica.Parse(args, &unknownArgs);
  1410. if (!unknownArgs.empty()) {
  1411. // Unknown argument.
  1412. status.SetError(
  1413. cmStrCat(args[0], " given unknown argument \"", unknownArgs[0], "\"."));
  1414. return false;
  1415. }
  1416. std::string type = ica.GetType();
  1417. if (!type.empty() && allowedTypes.count(type) == 0) {
  1418. status.SetError(
  1419. cmStrCat(args[0], " given non-type \"", type, "\" with TYPE argument."));
  1420. return false;
  1421. }
  1422. const std::vector<std::string>& filesVector = files;
  1423. // Check if there is something to do.
  1424. if (filesVector.empty()) {
  1425. return true;
  1426. }
  1427. if (!ica.GetRename().empty() && filesVector.size() > 1) {
  1428. // The rename option works only with one file.
  1429. status.SetError(
  1430. cmStrCat(args[0], " given RENAME option with more than one file."));
  1431. return false;
  1432. }
  1433. std::vector<std::string> absFiles;
  1434. if (!helper.MakeFilesFullPath(args[0].c_str(), filesVector, absFiles)) {
  1435. return false;
  1436. }
  1437. cmPolicies::PolicyStatus policyStatus =
  1438. helper.Makefile->GetPolicyStatus(cmPolicies::CMP0062);
  1439. cmGlobalGenerator* gg = helper.Makefile->GetGlobalGenerator();
  1440. for (std::string const& file : filesVector) {
  1441. if (gg->IsExportedTargetsFile(file)) {
  1442. const char* modal = nullptr;
  1443. std::ostringstream e;
  1444. MessageType messageType = MessageType::AUTHOR_WARNING;
  1445. switch (policyStatus) {
  1446. case cmPolicies::WARN:
  1447. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0062) << "\n";
  1448. modal = "should";
  1449. CM_FALLTHROUGH;
  1450. case cmPolicies::OLD:
  1451. break;
  1452. case cmPolicies::REQUIRED_IF_USED:
  1453. case cmPolicies::REQUIRED_ALWAYS:
  1454. case cmPolicies::NEW:
  1455. modal = "may";
  1456. messageType = MessageType::FATAL_ERROR;
  1457. break;
  1458. }
  1459. if (modal) {
  1460. e << "The file\n " << file
  1461. << "\nwas generated by the export() "
  1462. "command. It "
  1463. << modal
  1464. << " not be installed with the "
  1465. "install() command. Use the install(EXPORT) mechanism "
  1466. "instead. See the cmake-packages(7) manual for more.\n";
  1467. helper.Makefile->IssueMessage(messageType, e.str());
  1468. if (messageType == MessageType::FATAL_ERROR) {
  1469. return false;
  1470. }
  1471. }
  1472. }
  1473. }
  1474. if (!ica.Finalize()) {
  1475. return false;
  1476. }
  1477. if (!type.empty() && !ica.GetDestination().empty()) {
  1478. status.SetError(cmStrCat(args[0],
  1479. " given both TYPE and DESTINATION arguments. "
  1480. "You may only specify one."));
  1481. return false;
  1482. }
  1483. std::string destination = helper.GetDestinationForType(&ica, type);
  1484. if (destination.empty()) {
  1485. // A destination is required.
  1486. status.SetError(cmStrCat(args[0], " given no DESTINATION!"));
  1487. return false;
  1488. }
  1489. // Create the files install generator.
  1490. helper.Makefile->AddInstallGenerator(CreateInstallFilesGenerator(
  1491. helper.Makefile, absFiles, ica, programs, destination));
  1492. // Tell the global generator about any installation component names
  1493. // specified.
  1494. helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
  1495. ica.GetComponent());
  1496. return true;
  1497. }
  1498. bool HandleDirectoryMode(std::vector<std::string> const& args,
  1499. cmExecutionStatus& status)
  1500. {
  1501. Helper helper(status);
  1502. enum Doing
  1503. {
  1504. DoingNone,
  1505. DoingDirs,
  1506. DoingDestination,
  1507. DoingPattern,
  1508. DoingRegex,
  1509. DoingPermsFile,
  1510. DoingPermsDir,
  1511. DoingPermsMatch,
  1512. DoingConfigurations,
  1513. DoingComponent,
  1514. DoingType
  1515. };
  1516. Doing doing = DoingDirs;
  1517. bool in_match_mode = false;
  1518. bool optional = false;
  1519. bool exclude_from_all = false;
  1520. bool message_never = false;
  1521. std::vector<std::string> dirs;
  1522. const std::string* destination = nullptr;
  1523. std::string permissions_file;
  1524. std::string permissions_dir;
  1525. std::vector<std::string> configurations;
  1526. std::string component = helper.DefaultComponentName;
  1527. std::string literal_args;
  1528. std::string type;
  1529. for (unsigned int i = 1; i < args.size(); ++i) {
  1530. if (args[i] == "DESTINATION") {
  1531. if (in_match_mode) {
  1532. status.SetError(cmStrCat(args[0], " does not allow \"", args[i],
  1533. "\" after PATTERN or REGEX."));
  1534. return false;
  1535. }
  1536. // Switch to setting the destination property.
  1537. doing = DoingDestination;
  1538. } else if (args[i] == "TYPE") {
  1539. if (in_match_mode) {
  1540. status.SetError(cmStrCat(args[0], " does not allow \"", args[i],
  1541. "\" after PATTERN or REGEX."));
  1542. return false;
  1543. }
  1544. // Switch to setting the type.
  1545. doing = DoingType;
  1546. } else if (args[i] == "OPTIONAL") {
  1547. if (in_match_mode) {
  1548. status.SetError(cmStrCat(args[0], " does not allow \"", args[i],
  1549. "\" after PATTERN or REGEX."));
  1550. return false;
  1551. }
  1552. // Mark the rule as optional.
  1553. optional = true;
  1554. doing = DoingNone;
  1555. } else if (args[i] == "MESSAGE_NEVER") {
  1556. if (in_match_mode) {
  1557. status.SetError(cmStrCat(args[0], " does not allow \"", args[i],
  1558. "\" after PATTERN or REGEX."));
  1559. return false;
  1560. }
  1561. // Mark the rule as quiet.
  1562. message_never = true;
  1563. doing = DoingNone;
  1564. } else if (args[i] == "PATTERN") {
  1565. // Switch to a new pattern match rule.
  1566. doing = DoingPattern;
  1567. in_match_mode = true;
  1568. } else if (args[i] == "REGEX") {
  1569. // Switch to a new regex match rule.
  1570. doing = DoingRegex;
  1571. in_match_mode = true;
  1572. } else if (args[i] == "EXCLUDE") {
  1573. // Add this property to the current match rule.
  1574. if (!in_match_mode || doing == DoingPattern || doing == DoingRegex) {
  1575. status.SetError(cmStrCat(args[0], " does not allow \"", args[i],
  1576. "\" before a PATTERN or REGEX is given."));
  1577. return false;
  1578. }
  1579. literal_args += " EXCLUDE";
  1580. doing = DoingNone;
  1581. } else if (args[i] == "PERMISSIONS") {
  1582. if (!in_match_mode) {
  1583. status.SetError(cmStrCat(args[0], " does not allow \"", args[i],
  1584. "\" before a PATTERN or REGEX is given."));
  1585. return false;
  1586. }
  1587. // Switch to setting the current match permissions property.
  1588. literal_args += " PERMISSIONS";
  1589. doing = DoingPermsMatch;
  1590. } else if (args[i] == "FILE_PERMISSIONS") {
  1591. if (in_match_mode) {
  1592. status.SetError(cmStrCat(args[0], " does not allow \"", args[i],
  1593. "\" after PATTERN or REGEX."));
  1594. return false;
  1595. }
  1596. // Switch to setting the file permissions property.
  1597. doing = DoingPermsFile;
  1598. } else if (args[i] == "DIRECTORY_PERMISSIONS") {
  1599. if (in_match_mode) {
  1600. status.SetError(cmStrCat(args[0], " does not allow \"", args[i],
  1601. "\" after PATTERN or REGEX."));
  1602. return false;
  1603. }
  1604. // Switch to setting the directory permissions property.
  1605. doing = DoingPermsDir;
  1606. } else if (args[i] == "USE_SOURCE_PERMISSIONS") {
  1607. if (in_match_mode) {
  1608. status.SetError(cmStrCat(args[0], " does not allow \"", args[i],
  1609. "\" after PATTERN or REGEX."));
  1610. return false;
  1611. }
  1612. // Add this option literally.
  1613. literal_args += " USE_SOURCE_PERMISSIONS";
  1614. doing = DoingNone;
  1615. } else if (args[i] == "FILES_MATCHING") {
  1616. if (in_match_mode) {
  1617. status.SetError(cmStrCat(args[0], " does not allow \"", args[i],
  1618. "\" after PATTERN or REGEX."));
  1619. return false;
  1620. }
  1621. // Add this option literally.
  1622. literal_args += " FILES_MATCHING";
  1623. doing = DoingNone;
  1624. } else if (args[i] == "CONFIGURATIONS") {
  1625. if (in_match_mode) {
  1626. status.SetError(cmStrCat(args[0], " does not allow \"", args[i],
  1627. "\" after PATTERN or REGEX."));
  1628. return false;
  1629. }
  1630. // Switch to setting the configurations property.
  1631. doing = DoingConfigurations;
  1632. } else if (args[i] == "COMPONENT") {
  1633. if (in_match_mode) {
  1634. status.SetError(cmStrCat(args[0], " does not allow \"", args[i],
  1635. "\" after PATTERN or REGEX."));
  1636. return false;
  1637. }
  1638. // Switch to setting the component property.
  1639. doing = DoingComponent;
  1640. } else if (args[i] == "EXCLUDE_FROM_ALL") {
  1641. if (in_match_mode) {
  1642. status.SetError(cmStrCat(args[0], " does not allow \"", args[i],
  1643. "\" after PATTERN or REGEX."));
  1644. return false;
  1645. }
  1646. exclude_from_all = true;
  1647. doing = DoingNone;
  1648. } else if (doing == DoingDirs) {
  1649. // Convert this directory to a full path.
  1650. std::string dir = args[i];
  1651. std::string::size_type gpos = cmGeneratorExpression::Find(dir);
  1652. if (gpos != 0 && !cmSystemTools::FileIsFullPath(dir)) {
  1653. dir =
  1654. cmStrCat(helper.Makefile->GetCurrentSourceDirectory(), '/', args[i]);
  1655. }
  1656. // Make sure the name is a directory.
  1657. if (cmSystemTools::FileExists(dir) &&
  1658. !cmSystemTools::FileIsDirectory(dir)) {
  1659. status.SetError(cmStrCat(args[0], " given non-directory \"", args[i],
  1660. "\" to install."));
  1661. return false;
  1662. }
  1663. // Store the directory for installation.
  1664. dirs.push_back(std::move(dir));
  1665. } else if (doing == DoingConfigurations) {
  1666. configurations.push_back(args[i]);
  1667. } else if (doing == DoingDestination) {
  1668. destination = &args[i];
  1669. doing = DoingNone;
  1670. } else if (doing == DoingType) {
  1671. if (allowedTypes.count(args[i]) == 0) {
  1672. status.SetError(cmStrCat(args[0], " given non-type \"", args[i],
  1673. "\" with TYPE argument."));
  1674. return false;
  1675. }
  1676. type = args[i];
  1677. doing = DoingNone;
  1678. } else if (doing == DoingPattern) {
  1679. // Convert the pattern to a regular expression. Require a
  1680. // leading slash and trailing end-of-string in the matched
  1681. // string to make sure the pattern matches only whole file
  1682. // names.
  1683. literal_args += " REGEX \"/";
  1684. std::string regex = cmsys::Glob::PatternToRegex(args[i], false);
  1685. cmSystemTools::ReplaceString(regex, "\\", "\\\\");
  1686. literal_args += regex;
  1687. literal_args += "$\"";
  1688. doing = DoingNone;
  1689. } else if (doing == DoingRegex) {
  1690. literal_args += " REGEX \"";
  1691. // Match rules are case-insensitive on some platforms.
  1692. #if defined(_WIN32) || defined(__APPLE__)
  1693. std::string regex = cmSystemTools::LowerCase(args[i]);
  1694. #else
  1695. std::string regex = args[i];
  1696. #endif
  1697. cmSystemTools::ReplaceString(regex, "\\", "\\\\");
  1698. literal_args += regex;
  1699. literal_args += "\"";
  1700. doing = DoingNone;
  1701. } else if (doing == DoingComponent) {
  1702. component = args[i];
  1703. doing = DoingNone;
  1704. } else if (doing == DoingPermsFile) {
  1705. // Check the requested permission.
  1706. if (!cmInstallCommandArguments::CheckPermissions(args[i],
  1707. permissions_file)) {
  1708. status.SetError(cmStrCat(args[0], " given invalid file permission \"",
  1709. args[i], "\"."));
  1710. return false;
  1711. }
  1712. } else if (doing == DoingPermsDir) {
  1713. // Check the requested permission.
  1714. if (!cmInstallCommandArguments::CheckPermissions(args[i],
  1715. permissions_dir)) {
  1716. status.SetError(cmStrCat(
  1717. args[0], " given invalid directory permission \"", args[i], "\"."));
  1718. return false;
  1719. }
  1720. } else if (doing == DoingPermsMatch) {
  1721. // Check the requested permission.
  1722. if (!cmInstallCommandArguments::CheckPermissions(args[i],
  1723. literal_args)) {
  1724. status.SetError(
  1725. cmStrCat(args[0], " given invalid permission \"", args[i], "\"."));
  1726. return false;
  1727. }
  1728. } else {
  1729. // Unknown argument.
  1730. status.SetError(
  1731. cmStrCat(args[0], " given unknown argument \"", args[i], "\"."));
  1732. return false;
  1733. }
  1734. }
  1735. // Support installing an empty directory.
  1736. if (dirs.empty() && destination) {
  1737. dirs.emplace_back();
  1738. }
  1739. // Check if there is something to do.
  1740. if (dirs.empty()) {
  1741. return true;
  1742. }
  1743. std::string destinationStr;
  1744. if (!destination) {
  1745. if (type.empty()) {
  1746. // A destination is required.
  1747. status.SetError(cmStrCat(args[0], " given no DESTINATION!"));
  1748. return false;
  1749. }
  1750. destinationStr = helper.GetDestinationForType(nullptr, type);
  1751. destination = &destinationStr;
  1752. } else if (!type.empty()) {
  1753. status.SetError(cmStrCat(args[0],
  1754. " given both TYPE and DESTINATION "
  1755. "arguments. You may only specify one."));
  1756. return false;
  1757. }
  1758. cmInstallGenerator::MessageLevel message =
  1759. cmInstallGenerator::SelectMessageLevel(helper.Makefile, message_never);
  1760. // Create the directory install generator.
  1761. helper.Makefile->AddInstallGenerator(
  1762. cm::make_unique<cmInstallDirectoryGenerator>(
  1763. dirs, *destination, permissions_file, permissions_dir, configurations,
  1764. component, message, exclude_from_all, literal_args, optional,
  1765. helper.Makefile->GetBacktrace()));
  1766. // Tell the global generator about any installation component names
  1767. // specified.
  1768. helper.Makefile->GetGlobalGenerator()->AddInstallComponent(component);
  1769. return true;
  1770. }
  1771. bool HandleExportAndroidMKMode(std::vector<std::string> const& args,
  1772. cmExecutionStatus& status)
  1773. {
  1774. #ifndef CMAKE_BOOTSTRAP
  1775. Helper helper(status);
  1776. // This is the EXPORT mode.
  1777. cmInstallCommandArguments ica(helper.DefaultComponentName);
  1778. std::string exp;
  1779. std::string name_space;
  1780. bool exportOld = false;
  1781. std::string filename;
  1782. ica.Bind("EXPORT_ANDROID_MK"_s, exp);
  1783. ica.Bind("NAMESPACE"_s, name_space);
  1784. ica.Bind("EXPORT_LINK_INTERFACE_LIBRARIES"_s, exportOld);
  1785. ica.Bind("FILE"_s, filename);
  1786. std::vector<std::string> unknownArgs;
  1787. ica.Parse(args, &unknownArgs);
  1788. if (!unknownArgs.empty()) {
  1789. // Unknown argument.
  1790. status.SetError(
  1791. cmStrCat(args[0], " given unknown argument \"", unknownArgs[0], "\"."));
  1792. return false;
  1793. }
  1794. if (!ica.Finalize()) {
  1795. return false;
  1796. }
  1797. // Make sure there is a destination.
  1798. if (ica.GetDestination().empty()) {
  1799. // A destination is required.
  1800. status.SetError(cmStrCat(args[0], " given no DESTINATION!"));
  1801. return false;
  1802. }
  1803. // Check the file name.
  1804. std::string fname = filename;
  1805. if (fname.find_first_of(":/\\") != std::string::npos) {
  1806. status.SetError(cmStrCat(args[0], " given invalid export file name \"",
  1807. fname,
  1808. "\". The FILE argument may not contain a path. "
  1809. "Specify the path in the DESTINATION argument."));
  1810. return false;
  1811. }
  1812. // Check the file extension.
  1813. if (!fname.empty() &&
  1814. cmSystemTools::GetFilenameLastExtension(fname) != ".mk") {
  1815. status.SetError(cmStrCat(
  1816. args[0], " given invalid export file name \"", fname,
  1817. R"(". The FILE argument must specify a name ending in ".mk".)"));
  1818. return false;
  1819. }
  1820. if (fname.find_first_of(":/\\") != std::string::npos) {
  1821. status.SetError(
  1822. cmStrCat(args[0], " given export name \"", exp,
  1823. "\". "
  1824. "This name cannot be safely converted to a file name. "
  1825. "Specify a different export name or use the FILE option to set "
  1826. "a file name explicitly."));
  1827. return false;
  1828. }
  1829. // Use the default name
  1830. if (fname.empty()) {
  1831. fname = "Android.mk";
  1832. }
  1833. cmExportSet& exportSet =
  1834. helper.Makefile->GetGlobalGenerator()->GetExportSets()[exp];
  1835. cmInstallGenerator::MessageLevel message =
  1836. cmInstallGenerator::SelectMessageLevel(helper.Makefile);
  1837. // Create the export install generator.
  1838. helper.Makefile->AddInstallGenerator(
  1839. cm::make_unique<cmInstallExportGenerator>(
  1840. &exportSet, ica.GetDestination(), ica.GetPermissions(),
  1841. ica.GetConfigurations(), ica.GetComponent(), message,
  1842. ica.GetExcludeFromAll(), fname, name_space, "", exportOld, true,
  1843. helper.Makefile->GetBacktrace()));
  1844. return true;
  1845. #else
  1846. static_cast<void>(args);
  1847. status.SetError("EXPORT_ANDROID_MK not supported in bootstrap cmake");
  1848. return false;
  1849. #endif
  1850. }
  1851. bool HandleExportMode(std::vector<std::string> const& args,
  1852. cmExecutionStatus& status)
  1853. {
  1854. Helper helper(status);
  1855. // This is the EXPORT mode.
  1856. cmInstallCommandArguments ica(helper.DefaultComponentName);
  1857. std::string exp;
  1858. std::string name_space;
  1859. bool exportOld = false;
  1860. std::string filename;
  1861. std::string cxx_modules_directory;
  1862. ica.Bind("EXPORT"_s, exp);
  1863. ica.Bind("NAMESPACE"_s, name_space);
  1864. ica.Bind("EXPORT_LINK_INTERFACE_LIBRARIES"_s, exportOld);
  1865. ica.Bind("FILE"_s, filename);
  1866. bool const supportCxx20FileSetTypes = cmExperimental::HasSupportEnabled(
  1867. *helper.Makefile, cmExperimental::Feature::CxxModuleCMakeApi);
  1868. if (supportCxx20FileSetTypes) {
  1869. ica.Bind("CXX_MODULES_DIRECTORY"_s, cxx_modules_directory);
  1870. }
  1871. std::vector<std::string> unknownArgs;
  1872. ica.Parse(args, &unknownArgs);
  1873. if (!unknownArgs.empty()) {
  1874. // Unknown argument.
  1875. status.SetError(
  1876. cmStrCat(args[0], " given unknown argument \"", unknownArgs[0], "\"."));
  1877. return false;
  1878. }
  1879. if (!ica.Finalize()) {
  1880. return false;
  1881. }
  1882. // Make sure there is a destination.
  1883. if (ica.GetDestination().empty()) {
  1884. // A destination is required.
  1885. status.SetError(cmStrCat(args[0], " given no DESTINATION!"));
  1886. return false;
  1887. }
  1888. // Check the file name.
  1889. std::string fname = filename;
  1890. if (fname.find_first_of(":/\\") != std::string::npos) {
  1891. status.SetError(cmStrCat(args[0], " given invalid export file name \"",
  1892. fname,
  1893. "\". "
  1894. "The FILE argument may not contain a path. "
  1895. "Specify the path in the DESTINATION argument."));
  1896. return false;
  1897. }
  1898. // Check the file extension.
  1899. if (!fname.empty() &&
  1900. cmSystemTools::GetFilenameLastExtension(fname) != ".cmake") {
  1901. status.SetError(
  1902. cmStrCat(args[0], " given invalid export file name \"", fname,
  1903. "\". "
  1904. "The FILE argument must specify a name ending in \".cmake\"."));
  1905. return false;
  1906. }
  1907. // Construct the file name.
  1908. if (fname.empty()) {
  1909. fname = cmStrCat(exp, ".cmake");
  1910. if (fname.find_first_of(":/\\") != std::string::npos) {
  1911. status.SetError(cmStrCat(
  1912. args[0], " given export name \"", exp,
  1913. "\". "
  1914. "This name cannot be safely converted to a file name. "
  1915. "Specify a different export name or use the FILE option to set "
  1916. "a file name explicitly."));
  1917. return false;
  1918. }
  1919. }
  1920. cmExportSet& exportSet =
  1921. helper.Makefile->GetGlobalGenerator()->GetExportSets()[exp];
  1922. if (exportOld) {
  1923. for (auto const& te : exportSet.GetTargetExports()) {
  1924. cmTarget* tgt =
  1925. helper.Makefile->GetGlobalGenerator()->FindTarget(te->TargetName);
  1926. const bool newCMP0022Behavior =
  1927. (tgt && tgt->GetPolicyStatusCMP0022() != cmPolicies::WARN &&
  1928. tgt->GetPolicyStatusCMP0022() != cmPolicies::OLD);
  1929. if (!newCMP0022Behavior) {
  1930. status.SetError(cmStrCat(
  1931. "INSTALL(EXPORT) given keyword \""
  1932. "EXPORT_LINK_INTERFACE_LIBRARIES\", but target \"",
  1933. te->TargetName, "\" does not have policy CMP0022 set to NEW."));
  1934. return false;
  1935. }
  1936. }
  1937. }
  1938. cmInstallGenerator::MessageLevel message =
  1939. cmInstallGenerator::SelectMessageLevel(helper.Makefile);
  1940. // Create the export install generator.
  1941. helper.Makefile->AddInstallGenerator(
  1942. cm::make_unique<cmInstallExportGenerator>(
  1943. &exportSet, ica.GetDestination(), ica.GetPermissions(),
  1944. ica.GetConfigurations(), ica.GetComponent(), message,
  1945. ica.GetExcludeFromAll(), fname, name_space, cxx_modules_directory,
  1946. exportOld, false, helper.Makefile->GetBacktrace()));
  1947. return true;
  1948. }
  1949. bool HandleRuntimeDependencySetMode(std::vector<std::string> const& args,
  1950. cmExecutionStatus& status)
  1951. {
  1952. Helper helper(status);
  1953. auto system = helper.Makefile->GetSafeDefinition("CMAKE_HOST_SYSTEM_NAME");
  1954. if (!cmRuntimeDependencyArchive::PlatformSupportsRuntimeDependencies(
  1955. system)) {
  1956. status.SetError(cmStrCat(
  1957. "RUNTIME_DEPENDENCY_SET is not supported on system \"", system, '"'));
  1958. return false;
  1959. }
  1960. // This is the RUNTIME_DEPENDENCY_SET mode.
  1961. cmInstallRuntimeDependencySet* runtimeDependencySet;
  1962. struct ArgVectors
  1963. {
  1964. std::vector<std::string> Library;
  1965. std::vector<std::string> Runtime;
  1966. std::vector<std::string> Framework;
  1967. };
  1968. static auto const argHelper = cmArgumentParser<ArgVectors>{}
  1969. .Bind("LIBRARY"_s, &ArgVectors::Library)
  1970. .Bind("RUNTIME"_s, &ArgVectors::Runtime)
  1971. .Bind("FRAMEWORK"_s, &ArgVectors::Framework);
  1972. std::vector<std::string> genericArgVector;
  1973. ArgVectors const argVectors = argHelper.Parse(args, &genericArgVector);
  1974. // now parse the generic args (i.e. the ones not specialized on LIBRARY,
  1975. // RUNTIME, FRAMEWORK etc. (see above)
  1976. // These generic args also contain the runtime dependency set
  1977. std::string runtimeDependencySetArg;
  1978. std::vector<std::string> runtimeDependencyArgVector;
  1979. std::vector<cm::string_view> parsedArgs;
  1980. cmInstallCommandArguments genericArgs(helper.DefaultComponentName);
  1981. genericArgs.Bind("RUNTIME_DEPENDENCY_SET"_s, runtimeDependencySetArg);
  1982. genericArgs.Parse(genericArgVector, &runtimeDependencyArgVector, nullptr,
  1983. &parsedArgs);
  1984. bool success = genericArgs.Finalize();
  1985. cmInstallCommandArguments libraryArgs(helper.DefaultComponentName);
  1986. cmInstallCommandArguments runtimeArgs(helper.DefaultComponentName);
  1987. cmInstallCommandArguments frameworkArgs(helper.DefaultComponentName);
  1988. // Now also parse the file(GET_RUNTIME_DEPENDENCY) args
  1989. std::vector<std::string> unknownArgs;
  1990. auto runtimeDependencyArgs = RuntimeDependenciesArgHelper.Parse(
  1991. runtimeDependencyArgVector, &unknownArgs);
  1992. // now parse the args for specific parts of the target (e.g. LIBRARY,
  1993. // RUNTIME, FRAMEWORK etc.
  1994. libraryArgs.Parse(argVectors.Library, &unknownArgs);
  1995. runtimeArgs.Parse(argVectors.Runtime, &unknownArgs);
  1996. frameworkArgs.Parse(argVectors.Framework, &unknownArgs);
  1997. libraryArgs.SetGenericArguments(&genericArgs);
  1998. runtimeArgs.SetGenericArguments(&genericArgs);
  1999. frameworkArgs.SetGenericArguments(&genericArgs);
  2000. success = success && libraryArgs.Finalize();
  2001. success = success && runtimeArgs.Finalize();
  2002. success = success && frameworkArgs.Finalize();
  2003. if (!success) {
  2004. return false;
  2005. }
  2006. if (!unknownArgs.empty()) {
  2007. helper.SetError(
  2008. cmStrCat("RUNTIME_DEPENDENCY_SET given unknown argument \"",
  2009. unknownArgs.front(), "\"."));
  2010. return false;
  2011. }
  2012. if (runtimeDependencySetArg.empty()) {
  2013. helper.SetError(
  2014. "RUNTIME_DEPENDENCY_SET not given a runtime dependency set.");
  2015. return false;
  2016. }
  2017. runtimeDependencySet =
  2018. helper.Makefile->GetGlobalGenerator()->GetNamedRuntimeDependencySet(
  2019. runtimeDependencySetArg);
  2020. bool installsRuntime = false;
  2021. bool installsLibrary = false;
  2022. bool installsFramework = false;
  2023. AddInstallRuntimeDependenciesGenerator(
  2024. helper, runtimeDependencySet, runtimeArgs, libraryArgs, frameworkArgs,
  2025. std::move(runtimeDependencyArgs), installsRuntime, installsLibrary,
  2026. installsFramework);
  2027. // Tell the global generator about any installation component names
  2028. // specified
  2029. if (installsLibrary) {
  2030. helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
  2031. libraryArgs.GetComponent());
  2032. }
  2033. if (installsRuntime) {
  2034. helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
  2035. runtimeArgs.GetComponent());
  2036. }
  2037. if (installsFramework) {
  2038. helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
  2039. frameworkArgs.GetComponent());
  2040. }
  2041. return true;
  2042. }
  2043. bool Helper::MakeFilesFullPath(const char* modeName,
  2044. const std::vector<std::string>& relFiles,
  2045. std::vector<std::string>& absFiles)
  2046. {
  2047. return this->MakeFilesFullPath(
  2048. modeName, this->Makefile->GetCurrentSourceDirectory(), relFiles, absFiles);
  2049. }
  2050. bool Helper::MakeFilesFullPath(const char* modeName,
  2051. const std::string& basePath,
  2052. const std::vector<std::string>& relFiles,
  2053. std::vector<std::string>& absFiles)
  2054. {
  2055. for (std::string const& relFile : relFiles) {
  2056. std::string file = relFile;
  2057. std::string::size_type gpos = cmGeneratorExpression::Find(file);
  2058. if (gpos != 0 && !cmSystemTools::FileIsFullPath(file)) {
  2059. file = cmStrCat(basePath, '/', relFile);
  2060. }
  2061. // Make sure the file is not a directory.
  2062. if (gpos == std::string::npos && !cmSystemTools::FileIsSymlink(file) &&
  2063. cmSystemTools::FileIsDirectory(file)) {
  2064. this->SetError(
  2065. cmStrCat(modeName, " given directory \"", relFile, "\" to install."));
  2066. return false;
  2067. }
  2068. // Store the file for installation.
  2069. absFiles.push_back(std::move(file));
  2070. }
  2071. return true;
  2072. }
  2073. bool Helper::CheckCMP0006(bool& failure) const
  2074. {
  2075. switch (this->Makefile->GetPolicyStatus(cmPolicies::CMP0006)) {
  2076. case cmPolicies::WARN:
  2077. this->Makefile->IssueMessage(
  2078. MessageType::AUTHOR_WARNING,
  2079. cmPolicies::GetPolicyWarning(cmPolicies::CMP0006));
  2080. CM_FALLTHROUGH;
  2081. case cmPolicies::OLD:
  2082. // OLD behavior is to allow compatibility
  2083. return true;
  2084. case cmPolicies::NEW:
  2085. // NEW behavior is to disallow compatibility
  2086. break;
  2087. case cmPolicies::REQUIRED_IF_USED:
  2088. case cmPolicies::REQUIRED_ALWAYS:
  2089. failure = true;
  2090. this->Makefile->IssueMessage(
  2091. MessageType::FATAL_ERROR,
  2092. cmPolicies::GetRequiredPolicyError(cmPolicies::CMP0006));
  2093. break;
  2094. }
  2095. return false;
  2096. }
  2097. std::string Helper::GetDestination(const cmInstallCommandArguments* args,
  2098. const std::string& varName,
  2099. const std::string& guess) const
  2100. {
  2101. if (args && !args->GetDestination().empty()) {
  2102. return args->GetDestination();
  2103. }
  2104. std::string val = this->Makefile->GetSafeDefinition(varName);
  2105. if (!val.empty()) {
  2106. return val;
  2107. }
  2108. return guess;
  2109. }
  2110. std::string Helper::GetRuntimeDestination(
  2111. const cmInstallCommandArguments* args) const
  2112. {
  2113. return this->GetDestination(args, "CMAKE_INSTALL_BINDIR", "bin");
  2114. }
  2115. std::string Helper::GetSbinDestination(
  2116. const cmInstallCommandArguments* args) const
  2117. {
  2118. return this->GetDestination(args, "CMAKE_INSTALL_SBINDIR", "sbin");
  2119. }
  2120. std::string Helper::GetArchiveDestination(
  2121. const cmInstallCommandArguments* args) const
  2122. {
  2123. return this->GetDestination(args, "CMAKE_INSTALL_LIBDIR", "lib");
  2124. }
  2125. std::string Helper::GetLibraryDestination(
  2126. const cmInstallCommandArguments* args) const
  2127. {
  2128. return this->GetDestination(args, "CMAKE_INSTALL_LIBDIR", "lib");
  2129. }
  2130. std::string Helper::GetCxxModulesBmiDestination(
  2131. const cmInstallCommandArguments* args) const
  2132. {
  2133. if (args) {
  2134. return args->GetDestination();
  2135. }
  2136. return {};
  2137. }
  2138. std::string Helper::GetIncludeDestination(
  2139. const cmInstallCommandArguments* args) const
  2140. {
  2141. return this->GetDestination(args, "CMAKE_INSTALL_INCLUDEDIR", "include");
  2142. }
  2143. std::string Helper::GetSysconfDestination(
  2144. const cmInstallCommandArguments* args) const
  2145. {
  2146. return this->GetDestination(args, "CMAKE_INSTALL_SYSCONFDIR", "etc");
  2147. }
  2148. std::string Helper::GetSharedStateDestination(
  2149. const cmInstallCommandArguments* args) const
  2150. {
  2151. return this->GetDestination(args, "CMAKE_INSTALL_SHAREDSTATEDIR", "com");
  2152. }
  2153. std::string Helper::GetLocalStateDestination(
  2154. const cmInstallCommandArguments* args) const
  2155. {
  2156. return this->GetDestination(args, "CMAKE_INSTALL_LOCALSTATEDIR", "var");
  2157. }
  2158. std::string Helper::GetRunStateDestination(
  2159. const cmInstallCommandArguments* args) const
  2160. {
  2161. return this->GetDestination(args, "CMAKE_INSTALL_RUNSTATEDIR",
  2162. this->GetLocalStateDestination(nullptr) +
  2163. "/run");
  2164. }
  2165. std::string Helper::GetDataRootDestination(
  2166. const cmInstallCommandArguments* args) const
  2167. {
  2168. return this->GetDestination(args, "CMAKE_INSTALL_DATAROOTDIR", "share");
  2169. }
  2170. std::string Helper::GetDataDestination(
  2171. const cmInstallCommandArguments* args) const
  2172. {
  2173. return this->GetDestination(args, "CMAKE_INSTALL_DATADIR",
  2174. this->GetDataRootDestination(nullptr));
  2175. }
  2176. std::string Helper::GetInfoDestination(
  2177. const cmInstallCommandArguments* args) const
  2178. {
  2179. return this->GetDestination(args, "CMAKE_INSTALL_INFODIR",
  2180. this->GetDataRootDestination(nullptr) + "/info");
  2181. }
  2182. std::string Helper::GetLocaleDestination(
  2183. const cmInstallCommandArguments* args) const
  2184. {
  2185. return this->GetDestination(args, "CMAKE_INSTALL_LOCALEDIR",
  2186. this->GetDataRootDestination(nullptr) +
  2187. "/locale");
  2188. }
  2189. std::string Helper::GetManDestination(
  2190. const cmInstallCommandArguments* args) const
  2191. {
  2192. return this->GetDestination(args, "CMAKE_INSTALL_MANDIR",
  2193. this->GetDataRootDestination(nullptr) + "/man");
  2194. }
  2195. std::string Helper::GetDocDestination(
  2196. const cmInstallCommandArguments* args) const
  2197. {
  2198. return this->GetDestination(args, "CMAKE_INSTALL_DOCDIR",
  2199. this->GetDataRootDestination(nullptr) + "/doc");
  2200. }
  2201. std::string Helper::GetDestinationForType(
  2202. const cmInstallCommandArguments* args, const std::string& type) const
  2203. {
  2204. if (args && !args->GetDestination().empty()) {
  2205. return args->GetDestination();
  2206. }
  2207. if (type == "BIN") {
  2208. return this->GetRuntimeDestination(nullptr);
  2209. }
  2210. if (type == "SBIN") {
  2211. return this->GetSbinDestination(nullptr);
  2212. }
  2213. if (type == "SYSCONF") {
  2214. return this->GetSysconfDestination(nullptr);
  2215. }
  2216. if (type == "SHAREDSTATE") {
  2217. return this->GetSharedStateDestination(nullptr);
  2218. }
  2219. if (type == "LOCALSTATE") {
  2220. return this->GetLocalStateDestination(nullptr);
  2221. }
  2222. if (type == "RUNSTATE") {
  2223. return this->GetRunStateDestination(nullptr);
  2224. }
  2225. if (type == "LIB") {
  2226. return this->GetLibraryDestination(nullptr);
  2227. }
  2228. if (type == "INCLUDE") {
  2229. return this->GetIncludeDestination(nullptr);
  2230. }
  2231. if (type == "DATA") {
  2232. return this->GetDataDestination(nullptr);
  2233. }
  2234. if (type == "INFO") {
  2235. return this->GetInfoDestination(nullptr);
  2236. }
  2237. if (type == "LOCALE") {
  2238. return this->GetLocaleDestination(nullptr);
  2239. }
  2240. if (type == "MAN") {
  2241. return this->GetManDestination(nullptr);
  2242. }
  2243. if (type == "DOC") {
  2244. return this->GetDocDestination(nullptr);
  2245. }
  2246. return "";
  2247. }
  2248. } // namespace
  2249. bool cmInstallCommand(std::vector<std::string> const& args,
  2250. cmExecutionStatus& status)
  2251. {
  2252. // Allow calling with no arguments so that arguments may be built up
  2253. // using a variable that may be left empty.
  2254. if (args.empty()) {
  2255. return true;
  2256. }
  2257. // Enable the install target.
  2258. status.GetMakefile().GetGlobalGenerator()->EnableInstallTarget();
  2259. static cmSubcommandTable const subcommand{
  2260. { "SCRIPT"_s, HandleScriptMode },
  2261. { "CODE"_s, HandleScriptMode },
  2262. { "TARGETS"_s, HandleTargetsMode },
  2263. { "IMPORTED_RUNTIME_ARTIFACTS"_s, HandleImportedRuntimeArtifactsMode },
  2264. { "FILES"_s, HandleFilesMode },
  2265. { "PROGRAMS"_s, HandleFilesMode },
  2266. { "DIRECTORY"_s, HandleDirectoryMode },
  2267. { "EXPORT"_s, HandleExportMode },
  2268. { "EXPORT_ANDROID_MK"_s, HandleExportAndroidMKMode },
  2269. { "RUNTIME_DEPENDENCY_SET"_s, HandleRuntimeDependencySetMode },
  2270. };
  2271. return subcommand(args[0], args, status);
  2272. }