Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Source / cmFindBase.cxx
blobb80f552d30538b06581e215fc77e04c252f4b003
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmFindBase.cxx,v $
5 Language: C++
6 Date: $Date: 2008-10-14 12:43:19 $
7 Version: $Revision: 1.51 $
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 "cmFindBase.h"
19 cmFindBase::cmFindBase()
21 cmSystemTools::ReplaceString(this->GenericDocumentationPathsOrder,
22 "FIND_ARGS_XXX", "<VAR> NAMES name");
23 this->AlreadyInCache = false;
24 this->AlreadyInCacheWithoutMetaInfo = false;
25 this->GenericDocumentation =
26 " FIND_XXX(<VAR> name1 [path1 path2 ...])\n"
27 "This is the short-hand signature for the command that "
28 "is sufficient in many cases. It is the same "
29 "as FIND_XXX(<VAR> name1 [PATHS path1 path2 ...])\n"
30 " FIND_XXX(\n"
31 " <VAR>\n"
32 " name | NAMES name1 [name2 ...]\n"
33 " [HINTS path1 [path2 ... ENV var]]\n"
34 " [PATHS path1 [path2 ... ENV var]]\n"
35 " [PATH_SUFFIXES suffix1 [suffix2 ...]]\n"
36 " [DOC \"cache documentation string\"]\n"
37 " [NO_DEFAULT_PATH]\n"
38 " [NO_CMAKE_ENVIRONMENT_PATH]\n"
39 " [NO_CMAKE_PATH]\n"
40 " [NO_SYSTEM_ENVIRONMENT_PATH]\n"
41 " [NO_CMAKE_SYSTEM_PATH]\n"
42 " [CMAKE_FIND_ROOT_PATH_BOTH |\n"
43 " ONLY_CMAKE_FIND_ROOT_PATH |\n"
44 " NO_CMAKE_FIND_ROOT_PATH]\n"
45 " )\n"
47 "This command is used to find a SEARCH_XXX_DESC. "
48 "A cache entry named by <VAR> is created to store the result "
49 "of this command. "
50 "If the SEARCH_XXX is found the result is stored in the variable "
51 "and the search will not be repeated unless the variable is cleared. "
52 "If nothing is found, the result will be "
53 "<VAR>-NOTFOUND, and the search will be attempted again the "
54 "next time FIND_XXX is invoked with the same variable. "
55 "The name of the SEARCH_XXX that "
56 "is searched for is specified by the names listed "
57 "after the NAMES argument. Additional search locations "
58 "can be specified after the PATHS argument. If ENV var is "
59 "found in the HINTS or PATHS section the environment variable var "
60 "will be read and converted from a system environment variable to "
61 "a cmake style list of paths. For example ENV PATH would be a way "
62 "to list the system path variable. The argument "
63 "after DOC will be used for the documentation string in "
64 "the cache. "
65 "PATH_SUFFIXES specifies additional subdirectories to check below "
66 "each search path."
67 "\n"
68 "If NO_DEFAULT_PATH is specified, then no additional paths are "
69 "added to the search. "
70 "If NO_DEFAULT_PATH is not specified, the search process is as follows:\n"
71 "1. Search paths specified in cmake-specific cache variables. "
72 "These are intended to be used on the command line with a -DVAR=value. "
73 "This can be skipped if NO_CMAKE_PATH is passed.\n"
74 " <prefix>/XXX_SUBDIR for each <prefix> in CMAKE_PREFIX_PATH\n"
75 " CMAKE_XXX_PATH\n"
76 " CMAKE_XXX_MAC_PATH\n"
77 "2. Search paths specified in cmake-specific environment variables. "
78 "These are intended to be set in the user's shell configuration. "
79 "This can be skipped if NO_CMAKE_ENVIRONMENT_PATH is passed.\n"
80 " <prefix>/XXX_SUBDIR for each <prefix> in CMAKE_PREFIX_PATH\n"
81 " CMAKE_XXX_PATH\n"
82 " CMAKE_XXX_MAC_PATH\n"
83 "3. Search the paths specified by the HINTS option. "
84 "These should be paths computed by system introspection, such as a "
85 "hint provided by the location of another item already found. "
86 "Hard-coded guesses should be specified with the PATHS option.\n"
87 "4. Search the standard system environment variables. "
88 "This can be skipped if NO_SYSTEM_ENVIRONMENT_PATH is an argument.\n"
89 " PATH\n"
90 " XXX_SYSTEM\n" // replace with "", LIB, or INCLUDE
91 "5. Search cmake variables defined in the Platform files "
92 "for the current system. This can be skipped if NO_CMAKE_SYSTEM_PATH "
93 "is passed.\n"
94 " <prefix>/XXX_SUBDIR for each <prefix> in CMAKE_SYSTEM_PREFIX_PATH\n"
95 " CMAKE_SYSTEM_XXX_PATH\n"
96 " CMAKE_SYSTEM_XXX_MAC_PATH\n"
97 "6. Search the paths specified by the PATHS option "
98 "or in the short-hand version of the command. "
99 "These are typically hard-coded guesses.\n"
101 this->GenericDocumentation += this->GenericDocumentationMacPolicy;
102 this->GenericDocumentation += this->GenericDocumentationRootPath;
103 this->GenericDocumentation += this->GenericDocumentationPathsOrder;
106 bool cmFindBase::ParseArguments(std::vector<std::string> const& argsIn)
108 if(argsIn.size() < 2 )
110 this->SetError("called with incorrect number of arguments");
111 return false;
114 // CMake versions below 2.3 did not search all these extra
115 // locations. Preserve compatibility unless a modern argument is
116 // passed.
117 bool compatibility = this->Makefile->NeedBackwardsCompatibility(2,3);
119 // copy argsIn into args so it can be modified,
120 // in the process extract the DOC "documentation"
121 size_t size = argsIn.size();
122 std::vector<std::string> args;
123 bool foundDoc = false;
124 for(unsigned int j = 0; j < size; ++j)
126 if(foundDoc || argsIn[j] != "DOC" )
128 if(argsIn[j] == "ENV")
130 if(j+1 < size)
132 j++;
133 cmSystemTools::GetPath(args, argsIn[j].c_str());
136 else
138 args.push_back(argsIn[j]);
141 else
143 if(j+1 < size)
145 foundDoc = true;
146 this->VariableDocumentation = argsIn[j+1];
147 j++;
148 if(j >= size)
150 break;
155 this->VariableName = args[0];
156 if(this->CheckForVariableInCache())
158 this->AlreadyInCache = true;
159 return true;
161 this->AlreadyInCache = false;
163 // Find the current root path mode.
164 this->SelectDefaultRootPathMode();
166 // Find the current bundle/framework search policy.
167 this->SelectDefaultMacMode();
169 bool newStyle = false;
170 enum Doing { DoingNone, DoingNames, DoingPaths, DoingPathSuffixes,
171 DoingHints };
172 Doing doing = DoingNames; // assume it starts with a name
173 for (unsigned int j = 1; j < args.size(); ++j)
175 if(args[j] == "NAMES")
177 doing = DoingNames;
178 newStyle = true;
180 else if (args[j] == "PATHS")
182 doing = DoingPaths;
183 newStyle = true;
185 else if (args[j] == "HINTS")
187 doing = DoingHints;
188 newStyle = true;
190 else if (args[j] == "PATH_SUFFIXES")
192 doing = DoingPathSuffixes;
193 compatibility = false;
194 newStyle = true;
196 else if (args[j] == "NO_SYSTEM_PATH")
198 doing = DoingNone;
199 this->NoDefaultPath = true;
201 else if (this->CheckCommonArgument(args[j]))
203 doing = DoingNone;
204 compatibility = false;
205 // Some common arguments were accidentally supported by CMake
206 // 2.4 and 2.6.0 in the short-hand form of the command, so we
207 // must support it even though it is not documented.
209 else if(doing == DoingNames)
211 this->Names.push_back(args[j]);
213 else if(doing == DoingPaths)
215 this->AddUserPath(args[j], this->UserPaths);
217 else if(doing == DoingHints)
219 this->AddUserPath(args[j], this->UserHints);
221 else if(doing == DoingPathSuffixes)
223 this->AddPathSuffix(args[j]);
227 // Now that arguments have been parsed check the compatibility
228 // setting. If we need to be compatible with CMake 2.2 and earlier
229 // do not add the CMake system paths. It is safe to add the CMake
230 // environment paths and system environment paths because that
231 // existed in 2.2. It is safe to add the CMake user variable paths
232 // because the user or project has explicitly set them.
233 if(compatibility)
235 this->NoCMakeSystemPath = true;
238 if(this->VariableDocumentation.size() == 0)
240 this->VariableDocumentation = "Where can ";
241 if(this->Names.size() == 0)
243 this->VariableDocumentation += "the (unknown) library be found";
245 else if(this->Names.size() == 1)
247 this->VariableDocumentation += "the "
248 + this->Names[0] + " library be found";
250 else
252 this->VariableDocumentation += "one of the " + this->Names[0];
253 for (unsigned int j = 1; j < this->Names.size() - 1; ++j)
255 this->VariableDocumentation += ", " + this->Names[j];
257 this->VariableDocumentation += " or "
258 + this->Names[this->Names.size() - 1] + " libraries be found";
262 // look for old style
263 // FIND_*(VAR name path1 path2 ...)
264 if(!newStyle)
266 // All the short-hand arguments have been recorded as names.
267 std::vector<std::string> shortArgs = this->Names;
268 this->Names.clear(); // clear out any values in Names
269 this->Names.push_back(shortArgs[0]);
270 for(unsigned int j = 1; j < shortArgs.size(); ++j)
272 this->AddUserPath(shortArgs[j], this->UserPaths);
275 this->ExpandPaths();
277 // Handle search root stuff.
278 this->RerootPaths(this->SearchPaths);
280 // Add a trailing slash to all prefixes to aid the search process.
281 this->AddTrailingSlashes(this->SearchPaths);
283 return true;
286 void cmFindBase::ExpandPaths()
288 this->AddCMakeVariablePath();
289 this->AddCMakeEnvironmentPath();
290 this->AddUserHintsPath();
291 this->AddSystemEnvironmentPath();
292 this->AddCMakeSystemVariablePath();
293 this->AddUserGuessPath();
295 // Add suffixes and clean up paths.
296 this->AddPathSuffixes();
299 //----------------------------------------------------------------------------
300 void cmFindBase::AddPrefixPaths(std::vector<std::string> const& in_paths,
301 PathType pathType)
303 // default for programs
304 std::string subdir = "bin";
306 if (this->CMakePathName == "INCLUDE")
308 subdir = "include";
310 else if (this->CMakePathName == "LIBRARY")
312 subdir = "lib";
314 else if (this->CMakePathName == "FRAMEWORK")
316 subdir = ""; // ? what to do for frameworks ?
319 for(std::vector<std::string>::const_iterator it = in_paths.begin();
320 it != in_paths.end(); ++it)
322 std::string dir = it->c_str();
323 if(!subdir.empty() && !dir.empty() && dir[dir.size()-1] != '/')
325 dir += "/";
327 std::string add = dir + subdir;
328 if(add != "/")
330 this->AddPathInternal(add, pathType);
332 if (subdir == "bin")
334 this->AddPathInternal(dir+"sbin", pathType);
336 if(!subdir.empty() && *it != "/")
338 this->AddPathInternal(*it, pathType);
343 //----------------------------------------------------------------------------
344 void cmFindBase::AddCMakePrefixPath(const char* variable)
346 // Get a path from a CMake variable.
347 if(const char* varPath = this->Makefile->GetDefinition(variable))
349 std::vector<std::string> tmp;
350 cmSystemTools::ExpandListArgument(varPath, tmp);
351 this->AddPrefixPaths(tmp, CMakePath);
355 //----------------------------------------------------------------------------
356 void cmFindBase::AddEnvPrefixPath(const char* variable)
358 // Get a path from the environment.
359 std::vector<std::string> tmp;
360 cmSystemTools::GetPath(tmp, variable);
361 this->AddPrefixPaths(tmp, EnvPath);
364 //----------------------------------------------------------------------------
365 void cmFindBase::AddCMakeEnvironmentPath()
367 if(!this->NoCMakeEnvironmentPath && !this->NoDefaultPath)
369 // Add CMAKE_*_PATH environment variables
370 std::string var = "CMAKE_";
371 var += this->CMakePathName;
372 var += "_PATH";
373 this->AddEnvPrefixPath("CMAKE_PREFIX_PATH");
374 this->AddEnvPath(var.c_str());
376 if(this->CMakePathName == "PROGRAM")
378 this->AddEnvPath("CMAKE_APPBUNDLE_PATH");
380 else
382 this->AddEnvPath("CMAKE_FRAMEWORK_PATH");
387 //----------------------------------------------------------------------------
388 void cmFindBase::AddCMakeVariablePath()
390 if(!this->NoCMakePath && !this->NoDefaultPath)
392 // Add CMake varibles of the same name as the previous environment
393 // varibles CMAKE_*_PATH to be used most of the time with -D
394 // command line options
395 std::string var = "CMAKE_";
396 var += this->CMakePathName;
397 var += "_PATH";
398 this->AddCMakePrefixPath("CMAKE_PREFIX_PATH");
399 this->AddCMakePath(var.c_str());
401 if(this->CMakePathName == "PROGRAM")
403 this->AddCMakePath("CMAKE_APPBUNDLE_PATH");
405 else
407 this->AddCMakePath("CMAKE_FRAMEWORK_PATH");
412 //----------------------------------------------------------------------------
413 void cmFindBase::AddSystemEnvironmentPath()
415 if(!this->NoSystemEnvironmentPath && !this->NoDefaultPath)
417 // Add LIB or INCLUDE
418 if(!this->EnvironmentPath.empty())
420 this->AddEnvPath(this->EnvironmentPath.c_str());
422 // Add PATH
423 this->AddEnvPath(0);
427 //----------------------------------------------------------------------------
428 void cmFindBase::AddCMakeSystemVariablePath()
430 if(!this->NoCMakeSystemPath && !this->NoDefaultPath)
432 std::string var = "CMAKE_SYSTEM_";
433 var += this->CMakePathName;
434 var += "_PATH";
435 this->AddCMakePrefixPath("CMAKE_SYSTEM_PREFIX_PATH");
436 this->AddCMakePath(var.c_str());
438 if(this->CMakePathName == "PROGRAM")
440 this->AddCMakePath("CMAKE_SYSTEM_APPBUNDLE_PATH");
442 else
444 this->AddCMakePath("CMAKE_SYSTEM_FRAMEWORK_PATH");
449 //----------------------------------------------------------------------------
450 void cmFindBase::AddUserHintsPath()
452 this->AddPathsInternal(this->UserHints, CMakePath);
455 //----------------------------------------------------------------------------
456 void cmFindBase::AddUserGuessPath()
458 this->AddPathsInternal(this->UserPaths, CMakePath);
461 //----------------------------------------------------------------------------
462 void cmFindBase::AddPathSuffixes()
464 std::vector<std::string>& paths = this->SearchPaths;
465 std::vector<std::string> finalPath = paths;
466 std::vector<std::string>::iterator i;
467 // clear the path
468 paths.clear();
469 // convert all paths to unix slashes and add search path suffixes
470 // if there are any
471 for(i = finalPath.begin();
472 i != finalPath.end(); ++i)
474 cmSystemTools::ConvertToUnixSlashes(*i);
475 // copy each finalPath combined with SearchPathSuffixes
476 // to the SearchPaths ivar
477 for(std::vector<std::string>::iterator j =
478 this->SearchPathSuffixes.begin();
479 j != this->SearchPathSuffixes.end(); ++j)
481 // if *i is only / then do not add a //
482 // this will get incorrectly considered a network
483 // path on windows and cause huge delays.
484 std::string p = *i;
485 if(p.size() && p[p.size()-1] != '/')
487 p += std::string("/");
489 p += *j;
490 // add to all paths because the search path may be modified
491 // later with lib being replaced for lib64 which may exist
492 paths.push_back(p);
494 // now put the path without the path suffixes in the SearchPaths
495 paths.push_back(*i);
499 void cmFindBase::PrintFindStuff()
501 std::cerr << "SearchFrameworkLast: " << this->SearchFrameworkLast << "\n";
502 std::cerr << "SearchFrameworkOnly: " << this->SearchFrameworkOnly << "\n";
503 std::cerr << "SearchFrameworkFirst: " << this->SearchFrameworkFirst << "\n";
504 std::cerr << "SearchAppBundleLast: " << this->SearchAppBundleLast << "\n";
505 std::cerr << "SearchAppBundleOnly: " << this->SearchAppBundleOnly << "\n";
506 std::cerr << "SearchAppBundleFirst: " << this->SearchAppBundleFirst << "\n";
507 std::cerr << "VariableName " << this->VariableName << "\n";
508 std::cerr << "VariableDocumentation "
509 << this->VariableDocumentation << "\n";
510 std::cerr << "NoDefaultPath " << this->NoDefaultPath << "\n";
511 std::cerr << "NoCMakeEnvironmentPath "
512 << this->NoCMakeEnvironmentPath << "\n";
513 std::cerr << "NoCMakePath " << this->NoCMakePath << "\n";
514 std::cerr << "NoSystemEnvironmentPath "
515 << this->NoSystemEnvironmentPath << "\n";
516 std::cerr << "NoCMakeSystemPath " << this->NoCMakeSystemPath << "\n";
517 std::cerr << "EnvironmentPath " << this->EnvironmentPath << "\n";
518 std::cerr << "CMakePathName " << this->CMakePathName << "\n";
519 std::cerr << "Names ";
520 for(unsigned int i =0; i < this->Names.size(); ++i)
522 std::cerr << this->Names[i] << " ";
524 std::cerr << "\n";
525 std::cerr << "\n";
526 std::cerr << "SearchPathSuffixes ";
527 for(unsigned int i =0; i < this->SearchPathSuffixes.size(); ++i)
529 std::cerr << this->SearchPathSuffixes[i] << "\n";
531 std::cerr << "\n";
532 std::cerr << "SearchPaths\n";
533 for(unsigned int i =0; i < this->SearchPaths.size(); ++i)
535 std::cerr << "[" << this->SearchPaths[i] << "]\n";
539 bool cmFindBase::CheckForVariableInCache()
541 if(const char* cacheValue =
542 this->Makefile->GetDefinition(this->VariableName.c_str()))
544 cmCacheManager::CacheIterator it =
545 this->Makefile->GetCacheManager()->
546 GetCacheIterator(this->VariableName.c_str());
547 bool found = !cmSystemTools::IsNOTFOUND(cacheValue);
548 bool cached = !it.IsAtEnd();
549 if(found)
551 // If the user specifies the entry on the command line without a
552 // type we should add the type and docstring but keep the
553 // original value. Tell the subclass implementations to do
554 // this.
555 if(cached && it.GetType() == cmCacheManager::UNINITIALIZED)
557 this->AlreadyInCacheWithoutMetaInfo = true;
559 return true;
561 else if(cached)
563 const char* hs = it.GetProperty("HELPSTRING");
564 this->VariableDocumentation = hs?hs:"(none)";
567 return false;