cmFindPackageCommand.cxx 75 KB

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