cmFindPackageCommand.cxx 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302
  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->GetState()
  108. ->GetGlobalPropertyAsBool("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. const bool noPolicyScope = !this->PolicyScope || psr == NoPolicyScope;
  908. if(this->Makefile->ReadDependentFile(f, 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 = this->Makefile->GetState()
  923. ->GetGlobalProperty("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->GetState()
  938. ->GetGlobalProperty("PACKAGES_NOT_FOUND");
  939. if (notFoundProp && *notFoundProp)
  940. {
  941. std::string tmp = notFoundProp;
  942. cmSystemTools::ExpandListArgument(tmp, notFoundContents, false);
  943. std::vector<std::string>::iterator nameIt = std::find(
  944. notFoundContents.begin(), notFoundContents.end(), this->Name);
  945. if(nameIt != notFoundContents.end())
  946. {
  947. notFoundContents.erase(nameIt);
  948. }
  949. }
  950. if(found)
  951. {
  952. foundContents.push_back(this->Name);
  953. }
  954. else
  955. {
  956. notFoundContents.push_back(this->Name);
  957. }
  958. std::string tmp = cmJoin(foundContents, ";");
  959. this->Makefile->GetState()
  960. ->SetGlobalProperty("PACKAGES_FOUND", tmp.c_str());
  961. tmp = cmJoin(notFoundContents, ";");
  962. this->Makefile->GetState()
  963. ->SetGlobalProperty("PACKAGES_NOT_FOUND", tmp.c_str());
  964. }
  965. //----------------------------------------------------------------------------
  966. void cmFindPackageCommand::AppendSuccessInformation()
  967. {
  968. {
  969. std::string transitivePropName = "_CMAKE_";
  970. transitivePropName += this->Name + "_TRANSITIVE_DEPENDENCY";
  971. this->Makefile->GetState()
  972. ->SetGlobalProperty(transitivePropName, "False");
  973. }
  974. std::string found = this->Name;
  975. found += "_FOUND";
  976. std::string upperFound = cmSystemTools::UpperCase(found);
  977. const char* upperResult = this->Makefile->GetDefinition(upperFound);
  978. const char* result = this->Makefile->GetDefinition(found);
  979. bool packageFound = ((cmSystemTools::IsOn(result))
  980. || (cmSystemTools::IsOn(upperResult)));
  981. this->AppendToFoundProperty(packageFound);
  982. // Record whether the find was quiet or not, so this can be used
  983. // e.g. in FeatureSummary.cmake
  984. std::string quietInfoPropName = "_CMAKE_";
  985. quietInfoPropName += this->Name;
  986. quietInfoPropName += "_QUIET";
  987. this->Makefile->GetState()
  988. ->SetGlobalProperty(quietInfoPropName, this->Quiet ? "TRUE" : "FALSE");
  989. // set a global property to record the required version of this package
  990. std::string versionInfoPropName = "_CMAKE_";
  991. versionInfoPropName += this->Name;
  992. versionInfoPropName += "_REQUIRED_VERSION";
  993. std::string versionInfo;
  994. if(!this->Version.empty())
  995. {
  996. versionInfo = this->VersionExact ? "==" : ">=";
  997. versionInfo += " ";
  998. versionInfo += this->Version;
  999. }
  1000. this->Makefile->GetState()
  1001. ->SetGlobalProperty(versionInfoPropName, versionInfo.c_str());
  1002. if (this->Required)
  1003. {
  1004. std::string requiredInfoPropName = "_CMAKE_";
  1005. requiredInfoPropName += this->Name;
  1006. requiredInfoPropName += "_TYPE";
  1007. this->Makefile->GetState()
  1008. ->SetGlobalProperty(requiredInfoPropName, "REQUIRED");
  1009. }
  1010. // Restore original state of "_FIND_" variables we set.
  1011. this->RestoreFindDefinitions();
  1012. }
  1013. //----------------------------------------------------------------------------
  1014. void cmFindPackageCommand::ComputePrefixes()
  1015. {
  1016. if(!this->NoDefaultPath)
  1017. {
  1018. if(!this->NoCMakePath)
  1019. {
  1020. this->FillPrefixesCMakeVariable();
  1021. }
  1022. if(!this->NoCMakeEnvironmentPath)
  1023. {
  1024. this->FillPrefixesCMakeEnvironment();
  1025. }
  1026. if(!this->NoSystemEnvironmentPath)
  1027. {
  1028. this->FillPrefixesSystemEnvironment();
  1029. }
  1030. if(!this->NoUserRegistry)
  1031. {
  1032. this->FillPrefixesUserRegistry();
  1033. }
  1034. if(!this->NoBuilds)
  1035. {
  1036. this->FillPrefixesBuilds();
  1037. }
  1038. if(!this->NoCMakeSystemPath)
  1039. {
  1040. this->FillPrefixesCMakeSystemVariable();
  1041. }
  1042. if(!this->NoSystemRegistry)
  1043. {
  1044. this->FillPrefixesSystemRegistry();
  1045. }
  1046. }
  1047. this->FillPrefixesUserHints();
  1048. this->FillPrefixesUserGuess();
  1049. this->ComputeFinalPaths();
  1050. }
  1051. //----------------------------------------------------------------------------
  1052. void cmFindPackageCommand::FillPrefixesCMakeEnvironment()
  1053. {
  1054. cmSearchPath &paths = this->LabeledPaths[PathLabel::CMakeEnvironment];
  1055. // Check the environment variable with the same name as the cache
  1056. // entry.
  1057. paths.AddEnvPath(this->Variable);
  1058. // And now the general CMake environment variables
  1059. paths.AddEnvPath("CMAKE_PREFIX_PATH");
  1060. paths.AddEnvPath("CMAKE_FRAMEWORK_PATH");
  1061. paths.AddEnvPath("CMAKE_APPBUNDLE_PATH");
  1062. }
  1063. //----------------------------------------------------------------------------
  1064. void cmFindPackageCommand::FillPrefixesCMakeVariable()
  1065. {
  1066. cmSearchPath &paths = this->LabeledPaths[PathLabel::CMake];
  1067. paths.AddCMakePath("CMAKE_PREFIX_PATH");
  1068. paths.AddCMakePath("CMAKE_FRAMEWORK_PATH");
  1069. paths.AddCMakePath("CMAKE_APPBUNDLE_PATH");
  1070. }
  1071. //----------------------------------------------------------------------------
  1072. void cmFindPackageCommand::FillPrefixesSystemEnvironment()
  1073. {
  1074. cmSearchPath &paths = this->LabeledPaths[PathLabel::SystemEnvironment];
  1075. // Use the system search path to generate prefixes.
  1076. // Relative paths are interpreted with respect to the current
  1077. // working directory.
  1078. std::vector<std::string> tmp;
  1079. cmSystemTools::GetPath(tmp);
  1080. for(std::vector<std::string>::iterator i = tmp.begin();
  1081. i != tmp.end(); ++i)
  1082. {
  1083. // If the path is a PREFIX/bin case then add its parent instead.
  1084. if((cmHasLiteralSuffix(*i, "/bin")) ||
  1085. (cmHasLiteralSuffix(*i, "/sbin")))
  1086. {
  1087. paths.AddPath(cmSystemTools::GetFilenamePath(*i));
  1088. }
  1089. else
  1090. {
  1091. paths.AddPath(*i);
  1092. }
  1093. }
  1094. }
  1095. //----------------------------------------------------------------------------
  1096. void cmFindPackageCommand::FillPrefixesUserRegistry()
  1097. {
  1098. #if defined(_WIN32) && !defined(__CYGWIN__)
  1099. this->LoadPackageRegistryWinUser();
  1100. #elif defined(__HAIKU__)
  1101. char dir[B_PATH_NAME_LENGTH];
  1102. if (find_directory(B_USER_SETTINGS_DIRECTORY, -1, false, dir, sizeof(dir)) ==
  1103. B_OK)
  1104. {
  1105. std::string fname = dir;
  1106. fname += "/cmake/packages/";
  1107. fname += Name;
  1108. this->LoadPackageRegistryDir(fname,
  1109. this->LabeledPaths[PathLabel::UserRegistry]);
  1110. }
  1111. #else
  1112. if(const char* home = cmSystemTools::GetEnv("HOME"))
  1113. {
  1114. std::string dir = home;
  1115. dir += "/.cmake/packages/";
  1116. dir += this->Name;
  1117. this->LoadPackageRegistryDir(dir,
  1118. this->LabeledPaths[PathLabel::UserRegistry]);
  1119. }
  1120. #endif
  1121. }
  1122. //----------------------------------------------------------------------------
  1123. void cmFindPackageCommand::FillPrefixesSystemRegistry()
  1124. {
  1125. if(this->NoSystemRegistry || this->NoDefaultPath)
  1126. {
  1127. return;
  1128. }
  1129. #if defined(_WIN32) && !defined(__CYGWIN__)
  1130. this->LoadPackageRegistryWinSystem();
  1131. #endif
  1132. }
  1133. #if defined(_WIN32) && !defined(__CYGWIN__)
  1134. # include <windows.h>
  1135. # undef GetCurrentDirectory
  1136. // http://msdn.microsoft.com/en-us/library/aa384253%28v=vs.85%29.aspx
  1137. # if !defined(KEY_WOW64_32KEY)
  1138. # define KEY_WOW64_32KEY 0x0200
  1139. # endif
  1140. # if !defined(KEY_WOW64_64KEY)
  1141. # define KEY_WOW64_64KEY 0x0100
  1142. # endif
  1143. //----------------------------------------------------------------------------
  1144. void cmFindPackageCommand::LoadPackageRegistryWinUser()
  1145. {
  1146. // HKEY_CURRENT_USER\\Software shares 32-bit and 64-bit views.
  1147. this->LoadPackageRegistryWin(true, 0,
  1148. this->LabeledPaths[PathLabel::UserRegistry]);
  1149. }
  1150. //----------------------------------------------------------------------------
  1151. void cmFindPackageCommand::LoadPackageRegistryWinSystem()
  1152. {
  1153. cmSearchPath &paths = this->LabeledPaths[PathLabel::SystemRegistry];
  1154. // HKEY_LOCAL_MACHINE\\SOFTWARE has separate 32-bit and 64-bit views.
  1155. // Prefer the target platform view first.
  1156. if(this->Makefile->PlatformIs64Bit())
  1157. {
  1158. this->LoadPackageRegistryWin(false, KEY_WOW64_64KEY, paths);
  1159. this->LoadPackageRegistryWin(false, KEY_WOW64_32KEY, paths);
  1160. }
  1161. else
  1162. {
  1163. this->LoadPackageRegistryWin(false, KEY_WOW64_32KEY, paths);
  1164. this->LoadPackageRegistryWin(false, KEY_WOW64_64KEY, paths);
  1165. }
  1166. }
  1167. //----------------------------------------------------------------------------
  1168. void cmFindPackageCommand::LoadPackageRegistryWin(bool user, unsigned int view,
  1169. cmSearchPath& outPaths)
  1170. {
  1171. std::wstring key = L"Software\\Kitware\\CMake\\Packages\\";
  1172. key += cmsys::Encoding::ToWide(this->Name);
  1173. std::set<std::wstring> bad;
  1174. HKEY hKey;
  1175. if(RegOpenKeyExW(user? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE, key.c_str(),
  1176. 0, KEY_QUERY_VALUE|view, &hKey) == ERROR_SUCCESS)
  1177. {
  1178. DWORD valueType = REG_NONE;
  1179. wchar_t name[16383]; // RegEnumValue docs limit name to 32767 _bytes_
  1180. std::vector<wchar_t> data(512);
  1181. bool done = false;
  1182. DWORD index = 0;
  1183. while(!done)
  1184. {
  1185. DWORD nameSize = static_cast<DWORD>(sizeof(name));
  1186. DWORD dataSize = static_cast<DWORD>(data.size()*sizeof(data[0]));
  1187. switch(RegEnumValueW(hKey, index, name, &nameSize,
  1188. 0, &valueType, (BYTE*)&data[0], &dataSize))
  1189. {
  1190. case ERROR_SUCCESS:
  1191. ++index;
  1192. if(valueType == REG_SZ)
  1193. {
  1194. data[dataSize] = 0;
  1195. if(!this->CheckPackageRegistryEntry(
  1196. cmsys::Encoding::ToNarrow(&data[0]), outPaths))
  1197. {
  1198. // The entry is invalid.
  1199. bad.insert(name);
  1200. }
  1201. }
  1202. break;
  1203. case ERROR_MORE_DATA:
  1204. data.resize((dataSize+sizeof(data[0])-1)/sizeof(data[0]));
  1205. break;
  1206. case ERROR_NO_MORE_ITEMS: default: done = true; break;
  1207. }
  1208. }
  1209. RegCloseKey(hKey);
  1210. }
  1211. // Remove bad values if possible.
  1212. if(user && !bad.empty() &&
  1213. RegOpenKeyExW(HKEY_CURRENT_USER, key.c_str(),
  1214. 0, KEY_SET_VALUE|view, &hKey) == ERROR_SUCCESS)
  1215. {
  1216. for(std::set<std::wstring>::const_iterator vi = bad.begin();
  1217. vi != bad.end(); ++vi)
  1218. {
  1219. RegDeleteValueW(hKey, vi->c_str());
  1220. }
  1221. RegCloseKey(hKey);
  1222. }
  1223. }
  1224. #else
  1225. //----------------------------------------------------------------------------
  1226. class cmFindPackageCommandHoldFile
  1227. {
  1228. const char* File;
  1229. public:
  1230. cmFindPackageCommandHoldFile(const char* f): File(f) {}
  1231. ~cmFindPackageCommandHoldFile()
  1232. { if(this->File) { cmSystemTools::RemoveFile(this->File); } }
  1233. void Release() { this->File = 0; }
  1234. };
  1235. //----------------------------------------------------------------------------
  1236. void cmFindPackageCommand::LoadPackageRegistryDir(std::string const& dir,
  1237. cmSearchPath& outPaths)
  1238. {
  1239. cmsys::Directory files;
  1240. if(!files.Load(dir))
  1241. {
  1242. return;
  1243. }
  1244. std::string fname;
  1245. for(unsigned long i=0; i < files.GetNumberOfFiles(); ++i)
  1246. {
  1247. fname = dir;
  1248. fname += "/";
  1249. fname += files.GetFile(i);
  1250. if(!cmSystemTools::FileIsDirectory(fname))
  1251. {
  1252. // Hold this file hostage until it behaves.
  1253. cmFindPackageCommandHoldFile holdFile(fname.c_str());
  1254. // Load the file.
  1255. cmsys::ifstream fin(fname.c_str(), std::ios::in | cmsys_ios_binary);
  1256. std::string fentry;
  1257. if(fin && cmSystemTools::GetLineFromStream(fin, fentry) &&
  1258. this->CheckPackageRegistryEntry(fentry, outPaths))
  1259. {
  1260. // The file references an existing package, so release it.
  1261. holdFile.Release();
  1262. }
  1263. }
  1264. }
  1265. // TODO: Wipe out the directory if it is empty.
  1266. }
  1267. #endif
  1268. //----------------------------------------------------------------------------
  1269. bool cmFindPackageCommand::CheckPackageRegistryEntry(const std::string& fname,
  1270. cmSearchPath& outPaths)
  1271. {
  1272. // Parse the content of one package registry entry.
  1273. if(cmSystemTools::FileIsFullPath(fname.c_str()))
  1274. {
  1275. // The first line in the stream is the full path to a file or
  1276. // directory containing the package.
  1277. if(cmSystemTools::FileExists(fname.c_str()))
  1278. {
  1279. // The path exists. Look for the package here.
  1280. if(!cmSystemTools::FileIsDirectory(fname))
  1281. {
  1282. outPaths.AddPath(cmSystemTools::GetFilenamePath(fname));
  1283. }
  1284. else
  1285. {
  1286. outPaths.AddPath(fname);
  1287. }
  1288. return true;
  1289. }
  1290. else
  1291. {
  1292. // The path does not exist. Assume the stream content is
  1293. // associated with an old package that no longer exists, and
  1294. // delete it to keep the package registry clean.
  1295. return false;
  1296. }
  1297. }
  1298. else
  1299. {
  1300. // The first line in the stream is not the full path to a file or
  1301. // directory. Assume the stream content was created by a future
  1302. // version of CMake that uses a different format, and leave it.
  1303. return true;
  1304. }
  1305. }
  1306. //----------------------------------------------------------------------------
  1307. void cmFindPackageCommand::FillPrefixesBuilds()
  1308. {
  1309. cmSearchPath &paths = this->LabeledPaths[PathLabel::Builds];
  1310. // It is likely that CMake will have recently built the project.
  1311. for(int i=0; i <= 10; ++i)
  1312. {
  1313. std::ostringstream r;
  1314. r <<
  1315. "[HKEY_CURRENT_USER\\Software\\Kitware\\CMakeSetup\\"
  1316. "Settings\\StartPath;WhereBuild" << i << "]";
  1317. std::string f = r.str();
  1318. cmSystemTools::ExpandRegistryValues(f);
  1319. cmSystemTools::ConvertToUnixSlashes(f);
  1320. if(cmSystemTools::FileIsFullPath(f.c_str()) &&
  1321. cmSystemTools::FileIsDirectory(f))
  1322. {
  1323. paths.AddPath(f);
  1324. }
  1325. }
  1326. }
  1327. //----------------------------------------------------------------------------
  1328. void cmFindPackageCommand::FillPrefixesCMakeSystemVariable()
  1329. {
  1330. cmSearchPath &paths = this->LabeledPaths[PathLabel::CMakeSystem];
  1331. paths.AddCMakePath("CMAKE_SYSTEM_PREFIX_PATH");
  1332. paths.AddCMakePath("CMAKE_SYSTEM_FRAMEWORK_PATH");
  1333. paths.AddCMakePath("CMAKE_SYSTEM_APPBUNDLE_PATH");
  1334. }
  1335. //----------------------------------------------------------------------------
  1336. void cmFindPackageCommand::FillPrefixesUserGuess()
  1337. {
  1338. cmSearchPath &paths = this->LabeledPaths[PathLabel::Guess];
  1339. for(std::vector<std::string>::const_iterator p = this->UserGuessArgs.begin();
  1340. p != this->UserGuessArgs.end(); ++p)
  1341. {
  1342. paths.AddUserPath(*p);
  1343. }
  1344. }
  1345. //----------------------------------------------------------------------------
  1346. void cmFindPackageCommand::FillPrefixesUserHints()
  1347. {
  1348. cmSearchPath &paths = this->LabeledPaths[PathLabel::Hints];
  1349. for(std::vector<std::string>::const_iterator p = this->UserHintsArgs.begin();
  1350. p != this->UserHintsArgs.end(); ++p)
  1351. {
  1352. paths.AddUserPath(*p);
  1353. }
  1354. }
  1355. //----------------------------------------------------------------------------
  1356. bool cmFindPackageCommand::SearchDirectory(std::string const& dir)
  1357. {
  1358. assert(!dir.empty() && dir[dir.size()-1] == '/');
  1359. // Check each path suffix on this directory.
  1360. for(std::vector<std::string>::const_iterator
  1361. si = this->SearchPathSuffixes.begin();
  1362. si != this->SearchPathSuffixes.end(); ++si)
  1363. {
  1364. std::string d = dir;
  1365. if(!si->empty())
  1366. {
  1367. d += *si;
  1368. d += "/";
  1369. }
  1370. if(this->CheckDirectory(d))
  1371. {
  1372. return true;
  1373. }
  1374. }
  1375. return false;
  1376. }
  1377. //----------------------------------------------------------------------------
  1378. bool cmFindPackageCommand::CheckDirectory(std::string const& dir)
  1379. {
  1380. assert(!dir.empty() && dir[dir.size()-1] == '/');
  1381. // Look for the file in this directory.
  1382. std::string d = dir.substr(0, dir.size()-1);
  1383. if(this->FindConfigFile(d, this->FileFound))
  1384. {
  1385. // Remove duplicate slashes.
  1386. cmSystemTools::ConvertToUnixSlashes(this->FileFound);
  1387. return true;
  1388. }
  1389. return false;
  1390. }
  1391. //----------------------------------------------------------------------------
  1392. bool cmFindPackageCommand::FindConfigFile(std::string const& dir,
  1393. std::string& file)
  1394. {
  1395. if (this->IgnoredPaths.count(dir))
  1396. {
  1397. return false;
  1398. }
  1399. for(std::vector<std::string>::const_iterator ci = this->Configs.begin();
  1400. ci != this->Configs.end(); ++ci)
  1401. {
  1402. file = dir;
  1403. file += "/";
  1404. file += *ci;
  1405. if(this->DebugMode)
  1406. {
  1407. fprintf(stderr, "Checking file [%s]\n", file.c_str());
  1408. }
  1409. if(cmSystemTools::FileExists(file.c_str(), true) &&
  1410. this->CheckVersion(file))
  1411. {
  1412. return true;
  1413. }
  1414. }
  1415. return false;
  1416. }
  1417. //----------------------------------------------------------------------------
  1418. bool cmFindPackageCommand::CheckVersion(std::string const& config_file)
  1419. {
  1420. bool result = false; // by default, assume the version is not ok.
  1421. bool haveResult = false;
  1422. std::string version = "unknown";
  1423. // Get the filename without the .cmake extension.
  1424. std::string::size_type pos = config_file.rfind('.');
  1425. std::string version_file_base = config_file.substr(0, pos);
  1426. // Look for foo-config-version.cmake
  1427. std::string version_file = version_file_base;
  1428. version_file += "-version.cmake";
  1429. if ((haveResult == false)
  1430. && (cmSystemTools::FileExists(version_file.c_str(), true)))
  1431. {
  1432. result = this->CheckVersionFile(version_file, version);
  1433. haveResult = true;
  1434. }
  1435. // Look for fooConfigVersion.cmake
  1436. version_file = version_file_base;
  1437. version_file += "Version.cmake";
  1438. if ((haveResult == false)
  1439. && (cmSystemTools::FileExists(version_file.c_str(), true)))
  1440. {
  1441. result = this->CheckVersionFile(version_file, version);
  1442. haveResult = true;
  1443. }
  1444. // If no version was requested a versionless package is acceptable.
  1445. if ((haveResult == false) && (this->Version.empty()))
  1446. {
  1447. result = true;
  1448. }
  1449. ConfigFileInfo configFileInfo;
  1450. configFileInfo.filename = config_file;
  1451. configFileInfo.version = version;
  1452. this->ConsideredConfigs.push_back(configFileInfo);
  1453. return result;
  1454. }
  1455. //----------------------------------------------------------------------------
  1456. bool cmFindPackageCommand::CheckVersionFile(std::string const& version_file,
  1457. std::string& result_version)
  1458. {
  1459. // The version file will be loaded in an isolated scope.
  1460. cmMakefile::ScopePushPop varScope(this->Makefile);
  1461. cmMakefile::PolicyPushPop polScope(this->Makefile);
  1462. static_cast<void>(varScope);
  1463. static_cast<void>(polScope);
  1464. // Clear the output variables.
  1465. this->Makefile->RemoveDefinition("PACKAGE_VERSION");
  1466. this->Makefile->RemoveDefinition("PACKAGE_VERSION_UNSUITABLE");
  1467. this->Makefile->RemoveDefinition("PACKAGE_VERSION_COMPATIBLE");
  1468. this->Makefile->RemoveDefinition("PACKAGE_VERSION_EXACT");
  1469. // Set the input variables.
  1470. this->Makefile->AddDefinition("PACKAGE_FIND_NAME", this->Name.c_str());
  1471. this->Makefile->AddDefinition("PACKAGE_FIND_VERSION",
  1472. this->Version.c_str());
  1473. char buf[64];
  1474. sprintf(buf, "%u", this->VersionMajor);
  1475. this->Makefile->AddDefinition("PACKAGE_FIND_VERSION_MAJOR", buf);
  1476. sprintf(buf, "%u", this->VersionMinor);
  1477. this->Makefile->AddDefinition("PACKAGE_FIND_VERSION_MINOR", buf);
  1478. sprintf(buf, "%u", this->VersionPatch);
  1479. this->Makefile->AddDefinition("PACKAGE_FIND_VERSION_PATCH", buf);
  1480. sprintf(buf, "%u", this->VersionTweak);
  1481. this->Makefile->AddDefinition("PACKAGE_FIND_VERSION_TWEAK", buf);
  1482. sprintf(buf, "%u", this->VersionCount);
  1483. this->Makefile->AddDefinition("PACKAGE_FIND_VERSION_COUNT", buf);
  1484. // Load the version check file. Pass NoPolicyScope because we do
  1485. // our own policy push/pop independent of CMP0011.
  1486. bool suitable = false;
  1487. if(this->ReadListFile(version_file.c_str(), NoPolicyScope))
  1488. {
  1489. // Check the output variables.
  1490. bool okay = this->Makefile->IsOn("PACKAGE_VERSION_EXACT");
  1491. bool unsuitable = this->Makefile->IsOn("PACKAGE_VERSION_UNSUITABLE");
  1492. if(!okay && !this->VersionExact)
  1493. {
  1494. okay = this->Makefile->IsOn("PACKAGE_VERSION_COMPATIBLE");
  1495. }
  1496. // The package is suitable if the version is okay and not
  1497. // explicitly unsuitable.
  1498. suitable = !unsuitable && (okay || this->Version.empty());
  1499. if(suitable)
  1500. {
  1501. // Get the version found.
  1502. this->VersionFound =
  1503. this->Makefile->GetSafeDefinition("PACKAGE_VERSION");
  1504. // Try to parse the version number and store the results that were
  1505. // successfully parsed.
  1506. unsigned int parsed_major;
  1507. unsigned int parsed_minor;
  1508. unsigned int parsed_patch;
  1509. unsigned int parsed_tweak;
  1510. this->VersionFoundCount =
  1511. sscanf(this->VersionFound.c_str(), "%u.%u.%u.%u",
  1512. &parsed_major, &parsed_minor,
  1513. &parsed_patch, &parsed_tweak);
  1514. switch(this->VersionFoundCount)
  1515. {
  1516. case 4: this->VersionFoundTweak = parsed_tweak; // no break!
  1517. case 3: this->VersionFoundPatch = parsed_patch; // no break!
  1518. case 2: this->VersionFoundMinor = parsed_minor; // no break!
  1519. case 1: this->VersionFoundMajor = parsed_major; // no break!
  1520. default: break;
  1521. }
  1522. }
  1523. }
  1524. result_version = this->Makefile->GetSafeDefinition("PACKAGE_VERSION");
  1525. if (result_version.empty())
  1526. {
  1527. result_version = "unknown";
  1528. }
  1529. // Succeed if the version is suitable.
  1530. return suitable;
  1531. }
  1532. //----------------------------------------------------------------------------
  1533. void cmFindPackageCommand::StoreVersionFound()
  1534. {
  1535. // Store the whole version string.
  1536. std::string ver = this->Name;
  1537. ver += "_VERSION";
  1538. if(this->VersionFound.empty())
  1539. {
  1540. this->Makefile->RemoveDefinition(ver);
  1541. }
  1542. else
  1543. {
  1544. this->Makefile->AddDefinition(ver, this->VersionFound.c_str());
  1545. }
  1546. // Store the version components.
  1547. char buf[64];
  1548. sprintf(buf, "%u", this->VersionFoundMajor);
  1549. this->Makefile->AddDefinition(ver+"_MAJOR", buf);
  1550. sprintf(buf, "%u", this->VersionFoundMinor);
  1551. this->Makefile->AddDefinition(ver+"_MINOR", buf);
  1552. sprintf(buf, "%u", this->VersionFoundPatch);
  1553. this->Makefile->AddDefinition(ver+"_PATCH", buf);
  1554. sprintf(buf, "%u", this->VersionFoundTweak);
  1555. this->Makefile->AddDefinition(ver+"_TWEAK", buf);
  1556. sprintf(buf, "%u", this->VersionFoundCount);
  1557. this->Makefile->AddDefinition(ver+"_COUNT", buf);
  1558. }
  1559. //----------------------------------------------------------------------------
  1560. #include <cmsys/Glob.hxx>
  1561. #include <cmsys/String.h>
  1562. #include <cmsys/auto_ptr.hxx>
  1563. class cmFileList;
  1564. class cmFileListGeneratorBase
  1565. {
  1566. public:
  1567. virtual ~cmFileListGeneratorBase() {}
  1568. protected:
  1569. bool Consider(std::string const& fullPath, cmFileList& listing);
  1570. private:
  1571. bool Search(cmFileList&);
  1572. virtual bool Search(std::string const& parent, cmFileList&) = 0;
  1573. virtual cmsys::auto_ptr<cmFileListGeneratorBase> Clone() const = 0;
  1574. friend class cmFileList;
  1575. cmFileListGeneratorBase* SetNext(cmFileListGeneratorBase const& next);
  1576. cmsys::auto_ptr<cmFileListGeneratorBase> Next;
  1577. };
  1578. class cmFileList
  1579. {
  1580. public:
  1581. cmFileList(): First(), Last(0) {}
  1582. virtual ~cmFileList() {}
  1583. cmFileList& operator/(cmFileListGeneratorBase const& rhs)
  1584. {
  1585. if(this->Last)
  1586. {
  1587. this->Last = this->Last->SetNext(rhs);
  1588. }
  1589. else
  1590. {
  1591. this->First = rhs.Clone();
  1592. this->Last = this->First.get();
  1593. }
  1594. return *this;
  1595. }
  1596. bool Search()
  1597. {
  1598. if(this->First.get())
  1599. {
  1600. return this->First->Search(*this);
  1601. }
  1602. return false;
  1603. }
  1604. private:
  1605. virtual bool Visit(std::string const& fullPath) = 0;
  1606. friend class cmFileListGeneratorBase;
  1607. cmsys::auto_ptr<cmFileListGeneratorBase> First;
  1608. cmFileListGeneratorBase* Last;
  1609. };
  1610. class cmFindPackageFileList: public cmFileList
  1611. {
  1612. public:
  1613. cmFindPackageFileList(cmFindPackageCommand* fpc,
  1614. bool use_suffixes = true):
  1615. cmFileList(), FPC(fpc), UseSuffixes(use_suffixes) {}
  1616. private:
  1617. bool Visit(std::string const& fullPath)
  1618. {
  1619. if(this->UseSuffixes)
  1620. {
  1621. return this->FPC->SearchDirectory(fullPath);
  1622. }
  1623. else
  1624. {
  1625. return this->FPC->CheckDirectory(fullPath);
  1626. }
  1627. }
  1628. cmFindPackageCommand* FPC;
  1629. bool UseSuffixes;
  1630. };
  1631. bool cmFileListGeneratorBase::Search(cmFileList& listing)
  1632. {
  1633. return this->Search("", listing);
  1634. }
  1635. cmFileListGeneratorBase*
  1636. cmFileListGeneratorBase::SetNext(cmFileListGeneratorBase const& next)
  1637. {
  1638. this->Next = next.Clone();
  1639. return this->Next.get();
  1640. }
  1641. bool cmFileListGeneratorBase::Consider(std::string const& fullPath,
  1642. cmFileList& listing)
  1643. {
  1644. if(this->Next.get())
  1645. {
  1646. return this->Next->Search(fullPath + "/", listing);
  1647. }
  1648. else
  1649. {
  1650. return listing.Visit(fullPath + "/");
  1651. }
  1652. }
  1653. class cmFileListGeneratorFixed: public cmFileListGeneratorBase
  1654. {
  1655. public:
  1656. cmFileListGeneratorFixed(std::string const& str):
  1657. cmFileListGeneratorBase(), String(str) {}
  1658. cmFileListGeneratorFixed(cmFileListGeneratorFixed const& r):
  1659. cmFileListGeneratorBase(), String(r.String) {}
  1660. private:
  1661. std::string String;
  1662. virtual bool Search(std::string const& parent, cmFileList& lister)
  1663. {
  1664. std::string fullPath = parent + this->String;
  1665. return this->Consider(fullPath, lister);
  1666. }
  1667. virtual cmsys::auto_ptr<cmFileListGeneratorBase> Clone() const
  1668. {
  1669. cmsys::auto_ptr<cmFileListGeneratorBase>
  1670. g(new cmFileListGeneratorFixed(*this));
  1671. return g;
  1672. }
  1673. };
  1674. class cmFileListGeneratorEnumerate: public cmFileListGeneratorBase
  1675. {
  1676. public:
  1677. cmFileListGeneratorEnumerate(std::vector<std::string> const& v):
  1678. cmFileListGeneratorBase(), Vector(v) {}
  1679. cmFileListGeneratorEnumerate(cmFileListGeneratorEnumerate const& r):
  1680. cmFileListGeneratorBase(), Vector(r.Vector) {}
  1681. private:
  1682. std::vector<std::string> const& Vector;
  1683. virtual bool Search(std::string const& parent, cmFileList& lister)
  1684. {
  1685. for(std::vector<std::string>::const_iterator i = this->Vector.begin();
  1686. i != this->Vector.end(); ++i)
  1687. {
  1688. if(this->Consider(parent + *i, lister))
  1689. {
  1690. return true;
  1691. }
  1692. }
  1693. return false;
  1694. }
  1695. virtual cmsys::auto_ptr<cmFileListGeneratorBase> Clone() const
  1696. {
  1697. cmsys::auto_ptr<cmFileListGeneratorBase>
  1698. g(new cmFileListGeneratorEnumerate(*this));
  1699. return g;
  1700. }
  1701. };
  1702. class cmFileListGeneratorProject: public cmFileListGeneratorBase
  1703. {
  1704. public:
  1705. cmFileListGeneratorProject(std::vector<std::string> const& names):
  1706. cmFileListGeneratorBase(), Names(names) {}
  1707. cmFileListGeneratorProject(cmFileListGeneratorProject const& r):
  1708. cmFileListGeneratorBase(), Names(r.Names) {}
  1709. private:
  1710. std::vector<std::string> const& Names;
  1711. virtual bool Search(std::string const& parent, cmFileList& lister)
  1712. {
  1713. // Construct a list of matches.
  1714. std::vector<std::string> matches;
  1715. cmsys::Directory d;
  1716. d.Load(parent);
  1717. for(unsigned long i=0; i < d.GetNumberOfFiles(); ++i)
  1718. {
  1719. const char* fname = d.GetFile(i);
  1720. if(strcmp(fname, ".") == 0 ||
  1721. strcmp(fname, "..") == 0)
  1722. {
  1723. continue;
  1724. }
  1725. for(std::vector<std::string>::const_iterator ni = this->Names.begin();
  1726. ni != this->Names.end(); ++ni)
  1727. {
  1728. if(cmsysString_strncasecmp(fname, ni->c_str(),
  1729. ni->length()) == 0)
  1730. {
  1731. matches.push_back(fname);
  1732. }
  1733. }
  1734. }
  1735. for(std::vector<std::string>::const_iterator i = matches.begin();
  1736. i != matches.end(); ++i)
  1737. {
  1738. if(this->Consider(parent + *i, lister))
  1739. {
  1740. return true;
  1741. }
  1742. }
  1743. return false;
  1744. }
  1745. virtual cmsys::auto_ptr<cmFileListGeneratorBase> Clone() const
  1746. {
  1747. cmsys::auto_ptr<cmFileListGeneratorBase>
  1748. g(new cmFileListGeneratorProject(*this));
  1749. return g;
  1750. }
  1751. };
  1752. class cmFileListGeneratorMacProject: public cmFileListGeneratorBase
  1753. {
  1754. public:
  1755. cmFileListGeneratorMacProject(std::vector<std::string> const& names,
  1756. const char* ext):
  1757. cmFileListGeneratorBase(), Names(names), Extension(ext) {}
  1758. cmFileListGeneratorMacProject(cmFileListGeneratorMacProject const& r):
  1759. cmFileListGeneratorBase(), Names(r.Names), Extension(r.Extension) {}
  1760. private:
  1761. std::vector<std::string> const& Names;
  1762. std::string Extension;
  1763. virtual bool Search(std::string const& parent, cmFileList& lister)
  1764. {
  1765. // Construct a list of matches.
  1766. std::vector<std::string> matches;
  1767. cmsys::Directory d;
  1768. d.Load(parent);
  1769. for(unsigned long i=0; i < d.GetNumberOfFiles(); ++i)
  1770. {
  1771. const char* fname = d.GetFile(i);
  1772. if(strcmp(fname, ".") == 0 ||
  1773. strcmp(fname, "..") == 0)
  1774. {
  1775. continue;
  1776. }
  1777. for(std::vector<std::string>::const_iterator ni = this->Names.begin();
  1778. ni != this->Names.end(); ++ni)
  1779. {
  1780. std::string name = *ni;
  1781. name += this->Extension;
  1782. if(cmsysString_strcasecmp(fname, name.c_str()) == 0)
  1783. {
  1784. matches.push_back(fname);
  1785. }
  1786. }
  1787. }
  1788. for(std::vector<std::string>::const_iterator i = matches.begin();
  1789. i != matches.end(); ++i)
  1790. {
  1791. if(this->Consider(parent + *i, lister))
  1792. {
  1793. return true;
  1794. }
  1795. }
  1796. return false;
  1797. }
  1798. virtual cmsys::auto_ptr<cmFileListGeneratorBase> Clone() const
  1799. {
  1800. cmsys::auto_ptr<cmFileListGeneratorBase>
  1801. g(new cmFileListGeneratorMacProject(*this));
  1802. return g;
  1803. }
  1804. };
  1805. class cmFileListGeneratorCaseInsensitive: public cmFileListGeneratorBase
  1806. {
  1807. public:
  1808. cmFileListGeneratorCaseInsensitive(std::string const& str):
  1809. cmFileListGeneratorBase(), String(str) {}
  1810. cmFileListGeneratorCaseInsensitive(
  1811. cmFileListGeneratorCaseInsensitive const& r):
  1812. cmFileListGeneratorBase(), String(r.String) {}
  1813. private:
  1814. std::string String;
  1815. virtual bool Search(std::string const& parent, cmFileList& lister)
  1816. {
  1817. // Look for matching files.
  1818. std::vector<std::string> matches;
  1819. cmsys::Directory d;
  1820. d.Load(parent);
  1821. for(unsigned long i=0; i < d.GetNumberOfFiles(); ++i)
  1822. {
  1823. const char* fname = d.GetFile(i);
  1824. if(strcmp(fname, ".") == 0 ||
  1825. strcmp(fname, "..") == 0)
  1826. {
  1827. continue;
  1828. }
  1829. if(cmsysString_strcasecmp(fname, this->String.c_str()) == 0)
  1830. {
  1831. if(this->Consider(parent + fname, lister))
  1832. {
  1833. return true;
  1834. }
  1835. }
  1836. }
  1837. return false;
  1838. }
  1839. virtual cmsys::auto_ptr<cmFileListGeneratorBase> Clone() const
  1840. {
  1841. cmsys::auto_ptr<cmFileListGeneratorBase>
  1842. g(new cmFileListGeneratorCaseInsensitive(*this));
  1843. return g;
  1844. }
  1845. };
  1846. class cmFileListGeneratorGlob: public cmFileListGeneratorBase
  1847. {
  1848. public:
  1849. cmFileListGeneratorGlob(std::string const& str):
  1850. cmFileListGeneratorBase(), Pattern(str) {}
  1851. cmFileListGeneratorGlob(cmFileListGeneratorGlob const& r):
  1852. cmFileListGeneratorBase(), Pattern(r.Pattern) {}
  1853. private:
  1854. std::string Pattern;
  1855. virtual bool Search(std::string const& parent, cmFileList& lister)
  1856. {
  1857. // Glob the set of matching files.
  1858. std::string expr = parent;
  1859. expr += this->Pattern;
  1860. cmsys::Glob g;
  1861. if(!g.FindFiles(expr))
  1862. {
  1863. return false;
  1864. }
  1865. std::vector<std::string> const& files = g.GetFiles();
  1866. // Look for directories among the matches.
  1867. for(std::vector<std::string>::const_iterator fi = files.begin();
  1868. fi != files.end(); ++fi)
  1869. {
  1870. if(cmSystemTools::FileIsDirectory(*fi))
  1871. {
  1872. if(this->Consider(*fi, lister))
  1873. {
  1874. return true;
  1875. }
  1876. }
  1877. }
  1878. return false;
  1879. }
  1880. virtual cmsys::auto_ptr<cmFileListGeneratorBase> Clone() const
  1881. {
  1882. cmsys::auto_ptr<cmFileListGeneratorBase>
  1883. g(new cmFileListGeneratorGlob(*this));
  1884. return g;
  1885. }
  1886. };
  1887. //----------------------------------------------------------------------------
  1888. bool cmFindPackageCommand::SearchPrefix(std::string const& prefix_in)
  1889. {
  1890. assert(!prefix_in.empty() && prefix_in[prefix_in.size()-1] == '/');
  1891. if(this->DebugMode)
  1892. {
  1893. fprintf(stderr, "Checking prefix [%s]\n", prefix_in.c_str());
  1894. }
  1895. // Skip this if the prefix does not exist.
  1896. if(!cmSystemTools::FileIsDirectory(prefix_in))
  1897. {
  1898. return false;
  1899. }
  1900. // PREFIX/ (useful on windows or in build trees)
  1901. if(this->SearchDirectory(prefix_in))
  1902. {
  1903. return true;
  1904. }
  1905. // Strip the trailing slash because the path generator is about to
  1906. // add one.
  1907. std::string prefix = prefix_in.substr(0, prefix_in.size()-1);
  1908. // PREFIX/(cmake|CMake)/ (useful on windows or in build trees)
  1909. {
  1910. cmFindPackageFileList lister(this);
  1911. lister
  1912. / cmFileListGeneratorFixed(prefix)
  1913. / cmFileListGeneratorCaseInsensitive("cmake");
  1914. if(lister.Search())
  1915. {
  1916. return true;
  1917. }
  1918. }
  1919. // PREFIX/(Foo|foo|FOO).*/
  1920. {
  1921. cmFindPackageFileList lister(this);
  1922. lister
  1923. / cmFileListGeneratorFixed(prefix)
  1924. / cmFileListGeneratorProject(this->Names);
  1925. if(lister.Search())
  1926. {
  1927. return true;
  1928. }
  1929. }
  1930. // PREFIX/(Foo|foo|FOO).*/(cmake|CMake)/
  1931. {
  1932. cmFindPackageFileList lister(this);
  1933. lister
  1934. / cmFileListGeneratorFixed(prefix)
  1935. / cmFileListGeneratorProject(this->Names)
  1936. / cmFileListGeneratorCaseInsensitive("cmake");
  1937. if(lister.Search())
  1938. {
  1939. return true;
  1940. }
  1941. }
  1942. // Construct list of common install locations (lib and share).
  1943. std::vector<std::string> common;
  1944. if(!this->LibraryArchitecture.empty())
  1945. {
  1946. common.push_back("lib/"+this->LibraryArchitecture);
  1947. }
  1948. if(this->UseLib64Paths)
  1949. {
  1950. common.push_back("lib64");
  1951. }
  1952. common.push_back("lib");
  1953. common.push_back("share");
  1954. // PREFIX/(lib/ARCH|lib|share)/cmake/(Foo|foo|FOO).*/
  1955. {
  1956. cmFindPackageFileList lister(this);
  1957. lister
  1958. / cmFileListGeneratorFixed(prefix)
  1959. / cmFileListGeneratorEnumerate(common)
  1960. / cmFileListGeneratorFixed("cmake")
  1961. / cmFileListGeneratorProject(this->Names);
  1962. if(lister.Search())
  1963. {
  1964. return true;
  1965. }
  1966. }
  1967. // PREFIX/(lib/ARCH|lib|share)/(Foo|foo|FOO).*/
  1968. {
  1969. cmFindPackageFileList lister(this);
  1970. lister
  1971. / cmFileListGeneratorFixed(prefix)
  1972. / cmFileListGeneratorEnumerate(common)
  1973. / cmFileListGeneratorProject(this->Names);
  1974. if(lister.Search())
  1975. {
  1976. return true;
  1977. }
  1978. }
  1979. // PREFIX/(lib/ARCH|lib|share)/(Foo|foo|FOO).*/(cmake|CMake)/
  1980. {
  1981. cmFindPackageFileList lister(this);
  1982. lister
  1983. / cmFileListGeneratorFixed(prefix)
  1984. / cmFileListGeneratorEnumerate(common)
  1985. / cmFileListGeneratorProject(this->Names)
  1986. / cmFileListGeneratorCaseInsensitive("cmake");
  1987. if(lister.Search())
  1988. {
  1989. return true;
  1990. }
  1991. }
  1992. return false;
  1993. }
  1994. //----------------------------------------------------------------------------
  1995. bool cmFindPackageCommand::SearchFrameworkPrefix(std::string const& prefix_in)
  1996. {
  1997. assert(!prefix_in.empty() && prefix_in[prefix_in.size()-1] == '/');
  1998. if(this->DebugMode)
  1999. {
  2000. fprintf(stderr, "Checking framework prefix [%s]\n", prefix_in.c_str());
  2001. }
  2002. // Strip the trailing slash because the path generator is about to
  2003. // add one.
  2004. std::string prefix = prefix_in.substr(0, prefix_in.size()-1);
  2005. // <prefix>/Foo.framework/Resources/
  2006. {
  2007. cmFindPackageFileList lister(this);
  2008. lister
  2009. / cmFileListGeneratorFixed(prefix)
  2010. / cmFileListGeneratorMacProject(this->Names, ".framework")
  2011. / cmFileListGeneratorFixed("Resources");
  2012. if(lister.Search())
  2013. {
  2014. return true;
  2015. }
  2016. }
  2017. // <prefix>/Foo.framework/Resources/CMake/
  2018. {
  2019. cmFindPackageFileList lister(this);
  2020. lister
  2021. / cmFileListGeneratorFixed(prefix)
  2022. / cmFileListGeneratorMacProject(this->Names, ".framework")
  2023. / cmFileListGeneratorFixed("Resources")
  2024. / cmFileListGeneratorCaseInsensitive("cmake");
  2025. if(lister.Search())
  2026. {
  2027. return true;
  2028. }
  2029. }
  2030. // <prefix>/Foo.framework/Versions/*/Resources/
  2031. {
  2032. cmFindPackageFileList lister(this);
  2033. lister
  2034. / cmFileListGeneratorFixed(prefix)
  2035. / cmFileListGeneratorMacProject(this->Names, ".framework")
  2036. / cmFileListGeneratorFixed("Versions")
  2037. / cmFileListGeneratorGlob("*/Resources");
  2038. if(lister.Search())
  2039. {
  2040. return true;
  2041. }
  2042. }
  2043. // <prefix>/Foo.framework/Versions/*/Resources/CMake/
  2044. {
  2045. cmFindPackageFileList lister(this);
  2046. lister
  2047. / cmFileListGeneratorFixed(prefix)
  2048. / cmFileListGeneratorMacProject(this->Names, ".framework")
  2049. / cmFileListGeneratorFixed("Versions")
  2050. / cmFileListGeneratorGlob("*/Resources")
  2051. / cmFileListGeneratorCaseInsensitive("cmake");
  2052. if(lister.Search())
  2053. {
  2054. return true;
  2055. }
  2056. }
  2057. return false;
  2058. }
  2059. //----------------------------------------------------------------------------
  2060. bool cmFindPackageCommand::SearchAppBundlePrefix(std::string const& prefix_in)
  2061. {
  2062. assert(!prefix_in.empty() && prefix_in[prefix_in.size()-1] == '/');
  2063. if(this->DebugMode)
  2064. {
  2065. fprintf(stderr, "Checking bundle prefix [%s]\n", prefix_in.c_str());
  2066. }
  2067. // Strip the trailing slash because the path generator is about to
  2068. // add one.
  2069. std::string prefix = prefix_in.substr(0, prefix_in.size()-1);
  2070. // <prefix>/Foo.app/Contents/Resources
  2071. {
  2072. cmFindPackageFileList lister(this);
  2073. lister
  2074. / cmFileListGeneratorFixed(prefix)
  2075. / cmFileListGeneratorMacProject(this->Names, ".app")
  2076. / cmFileListGeneratorFixed("Contents/Resources");
  2077. if(lister.Search())
  2078. {
  2079. return true;
  2080. }
  2081. }
  2082. // <prefix>/Foo.app/Contents/Resources/CMake
  2083. {
  2084. cmFindPackageFileList lister(this);
  2085. lister
  2086. / cmFileListGeneratorFixed(prefix)
  2087. / cmFileListGeneratorMacProject(this->Names, ".app")
  2088. / cmFileListGeneratorFixed("Contents/Resources")
  2089. / cmFileListGeneratorCaseInsensitive("cmake");
  2090. if(lister.Search())
  2091. {
  2092. return true;
  2093. }
  2094. }
  2095. return false;
  2096. }
  2097. // TODO: Debug cmsys::Glob double slash problem.