cmFindPackageCommand.cxx 69 KB

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