cmFindPackageCommand.cxx 66 KB

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