cmFindPackageCommand.cxx 69 KB

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