cmFindPackageCommand.cxx 68 KB

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