cmFindPackageCommand.cxx 69 KB

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