cmFindPackageCommand.cxx 87 KB

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