cmInstallCommand.cxx 103 KB

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