cmInstallCommand.cxx 58 KB

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