cmFindPackageCommand.cxx 69 KB

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