cmFindPackageCommand.cxx 86 KB

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