BUG: fix some bad changes in progress calc
[cmake.git] / Source / cmFindPackageCommand.cxx
blob6d6d7c6e41b352fd840435a1ffb58d86c881a4fa
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmFindPackageCommand.cxx,v $
5 Language: C++
6 Date: $Date: 2008-01-29 14:57:39 $
7 Version: $Revision: 1.36 $
9 Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
10 See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
12 This software is distributed WITHOUT ANY WARRANTY; without even
13 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE. See the above copyright notices for more information.
16 =========================================================================*/
17 #include "cmFindPackageCommand.h"
18 #include <cmsys/RegularExpression.hxx>
20 #ifdef CMAKE_BUILD_WITH_CMAKE
21 #include "cmVariableWatch.h"
22 #endif
24 void cmFindPackageNeedBackwardsCompatibility(const std::string& variable,
25 int access_type, void*, const char* newValue,
26 const cmMakefile*)
28 (void)newValue;
29 #ifdef CMAKE_BUILD_WITH_CMAKE
30 if(access_type == cmVariableWatch::UNKNOWN_VARIABLE_READ_ACCESS)
32 std::string message = "An attempt was made to access a variable: ";
33 message += variable;
34 message +=
35 " that has not been defined. This variable is created by the "
36 "FIND_PACKAGE command. CMake version 1.6 always converted the "
37 "variable name to upper-case, but this behavior is no longer the "
38 "case. To fix this you might need to set the cache value of "
39 "CMAKE_BACKWARDS_COMPATIBILITY to 1.6 or less. If you are writing a "
40 "CMake listfile, you should change the variable reference to use "
41 "the case of the argument to FIND_PACKAGE.";
42 cmSystemTools::Error(message.c_str());
44 #else
45 (void)variable;
46 (void)access_type;
47 #endif
50 //----------------------------------------------------------------------------
51 cmFindPackageCommand::cmFindPackageCommand()
53 cmSystemTools::ReplaceString(this->GenericDocumentationRootPath,
54 "CMAKE_FIND_ROOT_PATH_MODE_XXX",
55 "CMAKE_FIND_ROOT_PATH_MODE_PACKAGE");
56 cmSystemTools::ReplaceString(this->GenericDocumentationPathsOrder,
57 "FIND_ARGS_XXX", "<package>");
58 cmSystemTools::ReplaceString(this->GenericDocumentationPathsOrder,
59 "FIND_XXX", "find_package");
60 this->CMakePathName = "PACKAGE";
61 this->Quiet = false;
62 this->Required = false;
63 this->NoBuilds = false;
64 this->NoModule = false;
65 this->DebugMode = false;
66 this->VersionMajor = 0;
67 this->VersionMinor = 0;
68 this->VersionPatch = 0;
69 this->VersionCount = 0;
70 this->VersionExact = false;
71 this->VersionFoundMajor = 0;
72 this->VersionFoundMinor = 0;
73 this->VersionFoundPatch = 0;
74 this->VersionFoundCount = 0;
75 this->CommandDocumentation =
76 " find_package(<package> [major[.minor[.patch]]] [EXACT] [QUIET]\n"
77 " [[REQUIRED|COMPONENTS] [components...]])\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 \"[major[.minor[.patch]]]\" version argument specifies a desired "
89 "version with which the package found should be compatible. "
90 "The EXACT option requests that the version be matched exactly. "
91 "Version support is currently provided only on a package-by-package "
92 "basis (details below).\n"
93 "User code should generally look for packages using the above simple "
94 "signature. The remainder of this command documentation specifies the "
95 "full command signature and details of the search process. Project "
96 "maintainers wishing to provide a package to be found by this command "
97 "are encouraged to read on.\n"
98 "The command has two modes by which it searches for packages: "
99 "\"Module\" mode and \"Config\" mode. "
100 "Module mode is available when the command is invoked with the above "
101 "reduced signature. "
102 "CMake searches for a file called \"Find<package>.cmake\" in "
103 "the CMAKE_MODULE_PATH followed by the CMake installation. "
104 "If the file is found, it is read and processed by CMake. "
105 "It is responsible for finding the package, checking the version, "
106 "and producing any needed messages. "
107 "Many find-modules provide limited or no support for versioning; "
108 "check the module documentation. "
109 "If no module is found the command proceeds to Config mode.\n"
110 "The complete Config mode command signature is:\n"
111 " find_package(<package> [major[.minor[.patch]]] [EXACT] [QUIET]\n"
112 " [[REQUIRED|COMPONENTS] [components...]] [NO_MODULE]\n"
113 " [NAMES name1 [name2 ...]]\n"
114 " [CONFIGS config1 [config2 ...]]\n"
115 " [PATHS path1 [path2 ... ]]\n"
116 " [PATH_SUFFIXES suffix1 [suffix2 ...]]\n"
117 " [NO_DEFAULT_PATH]\n"
118 " [NO_CMAKE_ENVIRONMENT_PATH]\n"
119 " [NO_CMAKE_PATH]\n"
120 " [NO_SYSTEM_ENVIRONMENT_PATH]\n"
121 " [NO_CMAKE_BUILDS_PATH]\n"
122 " [NO_CMAKE_SYSTEM_PATH]\n"
123 " [CMAKE_FIND_ROOT_PATH_BOTH |\n"
124 " ONLY_CMAKE_FIND_ROOT_PATH |\n"
125 " NO_CMAKE_FIND_ROOT_PATH])\n"
126 "The NO_MODULE option may be used to skip Module mode explicitly. "
127 "It is also implied by use of options not specified in the reduced "
128 "signature. "
129 "\n"
130 "Config mode attempts to locate a configuration file provided by the "
131 "package to be found. A cache entry called <package>_DIR is created to "
132 "hold the directory containing the file. "
133 "By default the command searches for a package with the name <package>. "
134 "If the NAMES option is given the names following it are used instead "
135 "of <package>. "
136 "The command searches for a file called \"<name>Config.cmake\" or "
137 "\"<lower-case-name>-config.cmake\" for each name specified. "
138 "A replacement set of possible configuration file names may be given "
139 "using the CONFIGS option. "
140 "The search procedure is specified below. Once found, the configuration "
141 "file is read and processed by CMake. Since the file is provided by the "
142 "package it already knows the location of package contents. "
143 "The full path to the configuration file is stored in the cmake "
144 "variable <package>_CONFIG."
145 "\n"
146 "If the package configuration file cannot be found CMake "
147 "will generate an error describing the problem unless the QUIET "
148 "argument is specified. If REQUIRED is specified and the package "
149 "is not found a fatal error is generated and the configure step stops "
150 "executing. If <package>_DIR has been set to a directory not containing "
151 "a configuration file a fatal error is always generated because user "
152 "intervention is required."
153 "\n"
154 "When the \"[major[.minor[.patch]]]\" version argument is specified "
155 "Config mode will only find a version of the package that claims "
156 "compatibility with the requested version. "
157 "If the EXACT option is given only a version of the package claiming "
158 "an exact match of the requested version may be found. "
159 "CMake does not establish any convention for the meaning of version "
160 "numbers. "
161 "Package version numbers are checked by \"version\" files provided by "
162 "the packages themselves. "
163 "For a candidate package confguration file \"<config-file>.cmake\" the "
164 "corresponding version file is located next to it and named either "
165 "\"<config-file>-version.cmake\" or \"<config-file>Version.cmake\". "
166 "If no such version file is available then the configuration file "
167 "is assumed to not be compatible with any requested version. "
168 "When a version file is found it is loaded to check the requested "
169 "version number. "
170 "The version file is loaded in a nested scope in which the following "
171 "variables have been defined:\n"
172 " PACKAGE_FIND_NAME = the <package> name\n"
173 " PACKAGE_FIND_VERSION = full requested version string\n"
174 " PACKAGE_FIND_VERSION_MAJOR = requested major version, if any\n"
175 " PACKAGE_FIND_VERSION_MINOR = requested minor version, if any\n"
176 " PACKAGE_FIND_VERSION_PATCH = requested patch version, if any\n"
177 "The version file checks whether it satisfies the requested version "
178 "and sets these variables:\n"
179 " PACKAGE_VERSION = package version (major[.minor[.patch]])\n"
180 " PACKAGE_VERSION_EXACT = true if version is exact match\n"
181 " PACKAGE_VERSION_COMPATIBLE = true if version is compatible\n"
182 "These variables are checked by the find_package command to determine "
183 "whether the configuration file provides an acceptable version. "
184 "They are not available after the find_package call returns. "
185 "If the version is acceptable the following variables are set:\n"
186 " <package>_VERSION = package version (major[.minor[.patch]])\n"
187 " <package>_VERSION_MAJOR = major from major[.minor[.patch]], if any\n"
188 " <package>_VERSION_MINOR = minor from major[.minor[.patch]], if any\n"
189 " <package>_VERSION_PATCH = patch from major[.minor[.patch]], if any\n"
190 "and the corresponding package configuration file is loaded. "
191 "When multiple package configuration files are available whose version "
192 "files claim compatibility with the version requested it is unspecified "
193 "which one is chosen. "
194 "No attempt is made to choose a highest or closest version number."
195 "\n"
196 "Config mode provides an elaborate interface and search procedure. "
197 "Much of the interface is provided for completeness and for use "
198 "internally by find-modules loaded by Module mode. "
199 "Most user code should simply call\n"
200 " find_package(<package> [major[.minor]] [EXACT] [REQUIRED|QUIET])\n"
201 "in order to find a package. Package maintainers providing CMake "
202 "package configuration files are encouraged to name and install "
203 "them such that the procedure outlined below will find them "
204 "without requiring use of additional options."
205 "\n"
206 "CMake constructs a set of possible installation prefixes for the "
207 "package. Under each prefix several directories are searched for a "
208 "configuration file. The tables below show the directories searched. "
209 "Each entry is meant for installation trees following Windows (W), "
210 "UNIX (U), or Apple (A) conventions.\n"
211 " <prefix>/ (W)\n"
212 " <prefix>/(cmake|CMake)/ (W)\n"
213 " <prefix>/(share|lib)/<name>*/ (U)\n"
214 " <prefix>/(share|lib)/<name>*/(cmake|CMake)/ (U)\n"
215 "On systems supporting OS X Frameworks and Application Bundles "
216 "the following directories are searched for frameworks or bundles "
217 "containing a configuration file:\n"
218 " <prefix>/<name>.framework/Resources/ (A)\n"
219 " <prefix>/<name>.framework/Resources/CMake/ (A)\n"
220 " <prefix>/<name>.framework/Versions/*/Resources/ (A)\n"
221 " <prefix>/<name>.framework/Versions/*/Resources/CMake/ (A)\n"
222 " <prefix>/<name>.app/Contents/Resources/ (A)\n"
223 " <prefix>/<name>.app/Contents/Resources/CMake/ (A)\n"
224 "In all cases the <name> is treated as case-insensitive and corresponds "
225 "to any of the names specified (<package> or names given by NAMES). "
226 "If PATH_SUFFIXES is specified the suffixes are appended to each "
227 "(W) or (U) directory entry one-by-one.\n"
228 "This set of directories is intended to work in cooperation with "
229 "projects that provide configuration files in their installation trees. "
230 "Directories above marked with (W) are intended for installations on "
231 "Windows where the prefix may point at the top of an application's "
232 "installation directory. Those marked with (U) are intended for "
233 "installations on UNIX platforms where the prefix is shared by "
234 "multiple packages. This is merely a convention, so all (W) and (U) "
235 "directories are still searched on all platforms. "
236 "Directories marked with (A) are intended for installations on "
237 "Apple platforms. The cmake variables CMAKE_FIND_FRAMEWORK and "
238 "CMAKE_FIND_APPBUNDLE determine the order of preference "
239 "as specified below.\n"
240 "The set of installation prefixes is constructed using the following "
241 "steps. If NO_DEFAULT_PATH is specified steps 1-5 are skipped.\n"
242 "1. Search cmake specific environment variables. This "
243 "can be skipped if NO_CMAKE_ENVIRONMENT_PATH is passed.\n"
244 " CMAKE_PREFIX_PATH\n"
245 " CMAKE_FRAMEWORK_PATH\n"
246 " CMAKE_APPBUNDLE_PATH\n"
247 "2. Search cmake variables with the same names as the cmake specific "
248 "environment variables. These are intended to be used on the command "
249 "line with a -DVAR=value. This can be skipped if NO_CMAKE_PATH "
250 "is passed.\n"
251 " CMAKE_PREFIX_PATH\n"
252 " CMAKE_FRAMEWORK_PATH\n"
253 " CMAKE_APPBUNDLE_PATH\n"
254 "3. Search the standard system environment variables. "
255 "This can be skipped if NO_SYSTEM_ENVIRONMENT_PATH is passed. "
256 "Path entries ending in \"/bin\" or \"/sbin\" are automatically "
257 "converted to their parent directories.\n"
258 " PATH\n"
259 "4. Search project build trees recently configured in a CMake GUI. "
260 "This can be skipped if NO_CMAKE_BUILDS_PATH is passed. "
261 "It is intended for the case when a user is building multiple "
262 "dependent projects one after another.\n"
263 "5. Search cmake variables defined in the Platform files "
264 "for the current system. This can be skipped if NO_CMAKE_SYSTEM_PATH "
265 "is passed.\n"
266 " CMAKE_SYSTEM_PREFIX_PATH\n"
267 " CMAKE_SYSTEM_FRAMEWORK_PATH\n"
268 " CMAKE_SYSTEM_APPBUNDLE_PATH\n"
269 "6. Search paths specified by the PATHS option.\n"
271 this->CommandDocumentation += this->GenericDocumentationMacPolicy;
272 this->CommandDocumentation += this->GenericDocumentationRootPath;
273 this->CommandDocumentation += this->GenericDocumentationPathsOrder;
276 //----------------------------------------------------------------------------
277 const char* cmFindPackageCommand::GetFullDocumentation()
279 return this->CommandDocumentation.c_str();
282 //----------------------------------------------------------------------------
283 bool cmFindPackageCommand
284 ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
286 if(args.size() < 1)
288 this->SetError("called with incorrect number of arguments");
289 return false;
292 // Check for debug mode.
293 this->DebugMode = this->Makefile->IsOn("CMAKE_FIND_DEBUG_MODE");
295 // Find the current root path mode.
296 this->SelectDefaultRootPathMode();
298 // Find the current bundle/framework search policy.
299 this->SelectDefaultMacMode();
301 // Record options.
302 this->Name = args[0];
303 std::string components;
304 const char* components_sep = "";
306 // Check ancient compatibility.
307 this->Compatibility_1_6 =
308 this->Makefile->GetLocalGenerator()
309 ->NeedBackwardsCompatibility(1, 6);
311 // Always search directly in a generated path.
312 this->SearchPathSuffixes.push_back("");
314 // Parse the arguments.
315 enum Doing { DoingNone, DoingComponents, DoingNames, DoingPaths,
316 DoingPathSuffixes, DoingConfigs };
317 Doing doing = DoingNone;
318 cmsys::RegularExpression version("^[0-9.]+$");
319 bool haveVersion = false;
320 for(unsigned int i=1; i < args.size(); ++i)
322 if(args[i] == "QUIET")
324 this->Quiet = true;
325 doing = DoingNone;
327 else if(args[i] == "EXACT")
329 this->VersionExact = true;
330 this->Compatibility_1_6 = false;
331 doing = DoingNone;
333 else if(args[i] == "NO_MODULE")
335 this->NoModule = true;
336 doing = DoingNone;
338 else if(args[i] == "REQUIRED")
340 this->Required = true;
341 doing = DoingComponents;
343 else if(args[i] == "COMPONENTS")
345 this->Compatibility_1_6 = false;
346 doing = DoingComponents;
348 else if(args[i] == "NAMES")
350 this->NoModule = true;
351 this->Compatibility_1_6 = false;
352 doing = DoingNames;
354 else if(args[i] == "PATHS")
356 this->NoModule = true;
357 this->Compatibility_1_6 = false;
358 doing = DoingPaths;
360 else if(args[i] == "PATH_SUFFIXES")
362 this->NoModule = true;
363 this->Compatibility_1_6 = false;
364 doing = DoingPathSuffixes;
366 else if(args[i] == "CONFIGS")
368 this->NoModule = true;
369 this->Compatibility_1_6 = false;
370 doing = DoingConfigs;
372 else if(args[i] == "NO_CMAKE_BUILDS_PATH")
374 this->NoBuilds = true;
375 this->NoModule = true;
376 this->Compatibility_1_6 = false;
377 doing = DoingNone;
379 else if(this->CheckCommonArgument(args[i]))
381 this->NoModule = true;
382 this->Compatibility_1_6 = false;
383 doing = DoingNone;
385 else if(doing == DoingComponents)
387 // Set a variable telling the find script this component
388 // is required.
389 std::string req_var = Name + "_FIND_REQUIRED_" + args[i];
390 this->Makefile->AddDefinition(req_var.c_str(), "1");
392 // Append to the list of required components.
393 components += components_sep;
394 components += args[i];
395 components_sep = ";";
397 else if(doing == DoingNames)
399 this->Names.push_back(args[i]);
401 else if(doing == DoingPaths)
403 this->AddUserPath(args[i]);
405 else if(doing == DoingPathSuffixes)
407 this->AddPathSuffix(args[i]);
409 else if(doing == DoingConfigs)
411 if(args[i].find_first_of(":/\\") != args[i].npos ||
412 cmSystemTools::GetFilenameLastExtension(args[i]) != ".cmake")
414 cmOStringStream e;
415 e << "given CONFIGS option followed by invalid file name \""
416 << args[i] << "\". The names given must be file names without "
417 << "a path and with a \".cmake\" extension.";
418 return false;
420 this->Configs.push_back(args[i]);
422 else if(!haveVersion && version.find(args[i].c_str()))
424 haveVersion = true;
425 this->Version = args[i];
427 else
429 cmOStringStream e;
430 e << "called with invalid argument \"" << args[i].c_str() << "\"";
431 this->SetError(e.str().c_str());
432 return false;
436 if(!this->Version.empty())
438 // Try to parse the version number and store the results that were
439 // successfully parsed.
440 unsigned int parsed_major;
441 unsigned int parsed_minor;
442 unsigned int parsed_patch;
443 this->VersionCount = sscanf(this->Version.c_str(), "%u.%u.%u",
444 &parsed_major, &parsed_minor, &parsed_patch);
445 switch(this->VersionCount)
447 case 3: this->VersionPatch = parsed_patch; // no break!
448 case 2: this->VersionMinor = parsed_minor; // no break!
449 case 1: this->VersionMajor = parsed_major; // no break!
450 default: break;
454 // Store the list of components.
455 std::string components_var = Name + "_FIND_COMPONENTS";
456 this->Makefile->AddDefinition(components_var.c_str(), components.c_str());
458 // See if there is a Find<package>.cmake module.
459 if(!this->NoModule)
461 bool foundModule = false;
462 if(!this->FindModule(foundModule))
464 this->AppendSuccessInformation();
465 return false;
467 if(foundModule)
469 this->AppendSuccessInformation();
470 return true;
474 // No find module. Assume the project has a CMake config file. Use
475 // a <package>_DIR cache variable to locate it.
476 this->Variable = this->Name;
477 this->Variable += "_DIR";
479 // Add the default name.
480 if(this->Names.empty())
482 this->Names.push_back(this->Name);
485 // Add the default configs.
486 if(this->Configs.empty())
488 for(std::vector<std::string>::const_iterator ni = this->Names.begin();
489 ni != this->Names.end(); ++ni)
491 std::string config = *ni;
492 config += "Config.cmake";
493 this->Configs.push_back(config);
495 config = cmSystemTools::LowerCase(*ni);
496 config += "-config.cmake";
497 this->Configs.push_back(config);
501 // Find and load the package.
502 bool result = this->HandlePackageMode();
503 this->AppendSuccessInformation();
504 return result;
507 //----------------------------------------------------------------------------
508 bool cmFindPackageCommand::FindModule(bool& found)
510 std::string module = "Find";
511 module += this->Name;
512 module += ".cmake";
513 std::string mfile = this->Makefile->GetModulesFile(module.c_str());
514 if ( mfile.size() )
516 if(this->Quiet)
518 // Tell the module that is about to be read that it should find
519 // quietly.
520 std::string quietly = this->Name;
521 quietly += "_FIND_QUIETLY";
522 this->Makefile->AddDefinition(quietly.c_str(), "1");
525 if(this->Required)
527 // Tell the module that is about to be read that it should report
528 // a fatal error if the package is not found.
529 std::string req = this->Name;
530 req += "_FIND_REQUIRED";
531 this->Makefile->AddDefinition(req.c_str(), "1");
534 if(!this->Version.empty())
536 // Tell the module that is about to be read what version of the
537 // package has been requested.
538 std::string ver = this->Name;
539 ver += "_FIND_VERSION";
540 this->Makefile->AddDefinition(ver.c_str(), this->Version.c_str());
541 char buf[64];
542 switch(this->VersionCount)
544 case 3:
546 sprintf(buf, "%u", this->VersionPatch);
547 this->Makefile->AddDefinition((ver+"_PATCH").c_str(), buf);
548 } // no break
549 case 2:
551 sprintf(buf, "%u", this->VersionMinor);
552 this->Makefile->AddDefinition((ver+"_MINOR").c_str(), buf);
553 } // no break
554 case 1:
556 sprintf(buf, "%u", this->VersionMajor);
557 this->Makefile->AddDefinition((ver+"_MAJOR").c_str(), buf);
558 } // no break
559 default: break;
562 // Tell the module whether an exact version has been requested.
563 std::string exact = this->Name;
564 exact += "_FIND_VERSION_EXACT";
565 this->Makefile->AddDefinition(exact.c_str(),
566 this->VersionExact? "1":"0");
569 // Load the module we found.
570 found = true;
571 return this->ReadListFile(mfile.c_str());
573 return true;
576 //----------------------------------------------------------------------------
577 bool cmFindPackageCommand::HandlePackageMode()
579 // Support old capitalization behavior.
580 std::string upperDir = cmSystemTools::UpperCase(this->Name);
581 std::string upperFound = cmSystemTools::UpperCase(this->Name);
582 upperDir += "_DIR";
583 upperFound += "_FOUND";
584 if(upperDir == this->Variable)
586 this->Compatibility_1_6 = false;
589 // Try to find the config file.
590 const char* def = this->Makefile->GetDefinition(this->Variable.c_str());
591 if(this->Compatibility_1_6 && cmSystemTools::IsOff(def))
593 // Use the setting of the old name of the variable to provide the
594 // value of the new.
595 const char* oldDef = this->Makefile->GetDefinition(upperDir.c_str());
596 if(!cmSystemTools::IsOff(oldDef))
598 this->Makefile->AddDefinition(this->Variable.c_str(), oldDef);
599 def = this->Makefile->GetDefinition(this->Variable.c_str());
603 // Search for the config file if it is not already found.
604 if(cmSystemTools::IsOff(def))
606 this->FindConfig();
607 def = this->Makefile->GetDefinition(this->Variable.c_str());
610 // If the config file was found, load it.
611 std::string file;
612 bool result = true;
613 bool found = false;
614 if(!cmSystemTools::IsOff(def))
616 // Get the directory from the variable value.
617 std::string dir = def;
618 cmSystemTools::ConvertToUnixSlashes(dir);
620 // Treat relative paths with respect to the current source dir.
621 if(!cmSystemTools::FileIsFullPath(dir.c_str()))
623 dir = "/" + dir;
624 dir = this->Makefile->GetCurrentDirectory() + dir;
627 // Find the configuration file.
628 if(this->FindConfigFileToLoad(dir, file))
630 // Set the version variables before loading the config file.
631 // It may override them.
632 this->StoreVersionFound();
634 // Parse the configuration file.
635 if(this->ReadListFile(file.c_str()))
637 // The package has been found.
638 found = true;
640 else
642 // The configuration file is invalid.
643 result = false;
646 else
648 // The variable setting is wrong.
649 cmOStringStream e;
650 e << "cannot find package " << this->Name << " because "
651 << this->Variable << " is set to \"" << def << "\" "
652 << "which is not a directory containing a package configuration "
653 << "file (or it is not for the requested version). "
654 << "Please set the cache entry " << this->Variable << " "
655 << "to the correct directory, or delete it to ask CMake "
656 << "to search.";
657 this->SetError(e.str().c_str());
658 result = false;
661 else if(!this->Quiet || this->Required)
663 // The variable is not set.
664 cmOStringStream e;
665 e << "could not find ";
666 if(!this->NoModule)
668 e << "module Find" << this->Name << ".cmake or ";
670 e << "a configuration file for package " << this->Name << ".\n";
671 if(!this->NoModule)
673 e << "Adjust CMAKE_MODULE_PATH to find Find"
674 << this->Name << ".cmake or set ";
676 else
678 e << "Set ";
680 e << this->Variable << " to the directory containing a CMake "
681 << "configuration file for " << this->Name << ". ";
682 if(this->Configs.size() == 1)
684 e << "The file will be called " << this->Configs[0];
686 else
688 e << "The file will have one of the following names:\n";
689 for(std::vector<std::string>::const_iterator ci = this->Configs.begin();
690 ci != this->Configs.end(); ++ci)
692 e << " " << *ci << "\n";
695 if(this->Required)
697 this->SetError(e.str().c_str());
698 result = false;
700 else
702 cmSystemTools::Error("find_package ", e.str().c_str());
706 // Set a variable marking whether the package was found.
707 std::string foundVar = this->Name;
708 foundVar += "_FOUND";
709 this->Makefile->AddDefinition(foundVar.c_str(), found? "1":"0");
711 // Set a variable naming the configuration file that was found.
712 std::string fileVar = this->Name;
713 fileVar += "_CONFIG";
714 if(found)
716 this->Makefile->AddDefinition(fileVar.c_str(), file.c_str());
718 else
720 this->Makefile->RemoveDefinition(fileVar.c_str());
723 // Handle some ancient compatibility stuff.
724 if(this->Compatibility_1_6)
726 // Listfiles will be looking for the capitalized version of the
727 // name. Provide it.
728 this->Makefile->AddDefinition
729 (upperDir.c_str(),
730 this->Makefile->GetDefinition(this->Variable.c_str()));
731 this->Makefile->AddDefinition
732 (upperFound.c_str(),
733 this->Makefile->GetDefinition(foundVar.c_str()));
736 #ifdef CMAKE_BUILD_WITH_CMAKE
737 if(!(upperDir == this->Variable))
739 if(this->Compatibility_1_6)
741 // Listfiles may use the capitalized version of the name.
742 // Remove any previously added watch.
743 this->Makefile->GetVariableWatch()->RemoveWatch(
744 upperDir.c_str(),
745 cmFindPackageNeedBackwardsCompatibility
748 else
750 // Listfiles should not be using the capitalized version of the
751 // name. Add a watch to warn the user.
752 this->Makefile->GetVariableWatch()->AddWatch(
753 upperDir.c_str(),
754 cmFindPackageNeedBackwardsCompatibility
758 #endif
760 return result;
763 //----------------------------------------------------------------------------
764 void cmFindPackageCommand::FindConfig()
766 // Compute the set of search prefixes.
767 this->ComputePrefixes();
769 // Look for the project's configuration file.
770 bool found = false;
772 // Search for frameworks.
773 if(!found && this->SearchFrameworkFirst || this->SearchFrameworkOnly)
775 found = this->FindFrameworkConfig();
778 // Search for apps.
779 if(!found && this->SearchAppBundleFirst || this->SearchAppBundleOnly)
781 found = this->FindAppBundleConfig();
784 // Search prefixes.
785 if(!found && !(this->SearchFrameworkOnly || this->SearchAppBundleOnly))
787 found = this->FindPrefixedConfig();
790 // Search for frameworks.
791 if(!found && this->SearchFrameworkLast)
793 found = this->FindFrameworkConfig();
796 // Search for apps.
797 if(!found && this->SearchAppBundleLast)
799 found = this->FindAppBundleConfig();
802 // Store the entry in the cache so it can be set by the user.
803 std::string init;
804 if(found)
806 init = cmSystemTools::GetFilenamePath(this->FileFound);
808 else
810 init = this->Variable + "-NOTFOUND";
812 std::string help =
813 "The directory containing a CMake configuration file for ";
814 help += this->Name;
815 help += ".";
816 this->Makefile->AddCacheDefinition(this->Variable.c_str(),
817 init.c_str(), help.c_str(),
818 cmCacheManager::PATH);
821 //----------------------------------------------------------------------------
822 bool cmFindPackageCommand::FindPrefixedConfig()
824 for(std::vector<std::string>::const_iterator pi = this->Prefixes.begin();
825 pi != this->Prefixes.end(); ++pi)
827 if(this->SearchPrefix(*pi))
829 return true;
832 return false;
835 //----------------------------------------------------------------------------
836 bool cmFindPackageCommand::FindFrameworkConfig()
838 for(std::vector<std::string>::const_iterator i = this->Prefixes.begin();
839 i != this->Prefixes.end(); ++i)
841 if(this->SearchFrameworkPrefix(*i))
843 return true;
846 return false;
849 //----------------------------------------------------------------------------
850 bool cmFindPackageCommand::FindAppBundleConfig()
852 for(std::vector<std::string>::const_iterator i = this->Prefixes.begin();
853 i != this->Prefixes.end(); ++i)
855 if(this->SearchAppBundlePrefix(*i))
857 return true;
860 return false;
863 //----------------------------------------------------------------------------
864 bool cmFindPackageCommand::ReadListFile(const char* f)
866 if(this->Makefile->ReadListFile(this->Makefile->GetCurrentListFile(),f))
868 return true;
870 std::string e = "Error reading CMake code from \"";
871 e += f;
872 e += "\".";
873 this->SetError(e.c_str());
874 return false;
877 //----------------------------------------------------------------------------
878 void cmFindPackageCommand::AppendToProperty(const char* propertyName)
880 std::string propertyValue;
881 const char *prop =
882 this->Makefile->GetCMakeInstance()->GetProperty(propertyName);
883 if (prop && *prop)
885 propertyValue = prop;
887 std::vector<std::string> contents;
888 cmSystemTools::ExpandListArgument(propertyValue, contents, false);
890 bool alreadyInserted = false;
891 for(std::vector<std::string>::const_iterator it = contents.begin();
892 it != contents.end(); ++ it )
894 if (*it == this->Name)
896 alreadyInserted = true;
897 break;
900 if (!alreadyInserted)
902 propertyValue += ";";
903 propertyValue += this->Name;
906 else
908 propertyValue = this->Name;
910 this->Makefile->GetCMakeInstance()->SetProperty(propertyName,
911 propertyValue.c_str());
914 //----------------------------------------------------------------------------
915 void cmFindPackageCommand::AppendSuccessInformation()
917 std::string found = this->Name;
918 found += "_FOUND";
919 std::string upperFound = cmSystemTools::UpperCase(found);
921 const char* upperResult = this->Makefile->GetDefinition(upperFound.c_str());
922 const char* result = this->Makefile->GetDefinition(found.c_str());
923 if ((cmSystemTools::IsOn(result)) || (cmSystemTools::IsOn(upperResult)))
925 this->AppendToProperty("PACKAGES_FOUND");
926 if (!this->Quiet)
928 this->AppendToProperty("ENABLED_FEATURES");
931 else
933 this->AppendToProperty("PACKAGES_NOT_FOUND");
934 if (!this->Quiet)
936 this->AppendToProperty("DISABLED_FEATURES");
941 //----------------------------------------------------------------------------
942 void cmFindPackageCommand::AddUserPath(std::string const& p)
944 std::string userPath = p;
945 cmSystemTools::ExpandRegistryValues(userPath);
946 this->UserPaths.push_back(userPath);
949 //----------------------------------------------------------------------------
950 void cmFindPackageCommand::ComputePrefixes()
952 std::vector<std::string>& prefixes = this->Prefixes;
953 std::set<cmStdString> emmitted;
955 if(!this->NoCMakeEnvironmentPath && !this->NoDefaultPath)
957 // Check the environment variable with the same name as the cache
958 // entry.
959 std::string env;
960 if(cmSystemTools::GetEnv(this->Variable.c_str(), env) && env.length() > 0)
962 cmSystemTools::ConvertToUnixSlashes(env);
963 this->AddPathInternal(prefixes, env, EnvPath, &emmitted);
966 this->AddEnvPath(prefixes, "CMAKE_PREFIX_PATH", &emmitted);
967 this->AddEnvPath(prefixes, "CMAKE_FRAMEWORK_PATH", &emmitted);
968 this->AddEnvPath(prefixes, "CMAKE_APPBUNDLE_PATH", &emmitted);
971 if(!this->NoCMakePath && !this->NoDefaultPath)
973 this->AddCMakePath(prefixes, "CMAKE_PREFIX_PATH", &emmitted);
974 this->AddCMakePath(prefixes, "CMAKE_FRAMEWORK_PATH", &emmitted);
975 this->AddCMakePath(prefixes, "CMAKE_APPBUNDLE_PATH", &emmitted);
978 if(!this->NoSystemEnvironmentPath && !this->NoDefaultPath)
980 // Use the system search path to generate prefixes.
981 // Relative paths are interpreted with respect to the current
982 // working directory.
983 std::vector<std::string> tmp;
984 cmSystemTools::GetPath(tmp);
985 for(std::vector<std::string>::iterator i = tmp.begin();
986 i != tmp.end(); ++i)
988 std::string const& d = *i;
990 // If the path is a PREFIX/bin case then add its parent instead.
991 if(d.size() >= 4 && strcmp(d.c_str()+d.size()-4, "/bin") == 0 ||
992 d.size() >= 5 && strcmp(d.c_str()+d.size()-5, "/sbin") == 0)
994 this->AddPathInternal(prefixes,
995 cmSystemTools::GetFilenamePath(d),
996 EnvPath, &emmitted);
998 else
1000 this->AddPathInternal(prefixes, d, EnvPath, &emmitted);
1005 if(!this->NoBuilds && !this->NoDefaultPath)
1007 // It is likely that CMake will have recently built the project.
1008 for(int i=1; i <= 10; ++i)
1010 cmOStringStream r;
1011 r <<
1012 "[HKEY_CURRENT_USER\\Software\\Kitware\\CMakeSetup\\"
1013 "Settings\\StartPath;WhereBuild" << i << "]";
1014 std::string f = r.str();
1015 cmSystemTools::ExpandRegistryValues(f);
1016 cmSystemTools::ConvertToUnixSlashes(f);
1017 if(cmSystemTools::FileIsFullPath(f.c_str()) &&
1018 cmSystemTools::FileIsDirectory(f.c_str()))
1020 this->AddPathInternal(prefixes, f, FullPath, &emmitted);
1025 if(!this->NoCMakeSystemPath && !this->NoDefaultPath)
1027 this->AddCMakePath(prefixes, "CMAKE_SYSTEM_PREFIX_PATH", &emmitted);
1028 this->AddCMakePath(prefixes, "CMAKE_SYSTEM_FRAMEWORK_PATH", &emmitted);
1029 this->AddCMakePath(prefixes, "CMAKE_SYSTEM_APPBUNDLE_PATH", &emmitted);
1032 if(!this->UserPaths.empty())
1034 // Add paths specified by the caller.
1035 this->AddPathsInternal(prefixes, this->UserPaths, CMakePath, &emmitted);
1038 // Construct the final set of prefixes.
1039 this->RerootPaths(prefixes);
1041 // Add a trailing slash to all prefixes to aid the search process.
1042 for(std::vector<std::string>::iterator i = prefixes.begin();
1043 i != prefixes.end(); ++i)
1045 std::string& prefix = *i;
1046 if(prefix[prefix.size()-1] != '/')
1048 prefix += "/";
1053 //----------------------------------------------------------------------------
1054 bool cmFindPackageCommand::SearchDirectory(std::string const& dir)
1056 assert(!dir.empty() && dir[dir.size()-1] == '/');
1058 // Check each path suffix on this directory.
1059 for(std::vector<std::string>::const_iterator
1060 si = this->SearchPathSuffixes.begin();
1061 si != this->SearchPathSuffixes.end(); ++si)
1063 std::string d = dir;
1064 if(!si->empty())
1066 d += *si;
1067 d += "/";
1069 if(this->CheckDirectory(d))
1071 return true;
1074 return false;
1077 //----------------------------------------------------------------------------
1078 bool cmFindPackageCommand::CheckDirectory(std::string const& dir)
1080 assert(!dir.empty() && dir[dir.size()-1] == '/');
1082 // Look for the file in this directory.
1083 std::string d = dir.substr(0, dir.size()-1);
1084 if(this->FindConfigFile(d, this->FileFound))
1086 // Remove duplicate slashes.
1087 cmSystemTools::ConvertToUnixSlashes(this->FileFound);
1088 return true;
1090 return false;
1093 //----------------------------------------------------------------------------
1094 bool cmFindPackageCommand::FindConfigFile(std::string const& dir,
1095 std::string& file)
1097 for(std::vector<std::string>::const_iterator ci = this->Configs.begin();
1098 ci != this->Configs.end(); ++ci)
1100 file = dir;
1101 file += "/";
1102 file += *ci;
1103 if(this->DebugMode)
1105 fprintf(stderr, "Checking file [%s]\n", file.c_str());
1107 if(cmSystemTools::FileExists(file.c_str(), true) &&
1108 this->CheckVersion(file))
1110 return true;
1113 return false;
1116 //----------------------------------------------------------------------------
1117 bool cmFindPackageCommand::FindConfigFileToLoad(std::string const& dir,
1118 std::string& file)
1120 if(this->FileFound.empty())
1122 // The file location was cached. Look for the correct file.
1123 return this->FindConfigFile(dir, file);
1125 else
1127 // The file location was just found during this call.
1128 // Use the file found without searching again.
1129 file = this->FileFound;
1130 return true;
1134 //----------------------------------------------------------------------------
1135 bool cmFindPackageCommand::CheckVersion(std::string const& config_file)
1137 // Get the filename without the .cmake extension.
1138 std::string::size_type pos = config_file.rfind('.');
1139 std::string version_file_base = config_file.substr(0, pos);
1141 // Look for foo-config-version.cmake
1142 std::string version_file = version_file_base;
1143 version_file += "-version.cmake";
1144 if(cmSystemTools::FileExists(version_file.c_str(), true))
1146 return this->CheckVersionFile(version_file);
1149 // Look for fooConfigVersion.cmake
1150 version_file = version_file_base;
1151 version_file += "Version.cmake";
1152 if(cmSystemTools::FileExists(version_file.c_str(), true))
1154 return this->CheckVersionFile(version_file);
1157 // If no version was requested a versionless package is acceptable.
1158 if(this->Version.empty())
1160 return true;
1163 // No version file found. Assume the version is incompatible.
1164 return false;
1167 //----------------------------------------------------------------------------
1168 bool cmFindPackageCommand::CheckVersionFile(std::string const& version_file)
1170 // The version file will be loaded in an isolated scope.
1171 this->Makefile->PushScope();
1173 // Clear the output variables.
1174 this->Makefile->RemoveDefinition("PACKAGE_VERSION");
1175 this->Makefile->RemoveDefinition("PACKAGE_VERSION_COMPATIBLE");
1176 this->Makefile->RemoveDefinition("PACKAGE_VERSION_EXACT");
1178 // Set the input variables.
1179 this->Makefile->AddDefinition("PACKAGE_FIND_NAME", this->Name.c_str());
1180 this->Makefile->AddDefinition("PACKAGE_FIND_VERSION",
1181 this->Version.c_str());
1182 if(this->VersionCount >= 3)
1184 char buf[64];
1185 sprintf(buf, "%u", this->VersionPatch);
1186 this->Makefile->AddDefinition("PACKAGE_FIND_VERSION_PATCH", buf);
1188 else
1190 this->Makefile->RemoveDefinition("PACKAGE_FIND_VERSION_PATCH");
1192 if(this->VersionCount >= 2)
1194 char buf[64];
1195 sprintf(buf, "%u", this->VersionMinor);
1196 this->Makefile->AddDefinition("PACKAGE_FIND_VERSION_MINOR", buf);
1198 else
1200 this->Makefile->RemoveDefinition("PACKAGE_FIND_VERSION_MINOR");
1202 if(this->VersionCount >= 1)
1204 char buf[64];
1205 sprintf(buf, "%u", this->VersionMajor);
1206 this->Makefile->AddDefinition("PACKAGE_FIND_VERSION_MAJOR", buf);
1208 else
1210 this->Makefile->RemoveDefinition("PACKAGE_FIND_VERSION_MAJOR");
1213 // Load the version check file.
1214 bool found = false;
1215 if(this->ReadListFile(version_file.c_str()))
1217 // Check the output variables.
1218 found = this->Makefile->IsOn("PACKAGE_VERSION_EXACT");
1219 if(!found && !this->VersionExact)
1221 found = this->Makefile->IsOn("PACKAGE_VERSION_COMPATIBLE");
1223 if(found || this->Version.empty())
1225 // Get the version found.
1226 this->VersionFound =
1227 this->Makefile->GetSafeDefinition("PACKAGE_VERSION");
1229 // Try to parse the version number and store the results that were
1230 // successfully parsed.
1231 unsigned int parsed_major;
1232 unsigned int parsed_minor;
1233 unsigned int parsed_patch;
1234 this->VersionFoundCount =
1235 sscanf(this->VersionFound.c_str(), "%u.%u.%u",
1236 &parsed_major, &parsed_minor, &parsed_patch);
1237 switch(this->VersionFoundCount)
1239 case 3: this->VersionFoundPatch = parsed_patch; // no break!
1240 case 2: this->VersionFoundMinor = parsed_minor; // no break!
1241 case 1: this->VersionFoundMajor = parsed_major; // no break!
1242 default: break;
1247 // Restore the original scope.
1248 this->Makefile->PopScope();
1250 // Succeed if the version was found or no version was requested.
1251 return found || this->Version.empty();
1254 //----------------------------------------------------------------------------
1255 void cmFindPackageCommand::StoreVersionFound()
1257 // Store the whole version string.
1258 std::string ver = this->Name;
1259 ver += "_VERSION";
1260 if(this->VersionFound.empty())
1262 this->Makefile->RemoveDefinition(ver.c_str());
1264 else
1266 this->Makefile->AddDefinition(ver.c_str(), this->VersionFound.c_str());
1269 // Store the portions that could be parsed.
1270 char buf[64];
1271 switch(this->VersionFoundCount)
1273 case 3:
1275 sprintf(buf, "%u", this->VersionFoundPatch);
1276 this->Makefile->AddDefinition((ver+"_PATCH").c_str(), buf);
1277 } // no break
1278 case 2:
1280 sprintf(buf, "%u", this->VersionFoundMinor);
1281 this->Makefile->AddDefinition((ver+"_MINOR").c_str(), buf);
1282 } // no break
1283 case 1:
1285 sprintf(buf, "%u", this->VersionFoundMajor);
1286 this->Makefile->AddDefinition((ver+"_MAJOR").c_str(), buf);
1287 } // no break
1288 default: break;
1292 //----------------------------------------------------------------------------
1293 #include <cmsys/Directory.hxx>
1294 #include <cmsys/Glob.hxx>
1295 #include <cmsys/String.h>
1296 #include <cmsys/auto_ptr.hxx>
1298 class cmFileList;
1299 class cmFileListGeneratorBase
1301 public:
1302 virtual ~cmFileListGeneratorBase() {}
1303 protected:
1304 bool Consider(std::string const& fullPath, cmFileList& listing);
1305 private:
1306 bool Search(cmFileList&);
1307 virtual bool Search(std::string const& parent, cmFileList&) = 0;
1308 virtual cmsys::auto_ptr<cmFileListGeneratorBase> Clone() const = 0;
1309 friend class cmFileList;
1310 cmFileListGeneratorBase* SetNext(cmFileListGeneratorBase const& next);
1311 cmsys::auto_ptr<cmFileListGeneratorBase> Next;
1314 class cmFileList
1316 public:
1317 cmFileList(): First(), Last(0) {}
1318 virtual ~cmFileList() {}
1319 cmFileList& operator/(cmFileListGeneratorBase const& rhs)
1321 if(this->Last)
1323 this->Last = this->Last->SetNext(rhs);
1325 else
1327 this->First = rhs.Clone();
1328 this->Last = this->First.get();
1330 return *this;
1332 bool Search()
1334 if(this->First.get())
1336 return this->First->Search(*this);
1338 return false;
1340 private:
1341 virtual bool Visit(std::string const& fullPath) = 0;
1342 friend class cmFileListGeneratorBase;
1343 cmsys::auto_ptr<cmFileListGeneratorBase> First;
1344 cmFileListGeneratorBase* Last;
1347 class cmFindPackageFileList: public cmFileList
1349 public:
1350 cmFindPackageFileList(cmFindPackageCommand* fpc,
1351 bool use_suffixes = true):
1352 cmFileList(), FPC(fpc), UseSuffixes(use_suffixes) {}
1353 private:
1354 bool Visit(std::string const& fullPath)
1356 if(this->UseSuffixes)
1358 return this->FPC->SearchDirectory(fullPath);
1360 else
1362 return this->FPC->CheckDirectory(fullPath);
1365 cmFindPackageCommand* FPC;
1366 bool UseSuffixes;
1369 bool cmFileListGeneratorBase::Search(cmFileList& listing)
1371 return this->Search("", listing);
1374 cmFileListGeneratorBase*
1375 cmFileListGeneratorBase::SetNext(cmFileListGeneratorBase const& next)
1377 this->Next = next.Clone();
1378 return this->Next.get();
1381 bool cmFileListGeneratorBase::Consider(std::string const& fullPath,
1382 cmFileList& listing)
1384 if(this->Next.get())
1386 return this->Next->Search(fullPath + "/", listing);
1388 else
1390 return listing.Visit(fullPath + "/");
1394 class cmFileListGeneratorFixed: public cmFileListGeneratorBase
1396 public:
1397 cmFileListGeneratorFixed(std::string const& str):
1398 cmFileListGeneratorBase(), String(str) {}
1399 cmFileListGeneratorFixed(cmFileListGeneratorFixed const& r):
1400 cmFileListGeneratorBase(), String(r.String) {}
1401 private:
1402 std::string String;
1403 virtual bool Search(std::string const& parent, cmFileList& lister)
1405 std::string fullPath = parent + this->String;
1406 return this->Consider(fullPath, lister);
1408 virtual cmsys::auto_ptr<cmFileListGeneratorBase> Clone() const
1410 cmsys::auto_ptr<cmFileListGeneratorBase>
1411 g(new cmFileListGeneratorFixed(*this));
1412 return g;
1416 class cmFileListGeneratorEnumerate: public cmFileListGeneratorBase
1418 public:
1419 cmFileListGeneratorEnumerate(const char* p1, const char* p2):
1420 cmFileListGeneratorBase()
1422 this->Vector.push_back(p1);
1423 this->Vector.push_back(p2);
1425 cmFileListGeneratorEnumerate(cmFileListGeneratorEnumerate const& r):
1426 cmFileListGeneratorBase(), Vector(r.Vector) {}
1427 private:
1428 std::vector<std::string> Vector;
1429 virtual bool Search(std::string const& parent, cmFileList& lister)
1431 for(std::vector<std::string>::const_iterator i = this->Vector.begin();
1432 i != this->Vector.end(); ++i)
1434 if(this->Consider(parent + *i, lister))
1436 return true;
1439 return false;
1441 virtual cmsys::auto_ptr<cmFileListGeneratorBase> Clone() const
1443 cmsys::auto_ptr<cmFileListGeneratorBase>
1444 g(new cmFileListGeneratorEnumerate(*this));
1445 return g;
1449 class cmFileListGeneratorProject: public cmFileListGeneratorBase
1451 public:
1452 cmFileListGeneratorProject(std::vector<std::string> const& names):
1453 cmFileListGeneratorBase(), Names(names) {}
1454 cmFileListGeneratorProject(cmFileListGeneratorProject const& r):
1455 cmFileListGeneratorBase(), Names(r.Names) {}
1456 private:
1457 std::vector<std::string> const& Names;
1458 virtual bool Search(std::string const& parent, cmFileList& lister)
1460 // Construct a list of matches.
1461 std::vector<std::string> matches;
1462 cmsys::Directory d;
1463 d.Load(parent.c_str());
1464 for(unsigned long i=0; i < d.GetNumberOfFiles(); ++i)
1466 const char* fname = d.GetFile(i);
1467 if(strcmp(fname, ".") == 0 ||
1468 strcmp(fname, "..") == 0)
1470 continue;
1472 for(std::vector<std::string>::const_iterator ni = this->Names.begin();
1473 ni != this->Names.end(); ++ni)
1475 if(cmsysString_strncasecmp(fname, ni->c_str(),
1476 ni->length()) == 0)
1478 matches.push_back(fname);
1483 for(std::vector<std::string>::const_iterator i = matches.begin();
1484 i != matches.end(); ++i)
1486 if(this->Consider(parent + *i, lister))
1488 return true;
1491 return false;
1493 virtual cmsys::auto_ptr<cmFileListGeneratorBase> Clone() const
1495 cmsys::auto_ptr<cmFileListGeneratorBase>
1496 g(new cmFileListGeneratorProject(*this));
1497 return g;
1501 class cmFileListGeneratorMacProject: public cmFileListGeneratorBase
1503 public:
1504 cmFileListGeneratorMacProject(std::vector<std::string> const& names,
1505 const char* ext):
1506 cmFileListGeneratorBase(), Names(names), Extension(ext) {}
1507 cmFileListGeneratorMacProject(cmFileListGeneratorMacProject const& r):
1508 cmFileListGeneratorBase(), Names(r.Names), Extension(r.Extension) {}
1509 private:
1510 std::vector<std::string> const& Names;
1511 std::string Extension;
1512 virtual bool Search(std::string const& parent, cmFileList& lister)
1514 // Construct a list of matches.
1515 std::vector<std::string> matches;
1516 cmsys::Directory d;
1517 d.Load(parent.c_str());
1518 for(unsigned long i=0; i < d.GetNumberOfFiles(); ++i)
1520 const char* fname = d.GetFile(i);
1521 if(strcmp(fname, ".") == 0 ||
1522 strcmp(fname, "..") == 0)
1524 continue;
1526 for(std::vector<std::string>::const_iterator ni = this->Names.begin();
1527 ni != this->Names.end(); ++ni)
1529 std::string name = *ni;
1530 name += this->Extension;
1531 if(cmsysString_strcasecmp(fname, name.c_str()) == 0)
1533 matches.push_back(fname);
1538 for(std::vector<std::string>::const_iterator i = matches.begin();
1539 i != matches.end(); ++i)
1541 if(this->Consider(parent + *i, lister))
1543 return true;
1546 return false;
1548 virtual cmsys::auto_ptr<cmFileListGeneratorBase> Clone() const
1550 cmsys::auto_ptr<cmFileListGeneratorBase>
1551 g(new cmFileListGeneratorMacProject(*this));
1552 return g;
1556 class cmFileListGeneratorCaseInsensitive: public cmFileListGeneratorBase
1558 public:
1559 cmFileListGeneratorCaseInsensitive(std::string const& str):
1560 cmFileListGeneratorBase(), String(str) {}
1561 cmFileListGeneratorCaseInsensitive(
1562 cmFileListGeneratorCaseInsensitive const& r):
1563 cmFileListGeneratorBase(), String(r.String) {}
1564 private:
1565 std::string String;
1566 virtual bool Search(std::string const& parent, cmFileList& lister)
1568 // Look for matching files.
1569 std::vector<std::string> matches;
1570 cmsys::Directory d;
1571 d.Load(parent.c_str());
1572 for(unsigned long i=0; i < d.GetNumberOfFiles(); ++i)
1574 const char* fname = d.GetFile(i);
1575 if(strcmp(fname, ".") == 0 ||
1576 strcmp(fname, "..") == 0)
1578 continue;
1580 if(cmsysString_strcasecmp(fname, this->String.c_str()) == 0)
1582 if(this->Consider(parent + fname, lister))
1584 return true;
1588 return false;
1590 virtual cmsys::auto_ptr<cmFileListGeneratorBase> Clone() const
1592 cmsys::auto_ptr<cmFileListGeneratorBase>
1593 g(new cmFileListGeneratorCaseInsensitive(*this));
1594 return g;
1598 class cmFileListGeneratorGlob: public cmFileListGeneratorBase
1600 public:
1601 cmFileListGeneratorGlob(std::string const& str):
1602 cmFileListGeneratorBase(), Pattern(str) {}
1603 cmFileListGeneratorGlob(cmFileListGeneratorGlob const& r):
1604 cmFileListGeneratorBase(), Pattern(r.Pattern) {}
1605 private:
1606 std::string Pattern;
1607 virtual bool Search(std::string const& parent, cmFileList& lister)
1609 // Glob the set of matching files.
1610 std::string expr = parent;
1611 expr += this->Pattern;
1612 cmsys::Glob g;
1613 if(!g.FindFiles(expr))
1615 return false;
1617 std::vector<std::string> const& files = g.GetFiles();
1619 // Look for directories among the matches.
1620 for(std::vector<std::string>::const_iterator fi = files.begin();
1621 fi != files.end(); ++fi)
1623 if(cmSystemTools::FileIsDirectory(fi->c_str()))
1625 if(this->Consider(*fi, lister))
1627 return true;
1631 return false;
1633 virtual cmsys::auto_ptr<cmFileListGeneratorBase> Clone() const
1635 cmsys::auto_ptr<cmFileListGeneratorBase>
1636 g(new cmFileListGeneratorGlob(*this));
1637 return g;
1641 //----------------------------------------------------------------------------
1642 bool cmFindPackageCommand::SearchPrefix(std::string const& prefix_in)
1644 assert(!prefix_in.empty() && prefix_in[prefix_in.size()-1] == '/');
1645 if(this->DebugMode)
1647 fprintf(stderr, "Checking prefix [%s]\n", prefix_in.c_str());
1650 // Skip this if the prefix does not exist.
1651 if(!cmSystemTools::FileIsDirectory(prefix_in.c_str()))
1653 return false;
1656 // PREFIX/ (useful on windows or in build trees)
1657 if(this->SearchDirectory(prefix_in))
1659 return true;
1662 // Strip the trailing slash because the path generator is about to
1663 // add one.
1664 std::string prefix = prefix_in.substr(0, prefix_in.size()-1);
1666 // PREFIX/(cmake|CMake)/ (useful on windows or in build trees)
1668 cmFindPackageFileList lister(this);
1669 lister
1670 / cmFileListGeneratorFixed(prefix)
1671 / cmFileListGeneratorCaseInsensitive("cmake");
1672 if(lister.Search())
1674 return true;
1678 // PREFIX/(share|lib)/(Foo|foo|FOO).*/
1680 cmFindPackageFileList lister(this);
1681 lister
1682 / cmFileListGeneratorFixed(prefix)
1683 / cmFileListGeneratorEnumerate("lib", "share")
1684 / cmFileListGeneratorProject(this->Names);
1685 if(lister.Search())
1687 return true;
1691 // PREFIX/(share|lib)/(Foo|foo|FOO).*/(cmake|CMake)/
1693 cmFindPackageFileList lister(this);
1694 lister
1695 / cmFileListGeneratorFixed(prefix)
1696 / cmFileListGeneratorEnumerate("lib", "share")
1697 / cmFileListGeneratorProject(this->Names)
1698 / cmFileListGeneratorCaseInsensitive("cmake");
1699 if(lister.Search())
1701 return true;
1705 return false;
1708 //----------------------------------------------------------------------------
1709 bool cmFindPackageCommand::SearchFrameworkPrefix(std::string const& prefix_in)
1711 assert(!prefix_in.empty() && prefix_in[prefix_in.size()-1] == '/');
1712 if(this->DebugMode)
1714 fprintf(stderr, "Checking framework prefix [%s]\n", prefix_in.c_str());
1717 // Strip the trailing slash because the path generator is about to
1718 // add one.
1719 std::string prefix = prefix_in.substr(0, prefix_in.size()-1);
1721 // <prefix>/Foo.framework/Resources/
1723 cmFindPackageFileList lister(this);
1724 lister
1725 / cmFileListGeneratorFixed(prefix)
1726 / cmFileListGeneratorMacProject(this->Names, ".framework")
1727 / cmFileListGeneratorFixed("Resources");
1728 if(lister.Search())
1730 return true;
1733 // <prefix>/Foo.framework/Resources/CMake/
1735 cmFindPackageFileList lister(this);
1736 lister
1737 / cmFileListGeneratorFixed(prefix)
1738 / cmFileListGeneratorMacProject(this->Names, ".framework")
1739 / cmFileListGeneratorFixed("Resources")
1740 / cmFileListGeneratorCaseInsensitive("cmake");
1741 if(lister.Search())
1743 return true;
1747 // <prefix>/Foo.framework/Versions/*/Resources/
1749 cmFindPackageFileList lister(this);
1750 lister
1751 / cmFileListGeneratorFixed(prefix)
1752 / cmFileListGeneratorMacProject(this->Names, ".framework")
1753 / cmFileListGeneratorFixed("Versions")
1754 / cmFileListGeneratorGlob("*/Resources");
1755 if(lister.Search())
1757 return true;
1761 // <prefix>/Foo.framework/Versions/*/Resources/CMake/
1763 cmFindPackageFileList lister(this);
1764 lister
1765 / cmFileListGeneratorFixed(prefix)
1766 / cmFileListGeneratorMacProject(this->Names, ".framework")
1767 / cmFileListGeneratorFixed("Versions")
1768 / cmFileListGeneratorGlob("*/Resources")
1769 / cmFileListGeneratorCaseInsensitive("cmake");
1770 if(lister.Search())
1772 return true;
1776 return false;
1779 //----------------------------------------------------------------------------
1780 bool cmFindPackageCommand::SearchAppBundlePrefix(std::string const& prefix_in)
1782 assert(!prefix_in.empty() && prefix_in[prefix_in.size()-1] == '/');
1783 if(this->DebugMode)
1785 fprintf(stderr, "Checking bundle prefix [%s]\n", prefix_in.c_str());
1788 // Strip the trailing slash because the path generator is about to
1789 // add one.
1790 std::string prefix = prefix_in.substr(0, prefix_in.size()-1);
1792 // <prefix>/Foo.app/Contents/Resources
1794 cmFindPackageFileList lister(this);
1795 lister
1796 / cmFileListGeneratorFixed(prefix)
1797 / cmFileListGeneratorMacProject(this->Names, ".app")
1798 / cmFileListGeneratorFixed("Contents/Resources");
1799 if(lister.Search())
1801 return true;
1805 // <prefix>/Foo.app/Contents/Resources/CMake
1807 cmFindPackageFileList lister(this);
1808 lister
1809 / cmFileListGeneratorFixed(prefix)
1810 / cmFileListGeneratorMacProject(this->Names, ".app")
1811 / cmFileListGeneratorFixed("Contents/Resources")
1812 / cmFileListGeneratorCaseInsensitive("cmake");
1813 if(lister.Search())
1815 return true;
1819 return false;
1822 // TODO: Version numbers? Perhaps have a listing component class that
1823 // sorts by lexicographic and numerical ordering. Also try to match
1824 // some command argument for the version. Alternatively provide an
1825 // API that just returns a list of valid directories? Perhaps push a
1826 // scope and try loading the target file just to get its version
1827 // number? Could add a foo-version.cmake or FooVersion.cmake file
1828 // in the projects that contains just version information.
1830 // TODO: Debug cmsys::Glob double slash problem.
1832 // TODO: Add registry entries after cmake system search path?
1833 // Currently the user must specify them with the PATHS option.
1835 // [HKEY_CURRENT_USER\Software\*\Foo*;InstallDir]
1836 // [HKEY_CURRENT_USER\Software\*\*\Foo*;InstallDir]
1837 // [HKEY_LOCAL_MACHINE\Software\*\Foo*;InstallDir]
1838 // [HKEY_LOCAL_MACHINE\Software\*\*\Foo*;InstallDir]