cmFindPackageCommand.cxx 73 KB

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