cmFindBase.cxx 23 KB

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