Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Source / cmFindCommon.cxx
blob1fbfb1846b44d7516c00f41a70b8a88931d00d19
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmFindCommon.cxx,v $
5 Language: C++
6 Date: $Date: 2008-10-03 12:16:37 $
7 Version: $Revision: 1.6 $
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 "cmFindCommon.h"
19 //----------------------------------------------------------------------------
20 cmFindCommon::cmFindCommon()
22 this->FindRootPathMode = RootPathModeBoth;
23 this->NoDefaultPath = false;
24 this->NoCMakePath = false;
25 this->NoCMakeEnvironmentPath = false;
26 this->NoSystemEnvironmentPath = false;
27 this->NoCMakeSystemPath = false;
29 // OS X Bundle and Framework search policy. The default is to
30 // search frameworks first on apple.
31 #if defined(__APPLE__)
32 this->SearchFrameworkFirst = true;
33 this->SearchAppBundleFirst = true;
34 #else
35 this->SearchFrameworkFirst = false;
36 this->SearchAppBundleFirst = false;
37 #endif
38 this->SearchFrameworkOnly = false;
39 this->SearchFrameworkLast = false;
40 this->SearchAppBundleOnly = false;
41 this->SearchAppBundleLast = false;
43 // Documentation components.
44 this->GenericDocumentationMacPolicy =
45 "On Darwin or systems supporting OS X Frameworks, the cmake variable"
46 " CMAKE_FIND_FRAMEWORK can be set to empty or one of the following:\n"
47 " \"FIRST\" - Try to find frameworks before standard\n"
48 " libraries or headers. This is the default on Darwin.\n"
49 " \"LAST\" - Try to find frameworks after standard\n"
50 " libraries or headers.\n"
51 " \"ONLY\" - Only try to find frameworks.\n"
52 " \"NEVER\". - Never try to find frameworks.\n"
53 "On Darwin or systems supporting OS X Application Bundles, the cmake "
54 "variable CMAKE_FIND_APPBUNDLE can be set to empty or one of the "
55 "following:\n"
56 " \"FIRST\" - Try to find application bundles before standard\n"
57 " programs. This is the default on Darwin.\n"
58 " \"LAST\" - Try to find application bundles after standard\n"
59 " programs.\n"
60 " \"ONLY\" - Only try to find application bundles.\n"
61 " \"NEVER\". - Never try to find application bundles.\n";
62 this->GenericDocumentationRootPath =
63 "The CMake variable CMAKE_FIND_ROOT_PATH specifies one or more "
64 "directories to be prepended to all other search directories. "
65 "This effectively \"re-roots\" the entire search under given locations. "
66 "By default it is empty. It is especially useful when "
67 "cross-compiling to point to the root directory of the "
68 "target environment and CMake will search there too. By default at first "
69 "the directories listed in CMAKE_FIND_ROOT_PATH and then the non-rooted "
70 "directories will be searched. "
71 "The default behavior can be adjusted by setting "
72 "CMAKE_FIND_ROOT_PATH_MODE_XXX. This behavior can be manually "
73 "overridden on a per-call basis. "
74 "By using CMAKE_FIND_ROOT_PATH_BOTH the search order will "
75 "be as described above. If NO_CMAKE_FIND_ROOT_PATH is used "
76 "then CMAKE_FIND_ROOT_PATH will not be used. If ONLY_CMAKE_FIND_ROOT_PATH "
77 "is used then only the re-rooted directories will be searched.\n";
78 this->GenericDocumentationPathsOrder =
79 "The default search order is designed to be most-specific to "
80 "least-specific for common use cases. "
81 "Projects may override the order by simply calling the command "
82 "multiple times and using the NO_* options:\n"
83 " FIND_XXX(FIND_ARGS_XXX PATHS paths... NO_DEFAULT_PATH)\n"
84 " FIND_XXX(FIND_ARGS_XXX)\n"
85 "Once one of the calls succeeds the result variable will be set "
86 "and stored in the cache so that no call will search again.";
89 //----------------------------------------------------------------------------
90 cmFindCommon::~cmFindCommon()
94 //----------------------------------------------------------------------------
95 void cmFindCommon::SelectDefaultRootPathMode()
97 // Use both by default.
98 this->FindRootPathMode = RootPathModeBoth;
100 // Check the policy variable for this find command type.
101 std::string findRootPathVar = "CMAKE_FIND_ROOT_PATH_MODE_";
102 findRootPathVar += this->CMakePathName;
103 std::string rootPathMode =
104 this->Makefile->GetSafeDefinition(findRootPathVar.c_str());
105 if (rootPathMode=="NEVER")
107 this->FindRootPathMode = RootPathModeNoRootPath;
109 else if (rootPathMode=="ONLY")
111 this->FindRootPathMode = RootPathModeOnlyRootPath;
113 else if (rootPathMode=="BOTH")
115 this->FindRootPathMode = RootPathModeBoth;
119 //----------------------------------------------------------------------------
120 void cmFindCommon::SelectDefaultMacMode()
122 std::string ff = this->Makefile->GetSafeDefinition("CMAKE_FIND_FRAMEWORK");
123 if(ff == "NEVER")
125 this->SearchFrameworkLast = false;
126 this->SearchFrameworkFirst = false;
127 this->SearchFrameworkOnly = false;
129 else if(ff == "ONLY")
131 this->SearchFrameworkLast = false;
132 this->SearchFrameworkFirst = false;
133 this->SearchFrameworkOnly = true;
135 else if(ff == "FIRST")
137 this->SearchFrameworkLast = false;
138 this->SearchFrameworkFirst = true;
139 this->SearchFrameworkOnly = false;
141 else if(ff == "LAST")
143 this->SearchFrameworkLast = true;
144 this->SearchFrameworkFirst = false;
145 this->SearchFrameworkOnly = false;
148 std::string fab = this->Makefile->GetSafeDefinition("CMAKE_FIND_APPBUNDLE");
149 if(fab == "NEVER")
151 this->SearchAppBundleLast = false;
152 this->SearchAppBundleFirst = false;
153 this->SearchAppBundleOnly = false;
155 else if(fab == "ONLY")
157 this->SearchAppBundleLast = false;
158 this->SearchAppBundleFirst = false;
159 this->SearchAppBundleOnly = true;
161 else if(fab == "FIRST")
163 this->SearchAppBundleLast = false;
164 this->SearchAppBundleFirst = true;
165 this->SearchAppBundleOnly = false;
167 else if(fab == "LAST")
169 this->SearchAppBundleLast = true;
170 this->SearchAppBundleFirst = false;
171 this->SearchAppBundleOnly = false;
175 //----------------------------------------------------------------------------
176 void cmFindCommon::RerootPaths(std::vector<std::string>& paths)
178 #if 0
179 for(std::vector<std::string>::const_iterator i = paths.begin();
180 i != paths.end(); ++i)
182 fprintf(stderr, "[%s]\n", i->c_str());
184 #endif
186 // Short-circuit if there is nothing to do.
187 if(this->FindRootPathMode == RootPathModeNoRootPath)
189 return;
191 const char* rootPath =
192 this->Makefile->GetDefinition("CMAKE_FIND_ROOT_PATH");
193 if((rootPath == 0) || (strlen(rootPath) == 0))
195 return;
198 // Construct the list of path roots with no trailing slashes.
199 std::vector<std::string> roots;
200 cmSystemTools::ExpandListArgument(rootPath, roots);
201 for(std::vector<std::string>::iterator ri = roots.begin();
202 ri != roots.end(); ++ri)
204 cmSystemTools::ConvertToUnixSlashes(*ri);
207 // Copy the original set of unrooted paths.
208 std::vector<std::string> unrootedPaths = paths;
209 paths.clear();
211 for(std::vector<std::string>::const_iterator ri = roots.begin();
212 ri != roots.end(); ++ri)
214 for(std::vector<std::string>::const_iterator ui = unrootedPaths.begin();
215 ui != unrootedPaths.end(); ++ui)
217 // Place the unrooted path under the current root if it is not
218 // already inside. Skip the unrooted path if it is relative to
219 // a user home directory or is empty.
220 std::string rootedDir;
221 if(cmSystemTools::IsSubDirectory(ui->c_str(), ri->c_str()))
223 rootedDir = *ui;
225 else if(!ui->empty() && (*ui)[0] != '~')
227 // Start with the new root.
228 rootedDir = *ri;
229 rootedDir += "/";
231 // Append the original path with its old root removed.
232 rootedDir += cmSystemTools::SplitPathRootComponent(ui->c_str());
235 // Store the new path.
236 paths.push_back(rootedDir);
240 // If searching both rooted and unrooted paths add the original
241 // paths again.
242 if(this->FindRootPathMode == RootPathModeBoth)
244 paths.insert(paths.end(), unrootedPaths.begin(), unrootedPaths.end());
248 //----------------------------------------------------------------------------
249 bool cmFindCommon::CheckCommonArgument(std::string const& arg)
251 if(arg == "NO_DEFAULT_PATH")
253 this->NoDefaultPath = true;
255 else if(arg == "NO_CMAKE_ENVIRONMENT_PATH")
257 this->NoCMakeEnvironmentPath = true;
259 else if(arg == "NO_CMAKE_PATH")
261 this->NoCMakePath = true;
263 else if(arg == "NO_SYSTEM_ENVIRONMENT_PATH")
265 this->NoSystemEnvironmentPath = true;
267 else if(arg == "NO_CMAKE_SYSTEM_PATH")
269 this->NoCMakeSystemPath = true;
271 else if(arg == "NO_CMAKE_FIND_ROOT_PATH")
273 this->FindRootPathMode = RootPathModeNoRootPath;
275 else if(arg == "ONLY_CMAKE_FIND_ROOT_PATH")
277 this->FindRootPathMode = RootPathModeOnlyRootPath;
279 else if(arg == "CMAKE_FIND_ROOT_PATH_BOTH")
281 this->FindRootPathMode = RootPathModeBoth;
283 else
285 // The argument is not one of the above.
286 return false;
289 // The argument is one of the above.
290 return true;
293 //----------------------------------------------------------------------------
294 void cmFindCommon::AddPathSuffix(std::string const& arg)
296 std::string suffix = arg;
298 // Strip leading and trailing slashes.
299 if(suffix.empty())
301 return;
303 if(suffix[0] == '/')
305 suffix = suffix.substr(1, suffix.npos);
307 if(suffix.empty())
309 return;
311 if(suffix[suffix.size()-1] == '/')
313 suffix = suffix.substr(0, suffix.size()-1);
315 if(suffix.empty())
317 return;
320 // Store the suffix.
321 this->SearchPathSuffixes.push_back(suffix);
324 //----------------------------------------------------------------------------
325 void cmFindCommon::AddUserPath(std::string const& p,
326 std::vector<std::string>& paths)
328 // We should view the registry as the target application would view
329 // it.
330 cmSystemTools::KeyWOW64 view = cmSystemTools::KeyWOW64_32;
331 cmSystemTools::KeyWOW64 other_view = cmSystemTools::KeyWOW64_64;
332 if(const char* psize =
333 this->Makefile->GetDefinition("CMAKE_SIZEOF_VOID_P"))
335 if(atoi(psize) == 8)
337 view = cmSystemTools::KeyWOW64_64;
338 other_view = cmSystemTools::KeyWOW64_32;
342 // Expand using the view of the target application.
343 std::string expanded = p;
344 cmSystemTools::ExpandRegistryValues(expanded, view);
345 cmSystemTools::GlobDirs(expanded.c_str(), paths);
347 // Executables can be either 32-bit or 64-bit, so expand using the
348 // alternative view.
349 if(expanded != p && this->CMakePathName == "PROGRAM")
351 expanded = p;
352 cmSystemTools::ExpandRegistryValues(expanded, other_view);
353 cmSystemTools::GlobDirs(expanded.c_str(), paths);
357 //----------------------------------------------------------------------------
358 void cmFindCommon::AddCMakePath(const char* variable)
360 // Get a path from a CMake variable.
361 if(const char* varPath = this->Makefile->GetDefinition(variable))
363 std::vector<std::string> tmp;
364 cmSystemTools::ExpandListArgument(varPath, tmp);
366 // Relative paths are interpreted with respect to the current
367 // source directory.
368 this->AddPathsInternal(tmp, CMakePath);
372 //----------------------------------------------------------------------------
373 void cmFindCommon::AddEnvPath(const char* variable)
375 // Get a path from the environment.
376 std::vector<std::string> tmp;
377 cmSystemTools::GetPath(tmp, variable);
378 // Relative paths are interpreted with respect to the current
379 // working directory.
380 this->AddPathsInternal(tmp, EnvPath);
383 //----------------------------------------------------------------------------
384 void cmFindCommon::AddPathsInternal(std::vector<std::string> const& in_paths,
385 PathType pathType)
387 for(std::vector<std::string>::const_iterator i = in_paths.begin();
388 i != in_paths.end(); ++i)
390 this->AddPathInternal(*i, pathType);
394 //----------------------------------------------------------------------------
395 void cmFindCommon::AddPathInternal(std::string const& in_path,
396 PathType pathType)
398 if(in_path.empty())
400 return;
403 // Select the base path with which to interpret relative paths.
404 const char* relbase = 0;
405 if(pathType == CMakePath)
407 relbase = this->Makefile->GetCurrentDirectory();
410 // Convert to clean full path.
411 std::string fullPath =
412 cmSystemTools::CollapseFullPath(in_path.c_str(), relbase);
414 // Insert the path if has not already been emitted.
415 if(this->SearchPathsEmitted.insert(fullPath).second)
417 this->SearchPaths.push_back(fullPath.c_str());
421 //----------------------------------------------------------------------------
422 void cmFindCommon::AddTrailingSlashes(std::vector<std::string>& paths)
424 // Add a trailing slash to all paths to aid the search process.
425 for(std::vector<std::string>::iterator i = paths.begin();
426 i != paths.end(); ++i)
428 std::string& p = *i;
429 if(!p.empty() && p[p.size()-1] != '/')
431 p += "/";