cmFindPackageCommand.cxx 72 KB

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