1
0

cmFindPackageCommand.cxx 68 KB

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