cmFindPackageCommand.cxx 69 KB

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