1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmFindBase.cxx,v $
6 Date: $Date: 2008-03-07 20:30:33 $
7 Version: $Revision: 1.35 $
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 " [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"
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"
46 "This command is used to find a SEARCH_XXX_DESC. "
47 "A cache entry named by <VAR> is created to store the result "
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"
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 "
80 " <prefix>/XXX_SUBDIR for each <prefix> in CMAKE_PREFIX_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"
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 "
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 "
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");
109 // CMake versions below 2.3 did not search all these extra
110 // locations. Preserve compatibility unless a modern argument is
112 bool compatibility
= this->Makefile
->NeedBackwardsCompatibility(2,3);
114 // copy argsIn into args so it can be modified,
115 // in the process extract the DOC "documentation"
116 size_t size
= argsIn
.size();
117 std::vector
<std::string
> args
;
118 bool foundDoc
= false;
119 for(unsigned int j
= 0; j
< size
; ++j
)
121 if(foundDoc
|| argsIn
[j
] != "DOC" )
123 if(argsIn
[j
] == "ENV")
128 cmSystemTools::GetPath(args
, argsIn
[j
].c_str());
133 args
.push_back(argsIn
[j
]);
141 this->VariableDocumentation
= argsIn
[j
+1];
150 this->VariableName
= args
[0];
151 if(this->CheckForVariableInCache())
153 this->AlreadyInCache
= true;
156 this->AlreadyInCache
= false;
158 // Find the current root path mode.
159 this->SelectDefaultRootPathMode();
161 // Find the current bundle/framework search policy.
162 this->SelectDefaultMacMode();
164 std::vector
<std::string
> userPaths
;
166 bool doingNames
= true; // assume it starts with a name
167 bool doingPaths
= false;
168 bool doingPathSuf
= false;
169 bool newStyle
= false;
171 for (unsigned int j
= 1; j
< args
.size(); ++j
)
173 if(args
[j
] == "NAMES")
177 doingPathSuf
= false;
180 else if (args
[j
] == "PATHS")
185 doingPathSuf
= false;
187 else if (args
[j
] == "PATH_SUFFIXES")
189 compatibility
= false;
195 else if (args
[j
] == "NO_SYSTEM_PATH")
198 doingPathSuf
= false;
200 this->NoDefaultPath
= true;
202 else if (this->CheckCommonArgument(args
[j
]))
204 compatibility
= false;
206 doingPathSuf
= false;
213 this->Names
.push_back(args
[j
]);
217 userPaths
.push_back(args
[j
]);
219 else if(doingPathSuf
)
221 this->AddPathSuffix(args
[j
]);
226 // Now that arguments have been parsed check the compatibility
227 // setting. If we need to be compatible with CMake 2.2 and earlier
228 // do not add the CMake system paths. It is safe to add the CMake
229 // environment paths and system environment paths because that
230 // existed in 2.2. It is safe to add the CMake user variable paths
231 // because the user or project has explicitly set them.
234 this->NoCMakeSystemPath
= true;
237 if(this->VariableDocumentation
.size() == 0)
239 this->VariableDocumentation
= "Where can ";
240 if(this->Names
.size() == 0)
242 this->VariableDocumentation
+= "the (unknown) library be found";
244 else if(this->Names
.size() == 1)
246 this->VariableDocumentation
+= "the "
247 + this->Names
[0] + " library be found";
251 this->VariableDocumentation
+= "one of the " + this->Names
[0];
252 for (unsigned int j
= 1; j
< this->Names
.size() - 1; ++j
)
254 this->VariableDocumentation
+= ", " + this->Names
[j
];
256 this->VariableDocumentation
+= " or "
257 + this->Names
[this->Names
.size() - 1] + " libraries be found";
261 // look for old style
262 // FIND_*(VAR name path1 path2 ...)
265 this->Names
.clear(); // clear out any values in Names
266 this->Names
.push_back(args
[1]);
267 for(unsigned int j
= 2; j
< args
.size(); ++j
)
269 userPaths
.push_back(args
[j
]);
272 this->ExpandPaths(userPaths
);
274 // Handle search root stuff.
275 this->RerootPaths(this->SearchPaths
);
279 void cmFindBase::ExpandPaths(std::vector
<std::string
> userPaths
)
281 // if NO Default paths was not specified add the
282 // standard search paths.
283 if(!this->NoDefaultPath
)
285 if(this->SearchFrameworkFirst
|| this->SearchFrameworkOnly
)
287 this->AddFrameWorkPaths();
289 if(this->SearchAppBundleFirst
|| this->SearchAppBundleOnly
)
291 this->AddAppBundlePaths();
293 if(!this->NoCMakeEnvironmentPath
&&
294 !(this->SearchFrameworkOnly
|| this->SearchAppBundleOnly
))
296 // Add CMAKE_*_PATH environment variables
297 this->AddEnvironmentVariables();
299 if(!this->NoCMakePath
&&
300 !(this->SearchFrameworkOnly
|| this->SearchAppBundleOnly
))
302 // Add CMake varibles of the same name as the previous environment
303 // varibles CMAKE_*_PATH to be used most of the time with -D
304 // command line options
305 this->AddCMakeVariables();
307 if(!this->NoSystemEnvironmentPath
&&
308 !(this->SearchFrameworkOnly
|| this->SearchAppBundleOnly
))
310 // add System environment PATH and (LIB or INCLUDE)
311 this->AddSystemEnvironmentVariables();
313 if(!this->NoCMakeSystemPath
&&
314 !(this->SearchFrameworkOnly
|| this->SearchAppBundleOnly
))
316 // Add CMAKE_SYSTEM_*_PATH variables which are defined in platform files
317 this->AddCMakeSystemVariables();
319 if(this->SearchAppBundleLast
)
321 this->AddAppBundlePaths();
323 if(this->SearchFrameworkLast
)
325 this->AddFrameWorkPaths();
328 std::vector
<std::string
> paths
;
329 // add the paths specified in the FIND_* call
330 for(unsigned int i
=0; i
< userPaths
.size(); ++i
)
332 paths
.push_back(userPaths
[i
]);
334 this->AddPaths(paths
);
337 //----------------------------------------------------------------------------
338 void cmFindBase::AddEnvironmentVariables()
340 std::vector
<std::string
> paths
;
342 std::vector
<std::string
> prefixPaths
;
343 cmSystemTools::GetPath(prefixPaths
, "CMAKE_PREFIX_PATH");
344 this->AddFindPrefix(paths
, prefixPaths
);
346 std::string var
= "CMAKE_";
347 var
+= this->CMakePathName
;
349 cmSystemTools::GetPath(paths
, var
.c_str());
350 this->AddPaths(paths
);
353 void cmFindBase::AddFindPrefix(std::vector
<std::string
>& dest
,
354 const std::vector
<std::string
>& src
)
356 // default for programs
357 std::string subdir
= "bin";
359 if (this->CMakePathName
== "INCLUDE")
363 else if (this->CMakePathName
== "LIBRARY")
367 else if (this->CMakePathName
== "FRAMEWORK")
369 subdir
= ""; // ? what to do for frameworks ?
372 for (std::vector
<std::string
>::const_iterator it
= src
.begin();
376 std::string dir
= it
->c_str();
377 if(!subdir
.empty() && !dir
.empty() && dir
[dir
.size()-1] != '/')
381 dest
.push_back(dir
+ subdir
);
384 dest
.push_back(dir
+ "sbin");
393 void cmFindBase::AddFrameWorkPaths()
395 std::vector
<std::string
> paths
;
396 this->GetFrameworkPaths(paths
);
397 this->AddPaths(paths
);
400 void cmFindBase::AddPaths(std::vector
<std::string
> & paths
)
402 // add suffixes and clean up paths
403 this->ExpandRegistryAndCleanPath(paths
);
404 // add the paths to the search paths
405 this->SearchPaths
.insert(this->SearchPaths
.end(),
410 void cmFindBase::AddAppBundlePaths()
412 std::vector
<std::string
> paths
;
413 this->GetAppBundlePaths(paths
);
414 this->AddPaths(paths
);
417 void cmFindBase::AddCMakeVariables()
419 std::string var
= "CMAKE_";
420 var
+= this->CMakePathName
;
422 std::vector
<std::string
> paths
;
424 if(const char* prefixPath
=
425 this->Makefile
->GetDefinition("CMAKE_PREFIX_PATH"))
427 std::vector
<std::string
> prefixPaths
;
428 cmSystemTools::ExpandListArgument(prefixPath
, prefixPaths
);
429 this->AddFindPrefix(paths
, prefixPaths
);
432 if(const char* path
= this->Makefile
->GetDefinition(var
.c_str()))
434 cmSystemTools::ExpandListArgument(path
, paths
);
436 this->AddPaths(paths
);
439 void cmFindBase::AddSystemEnvironmentVariables()
441 // Add LIB or INCLUDE
442 std::vector
<std::string
> paths
;
443 if(this->EnvironmentPath
.size())
445 cmSystemTools::GetPath(paths
, this->EnvironmentPath
.c_str());
448 cmSystemTools::GetPath(paths
);
449 this->AddPaths(paths
);
452 void cmFindBase::AddCMakeSystemVariables()
454 std::string var
= "CMAKE_SYSTEM_";
455 var
+= this->CMakePathName
;
457 std::vector
<std::string
> paths
;
458 if(const char* prefixPath
=
459 this->Makefile
->GetDefinition("CMAKE_SYSTEM_PREFIX_PATH"))
461 std::vector
<std::string
> prefixPaths
;
462 cmSystemTools::ExpandListArgument(prefixPath
, prefixPaths
);
463 this->AddFindPrefix(paths
, prefixPaths
);
465 if(const char* path
= this->Makefile
->GetDefinition(var
.c_str()))
467 cmSystemTools::ExpandListArgument(path
, paths
);
469 this->AddPaths(paths
);
472 void cmFindBase::ExpandRegistryAndCleanPath(std::vector
<std::string
>& paths
)
474 std::vector
<std::string
> finalPath
;
475 std::vector
<std::string
>::iterator i
;
476 // glob and expand registry stuff from paths and put
478 for(i
= paths
.begin();
479 i
!= paths
.end(); ++i
)
481 cmSystemTools::ExpandRegistryValues(*i
);
482 cmSystemTools::GlobDirs(i
->c_str(), finalPath
);
486 // convert all paths to unix slashes and add search path suffixes
488 for(i
= finalPath
.begin();
489 i
!= finalPath
.end(); ++i
)
491 cmSystemTools::ConvertToUnixSlashes(*i
);
492 // copy each finalPath combined with SearchPathSuffixes
493 // to the SearchPaths ivar
494 for(std::vector
<std::string
>::iterator j
=
495 this->SearchPathSuffixes
.begin();
496 j
!= this->SearchPathSuffixes
.end(); ++j
)
498 std::string p
= *i
+ std::string("/") + *j
;
499 // add to all paths because the search path may be modified
500 // later with lib being replaced for lib64 which may exist
504 // now put the path without the path suffixes in the SearchPaths
505 for(i
= finalPath
.begin();
506 i
!= finalPath
.end(); ++i
)
508 // put all search paths in because it may later be replaced
509 // by lib64 stuff fixes bug 4009
514 void cmFindBase::PrintFindStuff()
516 std::cerr
<< "SearchFrameworkLast: " << this->SearchFrameworkLast
<< "\n";
517 std::cerr
<< "SearchFrameworkOnly: " << this->SearchFrameworkOnly
<< "\n";
518 std::cerr
<< "SearchFrameworkFirst: " << this->SearchFrameworkFirst
<< "\n";
519 std::cerr
<< "SearchAppBundleLast: " << this->SearchAppBundleLast
<< "\n";
520 std::cerr
<< "SearchAppBundleOnly: " << this->SearchAppBundleOnly
<< "\n";
521 std::cerr
<< "SearchAppBundleFirst: " << this->SearchAppBundleFirst
<< "\n";
522 std::cerr
<< "VariableName " << this->VariableName
<< "\n";
523 std::cerr
<< "VariableDocumentation "
524 << this->VariableDocumentation
<< "\n";
525 std::cerr
<< "NoDefaultPath " << this->NoDefaultPath
<< "\n";
526 std::cerr
<< "NoCMakeEnvironmentPath "
527 << this->NoCMakeEnvironmentPath
<< "\n";
528 std::cerr
<< "NoCMakePath " << this->NoCMakePath
<< "\n";
529 std::cerr
<< "NoSystemEnvironmentPath "
530 << this->NoSystemEnvironmentPath
<< "\n";
531 std::cerr
<< "NoCMakeSystemPath " << this->NoCMakeSystemPath
<< "\n";
532 std::cerr
<< "EnvironmentPath " << this->EnvironmentPath
<< "\n";
533 std::cerr
<< "CMakePathName " << this->CMakePathName
<< "\n";
534 std::cerr
<< "Names ";
535 for(unsigned int i
=0; i
< this->Names
.size(); ++i
)
537 std::cerr
<< this->Names
[i
] << " ";
541 std::cerr
<< "SearchPathSuffixes ";
542 for(unsigned int i
=0; i
< this->SearchPathSuffixes
.size(); ++i
)
544 std::cerr
<< this->SearchPathSuffixes
[i
] << "\n";
547 std::cerr
<< "SearchPaths\n";
548 for(unsigned int i
=0; i
< this->SearchPaths
.size(); ++i
)
550 std::cerr
<< "[" << this->SearchPaths
[i
] << "]\n";
554 bool cmFindBase::CheckForVariableInCache()
556 if(const char* cacheValue
=
557 this->Makefile
->GetDefinition(this->VariableName
.c_str()))
559 cmCacheManager::CacheIterator it
=
560 this->Makefile
->GetCacheManager()->
561 GetCacheIterator(this->VariableName
.c_str());
562 bool found
= !cmSystemTools::IsNOTFOUND(cacheValue
);
563 bool cached
= !it
.IsAtEnd();
566 // If the user specifies the entry on the command line without a
567 // type we should add the type and docstring but keep the
568 // original value. Tell the subclass implementations to do
570 if(cached
&& it
.GetType() == cmCacheManager::UNINITIALIZED
)
572 this->AlreadyInCacheWithoutMetaInfo
= true;
578 const char* hs
= it
.GetProperty("HELPSTRING");
579 this->VariableDocumentation
= hs
?hs
:"(none)";