cmInstallCommand.cxx 77 KB

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