cmFindPackageCommand.cxx 68 KB

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