cmFindPackageCommand.cxx 68 KB

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