cmFindPackageCommand.cxx 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmFindPackageCommand.h"
  14. #include <cmsys/RegularExpression.hxx>
  15. #ifdef CMAKE_BUILD_WITH_CMAKE
  16. #include "cmVariableWatch.h"
  17. #endif
  18. void cmFindPackageNeedBackwardsCompatibility(const std::string& variable,
  19. int access_type, void*, const char* newValue,
  20. const cmMakefile*)
  21. {
  22. (void)newValue;
  23. #ifdef CMAKE_BUILD_WITH_CMAKE
  24. if(access_type == cmVariableWatch::UNKNOWN_VARIABLE_READ_ACCESS)
  25. {
  26. std::string message = "An attempt was made to access a variable: ";
  27. message += variable;
  28. message +=
  29. " that has not been defined. This variable is created by the "
  30. "FIND_PACKAGE command. CMake version 1.6 always converted the "
  31. "variable name to upper-case, but this behavior is no longer the "
  32. "case. To fix this you might need to set the cache value of "
  33. "CMAKE_BACKWARDS_COMPATIBILITY to 1.6 or less. If you are writing a "
  34. "CMake listfile, you should change the variable reference to use "
  35. "the case of the argument to FIND_PACKAGE.";
  36. cmSystemTools::Error(message.c_str());
  37. }
  38. #else
  39. (void)variable;
  40. (void)access_type;
  41. #endif
  42. }
  43. //----------------------------------------------------------------------------
  44. cmFindPackageCommand::cmFindPackageCommand()
  45. {
  46. cmSystemTools::ReplaceString(this->GenericDocumentationRootPath,
  47. "CMAKE_FIND_ROOT_PATH_MODE_XXX",
  48. "CMAKE_FIND_ROOT_PATH_MODE_PACKAGE");
  49. cmSystemTools::ReplaceString(this->GenericDocumentationPathsOrder,
  50. "FIND_ARGS_XXX", "<package>");
  51. cmSystemTools::ReplaceString(this->GenericDocumentationPathsOrder,
  52. "FIND_XXX", "find_package");
  53. this->CMakePathName = "PACKAGE";
  54. this->Quiet = false;
  55. this->Required = false;
  56. this->NoBuilds = false;
  57. this->NoModule = false;
  58. this->DebugMode = false;
  59. this->VersionMajor = 0;
  60. this->VersionMinor = 0;
  61. this->VersionPatch = 0;
  62. this->VersionCount = 0;
  63. this->CommandDocumentation =
  64. " find_package(<package> [major[.minor[.patch]]] [QUIET]\n"
  65. " [[REQUIRED|COMPONENTS] [components...]])\n"
  66. "Finds and loads settings from an external project. "
  67. "<package>_FOUND will be set to indicate whether the package was found. "
  68. "When the package is found package-specific information is provided "
  69. "through variables documented by the package itself. "
  70. "The QUIET option disables messages if the package cannot be found. "
  71. "The REQUIRED option stops processing with an error message if the "
  72. "package cannot be found. "
  73. "A package-specific list of components may be listed after the "
  74. "REQUIRED option or after the COMPONENTS option if no REQUIRED "
  75. "option is given. "
  76. "The \"[major[.minor[.patch]]]\" version argument specifies a desired "
  77. "version with which the package found should be compatible. "
  78. "Version support is currently provided only on a package-by-package "
  79. "basis and is not enforced by the command.\n"
  80. "User code should generally look for packages using the above simple "
  81. "signature. The remainder of this command documentation specifies the "
  82. "full command signature and details of the search process. Project "
  83. "maintainers wishing to provide a package to be found by this command "
  84. "are encouraged to read on.\n"
  85. "The command has two modes by which it searches for packages: "
  86. "\"Module\" mode and \"Config\" mode. "
  87. "Module mode is available when the command is invoked with the above "
  88. "reduced signature. "
  89. "CMake searches for a file called \"Find<package>.cmake\" in "
  90. "the CMAKE_MODULE_PATH followed by the CMake installation. "
  91. "If the file is found, it is read and processed by CMake. "
  92. "It is responsible for finding the package or producing an error message "
  93. "if package content cannot be found. "
  94. "Otherwise the command proceeds to Config mode.\n"
  95. "The complete Config mode command signature is:\n"
  96. " find_package(<package> [major[.minor[.patch]]] [QUIET] [NO_MODULE]\n"
  97. " [[REQUIRED|COMPONENTS] [components...]]\n"
  98. " [NAMES name1 [name2 ...]]\n"
  99. " [CONFIGS config1 [config2 ...]]\n"
  100. " [PATHS path1 [path2 ... ]]\n"
  101. " [PATH_SUFFIXES suffix1 [suffix2 ...]]\n"
  102. " [NO_DEFAULT_PATH]\n"
  103. " [NO_CMAKE_ENVIRONMENT_PATH]\n"
  104. " [NO_CMAKE_PATH]\n"
  105. " [NO_SYSTEM_ENVIRONMENT_PATH]\n"
  106. " [NO_CMAKE_BUILDS_PATH]\n"
  107. " [NO_CMAKE_SYSTEM_PATH]\n"
  108. " [CMAKE_FIND_ROOT_PATH_BOTH |\n"
  109. " ONLY_CMAKE_FIND_ROOT_PATH |\n"
  110. " NO_CMAKE_FIND_ROOT_PATH])\n"
  111. "The NO_MODULE option may be used to skip Module mode explicitly. "
  112. "It is also implied by use of options not specified in the reduced "
  113. "signature. "
  114. "\n"
  115. "Config mode attempts to locate a configuration file provided by the "
  116. "package to be found. A cache entry called <package>_DIR is created to "
  117. "hold the directory containing the file. "
  118. "Currently versioning is not implemented by Config mode. "
  119. "By default the command searches for a package with the name <package>. "
  120. "If the NAMES option is given the names following it are used instead "
  121. "of <package>. "
  122. "The command searches for a file called \"<name>Config.cmake\" or "
  123. "\"<lower-case-name>-config.cmake\" for each name specified. "
  124. "A replacement set of possible configuration file names may be given "
  125. "using the CONFIGS option. "
  126. "The search procedure is specified below. Once found, the configuration "
  127. "file is read and processed by CMake. Since the file is provided by the "
  128. "package it already knows the location of package contents. "
  129. "The full path to the configuration file is stored in the cmake "
  130. "variable <package>_CONFIG."
  131. "\n"
  132. "If the package configuration file cannot be found CMake "
  133. "will generate an error describing the problem unless the QUIET "
  134. "argument is specified. If REQUIRED is specified and the package "
  135. "is not found a fatal error is generated and the configure step stops "
  136. "executing. If <package>_DIR has been set to a directory not containing "
  137. "a configuration file a fatal error is always generated because user "
  138. "intervention is required."
  139. "\n"
  140. "Config mode provides an elaborate interface and search procedure. "
  141. "Much of the interface is provided for completeness and for use "
  142. "internally by find-modules loaded by Module mode. "
  143. "Most user code should simply call\n"
  144. " find_package(<package> [REQUIRED|QUIET])\n"
  145. "in order to find a package. Package maintainers providing CMake "
  146. "package configuration files are encouraged to name and install "
  147. "them such that the procedure outlined below will find them "
  148. "without requiring use of additional options."
  149. "\n"
  150. "CMake constructs a set of possible installation prefixes for the "
  151. "package. Under each prefix several directories are searched for a "
  152. "configuration file. The tables below show the directories searched. "
  153. "Each entry is meant for installation trees following Windows (W), "
  154. "UNIX (U), or Apple (A) conventions.\n"
  155. " <prefix>/ (W)\n"
  156. " <prefix>/(cmake|CMake)/ (W)\n"
  157. " <prefix>/(share|lib)/<name>*/ (U)\n"
  158. " <prefix>/(share|lib)/<name>*/(cmake|CMake)/ (U)\n"
  159. "On systems supporting OS X Frameworks and Application Bundles "
  160. "the following directories are searched for frameworks or bundles "
  161. "containing a configuration file:\n"
  162. " <prefix>/<name>.framework/Resources/ (A)\n"
  163. " <prefix>/<name>.framework/Resources/CMake/ (A)\n"
  164. " <prefix>/<name>.framework/Versions/*/Resources/ (A)\n"
  165. " <prefix>/<name>.framework/Versions/*/Resources/CMake/ (A)\n"
  166. " <prefix>/<name>.app/Contents/Resources/ (A)\n"
  167. " <prefix>/<name>.app/Contents/Resources/CMake/ (A)\n"
  168. "In all cases the <name> is treated as case-insensitive and corresponds "
  169. "to any of the names specified (<package> or names given by NAMES). "
  170. "If PATH_SUFFIXES is specified the suffixes are appended to each "
  171. "(W) or (U) directory entry one-by-one.\n"
  172. "This set of directories is intended to work in cooperation with "
  173. "projects that provide configuration files in their installation trees. "
  174. "Directories above marked with (W) are intended for installations on "
  175. "Windows where the prefix may point at the top of an application's "
  176. "installation directory. Those marked with (U) are intended for "
  177. "installations on UNIX platforms where the prefix is shared by "
  178. "multiple packages. This is merely a convention, so all (W) and (U) "
  179. "directories are still searched on all platforms. "
  180. "Directories marked with (A) are intended for installations on "
  181. "Apple platforms. The cmake variables CMAKE_FIND_FRAMEWORK and "
  182. "CMAKE_FIND_APPBUNDLE determine the order of preference "
  183. "as specified below.\n"
  184. "The set of installation prefixes is constructed using the following "
  185. "steps. If NO_DEFAULT_PATH is specified steps 1-5 are skipped.\n"
  186. "1. Search cmake specific environment variables. This "
  187. "can be skipped if NO_CMAKE_ENVIRONMENT_PATH is passed.\n"
  188. " CMAKE_PREFIX_PATH\n"
  189. " CMAKE_FRAMEWORK_PATH\n"
  190. " CMAKE_APPBUNDLE_PATH\n"
  191. "2. Search cmake variables with the same names as the cmake specific "
  192. "environment variables. These are intended to be used on the command "
  193. "line with a -DVAR=value. This can be skipped if NO_CMAKE_PATH "
  194. "is passed.\n"
  195. " CMAKE_PREFIX_PATH\n"
  196. " CMAKE_FRAMEWORK_PATH\n"
  197. " CMAKE_APPBUNDLE_PATH\n"
  198. "3. Search the standard system environment variables. "
  199. "This can be skipped if NO_SYSTEM_ENVIRONMENT_PATH is passed. "
  200. "Path entries ending in \"/bin\" or \"/sbin\" are automatically "
  201. "converted to their parent directories.\n"
  202. " PATH\n"
  203. "4. Search project build trees recently configured in a CMake GUI. "
  204. "This can be skipped if NO_CMAKE_BUILDS_PATH is passed. "
  205. "It is intended for the case when a user is building multiple "
  206. "dependent projects one after another.\n"
  207. "5. Search cmake variables defined in the Platform files "
  208. "for the current system. This can be skipped if NO_CMAKE_SYSTEM_PATH "
  209. "is passed.\n"
  210. " CMAKE_SYSTEM_PREFIX_PATH\n"
  211. " CMAKE_SYSTEM_FRAMEWORK_PATH\n"
  212. " CMAKE_SYSTEM_APPBUNDLE_PATH\n"
  213. "6. Search paths specified by the PATHS option.\n"
  214. ;
  215. this->CommandDocumentation += this->GenericDocumentationMacPolicy;
  216. this->CommandDocumentation += this->GenericDocumentationRootPath;
  217. this->CommandDocumentation += this->GenericDocumentationPathsOrder;
  218. }
  219. //----------------------------------------------------------------------------
  220. const char* cmFindPackageCommand::GetFullDocumentation()
  221. {
  222. return this->CommandDocumentation.c_str();
  223. }
  224. //----------------------------------------------------------------------------
  225. bool cmFindPackageCommand
  226. ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
  227. {
  228. if(args.size() < 1)
  229. {
  230. this->SetError("called with incorrect number of arguments");
  231. return false;
  232. }
  233. // Check for debug mode.
  234. this->DebugMode = this->Makefile->IsOn("CMAKE_FIND_DEBUG_MODE");
  235. // Find the current root path mode.
  236. this->SelectDefaultRootPathMode();
  237. // Find the current bundle/framework search policy.
  238. this->SelectDefaultMacMode();
  239. // Record options.
  240. this->Name = args[0];
  241. std::string components;
  242. const char* components_sep = "";
  243. // Check ancient compatibility.
  244. this->Compatibility_1_6 =
  245. this->Makefile->GetLocalGenerator()
  246. ->NeedBackwardsCompatibility(1, 6);
  247. // Always search directly in a generated path.
  248. this->SearchPathSuffixes.push_back("");
  249. // Parse the arguments.
  250. enum Doing { DoingNone, DoingComponents, DoingNames, DoingPaths,
  251. DoingPathSuffixes, DoingConfigs };
  252. Doing doing = DoingNone;
  253. cmsys::RegularExpression version("^[0-9.]+$");
  254. bool haveVersion = false;
  255. for(unsigned int i=1; i < args.size(); ++i)
  256. {
  257. if(args[i] == "QUIET")
  258. {
  259. this->Quiet = true;
  260. doing = DoingNone;
  261. }
  262. else if(args[i] == "NO_MODULE")
  263. {
  264. this->NoModule = true;
  265. doing = DoingNone;
  266. }
  267. else if(args[i] == "REQUIRED")
  268. {
  269. this->Required = true;
  270. doing = DoingComponents;
  271. }
  272. else if(args[i] == "COMPONENTS")
  273. {
  274. this->Compatibility_1_6 = false;
  275. doing = DoingComponents;
  276. }
  277. else if(args[i] == "NAMES")
  278. {
  279. this->NoModule = true;
  280. this->Compatibility_1_6 = false;
  281. doing = DoingNames;
  282. }
  283. else if(args[i] == "PATHS")
  284. {
  285. this->NoModule = true;
  286. this->Compatibility_1_6 = false;
  287. doing = DoingPaths;
  288. }
  289. else if(args[i] == "PATH_SUFFIXES")
  290. {
  291. this->NoModule = true;
  292. this->Compatibility_1_6 = false;
  293. doing = DoingPathSuffixes;
  294. }
  295. else if(args[i] == "CONFIGS")
  296. {
  297. this->NoModule = true;
  298. this->Compatibility_1_6 = false;
  299. doing = DoingConfigs;
  300. }
  301. else if(args[i] == "NO_CMAKE_BUILDS_PATH")
  302. {
  303. this->NoBuilds = true;
  304. this->NoModule = true;
  305. this->Compatibility_1_6 = false;
  306. doing = DoingNone;
  307. }
  308. else if(this->CheckCommonArgument(args[i]))
  309. {
  310. this->NoModule = true;
  311. this->Compatibility_1_6 = false;
  312. doing = DoingNone;
  313. }
  314. else if(doing == DoingComponents)
  315. {
  316. // Set a variable telling the find script this component
  317. // is required.
  318. std::string req_var = Name + "_FIND_REQUIRED_" + args[i];
  319. this->Makefile->AddDefinition(req_var.c_str(), "1");
  320. // Append to the list of required components.
  321. components += components_sep;
  322. components += args[i];
  323. components_sep = ";";
  324. }
  325. else if(doing == DoingNames)
  326. {
  327. this->Names.push_back(args[i]);
  328. }
  329. else if(doing == DoingPaths)
  330. {
  331. this->AddUserPath(args[i]);
  332. }
  333. else if(doing == DoingPathSuffixes)
  334. {
  335. this->AddPathSuffix(args[i]);
  336. }
  337. else if(doing == DoingConfigs)
  338. {
  339. this->Configs.push_back(args[i]);
  340. }
  341. else if(!haveVersion && version.find(args[i].c_str()))
  342. {
  343. haveVersion = true;
  344. this->Version = args[i];
  345. }
  346. else
  347. {
  348. cmOStringStream e;
  349. e << "called with invalid argument \"" << args[i].c_str() << "\"";
  350. this->SetError(e.str().c_str());
  351. return false;
  352. }
  353. }
  354. if(!this->Version.empty())
  355. {
  356. // Try to parse the version number and store the results that were
  357. // successfully parsed.
  358. unsigned int parsed_major;
  359. unsigned int parsed_minor;
  360. unsigned int parsed_patch;
  361. this->VersionCount = sscanf(this->Version.c_str(), "%u.%u.%u",
  362. &parsed_major, &parsed_minor, &parsed_patch);
  363. switch(this->VersionCount)
  364. {
  365. case 3: this->VersionPatch = parsed_patch; // no break!
  366. case 2: this->VersionMinor = parsed_minor; // no break!
  367. case 1: this->VersionMajor = parsed_major; // no break!
  368. default: break;
  369. }
  370. }
  371. // Store the list of components.
  372. std::string components_var = Name + "_FIND_COMPONENTS";
  373. this->Makefile->AddDefinition(components_var.c_str(), components.c_str());
  374. // See if there is a Find<package>.cmake module.
  375. if(!this->NoModule)
  376. {
  377. bool foundModule = false;
  378. if(!this->FindModule(foundModule))
  379. {
  380. this->AppendSuccessInformation();
  381. return false;
  382. }
  383. if(foundModule)
  384. {
  385. this->AppendSuccessInformation();
  386. return true;
  387. }
  388. }
  389. // No find module. Assume the project has a CMake config file. Use
  390. // a <package>_DIR cache variable to locate it.
  391. this->Variable = this->Name;
  392. this->Variable += "_DIR";
  393. // Add the default name.
  394. if(this->Names.empty())
  395. {
  396. this->Names.push_back(this->Name);
  397. }
  398. // Add the default configs.
  399. if(this->Configs.empty())
  400. {
  401. for(std::vector<std::string>::const_iterator ni = this->Names.begin();
  402. ni != this->Names.end(); ++ni)
  403. {
  404. std::string config = *ni;
  405. config += "Config.cmake";
  406. this->Configs.push_back(config);
  407. config = cmSystemTools::LowerCase(*ni);
  408. config += "-config.cmake";
  409. this->Configs.push_back(config);
  410. }
  411. }
  412. // Find and load the package.
  413. bool result = this->HandlePackageMode();
  414. this->AppendSuccessInformation();
  415. return result;
  416. }
  417. //----------------------------------------------------------------------------
  418. bool cmFindPackageCommand::FindModule(bool& found)
  419. {
  420. std::string module = "Find";
  421. module += this->Name;
  422. module += ".cmake";
  423. std::string mfile = this->Makefile->GetModulesFile(module.c_str());
  424. if ( mfile.size() )
  425. {
  426. if(this->Quiet)
  427. {
  428. // Tell the module that is about to be read that it should find
  429. // quietly.
  430. std::string quietly = this->Name;
  431. quietly += "_FIND_QUIETLY";
  432. this->Makefile->AddDefinition(quietly.c_str(), "1");
  433. }
  434. if(this->Required)
  435. {
  436. // Tell the module that is about to be read that it should report
  437. // a fatal error if the package is not found.
  438. std::string req = this->Name;
  439. req += "_FIND_REQUIRED";
  440. this->Makefile->AddDefinition(req.c_str(), "1");
  441. }
  442. if(!this->Version.empty())
  443. {
  444. // Tell the module that is about to be read what version of the
  445. // package has been requested.
  446. std::string ver = this->Name;
  447. ver += "_FIND_VERSION";
  448. this->Makefile->AddDefinition(ver.c_str(), this->Version.c_str());
  449. char buf[64];
  450. switch(this->VersionCount)
  451. {
  452. case 3:
  453. {
  454. sprintf(buf, "%u", this->VersionPatch);
  455. this->Makefile->AddDefinition((ver+"_PATCH").c_str(), buf);
  456. } // no break
  457. case 2:
  458. {
  459. sprintf(buf, "%u", this->VersionMinor);
  460. this->Makefile->AddDefinition((ver+"_MINOR").c_str(), buf);
  461. } // no break
  462. case 1:
  463. {
  464. sprintf(buf, "%u", this->VersionMajor);
  465. this->Makefile->AddDefinition((ver+"_MAJOR").c_str(), buf);
  466. } // no break
  467. default: break;
  468. }
  469. }
  470. // Load the module we found.
  471. found = true;
  472. return this->ReadListFile(mfile.c_str());
  473. }
  474. return true;
  475. }
  476. //----------------------------------------------------------------------------
  477. bool cmFindPackageCommand::HandlePackageMode()
  478. {
  479. // Support old capitalization behavior.
  480. std::string upperDir = cmSystemTools::UpperCase(this->Name);
  481. std::string upperFound = cmSystemTools::UpperCase(this->Name);
  482. upperDir += "_DIR";
  483. upperFound += "_FOUND";
  484. if(upperDir == this->Variable)
  485. {
  486. this->Compatibility_1_6 = false;
  487. }
  488. // Try to find the config file.
  489. const char* def = this->Makefile->GetDefinition(this->Variable.c_str());
  490. if(this->Compatibility_1_6 && cmSystemTools::IsOff(def))
  491. {
  492. // Use the setting of the old name of the variable to provide the
  493. // value of the new.
  494. const char* oldDef = this->Makefile->GetDefinition(upperDir.c_str());
  495. if(!cmSystemTools::IsOff(oldDef))
  496. {
  497. this->Makefile->AddDefinition(this->Variable.c_str(), oldDef);
  498. def = this->Makefile->GetDefinition(this->Variable.c_str());
  499. }
  500. }
  501. // Search for the config file if it is not already found.
  502. if(cmSystemTools::IsOff(def))
  503. {
  504. this->FindConfig();
  505. def = this->Makefile->GetDefinition(this->Variable.c_str());
  506. }
  507. // If the config file was found, load it.
  508. std::string file;
  509. bool result = true;
  510. bool found = false;
  511. if(!cmSystemTools::IsOff(def))
  512. {
  513. // Get the directory from the variable value.
  514. std::string dir = def;
  515. cmSystemTools::ConvertToUnixSlashes(dir);
  516. // Treat relative paths with respect to the current source dir.
  517. if(!cmSystemTools::FileIsFullPath(dir.c_str()))
  518. {
  519. dir = "/" + dir;
  520. dir = this->Makefile->GetCurrentDirectory() + dir;
  521. }
  522. // Find the configuration file.
  523. if(this->FindConfigFile(dir, file))
  524. {
  525. // Parse the configuration file.
  526. if(this->ReadListFile(file.c_str()))
  527. {
  528. // The package has been found.
  529. found = true;
  530. }
  531. else
  532. {
  533. // The configuration file is invalid.
  534. result = false;
  535. }
  536. }
  537. else
  538. {
  539. // The variable setting is wrong.
  540. cmOStringStream e;
  541. e << "cannot find package " << this->Name << " because "
  542. << this->Variable << " is set to \"" << def << "\" "
  543. << "which is not a directory containing a package configuration "
  544. << "file. "
  545. << "Please set the cache entry " << this->Variable << " "
  546. << "to the correct directory, or delete it to ask CMake "
  547. << "to search.";
  548. this->SetError(e.str().c_str());
  549. result = false;
  550. }
  551. }
  552. else if(!this->Quiet || this->Required)
  553. {
  554. // The variable is not set.
  555. cmOStringStream e;
  556. e << "could not find ";
  557. if(!this->NoModule)
  558. {
  559. e << "module Find" << this->Name << ".cmake or ";
  560. }
  561. e << "a configuration file for package " << this->Name << ".\n";
  562. if(!this->NoModule)
  563. {
  564. e << "Adjust CMAKE_MODULE_PATH to find Find"
  565. << this->Name << ".cmake or set ";
  566. }
  567. else
  568. {
  569. e << "Set ";
  570. }
  571. e << this->Variable << " to the directory containing a CMake "
  572. << "configuration file for " << this->Name << ". ";
  573. if(this->Configs.size() == 1)
  574. {
  575. e << "The file will be called " << this->Configs[0];
  576. }
  577. else
  578. {
  579. e << "The file will have one of the following names:\n";
  580. for(std::vector<std::string>::const_iterator ci = this->Configs.begin();
  581. ci != this->Configs.end(); ++ci)
  582. {
  583. e << " " << *ci << "\n";
  584. }
  585. }
  586. if(this->Required)
  587. {
  588. this->SetError(e.str().c_str());
  589. result = false;
  590. }
  591. else
  592. {
  593. cmSystemTools::Error("find_package ", e.str().c_str());
  594. }
  595. }
  596. // Set a variable marking whether the package was found.
  597. std::string foundVar = this->Name;
  598. foundVar += "_FOUND";
  599. this->Makefile->AddDefinition(foundVar.c_str(), found? "1":"0");
  600. // Set a variable naming the configuration file that was found.
  601. std::string fileVar = this->Name;
  602. fileVar += "_CONFIG";
  603. if(found)
  604. {
  605. this->Makefile->AddDefinition(fileVar.c_str(), file.c_str());
  606. }
  607. else
  608. {
  609. this->Makefile->RemoveDefinition(fileVar.c_str());
  610. }
  611. // Handle some ancient compatibility stuff.
  612. if(this->Compatibility_1_6)
  613. {
  614. // Listfiles will be looking for the capitalized version of the
  615. // name. Provide it.
  616. this->Makefile->AddDefinition
  617. (upperDir.c_str(),
  618. this->Makefile->GetDefinition(this->Variable.c_str()));
  619. this->Makefile->AddDefinition
  620. (upperFound.c_str(),
  621. this->Makefile->GetDefinition(foundVar.c_str()));
  622. }
  623. #ifdef CMAKE_BUILD_WITH_CMAKE
  624. if(!(upperDir == this->Variable))
  625. {
  626. if(this->Compatibility_1_6)
  627. {
  628. // Listfiles may use the capitalized version of the name.
  629. // Remove any previously added watch.
  630. this->Makefile->GetVariableWatch()->RemoveWatch(
  631. upperDir.c_str(),
  632. cmFindPackageNeedBackwardsCompatibility
  633. );
  634. }
  635. else
  636. {
  637. // Listfiles should not be using the capitalized version of the
  638. // name. Add a watch to warn the user.
  639. this->Makefile->GetVariableWatch()->AddWatch(
  640. upperDir.c_str(),
  641. cmFindPackageNeedBackwardsCompatibility
  642. );
  643. }
  644. }
  645. #endif
  646. return result;
  647. }
  648. //----------------------------------------------------------------------------
  649. void cmFindPackageCommand::FindConfig()
  650. {
  651. // Compute the set of search prefixes.
  652. this->ComputePrefixes();
  653. // Look for the project's configuration file.
  654. bool found = false;
  655. // Search for frameworks.
  656. if(!found && this->SearchFrameworkFirst || this->SearchFrameworkOnly)
  657. {
  658. found = this->FindFrameworkConfig();
  659. }
  660. // Search for apps.
  661. if(!found && this->SearchAppBundleFirst || this->SearchAppBundleOnly)
  662. {
  663. found = this->FindAppBundleConfig();
  664. }
  665. // Search prefixes.
  666. if(!found && !(this->SearchFrameworkOnly || this->SearchAppBundleOnly))
  667. {
  668. found = this->FindPrefixedConfig();
  669. }
  670. // Search for frameworks.
  671. if(!found && this->SearchFrameworkLast)
  672. {
  673. found = this->FindFrameworkConfig();
  674. }
  675. // Search for apps.
  676. if(!found && this->SearchAppBundleLast)
  677. {
  678. found = this->FindAppBundleConfig();
  679. }
  680. // Store the entry in the cache so it can be set by the user.
  681. std::string init;
  682. if(found)
  683. {
  684. init = cmSystemTools::GetFilenamePath(this->FileFound);
  685. }
  686. else
  687. {
  688. init = this->Variable + "-NOTFOUND";
  689. }
  690. std::string help =
  691. "The directory containing a CMake configuration file for ";
  692. help += this->Name;
  693. help += ".";
  694. this->Makefile->AddCacheDefinition(this->Variable.c_str(),
  695. init.c_str(), help.c_str(),
  696. cmCacheManager::PATH);
  697. }
  698. //----------------------------------------------------------------------------
  699. bool cmFindPackageCommand::FindPrefixedConfig()
  700. {
  701. for(std::vector<std::string>::const_iterator pi = this->Prefixes.begin();
  702. pi != this->Prefixes.end(); ++pi)
  703. {
  704. if(this->SearchPrefix(*pi))
  705. {
  706. return true;
  707. }
  708. }
  709. return false;
  710. }
  711. //----------------------------------------------------------------------------
  712. bool cmFindPackageCommand::FindFrameworkConfig()
  713. {
  714. for(std::vector<std::string>::const_iterator i = this->Prefixes.begin();
  715. i != this->Prefixes.end(); ++i)
  716. {
  717. if(this->SearchFrameworkPrefix(*i))
  718. {
  719. return true;
  720. }
  721. }
  722. return false;
  723. }
  724. //----------------------------------------------------------------------------
  725. bool cmFindPackageCommand::FindAppBundleConfig()
  726. {
  727. for(std::vector<std::string>::const_iterator i = this->Prefixes.begin();
  728. i != this->Prefixes.end(); ++i)
  729. {
  730. if(this->SearchAppBundlePrefix(*i))
  731. {
  732. return true;
  733. }
  734. }
  735. return false;
  736. }
  737. //----------------------------------------------------------------------------
  738. bool cmFindPackageCommand::ReadListFile(const char* f)
  739. {
  740. if(this->Makefile->ReadListFile(this->Makefile->GetCurrentListFile(),f))
  741. {
  742. return true;
  743. }
  744. std::string e = "Error reading CMake code from \"";
  745. e += f;
  746. e += "\".";
  747. this->SetError(e.c_str());
  748. return false;
  749. }
  750. //----------------------------------------------------------------------------
  751. void cmFindPackageCommand::AppendToProperty(const char* propertyName)
  752. {
  753. std::string propertyValue;
  754. const char *prop =
  755. this->Makefile->GetCMakeInstance()->GetProperty(propertyName);
  756. if (prop && *prop)
  757. {
  758. propertyValue = prop;
  759. std::vector<std::string> contents;
  760. cmSystemTools::ExpandListArgument(propertyValue, contents, false);
  761. bool alreadyInserted = false;
  762. for(std::vector<std::string>::const_iterator it = contents.begin();
  763. it != contents.end(); ++ it )
  764. {
  765. if (*it == this->Name)
  766. {
  767. alreadyInserted = true;
  768. break;
  769. }
  770. }
  771. if (!alreadyInserted)
  772. {
  773. propertyValue += ";";
  774. propertyValue += this->Name;
  775. }
  776. }
  777. else
  778. {
  779. propertyValue = this->Name;
  780. }
  781. this->Makefile->GetCMakeInstance()->SetProperty(propertyName,
  782. propertyValue.c_str());
  783. }
  784. //----------------------------------------------------------------------------
  785. void cmFindPackageCommand::AppendSuccessInformation()
  786. {
  787. std::string found = this->Name;
  788. found += "_FOUND";
  789. std::string upperFound = cmSystemTools::UpperCase(found);
  790. const char* upperResult = this->Makefile->GetDefinition(upperFound.c_str());
  791. const char* result = this->Makefile->GetDefinition(found.c_str());
  792. if ((cmSystemTools::IsOn(result)) || (cmSystemTools::IsOn(upperResult)))
  793. {
  794. this->AppendToProperty("PACKAGES_FOUND");
  795. if (!this->Quiet)
  796. {
  797. this->AppendToProperty("ENABLED_FEATURES");
  798. }
  799. }
  800. else
  801. {
  802. this->AppendToProperty("PACKAGES_NOT_FOUND");
  803. if (!this->Quiet)
  804. {
  805. this->AppendToProperty("DISABLED_FEATURES");
  806. }
  807. }
  808. }
  809. //----------------------------------------------------------------------------
  810. void cmFindPackageCommand::AddUserPath(std::string const& p)
  811. {
  812. std::string userPath = p;
  813. cmSystemTools::ExpandRegistryValues(userPath);
  814. this->UserPaths.push_back(userPath);
  815. }
  816. //----------------------------------------------------------------------------
  817. void cmFindPackageCommand::ComputePrefixes()
  818. {
  819. std::vector<std::string>& prefixes = this->Prefixes;
  820. std::set<cmStdString> emmitted;
  821. if(!this->NoCMakeEnvironmentPath && !this->NoDefaultPath)
  822. {
  823. // Check the environment variable with the same name as the cache
  824. // entry.
  825. std::string env;
  826. if(cmSystemTools::GetEnv(this->Variable.c_str(), env) && env.length() > 0)
  827. {
  828. cmSystemTools::ConvertToUnixSlashes(env);
  829. this->AddPathInternal(prefixes, env, EnvPath, &emmitted);
  830. }
  831. this->AddEnvPath(prefixes, "CMAKE_PREFIX_PATH", &emmitted);
  832. this->AddEnvPath(prefixes, "CMAKE_FRAMEWORK_PATH", &emmitted);
  833. this->AddEnvPath(prefixes, "CMAKE_APPBUNDLE_PATH", &emmitted);
  834. }
  835. if(!this->NoCMakePath && !this->NoDefaultPath)
  836. {
  837. this->AddCMakePath(prefixes, "CMAKE_PREFIX_PATH", &emmitted);
  838. this->AddCMakePath(prefixes, "CMAKE_FRAMEWORK_PATH", &emmitted);
  839. this->AddCMakePath(prefixes, "CMAKE_APPBUNDLE_PATH", &emmitted);
  840. }
  841. if(!this->NoSystemEnvironmentPath && !this->NoDefaultPath)
  842. {
  843. // Use the system search path to generate prefixes.
  844. // Relative paths are interpreted with respect to the current
  845. // working directory.
  846. std::vector<std::string> tmp;
  847. cmSystemTools::GetPath(tmp);
  848. for(std::vector<std::string>::iterator i = tmp.begin();
  849. i != tmp.end(); ++i)
  850. {
  851. std::string const& d = *i;
  852. // If the path is a PREFIX/bin case then add its parent instead.
  853. if(d.size() >= 4 && strcmp(d.c_str()+d.size()-4, "/bin") == 0 ||
  854. d.size() >= 5 && strcmp(d.c_str()+d.size()-5, "/sbin") == 0)
  855. {
  856. this->AddPathInternal(prefixes,
  857. cmSystemTools::GetFilenamePath(d),
  858. EnvPath, &emmitted);
  859. }
  860. else
  861. {
  862. this->AddPathInternal(prefixes, d, EnvPath, &emmitted);
  863. }
  864. }
  865. }
  866. if(!this->NoBuilds && !this->NoDefaultPath)
  867. {
  868. // It is likely that CMake will have recently built the project.
  869. for(int i=1; i <= 10; ++i)
  870. {
  871. cmOStringStream r;
  872. r <<
  873. "[HKEY_CURRENT_USER\\Software\\Kitware\\CMakeSetup\\"
  874. "Settings\\StartPath;WhereBuild" << i << "]";
  875. std::string f = r.str();
  876. cmSystemTools::ExpandRegistryValues(f);
  877. cmSystemTools::ConvertToUnixSlashes(f);
  878. if(cmSystemTools::FileIsFullPath(f.c_str()) &&
  879. cmSystemTools::FileIsDirectory(f.c_str()))
  880. {
  881. this->AddPathInternal(prefixes, f, FullPath, &emmitted);
  882. }
  883. }
  884. }
  885. if(!this->NoCMakeSystemPath && !this->NoDefaultPath)
  886. {
  887. this->AddCMakePath(prefixes, "CMAKE_SYSTEM_PREFIX_PATH", &emmitted);
  888. this->AddCMakePath(prefixes, "CMAKE_SYSTEM_FRAMEWORK_PATH", &emmitted);
  889. this->AddCMakePath(prefixes, "CMAKE_SYSTEM_APPBUNDLE_PATH", &emmitted);
  890. }
  891. if(!this->UserPaths.empty())
  892. {
  893. // Add paths specified by the caller.
  894. this->AddPathsInternal(prefixes, this->UserPaths, CMakePath, &emmitted);
  895. }
  896. // Construct the final set of prefixes.
  897. this->RerootPaths(prefixes);
  898. // Add a trailing slash to all prefixes to aid the search process.
  899. for(std::vector<std::string>::iterator i = prefixes.begin();
  900. i != prefixes.end(); ++i)
  901. {
  902. std::string& prefix = *i;
  903. if(prefix[prefix.size()-1] != '/')
  904. {
  905. prefix += "/";
  906. }
  907. }
  908. }
  909. //----------------------------------------------------------------------------
  910. bool cmFindPackageCommand::SearchDirectory(std::string const& dir)
  911. {
  912. assert(!dir.empty() && dir[dir.size()-1] == '/');
  913. // Check each path suffix on this directory.
  914. for(std::vector<std::string>::const_iterator
  915. si = this->SearchPathSuffixes.begin();
  916. si != this->SearchPathSuffixes.end(); ++si)
  917. {
  918. std::string d = dir;
  919. if(!si->empty())
  920. {
  921. d += *si;
  922. d += "/";
  923. }
  924. if(this->CheckDirectory(d))
  925. {
  926. return true;
  927. }
  928. }
  929. return false;
  930. }
  931. //----------------------------------------------------------------------------
  932. bool cmFindPackageCommand::CheckDirectory(std::string const& dir)
  933. {
  934. assert(!dir.empty() && dir[dir.size()-1] == '/');
  935. // Look for the file in this directory.
  936. std::string d = dir.substr(0, dir.size()-1);
  937. if(this->FindConfigFile(d, this->FileFound))
  938. {
  939. return true;
  940. }
  941. return false;
  942. }
  943. //----------------------------------------------------------------------------
  944. bool cmFindPackageCommand::FindConfigFile(std::string const& dir,
  945. std::string& file)
  946. {
  947. for(std::vector<std::string>::const_iterator ci = this->Configs.begin();
  948. ci != this->Configs.end(); ++ci)
  949. {
  950. file = dir;
  951. file += "/";
  952. file += *ci;
  953. if(this->DebugMode)
  954. {
  955. fprintf(stderr, "Checking file [%s]\n", file.c_str());
  956. }
  957. if(cmSystemTools::FileExists(file.c_str(), true))
  958. {
  959. return true;
  960. }
  961. }
  962. return false;
  963. }
  964. //----------------------------------------------------------------------------
  965. #include <cmsys/Directory.hxx>
  966. #include <cmsys/Glob.hxx>
  967. #include <cmsys/String.h>
  968. #include <cmsys/auto_ptr.hxx>
  969. class cmFileList;
  970. class cmFileListGeneratorBase
  971. {
  972. public:
  973. virtual ~cmFileListGeneratorBase() {}
  974. protected:
  975. bool Consider(std::string const& fullPath, cmFileList& listing);
  976. private:
  977. bool Search(cmFileList&);
  978. virtual bool Search(std::string const& parent, cmFileList&) = 0;
  979. virtual cmsys::auto_ptr<cmFileListGeneratorBase> Clone() const = 0;
  980. friend class cmFileList;
  981. cmFileListGeneratorBase* SetNext(cmFileListGeneratorBase const& next);
  982. cmsys::auto_ptr<cmFileListGeneratorBase> Next;
  983. };
  984. class cmFileList
  985. {
  986. public:
  987. cmFileList(): First(), Last(0) {}
  988. virtual ~cmFileList() {}
  989. cmFileList& operator/(cmFileListGeneratorBase const& rhs)
  990. {
  991. if(this->Last)
  992. {
  993. this->Last = this->Last->SetNext(rhs);
  994. }
  995. else
  996. {
  997. this->First = rhs.Clone();
  998. this->Last = this->First.get();
  999. }
  1000. return *this;
  1001. }
  1002. bool Search()
  1003. {
  1004. if(this->First.get())
  1005. {
  1006. return this->First->Search(*this);
  1007. }
  1008. return false;
  1009. }
  1010. private:
  1011. virtual bool Visit(std::string const& fullPath) = 0;
  1012. friend class cmFileListGeneratorBase;
  1013. cmsys::auto_ptr<cmFileListGeneratorBase> First;
  1014. cmFileListGeneratorBase* Last;
  1015. };
  1016. class cmFindPackageFileList: public cmFileList
  1017. {
  1018. public:
  1019. cmFindPackageFileList(cmFindPackageCommand* fpc,
  1020. bool use_suffixes = true):
  1021. cmFileList(), FPC(fpc), UseSuffixes(use_suffixes) {}
  1022. private:
  1023. bool Visit(std::string const& fullPath)
  1024. {
  1025. if(this->UseSuffixes)
  1026. {
  1027. return this->FPC->SearchDirectory(fullPath);
  1028. }
  1029. else
  1030. {
  1031. return this->FPC->CheckDirectory(fullPath);
  1032. }
  1033. }
  1034. cmFindPackageCommand* FPC;
  1035. bool UseSuffixes;
  1036. };
  1037. bool cmFileListGeneratorBase::Search(cmFileList& listing)
  1038. {
  1039. return this->Search("", listing);
  1040. }
  1041. cmFileListGeneratorBase*
  1042. cmFileListGeneratorBase::SetNext(cmFileListGeneratorBase const& next)
  1043. {
  1044. this->Next = next.Clone();
  1045. return this->Next.get();
  1046. }
  1047. bool cmFileListGeneratorBase::Consider(std::string const& fullPath,
  1048. cmFileList& listing)
  1049. {
  1050. if(this->Next.get())
  1051. {
  1052. return this->Next->Search(fullPath + "/", listing);
  1053. }
  1054. else
  1055. {
  1056. return listing.Visit(fullPath + "/");
  1057. }
  1058. }
  1059. class cmFileListGeneratorFixed: public cmFileListGeneratorBase
  1060. {
  1061. public:
  1062. cmFileListGeneratorFixed(std::string const& str):
  1063. cmFileListGeneratorBase(), String(str) {}
  1064. cmFileListGeneratorFixed(cmFileListGeneratorFixed const& r):
  1065. cmFileListGeneratorBase(), String(r.String) {}
  1066. private:
  1067. std::string String;
  1068. virtual bool Search(std::string const& parent, cmFileList& lister)
  1069. {
  1070. std::string fullPath = parent + this->String;
  1071. return this->Consider(fullPath, lister);
  1072. }
  1073. virtual cmsys::auto_ptr<cmFileListGeneratorBase> Clone() const
  1074. {
  1075. cmsys::auto_ptr<cmFileListGeneratorBase>
  1076. g(new cmFileListGeneratorFixed(*this));
  1077. return g;
  1078. }
  1079. };
  1080. class cmFileListGeneratorEnumerate: public cmFileListGeneratorBase
  1081. {
  1082. public:
  1083. cmFileListGeneratorEnumerate(const char* p1, const char* p2):
  1084. cmFileListGeneratorBase()
  1085. {
  1086. this->Vector.push_back(p1);
  1087. this->Vector.push_back(p2);
  1088. }
  1089. cmFileListGeneratorEnumerate(cmFileListGeneratorEnumerate const& r):
  1090. cmFileListGeneratorBase(), Vector(r.Vector) {}
  1091. private:
  1092. std::vector<std::string> Vector;
  1093. virtual bool Search(std::string const& parent, cmFileList& lister)
  1094. {
  1095. for(std::vector<std::string>::const_iterator i = this->Vector.begin();
  1096. i != this->Vector.end(); ++i)
  1097. {
  1098. if(this->Consider(parent + *i, lister))
  1099. {
  1100. return true;
  1101. }
  1102. }
  1103. return false;
  1104. }
  1105. virtual cmsys::auto_ptr<cmFileListGeneratorBase> Clone() const
  1106. {
  1107. cmsys::auto_ptr<cmFileListGeneratorBase>
  1108. g(new cmFileListGeneratorEnumerate(*this));
  1109. return g;
  1110. }
  1111. };
  1112. class cmFileListGeneratorProject: public cmFileListGeneratorBase
  1113. {
  1114. public:
  1115. cmFileListGeneratorProject(std::vector<std::string> const& names):
  1116. cmFileListGeneratorBase(), Names(names) {}
  1117. cmFileListGeneratorProject(cmFileListGeneratorProject const& r):
  1118. cmFileListGeneratorBase(), Names(r.Names) {}
  1119. private:
  1120. std::vector<std::string> const& Names;
  1121. virtual bool Search(std::string const& parent, cmFileList& lister)
  1122. {
  1123. // Construct a list of matches.
  1124. std::vector<std::string> matches;
  1125. cmsys::Directory d;
  1126. d.Load(parent.c_str());
  1127. for(unsigned long i=0; i < d.GetNumberOfFiles(); ++i)
  1128. {
  1129. const char* fname = d.GetFile(i);
  1130. if(strcmp(fname, ".") == 0 ||
  1131. strcmp(fname, "..") == 0)
  1132. {
  1133. continue;
  1134. }
  1135. for(std::vector<std::string>::const_iterator ni = this->Names.begin();
  1136. ni != this->Names.end(); ++ni)
  1137. {
  1138. if(cmsysString_strncasecmp(fname, ni->c_str(),
  1139. ni->length()) == 0)
  1140. {
  1141. matches.push_back(fname);
  1142. }
  1143. }
  1144. }
  1145. for(std::vector<std::string>::const_iterator i = matches.begin();
  1146. i != matches.end(); ++i)
  1147. {
  1148. if(this->Consider(parent + *i, lister))
  1149. {
  1150. return true;
  1151. }
  1152. }
  1153. return false;
  1154. }
  1155. virtual cmsys::auto_ptr<cmFileListGeneratorBase> Clone() const
  1156. {
  1157. cmsys::auto_ptr<cmFileListGeneratorBase>
  1158. g(new cmFileListGeneratorProject(*this));
  1159. return g;
  1160. }
  1161. };
  1162. class cmFileListGeneratorMacProject: public cmFileListGeneratorBase
  1163. {
  1164. public:
  1165. cmFileListGeneratorMacProject(std::vector<std::string> const& names,
  1166. const char* ext):
  1167. cmFileListGeneratorBase(), Names(names), Extension(ext) {}
  1168. cmFileListGeneratorMacProject(cmFileListGeneratorMacProject const& r):
  1169. cmFileListGeneratorBase(), Names(r.Names), Extension(r.Extension) {}
  1170. private:
  1171. std::vector<std::string> const& Names;
  1172. std::string Extension;
  1173. virtual bool Search(std::string const& parent, cmFileList& lister)
  1174. {
  1175. // Construct a list of matches.
  1176. std::vector<std::string> matches;
  1177. cmsys::Directory d;
  1178. d.Load(parent.c_str());
  1179. for(unsigned long i=0; i < d.GetNumberOfFiles(); ++i)
  1180. {
  1181. const char* fname = d.GetFile(i);
  1182. if(strcmp(fname, ".") == 0 ||
  1183. strcmp(fname, "..") == 0)
  1184. {
  1185. continue;
  1186. }
  1187. for(std::vector<std::string>::const_iterator ni = this->Names.begin();
  1188. ni != this->Names.end(); ++ni)
  1189. {
  1190. std::string name = *ni;
  1191. name += this->Extension;
  1192. if(cmsysString_strcasecmp(fname, name.c_str()) == 0)
  1193. {
  1194. matches.push_back(fname);
  1195. }
  1196. }
  1197. }
  1198. for(std::vector<std::string>::const_iterator i = matches.begin();
  1199. i != matches.end(); ++i)
  1200. {
  1201. if(this->Consider(parent + *i, lister))
  1202. {
  1203. return true;
  1204. }
  1205. }
  1206. return false;
  1207. }
  1208. virtual cmsys::auto_ptr<cmFileListGeneratorBase> Clone() const
  1209. {
  1210. cmsys::auto_ptr<cmFileListGeneratorBase>
  1211. g(new cmFileListGeneratorMacProject(*this));
  1212. return g;
  1213. }
  1214. };
  1215. class cmFileListGeneratorCaseInsensitive: public cmFileListGeneratorBase
  1216. {
  1217. public:
  1218. cmFileListGeneratorCaseInsensitive(std::string const& str):
  1219. cmFileListGeneratorBase(), String(str) {}
  1220. cmFileListGeneratorCaseInsensitive(
  1221. cmFileListGeneratorCaseInsensitive const& r):
  1222. cmFileListGeneratorBase(), String(r.String) {}
  1223. private:
  1224. std::string String;
  1225. virtual bool Search(std::string const& parent, cmFileList& lister)
  1226. {
  1227. // Look for matching files.
  1228. std::vector<std::string> matches;
  1229. cmsys::Directory d;
  1230. d.Load(parent.c_str());
  1231. for(unsigned long i=0; i < d.GetNumberOfFiles(); ++i)
  1232. {
  1233. const char* fname = d.GetFile(i);
  1234. if(strcmp(fname, ".") == 0 ||
  1235. strcmp(fname, "..") == 0)
  1236. {
  1237. continue;
  1238. }
  1239. if(cmsysString_strcasecmp(fname, this->String.c_str()) == 0)
  1240. {
  1241. if(this->Consider(parent + fname, lister))
  1242. {
  1243. return true;
  1244. }
  1245. }
  1246. }
  1247. return false;
  1248. }
  1249. virtual cmsys::auto_ptr<cmFileListGeneratorBase> Clone() const
  1250. {
  1251. cmsys::auto_ptr<cmFileListGeneratorBase>
  1252. g(new cmFileListGeneratorCaseInsensitive(*this));
  1253. return g;
  1254. }
  1255. };
  1256. class cmFileListGeneratorGlob: public cmFileListGeneratorBase
  1257. {
  1258. public:
  1259. cmFileListGeneratorGlob(std::string const& str):
  1260. cmFileListGeneratorBase(), Pattern(str) {}
  1261. cmFileListGeneratorGlob(cmFileListGeneratorGlob const& r):
  1262. cmFileListGeneratorBase(), Pattern(r.Pattern) {}
  1263. private:
  1264. std::string Pattern;
  1265. virtual bool Search(std::string const& parent, cmFileList& lister)
  1266. {
  1267. // Glob the set of matching files.
  1268. std::string expr = parent;
  1269. expr += this->Pattern;
  1270. cmsys::Glob g;
  1271. if(!g.FindFiles(expr))
  1272. {
  1273. return false;
  1274. }
  1275. std::vector<std::string> const& files = g.GetFiles();
  1276. // Look for directories among the matches.
  1277. for(std::vector<std::string>::const_iterator fi = files.begin();
  1278. fi != files.end(); ++fi)
  1279. {
  1280. if(cmSystemTools::FileIsDirectory(fi->c_str()))
  1281. {
  1282. if(this->Consider(*fi, lister))
  1283. {
  1284. return true;
  1285. }
  1286. }
  1287. }
  1288. return false;
  1289. }
  1290. virtual cmsys::auto_ptr<cmFileListGeneratorBase> Clone() const
  1291. {
  1292. cmsys::auto_ptr<cmFileListGeneratorBase>
  1293. g(new cmFileListGeneratorGlob(*this));
  1294. return g;
  1295. }
  1296. };
  1297. //----------------------------------------------------------------------------
  1298. bool cmFindPackageCommand::SearchPrefix(std::string const& prefix_in)
  1299. {
  1300. assert(!prefix_in.empty() && prefix_in[prefix_in.size()-1] == '/');
  1301. if(this->DebugMode)
  1302. {
  1303. fprintf(stderr, "Checking prefix [%s]\n", prefix_in.c_str());
  1304. }
  1305. // Skip this if the prefix does not exist.
  1306. if(!cmSystemTools::FileIsDirectory(prefix_in.c_str()))
  1307. {
  1308. return false;
  1309. }
  1310. // PREFIX/ (useful on windows or in build trees)
  1311. if(this->SearchDirectory(prefix_in))
  1312. {
  1313. return true;
  1314. }
  1315. // Strip the trailing slash because the path generator is about to
  1316. // add one.
  1317. std::string prefix = prefix_in.substr(0, prefix_in.size()-1);
  1318. // PREFIX/(cmake|CMake)/ (useful on windows or in build trees)
  1319. {
  1320. cmFindPackageFileList lister(this);
  1321. lister
  1322. / cmFileListGeneratorFixed(prefix)
  1323. / cmFileListGeneratorCaseInsensitive("cmake");
  1324. if(lister.Search())
  1325. {
  1326. return true;
  1327. }
  1328. }
  1329. // PREFIX/(share|lib)/(Foo|foo|FOO).*/
  1330. {
  1331. cmFindPackageFileList lister(this);
  1332. lister
  1333. / cmFileListGeneratorFixed(prefix)
  1334. / cmFileListGeneratorEnumerate("lib", "share")
  1335. / cmFileListGeneratorProject(this->Names);
  1336. if(lister.Search())
  1337. {
  1338. return true;
  1339. }
  1340. }
  1341. // PREFIX/(share|lib)/(Foo|foo|FOO).*/(cmake|CMake)/
  1342. {
  1343. cmFindPackageFileList lister(this);
  1344. lister
  1345. / cmFileListGeneratorFixed(prefix)
  1346. / cmFileListGeneratorEnumerate("lib", "share")
  1347. / cmFileListGeneratorProject(this->Names)
  1348. / cmFileListGeneratorCaseInsensitive("cmake");
  1349. if(lister.Search())
  1350. {
  1351. return true;
  1352. }
  1353. }
  1354. return false;
  1355. }
  1356. //----------------------------------------------------------------------------
  1357. bool cmFindPackageCommand::SearchFrameworkPrefix(std::string const& prefix_in)
  1358. {
  1359. assert(!prefix_in.empty() && prefix_in[prefix_in.size()-1] == '/');
  1360. if(this->DebugMode)
  1361. {
  1362. fprintf(stderr, "Checking framework prefix [%s]\n", prefix_in.c_str());
  1363. }
  1364. // Strip the trailing slash because the path generator is about to
  1365. // add one.
  1366. std::string prefix = prefix_in.substr(0, prefix_in.size()-1);
  1367. // <prefix>/Foo.framework/Resources/
  1368. {
  1369. cmFindPackageFileList lister(this);
  1370. lister
  1371. / cmFileListGeneratorFixed(prefix)
  1372. / cmFileListGeneratorMacProject(this->Names, ".framework")
  1373. / cmFileListGeneratorFixed("Resources");
  1374. if(lister.Search())
  1375. {
  1376. return true;
  1377. }
  1378. }
  1379. // <prefix>/Foo.framework/Resources/CMake/
  1380. {
  1381. cmFindPackageFileList lister(this);
  1382. lister
  1383. / cmFileListGeneratorFixed(prefix)
  1384. / cmFileListGeneratorMacProject(this->Names, ".framework")
  1385. / cmFileListGeneratorFixed("Resources")
  1386. / cmFileListGeneratorCaseInsensitive("cmake");
  1387. if(lister.Search())
  1388. {
  1389. return true;
  1390. }
  1391. }
  1392. // <prefix>/Foo.framework/Versions/*/Resources/
  1393. {
  1394. cmFindPackageFileList lister(this);
  1395. lister
  1396. / cmFileListGeneratorFixed(prefix)
  1397. / cmFileListGeneratorMacProject(this->Names, ".framework")
  1398. / cmFileListGeneratorFixed("Versions")
  1399. / cmFileListGeneratorGlob("*/Resources");
  1400. if(lister.Search())
  1401. {
  1402. return true;
  1403. }
  1404. }
  1405. // <prefix>/Foo.framework/Versions/*/Resources/CMake/
  1406. {
  1407. cmFindPackageFileList lister(this);
  1408. lister
  1409. / cmFileListGeneratorFixed(prefix)
  1410. / cmFileListGeneratorMacProject(this->Names, ".framework")
  1411. / cmFileListGeneratorFixed("Versions")
  1412. / cmFileListGeneratorGlob("*/Resources")
  1413. / cmFileListGeneratorCaseInsensitive("cmake");
  1414. if(lister.Search())
  1415. {
  1416. return true;
  1417. }
  1418. }
  1419. return false;
  1420. }
  1421. //----------------------------------------------------------------------------
  1422. bool cmFindPackageCommand::SearchAppBundlePrefix(std::string const& prefix_in)
  1423. {
  1424. assert(!prefix_in.empty() && prefix_in[prefix_in.size()-1] == '/');
  1425. if(this->DebugMode)
  1426. {
  1427. fprintf(stderr, "Checking bundle prefix [%s]\n", prefix_in.c_str());
  1428. }
  1429. // Strip the trailing slash because the path generator is about to
  1430. // add one.
  1431. std::string prefix = prefix_in.substr(0, prefix_in.size()-1);
  1432. // <prefix>/Foo.app/Contents/Resources
  1433. {
  1434. cmFindPackageFileList lister(this);
  1435. lister
  1436. / cmFileListGeneratorFixed(prefix)
  1437. / cmFileListGeneratorMacProject(this->Names, ".app")
  1438. / cmFileListGeneratorFixed("Contents/Resources");
  1439. if(lister.Search())
  1440. {
  1441. return true;
  1442. }
  1443. }
  1444. // <prefix>/Foo.app/Contents/Resources/CMake
  1445. {
  1446. cmFindPackageFileList lister(this);
  1447. lister
  1448. / cmFileListGeneratorFixed(prefix)
  1449. / cmFileListGeneratorMacProject(this->Names, ".app")
  1450. / cmFileListGeneratorFixed("Contents/Resources")
  1451. / cmFileListGeneratorCaseInsensitive("cmake");
  1452. if(lister.Search())
  1453. {
  1454. return true;
  1455. }
  1456. }
  1457. return false;
  1458. }
  1459. // TODO: Version numbers? Perhaps have a listing component class that
  1460. // sorts by lexicographic and numerical ordering. Also try to match
  1461. // some command argument for the version. Alternatively provide an
  1462. // API that just returns a list of valid directories? Perhaps push a
  1463. // scope and try loading the target file just to get its version
  1464. // number? Could add a foo-version.cmake or FooVersion.cmake file
  1465. // in the projects that contains just version information.
  1466. // TODO: Debug cmsys::Glob double slash problem.
  1467. // TODO: Add registry entries after cmake system search path?
  1468. // Currently the user must specify them with the PATHS option.
  1469. //
  1470. // [HKEY_CURRENT_USER\Software\*\Foo*;InstallDir]
  1471. // [HKEY_CURRENT_USER\Software\*\*\Foo*;InstallDir]
  1472. // [HKEY_LOCAL_MACHINE\Software\*\Foo*;InstallDir]
  1473. // [HKEY_LOCAL_MACHINE\Software\*\*\Foo*;InstallDir]