cmFindPackageCommand.cxx 87 KB

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