cmFindPackageCommand.cxx 69 KB

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