CVS resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Source / cmFindBase.cxx
blob83dd09ad4dec3bb0c423d82856121e977d46ad36
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmFindBase.cxx,v $
5 Language: C++
6 Date: $Date: 2008/01/17 22:49:30 $
7 Version: $Revision: 1.34 $
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 " [PATHS path1 [path2 ... ENV var]]\n"
34 " [PATH_SUFFIXES suffix1 [suffix2 ...]]\n"
35 " [DOC \"cache documentation string\"]\n"
36 " [NO_DEFAULT_PATH]\n"
37 " [NO_CMAKE_ENVIRONMENT_PATH]\n"
38 " [NO_CMAKE_PATH]\n"
39 " [NO_SYSTEM_ENVIRONMENT_PATH]\n"
40 " [NO_CMAKE_SYSTEM_PATH]\n"
41 " [CMAKE_FIND_ROOT_PATH_BOTH |\n"
42 " ONLY_CMAKE_FIND_ROOT_PATH |\n"
43 " NO_CMAKE_FIND_ROOT_PATH]\n"
44 " )\n"
46 "This command is used to find a SEARCH_XXX_DESC. "
47 "A cache entry named by <VAR> is created to store the result "
48 "of this command. "
49 "If the SEARCH_XXX is found the result is stored in the variable "
50 "and the search will not be repeated unless the variable is cleared. "
51 "If nothing is found, the result will be "
52 "<VAR>-NOTFOUND, and the search will be attempted again the "
53 "next time FIND_XXX is invoked with the same variable. "
54 "The name of the SEARCH_XXX that "
55 "is searched for is specified by the names listed "
56 "after the NAMES argument. Additional search locations "
57 "can be specified after the PATHS argument. If ENV var is "
58 "found in the PATHS section the environment variable var "
59 "will be read and converted from a system environment variable to "
60 "a cmake style list of paths. For example ENV PATH would be a way "
61 "to list the system path variable. The argument "
62 "after DOC will be used for the documentation string in "
63 "the cache. PATH_SUFFIXES can be used to give sub directories "
64 "that will be appended to the search paths.\n"
65 "If NO_DEFAULT_PATH is specified, then no additional paths are "
66 "added to the search. "
67 "If NO_DEFAULT_PATH is not specified, the search process is as follows:\n"
68 "1. Search cmake specific environment variables. This "
69 "can be skipped if NO_CMAKE_ENVIRONMENT_PATH is passed.\n"
71 " <prefix>/XXX_SUBDIR for each <prefix> in CMAKE_PREFIX_PATH\n"
72 " CMAKE_XXX_PATH\n"
73 " CMAKE_XXX_MAC_PATH\n"
74 "2. Search cmake variables with the same names as "
75 "the cmake specific environment variables. These "
76 "are intended to be used on the command line with a "
77 "-DVAR=value. This can be skipped if NO_CMAKE_PATH "
78 "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 standard system environment variables. "
84 "This can be skipped if NO_SYSTEM_ENVIRONMENT_PATH is an argument.\n"
85 " PATH\n"
86 " XXX_SYSTEM\n" // replace with "", LIB, or INCLUDE
87 "4. Search cmake variables defined in the Platform files "
88 "for the current system. This can be skipped if NO_CMAKE_SYSTEM_PATH "
89 "is passed.\n"
90 " <prefix>/XXX_SUBDIR for each <prefix> in CMAKE_SYSTEM_PREFIX_PATH\n"
91 " CMAKE_SYSTEM_XXX_PATH\n"
92 " CMAKE_SYSTEM_XXX_MAC_PATH\n"
93 "5. Search the paths specified after PATHS or in the short-hand version "
94 "of the command.\n"
96 this->GenericDocumentation += this->GenericDocumentationMacPolicy;
97 this->GenericDocumentation += this->GenericDocumentationRootPath;
98 this->GenericDocumentation += this->GenericDocumentationPathsOrder;
101 bool cmFindBase::ParseArguments(std::vector<std::string> const& argsIn)
103 if(argsIn.size() < 2 )
105 this->SetError("called with incorrect number of arguments");
106 return false;
109 // CMake versions below 2.3 did not search all these extra
110 // locations. Preserve compatibility unless a modern argument is
111 // passed.
112 bool compatibility = false;
113 const char* versionValue =
114 this->Makefile->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY");
115 int major = 0;
116 int minor = 0;
117 if(versionValue && sscanf(versionValue, "%d.%d", &major, &minor) != 2)
119 versionValue = 0;
121 if(versionValue && (major < 2 || major == 2 && minor < 3))
123 compatibility = true;
126 // copy argsIn into args so it can be modified,
127 // in the process extract the DOC "documentation"
128 size_t size = argsIn.size();
129 std::vector<std::string> args;
130 bool foundDoc = false;
131 for(unsigned int j = 0; j < size; ++j)
133 if(foundDoc || argsIn[j] != "DOC" )
135 if(argsIn[j] == "ENV")
137 if(j+1 < size)
139 j++;
140 cmSystemTools::GetPath(args, argsIn[j].c_str());
143 else
145 args.push_back(argsIn[j]);
148 else
150 if(j+1 < size)
152 foundDoc = true;
153 this->VariableDocumentation = argsIn[j+1];
154 j++;
155 if(j >= size)
157 break;
162 this->VariableName = args[0];
163 if(this->CheckForVariableInCache())
165 this->AlreadyInCache = true;
166 return true;
168 this->AlreadyInCache = false;
170 // Find the current root path mode.
171 this->SelectDefaultRootPathMode();
173 // Find the current bundle/framework search policy.
174 this->SelectDefaultMacMode();
176 std::vector<std::string> userPaths;
177 std::string doc;
178 bool doingNames = true; // assume it starts with a name
179 bool doingPaths = false;
180 bool doingPathSuf = false;
181 bool newStyle = false;
183 for (unsigned int j = 1; j < args.size(); ++j)
185 if(args[j] == "NAMES")
187 doingNames = true;
188 newStyle = true;
189 doingPathSuf = false;
190 doingPaths = false;
192 else if (args[j] == "PATHS")
194 doingPaths = true;
195 newStyle = true;
196 doingNames = false;
197 doingPathSuf = false;
199 else if (args[j] == "PATH_SUFFIXES")
201 compatibility = false;
202 doingPathSuf = true;
203 newStyle = true;
204 doingNames = false;
205 doingPaths = false;
207 else if (args[j] == "NO_SYSTEM_PATH")
209 doingPaths = false;
210 doingPathSuf = false;
211 doingNames = false;
212 this->NoDefaultPath = true;
214 else if (this->CheckCommonArgument(args[j]))
216 compatibility = false;
217 doingPaths = false;
218 doingPathSuf = false;
219 doingNames = false;
221 else
223 if(doingNames)
225 this->Names.push_back(args[j]);
227 else if(doingPaths)
229 userPaths.push_back(args[j]);
231 else if(doingPathSuf)
233 this->AddPathSuffix(args[j]);
238 // Now that arguments have been parsed check the compatibility
239 // setting. If we need to be compatible with CMake 2.2 and earlier
240 // do not add the CMake system paths. It is safe to add the CMake
241 // environment paths and system environment paths because that
242 // existed in 2.2. It is safe to add the CMake user variable paths
243 // because the user or project has explicitly set them.
244 if(compatibility)
246 this->NoCMakeSystemPath = true;
249 if(this->VariableDocumentation.size() == 0)
251 this->VariableDocumentation = "Where can ";
252 if(this->Names.size() == 0)
254 this->VariableDocumentation += "the (unknown) library be found";
256 else if(this->Names.size() == 1)
258 this->VariableDocumentation += "the "
259 + this->Names[0] + " library be found";
261 else
263 this->VariableDocumentation += "one of the " + this->Names[0];
264 for (unsigned int j = 1; j < this->Names.size() - 1; ++j)
266 this->VariableDocumentation += ", " + this->Names[j];
268 this->VariableDocumentation += " or "
269 + this->Names[this->Names.size() - 1] + " libraries be found";
273 // look for old style
274 // FIND_*(VAR name path1 path2 ...)
275 if(!newStyle)
277 this->Names.clear(); // clear out any values in Names
278 this->Names.push_back(args[1]);
279 for(unsigned int j = 2; j < args.size(); ++j)
281 userPaths.push_back(args[j]);
284 this->ExpandPaths(userPaths);
286 // Handle search root stuff.
287 this->RerootPaths(this->SearchPaths);
288 return true;
291 void cmFindBase::ExpandPaths(std::vector<std::string> userPaths)
293 // if NO Default paths was not specified add the
294 // standard search paths.
295 if(!this->NoDefaultPath)
297 if(this->SearchFrameworkFirst || this->SearchFrameworkOnly)
299 this->AddFrameWorkPaths();
301 if(this->SearchAppBundleFirst || this->SearchAppBundleOnly)
303 this->AddAppBundlePaths();
305 if(!this->NoCMakeEnvironmentPath &&
306 !(this->SearchFrameworkOnly || this->SearchAppBundleOnly))
308 // Add CMAKE_*_PATH environment variables
309 this->AddEnvironmentVariables();
311 if(!this->NoCMakePath &&
312 !(this->SearchFrameworkOnly || this->SearchAppBundleOnly))
314 // Add CMake varibles of the same name as the previous environment
315 // varibles CMAKE_*_PATH to be used most of the time with -D
316 // command line options
317 this->AddCMakeVariables();
319 if(!this->NoSystemEnvironmentPath &&
320 !(this->SearchFrameworkOnly || this->SearchAppBundleOnly))
322 // add System environment PATH and (LIB or INCLUDE)
323 this->AddSystemEnvironmentVariables();
325 if(!this->NoCMakeSystemPath &&
326 !(this->SearchFrameworkOnly || this->SearchAppBundleOnly))
328 // Add CMAKE_SYSTEM_*_PATH variables which are defined in platform files
329 this->AddCMakeSystemVariables();
331 if(this->SearchAppBundleLast)
333 this->AddAppBundlePaths();
335 if(this->SearchFrameworkLast)
337 this->AddFrameWorkPaths();
340 std::vector<std::string> paths;
341 // add the paths specified in the FIND_* call
342 for(unsigned int i =0; i < userPaths.size(); ++i)
344 paths.push_back(userPaths[i]);
346 this->AddPaths(paths);
349 //----------------------------------------------------------------------------
350 void cmFindBase::AddEnvironmentVariables()
352 std::vector<std::string> paths;
354 std::vector<std::string> prefixPaths;
355 cmSystemTools::GetPath(prefixPaths, "CMAKE_PREFIX_PATH");
356 this->AddFindPrefix(paths, prefixPaths);
358 std::string var = "CMAKE_";
359 var += this->CMakePathName;
360 var += "_PATH";
361 cmSystemTools::GetPath(paths, var.c_str());
362 this->AddPaths(paths);
365 void cmFindBase::AddFindPrefix(std::vector<std::string>& dest,
366 const std::vector<std::string>& src)
368 // default for programs
369 std::string subdir = "bin";
371 if (this->CMakePathName == "INCLUDE")
373 subdir = "include";
375 else if (this->CMakePathName == "LIBRARY")
377 subdir = "lib";
379 else if (this->CMakePathName == "FRAMEWORK")
381 subdir = ""; // ? what to do for frameworks ?
384 for (std::vector<std::string>::const_iterator it = src.begin();
385 it != src.end();
386 ++it)
388 std::string dir = it->c_str();
389 if(!subdir.empty() && !dir.empty() && dir[dir.size()-1] != '/')
391 dir += "/";
393 dest.push_back(dir + subdir);
394 if (subdir == "bin")
396 dest.push_back(dir + "sbin");
398 if(!subdir.empty())
400 dest.push_back(*it);
405 void cmFindBase::AddFrameWorkPaths()
407 std::vector<std::string> paths;
408 this->GetFrameworkPaths(paths);
409 this->AddPaths(paths);
412 void cmFindBase::AddPaths(std::vector<std::string> & paths)
414 // add suffixes and clean up paths
415 this->ExpandRegistryAndCleanPath(paths);
416 // add the paths to the search paths
417 this->SearchPaths.insert(this->SearchPaths.end(),
418 paths.begin(),
419 paths.end());
422 void cmFindBase::AddAppBundlePaths()
424 std::vector<std::string> paths;
425 this->GetAppBundlePaths(paths);
426 this->AddPaths(paths);
429 void cmFindBase::AddCMakeVariables()
431 std::string var = "CMAKE_";
432 var += this->CMakePathName;
433 var += "_PATH";
434 std::vector<std::string> paths;
436 if(const char* prefixPath =
437 this->Makefile->GetDefinition("CMAKE_PREFIX_PATH"))
439 std::vector<std::string> prefixPaths;
440 cmSystemTools::ExpandListArgument(prefixPath, prefixPaths);
441 this->AddFindPrefix(paths, prefixPaths);
444 if(const char* path = this->Makefile->GetDefinition(var.c_str()))
446 cmSystemTools::ExpandListArgument(path, paths);
448 this->AddPaths(paths);
451 void cmFindBase::AddSystemEnvironmentVariables()
453 // Add LIB or INCLUDE
454 std::vector<std::string> paths;
455 if(this->EnvironmentPath.size())
457 cmSystemTools::GetPath(paths, this->EnvironmentPath.c_str());
459 // Add PATH
460 cmSystemTools::GetPath(paths);
461 this->AddPaths(paths);
464 void cmFindBase::AddCMakeSystemVariables()
466 std::string var = "CMAKE_SYSTEM_";
467 var += this->CMakePathName;
468 var += "_PATH";
469 std::vector<std::string> paths;
470 if(const char* prefixPath =
471 this->Makefile->GetDefinition("CMAKE_SYSTEM_PREFIX_PATH"))
473 std::vector<std::string> prefixPaths;
474 cmSystemTools::ExpandListArgument(prefixPath, prefixPaths);
475 this->AddFindPrefix(paths, prefixPaths);
477 if(const char* path = this->Makefile->GetDefinition(var.c_str()))
479 cmSystemTools::ExpandListArgument(path, paths);
481 this->AddPaths(paths);
484 void cmFindBase::ExpandRegistryAndCleanPath(std::vector<std::string>& paths)
486 std::vector<std::string> finalPath;
487 std::vector<std::string>::iterator i;
488 // glob and expand registry stuff from paths and put
489 // into finalPath
490 for(i = paths.begin();
491 i != paths.end(); ++i)
493 cmSystemTools::ExpandRegistryValues(*i);
494 cmSystemTools::GlobDirs(i->c_str(), finalPath);
496 // clear the path
497 paths.clear();
498 // convert all paths to unix slashes and add search path suffixes
499 // if there are any
500 for(i = finalPath.begin();
501 i != finalPath.end(); ++i)
503 cmSystemTools::ConvertToUnixSlashes(*i);
504 // copy each finalPath combined with SearchPathSuffixes
505 // to the SearchPaths ivar
506 for(std::vector<std::string>::iterator j =
507 this->SearchPathSuffixes.begin();
508 j != this->SearchPathSuffixes.end(); ++j)
510 std::string p = *i + std::string("/") + *j;
511 // add to all paths because the search path may be modified
512 // later with lib being replaced for lib64 which may exist
513 paths.push_back(p);
516 // now put the path without the path suffixes in the SearchPaths
517 for(i = finalPath.begin();
518 i != finalPath.end(); ++i)
520 // put all search paths in because it may later be replaced
521 // by lib64 stuff fixes bug 4009
522 paths.push_back(*i);
526 void cmFindBase::PrintFindStuff()
528 std::cerr << "SearchFrameworkLast: " << this->SearchFrameworkLast << "\n";
529 std::cerr << "SearchFrameworkOnly: " << this->SearchFrameworkOnly << "\n";
530 std::cerr << "SearchFrameworkFirst: " << this->SearchFrameworkFirst << "\n";
531 std::cerr << "SearchAppBundleLast: " << this->SearchAppBundleLast << "\n";
532 std::cerr << "SearchAppBundleOnly: " << this->SearchAppBundleOnly << "\n";
533 std::cerr << "SearchAppBundleFirst: " << this->SearchAppBundleFirst << "\n";
534 std::cerr << "VariableName " << this->VariableName << "\n";
535 std::cerr << "VariableDocumentation "
536 << this->VariableDocumentation << "\n";
537 std::cerr << "NoDefaultPath " << this->NoDefaultPath << "\n";
538 std::cerr << "NoCMakeEnvironmentPath "
539 << this->NoCMakeEnvironmentPath << "\n";
540 std::cerr << "NoCMakePath " << this->NoCMakePath << "\n";
541 std::cerr << "NoSystemEnvironmentPath "
542 << this->NoSystemEnvironmentPath << "\n";
543 std::cerr << "NoCMakeSystemPath " << this->NoCMakeSystemPath << "\n";
544 std::cerr << "EnvironmentPath " << this->EnvironmentPath << "\n";
545 std::cerr << "CMakePathName " << this->CMakePathName << "\n";
546 std::cerr << "Names ";
547 for(unsigned int i =0; i < this->Names.size(); ++i)
549 std::cerr << this->Names[i] << " ";
551 std::cerr << "\n";
552 std::cerr << "\n";
553 std::cerr << "SearchPathSuffixes ";
554 for(unsigned int i =0; i < this->SearchPathSuffixes.size(); ++i)
556 std::cerr << this->SearchPathSuffixes[i] << "\n";
558 std::cerr << "\n";
559 std::cerr << "SearchPaths\n";
560 for(unsigned int i =0; i < this->SearchPaths.size(); ++i)
562 std::cerr << "[" << this->SearchPaths[i] << "]\n";
566 bool cmFindBase::CheckForVariableInCache()
568 if(const char* cacheValue =
569 this->Makefile->GetDefinition(this->VariableName.c_str()))
571 cmCacheManager::CacheIterator it =
572 this->Makefile->GetCacheManager()->
573 GetCacheIterator(this->VariableName.c_str());
574 bool found = !cmSystemTools::IsNOTFOUND(cacheValue);
575 bool cached = !it.IsAtEnd();
576 if(found)
578 // If the user specifies the entry on the command line without a
579 // type we should add the type and docstring but keep the
580 // original value. Tell the subclass implementations to do
581 // this.
582 if(cached && it.GetType() == cmCacheManager::UNINITIALIZED)
584 this->AlreadyInCacheWithoutMetaInfo = true;
586 return true;
588 else if(cached)
590 const char* hs = it.GetProperty("HELPSTRING");
591 this->VariableDocumentation = hs?hs:"(none)";
594 return false;