cmFindPackageCommand.cxx 66 KB

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