cmFindPackageCommand.cxx 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422
  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 "cmFindPackageCommand.h"
  4. #include <algorithm>
  5. #include <cassert>
  6. #include <cstdio>
  7. #include <cstring>
  8. #include <deque>
  9. #include <functional>
  10. #include <iterator>
  11. #include <sstream>
  12. #include <utility>
  13. #include <cm/memory>
  14. #include "cmsys/Directory.hxx"
  15. #include "cmsys/FStream.hxx"
  16. #include "cmsys/Glob.hxx"
  17. #include "cmsys/RegularExpression.hxx"
  18. #include "cmsys/String.h"
  19. #include "cmAlgorithms.h"
  20. #include "cmMakefile.h"
  21. #include "cmMessageType.h"
  22. #include "cmPolicies.h"
  23. #include "cmProperty.h"
  24. #include "cmRange.h"
  25. #include "cmSearchPath.h"
  26. #include "cmState.h"
  27. #include "cmStateTypes.h"
  28. #include "cmStringAlgorithms.h"
  29. #include "cmSystemTools.h"
  30. #include "cmVersion.h"
  31. #if defined(__HAIKU__)
  32. # include <FindDirectory.h>
  33. # include <StorageDefs.h>
  34. #endif
  35. class cmExecutionStatus;
  36. class cmFileList;
  37. cmFindPackageCommand::PathLabel cmFindPackageCommand::PathLabel::UserRegistry(
  38. "PACKAGE_REGISTRY");
  39. cmFindPackageCommand::PathLabel cmFindPackageCommand::PathLabel::Builds(
  40. "BUILDS");
  41. cmFindPackageCommand::PathLabel
  42. cmFindPackageCommand::PathLabel::SystemRegistry("SYSTEM_PACKAGE_REGISTRY");
  43. struct StrverscmpGreater
  44. {
  45. bool operator()(const std::string& lhs, const std::string& rhs) const
  46. {
  47. return cmSystemTools::strverscmp(lhs, rhs) > 0;
  48. }
  49. };
  50. struct StrverscmpLesser
  51. {
  52. bool operator()(const std::string& lhs, const std::string& rhs) const
  53. {
  54. return cmSystemTools::strverscmp(lhs, rhs) < 0;
  55. }
  56. };
  57. void cmFindPackageCommand::Sort(std::vector<std::string>::iterator begin,
  58. std::vector<std::string>::iterator end,
  59. SortOrderType order, SortDirectionType dir)
  60. {
  61. if (order == Name_order) {
  62. if (dir == Dec) {
  63. std::sort(begin, end, std::greater<std::string>());
  64. } else {
  65. std::sort(begin, end);
  66. }
  67. } else if (order == Natural)
  68. // natural order uses letters and numbers (contiguous numbers digit are
  69. // compared such that e.g. 000 00 < 01 < 010 < 09 < 0 < 1 < 9 < 10
  70. {
  71. if (dir == Dec) {
  72. std::sort(begin, end, StrverscmpGreater());
  73. } else {
  74. std::sort(begin, end, StrverscmpLesser());
  75. }
  76. }
  77. // else do not sort
  78. }
  79. cmFindPackageCommand::cmFindPackageCommand(cmExecutionStatus& status)
  80. : cmFindCommon(status)
  81. {
  82. this->CMakePathName = "PACKAGE";
  83. this->Quiet = false;
  84. this->Required = false;
  85. this->NoUserRegistry = false;
  86. this->NoSystemRegistry = false;
  87. this->UseConfigFiles = true;
  88. this->UseFindModules = true;
  89. this->DebugMode = false;
  90. this->UseLib32Paths = false;
  91. this->UseLib64Paths = false;
  92. this->UseLibx32Paths = false;
  93. this->UseRealPath = false;
  94. this->PolicyScope = true;
  95. this->VersionMajor = 0;
  96. this->VersionMinor = 0;
  97. this->VersionPatch = 0;
  98. this->VersionTweak = 0;
  99. this->VersionCount = 0;
  100. this->VersionExact = false;
  101. this->VersionFoundMajor = 0;
  102. this->VersionFoundMinor = 0;
  103. this->VersionFoundPatch = 0;
  104. this->VersionFoundTweak = 0;
  105. this->VersionFoundCount = 0;
  106. this->RequiredCMakeVersion = 0;
  107. this->SortOrder = None;
  108. this->SortDirection = Asc;
  109. this->AppendSearchPathGroups();
  110. this->DeprecatedFindModules["Qt"] = cmPolicies::CMP0084;
  111. }
  112. void cmFindPackageCommand::AppendSearchPathGroups()
  113. {
  114. std::vector<cmFindCommon::PathLabel>* labels;
  115. // Update the All group with new paths
  116. labels = &this->PathGroupLabelMap[PathGroup::All];
  117. labels->insert(
  118. std::find(labels->begin(), labels->end(), PathLabel::CMakeSystem),
  119. PathLabel::UserRegistry);
  120. labels->insert(
  121. std::find(labels->begin(), labels->end(), PathLabel::CMakeSystem),
  122. PathLabel::Builds);
  123. labels->insert(std::find(labels->begin(), labels->end(), PathLabel::Guess),
  124. PathLabel::SystemRegistry);
  125. // Create the new path objects
  126. this->LabeledPaths.insert(
  127. std::make_pair(PathLabel::UserRegistry, cmSearchPath(this)));
  128. this->LabeledPaths.insert(
  129. std::make_pair(PathLabel::Builds, cmSearchPath(this)));
  130. this->LabeledPaths.insert(
  131. std::make_pair(PathLabel::SystemRegistry, cmSearchPath(this)));
  132. }
  133. bool cmFindPackageCommand::InitialPass(std::vector<std::string> const& args)
  134. {
  135. if (args.empty()) {
  136. this->SetError("called with incorrect number of arguments");
  137. return false;
  138. }
  139. // Lookup required version of CMake.
  140. if (const char* rv =
  141. this->Makefile->GetDefinition("CMAKE_MINIMUM_REQUIRED_VERSION")) {
  142. unsigned int v[3] = { 0, 0, 0 };
  143. sscanf(rv, "%u.%u.%u", &v[0], &v[1], &v[2]);
  144. this->RequiredCMakeVersion = CMake_VERSION_ENCODE(v[0], v[1], v[2]);
  145. }
  146. this->DebugMode = ComputeIfDebugModeWanted();
  147. this->DebugBuffer.clear();
  148. // Lookup target architecture, if any.
  149. if (const char* arch =
  150. this->Makefile->GetDefinition("CMAKE_LIBRARY_ARCHITECTURE")) {
  151. this->LibraryArchitecture = arch;
  152. }
  153. // Lookup whether lib32 paths should be used.
  154. if (this->Makefile->PlatformIs32Bit() &&
  155. this->Makefile->GetState()->GetGlobalPropertyAsBool(
  156. "FIND_LIBRARY_USE_LIB32_PATHS")) {
  157. this->UseLib32Paths = true;
  158. }
  159. // Lookup whether lib64 paths should be used.
  160. if (this->Makefile->PlatformIs64Bit() &&
  161. this->Makefile->GetState()->GetGlobalPropertyAsBool(
  162. "FIND_LIBRARY_USE_LIB64_PATHS")) {
  163. this->UseLib64Paths = true;
  164. }
  165. // Lookup whether libx32 paths should be used.
  166. if (this->Makefile->PlatformIsx32() &&
  167. this->Makefile->GetState()->GetGlobalPropertyAsBool(
  168. "FIND_LIBRARY_USE_LIBX32_PATHS")) {
  169. this->UseLibx32Paths = true;
  170. }
  171. // Check if User Package Registry should be disabled
  172. // The `CMAKE_FIND_USE_PACKAGE_REGISTRY` has
  173. // priority over the deprecated CMAKE_FIND_PACKAGE_NO_PACKAGE_REGISTRY
  174. if (const char* def =
  175. this->Makefile->GetDefinition("CMAKE_FIND_USE_PACKAGE_REGISTRY")) {
  176. this->NoUserRegistry = !cmIsOn(def);
  177. } else if (this->Makefile->IsOn("CMAKE_FIND_PACKAGE_NO_PACKAGE_REGISTRY")) {
  178. this->NoUserRegistry = true;
  179. }
  180. // Check if System Package Registry should be disabled
  181. // The `CMAKE_FIND_USE_SYSTEM_PACKAGE_REGISTRY` has
  182. // priority over the deprecated CMAKE_FIND_PACKAGE_NO_SYSTEM_PACKAGE_REGISTRY
  183. if (const char* def = this->Makefile->GetDefinition(
  184. "CMAKE_FIND_USE_SYSTEM_PACKAGE_REGISTRY")) {
  185. this->NoSystemRegistry = !cmIsOn(def);
  186. } else if (this->Makefile->IsOn(
  187. "CMAKE_FIND_PACKAGE_NO_SYSTEM_PACKAGE_REGISTRY")) {
  188. this->NoSystemRegistry = true;
  189. }
  190. // Check whether we should resolve symlinks when finding packages
  191. if (this->Makefile->IsOn("CMAKE_FIND_PACKAGE_RESOLVE_SYMLINKS")) {
  192. this->UseRealPath = true;
  193. }
  194. // Check if Sorting should be enabled
  195. if (const char* so =
  196. this->Makefile->GetDefinition("CMAKE_FIND_PACKAGE_SORT_ORDER")) {
  197. if (strcmp(so, "NAME") == 0) {
  198. this->SortOrder = Name_order;
  199. } else if (strcmp(so, "NATURAL") == 0) {
  200. this->SortOrder = Natural;
  201. } else {
  202. this->SortOrder = None;
  203. }
  204. }
  205. if (const char* sd =
  206. this->Makefile->GetDefinition("CMAKE_FIND_PACKAGE_SORT_DIRECTION")) {
  207. this->SortDirection = strcmp(sd, "ASC") == 0 ? Asc : Dec;
  208. }
  209. // Find what search path locations have been enabled/disable
  210. this->SelectDefaultSearchModes();
  211. // Find the current root path mode.
  212. this->SelectDefaultRootPathMode();
  213. // Find the current bundle/framework search policy.
  214. this->SelectDefaultMacMode();
  215. // Record options.
  216. this->Name = args[0];
  217. std::string components;
  218. const char* components_sep = "";
  219. std::set<std::string> requiredComponents;
  220. std::set<std::string> optionalComponents;
  221. // Always search directly in a generated path.
  222. this->SearchPathSuffixes.emplace_back();
  223. // Parse the arguments.
  224. enum Doing
  225. {
  226. DoingNone,
  227. DoingComponents,
  228. DoingOptionalComponents,
  229. DoingNames,
  230. DoingPaths,
  231. DoingPathSuffixes,
  232. DoingConfigs,
  233. DoingHints
  234. };
  235. Doing doing = DoingNone;
  236. cmsys::RegularExpression version("^[0-9.]+$");
  237. bool haveVersion = false;
  238. std::set<unsigned int> configArgs;
  239. std::set<unsigned int> moduleArgs;
  240. for (unsigned int i = 1; i < args.size(); ++i) {
  241. if (args[i] == "QUIET") {
  242. this->Quiet = true;
  243. doing = DoingNone;
  244. } else if (args[i] == "EXACT") {
  245. this->VersionExact = true;
  246. doing = DoingNone;
  247. } else if (args[i] == "MODULE") {
  248. moduleArgs.insert(i);
  249. doing = DoingNone;
  250. // XXX(clang-tidy): https://bugs.llvm.org/show_bug.cgi?id=44165
  251. // NOLINTNEXTLINE(bugprone-branch-clone)
  252. } else if (args[i] == "CONFIG") {
  253. configArgs.insert(i);
  254. doing = DoingNone;
  255. // XXX(clang-tidy): https://bugs.llvm.org/show_bug.cgi?id=44165
  256. // NOLINTNEXTLINE(bugprone-branch-clone)
  257. } else if (args[i] == "NO_MODULE") {
  258. configArgs.insert(i);
  259. doing = DoingNone;
  260. } else if (args[i] == "REQUIRED") {
  261. this->Required = true;
  262. doing = DoingComponents;
  263. } else if (args[i] == "COMPONENTS") {
  264. doing = DoingComponents;
  265. } else if (args[i] == "OPTIONAL_COMPONENTS") {
  266. doing = DoingOptionalComponents;
  267. } else if (args[i] == "NAMES") {
  268. configArgs.insert(i);
  269. doing = DoingNames;
  270. } else if (args[i] == "PATHS") {
  271. configArgs.insert(i);
  272. doing = DoingPaths;
  273. } else if (args[i] == "HINTS") {
  274. configArgs.insert(i);
  275. doing = DoingHints;
  276. } else if (args[i] == "PATH_SUFFIXES") {
  277. configArgs.insert(i);
  278. doing = DoingPathSuffixes;
  279. } else if (args[i] == "CONFIGS") {
  280. configArgs.insert(i);
  281. doing = DoingConfigs;
  282. } else if (args[i] == "NO_POLICY_SCOPE") {
  283. this->PolicyScope = false;
  284. doing = DoingNone;
  285. } else if (args[i] == "NO_CMAKE_PACKAGE_REGISTRY") {
  286. this->NoUserRegistry = true;
  287. configArgs.insert(i);
  288. doing = DoingNone;
  289. } else if (args[i] == "NO_CMAKE_SYSTEM_PACKAGE_REGISTRY") {
  290. this->NoSystemRegistry = true;
  291. configArgs.insert(i);
  292. doing = DoingNone;
  293. // XXX(clang-tidy): https://bugs.llvm.org/show_bug.cgi?id=44165
  294. // NOLINTNEXTLINE(bugprone-branch-clone)
  295. } else if (args[i] == "NO_CMAKE_BUILDS_PATH") {
  296. // Ignore legacy option.
  297. configArgs.insert(i);
  298. doing = DoingNone;
  299. } else if (this->CheckCommonArgument(args[i])) {
  300. configArgs.insert(i);
  301. doing = DoingNone;
  302. } else if ((doing == DoingComponents) ||
  303. (doing == DoingOptionalComponents)) {
  304. // Set a variable telling the find script whether this component
  305. // is required.
  306. const char* isRequired = "1";
  307. if (doing == DoingOptionalComponents) {
  308. isRequired = "0";
  309. optionalComponents.insert(args[i]);
  310. } else {
  311. requiredComponents.insert(args[i]);
  312. }
  313. std::string req_var = this->Name + "_FIND_REQUIRED_" + args[i];
  314. this->AddFindDefinition(req_var, isRequired);
  315. // Append to the list of required components.
  316. components += components_sep;
  317. components += args[i];
  318. components_sep = ";";
  319. } else if (doing == DoingNames) {
  320. this->Names.push_back(args[i]);
  321. } else if (doing == DoingPaths) {
  322. this->UserGuessArgs.push_back(args[i]);
  323. } else if (doing == DoingHints) {
  324. this->UserHintsArgs.push_back(args[i]);
  325. } else if (doing == DoingPathSuffixes) {
  326. this->AddPathSuffix(args[i]);
  327. } else if (doing == DoingConfigs) {
  328. if (args[i].find_first_of(":/\\") != std::string::npos ||
  329. cmSystemTools::GetFilenameLastExtension(args[i]) != ".cmake") {
  330. this->SetError(cmStrCat(
  331. "given CONFIGS option followed by invalid file name \"", args[i],
  332. "\". The names given must be file names without "
  333. "a path and with a \".cmake\" extension."));
  334. return false;
  335. }
  336. this->Configs.push_back(args[i]);
  337. } else if (!haveVersion && version.find(args[i])) {
  338. haveVersion = true;
  339. this->Version = args[i];
  340. } else {
  341. this->SetError(
  342. cmStrCat("called with invalid argument \"", args[i], "\""));
  343. return false;
  344. }
  345. }
  346. std::vector<std::string> doubledComponents;
  347. std::set_intersection(requiredComponents.begin(), requiredComponents.end(),
  348. optionalComponents.begin(), optionalComponents.end(),
  349. std::back_inserter(doubledComponents));
  350. if (!doubledComponents.empty()) {
  351. this->SetError(
  352. cmStrCat("called with components that are both required and "
  353. "optional:\n",
  354. cmWrap(" ", doubledComponents, "", "\n"), "\n"));
  355. return false;
  356. }
  357. // Maybe choose one mode exclusively.
  358. this->UseFindModules = configArgs.empty();
  359. this->UseConfigFiles = moduleArgs.empty();
  360. if (!this->UseFindModules && !this->UseConfigFiles) {
  361. std::ostringstream e;
  362. e << "given options exclusive to Module mode:\n";
  363. for (unsigned int si : moduleArgs) {
  364. e << " " << args[si] << "\n";
  365. }
  366. e << "and options exclusive to Config mode:\n";
  367. for (unsigned int si : configArgs) {
  368. e << " " << args[si] << "\n";
  369. }
  370. e << "The options are incompatible.";
  371. this->SetError(e.str());
  372. return false;
  373. }
  374. // Ignore EXACT with no version.
  375. if (this->Version.empty() && this->VersionExact) {
  376. this->VersionExact = false;
  377. this->Makefile->IssueMessage(
  378. MessageType::AUTHOR_WARNING,
  379. "Ignoring EXACT since no version is requested.");
  380. }
  381. if (this->Version.empty() || components.empty()) {
  382. // Check whether we are recursing inside "Find<name>.cmake" within
  383. // another find_package(<name>) call.
  384. std::string mod = cmStrCat(this->Name, "_FIND_MODULE");
  385. if (this->Makefile->IsOn(mod)) {
  386. if (this->Version.empty()) {
  387. // Get version information from the outer call if necessary.
  388. // Requested version string.
  389. std::string ver = cmStrCat(this->Name, "_FIND_VERSION");
  390. this->Version = this->Makefile->GetSafeDefinition(ver);
  391. // Whether an exact version is required.
  392. std::string exact = cmStrCat(this->Name, "_FIND_VERSION_EXACT");
  393. this->VersionExact = this->Makefile->IsOn(exact);
  394. }
  395. if (components.empty()) {
  396. std::string components_var = this->Name + "_FIND_COMPONENTS";
  397. components = this->Makefile->GetSafeDefinition(components_var);
  398. }
  399. }
  400. }
  401. if (!this->Version.empty()) {
  402. // Try to parse the version number and store the results that were
  403. // successfully parsed.
  404. unsigned int parsed_major;
  405. unsigned int parsed_minor;
  406. unsigned int parsed_patch;
  407. unsigned int parsed_tweak;
  408. this->VersionCount =
  409. sscanf(this->Version.c_str(), "%u.%u.%u.%u", &parsed_major,
  410. &parsed_minor, &parsed_patch, &parsed_tweak);
  411. switch (this->VersionCount) {
  412. case 4:
  413. this->VersionTweak = parsed_tweak;
  414. CM_FALLTHROUGH;
  415. case 3:
  416. this->VersionPatch = parsed_patch;
  417. CM_FALLTHROUGH;
  418. case 2:
  419. this->VersionMinor = parsed_minor;
  420. CM_FALLTHROUGH;
  421. case 1:
  422. this->VersionMajor = parsed_major;
  423. CM_FALLTHROUGH;
  424. default:
  425. break;
  426. }
  427. }
  428. std::string disableFindPackageVar =
  429. cmStrCat("CMAKE_DISABLE_FIND_PACKAGE_", this->Name);
  430. if (this->Makefile->IsOn(disableFindPackageVar)) {
  431. if (this->Required) {
  432. this->SetError(
  433. cmStrCat("for module ", this->Name, " called with REQUIRED, but ",
  434. disableFindPackageVar,
  435. " is enabled. A REQUIRED package cannot be disabled."));
  436. return false;
  437. }
  438. return true;
  439. }
  440. {
  441. // Allocate a PACKAGE_ROOT_PATH for the current find_package call.
  442. this->Makefile->FindPackageRootPathStack.emplace_back();
  443. std::vector<std::string>& rootPaths =
  444. this->Makefile->FindPackageRootPathStack.back();
  445. // Add root paths from <PackageName>_ROOT CMake and environment variables,
  446. // subject to CMP0074.
  447. switch (this->Makefile->GetPolicyStatus(cmPolicies::CMP0074)) {
  448. case cmPolicies::WARN:
  449. this->Makefile->MaybeWarnCMP0074(this->Name);
  450. CM_FALLTHROUGH;
  451. case cmPolicies::OLD:
  452. // OLD behavior is to ignore the <pkg>_ROOT variables.
  453. break;
  454. case cmPolicies::REQUIRED_IF_USED:
  455. case cmPolicies::REQUIRED_ALWAYS:
  456. this->Makefile->IssueMessage(
  457. MessageType::FATAL_ERROR,
  458. cmPolicies::GetRequiredPolicyError(cmPolicies::CMP0074));
  459. break;
  460. case cmPolicies::NEW: {
  461. // NEW behavior is to honor the <pkg>_ROOT variables.
  462. std::string const rootVar = this->Name + "_ROOT";
  463. this->Makefile->GetDefExpandList(rootVar, rootPaths, false);
  464. cmSystemTools::GetPath(rootPaths, rootVar.c_str());
  465. } break;
  466. }
  467. }
  468. this->SetModuleVariables(components);
  469. // See if there is a Find<PackageName>.cmake module.
  470. bool loadedPackage = false;
  471. if (this->Makefile->IsOn("CMAKE_FIND_PACKAGE_PREFER_CONFIG")) {
  472. if (this->UseConfigFiles && this->FindPackageUsingConfigMode()) {
  473. loadedPackage = true;
  474. } else {
  475. if (this->FindPackageUsingModuleMode()) {
  476. loadedPackage = true;
  477. } else {
  478. // The package was not loaded. Report errors.
  479. if (HandlePackageMode(HandlePackageModeType::Module)) {
  480. loadedPackage = true;
  481. }
  482. }
  483. }
  484. } else {
  485. if (this->UseFindModules && this->FindPackageUsingModuleMode()) {
  486. loadedPackage = true;
  487. } else {
  488. // Handle CMAKE_FIND_PACKAGE_WARN_NO_MODULE (warn when CONFIG mode is
  489. // implicitly assumed)
  490. if (this->UseFindModules && this->UseConfigFiles &&
  491. this->Makefile->IsOn("CMAKE_FIND_PACKAGE_WARN_NO_MODULE")) {
  492. std::ostringstream aw;
  493. if (this->RequiredCMakeVersion >= CMake_VERSION_ENCODE(2, 8, 8)) {
  494. aw << "find_package called without either MODULE or CONFIG option "
  495. "and "
  496. "no Find"
  497. << this->Name
  498. << ".cmake module is in CMAKE_MODULE_PATH. "
  499. "Add MODULE to exclusively request Module mode and fail if "
  500. "Find"
  501. << this->Name
  502. << ".cmake is missing. "
  503. "Add CONFIG to exclusively request Config mode and search for "
  504. "a "
  505. "package configuration file provided by "
  506. << this->Name << " (" << this->Name << "Config.cmake or "
  507. << cmSystemTools::LowerCase(this->Name) << "-config.cmake). ";
  508. } else {
  509. aw << "find_package called without NO_MODULE option and no "
  510. "Find"
  511. << this->Name
  512. << ".cmake module is in CMAKE_MODULE_PATH. "
  513. "Add NO_MODULE to exclusively request Config mode and search "
  514. "for a "
  515. "package configuration file provided by "
  516. << this->Name << " (" << this->Name << "Config.cmake or "
  517. << cmSystemTools::LowerCase(this->Name)
  518. << "-config.cmake). "
  519. "Otherwise make Find"
  520. << this->Name
  521. << ".cmake available in "
  522. "CMAKE_MODULE_PATH.";
  523. }
  524. aw << "\n"
  525. "(Variable CMAKE_FIND_PACKAGE_WARN_NO_MODULE enabled this "
  526. "warning.)";
  527. this->Makefile->IssueMessage(MessageType::AUTHOR_WARNING, aw.str());
  528. }
  529. if (this->FindPackageUsingConfigMode()) {
  530. loadedPackage = true;
  531. }
  532. }
  533. }
  534. this->AppendSuccessInformation();
  535. return loadedPackage;
  536. }
  537. bool cmFindPackageCommand::FindPackageUsingModuleMode()
  538. {
  539. bool foundModule = false;
  540. if (!this->FindModule(foundModule)) {
  541. return false;
  542. }
  543. return foundModule;
  544. }
  545. bool cmFindPackageCommand::FindPackageUsingConfigMode()
  546. {
  547. this->Variable = cmStrCat(this->Name, "_DIR");
  548. // Add the default name.
  549. if (this->Names.empty()) {
  550. this->Names.push_back(this->Name);
  551. }
  552. // Add the default configs.
  553. if (this->Configs.empty()) {
  554. for (std::string const& n : this->Names) {
  555. std::string config = cmStrCat(n, "Config.cmake");
  556. this->Configs.push_back(config);
  557. config = cmStrCat(cmSystemTools::LowerCase(n), "-config.cmake");
  558. this->Configs.push_back(std::move(config));
  559. }
  560. }
  561. // get igonored paths from vars and reroot them.
  562. std::vector<std::string> ignored;
  563. this->GetIgnoredPaths(ignored);
  564. this->RerootPaths(ignored);
  565. // Construct a set of ignored paths
  566. this->IgnoredPaths.clear();
  567. this->IgnoredPaths.insert(ignored.begin(), ignored.end());
  568. // Find and load the package.
  569. return this->HandlePackageMode(HandlePackageModeType::Config);
  570. }
  571. void cmFindPackageCommand::SetModuleVariables(const std::string& components)
  572. {
  573. this->AddFindDefinition("CMAKE_FIND_PACKAGE_NAME", this->Name.c_str());
  574. // Store the list of components.
  575. std::string components_var = this->Name + "_FIND_COMPONENTS";
  576. this->AddFindDefinition(components_var, components.c_str());
  577. if (this->Quiet) {
  578. // Tell the module that is about to be read that it should find
  579. // quietly.
  580. std::string quietly = cmStrCat(this->Name, "_FIND_QUIETLY");
  581. this->AddFindDefinition(quietly, "1");
  582. }
  583. if (this->Required) {
  584. // Tell the module that is about to be read that it should report
  585. // a fatal error if the package is not found.
  586. std::string req = cmStrCat(this->Name, "_FIND_REQUIRED");
  587. this->AddFindDefinition(req, "1");
  588. }
  589. if (!this->Version.empty()) {
  590. // Tell the module that is about to be read what version of the
  591. // package has been requested.
  592. std::string ver = cmStrCat(this->Name, "_FIND_VERSION");
  593. this->AddFindDefinition(ver, this->Version.c_str());
  594. char buf[64];
  595. sprintf(buf, "%u", this->VersionMajor);
  596. this->AddFindDefinition(ver + "_MAJOR", buf);
  597. sprintf(buf, "%u", this->VersionMinor);
  598. this->AddFindDefinition(ver + "_MINOR", buf);
  599. sprintf(buf, "%u", this->VersionPatch);
  600. this->AddFindDefinition(ver + "_PATCH", buf);
  601. sprintf(buf, "%u", this->VersionTweak);
  602. this->AddFindDefinition(ver + "_TWEAK", buf);
  603. sprintf(buf, "%u", this->VersionCount);
  604. this->AddFindDefinition(ver + "_COUNT", buf);
  605. // Tell the module whether an exact version has been requested.
  606. std::string exact = cmStrCat(this->Name, "_FIND_VERSION_EXACT");
  607. this->AddFindDefinition(exact, this->VersionExact ? "1" : "0");
  608. }
  609. }
  610. void cmFindPackageCommand::AddFindDefinition(const std::string& var,
  611. const char* val)
  612. {
  613. if (const char* old = this->Makefile->GetDefinition(var)) {
  614. this->OriginalDefs[var].exists = true;
  615. this->OriginalDefs[var].value = old;
  616. } else {
  617. this->OriginalDefs[var].exists = false;
  618. }
  619. if (val) {
  620. this->Makefile->AddDefinition(var, val);
  621. }
  622. }
  623. void cmFindPackageCommand::RestoreFindDefinitions()
  624. {
  625. for (auto const& i : this->OriginalDefs) {
  626. OriginalDef const& od = i.second;
  627. if (od.exists) {
  628. this->Makefile->AddDefinition(i.first, od.value);
  629. } else {
  630. this->Makefile->RemoveDefinition(i.first);
  631. }
  632. }
  633. }
  634. bool cmFindPackageCommand::FindModule(bool& found)
  635. {
  636. std::string module = cmStrCat("Find", this->Name, ".cmake");
  637. bool system = false;
  638. std::string debugBuffer =
  639. cmStrCat("find_package considered the following paths for ", this->Name,
  640. ".cmake\n");
  641. std::string mfile = this->Makefile->GetModulesFile(
  642. module, system, this->DebugMode, debugBuffer);
  643. if (this->DebugMode) {
  644. if (mfile.empty()) {
  645. debugBuffer = cmStrCat(debugBuffer, "The file was not found.");
  646. } else {
  647. debugBuffer =
  648. cmStrCat(debugBuffer, "The file was found at\n ", mfile, "\n");
  649. }
  650. this->DebugBuffer = cmStrCat(this->DebugBuffer, debugBuffer, "\n");
  651. }
  652. if (!mfile.empty()) {
  653. if (system) {
  654. auto it = this->DeprecatedFindModules.find(this->Name);
  655. if (it != this->DeprecatedFindModules.end()) {
  656. cmPolicies::PolicyStatus status =
  657. this->Makefile->GetPolicyStatus(it->second);
  658. switch (status) {
  659. case cmPolicies::WARN: {
  660. this->Makefile->IssueMessage(
  661. MessageType::AUTHOR_WARNING,
  662. cmStrCat(cmPolicies::GetPolicyWarning(it->second), "\n"));
  663. CM_FALLTHROUGH;
  664. }
  665. case cmPolicies::OLD:
  666. break;
  667. case cmPolicies::REQUIRED_IF_USED:
  668. case cmPolicies::REQUIRED_ALWAYS:
  669. case cmPolicies::NEW:
  670. return true;
  671. }
  672. }
  673. }
  674. // Load the module we found, and set "<name>_FIND_MODULE" to true
  675. // while inside it.
  676. found = true;
  677. std::string var = cmStrCat(this->Name, "_FIND_MODULE");
  678. this->Makefile->AddDefinition(var, "1");
  679. bool result = this->ReadListFile(mfile, DoPolicyScope);
  680. this->Makefile->RemoveDefinition(var);
  681. return result;
  682. }
  683. return true;
  684. }
  685. bool cmFindPackageCommand::HandlePackageMode(
  686. HandlePackageModeType handlePackageModeType)
  687. {
  688. this->ConsideredConfigs.clear();
  689. // Try to find the config file.
  690. const char* def = this->Makefile->GetDefinition(this->Variable);
  691. // Try to load the config file if the directory is known
  692. bool fileFound = false;
  693. if (this->UseConfigFiles) {
  694. if (!cmIsOff(def)) {
  695. // Get the directory from the variable value.
  696. std::string dir = def;
  697. cmSystemTools::ConvertToUnixSlashes(dir);
  698. // Treat relative paths with respect to the current source dir.
  699. if (!cmSystemTools::FileIsFullPath(dir)) {
  700. dir = "/" + dir;
  701. dir = this->Makefile->GetCurrentSourceDirectory() + dir;
  702. }
  703. // The file location was cached. Look for the correct file.
  704. std::string file;
  705. if (this->FindConfigFile(dir, file)) {
  706. this->FileFound = file;
  707. fileFound = true;
  708. }
  709. def = this->Makefile->GetDefinition(this->Variable);
  710. }
  711. // Search for the config file if it is not already found.
  712. if (cmIsOff(def) || !fileFound) {
  713. fileFound = this->FindConfig();
  714. }
  715. // Sanity check.
  716. if (fileFound && this->FileFound.empty()) {
  717. this->Makefile->IssueMessage(
  718. MessageType::INTERNAL_ERROR,
  719. "fileFound is true but FileFound is empty!");
  720. fileFound = false;
  721. }
  722. }
  723. std::string foundVar = cmStrCat(this->Name, "_FOUND");
  724. std::string notFoundMessageVar = cmStrCat(this->Name, "_NOT_FOUND_MESSAGE");
  725. std::string notFoundMessage;
  726. // If the directory for the config file was found, try to read the file.
  727. bool result = true;
  728. bool found = false;
  729. bool configFileSetFOUNDFalse = false;
  730. if (fileFound) {
  731. if (this->Makefile->IsDefinitionSet(foundVar) &&
  732. !this->Makefile->IsOn(foundVar)) {
  733. // by removing Foo_FOUND here if it is FALSE, we don't really change
  734. // the situation for the Config file which is about to be included,
  735. // but we make it possible to detect later on whether the Config file
  736. // has set Foo_FOUND to FALSE itself:
  737. this->Makefile->RemoveDefinition(foundVar);
  738. }
  739. this->Makefile->RemoveDefinition(notFoundMessageVar);
  740. // Set the version variables before loading the config file.
  741. // It may override them.
  742. this->StoreVersionFound();
  743. // Parse the configuration file.
  744. if (this->ReadListFile(this->FileFound, DoPolicyScope)) {
  745. // The package has been found.
  746. found = true;
  747. // Check whether the Config file has set Foo_FOUND to FALSE:
  748. if (this->Makefile->IsDefinitionSet(foundVar) &&
  749. !this->Makefile->IsOn(foundVar)) {
  750. // we get here if the Config file has set Foo_FOUND actively to FALSE
  751. found = false;
  752. configFileSetFOUNDFalse = true;
  753. notFoundMessage =
  754. this->Makefile->GetSafeDefinition(notFoundMessageVar);
  755. }
  756. } else {
  757. // The configuration file is invalid.
  758. result = false;
  759. }
  760. }
  761. if (this->UseFindModules && !found &&
  762. handlePackageModeType == HandlePackageModeType::Config &&
  763. this->Makefile->IsOn("CMAKE_FIND_PACKAGE_PREFER_CONFIG")) {
  764. // Config mode failed. Allow Module case.
  765. result = false;
  766. }
  767. if (this->DebugMode) {
  768. this->DebugMessage(this->DebugBuffer);
  769. this->DebugBuffer.clear();
  770. }
  771. // package not found
  772. if (result && !found) {
  773. // warn if package required or neither quiet nor in config mode
  774. if (this->Required ||
  775. !(this->Quiet ||
  776. (this->UseConfigFiles && !this->UseFindModules &&
  777. this->ConsideredConfigs.empty()))) {
  778. // The variable is not set.
  779. std::ostringstream e;
  780. std::ostringstream aw;
  781. if (configFileSetFOUNDFalse) {
  782. /* clang-format off */
  783. e << "Found package configuration file:\n"
  784. " " << this->FileFound << "\n"
  785. "but it set " << foundVar << " to FALSE so package \"" <<
  786. this->Name << "\" is considered to be NOT FOUND.";
  787. /* clang-format on */
  788. if (!notFoundMessage.empty()) {
  789. e << " Reason given by package: \n" << notFoundMessage << "\n";
  790. }
  791. }
  792. // If there are files in ConsideredConfigs, it means that FooConfig.cmake
  793. // have been found, but they didn't have appropriate versions.
  794. else if (!this->ConsideredConfigs.empty()) {
  795. auto duplicate_end = cmRemoveDuplicates(this->ConsideredConfigs);
  796. e << "Could not find a configuration file for package \"" << this->Name
  797. << "\" that "
  798. << (this->VersionExact ? "exactly matches" : "is compatible with")
  799. << " requested version \"" << this->Version << "\".\n"
  800. << "The following configuration files were considered but not "
  801. "accepted:\n";
  802. for (ConfigFileInfo const& info :
  803. cmMakeRange(this->ConsideredConfigs.cbegin(), duplicate_end)) {
  804. e << " " << info.filename << ", version: " << info.version << "\n";
  805. }
  806. } else {
  807. std::string requestedVersionString;
  808. if (!this->Version.empty()) {
  809. requestedVersionString =
  810. cmStrCat(" (requested version ", this->Version, ')');
  811. }
  812. if (this->UseConfigFiles) {
  813. if (this->UseFindModules) {
  814. e << "By not providing \"Find" << this->Name
  815. << ".cmake\" in "
  816. "CMAKE_MODULE_PATH this project has asked CMake to find a "
  817. "package configuration file provided by \""
  818. << this->Name
  819. << "\", "
  820. "but CMake did not find one.\n";
  821. }
  822. if (this->Configs.size() == 1) {
  823. e << "Could not find a package configuration file named \""
  824. << this->Configs[0] << "\" provided by package \"" << this->Name
  825. << "\"" << requestedVersionString << ".\n";
  826. } else {
  827. e << "Could not find a package configuration file provided by \""
  828. << this->Name << "\"" << requestedVersionString
  829. << " with any of the following names:\n"
  830. << cmWrap(" ", this->Configs, "", "\n") << "\n";
  831. }
  832. e << "Add the installation prefix of \"" << this->Name
  833. << "\" to "
  834. "CMAKE_PREFIX_PATH or set \""
  835. << this->Variable
  836. << "\" to a "
  837. "directory containing one of the above files. "
  838. "If \""
  839. << this->Name
  840. << "\" provides a separate development "
  841. "package or SDK, be sure it has been installed.";
  842. } else // if(!this->UseFindModules && !this->UseConfigFiles)
  843. {
  844. e << "No \"Find" << this->Name << ".cmake\" found in "
  845. << "CMAKE_MODULE_PATH.";
  846. aw
  847. << "Find" << this->Name
  848. << ".cmake must either be part of this "
  849. "project itself, in this case adjust CMAKE_MODULE_PATH so that "
  850. "it points to the correct location inside its source tree.\n"
  851. "Or it must be installed by a package which has already been "
  852. "found via find_package(). In this case make sure that "
  853. "package has indeed been found and adjust CMAKE_MODULE_PATH to "
  854. "contain the location where that package has installed "
  855. "Find"
  856. << this->Name
  857. << ".cmake. This must be a location "
  858. "provided by that package. This error in general means that "
  859. "the buildsystem of this project is relying on a Find-module "
  860. "without ensuring that it is actually available.\n";
  861. }
  862. }
  863. this->Makefile->IssueMessage(this->Required ? MessageType::FATAL_ERROR
  864. : MessageType::WARNING,
  865. e.str());
  866. if (this->Required) {
  867. cmSystemTools::SetFatalErrorOccured();
  868. }
  869. if (!aw.str().empty()) {
  870. this->Makefile->IssueMessage(MessageType::AUTHOR_WARNING, aw.str());
  871. }
  872. }
  873. // output result if in config mode but not in quiet mode
  874. else if (!this->Quiet) {
  875. this->Makefile->DisplayStatus(cmStrCat("Could NOT find ", this->Name,
  876. " (missing: ", this->Name,
  877. "_DIR)"),
  878. -1);
  879. }
  880. }
  881. // Set a variable marking whether the package was found.
  882. this->Makefile->AddDefinition(foundVar, found ? "1" : "0");
  883. // Set a variable naming the configuration file that was found.
  884. std::string fileVar = cmStrCat(this->Name, "_CONFIG");
  885. if (found) {
  886. this->Makefile->AddDefinition(fileVar, this->FileFound);
  887. } else {
  888. this->Makefile->RemoveDefinition(fileVar);
  889. }
  890. std::string consideredConfigsVar =
  891. cmStrCat(this->Name, "_CONSIDERED_CONFIGS");
  892. std::string consideredVersionsVar =
  893. cmStrCat(this->Name, "_CONSIDERED_VERSIONS");
  894. std::string consideredConfigFiles;
  895. std::string consideredVersions;
  896. const char* sep = "";
  897. for (ConfigFileInfo const& i : this->ConsideredConfigs) {
  898. consideredConfigFiles += sep;
  899. consideredVersions += sep;
  900. consideredConfigFiles += i.filename;
  901. consideredVersions += i.version;
  902. sep = ";";
  903. }
  904. this->Makefile->AddDefinition(consideredConfigsVar, consideredConfigFiles);
  905. this->Makefile->AddDefinition(consideredVersionsVar, consideredVersions);
  906. return result;
  907. }
  908. bool cmFindPackageCommand::FindConfig()
  909. {
  910. // Compute the set of search prefixes.
  911. this->ComputePrefixes();
  912. // Look for the project's configuration file.
  913. bool found = false;
  914. if (this->DebugMode) {
  915. this->DebugBuffer = cmStrCat(this->DebugBuffer,
  916. "find_package considered the following "
  917. "locations for the Config module:\n");
  918. }
  919. // Search for frameworks.
  920. if (!found && (this->SearchFrameworkFirst || this->SearchFrameworkOnly)) {
  921. found = this->FindFrameworkConfig();
  922. }
  923. // Search for apps.
  924. if (!found && (this->SearchAppBundleFirst || this->SearchAppBundleOnly)) {
  925. found = this->FindAppBundleConfig();
  926. }
  927. // Search prefixes.
  928. if (!found && !(this->SearchFrameworkOnly || this->SearchAppBundleOnly)) {
  929. found = this->FindPrefixedConfig();
  930. }
  931. // Search for frameworks.
  932. if (!found && this->SearchFrameworkLast) {
  933. found = this->FindFrameworkConfig();
  934. }
  935. // Search for apps.
  936. if (!found && this->SearchAppBundleLast) {
  937. found = this->FindAppBundleConfig();
  938. }
  939. if (this->DebugMode) {
  940. if (found) {
  941. this->DebugBuffer = cmStrCat(
  942. this->DebugBuffer, "The file was found at\n ", this->FileFound, "\n");
  943. } else {
  944. this->DebugBuffer =
  945. cmStrCat(this->DebugBuffer, "The file was not found.\n");
  946. }
  947. }
  948. // Store the entry in the cache so it can be set by the user.
  949. std::string init;
  950. if (found) {
  951. init = cmSystemTools::GetFilenamePath(this->FileFound);
  952. } else {
  953. init = this->Variable + "-NOTFOUND";
  954. }
  955. std::string help =
  956. cmStrCat("The directory containing a CMake configuration file for ",
  957. this->Name, '.');
  958. // We force the value since we do not get here if it was already set.
  959. this->Makefile->AddCacheDefinition(this->Variable, init, help.c_str(),
  960. cmStateEnums::PATH, true);
  961. return found;
  962. }
  963. bool cmFindPackageCommand::FindPrefixedConfig()
  964. {
  965. std::vector<std::string> const& prefixes = this->SearchPaths;
  966. for (std::string const& p : prefixes) {
  967. if (this->SearchPrefix(p)) {
  968. return true;
  969. }
  970. }
  971. return false;
  972. }
  973. bool cmFindPackageCommand::FindFrameworkConfig()
  974. {
  975. std::vector<std::string> const& prefixes = this->SearchPaths;
  976. for (std::string const& p : prefixes) {
  977. if (this->SearchFrameworkPrefix(p)) {
  978. return true;
  979. }
  980. }
  981. return false;
  982. }
  983. bool cmFindPackageCommand::FindAppBundleConfig()
  984. {
  985. std::vector<std::string> const& prefixes = this->SearchPaths;
  986. for (std::string const& p : prefixes) {
  987. if (this->SearchAppBundlePrefix(p)) {
  988. return true;
  989. }
  990. }
  991. return false;
  992. }
  993. bool cmFindPackageCommand::ReadListFile(const std::string& f,
  994. PolicyScopeRule psr)
  995. {
  996. const bool noPolicyScope = !this->PolicyScope || psr == NoPolicyScope;
  997. if (this->Makefile->ReadDependentFile(f, noPolicyScope)) {
  998. return true;
  999. }
  1000. std::string e = cmStrCat("Error reading CMake code from \"", f, "\".");
  1001. this->SetError(e);
  1002. return false;
  1003. }
  1004. void cmFindPackageCommand::AppendToFoundProperty(bool found)
  1005. {
  1006. std::vector<std::string> foundContents;
  1007. cmProp foundProp =
  1008. this->Makefile->GetState()->GetGlobalProperty("PACKAGES_FOUND");
  1009. if (foundProp && !foundProp->empty()) {
  1010. cmExpandList(*foundProp, foundContents, false);
  1011. auto nameIt =
  1012. std::find(foundContents.begin(), foundContents.end(), this->Name);
  1013. if (nameIt != foundContents.end()) {
  1014. foundContents.erase(nameIt);
  1015. }
  1016. }
  1017. std::vector<std::string> notFoundContents;
  1018. cmProp notFoundProp =
  1019. this->Makefile->GetState()->GetGlobalProperty("PACKAGES_NOT_FOUND");
  1020. if (notFoundProp && !notFoundProp->empty()) {
  1021. cmExpandList(*notFoundProp, notFoundContents, false);
  1022. auto nameIt =
  1023. std::find(notFoundContents.begin(), notFoundContents.end(), this->Name);
  1024. if (nameIt != notFoundContents.end()) {
  1025. notFoundContents.erase(nameIt);
  1026. }
  1027. }
  1028. if (found) {
  1029. foundContents.push_back(this->Name);
  1030. } else {
  1031. notFoundContents.push_back(this->Name);
  1032. }
  1033. std::string tmp = cmJoin(foundContents, ";");
  1034. this->Makefile->GetState()->SetGlobalProperty("PACKAGES_FOUND", tmp.c_str());
  1035. tmp = cmJoin(notFoundContents, ";");
  1036. this->Makefile->GetState()->SetGlobalProperty("PACKAGES_NOT_FOUND",
  1037. tmp.c_str());
  1038. }
  1039. void cmFindPackageCommand::AppendSuccessInformation()
  1040. {
  1041. {
  1042. std::string transitivePropName =
  1043. cmStrCat("_CMAKE_", this->Name, "_TRANSITIVE_DEPENDENCY");
  1044. this->Makefile->GetState()->SetGlobalProperty(transitivePropName, "False");
  1045. }
  1046. std::string found = cmStrCat(this->Name, "_FOUND");
  1047. std::string upperFound = cmSystemTools::UpperCase(found);
  1048. bool upperResult = this->Makefile->IsOn(upperFound);
  1049. bool result = this->Makefile->IsOn(found);
  1050. bool packageFound = (result || upperResult);
  1051. this->AppendToFoundProperty(packageFound);
  1052. // Record whether the find was quiet or not, so this can be used
  1053. // e.g. in FeatureSummary.cmake
  1054. std::string quietInfoPropName = cmStrCat("_CMAKE_", this->Name, "_QUIET");
  1055. this->Makefile->GetState()->SetGlobalProperty(
  1056. quietInfoPropName, this->Quiet ? "TRUE" : "FALSE");
  1057. // set a global property to record the required version of this package
  1058. std::string versionInfoPropName =
  1059. cmStrCat("_CMAKE_", this->Name, "_REQUIRED_VERSION");
  1060. std::string versionInfo;
  1061. if (!this->Version.empty()) {
  1062. versionInfo =
  1063. cmStrCat(this->VersionExact ? "==" : ">=", ' ', this->Version);
  1064. }
  1065. this->Makefile->GetState()->SetGlobalProperty(versionInfoPropName,
  1066. versionInfo.c_str());
  1067. if (this->Required) {
  1068. std::string requiredInfoPropName =
  1069. cmStrCat("_CMAKE_", this->Name, "_TYPE");
  1070. this->Makefile->GetState()->SetGlobalProperty(requiredInfoPropName,
  1071. "REQUIRED");
  1072. }
  1073. // Restore original state of "_FIND_" variables we set.
  1074. this->RestoreFindDefinitions();
  1075. // Pop the package stack
  1076. this->Makefile->FindPackageRootPathStack.pop_back();
  1077. }
  1078. inline std::size_t collectPathsForDebug(std::string& buffer,
  1079. cmSearchPath const& searchPath,
  1080. std::size_t startIndex = 0)
  1081. {
  1082. const auto& paths = searchPath.GetPaths();
  1083. if (paths.empty()) {
  1084. buffer += " none";
  1085. return 0;
  1086. }
  1087. for (std::size_t i = startIndex; i < paths.size(); i++) {
  1088. buffer += " " + paths[i] + "\n";
  1089. }
  1090. return paths.size();
  1091. }
  1092. void cmFindPackageCommand::ComputePrefixes()
  1093. {
  1094. if (!this->NoDefaultPath) {
  1095. if (!this->NoPackageRootPath) {
  1096. this->FillPrefixesPackageRoot();
  1097. }
  1098. if (!this->NoCMakePath) {
  1099. this->FillPrefixesCMakeVariable();
  1100. }
  1101. if (!this->NoCMakeEnvironmentPath) {
  1102. this->FillPrefixesCMakeEnvironment();
  1103. }
  1104. }
  1105. this->FillPrefixesUserHints();
  1106. if (!this->NoDefaultPath) {
  1107. if (!this->NoSystemEnvironmentPath) {
  1108. this->FillPrefixesSystemEnvironment();
  1109. }
  1110. if (!this->NoUserRegistry) {
  1111. this->FillPrefixesUserRegistry();
  1112. }
  1113. if (!this->NoCMakeSystemPath) {
  1114. this->FillPrefixesCMakeSystemVariable();
  1115. }
  1116. if (!this->NoSystemRegistry) {
  1117. this->FillPrefixesSystemRegistry();
  1118. }
  1119. }
  1120. this->FillPrefixesUserGuess();
  1121. this->ComputeFinalPaths();
  1122. }
  1123. void cmFindPackageCommand::FillPrefixesPackageRoot()
  1124. {
  1125. cmSearchPath& paths = this->LabeledPaths[PathLabel::PackageRoot];
  1126. // Add the PACKAGE_ROOT_PATH from each enclosing find_package call.
  1127. for (auto pkgPaths = this->Makefile->FindPackageRootPathStack.rbegin();
  1128. pkgPaths != this->Makefile->FindPackageRootPathStack.rend();
  1129. ++pkgPaths) {
  1130. for (std::string const& path : *pkgPaths) {
  1131. paths.AddPath(path);
  1132. }
  1133. }
  1134. if (this->DebugMode) {
  1135. std::string debugBuffer = "<PackageName>_ROOT CMake variable "
  1136. "[CMAKE_FIND_USE_PACKAGE_ROOT_PATH].\n";
  1137. collectPathsForDebug(debugBuffer, paths);
  1138. this->DebugBuffer = cmStrCat(this->DebugBuffer, debugBuffer, "\n");
  1139. }
  1140. }
  1141. void cmFindPackageCommand::FillPrefixesCMakeEnvironment()
  1142. {
  1143. cmSearchPath& paths = this->LabeledPaths[PathLabel::CMakeEnvironment];
  1144. std::string debugBuffer;
  1145. std::size_t debugOffset = 0;
  1146. // Check the environment variable with the same name as the cache
  1147. // entry.
  1148. paths.AddEnvPath(this->Variable);
  1149. if (this->DebugMode) {
  1150. debugBuffer = cmStrCat("Env variable ", this->Variable,
  1151. " [CMAKE_FIND_USE_CMAKE_ENVIRONMENT_PATH].\n");
  1152. debugOffset = collectPathsForDebug(debugBuffer, paths);
  1153. }
  1154. // And now the general CMake environment variables
  1155. paths.AddEnvPath("CMAKE_PREFIX_PATH");
  1156. if (this->DebugMode) {
  1157. debugBuffer = cmStrCat(debugBuffer,
  1158. "\nCMAKE_PREFIX_PATH env variable "
  1159. "[CMAKE_FIND_USE_CMAKE_ENVIRONMENT_PATH].\n");
  1160. debugOffset = collectPathsForDebug(debugBuffer, paths, debugOffset);
  1161. }
  1162. paths.AddEnvPath("CMAKE_FRAMEWORK_PATH");
  1163. paths.AddEnvPath("CMAKE_APPBUNDLE_PATH");
  1164. if (this->DebugMode) {
  1165. debugBuffer =
  1166. cmStrCat(debugBuffer,
  1167. "\nCMAKE_FRAMEWORK_PATH and CMAKE_APPBUNDLE_PATH env "
  1168. "variables [CMAKE_FIND_USE_CMAKE_ENVIRONMENT_PATH].\n");
  1169. collectPathsForDebug(debugBuffer, paths, debugOffset);
  1170. this->DebugBuffer = cmStrCat(this->DebugBuffer, debugBuffer, "\n");
  1171. }
  1172. }
  1173. void cmFindPackageCommand::FillPrefixesCMakeVariable()
  1174. {
  1175. cmSearchPath& paths = this->LabeledPaths[PathLabel::CMake];
  1176. std::string debugBuffer;
  1177. std::size_t debugOffset = 0;
  1178. paths.AddCMakePath("CMAKE_PREFIX_PATH");
  1179. if (this->DebugMode) {
  1180. debugBuffer = "CMAKE_PREFIX_PATH variable [CMAKE_FIND_USE_CMAKE_PATH].\n";
  1181. debugOffset = collectPathsForDebug(debugBuffer, paths);
  1182. }
  1183. paths.AddCMakePath("CMAKE_FRAMEWORK_PATH");
  1184. paths.AddCMakePath("CMAKE_APPBUNDLE_PATH");
  1185. if (this->DebugMode) {
  1186. debugBuffer =
  1187. cmStrCat(debugBuffer,
  1188. "\nCMAKE_FRAMEWORK_PATH and CMAKE_APPBUNDLE_PATH variables "
  1189. "[CMAKE_FIND_USE_CMAKE_PATH].\n");
  1190. collectPathsForDebug(debugBuffer, paths, debugOffset);
  1191. this->DebugBuffer = cmStrCat(this->DebugBuffer, debugBuffer, "\n");
  1192. }
  1193. }
  1194. void cmFindPackageCommand::FillPrefixesSystemEnvironment()
  1195. {
  1196. cmSearchPath& paths = this->LabeledPaths[PathLabel::SystemEnvironment];
  1197. // Use the system search path to generate prefixes.
  1198. // Relative paths are interpreted with respect to the current
  1199. // working directory.
  1200. std::vector<std::string> tmp;
  1201. cmSystemTools::GetPath(tmp);
  1202. for (std::string const& i : tmp) {
  1203. // If the path is a PREFIX/bin case then add its parent instead.
  1204. if ((cmHasLiteralSuffix(i, "/bin")) || (cmHasLiteralSuffix(i, "/sbin"))) {
  1205. paths.AddPath(cmSystemTools::GetFilenamePath(i));
  1206. } else {
  1207. paths.AddPath(i);
  1208. }
  1209. }
  1210. if (this->DebugMode) {
  1211. std::string debugBuffer = "Standard system environment variables "
  1212. "[CMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH].\n";
  1213. collectPathsForDebug(debugBuffer, paths);
  1214. this->DebugBuffer = cmStrCat(this->DebugBuffer, debugBuffer, "\n");
  1215. }
  1216. }
  1217. void cmFindPackageCommand::FillPrefixesUserRegistry()
  1218. {
  1219. #if defined(_WIN32) && !defined(__CYGWIN__)
  1220. this->LoadPackageRegistryWinUser();
  1221. #elif defined(__HAIKU__)
  1222. char dir[B_PATH_NAME_LENGTH];
  1223. if (find_directory(B_USER_SETTINGS_DIRECTORY, -1, false, dir, sizeof(dir)) ==
  1224. B_OK) {
  1225. std::string fname = cmStrCat(dir, "/cmake/packages/", Name);
  1226. this->LoadPackageRegistryDir(fname,
  1227. this->LabeledPaths[PathLabel::UserRegistry]);
  1228. }
  1229. #else
  1230. std::string dir;
  1231. if (cmSystemTools::GetEnv("HOME", dir)) {
  1232. dir += "/.cmake/packages/";
  1233. dir += this->Name;
  1234. this->LoadPackageRegistryDir(dir,
  1235. this->LabeledPaths[PathLabel::UserRegistry]);
  1236. }
  1237. #endif
  1238. if (this->DebugMode) {
  1239. std::string debugBuffer =
  1240. "CMake User Package Registry [CMAKE_FIND_USE_PACKAGE_REGISTRY].\n";
  1241. collectPathsForDebug(debugBuffer,
  1242. this->LabeledPaths[PathLabel::UserRegistry]);
  1243. this->DebugBuffer = cmStrCat(this->DebugBuffer, debugBuffer, "\n");
  1244. }
  1245. }
  1246. void cmFindPackageCommand::FillPrefixesSystemRegistry()
  1247. {
  1248. if (this->NoSystemRegistry || this->NoDefaultPath) {
  1249. return;
  1250. }
  1251. #if defined(_WIN32) && !defined(__CYGWIN__)
  1252. this->LoadPackageRegistryWinSystem();
  1253. #endif
  1254. if (this->DebugMode) {
  1255. std::string debugBuffer =
  1256. "CMake System Package Registry "
  1257. "[CMAKE_FIND_PACKAGE_NO_SYSTEM_PACKAGE_REGISTRY].\n";
  1258. collectPathsForDebug(debugBuffer,
  1259. this->LabeledPaths[PathLabel::SystemRegistry]);
  1260. this->DebugBuffer = cmStrCat(this->DebugBuffer, debugBuffer, "\n");
  1261. }
  1262. }
  1263. #if defined(_WIN32) && !defined(__CYGWIN__)
  1264. # include <windows.h>
  1265. // http://msdn.microsoft.com/en-us/library/aa384253%28v=vs.85%29.aspx
  1266. # if !defined(KEY_WOW64_32KEY)
  1267. # define KEY_WOW64_32KEY 0x0200
  1268. # endif
  1269. # if !defined(KEY_WOW64_64KEY)
  1270. # define KEY_WOW64_64KEY 0x0100
  1271. # endif
  1272. void cmFindPackageCommand::LoadPackageRegistryWinUser()
  1273. {
  1274. // HKEY_CURRENT_USER\\Software shares 32-bit and 64-bit views.
  1275. this->LoadPackageRegistryWin(true, 0,
  1276. this->LabeledPaths[PathLabel::UserRegistry]);
  1277. }
  1278. void cmFindPackageCommand::LoadPackageRegistryWinSystem()
  1279. {
  1280. cmSearchPath& paths = this->LabeledPaths[PathLabel::SystemRegistry];
  1281. // HKEY_LOCAL_MACHINE\\SOFTWARE has separate 32-bit and 64-bit views.
  1282. // Prefer the target platform view first.
  1283. if (this->Makefile->PlatformIs64Bit()) {
  1284. this->LoadPackageRegistryWin(false, KEY_WOW64_64KEY, paths);
  1285. this->LoadPackageRegistryWin(false, KEY_WOW64_32KEY, paths);
  1286. } else {
  1287. this->LoadPackageRegistryWin(false, KEY_WOW64_32KEY, paths);
  1288. this->LoadPackageRegistryWin(false, KEY_WOW64_64KEY, paths);
  1289. }
  1290. }
  1291. void cmFindPackageCommand::LoadPackageRegistryWin(bool user, unsigned int view,
  1292. cmSearchPath& outPaths)
  1293. {
  1294. std::wstring key = L"Software\\Kitware\\CMake\\Packages\\";
  1295. key += cmsys::Encoding::ToWide(this->Name);
  1296. std::set<std::wstring> bad;
  1297. HKEY hKey;
  1298. if (RegOpenKeyExW(user ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE, key.c_str(),
  1299. 0, KEY_QUERY_VALUE | view, &hKey) == ERROR_SUCCESS) {
  1300. DWORD valueType = REG_NONE;
  1301. wchar_t name[16383]; // RegEnumValue docs limit name to 32767 _bytes_
  1302. std::vector<wchar_t> data(512);
  1303. bool done = false;
  1304. DWORD index = 0;
  1305. while (!done) {
  1306. DWORD nameSize = static_cast<DWORD>(sizeof(name));
  1307. DWORD dataSize = static_cast<DWORD>(data.size() * sizeof(data[0]));
  1308. switch (RegEnumValueW(hKey, index, name, &nameSize, 0, &valueType,
  1309. (BYTE*)&data[0], &dataSize)) {
  1310. case ERROR_SUCCESS:
  1311. ++index;
  1312. if (valueType == REG_SZ) {
  1313. data[dataSize] = 0;
  1314. if (!this->CheckPackageRegistryEntry(
  1315. cmsys::Encoding::ToNarrow(&data[0]), outPaths)) {
  1316. // The entry is invalid.
  1317. bad.insert(name);
  1318. }
  1319. }
  1320. break;
  1321. case ERROR_MORE_DATA:
  1322. data.resize((dataSize + sizeof(data[0]) - 1) / sizeof(data[0]));
  1323. break;
  1324. case ERROR_NO_MORE_ITEMS:
  1325. default:
  1326. done = true;
  1327. break;
  1328. }
  1329. }
  1330. RegCloseKey(hKey);
  1331. }
  1332. // Remove bad values if possible.
  1333. if (user && !bad.empty() &&
  1334. RegOpenKeyExW(HKEY_CURRENT_USER, key.c_str(), 0, KEY_SET_VALUE | view,
  1335. &hKey) == ERROR_SUCCESS) {
  1336. for (std::wstring const& v : bad) {
  1337. RegDeleteValueW(hKey, v.c_str());
  1338. }
  1339. RegCloseKey(hKey);
  1340. }
  1341. }
  1342. #else
  1343. class cmFindPackageCommandHoldFile
  1344. {
  1345. const char* File;
  1346. public:
  1347. cmFindPackageCommandHoldFile(const char* f)
  1348. : File(f)
  1349. {
  1350. }
  1351. ~cmFindPackageCommandHoldFile()
  1352. {
  1353. if (this->File) {
  1354. cmSystemTools::RemoveFile(this->File);
  1355. }
  1356. }
  1357. cmFindPackageCommandHoldFile(const cmFindPackageCommandHoldFile&) = delete;
  1358. cmFindPackageCommandHoldFile& operator=(
  1359. const cmFindPackageCommandHoldFile&) = delete;
  1360. void Release() { this->File = nullptr; }
  1361. };
  1362. void cmFindPackageCommand::LoadPackageRegistryDir(std::string const& dir,
  1363. cmSearchPath& outPaths)
  1364. {
  1365. cmsys::Directory files;
  1366. if (!files.Load(dir)) {
  1367. return;
  1368. }
  1369. std::string fname;
  1370. for (unsigned long i = 0; i < files.GetNumberOfFiles(); ++i) {
  1371. fname = cmStrCat(dir, '/', files.GetFile(i));
  1372. if (!cmSystemTools::FileIsDirectory(fname)) {
  1373. // Hold this file hostage until it behaves.
  1374. cmFindPackageCommandHoldFile holdFile(fname.c_str());
  1375. // Load the file.
  1376. cmsys::ifstream fin(fname.c_str(), std::ios::in | std::ios::binary);
  1377. std::string fentry;
  1378. if (fin && cmSystemTools::GetLineFromStream(fin, fentry) &&
  1379. this->CheckPackageRegistryEntry(fentry, outPaths)) {
  1380. // The file references an existing package, so release it.
  1381. holdFile.Release();
  1382. }
  1383. }
  1384. }
  1385. // TODO: Wipe out the directory if it is empty.
  1386. }
  1387. #endif
  1388. bool cmFindPackageCommand::CheckPackageRegistryEntry(const std::string& fname,
  1389. cmSearchPath& outPaths)
  1390. {
  1391. // Parse the content of one package registry entry.
  1392. if (cmSystemTools::FileIsFullPath(fname)) {
  1393. // The first line in the stream is the full path to a file or
  1394. // directory containing the package.
  1395. if (cmSystemTools::FileExists(fname)) {
  1396. // The path exists. Look for the package here.
  1397. if (!cmSystemTools::FileIsDirectory(fname)) {
  1398. outPaths.AddPath(cmSystemTools::GetFilenamePath(fname));
  1399. } else {
  1400. outPaths.AddPath(fname);
  1401. }
  1402. return true;
  1403. }
  1404. // The path does not exist. Assume the stream content is
  1405. // associated with an old package that no longer exists, and
  1406. // delete it to keep the package registry clean.
  1407. return false;
  1408. }
  1409. // The first line in the stream is not the full path to a file or
  1410. // directory. Assume the stream content was created by a future
  1411. // version of CMake that uses a different format, and leave it.
  1412. return true;
  1413. }
  1414. void cmFindPackageCommand::FillPrefixesCMakeSystemVariable()
  1415. {
  1416. cmSearchPath& paths = this->LabeledPaths[PathLabel::CMakeSystem];
  1417. paths.AddCMakePath("CMAKE_SYSTEM_PREFIX_PATH");
  1418. paths.AddCMakePath("CMAKE_SYSTEM_FRAMEWORK_PATH");
  1419. paths.AddCMakePath("CMAKE_SYSTEM_APPBUNDLE_PATH");
  1420. if (this->DebugMode) {
  1421. std::string debugBuffer = "CMake variables defined in the Platform file "
  1422. "[CMAKE_FIND_USE_CMAKE_SYSTEM_PATH].\n";
  1423. collectPathsForDebug(debugBuffer, paths);
  1424. this->DebugBuffer = cmStrCat(this->DebugBuffer, debugBuffer, "\n");
  1425. }
  1426. }
  1427. void cmFindPackageCommand::FillPrefixesUserGuess()
  1428. {
  1429. cmSearchPath& paths = this->LabeledPaths[PathLabel::Guess];
  1430. for (std::string const& p : this->UserGuessArgs) {
  1431. paths.AddUserPath(p);
  1432. }
  1433. if (this->DebugMode) {
  1434. std::string debugBuffer =
  1435. "Paths specified by the find_package PATHS option.\n";
  1436. collectPathsForDebug(debugBuffer, paths);
  1437. this->DebugBuffer = cmStrCat(this->DebugBuffer, debugBuffer, "\n");
  1438. }
  1439. }
  1440. void cmFindPackageCommand::FillPrefixesUserHints()
  1441. {
  1442. cmSearchPath& paths = this->LabeledPaths[PathLabel::Hints];
  1443. for (std::string const& p : this->UserHintsArgs) {
  1444. paths.AddUserPath(p);
  1445. }
  1446. if (this->DebugMode) {
  1447. std::string debugBuffer =
  1448. "Paths specified by the find_package HINTS option.\n";
  1449. collectPathsForDebug(debugBuffer, paths);
  1450. this->DebugBuffer = cmStrCat(this->DebugBuffer, debugBuffer, "\n");
  1451. }
  1452. }
  1453. bool cmFindPackageCommand::SearchDirectory(std::string const& dir)
  1454. {
  1455. assert(!dir.empty() && dir.back() == '/');
  1456. // Check each path suffix on this directory.
  1457. for (std::string const& s : this->SearchPathSuffixes) {
  1458. std::string d = dir;
  1459. if (!s.empty()) {
  1460. d += s;
  1461. d += "/";
  1462. }
  1463. if (this->CheckDirectory(d)) {
  1464. return true;
  1465. }
  1466. }
  1467. return false;
  1468. }
  1469. bool cmFindPackageCommand::CheckDirectory(std::string const& dir)
  1470. {
  1471. assert(!dir.empty() && dir.back() == '/');
  1472. // Look for the file in this directory.
  1473. std::string d = dir.substr(0, dir.size() - 1);
  1474. if (this->FindConfigFile(d, this->FileFound)) {
  1475. // Remove duplicate slashes.
  1476. cmSystemTools::ConvertToUnixSlashes(this->FileFound);
  1477. return true;
  1478. }
  1479. return false;
  1480. }
  1481. bool cmFindPackageCommand::FindConfigFile(std::string const& dir,
  1482. std::string& file)
  1483. {
  1484. if (this->IgnoredPaths.count(dir)) {
  1485. return false;
  1486. }
  1487. for (std::string const& c : this->Configs) {
  1488. file = cmStrCat(dir, '/', c);
  1489. if (this->DebugMode) {
  1490. this->DebugBuffer = cmStrCat(this->DebugBuffer, " ", file, "\n");
  1491. }
  1492. if (cmSystemTools::FileExists(file, true) && this->CheckVersion(file)) {
  1493. // Allow resolving symlinks when the config file is found through a link
  1494. if (this->UseRealPath) {
  1495. file = cmSystemTools::GetRealPath(file);
  1496. }
  1497. return true;
  1498. }
  1499. }
  1500. return false;
  1501. }
  1502. bool cmFindPackageCommand::CheckVersion(std::string const& config_file)
  1503. {
  1504. bool result = false; // by default, assume the version is not ok.
  1505. bool haveResult = false;
  1506. std::string version = "unknown";
  1507. // Get the filename without the .cmake extension.
  1508. std::string::size_type pos = config_file.rfind('.');
  1509. std::string version_file_base = config_file.substr(0, pos);
  1510. // Look for foo-config-version.cmake
  1511. std::string version_file = cmStrCat(version_file_base, "-version.cmake");
  1512. if (!haveResult && cmSystemTools::FileExists(version_file, true)) {
  1513. result = this->CheckVersionFile(version_file, version);
  1514. haveResult = true;
  1515. }
  1516. // Look for fooConfigVersion.cmake
  1517. version_file = cmStrCat(version_file_base, "Version.cmake");
  1518. if (!haveResult && cmSystemTools::FileExists(version_file, true)) {
  1519. result = this->CheckVersionFile(version_file, version);
  1520. haveResult = true;
  1521. }
  1522. // If no version was requested a versionless package is acceptable.
  1523. if (!haveResult && this->Version.empty()) {
  1524. result = true;
  1525. }
  1526. ConfigFileInfo configFileInfo;
  1527. configFileInfo.filename = config_file;
  1528. configFileInfo.version = version;
  1529. this->ConsideredConfigs.push_back(std::move(configFileInfo));
  1530. return result;
  1531. }
  1532. bool cmFindPackageCommand::CheckVersionFile(std::string const& version_file,
  1533. std::string& result_version)
  1534. {
  1535. // The version file will be loaded in an isolated scope.
  1536. cmMakefile::ScopePushPop varScope(this->Makefile);
  1537. cmMakefile::PolicyPushPop polScope(this->Makefile);
  1538. static_cast<void>(varScope);
  1539. static_cast<void>(polScope);
  1540. // Clear the output variables.
  1541. this->Makefile->RemoveDefinition("PACKAGE_VERSION");
  1542. this->Makefile->RemoveDefinition("PACKAGE_VERSION_UNSUITABLE");
  1543. this->Makefile->RemoveDefinition("PACKAGE_VERSION_COMPATIBLE");
  1544. this->Makefile->RemoveDefinition("PACKAGE_VERSION_EXACT");
  1545. // Set the input variables.
  1546. this->Makefile->AddDefinition("PACKAGE_FIND_NAME", this->Name);
  1547. this->Makefile->AddDefinition("PACKAGE_FIND_VERSION", this->Version);
  1548. char buf[64];
  1549. sprintf(buf, "%u", this->VersionMajor);
  1550. this->Makefile->AddDefinition("PACKAGE_FIND_VERSION_MAJOR", buf);
  1551. sprintf(buf, "%u", this->VersionMinor);
  1552. this->Makefile->AddDefinition("PACKAGE_FIND_VERSION_MINOR", buf);
  1553. sprintf(buf, "%u", this->VersionPatch);
  1554. this->Makefile->AddDefinition("PACKAGE_FIND_VERSION_PATCH", buf);
  1555. sprintf(buf, "%u", this->VersionTweak);
  1556. this->Makefile->AddDefinition("PACKAGE_FIND_VERSION_TWEAK", buf);
  1557. sprintf(buf, "%u", this->VersionCount);
  1558. this->Makefile->AddDefinition("PACKAGE_FIND_VERSION_COUNT", buf);
  1559. // Load the version check file. Pass NoPolicyScope because we do
  1560. // our own policy push/pop independent of CMP0011.
  1561. bool suitable = false;
  1562. if (this->ReadListFile(version_file, NoPolicyScope)) {
  1563. // Check the output variables.
  1564. bool okay = this->Makefile->IsOn("PACKAGE_VERSION_EXACT");
  1565. bool unsuitable = this->Makefile->IsOn("PACKAGE_VERSION_UNSUITABLE");
  1566. if (!okay && !this->VersionExact) {
  1567. okay = this->Makefile->IsOn("PACKAGE_VERSION_COMPATIBLE");
  1568. }
  1569. // The package is suitable if the version is okay and not
  1570. // explicitly unsuitable.
  1571. suitable = !unsuitable && (okay || this->Version.empty());
  1572. if (suitable) {
  1573. // Get the version found.
  1574. this->VersionFound =
  1575. this->Makefile->GetSafeDefinition("PACKAGE_VERSION");
  1576. // Try to parse the version number and store the results that were
  1577. // successfully parsed.
  1578. unsigned int parsed_major;
  1579. unsigned int parsed_minor;
  1580. unsigned int parsed_patch;
  1581. unsigned int parsed_tweak;
  1582. this->VersionFoundCount =
  1583. sscanf(this->VersionFound.c_str(), "%u.%u.%u.%u", &parsed_major,
  1584. &parsed_minor, &parsed_patch, &parsed_tweak);
  1585. switch (this->VersionFoundCount) {
  1586. case 4:
  1587. this->VersionFoundTweak = parsed_tweak;
  1588. CM_FALLTHROUGH;
  1589. case 3:
  1590. this->VersionFoundPatch = parsed_patch;
  1591. CM_FALLTHROUGH;
  1592. case 2:
  1593. this->VersionFoundMinor = parsed_minor;
  1594. CM_FALLTHROUGH;
  1595. case 1:
  1596. this->VersionFoundMajor = parsed_major;
  1597. CM_FALLTHROUGH;
  1598. default:
  1599. break;
  1600. }
  1601. }
  1602. }
  1603. result_version = this->Makefile->GetSafeDefinition("PACKAGE_VERSION");
  1604. if (result_version.empty()) {
  1605. result_version = "unknown";
  1606. }
  1607. // Succeed if the version is suitable.
  1608. return suitable;
  1609. }
  1610. void cmFindPackageCommand::StoreVersionFound()
  1611. {
  1612. // Store the whole version string.
  1613. std::string ver = cmStrCat(this->Name, "_VERSION");
  1614. if (this->VersionFound.empty()) {
  1615. this->Makefile->RemoveDefinition(ver);
  1616. } else {
  1617. this->Makefile->AddDefinition(ver, this->VersionFound);
  1618. }
  1619. // Store the version components.
  1620. char buf[64];
  1621. sprintf(buf, "%u", this->VersionFoundMajor);
  1622. this->Makefile->AddDefinition(ver + "_MAJOR", buf);
  1623. sprintf(buf, "%u", this->VersionFoundMinor);
  1624. this->Makefile->AddDefinition(ver + "_MINOR", buf);
  1625. sprintf(buf, "%u", this->VersionFoundPatch);
  1626. this->Makefile->AddDefinition(ver + "_PATCH", buf);
  1627. sprintf(buf, "%u", this->VersionFoundTweak);
  1628. this->Makefile->AddDefinition(ver + "_TWEAK", buf);
  1629. sprintf(buf, "%u", this->VersionFoundCount);
  1630. this->Makefile->AddDefinition(ver + "_COUNT", buf);
  1631. }
  1632. class cmFileListGeneratorBase
  1633. {
  1634. public:
  1635. virtual ~cmFileListGeneratorBase() = default;
  1636. protected:
  1637. bool Consider(std::string const& fullPath, cmFileList& listing);
  1638. private:
  1639. bool Search(cmFileList&);
  1640. virtual bool Search(std::string const& parent, cmFileList&) = 0;
  1641. virtual std::unique_ptr<cmFileListGeneratorBase> Clone() const = 0;
  1642. friend class cmFileList;
  1643. cmFileListGeneratorBase* SetNext(cmFileListGeneratorBase const& next);
  1644. std::unique_ptr<cmFileListGeneratorBase> Next;
  1645. };
  1646. class cmFileList
  1647. {
  1648. public:
  1649. virtual ~cmFileList() = default;
  1650. cmFileList& operator/(cmFileListGeneratorBase const& rhs)
  1651. {
  1652. if (this->Last) {
  1653. this->Last = this->Last->SetNext(rhs);
  1654. } else {
  1655. this->First = rhs.Clone();
  1656. this->Last = this->First.get();
  1657. }
  1658. return *this;
  1659. }
  1660. bool Search()
  1661. {
  1662. if (this->First) {
  1663. return this->First->Search(*this);
  1664. }
  1665. return false;
  1666. }
  1667. private:
  1668. virtual bool Visit(std::string const& fullPath) = 0;
  1669. friend class cmFileListGeneratorBase;
  1670. std::unique_ptr<cmFileListGeneratorBase> First;
  1671. cmFileListGeneratorBase* Last = nullptr;
  1672. };
  1673. class cmFindPackageFileList : public cmFileList
  1674. {
  1675. public:
  1676. cmFindPackageFileList(cmFindPackageCommand* fpc, bool use_suffixes = true)
  1677. : FPC(fpc)
  1678. , UseSuffixes(use_suffixes)
  1679. {
  1680. }
  1681. private:
  1682. bool Visit(std::string const& fullPath) override
  1683. {
  1684. if (this->UseSuffixes) {
  1685. return this->FPC->SearchDirectory(fullPath);
  1686. }
  1687. return this->FPC->CheckDirectory(fullPath);
  1688. }
  1689. cmFindPackageCommand* FPC;
  1690. bool UseSuffixes;
  1691. };
  1692. bool cmFileListGeneratorBase::Search(cmFileList& listing)
  1693. {
  1694. return this->Search("", listing);
  1695. }
  1696. cmFileListGeneratorBase* cmFileListGeneratorBase::SetNext(
  1697. cmFileListGeneratorBase const& next)
  1698. {
  1699. this->Next = next.Clone();
  1700. return this->Next.get();
  1701. }
  1702. bool cmFileListGeneratorBase::Consider(std::string const& fullPath,
  1703. cmFileList& listing)
  1704. {
  1705. if (this->Next) {
  1706. return this->Next->Search(fullPath + "/", listing);
  1707. }
  1708. return listing.Visit(fullPath + "/");
  1709. }
  1710. class cmFileListGeneratorFixed : public cmFileListGeneratorBase
  1711. {
  1712. public:
  1713. cmFileListGeneratorFixed(std::string str)
  1714. : String(std::move(str))
  1715. {
  1716. }
  1717. cmFileListGeneratorFixed(cmFileListGeneratorFixed const& r)
  1718. : String(r.String)
  1719. {
  1720. }
  1721. private:
  1722. std::string String;
  1723. bool Search(std::string const& parent, cmFileList& lister) override
  1724. {
  1725. std::string fullPath = parent + this->String;
  1726. return this->Consider(fullPath, lister);
  1727. }
  1728. std::unique_ptr<cmFileListGeneratorBase> Clone() const override
  1729. {
  1730. std::unique_ptr<cmFileListGeneratorBase> g(
  1731. new cmFileListGeneratorFixed(*this));
  1732. return g;
  1733. }
  1734. };
  1735. class cmFileListGeneratorEnumerate : public cmFileListGeneratorBase
  1736. {
  1737. public:
  1738. cmFileListGeneratorEnumerate(std::vector<std::string> const& v)
  1739. : Vector(v)
  1740. {
  1741. }
  1742. cmFileListGeneratorEnumerate(cmFileListGeneratorEnumerate const& r)
  1743. : Vector(r.Vector)
  1744. {
  1745. }
  1746. private:
  1747. std::vector<std::string> const& Vector;
  1748. bool Search(std::string const& parent, cmFileList& lister) override
  1749. {
  1750. for (std::string const& i : this->Vector) {
  1751. if (this->Consider(parent + i, lister)) {
  1752. return true;
  1753. }
  1754. }
  1755. return false;
  1756. }
  1757. std::unique_ptr<cmFileListGeneratorBase> Clone() const override
  1758. {
  1759. std::unique_ptr<cmFileListGeneratorBase> g(
  1760. new cmFileListGeneratorEnumerate(*this));
  1761. return g;
  1762. }
  1763. };
  1764. class cmFileListGeneratorProject : public cmFileListGeneratorBase
  1765. {
  1766. public:
  1767. cmFileListGeneratorProject(std::vector<std::string> const& names,
  1768. cmFindPackageCommand::SortOrderType so,
  1769. cmFindPackageCommand::SortDirectionType sd)
  1770. : Names(names)
  1771. {
  1772. this->SetSort(so, sd);
  1773. }
  1774. cmFileListGeneratorProject(cmFileListGeneratorProject const& r)
  1775. : Names(r.Names)
  1776. {
  1777. this->SetSort(r.SortOrder, r.SortDirection);
  1778. }
  1779. void SetSort(cmFindPackageCommand::SortOrderType o,
  1780. cmFindPackageCommand::SortDirectionType d)
  1781. {
  1782. SortOrder = o;
  1783. SortDirection = d;
  1784. }
  1785. protected:
  1786. // sort parameters
  1787. cmFindPackageCommand::SortOrderType SortOrder;
  1788. cmFindPackageCommand::SortDirectionType SortDirection;
  1789. private:
  1790. std::vector<std::string> const& Names;
  1791. bool Search(std::string const& parent, cmFileList& lister) override
  1792. {
  1793. // Construct a list of matches.
  1794. std::vector<std::string> matches;
  1795. cmsys::Directory d;
  1796. d.Load(parent);
  1797. for (unsigned long i = 0; i < d.GetNumberOfFiles(); ++i) {
  1798. const char* fname = d.GetFile(i);
  1799. if (strcmp(fname, ".") == 0 || strcmp(fname, "..") == 0) {
  1800. continue;
  1801. }
  1802. for (std::string const& n : this->Names) {
  1803. if (cmsysString_strncasecmp(fname, n.c_str(), n.length()) == 0) {
  1804. matches.emplace_back(fname);
  1805. }
  1806. }
  1807. }
  1808. // before testing the matches check if there is a specific sorting order to
  1809. // perform
  1810. if (this->SortOrder != cmFindPackageCommand::None) {
  1811. cmFindPackageCommand::Sort(matches.begin(), matches.end(), SortOrder,
  1812. SortDirection);
  1813. }
  1814. for (std::string const& i : matches) {
  1815. if (this->Consider(parent + i, lister)) {
  1816. return true;
  1817. }
  1818. }
  1819. return false;
  1820. }
  1821. std::unique_ptr<cmFileListGeneratorBase> Clone() const override
  1822. {
  1823. std::unique_ptr<cmFileListGeneratorBase> g(
  1824. new cmFileListGeneratorProject(*this));
  1825. return g;
  1826. }
  1827. };
  1828. class cmFileListGeneratorMacProject : public cmFileListGeneratorBase
  1829. {
  1830. public:
  1831. cmFileListGeneratorMacProject(std::vector<std::string> const& names,
  1832. const char* ext)
  1833. : Names(names)
  1834. , Extension(ext)
  1835. {
  1836. }
  1837. cmFileListGeneratorMacProject(cmFileListGeneratorMacProject const& r)
  1838. : Names(r.Names)
  1839. , Extension(r.Extension)
  1840. {
  1841. }
  1842. private:
  1843. std::vector<std::string> const& Names;
  1844. std::string Extension;
  1845. bool Search(std::string const& parent, cmFileList& lister) override
  1846. {
  1847. // Construct a list of matches.
  1848. std::vector<std::string> matches;
  1849. cmsys::Directory d;
  1850. d.Load(parent);
  1851. for (unsigned long i = 0; i < d.GetNumberOfFiles(); ++i) {
  1852. const char* fname = d.GetFile(i);
  1853. if (strcmp(fname, ".") == 0 || strcmp(fname, "..") == 0) {
  1854. continue;
  1855. }
  1856. for (std::string name : this->Names) {
  1857. name += this->Extension;
  1858. if (cmsysString_strcasecmp(fname, name.c_str()) == 0) {
  1859. matches.emplace_back(fname);
  1860. }
  1861. }
  1862. }
  1863. for (std::string const& i : matches) {
  1864. if (this->Consider(parent + i, lister)) {
  1865. return true;
  1866. }
  1867. }
  1868. return false;
  1869. }
  1870. std::unique_ptr<cmFileListGeneratorBase> Clone() const override
  1871. {
  1872. std::unique_ptr<cmFileListGeneratorBase> g(
  1873. new cmFileListGeneratorMacProject(*this));
  1874. return g;
  1875. }
  1876. };
  1877. class cmFileListGeneratorCaseInsensitive : public cmFileListGeneratorBase
  1878. {
  1879. public:
  1880. cmFileListGeneratorCaseInsensitive(std::string str)
  1881. : String(std::move(str))
  1882. {
  1883. }
  1884. cmFileListGeneratorCaseInsensitive(
  1885. cmFileListGeneratorCaseInsensitive const& r)
  1886. : String(r.String)
  1887. {
  1888. }
  1889. private:
  1890. std::string String;
  1891. bool Search(std::string const& parent, cmFileList& lister) override
  1892. {
  1893. // Look for matching files.
  1894. std::vector<std::string> matches;
  1895. cmsys::Directory d;
  1896. d.Load(parent);
  1897. for (unsigned long i = 0; i < d.GetNumberOfFiles(); ++i) {
  1898. const char* fname = d.GetFile(i);
  1899. if (strcmp(fname, ".") == 0 || strcmp(fname, "..") == 0) {
  1900. continue;
  1901. }
  1902. if (cmsysString_strcasecmp(fname, this->String.c_str()) == 0) {
  1903. if (this->Consider(parent + fname, lister)) {
  1904. return true;
  1905. }
  1906. }
  1907. }
  1908. return false;
  1909. }
  1910. std::unique_ptr<cmFileListGeneratorBase> Clone() const override
  1911. {
  1912. std::unique_ptr<cmFileListGeneratorBase> g(
  1913. new cmFileListGeneratorCaseInsensitive(*this));
  1914. return g;
  1915. }
  1916. };
  1917. class cmFileListGeneratorGlob : public cmFileListGeneratorBase
  1918. {
  1919. public:
  1920. cmFileListGeneratorGlob(std::string str)
  1921. : Pattern(std::move(str))
  1922. {
  1923. }
  1924. cmFileListGeneratorGlob(cmFileListGeneratorGlob const& r)
  1925. : Pattern(r.Pattern)
  1926. {
  1927. }
  1928. private:
  1929. std::string Pattern;
  1930. bool Search(std::string const& parent, cmFileList& lister) override
  1931. {
  1932. // Glob the set of matching files.
  1933. std::string expr = cmStrCat(parent, this->Pattern);
  1934. cmsys::Glob g;
  1935. if (!g.FindFiles(expr)) {
  1936. return false;
  1937. }
  1938. std::vector<std::string> const& files = g.GetFiles();
  1939. // Look for directories among the matches.
  1940. for (std::string const& f : files) {
  1941. if (cmSystemTools::FileIsDirectory(f)) {
  1942. if (this->Consider(f, lister)) {
  1943. return true;
  1944. }
  1945. }
  1946. }
  1947. return false;
  1948. }
  1949. std::unique_ptr<cmFileListGeneratorBase> Clone() const override
  1950. {
  1951. return cm::make_unique<cmFileListGeneratorGlob>(*this);
  1952. }
  1953. };
  1954. bool cmFindPackageCommand::SearchPrefix(std::string const& prefix_in)
  1955. {
  1956. assert(!prefix_in.empty() && prefix_in.back() == '/');
  1957. // Skip this if the prefix does not exist.
  1958. if (!cmSystemTools::FileIsDirectory(prefix_in)) {
  1959. return false;
  1960. }
  1961. // PREFIX/ (useful on windows or in build trees)
  1962. if (this->SearchDirectory(prefix_in)) {
  1963. return true;
  1964. }
  1965. // Strip the trailing slash because the path generator is about to
  1966. // add one.
  1967. std::string prefix = prefix_in.substr(0, prefix_in.size() - 1);
  1968. // PREFIX/(cmake|CMake)/ (useful on windows or in build trees)
  1969. {
  1970. cmFindPackageFileList lister(this);
  1971. lister / cmFileListGeneratorFixed(prefix) /
  1972. cmFileListGeneratorCaseInsensitive("cmake");
  1973. if (lister.Search()) {
  1974. return true;
  1975. }
  1976. }
  1977. // PREFIX/(Foo|foo|FOO).*/
  1978. {
  1979. cmFindPackageFileList lister(this);
  1980. lister / cmFileListGeneratorFixed(prefix) /
  1981. cmFileListGeneratorProject(this->Names, this->SortOrder,
  1982. this->SortDirection);
  1983. if (lister.Search()) {
  1984. return true;
  1985. }
  1986. }
  1987. // PREFIX/(Foo|foo|FOO).*/(cmake|CMake)/
  1988. {
  1989. cmFindPackageFileList lister(this);
  1990. lister / cmFileListGeneratorFixed(prefix) /
  1991. cmFileListGeneratorProject(this->Names, this->SortOrder,
  1992. this->SortDirection) /
  1993. cmFileListGeneratorCaseInsensitive("cmake");
  1994. if (lister.Search()) {
  1995. return true;
  1996. }
  1997. }
  1998. // Construct list of common install locations (lib and share).
  1999. std::vector<std::string> common;
  2000. if (!this->LibraryArchitecture.empty()) {
  2001. common.push_back("lib/" + this->LibraryArchitecture);
  2002. }
  2003. if (this->UseLib32Paths) {
  2004. common.emplace_back("lib32");
  2005. }
  2006. if (this->UseLib64Paths) {
  2007. common.emplace_back("lib64");
  2008. }
  2009. if (this->UseLibx32Paths) {
  2010. common.emplace_back("libx32");
  2011. }
  2012. common.emplace_back("lib");
  2013. common.emplace_back("share");
  2014. // PREFIX/(lib/ARCH|lib*|share)/cmake/(Foo|foo|FOO).*/
  2015. {
  2016. cmFindPackageFileList lister(this);
  2017. lister / cmFileListGeneratorFixed(prefix) /
  2018. cmFileListGeneratorEnumerate(common) /
  2019. cmFileListGeneratorFixed("cmake") /
  2020. cmFileListGeneratorProject(this->Names, this->SortOrder,
  2021. this->SortDirection);
  2022. if (lister.Search()) {
  2023. return true;
  2024. }
  2025. }
  2026. // PREFIX/(lib/ARCH|lib*|share)/(Foo|foo|FOO).*/
  2027. {
  2028. cmFindPackageFileList lister(this);
  2029. lister / cmFileListGeneratorFixed(prefix) /
  2030. cmFileListGeneratorEnumerate(common) /
  2031. cmFileListGeneratorProject(this->Names, this->SortOrder,
  2032. this->SortDirection);
  2033. if (lister.Search()) {
  2034. return true;
  2035. }
  2036. }
  2037. // PREFIX/(lib/ARCH|lib*|share)/(Foo|foo|FOO).*/(cmake|CMake)/
  2038. {
  2039. cmFindPackageFileList lister(this);
  2040. lister / cmFileListGeneratorFixed(prefix) /
  2041. cmFileListGeneratorEnumerate(common) /
  2042. cmFileListGeneratorProject(this->Names, this->SortOrder,
  2043. this->SortDirection) /
  2044. cmFileListGeneratorCaseInsensitive("cmake");
  2045. if (lister.Search()) {
  2046. return true;
  2047. }
  2048. }
  2049. // PREFIX/(Foo|foo|FOO).*/(lib/ARCH|lib*|share)/cmake/(Foo|foo|FOO).*/
  2050. {
  2051. cmFindPackageFileList lister(this);
  2052. lister / cmFileListGeneratorFixed(prefix) /
  2053. cmFileListGeneratorProject(this->Names, this->SortOrder,
  2054. this->SortDirection) /
  2055. cmFileListGeneratorEnumerate(common) /
  2056. cmFileListGeneratorFixed("cmake") /
  2057. cmFileListGeneratorProject(this->Names, this->SortOrder,
  2058. this->SortDirection);
  2059. if (lister.Search()) {
  2060. return true;
  2061. }
  2062. }
  2063. // PREFIX/(Foo|foo|FOO).*/(lib/ARCH|lib*|share)/(Foo|foo|FOO).*/
  2064. {
  2065. cmFindPackageFileList lister(this);
  2066. lister / cmFileListGeneratorFixed(prefix) /
  2067. cmFileListGeneratorProject(this->Names, this->SortOrder,
  2068. this->SortDirection) /
  2069. cmFileListGeneratorEnumerate(common) /
  2070. cmFileListGeneratorProject(this->Names, this->SortOrder,
  2071. this->SortDirection);
  2072. if (lister.Search()) {
  2073. return true;
  2074. }
  2075. }
  2076. // PREFIX/(Foo|foo|FOO).*/(lib/ARCH|lib*|share)/(Foo|foo|FOO).*/(cmake|CMake)/
  2077. {
  2078. cmFindPackageFileList lister(this);
  2079. lister / cmFileListGeneratorFixed(prefix) /
  2080. cmFileListGeneratorProject(this->Names, this->SortOrder,
  2081. this->SortDirection) /
  2082. cmFileListGeneratorEnumerate(common) /
  2083. cmFileListGeneratorProject(this->Names, this->SortOrder,
  2084. this->SortDirection) /
  2085. cmFileListGeneratorCaseInsensitive("cmake");
  2086. if (lister.Search()) {
  2087. return true;
  2088. }
  2089. }
  2090. return false;
  2091. }
  2092. bool cmFindPackageCommand::SearchFrameworkPrefix(std::string const& prefix_in)
  2093. {
  2094. assert(!prefix_in.empty() && prefix_in.back() == '/');
  2095. // Strip the trailing slash because the path generator is about to
  2096. // add one.
  2097. std::string prefix = prefix_in.substr(0, prefix_in.size() - 1);
  2098. // <prefix>/Foo.framework/Resources/
  2099. {
  2100. cmFindPackageFileList lister(this);
  2101. lister / cmFileListGeneratorFixed(prefix) /
  2102. cmFileListGeneratorMacProject(this->Names, ".framework") /
  2103. cmFileListGeneratorFixed("Resources");
  2104. if (lister.Search()) {
  2105. return true;
  2106. }
  2107. }
  2108. // <prefix>/Foo.framework/Resources/CMake/
  2109. {
  2110. cmFindPackageFileList lister(this);
  2111. lister / cmFileListGeneratorFixed(prefix) /
  2112. cmFileListGeneratorMacProject(this->Names, ".framework") /
  2113. cmFileListGeneratorFixed("Resources") /
  2114. cmFileListGeneratorCaseInsensitive("cmake");
  2115. if (lister.Search()) {
  2116. return true;
  2117. }
  2118. }
  2119. // <prefix>/Foo.framework/Versions/*/Resources/
  2120. {
  2121. cmFindPackageFileList lister(this);
  2122. lister / cmFileListGeneratorFixed(prefix) /
  2123. cmFileListGeneratorMacProject(this->Names, ".framework") /
  2124. cmFileListGeneratorFixed("Versions") /
  2125. cmFileListGeneratorGlob("*/Resources");
  2126. if (lister.Search()) {
  2127. return true;
  2128. }
  2129. }
  2130. // <prefix>/Foo.framework/Versions/*/Resources/CMake/
  2131. {
  2132. cmFindPackageFileList lister(this);
  2133. lister / cmFileListGeneratorFixed(prefix) /
  2134. cmFileListGeneratorMacProject(this->Names, ".framework") /
  2135. cmFileListGeneratorFixed("Versions") /
  2136. cmFileListGeneratorGlob("*/Resources") /
  2137. cmFileListGeneratorCaseInsensitive("cmake");
  2138. if (lister.Search()) {
  2139. return true;
  2140. }
  2141. }
  2142. return false;
  2143. }
  2144. bool cmFindPackageCommand::SearchAppBundlePrefix(std::string const& prefix_in)
  2145. {
  2146. assert(!prefix_in.empty() && prefix_in.back() == '/');
  2147. // Strip the trailing slash because the path generator is about to
  2148. // add one.
  2149. std::string prefix = prefix_in.substr(0, prefix_in.size() - 1);
  2150. // <prefix>/Foo.app/Contents/Resources
  2151. {
  2152. cmFindPackageFileList lister(this);
  2153. lister / cmFileListGeneratorFixed(prefix) /
  2154. cmFileListGeneratorMacProject(this->Names, ".app") /
  2155. cmFileListGeneratorFixed("Contents/Resources");
  2156. if (lister.Search()) {
  2157. return true;
  2158. }
  2159. }
  2160. // <prefix>/Foo.app/Contents/Resources/CMake
  2161. {
  2162. cmFindPackageFileList lister(this);
  2163. lister / cmFileListGeneratorFixed(prefix) /
  2164. cmFileListGeneratorMacProject(this->Names, ".app") /
  2165. cmFileListGeneratorFixed("Contents/Resources") /
  2166. cmFileListGeneratorCaseInsensitive("cmake");
  2167. if (lister.Search()) {
  2168. return true;
  2169. }
  2170. }
  2171. return false;
  2172. }
  2173. // TODO: Debug cmsys::Glob double slash problem.
  2174. bool cmFindPackage(std::vector<std::string> const& args,
  2175. cmExecutionStatus& status)
  2176. {
  2177. return cmFindPackageCommand(status).InitialPass(args);
  2178. }