1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmFindBase.cxx,v $
6 Date: $Date: 2008-10-13 13:58:22 $
7 Version: $Revision: 1.50 $
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"
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"
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"
47 "This command is used to find a SEARCH_XXX_DESC. "
48 "A cache entry named by <VAR> is created to store the result "
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. PATH_SUFFIXES can be used to give sub directories "
65 "that will be appended to the search paths.\n"
66 "If NO_DEFAULT_PATH is specified, then no additional paths are "
67 "added to the search. "
68 "If NO_DEFAULT_PATH is not specified, the search process is as follows:\n"
69 "1. Search paths specified in cmake-specific cache variables. "
70 "These are intended to be used on the command line with a -DVAR=value. "
71 "This can be skipped if NO_CMAKE_PATH is passed.\n"
72 " <prefix>/XXX_SUBDIR for each <prefix> in CMAKE_PREFIX_PATH\n"
74 " CMAKE_XXX_MAC_PATH\n"
75 "2. Search paths specified in cmake-specific environment variables. "
76 "These are intended to be set in the user's shell configuration. "
77 "This can be skipped if NO_CMAKE_ENVIRONMENT_PATH is passed.\n"
78 " <prefix>/XXX_SUBDIR for each <prefix> in CMAKE_PREFIX_PATH\n"
80 " CMAKE_XXX_MAC_PATH\n"
81 "3. Search the paths specified by the HINTS option. "
82 "These should be paths computed by system introspection, such as a "
83 "hint provided by the location of another item already found. "
84 "Hard-coded guesses should be specified with the PATHS option.\n"
85 "4. Search the standard system environment variables. "
86 "This can be skipped if NO_SYSTEM_ENVIRONMENT_PATH is an argument.\n"
88 " XXX_SYSTEM\n" // replace with "", LIB, or INCLUDE
89 "5. Search cmake variables defined in the Platform files "
90 "for the current system. This can be skipped if NO_CMAKE_SYSTEM_PATH "
92 " <prefix>/XXX_SUBDIR for each <prefix> in CMAKE_SYSTEM_PREFIX_PATH\n"
93 " CMAKE_SYSTEM_XXX_PATH\n"
94 " CMAKE_SYSTEM_XXX_MAC_PATH\n"
95 "6. Search the paths specified by the PATHS option "
96 "or in the short-hand version of the command. "
97 "These are typically hard-coded guesses.\n"
99 this->GenericDocumentation
+= this->GenericDocumentationMacPolicy
;
100 this->GenericDocumentation
+= this->GenericDocumentationRootPath
;
101 this->GenericDocumentation
+= this->GenericDocumentationPathsOrder
;
104 bool cmFindBase::ParseArguments(std::vector
<std::string
> const& argsIn
)
106 if(argsIn
.size() < 2 )
108 this->SetError("called with incorrect number of arguments");
112 // CMake versions below 2.3 did not search all these extra
113 // locations. Preserve compatibility unless a modern argument is
115 bool compatibility
= this->Makefile
->NeedBackwardsCompatibility(2,3);
117 // copy argsIn into args so it can be modified,
118 // in the process extract the DOC "documentation"
119 size_t size
= argsIn
.size();
120 std::vector
<std::string
> args
;
121 bool foundDoc
= false;
122 for(unsigned int j
= 0; j
< size
; ++j
)
124 if(foundDoc
|| argsIn
[j
] != "DOC" )
126 if(argsIn
[j
] == "ENV")
131 cmSystemTools::GetPath(args
, argsIn
[j
].c_str());
136 args
.push_back(argsIn
[j
]);
144 this->VariableDocumentation
= argsIn
[j
+1];
153 this->VariableName
= args
[0];
154 if(this->CheckForVariableInCache())
156 this->AlreadyInCache
= true;
159 this->AlreadyInCache
= false;
161 // Find the current root path mode.
162 this->SelectDefaultRootPathMode();
164 // Find the current bundle/framework search policy.
165 this->SelectDefaultMacMode();
167 bool newStyle
= false;
168 enum Doing
{ DoingNone
, DoingNames
, DoingPaths
, DoingPathSuffixes
,
170 Doing doing
= DoingNames
; // assume it starts with a name
171 for (unsigned int j
= 1; j
< args
.size(); ++j
)
173 if(args
[j
] == "NAMES")
178 else if (args
[j
] == "PATHS")
183 else if (args
[j
] == "HINTS")
188 else if (args
[j
] == "PATH_SUFFIXES")
190 doing
= DoingPathSuffixes
;
191 compatibility
= false;
194 else if (args
[j
] == "NO_SYSTEM_PATH")
197 this->NoDefaultPath
= true;
199 else if (this->CheckCommonArgument(args
[j
]))
202 compatibility
= false;
203 // Some common arguments were accidentally supported by CMake
204 // 2.4 and 2.6.0 in the short-hand form of the command, so we
205 // must support it even though it is not documented.
207 else if(doing
== DoingNames
)
209 this->Names
.push_back(args
[j
]);
211 else if(doing
== DoingPaths
)
213 this->AddUserPath(args
[j
], this->UserPaths
);
215 else if(doing
== DoingHints
)
217 this->AddUserPath(args
[j
], this->UserHints
);
219 else if(doing
== DoingPathSuffixes
)
221 this->AddPathSuffix(args
[j
]);
225 // Now that arguments have been parsed check the compatibility
226 // setting. If we need to be compatible with CMake 2.2 and earlier
227 // do not add the CMake system paths. It is safe to add the CMake
228 // environment paths and system environment paths because that
229 // existed in 2.2. It is safe to add the CMake user variable paths
230 // because the user or project has explicitly set them.
233 this->NoCMakeSystemPath
= true;
236 if(this->VariableDocumentation
.size() == 0)
238 this->VariableDocumentation
= "Where can ";
239 if(this->Names
.size() == 0)
241 this->VariableDocumentation
+= "the (unknown) library be found";
243 else if(this->Names
.size() == 1)
245 this->VariableDocumentation
+= "the "
246 + this->Names
[0] + " library be found";
250 this->VariableDocumentation
+= "one of the " + this->Names
[0];
251 for (unsigned int j
= 1; j
< this->Names
.size() - 1; ++j
)
253 this->VariableDocumentation
+= ", " + this->Names
[j
];
255 this->VariableDocumentation
+= " or "
256 + this->Names
[this->Names
.size() - 1] + " libraries be found";
260 // look for old style
261 // FIND_*(VAR name path1 path2 ...)
264 // All the short-hand arguments have been recorded as names.
265 std::vector
<std::string
> shortArgs
= this->Names
;
266 this->Names
.clear(); // clear out any values in Names
267 this->Names
.push_back(shortArgs
[0]);
268 for(unsigned int j
= 1; j
< shortArgs
.size(); ++j
)
270 this->AddUserPath(shortArgs
[j
], this->UserPaths
);
275 // Handle search root stuff.
276 this->RerootPaths(this->SearchPaths
);
278 // Add a trailing slash to all prefixes to aid the search process.
279 this->AddTrailingSlashes(this->SearchPaths
);
284 void cmFindBase::ExpandPaths()
286 this->AddCMakeVariablePath();
287 this->AddCMakeEnvironmentPath();
288 this->AddUserHintsPath();
289 this->AddSystemEnvironmentPath();
290 this->AddCMakeSystemVariablePath();
291 this->AddUserGuessPath();
293 // Add suffixes and clean up paths.
294 this->AddPathSuffixes();
297 //----------------------------------------------------------------------------
298 void cmFindBase::AddPrefixPaths(std::vector
<std::string
> const& in_paths
,
301 // default for programs
302 std::string subdir
= "bin";
304 if (this->CMakePathName
== "INCLUDE")
308 else if (this->CMakePathName
== "LIBRARY")
312 else if (this->CMakePathName
== "FRAMEWORK")
314 subdir
= ""; // ? what to do for frameworks ?
317 for(std::vector
<std::string
>::const_iterator it
= in_paths
.begin();
318 it
!= in_paths
.end(); ++it
)
320 std::string dir
= it
->c_str();
321 if(!subdir
.empty() && !dir
.empty() && dir
[dir
.size()-1] != '/')
325 std::string add
= dir
+ subdir
;
328 this->AddPathInternal(add
, pathType
);
332 this->AddPathInternal(dir
+"sbin", pathType
);
334 if(!subdir
.empty() && *it
!= "/")
336 this->AddPathInternal(*it
, pathType
);
341 //----------------------------------------------------------------------------
342 void cmFindBase::AddCMakePrefixPath(const char* variable
)
344 // Get a path from a CMake variable.
345 if(const char* varPath
= this->Makefile
->GetDefinition(variable
))
347 std::vector
<std::string
> tmp
;
348 cmSystemTools::ExpandListArgument(varPath
, tmp
);
349 this->AddPrefixPaths(tmp
, CMakePath
);
353 //----------------------------------------------------------------------------
354 void cmFindBase::AddEnvPrefixPath(const char* variable
)
356 // Get a path from the environment.
357 std::vector
<std::string
> tmp
;
358 cmSystemTools::GetPath(tmp
, variable
);
359 this->AddPrefixPaths(tmp
, EnvPath
);
362 //----------------------------------------------------------------------------
363 void cmFindBase::AddCMakeEnvironmentPath()
365 if(!this->NoCMakeEnvironmentPath
&& !this->NoDefaultPath
)
367 // Add CMAKE_*_PATH environment variables
368 std::string var
= "CMAKE_";
369 var
+= this->CMakePathName
;
371 this->AddEnvPrefixPath("CMAKE_PREFIX_PATH");
372 this->AddEnvPath(var
.c_str());
374 if(this->CMakePathName
== "PROGRAM")
376 this->AddEnvPath("CMAKE_APPBUNDLE_PATH");
380 this->AddEnvPath("CMAKE_FRAMEWORK_PATH");
385 //----------------------------------------------------------------------------
386 void cmFindBase::AddCMakeVariablePath()
388 if(!this->NoCMakePath
&& !this->NoDefaultPath
)
390 // Add CMake varibles of the same name as the previous environment
391 // varibles CMAKE_*_PATH to be used most of the time with -D
392 // command line options
393 std::string var
= "CMAKE_";
394 var
+= this->CMakePathName
;
396 this->AddCMakePrefixPath("CMAKE_PREFIX_PATH");
397 this->AddCMakePath(var
.c_str());
399 if(this->CMakePathName
== "PROGRAM")
401 this->AddCMakePath("CMAKE_APPBUNDLE_PATH");
405 this->AddCMakePath("CMAKE_FRAMEWORK_PATH");
410 //----------------------------------------------------------------------------
411 void cmFindBase::AddSystemEnvironmentPath()
413 if(!this->NoSystemEnvironmentPath
&& !this->NoDefaultPath
)
415 // Add LIB or INCLUDE
416 if(!this->EnvironmentPath
.empty())
418 this->AddEnvPath(this->EnvironmentPath
.c_str());
425 //----------------------------------------------------------------------------
426 void cmFindBase::AddCMakeSystemVariablePath()
428 if(!this->NoCMakeSystemPath
&& !this->NoDefaultPath
)
430 std::string var
= "CMAKE_SYSTEM_";
431 var
+= this->CMakePathName
;
433 this->AddCMakePrefixPath("CMAKE_SYSTEM_PREFIX_PATH");
434 this->AddCMakePath(var
.c_str());
436 if(this->CMakePathName
== "PROGRAM")
438 this->AddCMakePath("CMAKE_SYSTEM_APPBUNDLE_PATH");
442 this->AddCMakePath("CMAKE_SYSTEM_FRAMEWORK_PATH");
447 //----------------------------------------------------------------------------
448 void cmFindBase::AddUserHintsPath()
450 this->AddPathsInternal(this->UserHints
, CMakePath
);
453 //----------------------------------------------------------------------------
454 void cmFindBase::AddUserGuessPath()
456 this->AddPathsInternal(this->UserPaths
, CMakePath
);
459 //----------------------------------------------------------------------------
460 void cmFindBase::AddPathSuffixes()
462 std::vector
<std::string
>& paths
= this->SearchPaths
;
463 std::vector
<std::string
> finalPath
= paths
;
464 std::vector
<std::string
>::iterator i
;
467 // convert all paths to unix slashes and add search path suffixes
469 for(i
= finalPath
.begin();
470 i
!= finalPath
.end(); ++i
)
472 cmSystemTools::ConvertToUnixSlashes(*i
);
473 // copy each finalPath combined with SearchPathSuffixes
474 // to the SearchPaths ivar
475 for(std::vector
<std::string
>::iterator j
=
476 this->SearchPathSuffixes
.begin();
477 j
!= this->SearchPathSuffixes
.end(); ++j
)
479 // if *i is only / then do not add a //
480 // this will get incorrectly considered a network
481 // path on windows and cause huge delays.
483 if(p
.size() && p
[p
.size()-1] != '/')
485 p
+= std::string("/");
488 // add to all paths because the search path may be modified
489 // later with lib being replaced for lib64 which may exist
492 // now put the path without the path suffixes in the SearchPaths
497 void cmFindBase::PrintFindStuff()
499 std::cerr
<< "SearchFrameworkLast: " << this->SearchFrameworkLast
<< "\n";
500 std::cerr
<< "SearchFrameworkOnly: " << this->SearchFrameworkOnly
<< "\n";
501 std::cerr
<< "SearchFrameworkFirst: " << this->SearchFrameworkFirst
<< "\n";
502 std::cerr
<< "SearchAppBundleLast: " << this->SearchAppBundleLast
<< "\n";
503 std::cerr
<< "SearchAppBundleOnly: " << this->SearchAppBundleOnly
<< "\n";
504 std::cerr
<< "SearchAppBundleFirst: " << this->SearchAppBundleFirst
<< "\n";
505 std::cerr
<< "VariableName " << this->VariableName
<< "\n";
506 std::cerr
<< "VariableDocumentation "
507 << this->VariableDocumentation
<< "\n";
508 std::cerr
<< "NoDefaultPath " << this->NoDefaultPath
<< "\n";
509 std::cerr
<< "NoCMakeEnvironmentPath "
510 << this->NoCMakeEnvironmentPath
<< "\n";
511 std::cerr
<< "NoCMakePath " << this->NoCMakePath
<< "\n";
512 std::cerr
<< "NoSystemEnvironmentPath "
513 << this->NoSystemEnvironmentPath
<< "\n";
514 std::cerr
<< "NoCMakeSystemPath " << this->NoCMakeSystemPath
<< "\n";
515 std::cerr
<< "EnvironmentPath " << this->EnvironmentPath
<< "\n";
516 std::cerr
<< "CMakePathName " << this->CMakePathName
<< "\n";
517 std::cerr
<< "Names ";
518 for(unsigned int i
=0; i
< this->Names
.size(); ++i
)
520 std::cerr
<< this->Names
[i
] << " ";
524 std::cerr
<< "SearchPathSuffixes ";
525 for(unsigned int i
=0; i
< this->SearchPathSuffixes
.size(); ++i
)
527 std::cerr
<< this->SearchPathSuffixes
[i
] << "\n";
530 std::cerr
<< "SearchPaths\n";
531 for(unsigned int i
=0; i
< this->SearchPaths
.size(); ++i
)
533 std::cerr
<< "[" << this->SearchPaths
[i
] << "]\n";
537 bool cmFindBase::CheckForVariableInCache()
539 if(const char* cacheValue
=
540 this->Makefile
->GetDefinition(this->VariableName
.c_str()))
542 cmCacheManager::CacheIterator it
=
543 this->Makefile
->GetCacheManager()->
544 GetCacheIterator(this->VariableName
.c_str());
545 bool found
= !cmSystemTools::IsNOTFOUND(cacheValue
);
546 bool cached
= !it
.IsAtEnd();
549 // If the user specifies the entry on the command line without a
550 // type we should add the type and docstring but keep the
551 // original value. Tell the subclass implementations to do
553 if(cached
&& it
.GetType() == cmCacheManager::UNINITIALIZED
)
555 this->AlreadyInCacheWithoutMetaInfo
= true;
561 const char* hs
= it
.GetProperty("HELPSTRING");
562 this->VariableDocumentation
= hs
?hs
:"(none)";