1
0

cmInstallCommand.cxx 105 KB

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