cmFindPackageCommand.cxx 68 KB

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