cmFindPackageCommand.cxx 69 KB

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