cmFindPackageCommand.cxx 87 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648
  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. this->AddFindDefinition("CMAKE_FIND_PACKAGE_NAME", this->Name.c_str());
  801. // Store the list of components.
  802. std::string components_var = this->Name + "_FIND_COMPONENTS";
  803. this->AddFindDefinition(components_var.c_str(), components.c_str());
  804. if(this->Quiet)
  805. {
  806. // Tell the module that is about to be read that it should find
  807. // quietly.
  808. std::string quietly = this->Name;
  809. quietly += "_FIND_QUIETLY";
  810. this->AddFindDefinition(quietly.c_str(), "1");
  811. }
  812. if(this->Required)
  813. {
  814. // Tell the module that is about to be read that it should report
  815. // a fatal error if the package is not found.
  816. std::string req = this->Name;
  817. req += "_FIND_REQUIRED";
  818. this->AddFindDefinition(req.c_str(), "1");
  819. }
  820. if(!this->Version.empty())
  821. {
  822. // Tell the module that is about to be read what version of the
  823. // package has been requested.
  824. std::string ver = this->Name;
  825. ver += "_FIND_VERSION";
  826. this->AddFindDefinition(ver.c_str(), this->Version.c_str());
  827. char buf[64];
  828. sprintf(buf, "%u", this->VersionMajor);
  829. this->AddFindDefinition((ver+"_MAJOR").c_str(), buf);
  830. sprintf(buf, "%u", this->VersionMinor);
  831. this->AddFindDefinition((ver+"_MINOR").c_str(), buf);
  832. sprintf(buf, "%u", this->VersionPatch);
  833. this->AddFindDefinition((ver+"_PATCH").c_str(), buf);
  834. sprintf(buf, "%u", this->VersionTweak);
  835. this->AddFindDefinition((ver+"_TWEAK").c_str(), buf);
  836. sprintf(buf, "%u", this->VersionCount);
  837. this->AddFindDefinition((ver+"_COUNT").c_str(), buf);
  838. // Tell the module whether an exact version has been requested.
  839. std::string exact = this->Name;
  840. exact += "_FIND_VERSION_EXACT";
  841. this->AddFindDefinition(exact.c_str(), this->VersionExact? "1":"0");
  842. }
  843. }
  844. //----------------------------------------------------------------------------
  845. void cmFindPackageCommand::AddFindDefinition(const char* var, const char* val)
  846. {
  847. if(const char* old = this->Makefile->GetDefinition(var))
  848. {
  849. this->OriginalDefs[var].exists = true;
  850. this->OriginalDefs[var].value = old;
  851. }
  852. else
  853. {
  854. this->OriginalDefs[var].exists = false;
  855. }
  856. this->Makefile->AddDefinition(var, val);
  857. }
  858. //----------------------------------------------------------------------------
  859. void cmFindPackageCommand::RestoreFindDefinitions()
  860. {
  861. for(std::map<cmStdString, OriginalDef>::iterator
  862. i = this->OriginalDefs.begin(); i != this->OriginalDefs.end(); ++i)
  863. {
  864. OriginalDef const& od = i->second;
  865. if(od.exists)
  866. {
  867. this->Makefile->AddDefinition(i->first.c_str(), od.value.c_str());
  868. }
  869. else
  870. {
  871. this->Makefile->RemoveDefinition(i->first.c_str());
  872. }
  873. }
  874. }
  875. //----------------------------------------------------------------------------
  876. bool cmFindPackageCommand::FindModule(bool& found)
  877. {
  878. std::string module = "Find";
  879. module += this->Name;
  880. module += ".cmake";
  881. std::string mfile = this->Makefile->GetModulesFile(module.c_str());
  882. if ( mfile.size() )
  883. {
  884. // Load the module we found, and set "<name>_FIND_MODULE" to true
  885. // while inside it.
  886. found = true;
  887. std::string var = this->Name;
  888. var += "_FIND_MODULE";
  889. this->Makefile->AddDefinition(var.c_str(), "1");
  890. bool result = this->ReadListFile(mfile.c_str(), DoPolicyScope);
  891. this->Makefile->RemoveDefinition(var.c_str());
  892. return result;
  893. }
  894. return true;
  895. }
  896. //----------------------------------------------------------------------------
  897. bool cmFindPackageCommand::HandlePackageMode()
  898. {
  899. this->ConsideredConfigs.clear();
  900. // Support old capitalization behavior.
  901. std::string upperDir = cmSystemTools::UpperCase(this->Name);
  902. std::string upperFound = cmSystemTools::UpperCase(this->Name);
  903. upperDir += "_DIR";
  904. upperFound += "_FOUND";
  905. if(upperDir == this->Variable)
  906. {
  907. this->Compatibility_1_6 = false;
  908. }
  909. // Try to find the config file.
  910. const char* def = this->Makefile->GetDefinition(this->Variable.c_str());
  911. if(this->Compatibility_1_6 && cmSystemTools::IsOff(def))
  912. {
  913. // Use the setting of the old name of the variable to provide the
  914. // value of the new.
  915. const char* oldDef = this->Makefile->GetDefinition(upperDir.c_str());
  916. if(!cmSystemTools::IsOff(oldDef))
  917. {
  918. this->Makefile->AddDefinition(this->Variable.c_str(), oldDef);
  919. def = this->Makefile->GetDefinition(this->Variable.c_str());
  920. }
  921. }
  922. // Try to load the config file if the directory is known
  923. bool fileFound = false;
  924. if (this->UseConfigFiles)
  925. {
  926. if(!cmSystemTools::IsOff(def))
  927. {
  928. // Get the directory from the variable value.
  929. std::string dir = def;
  930. cmSystemTools::ConvertToUnixSlashes(dir);
  931. // Treat relative paths with respect to the current source dir.
  932. if(!cmSystemTools::FileIsFullPath(dir.c_str()))
  933. {
  934. dir = "/" + dir;
  935. dir = this->Makefile->GetCurrentDirectory() + dir;
  936. }
  937. // The file location was cached. Look for the correct file.
  938. std::string file;
  939. if (this->FindConfigFile(dir, file))
  940. {
  941. this->FileFound = file;
  942. fileFound = true;
  943. }
  944. def = this->Makefile->GetDefinition(this->Variable.c_str());
  945. }
  946. // Search for the config file if it is not already found.
  947. if(cmSystemTools::IsOff(def) || !fileFound)
  948. {
  949. fileFound = this->FindConfig();
  950. def = this->Makefile->GetDefinition(this->Variable.c_str());
  951. }
  952. // Sanity check.
  953. if(fileFound && this->FileFound.empty())
  954. {
  955. this->Makefile->IssueMessage(
  956. cmake::INTERNAL_ERROR, "fileFound is true but FileFound is empty!");
  957. fileFound = false;
  958. }
  959. }
  960. std::string foundVar = this->Name;
  961. foundVar += "_FOUND";
  962. std::string notFoundMessageVar = this->Name;
  963. notFoundMessageVar += "_NOT_FOUND_MESSAGE";
  964. std::string notFoundMessage;
  965. // If the directory for the config file was found, try to read the file.
  966. bool result = true;
  967. bool found = false;
  968. bool configFileSetFOUNDFalse = false;
  969. if(fileFound)
  970. {
  971. if ((this->Makefile->IsDefinitionSet(foundVar.c_str()))
  972. && (this->Makefile->IsOn(foundVar.c_str()) == false))
  973. {
  974. // by removing Foo_FOUND here if it is FALSE, we don't really change
  975. // the situation for the Config file which is about to be included,
  976. // but we make it possible to detect later on whether the Config file
  977. // has set Foo_FOUND to FALSE itself:
  978. this->Makefile->RemoveDefinition(foundVar.c_str());
  979. }
  980. this->Makefile->RemoveDefinition(notFoundMessageVar.c_str());
  981. // Set the version variables before loading the config file.
  982. // It may override them.
  983. this->StoreVersionFound();
  984. // Parse the configuration file.
  985. if(this->ReadListFile(this->FileFound.c_str(), DoPolicyScope))
  986. {
  987. // The package has been found.
  988. found = true;
  989. // Check whether the Config file has set Foo_FOUND to FALSE:
  990. if ((this->Makefile->IsDefinitionSet(foundVar.c_str()))
  991. && (this->Makefile->IsOn(foundVar.c_str()) == false))
  992. {
  993. // we get here if the Config file has set Foo_FOUND actively to FALSE
  994. found = false;
  995. configFileSetFOUNDFalse = true;
  996. notFoundMessage = this->Makefile->GetSafeDefinition(
  997. notFoundMessageVar.c_str());
  998. }
  999. }
  1000. else
  1001. {
  1002. // The configuration file is invalid.
  1003. result = false;
  1004. }
  1005. }
  1006. if (result && !found && (!this->Quiet || this->Required))
  1007. {
  1008. // The variable is not set.
  1009. cmOStringStream e;
  1010. cmOStringStream aw;
  1011. if (configFileSetFOUNDFalse)
  1012. {
  1013. e << "Found package configuration file:\n"
  1014. " " << this->FileFound << "\n"
  1015. "but it set " << foundVar << " to FALSE so package \"" <<
  1016. this->Name << "\" is considered to be NOT FOUND.";
  1017. if (!notFoundMessage.empty())
  1018. {
  1019. e << " Reason given by package: \n" << notFoundMessage << "\n";
  1020. }
  1021. }
  1022. // If there are files in ConsideredConfigs, it means that FooConfig.cmake
  1023. // have been found, but they didn't have appropriate versions.
  1024. else if (this->ConsideredConfigs.size() > 0)
  1025. {
  1026. e << "Could not find a configuration file for package \""
  1027. << this->Name << "\" that "
  1028. << (this->VersionExact? "exactly matches" : "is compatible with")
  1029. << " requested version \"" << this->Version << "\".\n"
  1030. << "The following configuration files were considered but not "
  1031. "accepted:\n";
  1032. for(std::vector<ConfigFileInfo>::size_type i=0;
  1033. i<this->ConsideredConfigs.size(); i++)
  1034. {
  1035. e << " " << this->ConsideredConfigs[i].filename
  1036. << ", version: " << this->ConsideredConfigs[i].version << "\n";
  1037. }
  1038. }
  1039. else
  1040. {
  1041. std::string requestedVersionString;
  1042. if(!this->Version.empty())
  1043. {
  1044. requestedVersionString = " (requested version ";
  1045. requestedVersionString += this->Version;
  1046. requestedVersionString += ")";
  1047. }
  1048. if (this->UseConfigFiles)
  1049. {
  1050. if(this->UseFindModules)
  1051. {
  1052. e << "By not providing \"Find" << this->Name << ".cmake\" in "
  1053. "CMAKE_MODULE_PATH this project has asked CMake to find a "
  1054. "package configuration file provided by \""<<this->Name<< "\", "
  1055. "but CMake did not find one.\n";
  1056. }
  1057. if(this->Configs.size() == 1)
  1058. {
  1059. e << "Could not find a package configuration file named \""
  1060. << this->Configs[0] << "\" provided by package \""
  1061. << this->Name << "\"" << requestedVersionString <<".\n";
  1062. }
  1063. else
  1064. {
  1065. e << "Could not find a package configuration file provided by \""
  1066. << this->Name << "\"" << requestedVersionString
  1067. << " with any of the following names:\n";
  1068. for(std::vector<std::string>::const_iterator ci =
  1069. this->Configs.begin();
  1070. ci != this->Configs.end(); ++ci)
  1071. {
  1072. e << " " << *ci << "\n";
  1073. }
  1074. }
  1075. e << "Add the installation prefix of \"" << this->Name << "\" to "
  1076. "CMAKE_PREFIX_PATH or set \"" << this->Variable << "\" to a "
  1077. "directory containing one of the above files. "
  1078. "If \"" << this->Name << "\" provides a separate development "
  1079. "package or SDK, be sure it has been installed.";
  1080. }
  1081. else // if(!this->UseFindModules && !this->UseConfigFiles)
  1082. {
  1083. e << "No \"Find" << this->Name << ".cmake\" found in "
  1084. << "CMAKE_MODULE_PATH.";
  1085. aw<< "Find"<< this->Name <<".cmake must either be part of this "
  1086. "project itself, in this case adjust CMAKE_MODULE_PATH so that "
  1087. "it points to the correct location inside its source tree.\n"
  1088. "Or it must be installed by a package which has already been "
  1089. "found via find_package(). In this case make sure that "
  1090. "package has indeed been found and adjust CMAKE_MODULE_PATH to "
  1091. "contain the location where that package has installed "
  1092. "Find" << this->Name << ".cmake. This must be a location "
  1093. "provided by that package. This error in general means that "
  1094. "the buildsystem of this project is relying on a Find-module "
  1095. "without ensuring that it is actually available.\n";
  1096. }
  1097. }
  1098. this->Makefile->IssueMessage(
  1099. this->Required? cmake::FATAL_ERROR : cmake::WARNING, e.str());
  1100. if (this->Required)
  1101. {
  1102. cmSystemTools::SetFatalErrorOccured();
  1103. }
  1104. if (!aw.str().empty())
  1105. {
  1106. this->Makefile->IssueMessage(cmake::AUTHOR_WARNING,aw.str());
  1107. }
  1108. }
  1109. // Set a variable marking whether the package was found.
  1110. this->Makefile->AddDefinition(foundVar.c_str(), found? "1":"0");
  1111. // Set a variable naming the configuration file that was found.
  1112. std::string fileVar = this->Name;
  1113. fileVar += "_CONFIG";
  1114. if(found)
  1115. {
  1116. this->Makefile->AddDefinition(fileVar.c_str(), this->FileFound.c_str());
  1117. }
  1118. else
  1119. {
  1120. this->Makefile->RemoveDefinition(fileVar.c_str());
  1121. }
  1122. // Handle some ancient compatibility stuff.
  1123. if(this->Compatibility_1_6)
  1124. {
  1125. // Listfiles will be looking for the capitalized version of the
  1126. // name. Provide it.
  1127. this->Makefile->AddDefinition
  1128. (upperDir.c_str(),
  1129. this->Makefile->GetDefinition(this->Variable.c_str()));
  1130. this->Makefile->AddDefinition
  1131. (upperFound.c_str(),
  1132. this->Makefile->GetDefinition(foundVar.c_str()));
  1133. }
  1134. #ifdef CMAKE_BUILD_WITH_CMAKE
  1135. if(!(upperDir == this->Variable))
  1136. {
  1137. if(this->Compatibility_1_6)
  1138. {
  1139. // Listfiles may use the capitalized version of the name.
  1140. // Remove any previously added watch.
  1141. this->Makefile->GetVariableWatch()->RemoveWatch(
  1142. upperDir.c_str(),
  1143. cmFindPackageNeedBackwardsCompatibility
  1144. );
  1145. }
  1146. else
  1147. {
  1148. // Listfiles should not be using the capitalized version of the
  1149. // name. Add a watch to warn the user.
  1150. this->Makefile->GetVariableWatch()->AddWatch(
  1151. upperDir.c_str(),
  1152. cmFindPackageNeedBackwardsCompatibility
  1153. );
  1154. }
  1155. }
  1156. #endif
  1157. std::string consideredConfigsVar = this->Name;
  1158. consideredConfigsVar += "_CONSIDERED_CONFIGS";
  1159. std::string consideredVersionsVar = this->Name;
  1160. consideredVersionsVar += "_CONSIDERED_VERSIONS";
  1161. std::string consideredConfigFiles;
  1162. std::string consideredVersions;
  1163. const char* sep = "";
  1164. for(std::vector<ConfigFileInfo>::size_type i=0;
  1165. i<this->ConsideredConfigs.size(); i++)
  1166. {
  1167. consideredConfigFiles += sep;
  1168. consideredVersions += sep;
  1169. consideredConfigFiles += this->ConsideredConfigs[i].filename;
  1170. consideredVersions += this->ConsideredConfigs[i].version;
  1171. sep = ";";
  1172. }
  1173. this->Makefile->AddDefinition(consideredConfigsVar.c_str(),
  1174. consideredConfigFiles.c_str());
  1175. this->Makefile->AddDefinition(consideredVersionsVar.c_str(),
  1176. consideredVersions.c_str());
  1177. return result;
  1178. }
  1179. //----------------------------------------------------------------------------
  1180. bool cmFindPackageCommand::FindConfig()
  1181. {
  1182. // Compute the set of search prefixes.
  1183. this->ComputePrefixes();
  1184. // Look for the project's configuration file.
  1185. bool found = false;
  1186. // Search for frameworks.
  1187. if(!found && (this->SearchFrameworkFirst || this->SearchFrameworkOnly))
  1188. {
  1189. found = this->FindFrameworkConfig();
  1190. }
  1191. // Search for apps.
  1192. if(!found && (this->SearchAppBundleFirst || this->SearchAppBundleOnly))
  1193. {
  1194. found = this->FindAppBundleConfig();
  1195. }
  1196. // Search prefixes.
  1197. if(!found && !(this->SearchFrameworkOnly || this->SearchAppBundleOnly))
  1198. {
  1199. found = this->FindPrefixedConfig();
  1200. }
  1201. // Search for frameworks.
  1202. if(!found && this->SearchFrameworkLast)
  1203. {
  1204. found = this->FindFrameworkConfig();
  1205. }
  1206. // Search for apps.
  1207. if(!found && this->SearchAppBundleLast)
  1208. {
  1209. found = this->FindAppBundleConfig();
  1210. }
  1211. // Store the entry in the cache so it can be set by the user.
  1212. std::string init;
  1213. if(found)
  1214. {
  1215. init = cmSystemTools::GetFilenamePath(this->FileFound);
  1216. }
  1217. else
  1218. {
  1219. init = this->Variable + "-NOTFOUND";
  1220. }
  1221. std::string help =
  1222. "The directory containing a CMake configuration file for ";
  1223. help += this->Name;
  1224. help += ".";
  1225. // We force the value since we do not get here if it was already set.
  1226. this->Makefile->AddCacheDefinition(this->Variable.c_str(),
  1227. init.c_str(), help.c_str(),
  1228. cmCacheManager::PATH, true);
  1229. return found;
  1230. }
  1231. //----------------------------------------------------------------------------
  1232. bool cmFindPackageCommand::FindPrefixedConfig()
  1233. {
  1234. std::vector<std::string>& prefixes = this->SearchPaths;
  1235. for(std::vector<std::string>::const_iterator pi = prefixes.begin();
  1236. pi != prefixes.end(); ++pi)
  1237. {
  1238. if(this->SearchPrefix(*pi))
  1239. {
  1240. return true;
  1241. }
  1242. }
  1243. return false;
  1244. }
  1245. //----------------------------------------------------------------------------
  1246. bool cmFindPackageCommand::FindFrameworkConfig()
  1247. {
  1248. std::vector<std::string>& prefixes = this->SearchPaths;
  1249. for(std::vector<std::string>::const_iterator i = prefixes.begin();
  1250. i != prefixes.end(); ++i)
  1251. {
  1252. if(this->SearchFrameworkPrefix(*i))
  1253. {
  1254. return true;
  1255. }
  1256. }
  1257. return false;
  1258. }
  1259. //----------------------------------------------------------------------------
  1260. bool cmFindPackageCommand::FindAppBundleConfig()
  1261. {
  1262. std::vector<std::string>& prefixes = this->SearchPaths;
  1263. for(std::vector<std::string>::const_iterator i = prefixes.begin();
  1264. i != prefixes.end(); ++i)
  1265. {
  1266. if(this->SearchAppBundlePrefix(*i))
  1267. {
  1268. return true;
  1269. }
  1270. }
  1271. return false;
  1272. }
  1273. //----------------------------------------------------------------------------
  1274. bool cmFindPackageCommand::ReadListFile(const char* f, PolicyScopeRule psr)
  1275. {
  1276. if(this->Makefile->ReadListFile(this->Makefile->GetCurrentListFile(), f, 0,
  1277. !this->PolicyScope || psr == NoPolicyScope))
  1278. {
  1279. return true;
  1280. }
  1281. std::string e = "Error reading CMake code from \"";
  1282. e += f;
  1283. e += "\".";
  1284. this->SetError(e.c_str());
  1285. return false;
  1286. }
  1287. //----------------------------------------------------------------------------
  1288. void cmFindPackageCommand::AppendToFoundProperty(bool found)
  1289. {
  1290. std::vector<std::string> foundContents;
  1291. const char *foundProp =
  1292. this->Makefile->GetCMakeInstance()->GetProperty("PACKAGES_FOUND");
  1293. if (foundProp && *foundProp)
  1294. {
  1295. std::string tmp = foundProp;
  1296. cmSystemTools::ExpandListArgument(tmp, foundContents, false);
  1297. std::vector<std::string>::iterator nameIt = std::find(
  1298. foundContents.begin(), foundContents.end(), this->Name);
  1299. if(nameIt != foundContents.end())
  1300. {
  1301. foundContents.erase(nameIt);
  1302. }
  1303. }
  1304. std::vector<std::string> notFoundContents;
  1305. const char *notFoundProp =
  1306. this->Makefile->GetCMakeInstance()->GetProperty("PACKAGES_NOT_FOUND");
  1307. if (notFoundProp && *notFoundProp)
  1308. {
  1309. std::string tmp = notFoundProp;
  1310. cmSystemTools::ExpandListArgument(tmp, notFoundContents, false);
  1311. std::vector<std::string>::iterator nameIt = std::find(
  1312. notFoundContents.begin(), notFoundContents.end(), this->Name);
  1313. if(nameIt != notFoundContents.end())
  1314. {
  1315. notFoundContents.erase(nameIt);
  1316. }
  1317. }
  1318. if(found)
  1319. {
  1320. foundContents.push_back(this->Name);
  1321. }
  1322. else
  1323. {
  1324. notFoundContents.push_back(this->Name);
  1325. }
  1326. std::string tmp;
  1327. const char* sep ="";
  1328. for(size_t i=0; i<foundContents.size(); i++)
  1329. {
  1330. tmp += sep;
  1331. tmp += foundContents[i];
  1332. sep = ";";
  1333. }
  1334. this->Makefile->GetCMakeInstance()->SetProperty("PACKAGES_FOUND",
  1335. tmp.c_str());
  1336. tmp = "";
  1337. sep = "";
  1338. for(size_t i=0; i<notFoundContents.size(); i++)
  1339. {
  1340. tmp += sep;
  1341. tmp += notFoundContents[i];
  1342. sep = ";";
  1343. }
  1344. this->Makefile->GetCMakeInstance()->SetProperty("PACKAGES_NOT_FOUND",
  1345. tmp.c_str());
  1346. }
  1347. //----------------------------------------------------------------------------
  1348. void cmFindPackageCommand::AppendSuccessInformation()
  1349. {
  1350. std::string found = this->Name;
  1351. found += "_FOUND";
  1352. std::string upperFound = cmSystemTools::UpperCase(found);
  1353. const char* upperResult = this->Makefile->GetDefinition(upperFound.c_str());
  1354. const char* result = this->Makefile->GetDefinition(found.c_str());
  1355. bool packageFound = ((cmSystemTools::IsOn(result))
  1356. || (cmSystemTools::IsOn(upperResult)));
  1357. this->AppendToFoundProperty(packageFound);
  1358. // Record whether the find was quiet or not, so this can be used
  1359. // e.g. in FeatureSummary.cmake
  1360. std::string quietInfoPropName = "_CMAKE_";
  1361. quietInfoPropName += this->Name;
  1362. quietInfoPropName += "_QUIET";
  1363. this->Makefile->GetCMakeInstance()->SetProperty(quietInfoPropName.c_str(),
  1364. this->Quiet ? "TRUE" : "FALSE");
  1365. // set a global property to record the required version of this package
  1366. std::string versionInfoPropName = "_CMAKE_";
  1367. versionInfoPropName += this->Name;
  1368. versionInfoPropName += "_REQUIRED_VERSION";
  1369. std::string versionInfo;
  1370. if(!this->Version.empty())
  1371. {
  1372. versionInfo = this->VersionExact ? "==" : ">=";
  1373. versionInfo += " ";
  1374. versionInfo += this->Version;
  1375. }
  1376. this->Makefile->GetCMakeInstance()->SetProperty(versionInfoPropName.c_str(),
  1377. versionInfo.c_str());
  1378. if (this->Required)
  1379. {
  1380. std::string requiredInfoPropName = "_CMAKE_";
  1381. requiredInfoPropName += this->Name;
  1382. requiredInfoPropName += "_TYPE";
  1383. this->Makefile->GetCMakeInstance()->SetProperty(
  1384. requiredInfoPropName.c_str(), "REQUIRED");
  1385. }
  1386. // Restore original state of "_FIND_" variables we set.
  1387. this->RestoreFindDefinitions();
  1388. }
  1389. //----------------------------------------------------------------------------
  1390. void cmFindPackageCommand::ComputePrefixes()
  1391. {
  1392. this->AddPrefixesCMakeVariable();
  1393. this->AddPrefixesCMakeEnvironment();
  1394. this->AddPrefixesUserHints();
  1395. this->AddPrefixesSystemEnvironment();
  1396. this->AddPrefixesUserRegistry();
  1397. this->AddPrefixesBuilds();
  1398. this->AddPrefixesCMakeSystemVariable();
  1399. this->AddPrefixesSystemRegistry();
  1400. this->AddPrefixesUserGuess();
  1401. this->ComputeFinalPaths();
  1402. }
  1403. //----------------------------------------------------------------------------
  1404. void cmFindPackageCommand::AddPrefixesCMakeEnvironment()
  1405. {
  1406. if(!this->NoCMakeEnvironmentPath && !this->NoDefaultPath)
  1407. {
  1408. // Check the environment variable with the same name as the cache
  1409. // entry.
  1410. std::string env;
  1411. if(cmSystemTools::GetEnv(this->Variable.c_str(), env) && env.length() > 0)
  1412. {
  1413. cmSystemTools::ConvertToUnixSlashes(env);
  1414. this->AddPathInternal(env, EnvPath);
  1415. }
  1416. this->AddEnvPath("CMAKE_PREFIX_PATH");
  1417. this->AddEnvPath("CMAKE_FRAMEWORK_PATH");
  1418. this->AddEnvPath("CMAKE_APPBUNDLE_PATH");
  1419. }
  1420. }
  1421. //----------------------------------------------------------------------------
  1422. void cmFindPackageCommand::AddPrefixesCMakeVariable()
  1423. {
  1424. if(!this->NoCMakePath && !this->NoDefaultPath)
  1425. {
  1426. this->AddCMakePath("CMAKE_PREFIX_PATH");
  1427. this->AddCMakePath("CMAKE_FRAMEWORK_PATH");
  1428. this->AddCMakePath("CMAKE_APPBUNDLE_PATH");
  1429. }
  1430. }
  1431. //----------------------------------------------------------------------------
  1432. void cmFindPackageCommand::AddPrefixesSystemEnvironment()
  1433. {
  1434. if(!this->NoSystemEnvironmentPath && !this->NoDefaultPath)
  1435. {
  1436. // Use the system search path to generate prefixes.
  1437. // Relative paths are interpreted with respect to the current
  1438. // working directory.
  1439. std::vector<std::string> tmp;
  1440. cmSystemTools::GetPath(tmp);
  1441. for(std::vector<std::string>::iterator i = tmp.begin();
  1442. i != tmp.end(); ++i)
  1443. {
  1444. std::string const& d = *i;
  1445. // If the path is a PREFIX/bin case then add its parent instead.
  1446. if((d.size() >= 4 && strcmp(d.c_str()+d.size()-4, "/bin") == 0) ||
  1447. (d.size() >= 5 && strcmp(d.c_str()+d.size()-5, "/sbin") == 0))
  1448. {
  1449. this->AddPathInternal(cmSystemTools::GetFilenamePath(d), EnvPath);
  1450. }
  1451. else
  1452. {
  1453. this->AddPathInternal(d, EnvPath);
  1454. }
  1455. }
  1456. }
  1457. }
  1458. //----------------------------------------------------------------------------
  1459. void cmFindPackageCommand::AddPrefixesUserRegistry()
  1460. {
  1461. if(this->NoUserRegistry || this->NoDefaultPath)
  1462. {
  1463. return;
  1464. }
  1465. #if defined(_WIN32) && !defined(__CYGWIN__)
  1466. this->LoadPackageRegistryWinUser();
  1467. #elif defined(__HAIKU__)
  1468. BPath dir;
  1469. if (find_directory(B_USER_SETTINGS_DIRECTORY, &dir) == B_OK)
  1470. {
  1471. dir.Append("cmake/packages");
  1472. dir.Append(this->Name.c_str());
  1473. this->LoadPackageRegistryDir(dir.Path());
  1474. }
  1475. #else
  1476. if(const char* home = cmSystemTools::GetEnv("HOME"))
  1477. {
  1478. std::string dir = home;
  1479. dir += "/.cmake/packages/";
  1480. dir += this->Name;
  1481. this->LoadPackageRegistryDir(dir);
  1482. }
  1483. #endif
  1484. }
  1485. //----------------------------------------------------------------------------
  1486. void cmFindPackageCommand::AddPrefixesSystemRegistry()
  1487. {
  1488. if(this->NoSystemRegistry || this->NoDefaultPath)
  1489. {
  1490. return;
  1491. }
  1492. #if defined(_WIN32) && !defined(__CYGWIN__)
  1493. this->LoadPackageRegistryWinSystem();
  1494. #endif
  1495. }
  1496. #if defined(_WIN32) && !defined(__CYGWIN__)
  1497. # include <windows.h>
  1498. # undef GetCurrentDirectory
  1499. // http://msdn.microsoft.com/en-us/library/aa384253%28v=vs.85%29.aspx
  1500. # if !defined(KEY_WOW64_32KEY)
  1501. # define KEY_WOW64_32KEY 0x0200
  1502. # endif
  1503. # if !defined(KEY_WOW64_64KEY)
  1504. # define KEY_WOW64_64KEY 0x0100
  1505. # endif
  1506. //----------------------------------------------------------------------------
  1507. void cmFindPackageCommand::LoadPackageRegistryWinUser()
  1508. {
  1509. // HKEY_CURRENT_USER\\Software shares 32-bit and 64-bit views.
  1510. this->LoadPackageRegistryWin(true, 0);
  1511. }
  1512. //----------------------------------------------------------------------------
  1513. void cmFindPackageCommand::LoadPackageRegistryWinSystem()
  1514. {
  1515. // HKEY_LOCAL_MACHINE\\SOFTWARE has separate 32-bit and 64-bit views.
  1516. // Prefer the target platform view first.
  1517. if(this->Makefile->PlatformIs64Bit())
  1518. {
  1519. this->LoadPackageRegistryWin(false, KEY_WOW64_64KEY);
  1520. this->LoadPackageRegistryWin(false, KEY_WOW64_32KEY);
  1521. }
  1522. else
  1523. {
  1524. this->LoadPackageRegistryWin(false, KEY_WOW64_32KEY);
  1525. this->LoadPackageRegistryWin(false, KEY_WOW64_64KEY);
  1526. }
  1527. }
  1528. //----------------------------------------------------------------------------
  1529. void cmFindPackageCommand::LoadPackageRegistryWin(bool user,
  1530. unsigned int view)
  1531. {
  1532. std::string key = "Software\\Kitware\\CMake\\Packages\\";
  1533. key += this->Name;
  1534. std::set<cmStdString> bad;
  1535. HKEY hKey;
  1536. if(RegOpenKeyEx(user? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE, key.c_str(),
  1537. 0, KEY_QUERY_VALUE|view, &hKey) == ERROR_SUCCESS)
  1538. {
  1539. DWORD valueType = REG_NONE;
  1540. char name[16384];
  1541. std::vector<char> data(512);
  1542. bool done = false;
  1543. DWORD index = 0;
  1544. while(!done)
  1545. {
  1546. DWORD nameSize = static_cast<DWORD>(sizeof(name));
  1547. DWORD dataSize = static_cast<DWORD>(data.size()-1);
  1548. switch(RegEnumValue(hKey, index, name, &nameSize,
  1549. 0, &valueType, (BYTE*)&data[0], &dataSize))
  1550. {
  1551. case ERROR_SUCCESS:
  1552. ++index;
  1553. if(valueType == REG_SZ)
  1554. {
  1555. data[dataSize] = 0;
  1556. cmsys_ios::stringstream ss(&data[0]);
  1557. if(!this->CheckPackageRegistryEntry(ss))
  1558. {
  1559. // The entry is invalid.
  1560. bad.insert(name);
  1561. }
  1562. }
  1563. break;
  1564. case ERROR_MORE_DATA:
  1565. data.resize(dataSize+1);
  1566. break;
  1567. case ERROR_NO_MORE_ITEMS: default: done = true; break;
  1568. }
  1569. }
  1570. RegCloseKey(hKey);
  1571. }
  1572. // Remove bad values if possible.
  1573. if(user && !bad.empty() &&
  1574. RegOpenKeyEx(HKEY_CURRENT_USER, key.c_str(),
  1575. 0, KEY_SET_VALUE|view, &hKey) == ERROR_SUCCESS)
  1576. {
  1577. for(std::set<cmStdString>::const_iterator vi = bad.begin();
  1578. vi != bad.end(); ++vi)
  1579. {
  1580. RegDeleteValue(hKey, vi->c_str());
  1581. }
  1582. RegCloseKey(hKey);
  1583. }
  1584. }
  1585. #else
  1586. //----------------------------------------------------------------------------
  1587. class cmFindPackageCommandHoldFile
  1588. {
  1589. const char* File;
  1590. public:
  1591. cmFindPackageCommandHoldFile(const char* f): File(f) {}
  1592. ~cmFindPackageCommandHoldFile()
  1593. { if(this->File) { cmSystemTools::RemoveFile(this->File); } }
  1594. void Release() { this->File = 0; }
  1595. };
  1596. //----------------------------------------------------------------------------
  1597. void cmFindPackageCommand::LoadPackageRegistryDir(std::string const& dir)
  1598. {
  1599. cmsys::Directory files;
  1600. if(!files.Load(dir.c_str()))
  1601. {
  1602. return;
  1603. }
  1604. std::string fname;
  1605. for(unsigned long i=0; i < files.GetNumberOfFiles(); ++i)
  1606. {
  1607. fname = dir;
  1608. fname += "/";
  1609. fname += files.GetFile(i);
  1610. if(!cmSystemTools::FileIsDirectory(fname.c_str()))
  1611. {
  1612. // Hold this file hostage until it behaves.
  1613. cmFindPackageCommandHoldFile holdFile(fname.c_str());
  1614. // Load the file.
  1615. std::ifstream fin(fname.c_str(), std::ios::in | cmsys_ios_binary);
  1616. if(fin && this->CheckPackageRegistryEntry(fin))
  1617. {
  1618. // The file references an existing package, so release it.
  1619. holdFile.Release();
  1620. }
  1621. }
  1622. }
  1623. // TODO: Wipe out the directory if it is empty.
  1624. }
  1625. #endif
  1626. //----------------------------------------------------------------------------
  1627. bool cmFindPackageCommand::CheckPackageRegistryEntry(std::istream& is)
  1628. {
  1629. // Parse the content of one package registry entry.
  1630. std::string fname;
  1631. if(cmSystemTools::GetLineFromStream(is, fname) &&
  1632. cmSystemTools::FileIsFullPath(fname.c_str()))
  1633. {
  1634. // The first line in the stream is the full path to a file or
  1635. // directory containing the package.
  1636. if(cmSystemTools::FileExists(fname.c_str()))
  1637. {
  1638. // The path exists. Look for the package here.
  1639. if(!cmSystemTools::FileIsDirectory(fname.c_str()))
  1640. {
  1641. fname = cmSystemTools::GetFilenamePath(fname);
  1642. }
  1643. this->AddPathInternal(fname, FullPath);
  1644. return true;
  1645. }
  1646. else
  1647. {
  1648. // The path does not exist. Assume the stream content is
  1649. // associated with an old package that no longer exists, and
  1650. // delete it to keep the package registry clean.
  1651. return false;
  1652. }
  1653. }
  1654. else
  1655. {
  1656. // The first line in the stream is not the full path to a file or
  1657. // directory. Assume the stream content was created by a future
  1658. // version of CMake that uses a different format, and leave it.
  1659. return true;
  1660. }
  1661. }
  1662. //----------------------------------------------------------------------------
  1663. void cmFindPackageCommand::AddPrefixesBuilds()
  1664. {
  1665. if(!this->NoBuilds && !this->NoDefaultPath)
  1666. {
  1667. // It is likely that CMake will have recently built the project.
  1668. for(int i=0; i <= 10; ++i)
  1669. {
  1670. cmOStringStream r;
  1671. r <<
  1672. "[HKEY_CURRENT_USER\\Software\\Kitware\\CMakeSetup\\"
  1673. "Settings\\StartPath;WhereBuild" << i << "]";
  1674. std::string f = r.str();
  1675. cmSystemTools::ExpandRegistryValues(f);
  1676. cmSystemTools::ConvertToUnixSlashes(f);
  1677. if(cmSystemTools::FileIsFullPath(f.c_str()) &&
  1678. cmSystemTools::FileIsDirectory(f.c_str()))
  1679. {
  1680. this->AddPathInternal(f, FullPath);
  1681. }
  1682. }
  1683. }
  1684. }
  1685. //----------------------------------------------------------------------------
  1686. void cmFindPackageCommand::AddPrefixesCMakeSystemVariable()
  1687. {
  1688. if(!this->NoCMakeSystemPath && !this->NoDefaultPath)
  1689. {
  1690. this->AddCMakePath("CMAKE_SYSTEM_PREFIX_PATH");
  1691. this->AddCMakePath("CMAKE_SYSTEM_FRAMEWORK_PATH");
  1692. this->AddCMakePath("CMAKE_SYSTEM_APPBUNDLE_PATH");
  1693. }
  1694. }
  1695. //----------------------------------------------------------------------------
  1696. void cmFindPackageCommand::AddPrefixesUserGuess()
  1697. {
  1698. // Add guesses specified by the caller.
  1699. this->AddPathsInternal(this->UserPaths, CMakePath);
  1700. }
  1701. //----------------------------------------------------------------------------
  1702. void cmFindPackageCommand::AddPrefixesUserHints()
  1703. {
  1704. // Add hints specified by the caller.
  1705. this->AddPathsInternal(this->UserHints, CMakePath);
  1706. }
  1707. //----------------------------------------------------------------------------
  1708. bool cmFindPackageCommand::SearchDirectory(std::string const& dir)
  1709. {
  1710. assert(!dir.empty() && dir[dir.size()-1] == '/');
  1711. // Check each path suffix on this directory.
  1712. for(std::vector<std::string>::const_iterator
  1713. si = this->SearchPathSuffixes.begin();
  1714. si != this->SearchPathSuffixes.end(); ++si)
  1715. {
  1716. std::string d = dir;
  1717. if(!si->empty())
  1718. {
  1719. d += *si;
  1720. d += "/";
  1721. }
  1722. if(this->CheckDirectory(d))
  1723. {
  1724. return true;
  1725. }
  1726. }
  1727. return false;
  1728. }
  1729. //----------------------------------------------------------------------------
  1730. bool cmFindPackageCommand::CheckDirectory(std::string const& dir)
  1731. {
  1732. assert(!dir.empty() && dir[dir.size()-1] == '/');
  1733. // Look for the file in this directory.
  1734. std::string d = dir.substr(0, dir.size()-1);
  1735. if(this->FindConfigFile(d, this->FileFound))
  1736. {
  1737. // Remove duplicate slashes.
  1738. cmSystemTools::ConvertToUnixSlashes(this->FileFound);
  1739. return true;
  1740. }
  1741. return false;
  1742. }
  1743. //----------------------------------------------------------------------------
  1744. bool cmFindPackageCommand::FindConfigFile(std::string const& dir,
  1745. std::string& file)
  1746. {
  1747. if (this->IgnoredPaths.count(dir))
  1748. {
  1749. return false;
  1750. }
  1751. for(std::vector<std::string>::const_iterator ci = this->Configs.begin();
  1752. ci != this->Configs.end(); ++ci)
  1753. {
  1754. file = dir;
  1755. file += "/";
  1756. file += *ci;
  1757. if(this->DebugMode)
  1758. {
  1759. fprintf(stderr, "Checking file [%s]\n", file.c_str());
  1760. }
  1761. if(cmSystemTools::FileExists(file.c_str(), true) &&
  1762. this->CheckVersion(file))
  1763. {
  1764. return true;
  1765. }
  1766. }
  1767. return false;
  1768. }
  1769. //----------------------------------------------------------------------------
  1770. bool cmFindPackageCommand::CheckVersion(std::string const& config_file)
  1771. {
  1772. bool result = false; // by default, assume the version is not ok.
  1773. bool haveResult = false;
  1774. std::string version = "unknown";
  1775. // Get the filename without the .cmake extension.
  1776. std::string::size_type pos = config_file.rfind('.');
  1777. std::string version_file_base = config_file.substr(0, pos);
  1778. // Look for foo-config-version.cmake
  1779. std::string version_file = version_file_base;
  1780. version_file += "-version.cmake";
  1781. if ((haveResult == false)
  1782. && (cmSystemTools::FileExists(version_file.c_str(), true)))
  1783. {
  1784. result = this->CheckVersionFile(version_file, version);
  1785. haveResult = true;
  1786. }
  1787. // Look for fooConfigVersion.cmake
  1788. version_file = version_file_base;
  1789. version_file += "Version.cmake";
  1790. if ((haveResult == false)
  1791. && (cmSystemTools::FileExists(version_file.c_str(), true)))
  1792. {
  1793. result = this->CheckVersionFile(version_file, version);
  1794. haveResult = true;
  1795. }
  1796. // If no version was requested a versionless package is acceptable.
  1797. if ((haveResult == false) && (this->Version.empty()))
  1798. {
  1799. result = true;
  1800. haveResult = true;
  1801. }
  1802. ConfigFileInfo configFileInfo;
  1803. configFileInfo.filename = config_file;
  1804. configFileInfo.version = version;
  1805. this->ConsideredConfigs.push_back(configFileInfo);
  1806. return result;
  1807. }
  1808. //----------------------------------------------------------------------------
  1809. bool cmFindPackageCommand::CheckVersionFile(std::string const& version_file,
  1810. std::string& result_version)
  1811. {
  1812. // The version file will be loaded in an isolated scope.
  1813. cmMakefile::ScopePushPop varScope(this->Makefile);
  1814. cmMakefile::PolicyPushPop polScope(this->Makefile);
  1815. static_cast<void>(varScope);
  1816. static_cast<void>(polScope);
  1817. // Clear the output variables.
  1818. this->Makefile->RemoveDefinition("PACKAGE_VERSION");
  1819. this->Makefile->RemoveDefinition("PACKAGE_VERSION_UNSUITABLE");
  1820. this->Makefile->RemoveDefinition("PACKAGE_VERSION_COMPATIBLE");
  1821. this->Makefile->RemoveDefinition("PACKAGE_VERSION_EXACT");
  1822. // Set the input variables.
  1823. this->Makefile->AddDefinition("PACKAGE_FIND_NAME", this->Name.c_str());
  1824. this->Makefile->AddDefinition("PACKAGE_FIND_VERSION",
  1825. this->Version.c_str());
  1826. char buf[64];
  1827. sprintf(buf, "%u", this->VersionMajor);
  1828. this->Makefile->AddDefinition("PACKAGE_FIND_VERSION_MAJOR", buf);
  1829. sprintf(buf, "%u", this->VersionMinor);
  1830. this->Makefile->AddDefinition("PACKAGE_FIND_VERSION_MINOR", buf);
  1831. sprintf(buf, "%u", this->VersionPatch);
  1832. this->Makefile->AddDefinition("PACKAGE_FIND_VERSION_PATCH", buf);
  1833. sprintf(buf, "%u", this->VersionTweak);
  1834. this->Makefile->AddDefinition("PACKAGE_FIND_VERSION_TWEAK", buf);
  1835. sprintf(buf, "%u", this->VersionCount);
  1836. this->Makefile->AddDefinition("PACKAGE_FIND_VERSION_COUNT", buf);
  1837. // Load the version check file. Pass NoPolicyScope because we do
  1838. // our own policy push/pop independent of CMP0011.
  1839. bool suitable = false;
  1840. if(this->ReadListFile(version_file.c_str(), NoPolicyScope))
  1841. {
  1842. // Check the output variables.
  1843. bool okay = this->Makefile->IsOn("PACKAGE_VERSION_EXACT");
  1844. bool unsuitable = this->Makefile->IsOn("PACKAGE_VERSION_UNSUITABLE");
  1845. if(!okay && !this->VersionExact)
  1846. {
  1847. okay = this->Makefile->IsOn("PACKAGE_VERSION_COMPATIBLE");
  1848. }
  1849. // The package is suitable if the version is okay and not
  1850. // explicitly unsuitable.
  1851. suitable = !unsuitable && (okay || this->Version.empty());
  1852. if(suitable)
  1853. {
  1854. // Get the version found.
  1855. this->VersionFound =
  1856. this->Makefile->GetSafeDefinition("PACKAGE_VERSION");
  1857. // Try to parse the version number and store the results that were
  1858. // successfully parsed.
  1859. unsigned int parsed_major;
  1860. unsigned int parsed_minor;
  1861. unsigned int parsed_patch;
  1862. unsigned int parsed_tweak;
  1863. this->VersionFoundCount =
  1864. sscanf(this->VersionFound.c_str(), "%u.%u.%u.%u",
  1865. &parsed_major, &parsed_minor,
  1866. &parsed_patch, &parsed_tweak);
  1867. switch(this->VersionFoundCount)
  1868. {
  1869. case 4: this->VersionFoundTweak = parsed_tweak; // no break!
  1870. case 3: this->VersionFoundPatch = parsed_patch; // no break!
  1871. case 2: this->VersionFoundMinor = parsed_minor; // no break!
  1872. case 1: this->VersionFoundMajor = parsed_major; // no break!
  1873. default: break;
  1874. }
  1875. }
  1876. }
  1877. result_version = this->Makefile->GetSafeDefinition("PACKAGE_VERSION");
  1878. if (result_version.empty())
  1879. {
  1880. result_version = "unknown";
  1881. }
  1882. // Succeed if the version is suitable.
  1883. return suitable;
  1884. }
  1885. //----------------------------------------------------------------------------
  1886. void cmFindPackageCommand::StoreVersionFound()
  1887. {
  1888. // Store the whole version string.
  1889. std::string ver = this->Name;
  1890. ver += "_VERSION";
  1891. if(this->VersionFound.empty())
  1892. {
  1893. this->Makefile->RemoveDefinition(ver.c_str());
  1894. }
  1895. else
  1896. {
  1897. this->Makefile->AddDefinition(ver.c_str(), this->VersionFound.c_str());
  1898. }
  1899. // Store the version components.
  1900. char buf[64];
  1901. sprintf(buf, "%u", this->VersionFoundMajor);
  1902. this->Makefile->AddDefinition((ver+"_MAJOR").c_str(), buf);
  1903. sprintf(buf, "%u", this->VersionFoundMinor);
  1904. this->Makefile->AddDefinition((ver+"_MINOR").c_str(), buf);
  1905. sprintf(buf, "%u", this->VersionFoundPatch);
  1906. this->Makefile->AddDefinition((ver+"_PATCH").c_str(), buf);
  1907. sprintf(buf, "%u", this->VersionFoundTweak);
  1908. this->Makefile->AddDefinition((ver+"_TWEAK").c_str(), buf);
  1909. sprintf(buf, "%u", this->VersionFoundCount);
  1910. this->Makefile->AddDefinition((ver+"_COUNT").c_str(), buf);
  1911. }
  1912. //----------------------------------------------------------------------------
  1913. #include <cmsys/Glob.hxx>
  1914. #include <cmsys/String.h>
  1915. #include <cmsys/auto_ptr.hxx>
  1916. class cmFileList;
  1917. class cmFileListGeneratorBase
  1918. {
  1919. public:
  1920. virtual ~cmFileListGeneratorBase() {}
  1921. protected:
  1922. bool Consider(std::string const& fullPath, cmFileList& listing);
  1923. private:
  1924. bool Search(cmFileList&);
  1925. virtual bool Search(std::string const& parent, cmFileList&) = 0;
  1926. virtual cmsys::auto_ptr<cmFileListGeneratorBase> Clone() const = 0;
  1927. friend class cmFileList;
  1928. cmFileListGeneratorBase* SetNext(cmFileListGeneratorBase const& next);
  1929. cmsys::auto_ptr<cmFileListGeneratorBase> Next;
  1930. };
  1931. class cmFileList
  1932. {
  1933. public:
  1934. cmFileList(): First(), Last(0) {}
  1935. virtual ~cmFileList() {}
  1936. cmFileList& operator/(cmFileListGeneratorBase const& rhs)
  1937. {
  1938. if(this->Last)
  1939. {
  1940. this->Last = this->Last->SetNext(rhs);
  1941. }
  1942. else
  1943. {
  1944. this->First = rhs.Clone();
  1945. this->Last = this->First.get();
  1946. }
  1947. return *this;
  1948. }
  1949. bool Search()
  1950. {
  1951. if(this->First.get())
  1952. {
  1953. return this->First->Search(*this);
  1954. }
  1955. return false;
  1956. }
  1957. private:
  1958. virtual bool Visit(std::string const& fullPath) = 0;
  1959. friend class cmFileListGeneratorBase;
  1960. cmsys::auto_ptr<cmFileListGeneratorBase> First;
  1961. cmFileListGeneratorBase* Last;
  1962. };
  1963. class cmFindPackageFileList: public cmFileList
  1964. {
  1965. public:
  1966. cmFindPackageFileList(cmFindPackageCommand* fpc,
  1967. bool use_suffixes = true):
  1968. cmFileList(), FPC(fpc), UseSuffixes(use_suffixes) {}
  1969. private:
  1970. bool Visit(std::string const& fullPath)
  1971. {
  1972. if(this->UseSuffixes)
  1973. {
  1974. return this->FPC->SearchDirectory(fullPath);
  1975. }
  1976. else
  1977. {
  1978. return this->FPC->CheckDirectory(fullPath);
  1979. }
  1980. }
  1981. cmFindPackageCommand* FPC;
  1982. bool UseSuffixes;
  1983. };
  1984. bool cmFileListGeneratorBase::Search(cmFileList& listing)
  1985. {
  1986. return this->Search("", listing);
  1987. }
  1988. cmFileListGeneratorBase*
  1989. cmFileListGeneratorBase::SetNext(cmFileListGeneratorBase const& next)
  1990. {
  1991. this->Next = next.Clone();
  1992. return this->Next.get();
  1993. }
  1994. bool cmFileListGeneratorBase::Consider(std::string const& fullPath,
  1995. cmFileList& listing)
  1996. {
  1997. if(this->Next.get())
  1998. {
  1999. return this->Next->Search(fullPath + "/", listing);
  2000. }
  2001. else
  2002. {
  2003. return listing.Visit(fullPath + "/");
  2004. }
  2005. }
  2006. class cmFileListGeneratorFixed: public cmFileListGeneratorBase
  2007. {
  2008. public:
  2009. cmFileListGeneratorFixed(std::string const& str):
  2010. cmFileListGeneratorBase(), String(str) {}
  2011. cmFileListGeneratorFixed(cmFileListGeneratorFixed const& r):
  2012. cmFileListGeneratorBase(), String(r.String) {}
  2013. private:
  2014. std::string String;
  2015. virtual bool Search(std::string const& parent, cmFileList& lister)
  2016. {
  2017. std::string fullPath = parent + this->String;
  2018. return this->Consider(fullPath, lister);
  2019. }
  2020. virtual cmsys::auto_ptr<cmFileListGeneratorBase> Clone() const
  2021. {
  2022. cmsys::auto_ptr<cmFileListGeneratorBase>
  2023. g(new cmFileListGeneratorFixed(*this));
  2024. return g;
  2025. }
  2026. };
  2027. class cmFileListGeneratorEnumerate: public cmFileListGeneratorBase
  2028. {
  2029. public:
  2030. cmFileListGeneratorEnumerate(std::vector<std::string> const& v):
  2031. cmFileListGeneratorBase(), Vector(v) {}
  2032. cmFileListGeneratorEnumerate(cmFileListGeneratorEnumerate const& r):
  2033. cmFileListGeneratorBase(), Vector(r.Vector) {}
  2034. private:
  2035. std::vector<std::string> const& Vector;
  2036. virtual bool Search(std::string const& parent, cmFileList& lister)
  2037. {
  2038. for(std::vector<std::string>::const_iterator i = this->Vector.begin();
  2039. i != this->Vector.end(); ++i)
  2040. {
  2041. if(this->Consider(parent + *i, lister))
  2042. {
  2043. return true;
  2044. }
  2045. }
  2046. return false;
  2047. }
  2048. virtual cmsys::auto_ptr<cmFileListGeneratorBase> Clone() const
  2049. {
  2050. cmsys::auto_ptr<cmFileListGeneratorBase>
  2051. g(new cmFileListGeneratorEnumerate(*this));
  2052. return g;
  2053. }
  2054. };
  2055. class cmFileListGeneratorProject: public cmFileListGeneratorBase
  2056. {
  2057. public:
  2058. cmFileListGeneratorProject(std::vector<std::string> const& names):
  2059. cmFileListGeneratorBase(), Names(names) {}
  2060. cmFileListGeneratorProject(cmFileListGeneratorProject const& r):
  2061. cmFileListGeneratorBase(), Names(r.Names) {}
  2062. private:
  2063. std::vector<std::string> const& Names;
  2064. virtual bool Search(std::string const& parent, cmFileList& lister)
  2065. {
  2066. // Construct a list of matches.
  2067. std::vector<std::string> matches;
  2068. cmsys::Directory d;
  2069. d.Load(parent.c_str());
  2070. for(unsigned long i=0; i < d.GetNumberOfFiles(); ++i)
  2071. {
  2072. const char* fname = d.GetFile(i);
  2073. if(strcmp(fname, ".") == 0 ||
  2074. strcmp(fname, "..") == 0)
  2075. {
  2076. continue;
  2077. }
  2078. for(std::vector<std::string>::const_iterator ni = this->Names.begin();
  2079. ni != this->Names.end(); ++ni)
  2080. {
  2081. if(cmsysString_strncasecmp(fname, ni->c_str(),
  2082. ni->length()) == 0)
  2083. {
  2084. matches.push_back(fname);
  2085. }
  2086. }
  2087. }
  2088. for(std::vector<std::string>::const_iterator i = matches.begin();
  2089. i != matches.end(); ++i)
  2090. {
  2091. if(this->Consider(parent + *i, lister))
  2092. {
  2093. return true;
  2094. }
  2095. }
  2096. return false;
  2097. }
  2098. virtual cmsys::auto_ptr<cmFileListGeneratorBase> Clone() const
  2099. {
  2100. cmsys::auto_ptr<cmFileListGeneratorBase>
  2101. g(new cmFileListGeneratorProject(*this));
  2102. return g;
  2103. }
  2104. };
  2105. class cmFileListGeneratorMacProject: public cmFileListGeneratorBase
  2106. {
  2107. public:
  2108. cmFileListGeneratorMacProject(std::vector<std::string> const& names,
  2109. const char* ext):
  2110. cmFileListGeneratorBase(), Names(names), Extension(ext) {}
  2111. cmFileListGeneratorMacProject(cmFileListGeneratorMacProject const& r):
  2112. cmFileListGeneratorBase(), Names(r.Names), Extension(r.Extension) {}
  2113. private:
  2114. std::vector<std::string> const& Names;
  2115. std::string Extension;
  2116. virtual bool Search(std::string const& parent, cmFileList& lister)
  2117. {
  2118. // Construct a list of matches.
  2119. std::vector<std::string> matches;
  2120. cmsys::Directory d;
  2121. d.Load(parent.c_str());
  2122. for(unsigned long i=0; i < d.GetNumberOfFiles(); ++i)
  2123. {
  2124. const char* fname = d.GetFile(i);
  2125. if(strcmp(fname, ".") == 0 ||
  2126. strcmp(fname, "..") == 0)
  2127. {
  2128. continue;
  2129. }
  2130. for(std::vector<std::string>::const_iterator ni = this->Names.begin();
  2131. ni != this->Names.end(); ++ni)
  2132. {
  2133. std::string name = *ni;
  2134. name += this->Extension;
  2135. if(cmsysString_strcasecmp(fname, name.c_str()) == 0)
  2136. {
  2137. matches.push_back(fname);
  2138. }
  2139. }
  2140. }
  2141. for(std::vector<std::string>::const_iterator i = matches.begin();
  2142. i != matches.end(); ++i)
  2143. {
  2144. if(this->Consider(parent + *i, lister))
  2145. {
  2146. return true;
  2147. }
  2148. }
  2149. return false;
  2150. }
  2151. virtual cmsys::auto_ptr<cmFileListGeneratorBase> Clone() const
  2152. {
  2153. cmsys::auto_ptr<cmFileListGeneratorBase>
  2154. g(new cmFileListGeneratorMacProject(*this));
  2155. return g;
  2156. }
  2157. };
  2158. class cmFileListGeneratorCaseInsensitive: public cmFileListGeneratorBase
  2159. {
  2160. public:
  2161. cmFileListGeneratorCaseInsensitive(std::string const& str):
  2162. cmFileListGeneratorBase(), String(str) {}
  2163. cmFileListGeneratorCaseInsensitive(
  2164. cmFileListGeneratorCaseInsensitive const& r):
  2165. cmFileListGeneratorBase(), String(r.String) {}
  2166. private:
  2167. std::string String;
  2168. virtual bool Search(std::string const& parent, cmFileList& lister)
  2169. {
  2170. // Look for matching files.
  2171. std::vector<std::string> matches;
  2172. cmsys::Directory d;
  2173. d.Load(parent.c_str());
  2174. for(unsigned long i=0; i < d.GetNumberOfFiles(); ++i)
  2175. {
  2176. const char* fname = d.GetFile(i);
  2177. if(strcmp(fname, ".") == 0 ||
  2178. strcmp(fname, "..") == 0)
  2179. {
  2180. continue;
  2181. }
  2182. if(cmsysString_strcasecmp(fname, this->String.c_str()) == 0)
  2183. {
  2184. if(this->Consider(parent + fname, lister))
  2185. {
  2186. return true;
  2187. }
  2188. }
  2189. }
  2190. return false;
  2191. }
  2192. virtual cmsys::auto_ptr<cmFileListGeneratorBase> Clone() const
  2193. {
  2194. cmsys::auto_ptr<cmFileListGeneratorBase>
  2195. g(new cmFileListGeneratorCaseInsensitive(*this));
  2196. return g;
  2197. }
  2198. };
  2199. class cmFileListGeneratorGlob: public cmFileListGeneratorBase
  2200. {
  2201. public:
  2202. cmFileListGeneratorGlob(std::string const& str):
  2203. cmFileListGeneratorBase(), Pattern(str) {}
  2204. cmFileListGeneratorGlob(cmFileListGeneratorGlob const& r):
  2205. cmFileListGeneratorBase(), Pattern(r.Pattern) {}
  2206. private:
  2207. std::string Pattern;
  2208. virtual bool Search(std::string const& parent, cmFileList& lister)
  2209. {
  2210. // Glob the set of matching files.
  2211. std::string expr = parent;
  2212. expr += this->Pattern;
  2213. cmsys::Glob g;
  2214. if(!g.FindFiles(expr))
  2215. {
  2216. return false;
  2217. }
  2218. std::vector<std::string> const& files = g.GetFiles();
  2219. // Look for directories among the matches.
  2220. for(std::vector<std::string>::const_iterator fi = files.begin();
  2221. fi != files.end(); ++fi)
  2222. {
  2223. if(cmSystemTools::FileIsDirectory(fi->c_str()))
  2224. {
  2225. if(this->Consider(*fi, lister))
  2226. {
  2227. return true;
  2228. }
  2229. }
  2230. }
  2231. return false;
  2232. }
  2233. virtual cmsys::auto_ptr<cmFileListGeneratorBase> Clone() const
  2234. {
  2235. cmsys::auto_ptr<cmFileListGeneratorBase>
  2236. g(new cmFileListGeneratorGlob(*this));
  2237. return g;
  2238. }
  2239. };
  2240. //----------------------------------------------------------------------------
  2241. bool cmFindPackageCommand::SearchPrefix(std::string const& prefix_in)
  2242. {
  2243. assert(!prefix_in.empty() && prefix_in[prefix_in.size()-1] == '/');
  2244. if(this->DebugMode)
  2245. {
  2246. fprintf(stderr, "Checking prefix [%s]\n", prefix_in.c_str());
  2247. }
  2248. // Skip this if the prefix does not exist.
  2249. if(!cmSystemTools::FileIsDirectory(prefix_in.c_str()))
  2250. {
  2251. return false;
  2252. }
  2253. // PREFIX/ (useful on windows or in build trees)
  2254. if(this->SearchDirectory(prefix_in))
  2255. {
  2256. return true;
  2257. }
  2258. // Strip the trailing slash because the path generator is about to
  2259. // add one.
  2260. std::string prefix = prefix_in.substr(0, prefix_in.size()-1);
  2261. // PREFIX/(cmake|CMake)/ (useful on windows or in build trees)
  2262. {
  2263. cmFindPackageFileList lister(this);
  2264. lister
  2265. / cmFileListGeneratorFixed(prefix)
  2266. / cmFileListGeneratorCaseInsensitive("cmake");
  2267. if(lister.Search())
  2268. {
  2269. return true;
  2270. }
  2271. }
  2272. // PREFIX/(Foo|foo|FOO).*/
  2273. {
  2274. cmFindPackageFileList lister(this);
  2275. lister
  2276. / cmFileListGeneratorFixed(prefix)
  2277. / cmFileListGeneratorProject(this->Names);
  2278. if(lister.Search())
  2279. {
  2280. return true;
  2281. }
  2282. }
  2283. // PREFIX/(Foo|foo|FOO).*/(cmake|CMake)/
  2284. {
  2285. cmFindPackageFileList lister(this);
  2286. lister
  2287. / cmFileListGeneratorFixed(prefix)
  2288. / cmFileListGeneratorProject(this->Names)
  2289. / cmFileListGeneratorCaseInsensitive("cmake");
  2290. if(lister.Search())
  2291. {
  2292. return true;
  2293. }
  2294. }
  2295. // Construct list of common install locations (lib and share).
  2296. std::vector<std::string> common;
  2297. if(!this->LibraryArchitecture.empty())
  2298. {
  2299. common.push_back("lib/"+this->LibraryArchitecture);
  2300. }
  2301. if(this->UseLib64Paths)
  2302. {
  2303. common.push_back("lib64");
  2304. }
  2305. common.push_back("lib");
  2306. common.push_back("share");
  2307. // PREFIX/(lib/ARCH|lib|share)/cmake/(Foo|foo|FOO).*/
  2308. {
  2309. cmFindPackageFileList lister(this);
  2310. lister
  2311. / cmFileListGeneratorFixed(prefix)
  2312. / cmFileListGeneratorEnumerate(common)
  2313. / cmFileListGeneratorFixed("cmake")
  2314. / cmFileListGeneratorProject(this->Names);
  2315. if(lister.Search())
  2316. {
  2317. return true;
  2318. }
  2319. }
  2320. // PREFIX/(lib/ARCH|lib|share)/(Foo|foo|FOO).*/
  2321. {
  2322. cmFindPackageFileList lister(this);
  2323. lister
  2324. / cmFileListGeneratorFixed(prefix)
  2325. / cmFileListGeneratorEnumerate(common)
  2326. / cmFileListGeneratorProject(this->Names);
  2327. if(lister.Search())
  2328. {
  2329. return true;
  2330. }
  2331. }
  2332. // PREFIX/(lib/ARCH|lib|share)/(Foo|foo|FOO).*/(cmake|CMake)/
  2333. {
  2334. cmFindPackageFileList lister(this);
  2335. lister
  2336. / cmFileListGeneratorFixed(prefix)
  2337. / cmFileListGeneratorEnumerate(common)
  2338. / cmFileListGeneratorProject(this->Names)
  2339. / cmFileListGeneratorCaseInsensitive("cmake");
  2340. if(lister.Search())
  2341. {
  2342. return true;
  2343. }
  2344. }
  2345. return false;
  2346. }
  2347. //----------------------------------------------------------------------------
  2348. bool cmFindPackageCommand::SearchFrameworkPrefix(std::string const& prefix_in)
  2349. {
  2350. assert(!prefix_in.empty() && prefix_in[prefix_in.size()-1] == '/');
  2351. if(this->DebugMode)
  2352. {
  2353. fprintf(stderr, "Checking framework prefix [%s]\n", prefix_in.c_str());
  2354. }
  2355. // Strip the trailing slash because the path generator is about to
  2356. // add one.
  2357. std::string prefix = prefix_in.substr(0, prefix_in.size()-1);
  2358. // <prefix>/Foo.framework/Resources/
  2359. {
  2360. cmFindPackageFileList lister(this);
  2361. lister
  2362. / cmFileListGeneratorFixed(prefix)
  2363. / cmFileListGeneratorMacProject(this->Names, ".framework")
  2364. / cmFileListGeneratorFixed("Resources");
  2365. if(lister.Search())
  2366. {
  2367. return true;
  2368. }
  2369. }
  2370. // <prefix>/Foo.framework/Resources/CMake/
  2371. {
  2372. cmFindPackageFileList lister(this);
  2373. lister
  2374. / cmFileListGeneratorFixed(prefix)
  2375. / cmFileListGeneratorMacProject(this->Names, ".framework")
  2376. / cmFileListGeneratorFixed("Resources")
  2377. / cmFileListGeneratorCaseInsensitive("cmake");
  2378. if(lister.Search())
  2379. {
  2380. return true;
  2381. }
  2382. }
  2383. // <prefix>/Foo.framework/Versions/*/Resources/
  2384. {
  2385. cmFindPackageFileList lister(this);
  2386. lister
  2387. / cmFileListGeneratorFixed(prefix)
  2388. / cmFileListGeneratorMacProject(this->Names, ".framework")
  2389. / cmFileListGeneratorFixed("Versions")
  2390. / cmFileListGeneratorGlob("*/Resources");
  2391. if(lister.Search())
  2392. {
  2393. return true;
  2394. }
  2395. }
  2396. // <prefix>/Foo.framework/Versions/*/Resources/CMake/
  2397. {
  2398. cmFindPackageFileList lister(this);
  2399. lister
  2400. / cmFileListGeneratorFixed(prefix)
  2401. / cmFileListGeneratorMacProject(this->Names, ".framework")
  2402. / cmFileListGeneratorFixed("Versions")
  2403. / cmFileListGeneratorGlob("*/Resources")
  2404. / cmFileListGeneratorCaseInsensitive("cmake");
  2405. if(lister.Search())
  2406. {
  2407. return true;
  2408. }
  2409. }
  2410. return false;
  2411. }
  2412. //----------------------------------------------------------------------------
  2413. bool cmFindPackageCommand::SearchAppBundlePrefix(std::string const& prefix_in)
  2414. {
  2415. assert(!prefix_in.empty() && prefix_in[prefix_in.size()-1] == '/');
  2416. if(this->DebugMode)
  2417. {
  2418. fprintf(stderr, "Checking bundle prefix [%s]\n", prefix_in.c_str());
  2419. }
  2420. // Strip the trailing slash because the path generator is about to
  2421. // add one.
  2422. std::string prefix = prefix_in.substr(0, prefix_in.size()-1);
  2423. // <prefix>/Foo.app/Contents/Resources
  2424. {
  2425. cmFindPackageFileList lister(this);
  2426. lister
  2427. / cmFileListGeneratorFixed(prefix)
  2428. / cmFileListGeneratorMacProject(this->Names, ".app")
  2429. / cmFileListGeneratorFixed("Contents/Resources");
  2430. if(lister.Search())
  2431. {
  2432. return true;
  2433. }
  2434. }
  2435. // <prefix>/Foo.app/Contents/Resources/CMake
  2436. {
  2437. cmFindPackageFileList lister(this);
  2438. lister
  2439. / cmFileListGeneratorFixed(prefix)
  2440. / cmFileListGeneratorMacProject(this->Names, ".app")
  2441. / cmFileListGeneratorFixed("Contents/Resources")
  2442. / cmFileListGeneratorCaseInsensitive("cmake");
  2443. if(lister.Search())
  2444. {
  2445. return true;
  2446. }
  2447. }
  2448. return false;
  2449. }
  2450. // TODO: Debug cmsys::Glob double slash problem.