1 /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 file Copyright.txt or https://cmake.org/licensing for details. */
3 #include "cmFindBase.h"
10 #include <cm/optional>
11 #include <cmext/algorithm>
13 #include "cmCMakePath.h"
14 #include "cmMakefile.h"
15 #include "cmMessageType.h"
16 #include "cmPolicies.h"
18 #include "cmSearchPath.h"
20 #include "cmStateTypes.h"
21 #include "cmStringAlgorithms.h"
22 #include "cmSystemTools.h"
24 #include "cmWindowsRegistry.h"
27 class cmExecutionStatus;
29 cmFindBase::cmFindBase(std::string findCommandName, cmExecutionStatus& status)
30 : cmFindCommon(status)
31 , FindCommandName(std::move(findCommandName))
35 bool cmFindBase::ParseArguments(std::vector<std::string> const& argsIn)
37 if (argsIn.size() < 2) {
38 this->SetError("called with incorrect number of arguments");
42 // copy argsIn into args so it can be modified,
43 // in the process extract the DOC "documentation"
44 // and handle options NO_CACHE and ENV
45 size_t size = argsIn.size();
46 std::vector<std::string> args;
47 bool foundDoc = false;
48 for (unsigned int j = 0; j < size; ++j) {
49 if (foundDoc || argsIn[j] != "DOC") {
50 if (argsIn[j] == "NO_CACHE") {
51 this->StoreResultInCache = false;
52 } else if (argsIn[j] == "ENV") {
55 cmSystemTools::GetPath(args, argsIn[j].c_str());
58 args.push_back(argsIn[j]);
63 this->VariableDocumentation = argsIn[j + 1];
71 if (args.size() < 2) {
72 this->SetError("called with incorrect number of arguments");
75 this->VariableName = args[0];
76 if (this->CheckForVariableDefined()) {
77 this->AlreadyDefined = true;
81 // Find what search path locations have been enabled/disable
82 this->SelectDefaultSearchModes();
84 // Find the current root path mode.
85 this->SelectDefaultRootPathMode();
87 // Find the current bundle/framework search policy.
88 this->SelectDefaultMacMode();
90 bool newStyle = false;
99 Doing doing = DoingNames; // assume it starts with a name
100 for (unsigned int j = 1; j < args.size(); ++j) {
101 if (args[j] == "NAMES") {
104 } else if (args[j] == "PATHS") {
107 } else if (args[j] == "HINTS") {
110 } else if (args[j] == "PATH_SUFFIXES") {
111 doing = DoingPathSuffixes;
113 } else if (args[j] == "NAMES_PER_DIR") {
115 if (this->NamesPerDirAllowed) {
116 this->NamesPerDir = true;
118 this->SetError("does not support NAMES_PER_DIR");
121 } else if (args[j] == "NO_SYSTEM_PATH") {
123 this->NoDefaultPath = true;
124 } else if (args[j] == "REQUIRED") {
126 this->Required = true;
128 } else if (args[j] == "REGISTRY_VIEW") {
129 if (++j == args.size()) {
130 this->SetError("missing required argument for \"REGISTRY_VIEW\"");
133 auto view = cmWindowsRegistry::ToView(args[j]);
135 this->RegistryView = *view;
138 cmStrCat("given invalid value for \"REGISTRY_VIEW\": ", args[j]));
141 } else if (this->CheckCommonArgument(args[j])) {
144 // Some common arguments were accidentally supported by CMake
145 // 2.4 and 2.6.0 in the short-hand form of the command, so we
146 // must support it even though it is not documented.
147 if (doing == DoingNames) {
148 this->Names.push_back(args[j]);
149 } else if (doing == DoingPaths) {
150 this->UserGuessArgs.push_back(args[j]);
151 } else if (doing == DoingHints) {
152 this->UserHintsArgs.push_back(args[j]);
153 } else if (doing == DoingPathSuffixes) {
154 this->AddPathSuffix(args[j]);
159 if (this->VariableDocumentation.empty()) {
160 this->VariableDocumentation = "Where can ";
161 if (this->Names.empty()) {
162 this->VariableDocumentation += "the (unknown) library be found";
163 } else if (this->Names.size() == 1) {
164 this->VariableDocumentation +=
165 "the " + this->Names.front() + " library be found";
167 this->VariableDocumentation += "one of the ";
168 this->VariableDocumentation +=
169 cmJoin(cmMakeRange(this->Names).retreat(1), ", ");
170 this->VariableDocumentation +=
171 " or " + this->Names.back() + " libraries be found";
175 // look for old style
176 // FIND_*(VAR name path1 path2 ...)
177 if (!newStyle && !this->Names.empty()) {
178 // All the short-hand arguments have been recorded as names.
179 std::vector<std::string> shortArgs = this->Names;
180 this->Names.clear(); // clear out any values in Names
181 this->Names.push_back(shortArgs[0]);
182 cm::append(this->UserGuessArgs, shortArgs.begin() + 1, shortArgs.end());
186 this->ComputeFinalPaths(IgnorePaths::Yes);
191 void cmFindBase::ExpandPaths()
193 if (!this->NoDefaultPath) {
194 if (!this->NoPackageRootPath) {
195 this->FillPackageRootPath();
197 if (!this->NoCMakePath) {
198 this->FillCMakeVariablePath();
200 if (!this->NoCMakeEnvironmentPath) {
201 this->FillCMakeEnvironmentPath();
204 this->FillUserHintsPath();
205 if (!this->NoDefaultPath) {
206 if (!this->NoSystemEnvironmentPath) {
207 this->FillSystemEnvironmentPath();
209 if (!this->NoCMakeSystemPath) {
210 this->FillCMakeSystemVariablePath();
213 this->FillUserGuessPath();
216 void cmFindBase::FillCMakeEnvironmentPath()
218 cmSearchPath& paths = this->LabeledPaths[PathLabel::CMakeEnvironment];
220 // Add CMAKE_*_PATH environment variables
221 std::string var = cmStrCat("CMAKE_", this->CMakePathName, "_PATH");
222 paths.AddEnvPrefixPath("CMAKE_PREFIX_PATH");
223 paths.AddEnvPath(var);
225 if (this->CMakePathName == "PROGRAM") {
226 paths.AddEnvPath("CMAKE_APPBUNDLE_PATH");
228 paths.AddEnvPath("CMAKE_FRAMEWORK_PATH");
230 paths.AddSuffixes(this->SearchPathSuffixes);
233 void cmFindBase::FillPackageRootPath()
235 cmSearchPath& paths = this->LabeledPaths[PathLabel::PackageRoot];
237 // Add the PACKAGE_ROOT_PATH from each enclosing find_package call.
238 for (std::vector<std::string> const& pkgPaths :
239 cmReverseRange(this->Makefile->FindPackageRootPathStack)) {
240 paths.AddPrefixPaths(pkgPaths);
243 paths.AddSuffixes(this->SearchPathSuffixes);
246 void cmFindBase::FillCMakeVariablePath()
248 cmSearchPath& paths = this->LabeledPaths[PathLabel::CMake];
250 // Add CMake variables of the same name as the previous environment
251 // variables CMAKE_*_PATH to be used most of the time with -D
252 // command line options
253 std::string var = cmStrCat("CMAKE_", this->CMakePathName, "_PATH");
254 paths.AddCMakePrefixPath("CMAKE_PREFIX_PATH");
255 paths.AddCMakePath(var);
257 if (this->CMakePathName == "PROGRAM") {
258 paths.AddCMakePath("CMAKE_APPBUNDLE_PATH");
260 paths.AddCMakePath("CMAKE_FRAMEWORK_PATH");
262 paths.AddSuffixes(this->SearchPathSuffixes);
265 void cmFindBase::FillSystemEnvironmentPath()
267 cmSearchPath& paths = this->LabeledPaths[PathLabel::SystemEnvironment];
269 // Add LIB or INCLUDE
270 if (!this->EnvironmentPath.empty()) {
271 paths.AddEnvPath(this->EnvironmentPath);
272 #if defined(_WIN32) || defined(__CYGWIN__)
273 paths.AddEnvPrefixPath("PATH", true);
277 paths.AddEnvPath("PATH");
278 paths.AddSuffixes(this->SearchPathSuffixes);
282 struct entry_to_remove
284 entry_to_remove(std::string const& name, cmMakefile* makefile)
287 if (cmValue to_skip = makefile->GetDefinition(
288 cmStrCat("_CMAKE_SYSTEM_PREFIX_PATH_", name, "_PREFIX_COUNT"))) {
289 cmStrToLong(to_skip, &count);
291 if (cmValue prefix_value = makefile->GetDefinition(
292 cmStrCat("_CMAKE_SYSTEM_PREFIX_PATH_", name, "_PREFIX_VALUE"))) {
293 value = *prefix_value;
296 bool valid() const { return count > 0 && !value.empty(); }
298 void remove_self(std::vector<std::string>& entries) const
301 long to_skip = this->count;
302 long index_to_remove = 0;
303 for (const auto& path : entries) {
304 if (path == this->value && --to_skip == 0) {
309 entries.erase(entries.begin() + index_to_remove);
318 void cmFindBase::FillCMakeSystemVariablePath()
320 cmSearchPath& paths = this->LabeledPaths[PathLabel::CMakeSystem];
322 const bool install_prefix_in_list =
323 !this->Makefile->IsOn("CMAKE_FIND_NO_INSTALL_PREFIX");
324 const bool remove_install_prefix = this->NoCMakeInstallPath;
325 const bool add_install_prefix = !this->NoCMakeInstallPath &&
326 this->Makefile->IsDefinitionSet("CMAKE_FIND_USE_INSTALL_PREFIX");
328 // We have 3 possible states for `CMAKE_SYSTEM_PREFIX_PATH` and
329 // `CMAKE_INSTALL_PREFIX`.
330 // Either we need to remove `CMAKE_INSTALL_PREFIX`, add
331 // `CMAKE_INSTALL_PREFIX`, or do nothing.
333 // When we need to remove `CMAKE_INSTALL_PREFIX` we remove the Nth occurrence
334 // of `CMAKE_INSTALL_PREFIX` from `CMAKE_SYSTEM_PREFIX_PATH`, where `N` is
335 // computed by `CMakeSystemSpecificInformation.cmake` while constructing
336 // `CMAKE_SYSTEM_PREFIX_PATH`. This ensures that if projects / toolchains
337 // have removed `CMAKE_INSTALL_PREFIX` from the list, we don't remove
338 // some other entry by mistake ( likewise for `CMAKE_STAGING_PREFIX` )
339 entry_to_remove install_entry("INSTALL", this->Makefile);
340 entry_to_remove staging_entry("STAGING", this->Makefile);
342 if (remove_install_prefix && install_prefix_in_list &&
343 (install_entry.valid() || staging_entry.valid())) {
344 cmValue prefix_paths =
345 this->Makefile->GetDefinition("CMAKE_SYSTEM_PREFIX_PATH");
347 // remove entries from CMAKE_SYSTEM_PREFIX_PATH
348 std::vector<std::string> expanded = cmExpandedList(*prefix_paths);
349 install_entry.remove_self(expanded);
350 staging_entry.remove_self(expanded);
352 paths.AddPrefixPaths(expanded,
353 this->Makefile->GetCurrentSourceDirectory().c_str());
354 } else if (add_install_prefix && !install_prefix_in_list) {
355 paths.AddCMakePrefixPath("CMAKE_INSTALL_PREFIX");
356 paths.AddCMakePrefixPath("CMAKE_STAGING_PREFIX");
357 paths.AddCMakePrefixPath("CMAKE_SYSTEM_PREFIX_PATH");
359 // Otherwise the current setup of `CMAKE_SYSTEM_PREFIX_PATH` is correct
360 paths.AddCMakePrefixPath("CMAKE_SYSTEM_PREFIX_PATH");
363 std::string var = cmStrCat("CMAKE_SYSTEM_", this->CMakePathName, "_PATH");
364 paths.AddCMakePath(var);
366 if (this->CMakePathName == "PROGRAM") {
367 paths.AddCMakePath("CMAKE_SYSTEM_APPBUNDLE_PATH");
369 paths.AddCMakePath("CMAKE_SYSTEM_FRAMEWORK_PATH");
371 paths.AddSuffixes(this->SearchPathSuffixes);
374 void cmFindBase::FillUserHintsPath()
376 cmSearchPath& paths = this->LabeledPaths[PathLabel::Hints];
378 for (std::string const& p : this->UserHintsArgs) {
379 paths.AddUserPath(p);
381 paths.AddSuffixes(this->SearchPathSuffixes);
384 void cmFindBase::FillUserGuessPath()
386 cmSearchPath& paths = this->LabeledPaths[PathLabel::Guess];
388 for (std::string const& p : this->UserGuessArgs) {
389 paths.AddUserPath(p);
391 paths.AddSuffixes(this->SearchPathSuffixes);
394 bool cmFindBase::CheckForVariableDefined()
396 if (cmValue value = this->Makefile->GetDefinition(this->VariableName)) {
397 cmState* state = this->Makefile->GetState();
398 cmValue cacheEntry = state->GetCacheEntryValue(this->VariableName);
399 bool found = !cmIsNOTFOUND(*value);
400 bool cached = cacheEntry != nullptr;
401 auto cacheType = cached ? state->GetCacheEntryType(this->VariableName)
402 : cmStateEnums::UNINITIALIZED;
404 if (cached && cacheType != cmStateEnums::UNINITIALIZED) {
405 this->VariableType = cacheType;
407 state->GetCacheEntryProperty(this->VariableName, "HELPSTRING")) {
408 this->VariableDocumentation = *hs;
413 // If the user specifies the entry on the command line without a
414 // type we should add the type and docstring but keep the
415 // original value. Tell the subclass implementations to do
417 if (cached && cacheType == cmStateEnums::UNINITIALIZED) {
418 this->AlreadyInCacheWithoutMetaInfo = true;
426 void cmFindBase::NormalizeFindResult()
428 if (this->Makefile->GetPolicyStatus(cmPolicies::CMP0125) ==
430 // ensure the path returned by find_* command is absolute
431 const auto& existingValue =
432 this->Makefile->GetDefinition(this->VariableName);
434 if (!existingValue->empty()) {
436 cmCMakePath(*existingValue, cmCMakePath::auto_format)
437 .Absolute(cmCMakePath(
438 this->Makefile->GetCMakeInstance()->GetCMakeWorkingDirectory()))
441 // value = cmSystemTools::CollapseFullPath(*existingValue);
442 if (!cmSystemTools::FileExists(value, false)) {
443 value = *existingValue;
447 if (this->StoreResultInCache) {
448 // If the user specifies the entry on the command line without a
449 // type we should add the type and docstring but keep the original
451 if (value != *existingValue || this->AlreadyInCacheWithoutMetaInfo) {
452 this->Makefile->GetCMakeInstance()->AddCacheEntry(
453 this->VariableName, value, this->VariableDocumentation.c_str(),
455 if (this->Makefile->GetPolicyStatus(cmPolicies::CMP0126) ==
457 if (this->Makefile->IsNormalDefinitionSet(this->VariableName)) {
458 this->Makefile->AddDefinition(this->VariableName, value);
461 // if there was a definition then remove it
462 // This is required to ensure same behavior as
463 // cmMakefile::AddCacheDefinition.
464 this->Makefile->RemoveDefinition(this->VariableName);
468 // ensure a normal variable is defined.
469 this->Makefile->AddDefinition(this->VariableName, value);
472 // If the user specifies the entry on the command line without a
473 // type we should add the type and docstring but keep the original
475 if (this->StoreResultInCache) {
476 if (this->AlreadyInCacheWithoutMetaInfo) {
477 this->Makefile->AddCacheDefinition(this->VariableName, "",
478 this->VariableDocumentation.c_str(),
480 if (this->Makefile->GetPolicyStatus(cmPolicies::CMP0126) ==
482 this->Makefile->IsNormalDefinitionSet(this->VariableName)) {
483 this->Makefile->AddDefinition(
485 *this->Makefile->GetCMakeInstance()->GetCacheDefinition(
486 this->VariableName));
490 // ensure a normal variable is defined.
491 this->Makefile->AddDefinition(
493 this->Makefile->GetSafeDefinition(this->VariableName));
498 void cmFindBase::StoreFindResult(const std::string& value)
501 this->Makefile->GetPolicyStatus(cmPolicies::CMP0125) == cmPolicies::NEW;
502 bool updateNormalVariable =
503 this->Makefile->GetPolicyStatus(cmPolicies::CMP0126) == cmPolicies::NEW;
505 if (!value.empty()) {
506 if (this->StoreResultInCache) {
507 this->Makefile->AddCacheDefinition(this->VariableName, value,
508 this->VariableDocumentation.c_str(),
509 this->VariableType, force);
510 if (updateNormalVariable &&
511 this->Makefile->IsNormalDefinitionSet(this->VariableName)) {
512 this->Makefile->AddDefinition(this->VariableName, value);
515 this->Makefile->AddDefinition(this->VariableName, value);
521 auto notFound = cmStrCat(this->VariableName, "-NOTFOUND");
522 if (this->StoreResultInCache) {
523 this->Makefile->AddCacheDefinition(this->VariableName, notFound,
524 this->VariableDocumentation.c_str(),
525 this->VariableType, force);
526 if (updateNormalVariable &&
527 this->Makefile->IsNormalDefinitionSet(this->VariableName)) {
528 this->Makefile->AddDefinition(this->VariableName, notFound);
531 this->Makefile->AddDefinition(this->VariableName, notFound);
534 if (this->Required) {
535 this->Makefile->IssueMessage(
536 MessageType::FATAL_ERROR,
537 cmStrCat("Could not find ", this->VariableName, " using the following ",
538 (this->FindCommandName == "find_file" ||
539 this->FindCommandName == "find_path"
542 ": ", cmJoin(this->Names, ", ")));
543 cmSystemTools::SetFatalErrorOccurred();
547 cmFindBaseDebugState::cmFindBaseDebugState(std::string commandName,
548 cmFindBase const* findBase)
549 : FindCommand(findBase)
550 , CommandName(std::move(commandName))
554 cmFindBaseDebugState::~cmFindBaseDebugState()
556 if (this->FindCommand->DebugMode) {
558 cmStrCat(this->CommandName, " called with the following settings:\n");
559 buffer += cmStrCat(" VAR: ", this->FindCommand->VariableName, "\n");
561 " NAMES: ", cmWrap("\"", this->FindCommand->Names, "\"", "\n "),
564 " Documentation: ", this->FindCommand->VariableDocumentation, "\n");
565 buffer += " Framework\n";
566 buffer += cmStrCat(" Only Search Frameworks: ",
567 this->FindCommand->SearchFrameworkOnly, "\n");
569 buffer += cmStrCat(" Search Frameworks Last: ",
570 this->FindCommand->SearchFrameworkLast, "\n");
571 buffer += cmStrCat(" Search Frameworks First: ",
572 this->FindCommand->SearchFrameworkFirst, "\n");
573 buffer += " AppBundle\n";
574 buffer += cmStrCat(" Only Search AppBundle: ",
575 this->FindCommand->SearchAppBundleOnly, "\n");
576 buffer += cmStrCat(" Search AppBundle Last: ",
577 this->FindCommand->SearchAppBundleLast, "\n");
578 buffer += cmStrCat(" Search AppBundle First: ",
579 this->FindCommand->SearchAppBundleFirst, "\n");
581 if (this->FindCommand->NoDefaultPath) {
582 buffer += " NO_DEFAULT_PATH Enabled\n";
585 " CMAKE_FIND_USE_CMAKE_PATH: ", !this->FindCommand->NoCMakePath, "\n",
586 " CMAKE_FIND_USE_CMAKE_ENVIRONMENT_PATH: ",
587 !this->FindCommand->NoCMakeEnvironmentPath, "\n",
588 " CMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH: ",
589 !this->FindCommand->NoSystemEnvironmentPath, "\n",
590 " CMAKE_FIND_USE_CMAKE_SYSTEM_PATH: ",
591 !this->FindCommand->NoCMakeSystemPath, "\n",
592 " CMAKE_FIND_USE_INSTALL_PREFIX: ",
593 !this->FindCommand->NoCMakeInstallPath, "\n");
597 cmStrCat(this->CommandName, " considered the following locations:\n");
598 for (auto const& state : this->FailedSearchLocations) {
599 std::string path = cmStrCat(" ", state.path);
600 if (!state.regexName.empty()) {
601 path = cmStrCat(path, "/", state.regexName);
603 buffer += cmStrCat(path, "\n");
606 if (!this->FoundSearchLocation.path.empty()) {
607 buffer += cmStrCat("The item was found at\n ",
608 this->FoundSearchLocation.path, "\n");
610 buffer += "The item was not found.\n";
613 this->FindCommand->DebugMessage(buffer);
617 void cmFindBaseDebugState::FoundAt(std::string const& path,
618 std::string regexName)
620 if (this->FindCommand->DebugMode) {
621 this->FoundSearchLocation = DebugLibState{ std::move(regexName), path };
625 void cmFindBaseDebugState::FailedAt(std::string const& path,
626 std::string regexName)
628 if (this->FindCommand->DebugMode) {
629 this->FailedSearchLocations.emplace_back(std::move(regexName), path);