1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmFindBase.cxx,v $
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"
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
= false;
113 const char* versionValue
=
114 this->Makefile
->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY");
117 if(versionValue
&& sscanf(versionValue
, "%d.%d", &major
, &minor
) != 2)
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")
140 cmSystemTools::GetPath(args
, argsIn
[j
].c_str());
145 args
.push_back(argsIn
[j
]);
153 this->VariableDocumentation
= argsIn
[j
+1];
162 this->VariableName
= args
[0];
163 if(this->CheckForVariableInCache())
165 this->AlreadyInCache
= 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
;
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")
189 doingPathSuf
= false;
192 else if (args
[j
] == "PATHS")
197 doingPathSuf
= false;
199 else if (args
[j
] == "PATH_SUFFIXES")
201 compatibility
= false;
207 else if (args
[j
] == "NO_SYSTEM_PATH")
210 doingPathSuf
= false;
212 this->NoDefaultPath
= true;
214 else if (this->CheckCommonArgument(args
[j
]))
216 compatibility
= false;
218 doingPathSuf
= false;
225 this->Names
.push_back(args
[j
]);
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.
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";
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 ...)
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
);
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
;
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")
375 else if (this->CMakePathName
== "LIBRARY")
379 else if (this->CMakePathName
== "FRAMEWORK")
381 subdir
= ""; // ? what to do for frameworks ?
384 for (std::vector
<std::string
>::const_iterator it
= src
.begin();
388 std::string dir
= it
->c_str();
389 if(!subdir
.empty() && !dir
.empty() && dir
[dir
.size()-1] != '/')
393 dest
.push_back(dir
+ subdir
);
396 dest
.push_back(dir
+ "sbin");
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(),
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
;
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());
460 cmSystemTools::GetPath(paths
);
461 this->AddPaths(paths
);
464 void cmFindBase::AddCMakeSystemVariables()
466 std::string var
= "CMAKE_SYSTEM_";
467 var
+= this->CMakePathName
;
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
490 for(i
= paths
.begin();
491 i
!= paths
.end(); ++i
)
493 cmSystemTools::ExpandRegistryValues(*i
);
494 cmSystemTools::GlobDirs(i
->c_str(), finalPath
);
498 // convert all paths to unix slashes and add search path suffixes
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
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
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
] << " ";
553 std::cerr
<< "SearchPathSuffixes ";
554 for(unsigned int i
=0; i
< this->SearchPathSuffixes
.size(); ++i
)
556 std::cerr
<< this->SearchPathSuffixes
[i
] << "\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();
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
582 if(cached
&& it
.GetType() == cmCacheManager::UNINITIALIZED
)
584 this->AlreadyInCacheWithoutMetaInfo
= true;
590 const char* hs
= it
.GetProperty("HELPSTRING");
591 this->VariableDocumentation
= hs
?hs
:"(none)";