1
0

cmInstallCommand.cxx 58 KB

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