cmInstallCommand.cxx 58 KB

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