cmFindBase.cxx 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  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"
  4. #include <algorithm>
  5. #include <cstddef>
  6. #include <deque>
  7. #include <functional>
  8. #include <iterator>
  9. #include <map>
  10. #include <utility>
  11. #include <cm/optional>
  12. #include <cmext/algorithm>
  13. #include <cmext/string_view>
  14. #include "cmCMakePath.h"
  15. #include "cmExecutionStatus.h"
  16. #include "cmList.h"
  17. #include "cmListFileCache.h"
  18. #include "cmMakefile.h"
  19. #include "cmMessageType.h"
  20. #include "cmPolicies.h"
  21. #include "cmRange.h"
  22. #include "cmSearchPath.h"
  23. #include "cmState.h"
  24. #include "cmStateTypes.h"
  25. #include "cmStringAlgorithms.h"
  26. #include "cmSystemTools.h"
  27. #include "cmValue.h"
  28. #include "cmWindowsRegistry.h"
  29. #include "cmake.h"
  30. cmFindBase::cmFindBase(std::string findCommandName, cmExecutionStatus& status)
  31. : cmFindCommon(status)
  32. , FindCommandName(std::move(findCommandName))
  33. {
  34. }
  35. bool cmFindBase::ParseArguments(std::vector<std::string> const& argsIn)
  36. {
  37. if (argsIn.size() < 2) {
  38. this->SetError("called with incorrect number of arguments");
  39. return false;
  40. }
  41. // copy argsIn into args so it can be modified,
  42. // in the process extract the DOC "documentation"
  43. // and handle options NO_CACHE and ENV
  44. size_t size = argsIn.size();
  45. std::vector<std::string> args;
  46. bool foundDoc = false;
  47. for (unsigned int j = 0; j < size; ++j) {
  48. if (foundDoc || argsIn[j] != "DOC") {
  49. if (argsIn[j] == "NO_CACHE") {
  50. this->StoreResultInCache = false;
  51. } else if (argsIn[j] == "ENV") {
  52. if (j + 1 < size) {
  53. j++;
  54. cmSystemTools::GetPath(args, argsIn[j].c_str());
  55. }
  56. } else {
  57. args.push_back(argsIn[j]);
  58. }
  59. } else {
  60. if (j + 1 < size) {
  61. foundDoc = true;
  62. this->VariableDocumentation = argsIn[j + 1];
  63. j++;
  64. if (j >= size) {
  65. break;
  66. }
  67. }
  68. }
  69. }
  70. if (args.size() < 2) {
  71. this->SetError("called with incorrect number of arguments");
  72. return false;
  73. }
  74. this->VariableName = args[0];
  75. if (this->CheckForVariableDefined()) {
  76. this->AlreadyDefined = true;
  77. return true;
  78. }
  79. // Find what search path locations have been enabled/disable
  80. this->SelectDefaultSearchModes();
  81. // Find the current root path mode.
  82. this->SelectDefaultRootPathMode();
  83. // Find the current bundle/framework search policy.
  84. this->SelectDefaultMacMode();
  85. bool newStyle = false;
  86. enum Doing
  87. {
  88. DoingNone,
  89. DoingNames,
  90. DoingPaths,
  91. DoingPathSuffixes,
  92. DoingHints
  93. };
  94. Doing doing = DoingNames; // assume it starts with a name
  95. for (unsigned int j = 1; j < args.size(); ++j) {
  96. if (args[j] == "NAMES") {
  97. doing = DoingNames;
  98. newStyle = true;
  99. } else if (args[j] == "PATHS") {
  100. doing = DoingPaths;
  101. newStyle = true;
  102. } else if (args[j] == "HINTS") {
  103. doing = DoingHints;
  104. newStyle = true;
  105. } else if (args[j] == "PATH_SUFFIXES") {
  106. doing = DoingPathSuffixes;
  107. newStyle = true;
  108. } else if (args[j] == "NAMES_PER_DIR") {
  109. doing = DoingNone;
  110. if (this->NamesPerDirAllowed) {
  111. this->NamesPerDir = true;
  112. } else {
  113. this->SetError("does not support NAMES_PER_DIR");
  114. return false;
  115. }
  116. } else if (args[j] == "NO_SYSTEM_PATH") {
  117. doing = DoingNone;
  118. this->NoDefaultPath = true;
  119. } else if (args[j] == "REQUIRED") {
  120. doing = DoingNone;
  121. this->Required = true;
  122. newStyle = true;
  123. } else if (args[j] == "REGISTRY_VIEW") {
  124. if (++j == args.size()) {
  125. this->SetError("missing required argument for \"REGISTRY_VIEW\"");
  126. return false;
  127. }
  128. auto view = cmWindowsRegistry::ToView(args[j]);
  129. if (view) {
  130. this->RegistryView = *view;
  131. } else {
  132. this->SetError(
  133. cmStrCat("given invalid value for \"REGISTRY_VIEW\": ", args[j]));
  134. return false;
  135. }
  136. } else if (args[j] == "VALIDATOR") {
  137. if (++j == args.size()) {
  138. this->SetError("missing required argument for \"VALIDATOR\"");
  139. return false;
  140. }
  141. auto command = this->Makefile->GetState()->GetCommand(args[j]);
  142. if (command == nullptr) {
  143. this->SetError(cmStrCat(
  144. "command specified for \"VALIDATOR\" is undefined: ", args[j], '.'));
  145. return false;
  146. }
  147. // ensure a macro is not specified as validator
  148. const auto& validatorName = args[j];
  149. cmList macros{ this->Makefile->GetProperty("MACROS") };
  150. if (std::find_if(macros.begin(), macros.end(),
  151. [&validatorName](const std::string& item) {
  152. return cmSystemTools::Strucmp(validatorName.c_str(),
  153. item.c_str()) == 0;
  154. }) != macros.end()) {
  155. this->SetError(cmStrCat(
  156. "command specified for \"VALIDATOR\" is not a function: ", args[j],
  157. '.'));
  158. return false;
  159. }
  160. this->ValidatorName = args[j];
  161. } else if (this->CheckCommonArgument(args[j])) {
  162. doing = DoingNone;
  163. } else {
  164. // Some common arguments were accidentally supported by CMake
  165. // 2.4 and 2.6.0 in the short-hand form of the command, so we
  166. // must support it even though it is not documented.
  167. if (doing == DoingNames) {
  168. this->Names.push_back(args[j]);
  169. } else if (doing == DoingPaths) {
  170. this->UserGuessArgs.push_back(args[j]);
  171. } else if (doing == DoingHints) {
  172. this->UserHintsArgs.push_back(args[j]);
  173. } else if (doing == DoingPathSuffixes) {
  174. this->AddPathSuffix(args[j]);
  175. }
  176. }
  177. }
  178. if (this->VariableDocumentation.empty()) {
  179. this->VariableDocumentation = "Where can ";
  180. if (this->Names.empty()) {
  181. this->VariableDocumentation += "the (unknown) library be found";
  182. } else if (this->Names.size() == 1) {
  183. this->VariableDocumentation +=
  184. "the " + this->Names.front() + " library be found";
  185. } else {
  186. this->VariableDocumentation += "one of the ";
  187. this->VariableDocumentation +=
  188. cmJoin(cmMakeRange(this->Names).retreat(1), ", ");
  189. this->VariableDocumentation +=
  190. " or " + this->Names.back() + " libraries be found";
  191. }
  192. }
  193. // look for old style
  194. // FIND_*(VAR name path1 path2 ...)
  195. if (!newStyle && !this->Names.empty()) {
  196. // All the short-hand arguments have been recorded as names.
  197. std::vector<std::string> shortArgs = this->Names;
  198. this->Names.clear(); // clear out any values in Names
  199. this->Names.push_back(shortArgs[0]);
  200. cm::append(this->UserGuessArgs, shortArgs.begin() + 1, shortArgs.end());
  201. }
  202. this->ExpandPaths();
  203. this->ComputeFinalPaths(IgnorePaths::Yes);
  204. return true;
  205. }
  206. bool cmFindBase::Validate(const std::string& path) const
  207. {
  208. if (this->ValidatorName.empty()) {
  209. return true;
  210. }
  211. // The validator command will be executed in an isolated scope.
  212. cmMakefile::ScopePushPop varScope(this->Makefile);
  213. cmMakefile::PolicyPushPop polScope(this->Makefile);
  214. static_cast<void>(varScope);
  215. static_cast<void>(polScope);
  216. auto resultName =
  217. cmStrCat("CMAKE_"_s, cmSystemTools::UpperCase(this->FindCommandName),
  218. "_VALIDATOR_STATUS"_s);
  219. this->Makefile->AddDefinitionBool(resultName, true);
  220. cmListFileFunction validator(
  221. this->ValidatorName, 0, 0,
  222. { cmListFileArgument(resultName, cmListFileArgument::Unquoted, 0),
  223. cmListFileArgument(path, cmListFileArgument::Quoted, 0) });
  224. cmExecutionStatus status(*this->Makefile);
  225. if (this->Makefile->ExecuteCommand(validator, status)) {
  226. return this->Makefile->GetDefinition(resultName).IsOn();
  227. }
  228. return false;
  229. }
  230. void cmFindBase::ExpandPaths()
  231. {
  232. if (!this->NoDefaultPath) {
  233. if (!this->NoPackageRootPath) {
  234. this->FillPackageRootPath();
  235. }
  236. if (!this->NoCMakePath) {
  237. this->FillCMakeVariablePath();
  238. }
  239. if (!this->NoCMakeEnvironmentPath) {
  240. this->FillCMakeEnvironmentPath();
  241. }
  242. }
  243. this->FillUserHintsPath();
  244. if (!this->NoDefaultPath) {
  245. if (!this->NoSystemEnvironmentPath) {
  246. this->FillSystemEnvironmentPath();
  247. }
  248. if (!this->NoCMakeSystemPath) {
  249. this->FillCMakeSystemVariablePath();
  250. }
  251. }
  252. this->FillUserGuessPath();
  253. }
  254. void cmFindBase::FillCMakeEnvironmentPath()
  255. {
  256. cmSearchPath& paths = this->LabeledPaths[PathLabel::CMakeEnvironment];
  257. // Add CMAKE_*_PATH environment variables
  258. std::string var = cmStrCat("CMAKE_", this->CMakePathName, "_PATH");
  259. paths.AddEnvPrefixPath("CMAKE_PREFIX_PATH");
  260. paths.AddEnvPath(var);
  261. if (this->CMakePathName == "PROGRAM") {
  262. paths.AddEnvPath("CMAKE_APPBUNDLE_PATH");
  263. } else {
  264. paths.AddEnvPath("CMAKE_FRAMEWORK_PATH");
  265. }
  266. paths.AddSuffixes(this->SearchPathSuffixes);
  267. }
  268. void cmFindBase::FillPackageRootPath()
  269. {
  270. cmSearchPath& paths = this->LabeledPaths[PathLabel::PackageRoot];
  271. // Add the PACKAGE_ROOT_PATH from each enclosing find_package call.
  272. for (std::vector<std::string> const& pkgPaths :
  273. cmReverseRange(this->Makefile->FindPackageRootPathStack)) {
  274. paths.AddPrefixPaths(pkgPaths);
  275. }
  276. paths.AddSuffixes(this->SearchPathSuffixes);
  277. }
  278. void cmFindBase::FillCMakeVariablePath()
  279. {
  280. cmSearchPath& paths = this->LabeledPaths[PathLabel::CMake];
  281. // Add CMake variables of the same name as the previous environment
  282. // variables CMAKE_*_PATH to be used most of the time with -D
  283. // command line options
  284. std::string var = cmStrCat("CMAKE_", this->CMakePathName, "_PATH");
  285. paths.AddCMakePrefixPath("CMAKE_PREFIX_PATH");
  286. paths.AddCMakePath(var);
  287. if (this->CMakePathName == "PROGRAM") {
  288. paths.AddCMakePath("CMAKE_APPBUNDLE_PATH");
  289. } else {
  290. paths.AddCMakePath("CMAKE_FRAMEWORK_PATH");
  291. }
  292. paths.AddSuffixes(this->SearchPathSuffixes);
  293. }
  294. void cmFindBase::FillSystemEnvironmentPath()
  295. {
  296. cmSearchPath& paths = this->LabeledPaths[PathLabel::SystemEnvironment];
  297. // Add LIB or INCLUDE
  298. if (!this->EnvironmentPath.empty()) {
  299. paths.AddEnvPath(this->EnvironmentPath);
  300. #if defined(_WIN32) || defined(__CYGWIN__)
  301. paths.AddEnvPrefixPath("PATH", true);
  302. #endif
  303. }
  304. // Add PATH
  305. paths.AddEnvPath("PATH");
  306. paths.AddSuffixes(this->SearchPathSuffixes);
  307. }
  308. namespace {
  309. struct entry_to_remove
  310. {
  311. entry_to_remove(std::string const& name, cmMakefile* makefile)
  312. : value()
  313. {
  314. if (cmValue to_skip = makefile->GetDefinition(
  315. cmStrCat("_CMAKE_SYSTEM_PREFIX_PATH_", name, "_PREFIX_COUNT"))) {
  316. cmStrToLong(*to_skip, &count);
  317. }
  318. if (cmValue prefix_value = makefile->GetDefinition(
  319. cmStrCat("_CMAKE_SYSTEM_PREFIX_PATH_", name, "_PREFIX_VALUE"))) {
  320. value = *prefix_value;
  321. }
  322. }
  323. bool valid() const { return count > 0 && !value.empty(); }
  324. void remove_self(std::vector<std::string>& entries) const
  325. {
  326. if (this->valid()) {
  327. long to_skip = this->count;
  328. long index_to_remove = 0;
  329. for (const auto& path : entries) {
  330. if (path == this->value && --to_skip == 0) {
  331. break;
  332. }
  333. ++index_to_remove;
  334. }
  335. entries.erase(entries.begin() + index_to_remove);
  336. }
  337. }
  338. long count = -1;
  339. std::string value;
  340. };
  341. }
  342. void cmFindBase::FillCMakeSystemVariablePath()
  343. {
  344. cmSearchPath& paths = this->LabeledPaths[PathLabel::CMakeSystem];
  345. const bool install_prefix_in_list =
  346. !this->Makefile->IsOn("CMAKE_FIND_NO_INSTALL_PREFIX");
  347. const bool remove_install_prefix = this->NoCMakeInstallPath;
  348. const bool add_install_prefix = !this->NoCMakeInstallPath &&
  349. this->Makefile->IsDefinitionSet("CMAKE_FIND_USE_INSTALL_PREFIX");
  350. // We have 3 possible states for `CMAKE_SYSTEM_PREFIX_PATH` and
  351. // `CMAKE_INSTALL_PREFIX`.
  352. // Either we need to remove `CMAKE_INSTALL_PREFIX`, add
  353. // `CMAKE_INSTALL_PREFIX`, or do nothing.
  354. //
  355. // When we need to remove `CMAKE_INSTALL_PREFIX` we remove the Nth occurrence
  356. // of `CMAKE_INSTALL_PREFIX` from `CMAKE_SYSTEM_PREFIX_PATH`, where `N` is
  357. // computed by `CMakeSystemSpecificInformation.cmake` while constructing
  358. // `CMAKE_SYSTEM_PREFIX_PATH`. This ensures that if projects / toolchains
  359. // have removed `CMAKE_INSTALL_PREFIX` from the list, we don't remove
  360. // some other entry by mistake ( likewise for `CMAKE_STAGING_PREFIX` )
  361. entry_to_remove install_entry("INSTALL", this->Makefile);
  362. entry_to_remove staging_entry("STAGING", this->Makefile);
  363. if (remove_install_prefix && install_prefix_in_list &&
  364. (install_entry.valid() || staging_entry.valid())) {
  365. cmValue prefix_paths =
  366. this->Makefile->GetDefinition("CMAKE_SYSTEM_PREFIX_PATH");
  367. // remove entries from CMAKE_SYSTEM_PREFIX_PATH
  368. cmList expanded{ *prefix_paths };
  369. install_entry.remove_self(expanded);
  370. staging_entry.remove_self(expanded);
  371. paths.AddPrefixPaths(expanded,
  372. this->Makefile->GetCurrentSourceDirectory().c_str());
  373. } else if (add_install_prefix && !install_prefix_in_list) {
  374. paths.AddCMakePrefixPath("CMAKE_INSTALL_PREFIX");
  375. paths.AddCMakePrefixPath("CMAKE_STAGING_PREFIX");
  376. paths.AddCMakePrefixPath("CMAKE_SYSTEM_PREFIX_PATH");
  377. } else {
  378. // Otherwise the current setup of `CMAKE_SYSTEM_PREFIX_PATH` is correct
  379. paths.AddCMakePrefixPath("CMAKE_SYSTEM_PREFIX_PATH");
  380. }
  381. std::string var = cmStrCat("CMAKE_SYSTEM_", this->CMakePathName, "_PATH");
  382. paths.AddCMakePath(var);
  383. if (this->CMakePathName == "PROGRAM") {
  384. paths.AddCMakePath("CMAKE_SYSTEM_APPBUNDLE_PATH");
  385. } else {
  386. paths.AddCMakePath("CMAKE_SYSTEM_FRAMEWORK_PATH");
  387. }
  388. paths.AddSuffixes(this->SearchPathSuffixes);
  389. }
  390. void cmFindBase::FillUserHintsPath()
  391. {
  392. cmSearchPath& paths = this->LabeledPaths[PathLabel::Hints];
  393. for (std::string const& p : this->UserHintsArgs) {
  394. paths.AddUserPath(p);
  395. }
  396. paths.AddSuffixes(this->SearchPathSuffixes);
  397. }
  398. void cmFindBase::FillUserGuessPath()
  399. {
  400. cmSearchPath& paths = this->LabeledPaths[PathLabel::Guess];
  401. for (std::string const& p : this->UserGuessArgs) {
  402. paths.AddUserPath(p);
  403. }
  404. paths.AddSuffixes(this->SearchPathSuffixes);
  405. }
  406. bool cmFindBase::CheckForVariableDefined()
  407. {
  408. if (cmValue value = this->Makefile->GetDefinition(this->VariableName)) {
  409. cmState* state = this->Makefile->GetState();
  410. cmValue cacheEntry = state->GetCacheEntryValue(this->VariableName);
  411. bool found = !cmIsNOTFOUND(*value);
  412. bool cached = cacheEntry != nullptr;
  413. auto cacheType = cached ? state->GetCacheEntryType(this->VariableName)
  414. : cmStateEnums::UNINITIALIZED;
  415. if (cached && cacheType != cmStateEnums::UNINITIALIZED) {
  416. this->VariableType = cacheType;
  417. if (const auto& hs =
  418. state->GetCacheEntryProperty(this->VariableName, "HELPSTRING")) {
  419. this->VariableDocumentation = *hs;
  420. }
  421. }
  422. if (found) {
  423. // If the user specifies the entry on the command line without a
  424. // type we should add the type and docstring but keep the
  425. // original value. Tell the subclass implementations to do
  426. // this.
  427. if (cached && cacheType == cmStateEnums::UNINITIALIZED) {
  428. this->AlreadyInCacheWithoutMetaInfo = true;
  429. }
  430. return true;
  431. }
  432. }
  433. return false;
  434. }
  435. void cmFindBase::NormalizeFindResult()
  436. {
  437. if (this->Makefile->GetPolicyStatus(cmPolicies::CMP0125) ==
  438. cmPolicies::NEW) {
  439. // ensure the path returned by find_* command is absolute
  440. const auto& existingValue =
  441. this->Makefile->GetDefinition(this->VariableName);
  442. std::string value;
  443. if (!existingValue->empty()) {
  444. value =
  445. cmCMakePath(*existingValue, cmCMakePath::auto_format)
  446. .Absolute(cmCMakePath(
  447. this->Makefile->GetCMakeInstance()->GetCMakeWorkingDirectory()))
  448. .Normal()
  449. .GenericString();
  450. // value = cmSystemTools::CollapseFullPath(*existingValue);
  451. if (!cmSystemTools::FileExists(value, false)) {
  452. value = *existingValue;
  453. }
  454. }
  455. if (this->StoreResultInCache) {
  456. // If the user specifies the entry on the command line without a
  457. // type we should add the type and docstring but keep the original
  458. // value.
  459. if (value != *existingValue || this->AlreadyInCacheWithoutMetaInfo) {
  460. this->Makefile->GetCMakeInstance()->AddCacheEntry(
  461. this->VariableName, value, this->VariableDocumentation,
  462. this->VariableType);
  463. if (this->Makefile->GetPolicyStatus(cmPolicies::CMP0126) ==
  464. cmPolicies::NEW) {
  465. if (this->Makefile->IsNormalDefinitionSet(this->VariableName)) {
  466. this->Makefile->AddDefinition(this->VariableName, value);
  467. }
  468. } else {
  469. // if there was a definition then remove it
  470. // This is required to ensure same behavior as
  471. // cmMakefile::AddCacheDefinition.
  472. this->Makefile->RemoveDefinition(this->VariableName);
  473. }
  474. }
  475. } else {
  476. // ensure a normal variable is defined.
  477. this->Makefile->AddDefinition(this->VariableName, value);
  478. }
  479. } else {
  480. // If the user specifies the entry on the command line without a
  481. // type we should add the type and docstring but keep the original
  482. // value.
  483. if (this->StoreResultInCache) {
  484. if (this->AlreadyInCacheWithoutMetaInfo) {
  485. this->Makefile->AddCacheDefinition(this->VariableName, "",
  486. this->VariableDocumentation,
  487. this->VariableType);
  488. if (this->Makefile->GetPolicyStatus(cmPolicies::CMP0126) ==
  489. cmPolicies::NEW &&
  490. this->Makefile->IsNormalDefinitionSet(this->VariableName)) {
  491. this->Makefile->AddDefinition(
  492. this->VariableName,
  493. *this->Makefile->GetCMakeInstance()->GetCacheDefinition(
  494. this->VariableName));
  495. }
  496. }
  497. } else {
  498. // ensure a normal variable is defined.
  499. this->Makefile->AddDefinition(
  500. this->VariableName,
  501. this->Makefile->GetSafeDefinition(this->VariableName));
  502. }
  503. }
  504. }
  505. void cmFindBase::StoreFindResult(const std::string& value)
  506. {
  507. bool force =
  508. this->Makefile->GetPolicyStatus(cmPolicies::CMP0125) == cmPolicies::NEW;
  509. bool updateNormalVariable =
  510. this->Makefile->GetPolicyStatus(cmPolicies::CMP0126) == cmPolicies::NEW;
  511. if (!value.empty()) {
  512. if (this->StoreResultInCache) {
  513. this->Makefile->AddCacheDefinition(this->VariableName, value,
  514. this->VariableDocumentation,
  515. this->VariableType, force);
  516. if (updateNormalVariable &&
  517. this->Makefile->IsNormalDefinitionSet(this->VariableName)) {
  518. this->Makefile->AddDefinition(this->VariableName, value);
  519. }
  520. } else {
  521. this->Makefile->AddDefinition(this->VariableName, value);
  522. }
  523. return;
  524. }
  525. auto notFound = cmStrCat(this->VariableName, "-NOTFOUND");
  526. if (this->StoreResultInCache) {
  527. this->Makefile->AddCacheDefinition(this->VariableName, notFound,
  528. this->VariableDocumentation,
  529. this->VariableType, force);
  530. if (updateNormalVariable &&
  531. this->Makefile->IsNormalDefinitionSet(this->VariableName)) {
  532. this->Makefile->AddDefinition(this->VariableName, notFound);
  533. }
  534. } else {
  535. this->Makefile->AddDefinition(this->VariableName, notFound);
  536. }
  537. if (this->Required) {
  538. this->Makefile->IssueMessage(
  539. MessageType::FATAL_ERROR,
  540. cmStrCat("Could not find ", this->VariableName, " using the following ",
  541. (this->FindCommandName == "find_file" ||
  542. this->FindCommandName == "find_path"
  543. ? "files"
  544. : "names"),
  545. ": ", cmJoin(this->Names, ", ")));
  546. cmSystemTools::SetFatalErrorOccurred();
  547. }
  548. }
  549. cmFindBaseDebugState::cmFindBaseDebugState(std::string commandName,
  550. cmFindBase const* findBase)
  551. : FindCommand(findBase)
  552. , CommandName(std::move(commandName))
  553. {
  554. }
  555. cmFindBaseDebugState::~cmFindBaseDebugState()
  556. {
  557. if (this->FindCommand->DebugMode) {
  558. std::string buffer =
  559. cmStrCat(this->CommandName, " called with the following settings:\n");
  560. buffer += cmStrCat(" VAR: ", this->FindCommand->VariableName, "\n");
  561. buffer += cmStrCat(
  562. " NAMES: ", cmWrap("\"", this->FindCommand->Names, "\"", "\n "),
  563. "\n");
  564. buffer += cmStrCat(
  565. " Documentation: ", this->FindCommand->VariableDocumentation, "\n");
  566. buffer += " Framework\n";
  567. buffer += cmStrCat(" Only Search Frameworks: ",
  568. 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");
  580. if (this->FindCommand->NoDefaultPath) {
  581. buffer += " NO_DEFAULT_PATH Enabled\n";
  582. } else {
  583. buffer += cmStrCat(
  584. " CMAKE_FIND_USE_CMAKE_PATH: ", !this->FindCommand->NoCMakePath, "\n",
  585. " CMAKE_FIND_USE_CMAKE_ENVIRONMENT_PATH: ",
  586. !this->FindCommand->NoCMakeEnvironmentPath, "\n",
  587. " CMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH: ",
  588. !this->FindCommand->NoSystemEnvironmentPath, "\n",
  589. " CMAKE_FIND_USE_CMAKE_SYSTEM_PATH: ",
  590. !this->FindCommand->NoCMakeSystemPath, "\n",
  591. " CMAKE_FIND_USE_INSTALL_PREFIX: ",
  592. !this->FindCommand->NoCMakeInstallPath, "\n");
  593. }
  594. buffer +=
  595. cmStrCat(this->CommandName, " considered the following locations:\n");
  596. for (auto const& state : this->FailedSearchLocations) {
  597. std::string path = cmStrCat(" ", state.path);
  598. if (!state.regexName.empty()) {
  599. path = cmStrCat(path, "/", state.regexName);
  600. }
  601. buffer += cmStrCat(path, "\n");
  602. }
  603. if (!this->FoundSearchLocation.path.empty()) {
  604. buffer += cmStrCat("The item was found at\n ",
  605. this->FoundSearchLocation.path, "\n");
  606. } else {
  607. buffer += "The item was not found.\n";
  608. }
  609. this->FindCommand->DebugMessage(buffer);
  610. }
  611. }
  612. void cmFindBaseDebugState::FoundAt(std::string const& path,
  613. std::string regexName)
  614. {
  615. if (this->FindCommand->DebugMode) {
  616. this->FoundSearchLocation = DebugLibState{ std::move(regexName), path };
  617. }
  618. }
  619. void cmFindBaseDebugState::FailedAt(std::string const& path,
  620. std::string regexName)
  621. {
  622. if (this->FindCommand->DebugMode) {
  623. this->FailedSearchLocations.emplace_back(std::move(regexName), path);
  624. }
  625. }