cmFindPackageCommand.cxx 64 KB

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