cmInstallCommand.cxx 57 KB

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