cmInstallCommand.cxx 105 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837
  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. // If the given directory is not a full path, convert it to one by
  1663. // assuming it's relative to the current source directory.
  1664. std::string dir = args[i];
  1665. std::string::size_type gpos = cmGeneratorExpression::Find(dir);
  1666. if (!dir.empty() && gpos != 0 && !cmSystemTools::FileIsFullPath(dir)) {
  1667. dir =
  1668. cmStrCat(helper.Makefile->GetCurrentSourceDirectory(), '/', args[i]);
  1669. }
  1670. // Make sure the name is a directory.
  1671. if (cmSystemTools::FileExists(dir, true)) {
  1672. status.SetError(cmStrCat(args[0], " given non-directory \"", args[i],
  1673. "\" to install."));
  1674. return false;
  1675. }
  1676. // Store the directory for installation.
  1677. dirs.push_back(std::move(dir));
  1678. } else if (doing == DoingConfigurations) {
  1679. configurations.push_back(args[i]);
  1680. } else if (doing == DoingDestination) {
  1681. // A trailing slash is meaningful for this form, but normalization
  1682. // preserves it if present
  1683. switch (status.GetMakefile().GetPolicyStatus(cmPolicies::CMP0177)) {
  1684. case cmPolicies::NEW:
  1685. destination = cmCMakePath(args[i]).Normal().String();
  1686. break;
  1687. case cmPolicies::WARN:
  1688. // We can't be certain if a warning is appropriate if there are any
  1689. // generator expressions
  1690. if (cmGeneratorExpression::Find(args[i]) == cm::string_view::npos &&
  1691. args[i] != cmCMakePath(args[i]).Normal().String()) {
  1692. status.GetMakefile().IssueMessage(
  1693. MessageType::AUTHOR_WARNING,
  1694. cmPolicies::GetPolicyWarning(cmPolicies::CMP0177));
  1695. }
  1696. CM_FALLTHROUGH;
  1697. case cmPolicies::OLD:
  1698. destination = args[i];
  1699. break;
  1700. }
  1701. doing = DoingNone;
  1702. } else if (doing == DoingType) {
  1703. if (allowedTypes.count(args[i]) == 0) {
  1704. status.SetError(cmStrCat(args[0], " given non-type \"", args[i],
  1705. "\" with TYPE argument."));
  1706. return false;
  1707. }
  1708. type = args[i];
  1709. doing = DoingNone;
  1710. } else if (doing == DoingPattern) {
  1711. // Convert the pattern to a regular expression. Require a
  1712. // leading slash and trailing end-of-string in the matched
  1713. // string to make sure the pattern matches only whole file
  1714. // names.
  1715. literal_args += " REGEX \"/";
  1716. std::string regex = cmsys::Glob::PatternToRegex(args[i], false);
  1717. cmSystemTools::ReplaceString(regex, "\\", "\\\\");
  1718. literal_args += regex;
  1719. literal_args += "$\"";
  1720. doing = DoingNone;
  1721. } else if (doing == DoingRegex) {
  1722. literal_args += " REGEX \"";
  1723. // Match rules are case-insensitive on some platforms.
  1724. #if defined(_WIN32) || defined(__APPLE__)
  1725. std::string regex = cmSystemTools::LowerCase(args[i]);
  1726. #else
  1727. std::string regex = args[i];
  1728. #endif
  1729. cmSystemTools::ReplaceString(regex, "\\", "\\\\");
  1730. literal_args += regex;
  1731. literal_args += "\"";
  1732. doing = DoingNone;
  1733. } else if (doing == DoingComponent) {
  1734. component = args[i];
  1735. doing = DoingNone;
  1736. } else if (doing == DoingPermsFile) {
  1737. // Check the requested permission.
  1738. if (!cmInstallCommandArguments::CheckPermissions(args[i],
  1739. permissions_file)) {
  1740. status.SetError(cmStrCat(args[0], " given invalid file permission \"",
  1741. args[i], "\"."));
  1742. return false;
  1743. }
  1744. } else if (doing == DoingPermsDir) {
  1745. // Check the requested permission.
  1746. if (!cmInstallCommandArguments::CheckPermissions(args[i],
  1747. permissions_dir)) {
  1748. status.SetError(cmStrCat(
  1749. args[0], " given invalid directory permission \"", args[i], "\"."));
  1750. return false;
  1751. }
  1752. } else if (doing == DoingPermsMatch) {
  1753. // Check the requested permission.
  1754. if (!cmInstallCommandArguments::CheckPermissions(args[i],
  1755. literal_args)) {
  1756. status.SetError(
  1757. cmStrCat(args[0], " given invalid permission \"", args[i], "\"."));
  1758. return false;
  1759. }
  1760. } else {
  1761. // Unknown argument.
  1762. status.SetError(
  1763. cmStrCat(args[0], " given unknown argument \"", args[i], "\"."));
  1764. return false;
  1765. }
  1766. }
  1767. // Support installing an empty directory.
  1768. if (dirs.empty() && destination.has_value()) {
  1769. dirs.emplace_back();
  1770. }
  1771. // Check if there is something to do.
  1772. if (dirs.empty()) {
  1773. return true;
  1774. }
  1775. if (!destination.has_value()) {
  1776. if (type.empty()) {
  1777. // A destination is required.
  1778. status.SetError(cmStrCat(args[0], " given no DESTINATION!"));
  1779. return false;
  1780. }
  1781. destination = helper.GetDestinationForType(nullptr, type);
  1782. } else if (!type.empty()) {
  1783. status.SetError(cmStrCat(args[0],
  1784. " given both TYPE and DESTINATION "
  1785. "arguments. You may only specify one."));
  1786. return false;
  1787. }
  1788. cmInstallGenerator::MessageLevel message =
  1789. cmInstallGenerator::SelectMessageLevel(helper.Makefile, message_never);
  1790. // Create the directory install generator.
  1791. helper.Makefile->AddInstallGenerator(
  1792. cm::make_unique<cmInstallDirectoryGenerator>(
  1793. dirs, *destination, permissions_file, permissions_dir, configurations,
  1794. component, message, exclude_from_all, literal_args, optional,
  1795. helper.Makefile->GetBacktrace()));
  1796. // Tell the global generator about any installation component names
  1797. // specified.
  1798. helper.Makefile->GetGlobalGenerator()->AddInstallComponent(component);
  1799. return true;
  1800. }
  1801. bool HandleExportAndroidMKMode(std::vector<std::string> const& args,
  1802. cmExecutionStatus& status)
  1803. {
  1804. #ifndef CMAKE_BOOTSTRAP
  1805. Helper helper(status);
  1806. // This is the EXPORT mode.
  1807. cmInstallCommandArguments ica(helper.DefaultComponentName, *helper.Makefile);
  1808. std::string exp;
  1809. std::string name_space;
  1810. bool exportOld = false;
  1811. std::string filename;
  1812. ica.Bind("EXPORT_ANDROID_MK"_s, exp);
  1813. ica.Bind("NAMESPACE"_s, name_space);
  1814. ica.Bind("EXPORT_LINK_INTERFACE_LIBRARIES"_s, exportOld);
  1815. ica.Bind("FILE"_s, filename);
  1816. std::vector<std::string> unknownArgs;
  1817. ica.Parse(args, &unknownArgs);
  1818. if (!unknownArgs.empty()) {
  1819. // Unknown argument.
  1820. status.SetError(
  1821. cmStrCat(args[0], " given unknown argument \"", unknownArgs[0], "\"."));
  1822. return false;
  1823. }
  1824. if (!ica.Finalize()) {
  1825. return false;
  1826. }
  1827. // Make sure there is a destination.
  1828. if (ica.GetDestination().empty()) {
  1829. // A destination is required.
  1830. status.SetError(cmStrCat(args[0], " given no DESTINATION!"));
  1831. return false;
  1832. }
  1833. // Check the file name.
  1834. std::string fname = filename;
  1835. if (fname.find_first_of(":/\\") != std::string::npos) {
  1836. status.SetError(cmStrCat(args[0], " given invalid export file name \"",
  1837. fname,
  1838. "\". The FILE argument may not contain a path. "
  1839. "Specify the path in the DESTINATION argument."));
  1840. return false;
  1841. }
  1842. // Check the file extension.
  1843. if (!fname.empty() && !cmHasSuffix(fname, ".mk"_s)) {
  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() && !cmHasSuffix(fname, ".cmake"_s)) {
  2068. status.SetError(
  2069. cmStrCat(args[0], " given invalid export file name \"", fname,
  2070. "\". "
  2071. "The FILE argument must specify a name ending in \".cmake\"."));
  2072. return false;
  2073. }
  2074. // Construct the file name.
  2075. if (fname.empty()) {
  2076. fname = cmStrCat(exp, ".cmake");
  2077. if (fname.find_first_of(":/\\") != std::string::npos) {
  2078. status.SetError(cmStrCat(
  2079. args[0], " given export name \"", exp,
  2080. "\". "
  2081. "This name cannot be safely converted to a file name. "
  2082. "Specify a different export name or use the FILE option to set "
  2083. "a file name explicitly."));
  2084. return false;
  2085. }
  2086. }
  2087. cmExportSet& exportSet =
  2088. helper.Makefile->GetGlobalGenerator()->GetExportSets()[exp];
  2089. cmInstallGenerator::MessageLevel message =
  2090. cmInstallGenerator::SelectMessageLevel(helper.Makefile);
  2091. // Tell the global generator about any installation component names
  2092. // specified
  2093. helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
  2094. ica.GetComponent());
  2095. #ifndef CMAKE_BOOTSTRAP
  2096. // Check if PACKAGE_INFO export has been requested for this export set.
  2097. if (cmExperimental::HasSupportEnabled(
  2098. status.GetMakefile(), cmExperimental::Feature::MappedPackageInfo)) {
  2099. if (cmValue const& piExports = helper.Makefile->GetDefinition(
  2100. "CMAKE_INSTALL_EXPORTS_AS_PACKAGE_INFO")) {
  2101. for (auto const& pie : cmList{ piExports }) {
  2102. cm::optional<cm::string_view> const directive = MatchExport(pie, exp);
  2103. if (directive) {
  2104. if (!HandleMappedPackageInfo(exportSet, *directive, helper, ica,
  2105. status, message,
  2106. cxx_modules_directory)) {
  2107. return false;
  2108. }
  2109. }
  2110. }
  2111. }
  2112. }
  2113. #endif
  2114. // Create the export install generator.
  2115. helper.Makefile->AddInstallGenerator(
  2116. cm::make_unique<cmInstallCMakeConfigExportGenerator>(
  2117. &exportSet, ica.GetDestination(), ica.GetPermissions(),
  2118. ica.GetConfigurations(), ica.GetComponent(), message,
  2119. ica.GetExcludeFromAll(), std::move(fname), std::move(name_space),
  2120. std::move(cxx_modules_directory), exportOld, exportPackageDependencies,
  2121. helper.Makefile->GetBacktrace()));
  2122. return true;
  2123. }
  2124. bool HandlePackageInfoMode(std::vector<std::string> const& args,
  2125. cmExecutionStatus& status)
  2126. {
  2127. #ifndef CMAKE_BOOTSTRAP
  2128. Helper helper(status);
  2129. // This is the PACKAGE_INFO mode.
  2130. cmInstallCommandArguments ica(helper.DefaultComponentName, *helper.Makefile);
  2131. cmPackageInfoArguments arguments;
  2132. ArgumentParser::NonEmpty<std::string> exportName;
  2133. ArgumentParser::NonEmpty<std::string> cxxModulesDirectory;
  2134. arguments.Bind(ica);
  2135. ica.Bind("EXPORT"_s, exportName);
  2136. ica.Bind("CXX_MODULES_DIRECTORY"_s, cxxModulesDirectory);
  2137. std::vector<std::string> unknownArgs;
  2138. ArgumentParser::ParseResult result = ica.Parse(args, &unknownArgs);
  2139. if (!result.Check(args[0], &unknownArgs, status)) {
  2140. return false;
  2141. }
  2142. if (!ica.Finalize()) {
  2143. return false;
  2144. }
  2145. if (exportName.empty()) {
  2146. status.SetError(cmStrCat(args[0], " missing EXPORT."));
  2147. return false;
  2148. }
  2149. if (!arguments.Check(status) || !arguments.SetMetadataFromProject(status)) {
  2150. return false;
  2151. }
  2152. // Get or construct the destination path.
  2153. std::string dest = ica.GetDestination();
  2154. if (dest.empty()) {
  2155. if (helper.Makefile->GetSafeDefinition("CMAKE_SYSTEM_NAME") == "Windows") {
  2156. dest = std::string{ "cps"_s };
  2157. } else {
  2158. dest = cmStrCat(helper.GetLibraryDestination(nullptr), "/cps/",
  2159. arguments.GetPackageDirName());
  2160. }
  2161. }
  2162. cmExportSet& exportSet =
  2163. helper.Makefile->GetGlobalGenerator()->GetExportSets()[exportName];
  2164. cmInstallGenerator::MessageLevel message =
  2165. cmInstallGenerator::SelectMessageLevel(helper.Makefile);
  2166. // Tell the global generator about any installation component names
  2167. // specified
  2168. helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
  2169. ica.GetComponent());
  2170. // Create the export install generator.
  2171. helper.Makefile->AddInstallGenerator(
  2172. cm::make_unique<cmInstallPackageInfoExportGenerator>(
  2173. &exportSet, dest, ica.GetPermissions(), ica.GetConfigurations(),
  2174. ica.GetComponent(), message, ica.GetExcludeFromAll(),
  2175. std::move(arguments), std::move(cxxModulesDirectory),
  2176. helper.Makefile->GetBacktrace()));
  2177. return true;
  2178. #else
  2179. static_cast<void>(args);
  2180. status.SetError("PACKAGE_INFO not supported in bootstrap cmake");
  2181. return false;
  2182. #endif
  2183. }
  2184. bool HandleRuntimeDependencySetMode(std::vector<std::string> const& args,
  2185. cmExecutionStatus& status)
  2186. {
  2187. Helper helper(status);
  2188. auto system = helper.Makefile->GetSafeDefinition("CMAKE_HOST_SYSTEM_NAME");
  2189. if (!cmRuntimeDependencyArchive::PlatformSupportsRuntimeDependencies(
  2190. system)) {
  2191. status.SetError(cmStrCat(
  2192. "RUNTIME_DEPENDENCY_SET is not supported on system \"", system, '"'));
  2193. return false;
  2194. }
  2195. // This is the RUNTIME_DEPENDENCY_SET mode.
  2196. cmInstallRuntimeDependencySet* runtimeDependencySet;
  2197. struct ArgVectors
  2198. {
  2199. ArgumentParser::MaybeEmpty<std::vector<std::string>> Library;
  2200. ArgumentParser::MaybeEmpty<std::vector<std::string>> Runtime;
  2201. ArgumentParser::MaybeEmpty<std::vector<std::string>> Framework;
  2202. };
  2203. static auto const argHelper = cmArgumentParser<ArgVectors>{}
  2204. .Bind("LIBRARY"_s, &ArgVectors::Library)
  2205. .Bind("RUNTIME"_s, &ArgVectors::Runtime)
  2206. .Bind("FRAMEWORK"_s, &ArgVectors::Framework);
  2207. std::vector<std::string> genericArgVector;
  2208. ArgVectors const argVectors = argHelper.Parse(args, &genericArgVector);
  2209. // now parse the generic args (i.e. the ones not specialized on LIBRARY,
  2210. // RUNTIME, FRAMEWORK etc. (see above)
  2211. // These generic args also contain the runtime dependency set
  2212. std::string runtimeDependencySetArg;
  2213. std::vector<std::string> runtimeDependencyArgVector;
  2214. cmInstallCommandArguments genericArgs(helper.DefaultComponentName,
  2215. *helper.Makefile);
  2216. genericArgs.Bind("RUNTIME_DEPENDENCY_SET"_s, runtimeDependencySetArg);
  2217. genericArgs.Parse(genericArgVector, &runtimeDependencyArgVector);
  2218. bool success = genericArgs.Finalize();
  2219. cmInstallCommandArguments libraryArgs(helper.DefaultComponentName,
  2220. *helper.Makefile);
  2221. cmInstallCommandArguments runtimeArgs(helper.DefaultComponentName,
  2222. *helper.Makefile);
  2223. cmInstallCommandArguments frameworkArgs(helper.DefaultComponentName,
  2224. *helper.Makefile);
  2225. // Now also parse the file(GET_RUNTIME_DEPENDENCY) args
  2226. std::vector<std::string> unknownArgs;
  2227. auto runtimeDependencyArgs = RuntimeDependenciesArgHelper.Parse(
  2228. runtimeDependencyArgVector, &unknownArgs);
  2229. // now parse the args for specific parts of the target (e.g. LIBRARY,
  2230. // RUNTIME, FRAMEWORK etc.
  2231. libraryArgs.Parse(argVectors.Library, &unknownArgs);
  2232. runtimeArgs.Parse(argVectors.Runtime, &unknownArgs);
  2233. frameworkArgs.Parse(argVectors.Framework, &unknownArgs);
  2234. libraryArgs.SetGenericArguments(&genericArgs);
  2235. runtimeArgs.SetGenericArguments(&genericArgs);
  2236. frameworkArgs.SetGenericArguments(&genericArgs);
  2237. success = success && libraryArgs.Finalize();
  2238. success = success && runtimeArgs.Finalize();
  2239. success = success && frameworkArgs.Finalize();
  2240. if (!success) {
  2241. return false;
  2242. }
  2243. if (!unknownArgs.empty()) {
  2244. helper.SetError(
  2245. cmStrCat("RUNTIME_DEPENDENCY_SET given unknown argument \"",
  2246. unknownArgs.front(), "\"."));
  2247. return false;
  2248. }
  2249. if (runtimeDependencySetArg.empty()) {
  2250. helper.SetError(
  2251. "RUNTIME_DEPENDENCY_SET not given a runtime dependency set.");
  2252. return false;
  2253. }
  2254. runtimeDependencySet =
  2255. helper.Makefile->GetGlobalGenerator()->GetNamedRuntimeDependencySet(
  2256. runtimeDependencySetArg);
  2257. bool installsRuntime = false;
  2258. bool installsLibrary = false;
  2259. bool installsFramework = false;
  2260. AddInstallRuntimeDependenciesGenerator(
  2261. helper, runtimeDependencySet, runtimeArgs, libraryArgs, frameworkArgs,
  2262. std::move(runtimeDependencyArgs), installsRuntime, installsLibrary,
  2263. installsFramework);
  2264. // Tell the global generator about any installation component names
  2265. // specified
  2266. if (installsLibrary) {
  2267. helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
  2268. libraryArgs.GetComponent());
  2269. }
  2270. if (installsRuntime) {
  2271. helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
  2272. runtimeArgs.GetComponent());
  2273. }
  2274. if (installsFramework) {
  2275. helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
  2276. frameworkArgs.GetComponent());
  2277. }
  2278. return true;
  2279. }
  2280. bool HandleSbomMode(std::vector<std::string> const& args,
  2281. cmExecutionStatus& status)
  2282. {
  2283. #ifndef CMAKE_BOOTSTRAP
  2284. if (!cmExperimental::HasSupportEnabled(
  2285. status.GetMakefile(), cmExperimental::Feature::GenerateSbom)) {
  2286. status.SetError("does not recognize sub-command SBOM");
  2287. return false;
  2288. }
  2289. Helper helper(status);
  2290. cmInstallCommandArguments ica(helper.DefaultComponentName, *helper.Makefile);
  2291. cmSbomArguments arguments;
  2292. ArgumentParser::NonEmpty<std::string> exportName;
  2293. ArgumentParser::NonEmpty<std::string> cxxModulesDirectory;
  2294. arguments.Bind(ica);
  2295. ica.Bind("EXPORT"_s, exportName);
  2296. // ica.Bind("CXX_MODULES_DIRECTORY"_s, cxxModulesDirectory); TODO?
  2297. std::vector<std::string> unknownArgs;
  2298. ica.Parse(args, &unknownArgs);
  2299. ArgumentParser::ParseResult result = ica.Parse(args, &unknownArgs);
  2300. if (!result.Check(args[0], &unknownArgs, status)) {
  2301. return false;
  2302. }
  2303. if (!ica.Finalize()) {
  2304. return false;
  2305. }
  2306. if (arguments.PackageName.empty()) {
  2307. // TODO: Fix our use of the parser to enforce this.
  2308. status.SetError(cmStrCat(args[0], " missing SBOM name."));
  2309. return false;
  2310. }
  2311. if (exportName.empty()) {
  2312. status.SetError(cmStrCat(args[0], " missing EXPORT."));
  2313. return false;
  2314. }
  2315. if (!arguments.Check(status) || !arguments.SetMetadataFromProject(status)) {
  2316. return false;
  2317. }
  2318. // Get or construct the destination path.
  2319. std::string dest = ica.GetDestination();
  2320. if (dest.empty()) {
  2321. if (helper.Makefile->GetSafeDefinition("CMAKE_SYSTEM_NAME") == "Windows") {
  2322. dest = std::string{ "/sbom/"_s };
  2323. } else {
  2324. dest = cmStrCat(helper.GetLibraryDestination(nullptr), "/sbom/",
  2325. arguments.GetPackageDirName());
  2326. }
  2327. }
  2328. cmExportSet& exportSet =
  2329. helper.Makefile->GetGlobalGenerator()->GetExportSets()[exportName];
  2330. cmInstallGenerator::MessageLevel message =
  2331. cmInstallGenerator::SelectMessageLevel(helper.Makefile);
  2332. // Tell the global generator about any installation component names
  2333. // specified
  2334. helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
  2335. ica.GetComponent());
  2336. helper.Makefile->SetExplicitlyGeneratesSbom(true);
  2337. // Create the export install generator.
  2338. helper.Makefile->AddInstallGenerator(
  2339. cm::make_unique<cmInstallSbomExportGenerator>(
  2340. &exportSet, dest, ica.GetPermissions(), ica.GetConfigurations(),
  2341. ica.GetComponent(), message, ica.GetExcludeFromAll(),
  2342. std::move(arguments), std::move(cxxModulesDirectory),
  2343. helper.Makefile->GetBacktrace()));
  2344. return true;
  2345. #else
  2346. static_cast<void>(args);
  2347. status.SetError("SBOM not supported in bootstrap cmake");
  2348. return false;
  2349. #endif
  2350. }
  2351. bool Helper::MakeFilesFullPath(char const* modeName,
  2352. std::vector<std::string> const& relFiles,
  2353. std::vector<std::string>& absFiles)
  2354. {
  2355. return this->MakeFilesFullPath(
  2356. modeName, this->Makefile->GetCurrentSourceDirectory(), relFiles, absFiles);
  2357. }
  2358. bool Helper::MakeFilesFullPath(char const* modeName,
  2359. std::string const& basePath,
  2360. std::vector<std::string> const& relFiles,
  2361. std::vector<std::string>& absFiles)
  2362. {
  2363. for (std::string const& relFile : relFiles) {
  2364. std::string file = relFile;
  2365. std::string::size_type gpos = cmGeneratorExpression::Find(file);
  2366. if (gpos != 0 && !cmSystemTools::FileIsFullPath(file)) {
  2367. file = cmStrCat(basePath, '/', relFile);
  2368. }
  2369. // Make sure the file is not a directory.
  2370. if (gpos == std::string::npos && !cmSystemTools::FileIsSymlink(file) &&
  2371. cmSystemTools::FileIsDirectory(file)) {
  2372. this->SetError(
  2373. cmStrCat(modeName, " given directory \"", relFile, "\" to install."));
  2374. return false;
  2375. }
  2376. // Store the file for installation.
  2377. absFiles.push_back(std::move(file));
  2378. }
  2379. return true;
  2380. }
  2381. std::string Helper::GetDestination(cmInstallCommandArguments const* args,
  2382. std::string const& varName,
  2383. std::string const& guess) const
  2384. {
  2385. if (args && !args->GetDestination().empty()) {
  2386. return args->GetDestination();
  2387. }
  2388. std::string val = this->Makefile->GetSafeDefinition(varName);
  2389. if (!val.empty()) {
  2390. return val;
  2391. }
  2392. return guess;
  2393. }
  2394. std::string Helper::GetRuntimeDestination(
  2395. cmInstallCommandArguments const* args) const
  2396. {
  2397. return this->GetDestination(args, "CMAKE_INSTALL_BINDIR", "bin");
  2398. }
  2399. std::string Helper::GetSbinDestination(
  2400. cmInstallCommandArguments const* args) const
  2401. {
  2402. return this->GetDestination(args, "CMAKE_INSTALL_SBINDIR", "sbin");
  2403. }
  2404. std::string Helper::GetArchiveDestination(
  2405. cmInstallCommandArguments const* args) const
  2406. {
  2407. return this->GetDestination(args, "CMAKE_INSTALL_LIBDIR", "lib");
  2408. }
  2409. std::string Helper::GetLibraryDestination(
  2410. cmInstallCommandArguments const* args) const
  2411. {
  2412. return this->GetDestination(args, "CMAKE_INSTALL_LIBDIR", "lib");
  2413. }
  2414. std::string Helper::GetCxxModulesBmiDestination(
  2415. cmInstallCommandArguments const* args) const
  2416. {
  2417. if (args) {
  2418. return args->GetDestination();
  2419. }
  2420. return {};
  2421. }
  2422. std::string Helper::GetIncludeDestination(
  2423. cmInstallCommandArguments const* args) const
  2424. {
  2425. return this->GetDestination(args, "CMAKE_INSTALL_INCLUDEDIR", "include");
  2426. }
  2427. std::string Helper::GetSysconfDestination(
  2428. cmInstallCommandArguments const* args) const
  2429. {
  2430. return this->GetDestination(args, "CMAKE_INSTALL_SYSCONFDIR", "etc");
  2431. }
  2432. std::string Helper::GetSharedStateDestination(
  2433. cmInstallCommandArguments const* args) const
  2434. {
  2435. return this->GetDestination(args, "CMAKE_INSTALL_SHAREDSTATEDIR", "com");
  2436. }
  2437. std::string Helper::GetLocalStateDestination(
  2438. cmInstallCommandArguments const* args) const
  2439. {
  2440. return this->GetDestination(args, "CMAKE_INSTALL_LOCALSTATEDIR", "var");
  2441. }
  2442. std::string Helper::GetRunStateDestination(
  2443. cmInstallCommandArguments const* args) const
  2444. {
  2445. return this->GetDestination(args, "CMAKE_INSTALL_RUNSTATEDIR",
  2446. this->GetLocalStateDestination(nullptr) +
  2447. "/run");
  2448. }
  2449. std::string Helper::GetDataRootDestination(
  2450. cmInstallCommandArguments const* args) const
  2451. {
  2452. return this->GetDestination(args, "CMAKE_INSTALL_DATAROOTDIR", "share");
  2453. }
  2454. std::string Helper::GetDataDestination(
  2455. cmInstallCommandArguments const* args) const
  2456. {
  2457. return this->GetDestination(args, "CMAKE_INSTALL_DATADIR",
  2458. this->GetDataRootDestination(nullptr));
  2459. }
  2460. std::string Helper::GetInfoDestination(
  2461. cmInstallCommandArguments const* args) const
  2462. {
  2463. return this->GetDestination(args, "CMAKE_INSTALL_INFODIR",
  2464. this->GetDataRootDestination(nullptr) + "/info");
  2465. }
  2466. std::string Helper::GetLocaleDestination(
  2467. cmInstallCommandArguments const* args) const
  2468. {
  2469. return this->GetDestination(args, "CMAKE_INSTALL_LOCALEDIR",
  2470. this->GetDataRootDestination(nullptr) +
  2471. "/locale");
  2472. }
  2473. std::string Helper::GetManDestination(
  2474. cmInstallCommandArguments const* args) const
  2475. {
  2476. return this->GetDestination(args, "CMAKE_INSTALL_MANDIR",
  2477. this->GetDataRootDestination(nullptr) + "/man");
  2478. }
  2479. std::string Helper::GetDocDestination(
  2480. cmInstallCommandArguments const* args) const
  2481. {
  2482. return this->GetDestination(args, "CMAKE_INSTALL_DOCDIR",
  2483. this->GetDataRootDestination(nullptr) + "/doc");
  2484. }
  2485. std::string Helper::GetProgramExecutablesDestination(
  2486. cmInstallCommandArguments const* args) const
  2487. {
  2488. return this->GetDestination(args, "CMAKE_INSTALL_LIBEXECDIR", "libexec");
  2489. }
  2490. std::string Helper::GetDestinationForType(
  2491. cmInstallCommandArguments const* args, std::string const& type) const
  2492. {
  2493. if (args && !args->GetDestination().empty()) {
  2494. return args->GetDestination();
  2495. }
  2496. if (type == "BIN") {
  2497. return this->GetRuntimeDestination(nullptr);
  2498. }
  2499. if (type == "SBIN") {
  2500. return this->GetSbinDestination(nullptr);
  2501. }
  2502. if (type == "SYSCONF") {
  2503. return this->GetSysconfDestination(nullptr);
  2504. }
  2505. if (type == "SHAREDSTATE") {
  2506. return this->GetSharedStateDestination(nullptr);
  2507. }
  2508. if (type == "LOCALSTATE") {
  2509. return this->GetLocalStateDestination(nullptr);
  2510. }
  2511. if (type == "RUNSTATE") {
  2512. return this->GetRunStateDestination(nullptr);
  2513. }
  2514. if (type == "LIB") {
  2515. return this->GetLibraryDestination(nullptr);
  2516. }
  2517. if (type == "INCLUDE") {
  2518. return this->GetIncludeDestination(nullptr);
  2519. }
  2520. if (type == "DATA") {
  2521. return this->GetDataDestination(nullptr);
  2522. }
  2523. if (type == "INFO") {
  2524. return this->GetInfoDestination(nullptr);
  2525. }
  2526. if (type == "LOCALE") {
  2527. return this->GetLocaleDestination(nullptr);
  2528. }
  2529. if (type == "MAN") {
  2530. return this->GetManDestination(nullptr);
  2531. }
  2532. if (type == "DOC") {
  2533. return this->GetDocDestination(nullptr);
  2534. }
  2535. if (type == "LIBEXEC") {
  2536. return this->GetProgramExecutablesDestination(nullptr);
  2537. }
  2538. return "";
  2539. }
  2540. } // namespace
  2541. bool cmInstallCommand(std::vector<std::string> const& args,
  2542. cmExecutionStatus& status)
  2543. {
  2544. // Allow calling with no arguments so that arguments may be built up
  2545. // using a variable that may be left empty.
  2546. if (args.empty()) {
  2547. return true;
  2548. }
  2549. // Enable the install target.
  2550. status.GetMakefile().GetGlobalGenerator()->EnableInstallTarget();
  2551. static cmSubcommandTable const subcommand{
  2552. { "SCRIPT"_s, HandleScriptMode },
  2553. { "CODE"_s, HandleScriptMode },
  2554. { "TARGETS"_s, HandleTargetsMode },
  2555. { "IMPORTED_RUNTIME_ARTIFACTS"_s, HandleImportedRuntimeArtifactsMode },
  2556. { "FILES"_s, HandleFilesMode },
  2557. { "PROGRAMS"_s, HandleFilesMode },
  2558. { "DIRECTORY"_s, HandleDirectoryMode },
  2559. { "EXPORT"_s, HandleExportMode },
  2560. { "EXPORT_ANDROID_MK"_s, HandleExportAndroidMKMode },
  2561. { "PACKAGE_INFO"_s, HandlePackageInfoMode },
  2562. { "RUNTIME_DEPENDENCY_SET"_s, HandleRuntimeDependencySetMode },
  2563. { "SBOM"_s, HandleSbomMode }
  2564. };
  2565. return subcommand(args[0], args, status);
  2566. }