cmInstallCommand.cxx 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644
  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 <cstddef>
  5. #include <set>
  6. #include <sstream>
  7. #include <utility>
  8. #include <cm/memory>
  9. #include "cmsys/Glob.hxx"
  10. #include "cm_static_string_view.hxx"
  11. #include "cmArgumentParser.h"
  12. #include "cmExecutionStatus.h"
  13. #include "cmExportSet.h"
  14. #include "cmGeneratorExpression.h"
  15. #include "cmGlobalGenerator.h"
  16. #include "cmInstallCommandArguments.h"
  17. #include "cmInstallDirectoryGenerator.h"
  18. #include "cmInstallExportGenerator.h"
  19. #include "cmInstallFilesGenerator.h"
  20. #include "cmInstallGenerator.h"
  21. #include "cmInstallScriptGenerator.h"
  22. #include "cmInstallTargetGenerator.h"
  23. #include "cmListFileCache.h"
  24. #include "cmMakefile.h"
  25. #include "cmMessageType.h"
  26. #include "cmPolicies.h"
  27. #include "cmStateTypes.h"
  28. #include "cmStringAlgorithms.h"
  29. #include "cmSubcommandTable.h"
  30. #include "cmSystemTools.h"
  31. #include "cmTarget.h"
  32. #include "cmTargetExport.h"
  33. namespace {
  34. class Helper
  35. {
  36. public:
  37. Helper(cmExecutionStatus& status)
  38. : Status(status)
  39. , Makefile(&status.GetMakefile())
  40. {
  41. this->DefaultComponentName = this->Makefile->GetSafeDefinition(
  42. "CMAKE_INSTALL_DEFAULT_COMPONENT_NAME");
  43. if (this->DefaultComponentName.empty()) {
  44. this->DefaultComponentName = "Unspecified";
  45. }
  46. }
  47. void SetError(std::string const& err) { this->Status.SetError(err); }
  48. bool MakeFilesFullPath(const char* modeName,
  49. const std::vector<std::string>& relFiles,
  50. std::vector<std::string>& absFiles);
  51. bool CheckCMP0006(bool& failure);
  52. std::string GetDestination(const cmInstallCommandArguments* args,
  53. const std::string& varName,
  54. const std::string& guess);
  55. std::string GetRuntimeDestination(const cmInstallCommandArguments* args);
  56. std::string GetSbinDestination(const cmInstallCommandArguments* args);
  57. std::string GetArchiveDestination(const cmInstallCommandArguments* args);
  58. std::string GetLibraryDestination(const cmInstallCommandArguments* args);
  59. std::string GetIncludeDestination(const cmInstallCommandArguments* args);
  60. std::string GetSysconfDestination(const cmInstallCommandArguments* args);
  61. std::string GetSharedStateDestination(const cmInstallCommandArguments* args);
  62. std::string GetLocalStateDestination(const cmInstallCommandArguments* args);
  63. std::string GetRunStateDestination(const cmInstallCommandArguments* args);
  64. std::string GetDataRootDestination(const cmInstallCommandArguments* args);
  65. std::string GetDataDestination(const cmInstallCommandArguments* args);
  66. std::string GetInfoDestination(const cmInstallCommandArguments* args);
  67. std::string GetLocaleDestination(const cmInstallCommandArguments* args);
  68. std::string GetManDestination(const cmInstallCommandArguments* args);
  69. std::string GetDocDestination(const cmInstallCommandArguments* args);
  70. std::string GetDestinationForType(const cmInstallCommandArguments* args,
  71. const std::string& type);
  72. cmExecutionStatus& Status;
  73. cmMakefile* Makefile;
  74. std::string DefaultComponentName;
  75. };
  76. std::unique_ptr<cmInstallTargetGenerator> CreateInstallTargetGenerator(
  77. cmTarget& target, const cmInstallCommandArguments& args, bool impLib,
  78. cmListFileBacktrace const& backtrace, const std::string& destination,
  79. bool forceOpt = false, bool namelink = false)
  80. {
  81. cmInstallGenerator::MessageLevel message =
  82. cmInstallGenerator::SelectMessageLevel(target.GetMakefile());
  83. target.SetHaveInstallRule(true);
  84. const std::string& component =
  85. namelink ? args.GetNamelinkComponent() : args.GetComponent();
  86. auto g = cm::make_unique<cmInstallTargetGenerator>(
  87. target.GetName(), destination, impLib, args.GetPermissions(),
  88. args.GetConfigurations(), component, message, args.GetExcludeFromAll(),
  89. args.GetOptional() || forceOpt, backtrace);
  90. target.AddInstallGenerator(g.get());
  91. return g;
  92. }
  93. std::unique_ptr<cmInstallTargetGenerator> CreateInstallTargetGenerator(
  94. cmTarget& target, const cmInstallCommandArguments& args, bool impLib,
  95. cmListFileBacktrace const& backtrace, bool forceOpt = false,
  96. bool namelink = false)
  97. {
  98. return CreateInstallTargetGenerator(target, args, impLib, backtrace,
  99. args.GetDestination(), forceOpt,
  100. namelink);
  101. }
  102. std::unique_ptr<cmInstallFilesGenerator> CreateInstallFilesGenerator(
  103. cmMakefile* mf, const std::vector<std::string>& absFiles,
  104. const cmInstallCommandArguments& args, bool programs,
  105. const std::string& destination)
  106. {
  107. cmInstallGenerator::MessageLevel message =
  108. cmInstallGenerator::SelectMessageLevel(mf);
  109. return cm::make_unique<cmInstallFilesGenerator>(
  110. absFiles, destination, programs, args.GetPermissions(),
  111. args.GetConfigurations(), args.GetComponent(), message,
  112. args.GetExcludeFromAll(), args.GetRename(), args.GetOptional());
  113. }
  114. std::unique_ptr<cmInstallFilesGenerator> CreateInstallFilesGenerator(
  115. cmMakefile* mf, const std::vector<std::string>& absFiles,
  116. const cmInstallCommandArguments& args, bool programs)
  117. {
  118. return CreateInstallFilesGenerator(mf, absFiles, args, programs,
  119. args.GetDestination());
  120. }
  121. std::set<std::string> const allowedTypes{
  122. "BIN", "SBIN", "LIB", "INCLUDE", "SYSCONF",
  123. "SHAREDSTATE", "LOCALSTATE", "RUNSTATE", "DATA", "INFO",
  124. "LOCALE", "MAN", "DOC",
  125. };
  126. bool HandleScriptMode(std::vector<std::string> const& args,
  127. cmExecutionStatus& status)
  128. {
  129. Helper helper(status);
  130. std::string component = helper.DefaultComponentName;
  131. int componentCount = 0;
  132. bool doing_script = false;
  133. bool doing_code = false;
  134. bool exclude_from_all = false;
  135. // Scan the args once for COMPONENT. Only allow one.
  136. //
  137. for (size_t i = 0; i < args.size(); ++i) {
  138. if (args[i] == "COMPONENT" && i + 1 < args.size()) {
  139. ++componentCount;
  140. ++i;
  141. component = args[i];
  142. }
  143. if (args[i] == "EXCLUDE_FROM_ALL") {
  144. exclude_from_all = true;
  145. }
  146. }
  147. if (componentCount > 1) {
  148. status.SetError("given more than one COMPONENT for the SCRIPT or CODE "
  149. "signature of the INSTALL command. "
  150. "Use multiple INSTALL commands with one COMPONENT each.");
  151. return false;
  152. }
  153. // Scan the args again, this time adding install generators each time we
  154. // encounter a SCRIPT or CODE arg:
  155. //
  156. for (std::string const& arg : args) {
  157. if (arg == "SCRIPT") {
  158. doing_script = true;
  159. doing_code = false;
  160. } else if (arg == "CODE") {
  161. doing_script = false;
  162. doing_code = true;
  163. } else if (arg == "COMPONENT") {
  164. doing_script = false;
  165. doing_code = false;
  166. } else if (doing_script) {
  167. doing_script = false;
  168. std::string script = arg;
  169. if (!cmSystemTools::FileIsFullPath(script)) {
  170. script =
  171. cmStrCat(helper.Makefile->GetCurrentSourceDirectory(), '/', arg);
  172. }
  173. if (cmSystemTools::FileIsDirectory(script)) {
  174. status.SetError("given a directory as value of SCRIPT argument.");
  175. return false;
  176. }
  177. helper.Makefile->AddInstallGenerator(
  178. cm::make_unique<cmInstallScriptGenerator>(
  179. script, false, component.c_str(), exclude_from_all));
  180. } else if (doing_code) {
  181. doing_code = false;
  182. std::string const& code = arg;
  183. helper.Makefile->AddInstallGenerator(
  184. cm::make_unique<cmInstallScriptGenerator>(
  185. code, true, component.c_str(), exclude_from_all));
  186. }
  187. }
  188. if (doing_script) {
  189. status.SetError("given no value for SCRIPT argument.");
  190. return false;
  191. }
  192. if (doing_code) {
  193. status.SetError("given no value for CODE argument.");
  194. return false;
  195. }
  196. // Tell the global generator about any installation component names
  197. // specified.
  198. helper.Makefile->GetGlobalGenerator()->AddInstallComponent(component);
  199. return true;
  200. }
  201. bool HandleTargetsMode(std::vector<std::string> const& args,
  202. cmExecutionStatus& status)
  203. {
  204. Helper helper(status);
  205. // This is the TARGETS mode.
  206. std::vector<cmTarget*> targets;
  207. struct ArgVectors
  208. {
  209. std::vector<std::string> Archive;
  210. std::vector<std::string> Library;
  211. std::vector<std::string> Runtime;
  212. std::vector<std::string> Object;
  213. std::vector<std::string> Framework;
  214. std::vector<std::string> Bundle;
  215. std::vector<std::string> Includes;
  216. std::vector<std::string> PrivateHeader;
  217. std::vector<std::string> PublicHeader;
  218. std::vector<std::string> Resource;
  219. };
  220. static auto const argHelper =
  221. cmArgumentParser<ArgVectors>{}
  222. .Bind("ARCHIVE"_s, &ArgVectors::Archive)
  223. .Bind("LIBRARY"_s, &ArgVectors::Library)
  224. .Bind("RUNTIME"_s, &ArgVectors::Runtime)
  225. .Bind("OBJECTS"_s, &ArgVectors::Object)
  226. .Bind("FRAMEWORK"_s, &ArgVectors::Framework)
  227. .Bind("BUNDLE"_s, &ArgVectors::Bundle)
  228. .Bind("INCLUDES"_s, &ArgVectors::Includes)
  229. .Bind("PRIVATE_HEADER"_s, &ArgVectors::PrivateHeader)
  230. .Bind("PUBLIC_HEADER"_s, &ArgVectors::PublicHeader)
  231. .Bind("RESOURCE"_s, &ArgVectors::Resource);
  232. std::vector<std::string> genericArgVector;
  233. ArgVectors const argVectors = argHelper.Parse(args, &genericArgVector);
  234. // now parse the generic args (i.e. the ones not specialized on LIBRARY/
  235. // ARCHIVE, RUNTIME etc. (see above)
  236. // These generic args also contain the targets and the export stuff
  237. std::vector<std::string> targetList;
  238. std::string exports;
  239. std::vector<std::string> unknownArgs;
  240. cmInstallCommandArguments genericArgs(helper.DefaultComponentName);
  241. genericArgs.Bind("TARGETS"_s, targetList);
  242. genericArgs.Bind("EXPORT"_s, exports);
  243. genericArgs.Parse(genericArgVector, &unknownArgs);
  244. bool success = genericArgs.Finalize();
  245. cmInstallCommandArguments archiveArgs(helper.DefaultComponentName);
  246. cmInstallCommandArguments libraryArgs(helper.DefaultComponentName);
  247. cmInstallCommandArguments runtimeArgs(helper.DefaultComponentName);
  248. cmInstallCommandArguments objectArgs(helper.DefaultComponentName);
  249. cmInstallCommandArguments frameworkArgs(helper.DefaultComponentName);
  250. cmInstallCommandArguments bundleArgs(helper.DefaultComponentName);
  251. cmInstallCommandArguments privateHeaderArgs(helper.DefaultComponentName);
  252. cmInstallCommandArguments publicHeaderArgs(helper.DefaultComponentName);
  253. cmInstallCommandArguments resourceArgs(helper.DefaultComponentName);
  254. cmInstallCommandIncludesArgument includesArgs;
  255. // now parse the args for specific parts of the target (e.g. LIBRARY,
  256. // RUNTIME, ARCHIVE etc.
  257. archiveArgs.Parse(argVectors.Archive, &unknownArgs);
  258. libraryArgs.Parse(argVectors.Library, &unknownArgs);
  259. runtimeArgs.Parse(argVectors.Runtime, &unknownArgs);
  260. objectArgs.Parse(argVectors.Object, &unknownArgs);
  261. frameworkArgs.Parse(argVectors.Framework, &unknownArgs);
  262. bundleArgs.Parse(argVectors.Bundle, &unknownArgs);
  263. privateHeaderArgs.Parse(argVectors.PrivateHeader, &unknownArgs);
  264. publicHeaderArgs.Parse(argVectors.PublicHeader, &unknownArgs);
  265. resourceArgs.Parse(argVectors.Resource, &unknownArgs);
  266. includesArgs.Parse(&argVectors.Includes, &unknownArgs);
  267. if (!unknownArgs.empty()) {
  268. // Unknown argument.
  269. status.SetError(
  270. cmStrCat("TARGETS given unknown argument \"", unknownArgs[0], "\"."));
  271. return false;
  272. }
  273. // apply generic args
  274. archiveArgs.SetGenericArguments(&genericArgs);
  275. libraryArgs.SetGenericArguments(&genericArgs);
  276. runtimeArgs.SetGenericArguments(&genericArgs);
  277. objectArgs.SetGenericArguments(&genericArgs);
  278. frameworkArgs.SetGenericArguments(&genericArgs);
  279. bundleArgs.SetGenericArguments(&genericArgs);
  280. privateHeaderArgs.SetGenericArguments(&genericArgs);
  281. publicHeaderArgs.SetGenericArguments(&genericArgs);
  282. resourceArgs.SetGenericArguments(&genericArgs);
  283. success = success && archiveArgs.Finalize();
  284. success = success && libraryArgs.Finalize();
  285. success = success && runtimeArgs.Finalize();
  286. success = success && objectArgs.Finalize();
  287. success = success && frameworkArgs.Finalize();
  288. success = success && bundleArgs.Finalize();
  289. success = success && privateHeaderArgs.Finalize();
  290. success = success && publicHeaderArgs.Finalize();
  291. success = success && resourceArgs.Finalize();
  292. if (!success) {
  293. return false;
  294. }
  295. // Enforce argument rules too complex to specify for the
  296. // general-purpose parser.
  297. if (archiveArgs.GetNamelinkOnly() || runtimeArgs.GetNamelinkOnly() ||
  298. objectArgs.GetNamelinkOnly() || frameworkArgs.GetNamelinkOnly() ||
  299. bundleArgs.GetNamelinkOnly() || privateHeaderArgs.GetNamelinkOnly() ||
  300. publicHeaderArgs.GetNamelinkOnly() || resourceArgs.GetNamelinkOnly()) {
  301. status.SetError(
  302. "TARGETS given NAMELINK_ONLY option not in LIBRARY group. "
  303. "The NAMELINK_ONLY option may be specified only following LIBRARY.");
  304. return false;
  305. }
  306. if (archiveArgs.GetNamelinkSkip() || runtimeArgs.GetNamelinkSkip() ||
  307. objectArgs.GetNamelinkSkip() || frameworkArgs.GetNamelinkSkip() ||
  308. bundleArgs.GetNamelinkSkip() || privateHeaderArgs.GetNamelinkSkip() ||
  309. publicHeaderArgs.GetNamelinkSkip() || resourceArgs.GetNamelinkSkip()) {
  310. status.SetError(
  311. "TARGETS given NAMELINK_SKIP option not in LIBRARY group. "
  312. "The NAMELINK_SKIP option may be specified only following LIBRARY.");
  313. return false;
  314. }
  315. if (archiveArgs.HasNamelinkComponent() ||
  316. runtimeArgs.HasNamelinkComponent() ||
  317. objectArgs.HasNamelinkComponent() ||
  318. frameworkArgs.HasNamelinkComponent() ||
  319. bundleArgs.HasNamelinkComponent() ||
  320. privateHeaderArgs.HasNamelinkComponent() ||
  321. publicHeaderArgs.HasNamelinkComponent() ||
  322. resourceArgs.HasNamelinkComponent()) {
  323. status.SetError(
  324. "TARGETS given NAMELINK_COMPONENT option not in LIBRARY group. "
  325. "The NAMELINK_COMPONENT option may be specified only following "
  326. "LIBRARY.");
  327. return false;
  328. }
  329. if (libraryArgs.GetNamelinkOnly() && libraryArgs.GetNamelinkSkip()) {
  330. status.SetError("TARGETS given NAMELINK_ONLY and NAMELINK_SKIP. "
  331. "At most one of these two options may be specified.");
  332. return false;
  333. }
  334. if (!genericArgs.GetType().empty() || !archiveArgs.GetType().empty() ||
  335. !libraryArgs.GetType().empty() || !runtimeArgs.GetType().empty() ||
  336. !objectArgs.GetType().empty() || !frameworkArgs.GetType().empty() ||
  337. !bundleArgs.GetType().empty() || !privateHeaderArgs.GetType().empty() ||
  338. !publicHeaderArgs.GetType().empty() || !resourceArgs.GetType().empty()) {
  339. status.SetError(
  340. "TARGETS given TYPE option. The TYPE option may only be specified in "
  341. " install(FILES) and install(DIRECTORIES).");
  342. return false;
  343. }
  344. // Select the mode for installing symlinks to versioned shared libraries.
  345. cmInstallTargetGenerator::NamelinkModeType namelinkMode =
  346. cmInstallTargetGenerator::NamelinkModeNone;
  347. if (libraryArgs.GetNamelinkOnly()) {
  348. namelinkMode = cmInstallTargetGenerator::NamelinkModeOnly;
  349. } else if (libraryArgs.GetNamelinkSkip()) {
  350. namelinkMode = cmInstallTargetGenerator::NamelinkModeSkip;
  351. }
  352. // Check if there is something to do.
  353. if (targetList.empty()) {
  354. return true;
  355. }
  356. for (std::string const& tgt : targetList) {
  357. if (helper.Makefile->IsAlias(tgt)) {
  358. status.SetError(
  359. cmStrCat("TARGETS given target \"", tgt, "\" which is an alias."));
  360. return false;
  361. }
  362. // Lookup this target in the current directory.
  363. cmTarget* target = helper.Makefile->FindLocalNonAliasTarget(tgt);
  364. if (!target) {
  365. // If no local target has been found, find it in the global scope.
  366. cmTarget* const global_target =
  367. helper.Makefile->GetGlobalGenerator()->FindTarget(tgt, true);
  368. if (global_target && !global_target->IsImported()) {
  369. target = global_target;
  370. }
  371. }
  372. if (target) {
  373. // Found the target. Check its type.
  374. if (target->GetType() != cmStateEnums::EXECUTABLE &&
  375. target->GetType() != cmStateEnums::STATIC_LIBRARY &&
  376. target->GetType() != cmStateEnums::SHARED_LIBRARY &&
  377. target->GetType() != cmStateEnums::MODULE_LIBRARY &&
  378. target->GetType() != cmStateEnums::OBJECT_LIBRARY &&
  379. target->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  380. status.SetError(
  381. cmStrCat("TARGETS given target \"", tgt,
  382. "\" which is not an executable, library, or module."));
  383. return false;
  384. }
  385. // Store the target in the list to be installed.
  386. targets.push_back(target);
  387. } else {
  388. // Did not find the target.
  389. status.SetError(
  390. cmStrCat("TARGETS given target \"", tgt, "\" which does not exist."));
  391. return false;
  392. }
  393. }
  394. // Keep track of whether we will be performing an installation of
  395. // any files of the given type.
  396. bool installsArchive = false;
  397. bool installsLibrary = false;
  398. bool installsNamelink = false;
  399. bool installsRuntime = false;
  400. bool installsObject = false;
  401. bool installsFramework = false;
  402. bool installsBundle = false;
  403. bool installsPrivateHeader = false;
  404. bool installsPublicHeader = false;
  405. bool installsResource = false;
  406. // Generate install script code to install the given targets.
  407. for (cmTarget* ti : targets) {
  408. // Handle each target type.
  409. cmTarget& target = *ti;
  410. std::unique_ptr<cmInstallTargetGenerator> archiveGenerator;
  411. std::unique_ptr<cmInstallTargetGenerator> libraryGenerator;
  412. std::unique_ptr<cmInstallTargetGenerator> namelinkGenerator;
  413. std::unique_ptr<cmInstallTargetGenerator> runtimeGenerator;
  414. std::unique_ptr<cmInstallTargetGenerator> objectGenerator;
  415. std::unique_ptr<cmInstallTargetGenerator> frameworkGenerator;
  416. std::unique_ptr<cmInstallTargetGenerator> bundleGenerator;
  417. std::unique_ptr<cmInstallFilesGenerator> privateHeaderGenerator;
  418. std::unique_ptr<cmInstallFilesGenerator> publicHeaderGenerator;
  419. std::unique_ptr<cmInstallFilesGenerator> resourceGenerator;
  420. // Track whether this is a namelink-only rule.
  421. bool namelinkOnly = false;
  422. switch (target.GetType()) {
  423. case cmStateEnums::SHARED_LIBRARY: {
  424. // Shared libraries are handled differently on DLL and non-DLL
  425. // platforms. All windows platforms are DLL platforms including
  426. // cygwin. Currently no other platform is a DLL platform.
  427. if (target.IsDLLPlatform()) {
  428. // When in namelink only mode skip all libraries on Windows.
  429. if (namelinkMode == cmInstallTargetGenerator::NamelinkModeOnly) {
  430. continue;
  431. }
  432. // This is a DLL platform.
  433. if (!archiveArgs.GetDestination().empty()) {
  434. // The import library uses the ARCHIVE properties.
  435. archiveGenerator = CreateInstallTargetGenerator(
  436. target, archiveArgs, true, helper.Makefile->GetBacktrace());
  437. }
  438. if (!runtimeArgs.GetDestination().empty()) {
  439. // The DLL uses the RUNTIME properties.
  440. runtimeGenerator = CreateInstallTargetGenerator(
  441. target, runtimeArgs, false, helper.Makefile->GetBacktrace());
  442. }
  443. if (!archiveGenerator && !runtimeGenerator) {
  444. archiveGenerator = CreateInstallTargetGenerator(
  445. target, archiveArgs, true, helper.Makefile->GetBacktrace(),
  446. helper.GetArchiveDestination(nullptr));
  447. runtimeGenerator = CreateInstallTargetGenerator(
  448. target, runtimeArgs, false, helper.Makefile->GetBacktrace(),
  449. helper.GetRuntimeDestination(nullptr));
  450. }
  451. } else {
  452. // This is a non-DLL platform.
  453. // If it is marked with FRAMEWORK property use the FRAMEWORK set of
  454. // INSTALL properties. Otherwise, use the LIBRARY properties.
  455. if (target.IsFrameworkOnApple()) {
  456. // When in namelink only mode skip frameworks.
  457. if (namelinkMode == cmInstallTargetGenerator::NamelinkModeOnly) {
  458. continue;
  459. }
  460. // Use the FRAMEWORK properties.
  461. if (!frameworkArgs.GetDestination().empty()) {
  462. frameworkGenerator = CreateInstallTargetGenerator(
  463. target, frameworkArgs, false, helper.Makefile->GetBacktrace());
  464. } else {
  465. status.SetError(
  466. cmStrCat("TARGETS given no FRAMEWORK DESTINATION for shared "
  467. "library FRAMEWORK target \"",
  468. target.GetName(), "\"."));
  469. return false;
  470. }
  471. } else {
  472. // The shared library uses the LIBRARY properties.
  473. if (namelinkMode != cmInstallTargetGenerator::NamelinkModeOnly) {
  474. libraryGenerator = CreateInstallTargetGenerator(
  475. target, libraryArgs, false, helper.Makefile->GetBacktrace(),
  476. helper.GetLibraryDestination(&libraryArgs));
  477. libraryGenerator->SetNamelinkMode(
  478. cmInstallTargetGenerator::NamelinkModeSkip);
  479. }
  480. if (namelinkMode != cmInstallTargetGenerator::NamelinkModeSkip) {
  481. namelinkGenerator = CreateInstallTargetGenerator(
  482. target, libraryArgs, false, helper.Makefile->GetBacktrace(),
  483. helper.GetLibraryDestination(&libraryArgs), false, true);
  484. namelinkGenerator->SetNamelinkMode(
  485. cmInstallTargetGenerator::NamelinkModeOnly);
  486. }
  487. namelinkOnly =
  488. (namelinkMode == cmInstallTargetGenerator::NamelinkModeOnly);
  489. }
  490. }
  491. } break;
  492. case cmStateEnums::STATIC_LIBRARY: {
  493. // If it is marked with FRAMEWORK property use the FRAMEWORK set of
  494. // INSTALL properties. Otherwise, use the LIBRARY properties.
  495. if (target.IsFrameworkOnApple()) {
  496. // When in namelink only mode skip frameworks.
  497. if (namelinkMode == cmInstallTargetGenerator::NamelinkModeOnly) {
  498. continue;
  499. }
  500. // Use the FRAMEWORK properties.
  501. if (!frameworkArgs.GetDestination().empty()) {
  502. frameworkGenerator = CreateInstallTargetGenerator(
  503. target, frameworkArgs, false, helper.Makefile->GetBacktrace());
  504. } else {
  505. status.SetError(
  506. cmStrCat("TARGETS given no FRAMEWORK DESTINATION for static "
  507. "library FRAMEWORK target \"",
  508. target.GetName(), "\"."));
  509. return false;
  510. }
  511. } else {
  512. // Static libraries use ARCHIVE properties.
  513. archiveGenerator = CreateInstallTargetGenerator(
  514. target, archiveArgs, false, helper.Makefile->GetBacktrace(),
  515. helper.GetArchiveDestination(&archiveArgs));
  516. }
  517. } break;
  518. case cmStateEnums::MODULE_LIBRARY: {
  519. // Modules use LIBRARY properties.
  520. if (!libraryArgs.GetDestination().empty()) {
  521. libraryGenerator = CreateInstallTargetGenerator(
  522. target, libraryArgs, false, helper.Makefile->GetBacktrace());
  523. libraryGenerator->SetNamelinkMode(namelinkMode);
  524. namelinkOnly =
  525. (namelinkMode == cmInstallTargetGenerator::NamelinkModeOnly);
  526. } else {
  527. status.SetError(
  528. cmStrCat("TARGETS given no LIBRARY DESTINATION for module "
  529. "target \"",
  530. target.GetName(), "\"."));
  531. return false;
  532. }
  533. } break;
  534. case cmStateEnums::OBJECT_LIBRARY: {
  535. // Objects use OBJECT properties.
  536. if (!objectArgs.GetDestination().empty()) {
  537. // Verify that we know where the objects are to install them.
  538. std::string reason;
  539. if (!helper.Makefile->GetGlobalGenerator()
  540. ->HasKnownObjectFileLocation(&reason)) {
  541. status.SetError(
  542. cmStrCat("TARGETS given OBJECT library \"", target.GetName(),
  543. "\" whose objects may not be installed", reason, "."));
  544. return false;
  545. }
  546. objectGenerator = CreateInstallTargetGenerator(
  547. target, objectArgs, false, helper.Makefile->GetBacktrace());
  548. } else {
  549. // Installing an OBJECT library without a destination transforms
  550. // it to an INTERFACE library. It installs no files but can be
  551. // exported.
  552. }
  553. } break;
  554. case cmStateEnums::EXECUTABLE: {
  555. if (target.IsAppBundleOnApple()) {
  556. // Application bundles use the BUNDLE properties.
  557. if (!bundleArgs.GetDestination().empty()) {
  558. bundleGenerator = CreateInstallTargetGenerator(
  559. target, bundleArgs, false, helper.Makefile->GetBacktrace());
  560. } else if (!runtimeArgs.GetDestination().empty()) {
  561. bool failure = false;
  562. if (helper.CheckCMP0006(failure)) {
  563. // For CMake 2.4 compatibility fallback to the RUNTIME
  564. // properties.
  565. bundleGenerator = CreateInstallTargetGenerator(
  566. target, runtimeArgs, false, helper.Makefile->GetBacktrace());
  567. } else if (failure) {
  568. return false;
  569. }
  570. }
  571. if (!bundleGenerator) {
  572. status.SetError(cmStrCat("TARGETS given no BUNDLE DESTINATION for "
  573. "MACOSX_BUNDLE executable target \"",
  574. target.GetName(), "\"."));
  575. return false;
  576. }
  577. } else {
  578. // Executables use the RUNTIME properties.
  579. runtimeGenerator = CreateInstallTargetGenerator(
  580. target, runtimeArgs, false, helper.Makefile->GetBacktrace(),
  581. helper.GetRuntimeDestination(&runtimeArgs));
  582. }
  583. // On DLL platforms an executable may also have an import
  584. // library. Install it to the archive destination if it
  585. // exists.
  586. if ((target.IsDLLPlatform() || target.IsAIX()) &&
  587. !archiveArgs.GetDestination().empty() &&
  588. target.IsExecutableWithExports()) {
  589. // The import library uses the ARCHIVE properties.
  590. archiveGenerator = CreateInstallTargetGenerator(
  591. target, archiveArgs, true, helper.Makefile->GetBacktrace(), true);
  592. }
  593. } break;
  594. case cmStateEnums::INTERFACE_LIBRARY:
  595. // Nothing to do. An INTERFACE_LIBRARY can be installed, but the
  596. // only effect of that is to make it exportable. It installs no
  597. // other files itself.
  598. break;
  599. default:
  600. // This should never happen due to the above type check.
  601. // Ignore the case.
  602. break;
  603. }
  604. // These well-known sets of files are installed *automatically* for
  605. // FRAMEWORK SHARED library targets on the Mac as part of installing the
  606. // FRAMEWORK. For other target types or on other platforms, they are not
  607. // installed automatically and so we need to create install files
  608. // generators for them.
  609. bool createInstallGeneratorsForTargetFileSets = true;
  610. if (target.IsFrameworkOnApple()) {
  611. createInstallGeneratorsForTargetFileSets = false;
  612. }
  613. if (createInstallGeneratorsForTargetFileSets && !namelinkOnly) {
  614. const char* files = target.GetProperty("PRIVATE_HEADER");
  615. if ((files) && (*files)) {
  616. std::vector<std::string> relFiles = cmExpandedList(files);
  617. std::vector<std::string> absFiles;
  618. if (!helper.MakeFilesFullPath("PRIVATE_HEADER", relFiles, absFiles)) {
  619. return false;
  620. }
  621. // Create the files install generator.
  622. privateHeaderGenerator = CreateInstallFilesGenerator(
  623. helper.Makefile, absFiles, privateHeaderArgs, false,
  624. helper.GetIncludeDestination(&privateHeaderArgs));
  625. }
  626. files = target.GetProperty("PUBLIC_HEADER");
  627. if ((files) && (*files)) {
  628. std::vector<std::string> relFiles = cmExpandedList(files);
  629. std::vector<std::string> absFiles;
  630. if (!helper.MakeFilesFullPath("PUBLIC_HEADER", relFiles, absFiles)) {
  631. return false;
  632. }
  633. // Create the files install generator.
  634. publicHeaderGenerator = CreateInstallFilesGenerator(
  635. helper.Makefile, absFiles, publicHeaderArgs, false,
  636. helper.GetIncludeDestination(&publicHeaderArgs));
  637. }
  638. files = target.GetProperty("RESOURCE");
  639. if ((files) && (*files)) {
  640. std::vector<std::string> relFiles = cmExpandedList(files);
  641. std::vector<std::string> absFiles;
  642. if (!helper.MakeFilesFullPath("RESOURCE", relFiles, absFiles)) {
  643. return false;
  644. }
  645. // Create the files install generator.
  646. if (!resourceArgs.GetDestination().empty()) {
  647. resourceGenerator = CreateInstallFilesGenerator(
  648. helper.Makefile, absFiles, resourceArgs, false);
  649. } else {
  650. cmSystemTools::Message(
  651. cmStrCat("INSTALL TARGETS - target ", target.GetName(),
  652. " has RESOURCE files but no RESOURCE DESTINATION."),
  653. "Warning");
  654. }
  655. }
  656. }
  657. // Add this install rule to an export if one was specified and
  658. // this is not a namelink-only rule.
  659. if (!exports.empty() && !namelinkOnly) {
  660. auto te = cm::make_unique<cmTargetExport>();
  661. te->TargetName = target.GetName();
  662. te->ArchiveGenerator = archiveGenerator.get();
  663. te->BundleGenerator = bundleGenerator.get();
  664. te->FrameworkGenerator = frameworkGenerator.get();
  665. te->HeaderGenerator = publicHeaderGenerator.get();
  666. te->LibraryGenerator = libraryGenerator.get();
  667. te->RuntimeGenerator = runtimeGenerator.get();
  668. te->ObjectsGenerator = objectGenerator.get();
  669. te->InterfaceIncludeDirectories =
  670. cmJoin(includesArgs.GetIncludeDirs(), ";");
  671. helper.Makefile->GetGlobalGenerator()
  672. ->GetExportSets()[exports]
  673. .AddTargetExport(std::move(te));
  674. }
  675. // Keep track of whether we're installing anything in each category
  676. installsArchive = installsArchive || archiveGenerator;
  677. installsLibrary = installsLibrary || libraryGenerator;
  678. installsNamelink = installsNamelink || namelinkGenerator;
  679. installsRuntime = installsRuntime || runtimeGenerator;
  680. installsObject = installsObject || objectGenerator;
  681. installsFramework = installsFramework || frameworkGenerator;
  682. installsBundle = installsBundle || bundleGenerator;
  683. installsPrivateHeader = installsPrivateHeader || privateHeaderGenerator;
  684. installsPublicHeader = installsPublicHeader || publicHeaderGenerator;
  685. installsResource = installsResource || resourceGenerator;
  686. helper.Makefile->AddInstallGenerator(std::move(archiveGenerator));
  687. helper.Makefile->AddInstallGenerator(std::move(libraryGenerator));
  688. helper.Makefile->AddInstallGenerator(std::move(namelinkGenerator));
  689. helper.Makefile->AddInstallGenerator(std::move(runtimeGenerator));
  690. helper.Makefile->AddInstallGenerator(std::move(objectGenerator));
  691. helper.Makefile->AddInstallGenerator(std::move(frameworkGenerator));
  692. helper.Makefile->AddInstallGenerator(std::move(bundleGenerator));
  693. helper.Makefile->AddInstallGenerator(std::move(privateHeaderGenerator));
  694. helper.Makefile->AddInstallGenerator(std::move(publicHeaderGenerator));
  695. helper.Makefile->AddInstallGenerator(std::move(resourceGenerator));
  696. }
  697. // Tell the global generator about any installation component names
  698. // specified
  699. if (installsArchive) {
  700. helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
  701. archiveArgs.GetComponent());
  702. }
  703. if (installsLibrary) {
  704. helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
  705. libraryArgs.GetComponent());
  706. }
  707. if (installsNamelink) {
  708. helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
  709. libraryArgs.GetNamelinkComponent());
  710. }
  711. if (installsRuntime) {
  712. helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
  713. runtimeArgs.GetComponent());
  714. }
  715. if (installsObject) {
  716. helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
  717. objectArgs.GetComponent());
  718. }
  719. if (installsFramework) {
  720. helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
  721. frameworkArgs.GetComponent());
  722. }
  723. if (installsBundle) {
  724. helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
  725. bundleArgs.GetComponent());
  726. }
  727. if (installsPrivateHeader) {
  728. helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
  729. privateHeaderArgs.GetComponent());
  730. }
  731. if (installsPublicHeader) {
  732. helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
  733. publicHeaderArgs.GetComponent());
  734. }
  735. if (installsResource) {
  736. helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
  737. resourceArgs.GetComponent());
  738. }
  739. return true;
  740. }
  741. bool HandleFilesMode(std::vector<std::string> const& args,
  742. cmExecutionStatus& status)
  743. {
  744. Helper helper(status);
  745. // This is the FILES mode.
  746. bool programs = (args[0] == "PROGRAMS");
  747. cmInstallCommandArguments ica(helper.DefaultComponentName);
  748. std::vector<std::string> files;
  749. ica.Bind(programs ? "PROGRAMS"_s : "FILES"_s, files);
  750. std::vector<std::string> unknownArgs;
  751. ica.Parse(args, &unknownArgs);
  752. if (!unknownArgs.empty()) {
  753. // Unknown argument.
  754. status.SetError(
  755. cmStrCat(args[0], " given unknown argument \"", unknownArgs[0], "\"."));
  756. return false;
  757. }
  758. std::string type = ica.GetType();
  759. if (!type.empty() && allowedTypes.count(type) == 0) {
  760. status.SetError(
  761. cmStrCat(args[0], " given non-type \"", type, "\" with TYPE argument."));
  762. return false;
  763. }
  764. const std::vector<std::string>& filesVector = files;
  765. // Check if there is something to do.
  766. if (filesVector.empty()) {
  767. return true;
  768. }
  769. if (!ica.GetRename().empty() && filesVector.size() > 1) {
  770. // The rename option works only with one file.
  771. status.SetError(
  772. cmStrCat(args[0], " given RENAME option with more than one file."));
  773. return false;
  774. }
  775. std::vector<std::string> absFiles;
  776. if (!helper.MakeFilesFullPath(args[0].c_str(), filesVector, absFiles)) {
  777. return false;
  778. }
  779. cmPolicies::PolicyStatus policyStatus =
  780. helper.Makefile->GetPolicyStatus(cmPolicies::CMP0062);
  781. cmGlobalGenerator* gg = helper.Makefile->GetGlobalGenerator();
  782. for (std::string const& file : filesVector) {
  783. if (gg->IsExportedTargetsFile(file)) {
  784. const char* modal = nullptr;
  785. std::ostringstream e;
  786. MessageType messageType = MessageType::AUTHOR_WARNING;
  787. switch (policyStatus) {
  788. case cmPolicies::WARN:
  789. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0062) << "\n";
  790. modal = "should";
  791. case cmPolicies::OLD:
  792. break;
  793. case cmPolicies::REQUIRED_IF_USED:
  794. case cmPolicies::REQUIRED_ALWAYS:
  795. case cmPolicies::NEW:
  796. modal = "may";
  797. messageType = MessageType::FATAL_ERROR;
  798. }
  799. if (modal) {
  800. e << "The file\n " << file
  801. << "\nwas generated by the export() "
  802. "command. It "
  803. << modal
  804. << " not be installed with the "
  805. "install() command. Use the install(EXPORT) mechanism "
  806. "instead. See the cmake-packages(7) manual for more.\n";
  807. helper.Makefile->IssueMessage(messageType, e.str());
  808. if (messageType == MessageType::FATAL_ERROR) {
  809. return false;
  810. }
  811. }
  812. }
  813. }
  814. if (!ica.Finalize()) {
  815. return false;
  816. }
  817. if (!type.empty() && !ica.GetDestination().empty()) {
  818. status.SetError(cmStrCat(args[0],
  819. " given both TYPE and DESTINATION arguments. "
  820. "You may only specify one."));
  821. return false;
  822. }
  823. std::string destination = helper.GetDestinationForType(&ica, type);
  824. if (destination.empty()) {
  825. // A destination is required.
  826. status.SetError(cmStrCat(args[0], " given no DESTINATION!"));
  827. return false;
  828. }
  829. // Create the files install generator.
  830. helper.Makefile->AddInstallGenerator(CreateInstallFilesGenerator(
  831. helper.Makefile, absFiles, ica, programs, destination));
  832. // Tell the global generator about any installation component names
  833. // specified.
  834. helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
  835. ica.GetComponent());
  836. return true;
  837. }
  838. bool HandleDirectoryMode(std::vector<std::string> const& args,
  839. cmExecutionStatus& status)
  840. {
  841. Helper helper(status);
  842. enum Doing
  843. {
  844. DoingNone,
  845. DoingDirs,
  846. DoingDestination,
  847. DoingPattern,
  848. DoingRegex,
  849. DoingPermsFile,
  850. DoingPermsDir,
  851. DoingPermsMatch,
  852. DoingConfigurations,
  853. DoingComponent,
  854. DoingType
  855. };
  856. Doing doing = DoingDirs;
  857. bool in_match_mode = false;
  858. bool optional = false;
  859. bool exclude_from_all = false;
  860. bool message_never = false;
  861. std::vector<std::string> dirs;
  862. const char* destination = nullptr;
  863. std::string permissions_file;
  864. std::string permissions_dir;
  865. std::vector<std::string> configurations;
  866. std::string component = helper.DefaultComponentName;
  867. std::string literal_args;
  868. std::string type;
  869. for (unsigned int i = 1; i < args.size(); ++i) {
  870. if (args[i] == "DESTINATION") {
  871. if (in_match_mode) {
  872. status.SetError(cmStrCat(args[0], " does not allow \"", args[i],
  873. "\" after PATTERN or REGEX."));
  874. return false;
  875. }
  876. // Switch to setting the destination property.
  877. doing = DoingDestination;
  878. } else if (args[i] == "TYPE") {
  879. if (in_match_mode) {
  880. status.SetError(cmStrCat(args[0], " does not allow \"", args[i],
  881. "\" after PATTERN or REGEX."));
  882. return false;
  883. }
  884. // Switch to setting the type.
  885. doing = DoingType;
  886. } else if (args[i] == "OPTIONAL") {
  887. if (in_match_mode) {
  888. status.SetError(cmStrCat(args[0], " does not allow \"", args[i],
  889. "\" after PATTERN or REGEX."));
  890. return false;
  891. }
  892. // Mark the rule as optional.
  893. optional = true;
  894. doing = DoingNone;
  895. } else if (args[i] == "MESSAGE_NEVER") {
  896. if (in_match_mode) {
  897. status.SetError(cmStrCat(args[0], " does not allow \"", args[i],
  898. "\" after PATTERN or REGEX."));
  899. return false;
  900. }
  901. // Mark the rule as quiet.
  902. message_never = true;
  903. doing = DoingNone;
  904. } else if (args[i] == "PATTERN") {
  905. // Switch to a new pattern match rule.
  906. doing = DoingPattern;
  907. in_match_mode = true;
  908. } else if (args[i] == "REGEX") {
  909. // Switch to a new regex match rule.
  910. doing = DoingRegex;
  911. in_match_mode = true;
  912. } else if (args[i] == "EXCLUDE") {
  913. // Add this property to the current match rule.
  914. if (!in_match_mode || doing == DoingPattern || doing == DoingRegex) {
  915. status.SetError(cmStrCat(args[0], " does not allow \"", args[i],
  916. "\" before a PATTERN or REGEX is given."));
  917. return false;
  918. }
  919. literal_args += " EXCLUDE";
  920. doing = DoingNone;
  921. } else if (args[i] == "PERMISSIONS") {
  922. if (!in_match_mode) {
  923. status.SetError(cmStrCat(args[0], " does not allow \"", args[i],
  924. "\" before a PATTERN or REGEX is given."));
  925. return false;
  926. }
  927. // Switch to setting the current match permissions property.
  928. literal_args += " PERMISSIONS";
  929. doing = DoingPermsMatch;
  930. } else if (args[i] == "FILE_PERMISSIONS") {
  931. if (in_match_mode) {
  932. status.SetError(cmStrCat(args[0], " does not allow \"", args[i],
  933. "\" after PATTERN or REGEX."));
  934. return false;
  935. }
  936. // Switch to setting the file permissions property.
  937. doing = DoingPermsFile;
  938. } else if (args[i] == "DIRECTORY_PERMISSIONS") {
  939. if (in_match_mode) {
  940. status.SetError(cmStrCat(args[0], " does not allow \"", args[i],
  941. "\" after PATTERN or REGEX."));
  942. return false;
  943. }
  944. // Switch to setting the directory permissions property.
  945. doing = DoingPermsDir;
  946. } else if (args[i] == "USE_SOURCE_PERMISSIONS") {
  947. if (in_match_mode) {
  948. status.SetError(cmStrCat(args[0], " does not allow \"", args[i],
  949. "\" after PATTERN or REGEX."));
  950. return false;
  951. }
  952. // Add this option literally.
  953. literal_args += " USE_SOURCE_PERMISSIONS";
  954. doing = DoingNone;
  955. } else if (args[i] == "FILES_MATCHING") {
  956. if (in_match_mode) {
  957. status.SetError(cmStrCat(args[0], " does not allow \"", args[i],
  958. "\" after PATTERN or REGEX."));
  959. return false;
  960. }
  961. // Add this option literally.
  962. literal_args += " FILES_MATCHING";
  963. doing = DoingNone;
  964. } else if (args[i] == "CONFIGURATIONS") {
  965. if (in_match_mode) {
  966. status.SetError(cmStrCat(args[0], " does not allow \"", args[i],
  967. "\" after PATTERN or REGEX."));
  968. return false;
  969. }
  970. // Switch to setting the configurations property.
  971. doing = DoingConfigurations;
  972. } else if (args[i] == "COMPONENT") {
  973. if (in_match_mode) {
  974. status.SetError(cmStrCat(args[0], " does not allow \"", args[i],
  975. "\" after PATTERN or REGEX."));
  976. return false;
  977. }
  978. // Switch to setting the component property.
  979. doing = DoingComponent;
  980. } else if (args[i] == "EXCLUDE_FROM_ALL") {
  981. if (in_match_mode) {
  982. status.SetError(cmStrCat(args[0], " does not allow \"", args[i],
  983. "\" after PATTERN or REGEX."));
  984. return false;
  985. }
  986. exclude_from_all = true;
  987. doing = DoingNone;
  988. } else if (doing == DoingDirs) {
  989. // Convert this directory to a full path.
  990. std::string dir = args[i];
  991. std::string::size_type gpos = cmGeneratorExpression::Find(dir);
  992. if (gpos != 0 && !cmSystemTools::FileIsFullPath(dir)) {
  993. dir =
  994. cmStrCat(helper.Makefile->GetCurrentSourceDirectory(), '/', args[i]);
  995. }
  996. // Make sure the name is a directory.
  997. if (cmSystemTools::FileExists(dir) &&
  998. !cmSystemTools::FileIsDirectory(dir)) {
  999. status.SetError(cmStrCat(args[0], " given non-directory \"", args[i],
  1000. "\" to install."));
  1001. return false;
  1002. }
  1003. // Store the directory for installation.
  1004. dirs.push_back(std::move(dir));
  1005. } else if (doing == DoingConfigurations) {
  1006. configurations.push_back(args[i]);
  1007. } else if (doing == DoingDestination) {
  1008. destination = args[i].c_str();
  1009. doing = DoingNone;
  1010. } else if (doing == DoingType) {
  1011. if (allowedTypes.count(args[i]) == 0) {
  1012. status.SetError(cmStrCat(args[0], " given non-type \"", args[i],
  1013. "\" with TYPE argument."));
  1014. return false;
  1015. }
  1016. type = args[i];
  1017. doing = DoingNone;
  1018. } else if (doing == DoingPattern) {
  1019. // Convert the pattern to a regular expression. Require a
  1020. // leading slash and trailing end-of-string in the matched
  1021. // string to make sure the pattern matches only whole file
  1022. // names.
  1023. literal_args += " REGEX \"/";
  1024. std::string regex = cmsys::Glob::PatternToRegex(args[i], false);
  1025. cmSystemTools::ReplaceString(regex, "\\", "\\\\");
  1026. literal_args += regex;
  1027. literal_args += "$\"";
  1028. doing = DoingNone;
  1029. } else if (doing == DoingRegex) {
  1030. literal_args += " REGEX \"";
  1031. // Match rules are case-insensitive on some platforms.
  1032. #if defined(_WIN32) || defined(__APPLE__) || defined(__CYGWIN__)
  1033. std::string regex = cmSystemTools::LowerCase(args[i]);
  1034. #else
  1035. std::string regex = args[i];
  1036. #endif
  1037. cmSystemTools::ReplaceString(regex, "\\", "\\\\");
  1038. literal_args += regex;
  1039. literal_args += "\"";
  1040. doing = DoingNone;
  1041. } else if (doing == DoingComponent) {
  1042. component = args[i];
  1043. doing = DoingNone;
  1044. } else if (doing == DoingPermsFile) {
  1045. // Check the requested permission.
  1046. if (!cmInstallCommandArguments::CheckPermissions(args[i],
  1047. permissions_file)) {
  1048. status.SetError(cmStrCat(args[0], " given invalid file permission \"",
  1049. args[i], "\"."));
  1050. return false;
  1051. }
  1052. } else if (doing == DoingPermsDir) {
  1053. // Check the requested permission.
  1054. if (!cmInstallCommandArguments::CheckPermissions(args[i],
  1055. permissions_dir)) {
  1056. status.SetError(cmStrCat(
  1057. args[0], " given invalid directory permission \"", args[i], "\"."));
  1058. return false;
  1059. }
  1060. } else if (doing == DoingPermsMatch) {
  1061. // Check the requested permission.
  1062. if (!cmInstallCommandArguments::CheckPermissions(args[i],
  1063. literal_args)) {
  1064. status.SetError(
  1065. cmStrCat(args[0], " given invalid permission \"", args[i], "\"."));
  1066. return false;
  1067. }
  1068. } else {
  1069. // Unknown argument.
  1070. status.SetError(
  1071. cmStrCat(args[0], " given unknown argument \"", args[i], "\"."));
  1072. return false;
  1073. }
  1074. }
  1075. // Support installing an empty directory.
  1076. if (dirs.empty() && destination) {
  1077. dirs.emplace_back();
  1078. }
  1079. // Check if there is something to do.
  1080. if (dirs.empty()) {
  1081. return true;
  1082. }
  1083. std::string destinationStr;
  1084. if (!destination) {
  1085. if (type.empty()) {
  1086. // A destination is required.
  1087. status.SetError(cmStrCat(args[0], " given no DESTINATION!"));
  1088. return false;
  1089. }
  1090. destinationStr = helper.GetDestinationForType(nullptr, type);
  1091. destination = destinationStr.c_str();
  1092. } else if (!type.empty()) {
  1093. status.SetError(cmStrCat(args[0],
  1094. " given both TYPE and DESTINATION "
  1095. "arguments. You may only specify one."));
  1096. return false;
  1097. }
  1098. cmInstallGenerator::MessageLevel message =
  1099. cmInstallGenerator::SelectMessageLevel(helper.Makefile, message_never);
  1100. // Create the directory install generator.
  1101. helper.Makefile->AddInstallGenerator(
  1102. cm::make_unique<cmInstallDirectoryGenerator>(
  1103. dirs, destination, permissions_file, permissions_dir, configurations,
  1104. component, message, exclude_from_all, literal_args, optional));
  1105. // Tell the global generator about any installation component names
  1106. // specified.
  1107. helper.Makefile->GetGlobalGenerator()->AddInstallComponent(component);
  1108. return true;
  1109. }
  1110. bool HandleExportAndroidMKMode(std::vector<std::string> const& args,
  1111. cmExecutionStatus& status)
  1112. {
  1113. #ifndef CMAKE_BOOTSTRAP
  1114. Helper helper(status);
  1115. // This is the EXPORT mode.
  1116. cmInstallCommandArguments ica(helper.DefaultComponentName);
  1117. std::string exp;
  1118. std::string name_space;
  1119. bool exportOld = false;
  1120. std::string filename;
  1121. ica.Bind("EXPORT_ANDROID_MK"_s, exp);
  1122. ica.Bind("NAMESPACE"_s, name_space);
  1123. ica.Bind("EXPORT_LINK_INTERFACE_LIBRARIES"_s, exportOld);
  1124. ica.Bind("FILE"_s, filename);
  1125. std::vector<std::string> unknownArgs;
  1126. ica.Parse(args, &unknownArgs);
  1127. if (!unknownArgs.empty()) {
  1128. // Unknown argument.
  1129. status.SetError(
  1130. cmStrCat(args[0], " given unknown argument \"", unknownArgs[0], "\"."));
  1131. return false;
  1132. }
  1133. if (!ica.Finalize()) {
  1134. return false;
  1135. }
  1136. // Make sure there is a destination.
  1137. if (ica.GetDestination().empty()) {
  1138. // A destination is required.
  1139. status.SetError(cmStrCat(args[0], " given no DESTINATION!"));
  1140. return false;
  1141. }
  1142. // Check the file name.
  1143. std::string fname = filename;
  1144. if (fname.find_first_of(":/\\") != std::string::npos) {
  1145. status.SetError(cmStrCat(args[0], " given invalid export file name \"",
  1146. fname,
  1147. "\". The FILE argument may not contain a path. "
  1148. "Specify the path in the DESTINATION argument."));
  1149. return false;
  1150. }
  1151. // Check the file extension.
  1152. if (!fname.empty() &&
  1153. cmSystemTools::GetFilenameLastExtension(fname) != ".mk") {
  1154. status.SetError(cmStrCat(
  1155. args[0], " given invalid export file name \"", fname,
  1156. R"(". The FILE argument must specify a name ending in ".mk".)"));
  1157. return false;
  1158. }
  1159. if (fname.find_first_of(":/\\") != std::string::npos) {
  1160. status.SetError(
  1161. cmStrCat(args[0], " given export name \"", exp,
  1162. "\". "
  1163. "This name cannot be safely converted to a file name. "
  1164. "Specify a different export name or use the FILE option to set "
  1165. "a file name explicitly."));
  1166. return false;
  1167. }
  1168. // Use the default name
  1169. if (fname.empty()) {
  1170. fname = "Android.mk";
  1171. }
  1172. cmExportSet& exportSet =
  1173. helper.Makefile->GetGlobalGenerator()->GetExportSets()[exp];
  1174. cmInstallGenerator::MessageLevel message =
  1175. cmInstallGenerator::SelectMessageLevel(helper.Makefile);
  1176. // Create the export install generator.
  1177. helper.Makefile->AddInstallGenerator(
  1178. cm::make_unique<cmInstallExportGenerator>(
  1179. &exportSet, ica.GetDestination(), ica.GetPermissions(),
  1180. ica.GetConfigurations(), ica.GetComponent(), message,
  1181. ica.GetExcludeFromAll(), fname, name_space, exportOld, true));
  1182. return true;
  1183. #else
  1184. static_cast<void>(args);
  1185. status.SetError("EXPORT_ANDROID_MK not supported in bootstrap cmake");
  1186. return false;
  1187. #endif
  1188. }
  1189. bool HandleExportMode(std::vector<std::string> const& args,
  1190. cmExecutionStatus& status)
  1191. {
  1192. Helper helper(status);
  1193. // This is the EXPORT mode.
  1194. cmInstallCommandArguments ica(helper.DefaultComponentName);
  1195. std::string exp;
  1196. std::string name_space;
  1197. bool exportOld = false;
  1198. std::string filename;
  1199. ica.Bind("EXPORT"_s, exp);
  1200. ica.Bind("NAMESPACE"_s, name_space);
  1201. ica.Bind("EXPORT_LINK_INTERFACE_LIBRARIES"_s, exportOld);
  1202. ica.Bind("FILE"_s, filename);
  1203. std::vector<std::string> unknownArgs;
  1204. ica.Parse(args, &unknownArgs);
  1205. if (!unknownArgs.empty()) {
  1206. // Unknown argument.
  1207. status.SetError(
  1208. cmStrCat(args[0], " given unknown argument \"", unknownArgs[0], "\"."));
  1209. return false;
  1210. }
  1211. if (!ica.Finalize()) {
  1212. return false;
  1213. }
  1214. // Make sure there is a destination.
  1215. if (ica.GetDestination().empty()) {
  1216. // A destination is required.
  1217. status.SetError(cmStrCat(args[0], " given no DESTINATION!"));
  1218. return false;
  1219. }
  1220. // Check the file name.
  1221. std::string fname = filename;
  1222. if (fname.find_first_of(":/\\") != std::string::npos) {
  1223. status.SetError(cmStrCat(args[0], " given invalid export file name \"",
  1224. fname,
  1225. "\". "
  1226. "The FILE argument may not contain a path. "
  1227. "Specify the path in the DESTINATION argument."));
  1228. return false;
  1229. }
  1230. // Check the file extension.
  1231. if (!fname.empty() &&
  1232. cmSystemTools::GetFilenameLastExtension(fname) != ".cmake") {
  1233. status.SetError(
  1234. cmStrCat(args[0], " given invalid export file name \"", fname,
  1235. "\". "
  1236. "The FILE argument must specify a name ending in \".cmake\"."));
  1237. return false;
  1238. }
  1239. // Construct the file name.
  1240. if (fname.empty()) {
  1241. fname = cmStrCat(exp, ".cmake");
  1242. if (fname.find_first_of(":/\\") != std::string::npos) {
  1243. status.SetError(cmStrCat(
  1244. args[0], " given export name \"", exp,
  1245. "\". "
  1246. "This name cannot be safely converted to a file name. "
  1247. "Specify a different export name or use the FILE option to set "
  1248. "a file name explicitly."));
  1249. return false;
  1250. }
  1251. }
  1252. cmExportSet& exportSet =
  1253. helper.Makefile->GetGlobalGenerator()->GetExportSets()[exp];
  1254. if (exportOld) {
  1255. for (auto const& te : exportSet.GetTargetExports()) {
  1256. cmTarget* tgt =
  1257. helper.Makefile->GetGlobalGenerator()->FindTarget(te->TargetName);
  1258. const bool newCMP0022Behavior =
  1259. (tgt && tgt->GetPolicyStatusCMP0022() != cmPolicies::WARN &&
  1260. tgt->GetPolicyStatusCMP0022() != cmPolicies::OLD);
  1261. if (!newCMP0022Behavior) {
  1262. status.SetError(cmStrCat(
  1263. "INSTALL(EXPORT) given keyword \""
  1264. "EXPORT_LINK_INTERFACE_LIBRARIES\", but target \"",
  1265. te->TargetName, "\" does not have policy CMP0022 set to NEW."));
  1266. return false;
  1267. }
  1268. }
  1269. }
  1270. cmInstallGenerator::MessageLevel message =
  1271. cmInstallGenerator::SelectMessageLevel(helper.Makefile);
  1272. // Create the export install generator.
  1273. helper.Makefile->AddInstallGenerator(
  1274. cm::make_unique<cmInstallExportGenerator>(
  1275. &exportSet, ica.GetDestination(), ica.GetPermissions(),
  1276. ica.GetConfigurations(), ica.GetComponent(), message,
  1277. ica.GetExcludeFromAll(), fname, name_space, exportOld, false));
  1278. return true;
  1279. }
  1280. bool Helper::MakeFilesFullPath(const char* modeName,
  1281. const std::vector<std::string>& relFiles,
  1282. std::vector<std::string>& absFiles)
  1283. {
  1284. for (std::string const& relFile : relFiles) {
  1285. std::string file = relFile;
  1286. std::string::size_type gpos = cmGeneratorExpression::Find(file);
  1287. if (gpos != 0 && !cmSystemTools::FileIsFullPath(file)) {
  1288. file =
  1289. cmStrCat(this->Makefile->GetCurrentSourceDirectory(), '/', relFile);
  1290. }
  1291. // Make sure the file is not a directory.
  1292. if (gpos == std::string::npos && cmSystemTools::FileIsDirectory(file)) {
  1293. this->SetError(
  1294. cmStrCat(modeName, " given directory \"", relFile, "\" to install."));
  1295. return false;
  1296. }
  1297. // Store the file for installation.
  1298. absFiles.push_back(std::move(file));
  1299. }
  1300. return true;
  1301. }
  1302. bool Helper::CheckCMP0006(bool& failure)
  1303. {
  1304. switch (this->Makefile->GetPolicyStatus(cmPolicies::CMP0006)) {
  1305. case cmPolicies::WARN:
  1306. this->Makefile->IssueMessage(
  1307. MessageType::AUTHOR_WARNING,
  1308. cmPolicies::GetPolicyWarning(cmPolicies::CMP0006));
  1309. CM_FALLTHROUGH;
  1310. case cmPolicies::OLD:
  1311. // OLD behavior is to allow compatibility
  1312. return true;
  1313. case cmPolicies::NEW:
  1314. // NEW behavior is to disallow compatibility
  1315. break;
  1316. case cmPolicies::REQUIRED_IF_USED:
  1317. case cmPolicies::REQUIRED_ALWAYS:
  1318. failure = true;
  1319. this->Makefile->IssueMessage(
  1320. MessageType::FATAL_ERROR,
  1321. cmPolicies::GetRequiredPolicyError(cmPolicies::CMP0006));
  1322. break;
  1323. }
  1324. return false;
  1325. }
  1326. std::string Helper::GetDestination(const cmInstallCommandArguments* args,
  1327. const std::string& varName,
  1328. const std::string& guess)
  1329. {
  1330. if (args && !args->GetDestination().empty()) {
  1331. return args->GetDestination();
  1332. }
  1333. std::string val = this->Makefile->GetSafeDefinition(varName);
  1334. if (!val.empty()) {
  1335. return val;
  1336. }
  1337. return guess;
  1338. }
  1339. std::string Helper::GetRuntimeDestination(
  1340. const cmInstallCommandArguments* args)
  1341. {
  1342. return this->GetDestination(args, "CMAKE_INSTALL_BINDIR", "bin");
  1343. }
  1344. std::string Helper::GetSbinDestination(const cmInstallCommandArguments* args)
  1345. {
  1346. return this->GetDestination(args, "CMAKE_INSTALL_SBINDIR", "sbin");
  1347. }
  1348. std::string Helper::GetArchiveDestination(
  1349. const cmInstallCommandArguments* args)
  1350. {
  1351. return this->GetDestination(args, "CMAKE_INSTALL_LIBDIR", "lib");
  1352. }
  1353. std::string Helper::GetLibraryDestination(
  1354. const cmInstallCommandArguments* args)
  1355. {
  1356. return this->GetDestination(args, "CMAKE_INSTALL_LIBDIR", "lib");
  1357. }
  1358. std::string Helper::GetIncludeDestination(
  1359. const cmInstallCommandArguments* args)
  1360. {
  1361. return this->GetDestination(args, "CMAKE_INSTALL_INCLUDEDIR", "include");
  1362. }
  1363. std::string Helper::GetSysconfDestination(
  1364. const cmInstallCommandArguments* args)
  1365. {
  1366. return this->GetDestination(args, "CMAKE_INSTALL_SYSCONFDIR", "etc");
  1367. }
  1368. std::string Helper::GetSharedStateDestination(
  1369. const cmInstallCommandArguments* args)
  1370. {
  1371. return this->GetDestination(args, "CMAKE_INSTALL_SHAREDSTATEDIR", "com");
  1372. }
  1373. std::string Helper::GetLocalStateDestination(
  1374. const cmInstallCommandArguments* args)
  1375. {
  1376. return this->GetDestination(args, "CMAKE_INSTALL_LOCALSTATEDIR", "var");
  1377. }
  1378. std::string Helper::GetRunStateDestination(
  1379. const cmInstallCommandArguments* args)
  1380. {
  1381. return this->GetDestination(args, "CMAKE_INSTALL_RUNSTATEDIR",
  1382. this->GetLocalStateDestination(nullptr) +
  1383. "/run");
  1384. }
  1385. std::string Helper::GetDataRootDestination(
  1386. const cmInstallCommandArguments* args)
  1387. {
  1388. return this->GetDestination(args, "CMAKE_INSTALL_DATAROOTDIR", "share");
  1389. }
  1390. std::string Helper::GetDataDestination(const cmInstallCommandArguments* args)
  1391. {
  1392. return this->GetDestination(args, "CMAKE_INSTALL_DATADIR",
  1393. this->GetDataRootDestination(nullptr));
  1394. }
  1395. std::string Helper::GetInfoDestination(const cmInstallCommandArguments* args)
  1396. {
  1397. return this->GetDestination(args, "CMAKE_INSTALL_INFODIR",
  1398. this->GetDataRootDestination(nullptr) + "/info");
  1399. }
  1400. std::string Helper::GetLocaleDestination(const cmInstallCommandArguments* args)
  1401. {
  1402. return this->GetDestination(args, "CMAKE_INSTALL_LOCALEDIR",
  1403. this->GetDataRootDestination(nullptr) +
  1404. "/locale");
  1405. }
  1406. std::string Helper::GetManDestination(const cmInstallCommandArguments* args)
  1407. {
  1408. return this->GetDestination(args, "CMAKE_INSTALL_MANDIR",
  1409. this->GetDataRootDestination(nullptr) + "/man");
  1410. }
  1411. std::string Helper::GetDocDestination(const cmInstallCommandArguments* args)
  1412. {
  1413. return this->GetDestination(args, "CMAKE_INSTALL_DOCDIR",
  1414. this->GetDataRootDestination(nullptr) + "/doc");
  1415. }
  1416. std::string Helper::GetDestinationForType(
  1417. const cmInstallCommandArguments* args, const std::string& type)
  1418. {
  1419. if (args && !args->GetDestination().empty()) {
  1420. return args->GetDestination();
  1421. }
  1422. if (type == "BIN") {
  1423. return this->GetRuntimeDestination(nullptr);
  1424. }
  1425. if (type == "SBIN") {
  1426. return this->GetSbinDestination(nullptr);
  1427. }
  1428. if (type == "SYSCONF") {
  1429. return this->GetSysconfDestination(nullptr);
  1430. }
  1431. if (type == "SHAREDSTATE") {
  1432. return this->GetSharedStateDestination(nullptr);
  1433. }
  1434. if (type == "LOCALSTATE") {
  1435. return this->GetLocalStateDestination(nullptr);
  1436. }
  1437. if (type == "RUNSTATE") {
  1438. return this->GetRunStateDestination(nullptr);
  1439. }
  1440. if (type == "LIB") {
  1441. return this->GetLibraryDestination(nullptr);
  1442. }
  1443. if (type == "INCLUDE") {
  1444. return this->GetIncludeDestination(nullptr);
  1445. }
  1446. if (type == "DATA") {
  1447. return this->GetDataDestination(nullptr);
  1448. }
  1449. if (type == "INFO") {
  1450. return this->GetInfoDestination(nullptr);
  1451. }
  1452. if (type == "LOCALE") {
  1453. return this->GetLocaleDestination(nullptr);
  1454. }
  1455. if (type == "MAN") {
  1456. return this->GetManDestination(nullptr);
  1457. }
  1458. if (type == "DOC") {
  1459. return this->GetDocDestination(nullptr);
  1460. }
  1461. return "";
  1462. }
  1463. } // namespace
  1464. bool cmInstallCommand(std::vector<std::string> const& args,
  1465. cmExecutionStatus& status)
  1466. {
  1467. // Allow calling with no arguments so that arguments may be built up
  1468. // using a variable that may be left empty.
  1469. if (args.empty()) {
  1470. return true;
  1471. }
  1472. // Enable the install target.
  1473. status.GetMakefile().GetGlobalGenerator()->EnableInstallTarget();
  1474. static cmSubcommandTable const subcommand{
  1475. { "SCRIPT"_s, HandleScriptMode },
  1476. { "CODE"_s, HandleScriptMode },
  1477. { "TARGETS"_s, HandleTargetsMode },
  1478. { "FILES"_s, HandleFilesMode },
  1479. { "PROGRAMS"_s, HandleFilesMode },
  1480. { "DIRECTORY"_s, HandleDirectoryMode },
  1481. { "EXPORT"_s, HandleExportMode },
  1482. { "EXPORT_ANDROID_MK"_s, HandleExportAndroidMKMode },
  1483. };
  1484. return subcommand(args[0], args, status);
  1485. }