cmFindPackageCommand.cxx 72 KB

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