cmFindPackageCommand.cxx 87 KB

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