cmFindPackageCommand.cxx 63 KB

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