cmFindPackageCommand.cxx 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmFindPackageCommand.h"
  14. #include <cmsys/RegularExpression.hxx>
  15. #ifdef CMAKE_BUILD_WITH_CMAKE
  16. #include "cmVariableWatch.h"
  17. #endif
  18. void cmFindPackageNeedBackwardsCompatibility(const std::string& variable,
  19. int access_type, void*, const char* newValue,
  20. const cmMakefile*)
  21. {
  22. (void)newValue;
  23. #ifdef CMAKE_BUILD_WITH_CMAKE
  24. if(access_type == cmVariableWatch::UNKNOWN_VARIABLE_READ_ACCESS)
  25. {
  26. std::string message = "An attempt was made to access a variable: ";
  27. message += variable;
  28. message +=
  29. " that has not been defined. This variable is created by the "
  30. "FIND_PACKAGE command. CMake version 1.6 always converted the "
  31. "variable name to upper-case, but this behavior is no longer the "
  32. "case. To fix this you might need to set the cache value of "
  33. "CMAKE_BACKWARDS_COMPATIBILITY to 1.6 or less. If you are writing a "
  34. "CMake listfile, you should change the variable reference to use "
  35. "the case of the argument to FIND_PACKAGE.";
  36. cmSystemTools::Error(message.c_str());
  37. }
  38. #else
  39. (void)variable;
  40. (void)access_type;
  41. #endif
  42. }
  43. //----------------------------------------------------------------------------
  44. cmFindPackageCommand::cmFindPackageCommand()
  45. {
  46. cmSystemTools::ReplaceString(this->GenericDocumentationRootPath,
  47. "CMAKE_FIND_ROOT_PATH_MODE_XXX",
  48. "CMAKE_FIND_ROOT_PATH_MODE_PACKAGE");
  49. cmSystemTools::ReplaceString(this->GenericDocumentationPathsOrder,
  50. "FIND_ARGS_XXX", "<package>");
  51. cmSystemTools::ReplaceString(this->GenericDocumentationPathsOrder,
  52. "FIND_XXX", "find_package");
  53. this->CMakePathName = "PACKAGE";
  54. this->Quiet = false;
  55. this->Required = false;
  56. this->NoBuilds = false;
  57. this->NoModule = false;
  58. this->DebugMode = false;
  59. this->CommandDocumentation =
  60. " find_package(<package> [major.minor] [QUIET] [NO_MODULE]\n"
  61. " [[REQUIRED|COMPONENTS] [components...]]\n"
  62. " [NAMES name1 [name2 ...]]\n"
  63. " [CONFIGS config1 [config2 ...]]\n"
  64. " [PATHS path1 [path2 ... ]]\n"
  65. " [PATH_SUFFIXES suffix1 [suffix2 ...]]\n"
  66. " [NO_DEFAULT_PATH]\n"
  67. " [NO_CMAKE_ENVIRONMENT_PATH]\n"
  68. " [NO_CMAKE_PATH]\n"
  69. " [NO_SYSTEM_ENVIRONMENT_PATH]\n"
  70. " [NO_CMAKE_BUILDS_PATH]\n"
  71. " [NO_CMAKE_SYSTEM_PATH]\n"
  72. " [CMAKE_FIND_ROOT_PATH_BOTH |\n"
  73. " ONLY_CMAKE_FIND_ROOT_PATH |\n"
  74. " NO_CMAKE_FIND_ROOT_PATH])\n"
  75. "Finds and loads settings from an external project. <package>_FOUND "
  76. "will be set to indicate whether the package was found. Settings that "
  77. "can be used when <package>_FOUND is true are package-specific. "
  78. "A package-specific list of components may be listed after the "
  79. "REQUIRED option, or after the COMPONENTS option if no REQUIRED "
  80. "option is given. The \"major.minor\" version argument is currently "
  81. "a placeholder for future use and is ignored. "
  82. "The command has two modes by which it searches for packages: "
  83. "\"Module\" mode and \"Config\" mode."
  84. "\n"
  85. "Module mode has a reduced signature:\n"
  86. " find_package(<package> [major.minor] [QUIET]\n"
  87. " [[REQUIRED|COMPONENTS] [components...]])\n"
  88. "CMake searches for a file called \"Find<package>.cmake\" in "
  89. "the CMAKE_MODULE_PATH followed by the CMake installation. "
  90. "If the file is found, it is read and processed by CMake. "
  91. "It is responsible for finding the package "
  92. "or producing an error message if package content cannot be found. "
  93. "Otherwise the command proceeds to Config mode. The NO_MODULE "
  94. "option may be used to skip Module mode explicitly, but the option "
  95. "is implied by use of options not specified in the reduced signature."
  96. "\n"
  97. "Config mode attempts to locate a configuration file provided by the "
  98. "package to be found. A cache entry called <package>_DIR is created to "
  99. "hold the directory containing the file. "
  100. "By default the command searches for a package with the name <package>. "
  101. "If the NAMES option is given the names following it are used instead "
  102. "of <package>. "
  103. "The command searches for a file called \"<name>Config.cmake\" or "
  104. "\"<lower-case-name>-config.cmake\" for each name specified. "
  105. "A replacement set of possible configuration file names may be given "
  106. "using the CONFIGS option. "
  107. "The search procedure is specified below. Once found, the configuration "
  108. "file is read and processed by CMake. Since the file is provided by the "
  109. "package it already knows the location of package contents. "
  110. "The full path to the configuration file is stored in the cmake "
  111. "variable <package>_CONFIG."
  112. "\n"
  113. "If the package configuration file cannot be found CMake "
  114. "will generate an error describing the problem unless the QUIET "
  115. "argument is specified. If REQUIRED is specified and the package "
  116. "is not found a fatal error is generated and the configure step stops "
  117. "executing. If <package>_DIR has been set to a directory not containing "
  118. "a configuration file a fatal error is always generated because user "
  119. "intervention is required."
  120. "\n"
  121. "CMake constructs a set of possible installation prefixes for the "
  122. "package. Under each prefix the following directories are searched "
  123. "for a configuration file:\n"
  124. " <prefix>/ (W)\n"
  125. " <prefix>/(cmake|CMake)/ (W)\n"
  126. " <prefix>/(share|lib)/<name>*/ (P)\n"
  127. " <prefix>/(share|lib)/<name>*/(cmake|CMake)/ (P)\n"
  128. "On systems supporting OS X Frameworks and Application Bundles "
  129. "the following directories are searched for frameworks or bundles "
  130. "containing a configuration file:\n"
  131. " <prefix>/<name>.framework/Resources/ (A)\n"
  132. " <prefix>/<name>.framework/Resources/CMake/ (A)\n"
  133. " <prefix>/<name>.framework/Versions/*/Resources/ (A)\n"
  134. " <prefix>/<name>.framework/Versions/*/Resources/CMake/ (A)\n"
  135. " <prefix>/<name>.app/Contents/Resources/ (A)\n"
  136. " <prefix>/<name>.app/Contents/Resources/CMake/ (A)\n"
  137. "In all cases the <name> is treated as case-insensitive and corresponds "
  138. "to any of the names specified (<package> or names given by NAMES). "
  139. "If PATH_SUFFIXES is specified the suffixes are appended to each "
  140. "(W) or (P) directory entry one-by-one.\n"
  141. "This set of directories is intended to work in cooperation with "
  142. "projects that provide configuration files in their installation trees. "
  143. "Directories above marked with (W) are intended for installations on "
  144. "Windows where the prefix may point at the top of an application's "
  145. "installation directory. Those marked with (P) are intended for "
  146. "installations on POSIX platforms where the prefix is shared by "
  147. "multiple packages. This is merely a convention, so all (W) and (P) "
  148. "directories are still searched on all platforms. "
  149. "Directories marked with (A) are intended for installations on "
  150. "Apple platforms. The cmake variables CMAKE_FIND_FRAMEWORK and "
  151. "CMAKE_FIND_APPBUNDLE determine the order of preference "
  152. "as specified below.\n"
  153. "The set of installation prefixes is constructed using the following "
  154. "steps. If NO_DEFAULT_PATH is specified steps 1-5 are skipped.\n"
  155. "1. Search cmake specific environment variables. This "
  156. "can be skipped if NO_CMAKE_ENVIRONMENT_PATH is passed.\n"
  157. " CMAKE_PREFIX_PATH\n"
  158. " CMAKE_FRAMEWORK_PATH\n"
  159. " CMAKE_APPBUNDLE_PATH\n"
  160. "2. Search cmake variables with the same names as the cmake specific "
  161. "environment variables. These are intended to be used on the command "
  162. "line with a -DVAR=value. This can be skipped if NO_CMAKE_PATH "
  163. "is passed.\n"
  164. " CMAKE_PREFIX_PATH\n"
  165. " CMAKE_FRAMEWORK_PATH\n"
  166. " CMAKE_APPBUNDLE_PATH\n"
  167. "3. Search the standard system environment variables. "
  168. "This can be skipped if NO_SYSTEM_ENVIRONMENT_PATH is passed. "
  169. "Path entries ending in \"/bin\" or \"/sbin\" are automatically "
  170. "converted to their parent directories.\n"
  171. " PATH\n"
  172. "4. Search project build trees recently configured in a CMake GUI. "
  173. "This can be skipped if NO_CMAKE_BUILDS_PATH is passed. "
  174. "It is intended for the case when a user is building multiple "
  175. "dependent projects one after another.\n"
  176. "5. Search cmake variables defined in the Platform files "
  177. "for the current system. This can be skipped if NO_CMAKE_SYSTEM_PATH "
  178. "is passed.\n"
  179. " CMAKE_SYSTEM_PREFIX_PATH\n"
  180. " CMAKE_SYSTEM_FRAMEWORK_PATH\n"
  181. " CMAKE_SYSTEM_APPBUNDLE_PATH\n"
  182. "6. Search paths specified by the PATHS option.\n"
  183. ;
  184. this->CommandDocumentation += this->GenericDocumentationMacPolicy;
  185. this->CommandDocumentation += this->GenericDocumentationRootPath;
  186. this->CommandDocumentation += this->GenericDocumentationPathsOrder;
  187. }
  188. //----------------------------------------------------------------------------
  189. const char* cmFindPackageCommand::GetFullDocumentation()
  190. {
  191. return this->CommandDocumentation.c_str();
  192. }
  193. //----------------------------------------------------------------------------
  194. bool cmFindPackageCommand::InitialPass(std::vector<std::string> const& args)
  195. {
  196. if(args.size() < 1)
  197. {
  198. this->SetError("called with incorrect number of arguments");
  199. return false;
  200. }
  201. // Check for debug mode.
  202. this->DebugMode = this->Makefile->IsOn("CMAKE_FIND_DEBUG_MODE");
  203. // Find the current root path mode.
  204. this->SelectDefaultRootPathMode();
  205. // Find the current bundle/framework search policy.
  206. this->SelectDefaultMacMode();
  207. // Record options.
  208. this->Name = args[0];
  209. std::string components;
  210. const char* components_sep = "";
  211. // Check ancient compatibility.
  212. this->Compatibility_1_6 =
  213. this->Makefile->GetLocalGenerator()
  214. ->NeedBackwardsCompatibility(1, 6);
  215. // Always search directly in a generated path.
  216. this->SearchPathSuffixes.push_back("");
  217. // Parse the arguments.
  218. enum Doing { DoingNone, DoingComponents, DoingNames, DoingPaths,
  219. DoingPathSuffixes, DoingConfigs };
  220. Doing doing = DoingNone;
  221. cmsys::RegularExpression version("^[0-9.]+$");
  222. bool haveVersion = false;
  223. for(unsigned int i=1; i < args.size(); ++i)
  224. {
  225. if(args[i] == "QUIET")
  226. {
  227. this->Quiet = true;
  228. doing = DoingNone;
  229. }
  230. else if(args[i] == "NO_MODULE")
  231. {
  232. this->NoModule = true;
  233. doing = DoingNone;
  234. }
  235. else if(args[i] == "REQUIRED")
  236. {
  237. this->Required = true;
  238. doing = DoingComponents;
  239. }
  240. else if(args[i] == "COMPONENTS")
  241. {
  242. this->Compatibility_1_6 = false;
  243. doing = DoingComponents;
  244. }
  245. else if(args[i] == "NAMES")
  246. {
  247. this->NoModule = true;
  248. this->Compatibility_1_6 = false;
  249. doing = DoingNames;
  250. }
  251. else if(args[i] == "PATHS")
  252. {
  253. this->NoModule = true;
  254. this->Compatibility_1_6 = false;
  255. doing = DoingPaths;
  256. }
  257. else if(args[i] == "PATH_SUFFIXES")
  258. {
  259. this->NoModule = true;
  260. this->Compatibility_1_6 = false;
  261. doing = DoingPathSuffixes;
  262. }
  263. else if(args[i] == "CONFIGS")
  264. {
  265. this->NoModule = true;
  266. this->Compatibility_1_6 = false;
  267. doing = DoingConfigs;
  268. }
  269. else if(args[i] == "NO_CMAKE_BUILDS_PATH")
  270. {
  271. this->NoBuilds = true;
  272. this->NoModule = true;
  273. this->Compatibility_1_6 = false;
  274. doing = DoingNone;
  275. }
  276. else if(this->CheckCommonArgument(args[i]))
  277. {
  278. this->NoModule = true;
  279. this->Compatibility_1_6 = false;
  280. doing = DoingNone;
  281. }
  282. else if(doing == DoingComponents)
  283. {
  284. // Set a variable telling the find script this component
  285. // is required.
  286. std::string req_var = Name + "_FIND_REQUIRED_" + args[i];
  287. this->Makefile->AddDefinition(req_var.c_str(), "1");
  288. // Append to the list of required components.
  289. components += components_sep;
  290. components += args[i];
  291. components_sep = ";";
  292. }
  293. else if(doing == DoingNames)
  294. {
  295. this->Names.push_back(args[i]);
  296. }
  297. else if(doing == DoingPaths)
  298. {
  299. this->AddUserPath(args[i]);
  300. }
  301. else if(doing == DoingPathSuffixes)
  302. {
  303. this->AddPathSuffix(args[i]);
  304. }
  305. else if(doing == DoingConfigs)
  306. {
  307. this->Configs.push_back(args[i]);
  308. }
  309. else if(!haveVersion && version.find(args[i].c_str()))
  310. {
  311. haveVersion = true;
  312. }
  313. else
  314. {
  315. cmOStringStream e;
  316. e << "called with invalid argument \"" << args[i].c_str() << "\"";
  317. this->SetError(e.str().c_str());
  318. return false;
  319. }
  320. }
  321. // Store the list of components.
  322. std::string components_var = Name + "_FIND_COMPONENTS";
  323. this->Makefile->AddDefinition(components_var.c_str(), components.c_str());
  324. // See if there is a Find<package>.cmake module.
  325. if(!this->NoModule)
  326. {
  327. bool foundModule = false;
  328. if(!this->FindModule(foundModule))
  329. {
  330. this->AppendSuccessInformation();
  331. return false;
  332. }
  333. if(foundModule)
  334. {
  335. this->AppendSuccessInformation();
  336. return true;
  337. }
  338. }
  339. // No find module. Assume the project has a CMake config file. Use
  340. // a <package>_DIR cache variable to locate it.
  341. this->Variable = this->Name;
  342. this->Variable += "_DIR";
  343. // Add the default name.
  344. if(this->Names.empty())
  345. {
  346. this->Names.push_back(this->Name);
  347. }
  348. // Add the default configs.
  349. if(this->Configs.empty())
  350. {
  351. for(std::vector<std::string>::const_iterator ni = this->Names.begin();
  352. ni != this->Names.end(); ++ni)
  353. {
  354. std::string config = *ni;
  355. config += "Config.cmake";
  356. this->Configs.push_back(config);
  357. config = cmSystemTools::LowerCase(*ni);
  358. config += "-config.cmake";
  359. this->Configs.push_back(config);
  360. }
  361. }
  362. // Find and load the package.
  363. bool result = this->HandlePackageMode();
  364. this->AppendSuccessInformation();
  365. return result;
  366. }
  367. //----------------------------------------------------------------------------
  368. bool cmFindPackageCommand::FindModule(bool& found)
  369. {
  370. std::string module = "Find";
  371. module += this->Name;
  372. module += ".cmake";
  373. std::string mfile = this->Makefile->GetModulesFile(module.c_str());
  374. if ( mfile.size() )
  375. {
  376. if(this->Quiet)
  377. {
  378. // Tell the module that is about to be read that it should find
  379. // quietly.
  380. std::string quietly = this->Name;
  381. quietly += "_FIND_QUIETLY";
  382. this->Makefile->AddDefinition(quietly.c_str(), "1");
  383. }
  384. if(this->Required)
  385. {
  386. // Tell the module that is about to be read that it should report
  387. // a fatal error if the package is not found.
  388. std::string req = this->Name;
  389. req += "_FIND_REQUIRED";
  390. this->Makefile->AddDefinition(req.c_str(), "1");
  391. }
  392. // Load the module we found.
  393. found = true;
  394. return this->ReadListFile(mfile.c_str());
  395. }
  396. return true;
  397. }
  398. //----------------------------------------------------------------------------
  399. bool cmFindPackageCommand::HandlePackageMode()
  400. {
  401. // Support old capitalization behavior.
  402. std::string upperDir = cmSystemTools::UpperCase(this->Name);
  403. std::string upperFound = cmSystemTools::UpperCase(this->Name);
  404. upperDir += "_DIR";
  405. upperFound += "_FOUND";
  406. if(upperDir == this->Variable)
  407. {
  408. this->Compatibility_1_6 = false;
  409. }
  410. // Try to find the config file.
  411. const char* def = this->Makefile->GetDefinition(this->Variable.c_str());
  412. if(this->Compatibility_1_6 && cmSystemTools::IsOff(def))
  413. {
  414. // Use the setting of the old name of the variable to provide the
  415. // value of the new.
  416. const char* oldDef = this->Makefile->GetDefinition(upperDir.c_str());
  417. if(!cmSystemTools::IsOff(oldDef))
  418. {
  419. this->Makefile->AddDefinition(this->Variable.c_str(), oldDef);
  420. def = this->Makefile->GetDefinition(this->Variable.c_str());
  421. }
  422. }
  423. // Search for the config file if it is not already found.
  424. if(cmSystemTools::IsOff(def))
  425. {
  426. this->FindConfig();
  427. def = this->Makefile->GetDefinition(this->Variable.c_str());
  428. }
  429. // If the config file was found, load it.
  430. std::string file;
  431. bool result = true;
  432. bool found = false;
  433. if(!cmSystemTools::IsOff(def))
  434. {
  435. // Get the directory from the variable value.
  436. std::string dir = def;
  437. cmSystemTools::ConvertToUnixSlashes(dir);
  438. // Treat relative paths with respect to the current source dir.
  439. if(!cmSystemTools::FileIsFullPath(dir.c_str()))
  440. {
  441. dir = "/" + dir;
  442. dir = this->Makefile->GetCurrentDirectory() + dir;
  443. }
  444. // Find the configuration file.
  445. if(this->FindConfigFile(dir, file))
  446. {
  447. // Parse the configuration file.
  448. if(this->ReadListFile(file.c_str()))
  449. {
  450. // The package has been found.
  451. found = true;
  452. }
  453. else
  454. {
  455. // The configuration file is invalid.
  456. result = false;
  457. }
  458. }
  459. else
  460. {
  461. // The variable setting is wrong.
  462. cmOStringStream e;
  463. e << "cannot find package " << this->Name << " because "
  464. << this->Variable << " is set to \"" << def << "\" "
  465. << "which is not a directory containing a package configuration "
  466. << "file. "
  467. << "Please set the cache entry " << this->Variable << " "
  468. << "to the correct directory, or delete it to ask CMake "
  469. << "to search.";
  470. this->SetError(e.str().c_str());
  471. result = false;
  472. }
  473. }
  474. else if(!this->Quiet || this->Required)
  475. {
  476. // The variable is not set.
  477. cmOStringStream e;
  478. e << "could not find ";
  479. if(!this->NoModule)
  480. {
  481. e << "module Find" << this->Name << ".cmake or ";
  482. }
  483. e << "a configuration file for package " << this->Name << ".\n";
  484. if(!this->NoModule)
  485. {
  486. e << "Adjust CMAKE_MODULE_PATH to find Find"
  487. << this->Name << ".cmake or set ";
  488. }
  489. else
  490. {
  491. e << "Set ";
  492. }
  493. e << this->Variable << " to the directory containing a CMake "
  494. << "configuration file for " << this->Name << ". ";
  495. if(this->Configs.size() == 1)
  496. {
  497. e << "The file will be called " << this->Configs[0];
  498. }
  499. else
  500. {
  501. e << "The file will have one of the following names:\n";
  502. for(std::vector<std::string>::const_iterator ci = this->Configs.begin();
  503. ci != this->Configs.end(); ++ci)
  504. {
  505. e << " " << *ci << "\n";
  506. }
  507. }
  508. if(this->Required)
  509. {
  510. this->SetError(e.str().c_str());
  511. result = false;
  512. }
  513. else
  514. {
  515. cmSystemTools::Error("find_package ", e.str().c_str());
  516. }
  517. }
  518. // Set a variable marking whether the package was found.
  519. std::string foundVar = this->Name;
  520. foundVar += "_FOUND";
  521. this->Makefile->AddDefinition(foundVar.c_str(), found? "1":"0");
  522. // Set a variable naming the configuration file that was found.
  523. std::string fileVar = this->Name;
  524. fileVar += "_CONFIG";
  525. if(found)
  526. {
  527. this->Makefile->AddDefinition(fileVar.c_str(), file.c_str());
  528. }
  529. else
  530. {
  531. this->Makefile->RemoveDefinition(fileVar.c_str());
  532. }
  533. // Handle some ancient compatibility stuff.
  534. if(this->Compatibility_1_6)
  535. {
  536. // Listfiles will be looking for the capitalized version of the
  537. // name. Provide it.
  538. this->Makefile->AddDefinition
  539. (upperDir.c_str(),
  540. this->Makefile->GetDefinition(this->Variable.c_str()));
  541. this->Makefile->AddDefinition
  542. (upperFound.c_str(),
  543. this->Makefile->GetDefinition(foundVar.c_str()));
  544. }
  545. #ifdef CMAKE_BUILD_WITH_CMAKE
  546. if(!(upperDir == this->Variable))
  547. {
  548. if(this->Compatibility_1_6)
  549. {
  550. // Listfiles may use the capitalized version of the name.
  551. // Remove any previously added watch.
  552. this->Makefile->GetVariableWatch()->RemoveWatch(
  553. upperDir.c_str(),
  554. cmFindPackageNeedBackwardsCompatibility
  555. );
  556. }
  557. else
  558. {
  559. // Listfiles should not be using the capitalized version of the
  560. // name. Add a watch to warn the user.
  561. this->Makefile->GetVariableWatch()->AddWatch(
  562. upperDir.c_str(),
  563. cmFindPackageNeedBackwardsCompatibility
  564. );
  565. }
  566. }
  567. #endif
  568. return result;
  569. }
  570. //----------------------------------------------------------------------------
  571. void cmFindPackageCommand::FindConfig()
  572. {
  573. // Compute the set of search prefixes.
  574. this->ComputePrefixes();
  575. // Look for the project's configuration file.
  576. bool found = false;
  577. // Search for frameworks.
  578. if(!found && this->SearchFrameworkFirst || this->SearchFrameworkOnly)
  579. {
  580. found = this->FindFrameworkConfig();
  581. }
  582. // Search for apps.
  583. if(!found && this->SearchAppBundleFirst || this->SearchAppBundleOnly)
  584. {
  585. found = this->FindAppBundleConfig();
  586. }
  587. // Search prefixes.
  588. if(!found && !(this->SearchFrameworkOnly || this->SearchAppBundleOnly))
  589. {
  590. found = this->FindPrefixedConfig();
  591. }
  592. // Search for frameworks.
  593. if(!found && this->SearchFrameworkLast)
  594. {
  595. found = this->FindFrameworkConfig();
  596. }
  597. // Search for apps.
  598. if(!found && this->SearchAppBundleLast)
  599. {
  600. found = this->FindAppBundleConfig();
  601. }
  602. // Store the entry in the cache so it can be set by the user.
  603. std::string init;
  604. if(found)
  605. {
  606. init = cmSystemTools::GetFilenamePath(this->FileFound);
  607. }
  608. else
  609. {
  610. init = this->Variable + "-NOTFOUND";
  611. }
  612. std::string help =
  613. "The directory containing a CMake configuration file for ";
  614. help += this->Name;
  615. help += ".";
  616. this->Makefile->AddCacheDefinition(this->Variable.c_str(),
  617. init.c_str(), help.c_str(),
  618. cmCacheManager::PATH);
  619. }
  620. //----------------------------------------------------------------------------
  621. bool cmFindPackageCommand::FindPrefixedConfig()
  622. {
  623. for(std::vector<std::string>::const_iterator pi = this->Prefixes.begin();
  624. pi != this->Prefixes.end(); ++pi)
  625. {
  626. if(this->SearchPrefix(*pi))
  627. {
  628. return true;
  629. }
  630. }
  631. return false;
  632. }
  633. //----------------------------------------------------------------------------
  634. bool cmFindPackageCommand::FindFrameworkConfig()
  635. {
  636. for(std::vector<std::string>::const_iterator i = this->Prefixes.begin();
  637. i != this->Prefixes.end(); ++i)
  638. {
  639. if(this->SearchFrameworkPrefix(*i))
  640. {
  641. return true;
  642. }
  643. }
  644. return false;
  645. }
  646. //----------------------------------------------------------------------------
  647. bool cmFindPackageCommand::FindAppBundleConfig()
  648. {
  649. for(std::vector<std::string>::const_iterator i = this->Prefixes.begin();
  650. i != this->Prefixes.end(); ++i)
  651. {
  652. if(this->SearchAppBundlePrefix(*i))
  653. {
  654. return true;
  655. }
  656. }
  657. return false;
  658. }
  659. //----------------------------------------------------------------------------
  660. bool cmFindPackageCommand::ReadListFile(const char* f)
  661. {
  662. if(this->Makefile->ReadListFile(this->Makefile->GetCurrentListFile(),f))
  663. {
  664. return true;
  665. }
  666. std::string e = "Error reading CMake code from \"";
  667. e += f;
  668. e += "\".";
  669. this->SetError(e.c_str());
  670. return false;
  671. }
  672. //----------------------------------------------------------------------------
  673. void cmFindPackageCommand::AppendToProperty(const char* propertyName)
  674. {
  675. std::string propertyValue;
  676. const char *prop =
  677. this->Makefile->GetCMakeInstance()->GetProperty(propertyName);
  678. if (prop && *prop)
  679. {
  680. propertyValue = prop;
  681. std::vector<std::string> contents;
  682. cmSystemTools::ExpandListArgument(propertyValue, contents, false);
  683. bool alreadyInserted = false;
  684. for(std::vector<std::string>::const_iterator it = contents.begin();
  685. it != contents.end(); ++ it )
  686. {
  687. if (*it == this->Name)
  688. {
  689. alreadyInserted = true;
  690. break;
  691. }
  692. }
  693. if (!alreadyInserted)
  694. {
  695. propertyValue += ";";
  696. propertyValue += this->Name;
  697. }
  698. }
  699. else
  700. {
  701. propertyValue = this->Name;
  702. }
  703. this->Makefile->GetCMakeInstance()->SetProperty(propertyName,
  704. propertyValue.c_str());
  705. }
  706. //----------------------------------------------------------------------------
  707. void cmFindPackageCommand::AppendSuccessInformation()
  708. {
  709. std::string found = this->Name;
  710. found += "_FOUND";
  711. std::string upperFound = cmSystemTools::UpperCase(found);
  712. const char* upperResult = this->Makefile->GetDefinition(upperFound.c_str());
  713. const char* result = this->Makefile->GetDefinition(found.c_str());
  714. if ((cmSystemTools::IsOn(result)) || (cmSystemTools::IsOn(upperResult)))
  715. {
  716. this->AppendToProperty("PACKAGES_FOUND");
  717. if (!this->Quiet)
  718. {
  719. this->AppendToProperty("ENABLED_FEATURES");
  720. }
  721. }
  722. else
  723. {
  724. this->AppendToProperty("PACKAGES_NOT_FOUND");
  725. if (!this->Quiet)
  726. {
  727. this->AppendToProperty("DISABLED_FEATURES");
  728. }
  729. }
  730. }
  731. //----------------------------------------------------------------------------
  732. void cmFindPackageCommand::AddUserPath(std::string const& p)
  733. {
  734. std::string userPath = p;
  735. cmSystemTools::ExpandRegistryValues(userPath);
  736. this->UserPaths.push_back(userPath);
  737. }
  738. //----------------------------------------------------------------------------
  739. void cmFindPackageCommand::ComputePrefixes()
  740. {
  741. std::vector<std::string>& prefixes = this->Prefixes;
  742. std::set<cmStdString> emmitted;
  743. if(!this->NoCMakeEnvironmentPath && !this->NoDefaultPath)
  744. {
  745. // Check the environment variable with the same name as the cache
  746. // entry.
  747. std::string env;
  748. if(cmSystemTools::GetEnv(this->Variable.c_str(), env) && env.length() > 0)
  749. {
  750. cmSystemTools::ConvertToUnixSlashes(env);
  751. this->AddPathInternal(prefixes, env, EnvPath, &emmitted);
  752. }
  753. this->AddEnvPath(prefixes, "CMAKE_PREFIX_PATH", &emmitted);
  754. this->AddEnvPath(prefixes, "CMAKE_FRAMEWORK_PATH", &emmitted);
  755. this->AddEnvPath(prefixes, "CMAKE_APPBUNDLE_PATH", &emmitted);
  756. }
  757. if(!this->NoCMakePath && !this->NoDefaultPath)
  758. {
  759. this->AddCMakePath(prefixes, "CMAKE_PREFIX_PATH", &emmitted);
  760. this->AddCMakePath(prefixes, "CMAKE_FRAMEWORK_PATH", &emmitted);
  761. this->AddCMakePath(prefixes, "CMAKE_APPBUNDLE_PATH", &emmitted);
  762. }
  763. if(!this->NoSystemEnvironmentPath && !this->NoDefaultPath)
  764. {
  765. // Use the system search path to generate prefixes.
  766. // Relative paths are interpreted with respect to the current
  767. // working directory.
  768. std::vector<std::string> tmp;
  769. cmSystemTools::GetPath(tmp);
  770. for(std::vector<std::string>::iterator i = tmp.begin();
  771. i != tmp.end(); ++i)
  772. {
  773. std::string const& d = *i;
  774. // If the path is a PREFIX/bin case then add its parent instead.
  775. if(d.size() >= 4 && strcmp(d.c_str()+d.size()-4, "/bin") == 0 ||
  776. d.size() >= 5 && strcmp(d.c_str()+d.size()-5, "/sbin") == 0)
  777. {
  778. this->AddPathInternal(prefixes,
  779. cmSystemTools::GetFilenamePath(d),
  780. EnvPath, &emmitted);
  781. }
  782. else
  783. {
  784. this->AddPathInternal(prefixes, d, EnvPath, &emmitted);
  785. }
  786. }
  787. }
  788. if(!this->NoBuilds && !this->NoDefaultPath)
  789. {
  790. // It is likely that CMake will have recently built the project.
  791. for(int i=1; i <= 10; ++i)
  792. {
  793. cmOStringStream r;
  794. r <<
  795. "[HKEY_CURRENT_USER\\Software\\Kitware\\CMakeSetup\\"
  796. "Settings\\StartPath;WhereBuild" << i << "]";
  797. std::string f = r.str();
  798. cmSystemTools::ExpandRegistryValues(f);
  799. cmSystemTools::ConvertToUnixSlashes(f);
  800. if(cmSystemTools::FileIsFullPath(f.c_str()) &&
  801. cmSystemTools::FileIsDirectory(f.c_str()))
  802. {
  803. this->AddPathInternal(prefixes, f, FullPath, &emmitted);
  804. }
  805. }
  806. }
  807. if(!this->NoCMakeSystemPath && !this->NoDefaultPath)
  808. {
  809. this->AddCMakePath(prefixes, "CMAKE_SYSTEM_PREFIX_PATH", &emmitted);
  810. this->AddCMakePath(prefixes, "CMAKE_SYSTEM_FRAMEWORK_PATH", &emmitted);
  811. this->AddCMakePath(prefixes, "CMAKE_SYSTEM_APPBUNDLE_PATH", &emmitted);
  812. }
  813. if(!this->UserPaths.empty())
  814. {
  815. // Add paths specified by the caller.
  816. this->AddPathsInternal(prefixes, this->UserPaths, CMakePath, &emmitted);
  817. }
  818. // Construct the final set of prefixes.
  819. this->RerootPaths(prefixes);
  820. // Add a trailing slash to all prefixes to aid the search process.
  821. for(std::vector<std::string>::iterator i = prefixes.begin();
  822. i != prefixes.end(); ++i)
  823. {
  824. std::string& prefix = *i;
  825. if(prefix[prefix.size()-1] != '/')
  826. {
  827. prefix += "/";
  828. }
  829. }
  830. }
  831. //----------------------------------------------------------------------------
  832. bool cmFindPackageCommand::SearchDirectory(std::string const& dir)
  833. {
  834. assert(!dir.empty() && dir[dir.size()-1] == '/');
  835. // Check each path suffix on this directory.
  836. for(std::vector<std::string>::const_iterator
  837. si = this->SearchPathSuffixes.begin();
  838. si != this->SearchPathSuffixes.end(); ++si)
  839. {
  840. std::string d = dir;
  841. if(!si->empty())
  842. {
  843. d += *si;
  844. d += "/";
  845. }
  846. if(this->CheckDirectory(d))
  847. {
  848. return true;
  849. }
  850. }
  851. return false;
  852. }
  853. //----------------------------------------------------------------------------
  854. bool cmFindPackageCommand::CheckDirectory(std::string const& dir)
  855. {
  856. assert(!dir.empty() && dir[dir.size()-1] == '/');
  857. // Look for the file in this directory.
  858. std::string d = dir.substr(0, dir.size()-1);
  859. if(this->FindConfigFile(d, this->FileFound))
  860. {
  861. return true;
  862. }
  863. return false;
  864. }
  865. //----------------------------------------------------------------------------
  866. bool cmFindPackageCommand::FindConfigFile(std::string const& dir,
  867. std::string& file)
  868. {
  869. for(std::vector<std::string>::const_iterator ci = this->Configs.begin();
  870. ci != this->Configs.end(); ++ci)
  871. {
  872. file = dir;
  873. file += "/";
  874. file += *ci;
  875. if(this->DebugMode)
  876. {
  877. fprintf(stderr, "Checking file [%s]\n", file.c_str());
  878. }
  879. if(cmSystemTools::FileExists(file.c_str(), true))
  880. {
  881. return true;
  882. }
  883. }
  884. return false;
  885. }
  886. //----------------------------------------------------------------------------
  887. #include <cmsys/Directory.hxx>
  888. #include <cmsys/Glob.hxx>
  889. #include <cmsys/String.h>
  890. #include <cmsys/auto_ptr.hxx>
  891. class cmFileList;
  892. class cmFileListGeneratorBase
  893. {
  894. protected:
  895. bool Consider(std::string const& fullPath, cmFileList& listing);
  896. private:
  897. bool Search(cmFileList&);
  898. virtual bool Search(std::string const& parent, cmFileList&) = 0;
  899. virtual cmsys::auto_ptr<cmFileListGeneratorBase> Clone() const = 0;
  900. friend class cmFileList;
  901. cmFileListGeneratorBase* SetNext(cmFileListGeneratorBase const& next);
  902. cmsys::auto_ptr<cmFileListGeneratorBase> Next;
  903. };
  904. class cmFileList
  905. {
  906. public:
  907. cmFileList(): First(), Last(0) {}
  908. cmFileList& operator/(cmFileListGeneratorBase const& rhs)
  909. {
  910. if(this->Last)
  911. {
  912. this->Last = this->Last->SetNext(rhs);
  913. }
  914. else
  915. {
  916. this->First = rhs.Clone();
  917. this->Last = this->First.get();
  918. }
  919. return *this;
  920. }
  921. bool Search()
  922. {
  923. if(this->First.get())
  924. {
  925. return this->First->Search(*this);
  926. }
  927. return false;
  928. }
  929. private:
  930. virtual bool Visit(std::string const& fullPath) = 0;
  931. friend class cmFileListGeneratorBase;
  932. cmsys::auto_ptr<cmFileListGeneratorBase> First;
  933. cmFileListGeneratorBase* Last;
  934. };
  935. class cmFindPackageFileList: public cmFileList
  936. {
  937. public:
  938. cmFindPackageFileList(cmFindPackageCommand* fpc,
  939. bool use_suffixes = true):
  940. cmFileList(), FPC(fpc), UseSuffixes(use_suffixes) {}
  941. private:
  942. bool Visit(std::string const& fullPath)
  943. {
  944. if(this->UseSuffixes)
  945. {
  946. return this->FPC->SearchDirectory(fullPath);
  947. }
  948. else
  949. {
  950. return this->FPC->CheckDirectory(fullPath);
  951. }
  952. }
  953. cmFindPackageCommand* FPC;
  954. bool UseSuffixes;
  955. };
  956. bool cmFileListGeneratorBase::Search(cmFileList& listing)
  957. {
  958. return this->Search("", listing);
  959. }
  960. cmFileListGeneratorBase*
  961. cmFileListGeneratorBase::SetNext(cmFileListGeneratorBase const& next)
  962. {
  963. this->Next = next.Clone();
  964. return this->Next.get();
  965. }
  966. bool cmFileListGeneratorBase::Consider(std::string const& fullPath,
  967. cmFileList& listing)
  968. {
  969. if(this->Next.get())
  970. {
  971. return this->Next->Search(fullPath + "/", listing);
  972. }
  973. else
  974. {
  975. return listing.Visit(fullPath + "/");
  976. }
  977. }
  978. class cmFileListGeneratorFixed: public cmFileListGeneratorBase
  979. {
  980. public:
  981. cmFileListGeneratorFixed(std::string const& str):
  982. cmFileListGeneratorBase(), String(str) {}
  983. cmFileListGeneratorFixed(cmFileListGeneratorFixed const& r):
  984. cmFileListGeneratorBase(), String(r.String) {}
  985. private:
  986. std::string String;
  987. virtual bool Search(std::string const& parent, cmFileList& lister)
  988. {
  989. std::string fullPath = parent + this->String;
  990. return this->Consider(fullPath, lister);
  991. }
  992. virtual cmsys::auto_ptr<cmFileListGeneratorBase> Clone() const
  993. {
  994. cmsys::auto_ptr<cmFileListGeneratorBase>
  995. g(new cmFileListGeneratorFixed(*this));
  996. return g;
  997. }
  998. };
  999. class cmFileListGeneratorEnumerate: public cmFileListGeneratorBase
  1000. {
  1001. public:
  1002. cmFileListGeneratorEnumerate(const char* p1, const char* p2):
  1003. cmFileListGeneratorBase()
  1004. {
  1005. this->Vector.push_back(p1);
  1006. this->Vector.push_back(p2);
  1007. }
  1008. cmFileListGeneratorEnumerate(cmFileListGeneratorEnumerate const& r):
  1009. cmFileListGeneratorBase(), Vector(r.Vector) {}
  1010. private:
  1011. std::vector<std::string> Vector;
  1012. virtual bool Search(std::string const& parent, cmFileList& lister)
  1013. {
  1014. for(std::vector<std::string>::const_iterator i = this->Vector.begin();
  1015. i != this->Vector.end(); ++i)
  1016. {
  1017. if(this->Consider(parent + *i, lister))
  1018. {
  1019. return true;
  1020. }
  1021. }
  1022. return false;
  1023. }
  1024. virtual cmsys::auto_ptr<cmFileListGeneratorBase> Clone() const
  1025. {
  1026. cmsys::auto_ptr<cmFileListGeneratorBase>
  1027. g(new cmFileListGeneratorEnumerate(*this));
  1028. return g;
  1029. }
  1030. };
  1031. class cmFileListGeneratorProject: public cmFileListGeneratorBase
  1032. {
  1033. public:
  1034. cmFileListGeneratorProject(std::vector<std::string> const& names):
  1035. cmFileListGeneratorBase(), Names(names) {}
  1036. cmFileListGeneratorProject(cmFileListGeneratorProject const& r):
  1037. cmFileListGeneratorBase(), Names(r.Names) {}
  1038. private:
  1039. std::vector<std::string> const& Names;
  1040. virtual bool Search(std::string const& parent, cmFileList& lister)
  1041. {
  1042. // Construct a list of matches.
  1043. std::vector<std::string> matches;
  1044. cmsys::Directory d;
  1045. d.Load(parent.c_str());
  1046. for(unsigned long i=0; i < d.GetNumberOfFiles(); ++i)
  1047. {
  1048. const char* fname = d.GetFile(i);
  1049. if(strcmp(fname, ".") == 0 ||
  1050. strcmp(fname, "..") == 0)
  1051. {
  1052. continue;
  1053. }
  1054. for(std::vector<std::string>::const_iterator ni = this->Names.begin();
  1055. ni != this->Names.end(); ++ni)
  1056. {
  1057. if(cmsysString_strncasecmp(fname, ni->c_str(),
  1058. ni->length()) == 0)
  1059. {
  1060. matches.push_back(fname);
  1061. }
  1062. }
  1063. }
  1064. for(std::vector<std::string>::const_iterator i = matches.begin();
  1065. i != matches.end(); ++i)
  1066. {
  1067. if(this->Consider(parent + *i, lister))
  1068. {
  1069. return true;
  1070. }
  1071. }
  1072. return false;
  1073. }
  1074. virtual cmsys::auto_ptr<cmFileListGeneratorBase> Clone() const
  1075. {
  1076. cmsys::auto_ptr<cmFileListGeneratorBase>
  1077. g(new cmFileListGeneratorProject(*this));
  1078. return g;
  1079. }
  1080. };
  1081. class cmFileListGeneratorMacProject: public cmFileListGeneratorBase
  1082. {
  1083. public:
  1084. cmFileListGeneratorMacProject(std::vector<std::string> const& names,
  1085. const char* ext):
  1086. cmFileListGeneratorBase(), Names(names), Extension(ext) {}
  1087. cmFileListGeneratorMacProject(cmFileListGeneratorMacProject const& r):
  1088. cmFileListGeneratorBase(), Names(r.Names), Extension(r.Extension) {}
  1089. private:
  1090. std::vector<std::string> const& Names;
  1091. std::string Extension;
  1092. virtual bool Search(std::string const& parent, cmFileList& lister)
  1093. {
  1094. // Construct a list of matches.
  1095. std::vector<std::string> matches;
  1096. cmsys::Directory d;
  1097. d.Load(parent.c_str());
  1098. for(unsigned long i=0; i < d.GetNumberOfFiles(); ++i)
  1099. {
  1100. const char* fname = d.GetFile(i);
  1101. if(strcmp(fname, ".") == 0 ||
  1102. strcmp(fname, "..") == 0)
  1103. {
  1104. continue;
  1105. }
  1106. for(std::vector<std::string>::const_iterator ni = this->Names.begin();
  1107. ni != this->Names.end(); ++ni)
  1108. {
  1109. std::string name = *ni;
  1110. name += this->Extension;
  1111. if(cmsysString_strcasecmp(fname, name.c_str()) == 0)
  1112. {
  1113. matches.push_back(fname);
  1114. }
  1115. }
  1116. }
  1117. for(std::vector<std::string>::const_iterator i = matches.begin();
  1118. i != matches.end(); ++i)
  1119. {
  1120. if(this->Consider(parent + *i, lister))
  1121. {
  1122. return true;
  1123. }
  1124. }
  1125. return false;
  1126. }
  1127. virtual cmsys::auto_ptr<cmFileListGeneratorBase> Clone() const
  1128. {
  1129. cmsys::auto_ptr<cmFileListGeneratorBase>
  1130. g(new cmFileListGeneratorMacProject(*this));
  1131. return g;
  1132. }
  1133. };
  1134. class cmFileListGeneratorCaseInsensitive: public cmFileListGeneratorBase
  1135. {
  1136. public:
  1137. cmFileListGeneratorCaseInsensitive(std::string const& str):
  1138. cmFileListGeneratorBase(), String(str) {}
  1139. cmFileListGeneratorCaseInsensitive(cmFileListGeneratorCaseInsensitive const& r):
  1140. cmFileListGeneratorBase(), String(r.String) {}
  1141. private:
  1142. std::string String;
  1143. virtual bool Search(std::string const& parent, cmFileList& lister)
  1144. {
  1145. // Look for matching files.
  1146. std::vector<std::string> matches;
  1147. cmsys::Directory d;
  1148. d.Load(parent.c_str());
  1149. for(unsigned long i=0; i < d.GetNumberOfFiles(); ++i)
  1150. {
  1151. const char* fname = d.GetFile(i);
  1152. if(strcmp(fname, ".") == 0 ||
  1153. strcmp(fname, "..") == 0)
  1154. {
  1155. continue;
  1156. }
  1157. if(cmsysString_strcasecmp(fname, this->String.c_str()) == 0)
  1158. {
  1159. if(this->Consider(parent + fname, lister))
  1160. {
  1161. return true;
  1162. }
  1163. }
  1164. }
  1165. return false;
  1166. }
  1167. virtual cmsys::auto_ptr<cmFileListGeneratorBase> Clone() const
  1168. {
  1169. cmsys::auto_ptr<cmFileListGeneratorBase>
  1170. g(new cmFileListGeneratorCaseInsensitive(*this));
  1171. return g;
  1172. }
  1173. };
  1174. class cmFileListGeneratorGlob: public cmFileListGeneratorBase
  1175. {
  1176. public:
  1177. cmFileListGeneratorGlob(std::string const& str):
  1178. cmFileListGeneratorBase(), Pattern(str) {}
  1179. cmFileListGeneratorGlob(cmFileListGeneratorGlob const& r):
  1180. cmFileListGeneratorBase(), Pattern(r.Pattern) {}
  1181. private:
  1182. std::string Pattern;
  1183. virtual bool Search(std::string const& parent, cmFileList& lister)
  1184. {
  1185. // Glob the set of matching files.
  1186. std::string expr = parent;
  1187. expr += this->Pattern;
  1188. cmsys::Glob g;
  1189. if(!g.FindFiles(expr))
  1190. {
  1191. return false;
  1192. }
  1193. std::vector<std::string> const& files = g.GetFiles();
  1194. // Look for directories among the matches.
  1195. for(std::vector<std::string>::const_iterator fi = files.begin();
  1196. fi != files.end(); ++fi)
  1197. {
  1198. if(cmSystemTools::FileIsDirectory(fi->c_str()))
  1199. {
  1200. if(this->Consider(*fi, lister))
  1201. {
  1202. return true;
  1203. }
  1204. }
  1205. }
  1206. return false;
  1207. }
  1208. virtual cmsys::auto_ptr<cmFileListGeneratorBase> Clone() const
  1209. {
  1210. cmsys::auto_ptr<cmFileListGeneratorBase>
  1211. g(new cmFileListGeneratorGlob(*this));
  1212. return g;
  1213. }
  1214. };
  1215. //----------------------------------------------------------------------------
  1216. bool cmFindPackageCommand::SearchPrefix(std::string const& prefix_in)
  1217. {
  1218. assert(!prefix_in.empty() && prefix_in[prefix_in.size()-1] == '/');
  1219. if(this->DebugMode)
  1220. {
  1221. fprintf(stderr, "Checking prefix [%s]\n", prefix_in.c_str());
  1222. }
  1223. // Skip this if the prefix does not exist.
  1224. if(!cmSystemTools::FileIsDirectory(prefix_in.c_str()))
  1225. {
  1226. return false;
  1227. }
  1228. // PREFIX/ (useful on windows or in build trees)
  1229. if(this->SearchDirectory(prefix_in))
  1230. {
  1231. return true;
  1232. }
  1233. // Strip the trailing slash because the path generator is about to
  1234. // add one.
  1235. std::string prefix = prefix_in.substr(0, prefix_in.size()-1);
  1236. // PREFIX/(cmake|CMake)/ (useful on windows or in build trees)
  1237. {
  1238. cmFindPackageFileList lister(this);
  1239. lister
  1240. / cmFileListGeneratorFixed(prefix)
  1241. / cmFileListGeneratorCaseInsensitive("cmake");
  1242. if(lister.Search())
  1243. {
  1244. return true;
  1245. }
  1246. }
  1247. // PREFIX/(share|lib)/(Foo|foo|FOO).*/
  1248. {
  1249. cmFindPackageFileList lister(this);
  1250. lister
  1251. / cmFileListGeneratorFixed(prefix)
  1252. / cmFileListGeneratorEnumerate("lib", "share")
  1253. / cmFileListGeneratorProject(this->Names);
  1254. if(lister.Search())
  1255. {
  1256. return true;
  1257. }
  1258. }
  1259. // PREFIX/(share|lib)/(Foo|foo|FOO).*/(cmake|CMake)/
  1260. {
  1261. cmFindPackageFileList lister(this);
  1262. lister
  1263. / cmFileListGeneratorFixed(prefix)
  1264. / cmFileListGeneratorEnumerate("lib", "share")
  1265. / cmFileListGeneratorProject(this->Names)
  1266. / cmFileListGeneratorCaseInsensitive("cmake");
  1267. if(lister.Search())
  1268. {
  1269. return true;
  1270. }
  1271. }
  1272. return false;
  1273. }
  1274. //----------------------------------------------------------------------------
  1275. bool cmFindPackageCommand::SearchFrameworkPrefix(std::string const& prefix_in)
  1276. {
  1277. assert(!prefix_in.empty() && prefix_in[prefix_in.size()-1] == '/');
  1278. if(this->DebugMode)
  1279. {
  1280. fprintf(stderr, "Checking framework prefix [%s]\n", prefix_in.c_str());
  1281. }
  1282. // Strip the trailing slash because the path generator is about to
  1283. // add one.
  1284. std::string prefix = prefix_in.substr(0, prefix_in.size()-1);
  1285. // <prefix>/Foo.framework/Resources/
  1286. {
  1287. cmFindPackageFileList lister(this);
  1288. lister
  1289. / cmFileListGeneratorFixed(prefix)
  1290. / cmFileListGeneratorMacProject(this->Names, ".framework")
  1291. / cmFileListGeneratorFixed("Resources");
  1292. if(lister.Search())
  1293. {
  1294. return true;
  1295. }
  1296. }
  1297. // <prefix>/Foo.framework/Resources/CMake/
  1298. {
  1299. cmFindPackageFileList lister(this);
  1300. lister
  1301. / cmFileListGeneratorFixed(prefix)
  1302. / cmFileListGeneratorMacProject(this->Names, ".framework")
  1303. / cmFileListGeneratorFixed("Resources")
  1304. / cmFileListGeneratorCaseInsensitive("cmake");
  1305. if(lister.Search())
  1306. {
  1307. return true;
  1308. }
  1309. }
  1310. // <prefix>/Foo.framework/Versions/*/Resources/
  1311. {
  1312. cmFindPackageFileList lister(this);
  1313. lister
  1314. / cmFileListGeneratorFixed(prefix)
  1315. / cmFileListGeneratorMacProject(this->Names, ".framework")
  1316. / cmFileListGeneratorFixed("Versions")
  1317. / cmFileListGeneratorGlob("*/Resources");
  1318. if(lister.Search())
  1319. {
  1320. return true;
  1321. }
  1322. }
  1323. // <prefix>/Foo.framework/Versions/*/Resources/CMake/
  1324. {
  1325. cmFindPackageFileList lister(this);
  1326. lister
  1327. / cmFileListGeneratorFixed(prefix)
  1328. / cmFileListGeneratorMacProject(this->Names, ".framework")
  1329. / cmFileListGeneratorFixed("Versions")
  1330. / cmFileListGeneratorGlob("*/Resources")
  1331. / cmFileListGeneratorCaseInsensitive("cmake");
  1332. if(lister.Search())
  1333. {
  1334. return true;
  1335. }
  1336. }
  1337. return false;
  1338. }
  1339. //----------------------------------------------------------------------------
  1340. bool cmFindPackageCommand::SearchAppBundlePrefix(std::string const& prefix_in)
  1341. {
  1342. assert(!prefix_in.empty() && prefix_in[prefix_in.size()-1] == '/');
  1343. if(this->DebugMode)
  1344. {
  1345. fprintf(stderr, "Checking bundle prefix [%s]\n", prefix_in.c_str());
  1346. }
  1347. // Strip the trailing slash because the path generator is about to
  1348. // add one.
  1349. std::string prefix = prefix_in.substr(0, prefix_in.size()-1);
  1350. // <prefix>/Foo.app/Contents/Resources
  1351. {
  1352. cmFindPackageFileList lister(this);
  1353. lister
  1354. / cmFileListGeneratorFixed(prefix)
  1355. / cmFileListGeneratorMacProject(this->Names, ".app")
  1356. / cmFileListGeneratorFixed("Contents/Resources");
  1357. if(lister.Search())
  1358. {
  1359. return true;
  1360. }
  1361. }
  1362. // <prefix>/Foo.app/Contents/Resources/CMake
  1363. {
  1364. cmFindPackageFileList lister(this);
  1365. lister
  1366. / cmFileListGeneratorFixed(prefix)
  1367. / cmFileListGeneratorMacProject(this->Names, ".app")
  1368. / cmFileListGeneratorFixed("Contents/Resources")
  1369. / cmFileListGeneratorCaseInsensitive("cmake");
  1370. if(lister.Search())
  1371. {
  1372. return true;
  1373. }
  1374. }
  1375. return false;
  1376. }
  1377. // TODO: Version numbers? Perhaps have a listing component class that
  1378. // sorts by lexicographic and numerical ordering. Also try to match
  1379. // some command argument for the version. Alternatively provide an
  1380. // API that just returns a list of valid directories? Perhaps push a
  1381. // scope and try loading the target file just to get its version
  1382. // number? Could add a foo-version.cmake or FooVersion.cmake file
  1383. // in the projects that contains just version information.
  1384. // TODO: Debug cmsys::Glob double slash problem.
  1385. // TODO: Add registry entries after cmake system search path?
  1386. // Currently the user must specify them with the PATHS option.
  1387. //
  1388. // [HKEY_CURRENT_USER\Software\*\Foo*;InstallDir]
  1389. // [HKEY_CURRENT_USER\Software\*\*\Foo*;InstallDir]
  1390. // [HKEY_LOCAL_MACHINE\Software\*\Foo*;InstallDir]
  1391. // [HKEY_LOCAL_MACHINE\Software\*\*\Foo*;InstallDir]