cmFindBase.cxx 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  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. enum Doing
  88. {
  89. DoingNone,
  90. DoingNames,
  91. DoingPaths,
  92. DoingPathSuffixes,
  93. DoingHints
  94. };
  95. Doing doing = DoingNames; // assume it starts with a name
  96. for (unsigned int j = 1; j < args.size(); ++j) {
  97. if (args[j] == "NAMES") {
  98. doing = DoingNames;
  99. newStyle = true;
  100. } else if (args[j] == "PATHS") {
  101. doing = DoingPaths;
  102. newStyle = true;
  103. } else if (args[j] == "HINTS") {
  104. doing = DoingHints;
  105. newStyle = true;
  106. } else if (args[j] == "PATH_SUFFIXES") {
  107. doing = DoingPathSuffixes;
  108. newStyle = true;
  109. } else if (args[j] == "NAMES_PER_DIR") {
  110. doing = DoingNone;
  111. if (this->NamesPerDirAllowed) {
  112. this->NamesPerDir = true;
  113. } else {
  114. this->SetError("does not support NAMES_PER_DIR");
  115. return false;
  116. }
  117. } else if (args[j] == "NO_SYSTEM_PATH") {
  118. doing = DoingNone;
  119. this->NoDefaultPath = true;
  120. } else if (args[j] == "REQUIRED") {
  121. doing = DoingNone;
  122. this->Required = true;
  123. newStyle = true;
  124. } else if (args[j] == "REGISTRY_VIEW") {
  125. if (++j == args.size()) {
  126. this->SetError("missing required argument for REGISTRY_VIEW");
  127. return false;
  128. }
  129. auto view = cmWindowsRegistry::ToView(args[j]);
  130. if (view) {
  131. this->RegistryView = *view;
  132. } else {
  133. this->SetError(
  134. cmStrCat("given invalid value for REGISTRY_VIEW: ", args[j]));
  135. return false;
  136. }
  137. } else if (args[j] == "VALIDATOR") {
  138. if (++j == args.size()) {
  139. this->SetError("missing required argument for VALIDATOR");
  140. return false;
  141. }
  142. auto command = this->Makefile->GetState()->GetCommand(args[j]);
  143. if (!command) {
  144. this->SetError(cmStrCat(
  145. "command specified for VALIDATOR is undefined: ", args[j], '.'));
  146. return false;
  147. }
  148. // ensure a macro is not specified as validator
  149. const auto& validatorName = args[j];
  150. cmList macros{ this->Makefile->GetProperty("MACROS") };
  151. if (std::find_if(macros.begin(), macros.end(),
  152. [&validatorName](const std::string& item) {
  153. return cmSystemTools::Strucmp(validatorName.c_str(),
  154. item.c_str()) == 0;
  155. }) != macros.end()) {
  156. this->SetError(cmStrCat(
  157. "command specified for VALIDATOR is not a function: ", args[j],
  158. '.'));
  159. return false;
  160. }
  161. this->ValidatorName = args[j];
  162. } else if (this->CheckCommonArgument(args[j])) {
  163. doing = DoingNone;
  164. } else {
  165. // Some common arguments were accidentally supported by CMake
  166. // 2.4 and 2.6.0 in the short-hand form of the command, so we
  167. // must support it even though it is not documented.
  168. if (doing == DoingNames) {
  169. this->Names.push_back(args[j]);
  170. } else if (doing == DoingPaths) {
  171. this->UserGuessArgs.push_back(args[j]);
  172. } else if (doing == DoingHints) {
  173. this->UserHintsArgs.push_back(args[j]);
  174. } else if (doing == DoingPathSuffixes) {
  175. this->AddPathSuffix(args[j]);
  176. }
  177. }
  178. }
  179. if (this->VariableDocumentation.empty()) {
  180. this->VariableDocumentation = "Where can ";
  181. if (this->Names.empty()) {
  182. this->VariableDocumentation += "the (unknown) library be found";
  183. } else if (this->Names.size() == 1) {
  184. this->VariableDocumentation +=
  185. "the " + this->Names.front() + " library be found";
  186. } else {
  187. this->VariableDocumentation += "one of the ";
  188. this->VariableDocumentation +=
  189. cmJoin(cmMakeRange(this->Names).retreat(1), ", ");
  190. this->VariableDocumentation +=
  191. " or " + this->Names.back() + " libraries be found";
  192. }
  193. }
  194. // look for old style
  195. // FIND_*(VAR name path1 path2 ...)
  196. if (!newStyle && !this->Names.empty()) {
  197. // All the short-hand arguments have been recorded as names.
  198. std::vector<std::string> shortArgs = this->Names;
  199. this->Names.clear(); // clear out any values in Names
  200. this->Names.push_back(shortArgs[0]);
  201. cm::append(this->UserGuessArgs, shortArgs.begin() + 1, shortArgs.end());
  202. }
  203. this->ExpandPaths();
  204. this->ComputeFinalPaths(IgnorePaths::Yes);
  205. return true;
  206. }
  207. bool cmFindBase::Validate(const std::string& path) const
  208. {
  209. if (this->ValidatorName.empty()) {
  210. return true;
  211. }
  212. // The validator command will be executed in an isolated scope.
  213. cmMakefile::ScopePushPop varScope(this->Makefile);
  214. cmMakefile::PolicyPushPop polScope(this->Makefile);
  215. static_cast<void>(varScope);
  216. static_cast<void>(polScope);
  217. auto resultName =
  218. cmStrCat("CMAKE_"_s, cmSystemTools::UpperCase(this->FindCommandName),
  219. "_VALIDATOR_STATUS"_s);
  220. this->Makefile->AddDefinitionBool(resultName, true);
  221. cmListFileFunction validator(
  222. this->ValidatorName, 0, 0,
  223. { cmListFileArgument(resultName, cmListFileArgument::Unquoted, 0),
  224. cmListFileArgument(path, cmListFileArgument::Quoted, 0) });
  225. cmExecutionStatus status(*this->Makefile);
  226. if (this->Makefile->ExecuteCommand(validator, status)) {
  227. return this->Makefile->GetDefinition(resultName).IsOn();
  228. }
  229. return false;
  230. }
  231. void cmFindBase::ExpandPaths()
  232. {
  233. if (!this->NoDefaultPath) {
  234. if (!this->NoPackageRootPath) {
  235. this->FillPackageRootPath();
  236. }
  237. if (!this->NoCMakePath) {
  238. this->FillCMakeVariablePath();
  239. }
  240. if (!this->NoCMakeEnvironmentPath) {
  241. this->FillCMakeEnvironmentPath();
  242. }
  243. }
  244. this->FillUserHintsPath();
  245. if (!this->NoDefaultPath) {
  246. if (!this->NoSystemEnvironmentPath) {
  247. this->FillSystemEnvironmentPath();
  248. }
  249. if (!this->NoCMakeSystemPath) {
  250. this->FillCMakeSystemVariablePath();
  251. }
  252. }
  253. this->FillUserGuessPath();
  254. }
  255. void cmFindBase::FillCMakeEnvironmentPath()
  256. {
  257. cmSearchPath& paths = this->LabeledPaths[PathLabel::CMakeEnvironment];
  258. // Add CMAKE_*_PATH environment variables
  259. std::string var = cmStrCat("CMAKE_", this->CMakePathName, "_PATH");
  260. paths.AddEnvPrefixPath("CMAKE_PREFIX_PATH");
  261. paths.AddEnvPath(var);
  262. if (this->CMakePathName == "PROGRAM") {
  263. paths.AddEnvPath("CMAKE_APPBUNDLE_PATH");
  264. } else {
  265. paths.AddEnvPath("CMAKE_FRAMEWORK_PATH");
  266. }
  267. paths.AddSuffixes(this->SearchPathSuffixes);
  268. }
  269. void cmFindBase::FillPackageRootPath()
  270. {
  271. cmSearchPath& paths = this->LabeledPaths[PathLabel::PackageRoot];
  272. // Add the PACKAGE_ROOT_PATH from each enclosing find_package call.
  273. for (std::vector<std::string> const& pkgPaths :
  274. cmReverseRange(this->Makefile->FindPackageRootPathStack)) {
  275. paths.AddPrefixPaths(pkgPaths);
  276. }
  277. paths.AddSuffixes(this->SearchPathSuffixes);
  278. }
  279. void cmFindBase::FillCMakeVariablePath()
  280. {
  281. cmSearchPath& paths = this->LabeledPaths[PathLabel::CMake];
  282. // Add CMake variables of the same name as the previous environment
  283. // variables CMAKE_*_PATH to be used most of the time with -D
  284. // command line options
  285. std::string var = cmStrCat("CMAKE_", this->CMakePathName, "_PATH");
  286. paths.AddCMakePrefixPath("CMAKE_PREFIX_PATH");
  287. paths.AddCMakePath(var);
  288. if (this->CMakePathName == "PROGRAM") {
  289. paths.AddCMakePath("CMAKE_APPBUNDLE_PATH");
  290. } else {
  291. paths.AddCMakePath("CMAKE_FRAMEWORK_PATH");
  292. }
  293. paths.AddSuffixes(this->SearchPathSuffixes);
  294. }
  295. void cmFindBase::FillSystemEnvironmentPath()
  296. {
  297. cmSearchPath& paths = this->LabeledPaths[PathLabel::SystemEnvironment];
  298. // Add LIB or INCLUDE
  299. if (!this->EnvironmentPath.empty()) {
  300. paths.AddEnvPath(this->EnvironmentPath);
  301. }
  302. // Add PATH
  303. paths.AddEnvPath("PATH");
  304. paths.AddSuffixes(this->SearchPathSuffixes);
  305. }
  306. namespace {
  307. struct entry_to_remove
  308. {
  309. entry_to_remove(std::string const& name, cmMakefile* makefile)
  310. {
  311. if (cmValue to_skip = makefile->GetDefinition(
  312. cmStrCat("_CMAKE_SYSTEM_PREFIX_PATH_", name, "_PREFIX_COUNT"))) {
  313. cmStrToLong(*to_skip, &count);
  314. }
  315. if (cmValue prefix_value = makefile->GetDefinition(
  316. cmStrCat("_CMAKE_SYSTEM_PREFIX_PATH_", name, "_PREFIX_VALUE"))) {
  317. value = *prefix_value;
  318. }
  319. }
  320. bool valid() const { return count > 0 && !value.empty(); }
  321. void remove_self(std::vector<std::string>& entries) const
  322. {
  323. if (this->valid()) {
  324. long to_skip = this->count;
  325. long index_to_remove = 0;
  326. for (const auto& path : entries) {
  327. if (path == this->value && --to_skip == 0) {
  328. break;
  329. }
  330. ++index_to_remove;
  331. }
  332. entries.erase(entries.begin() + index_to_remove);
  333. }
  334. }
  335. long count = -1;
  336. std::string value;
  337. };
  338. }
  339. void cmFindBase::FillCMakeSystemVariablePath()
  340. {
  341. cmSearchPath& paths = this->LabeledPaths[PathLabel::CMakeSystem];
  342. const bool install_prefix_in_list =
  343. !this->Makefile->IsOn("CMAKE_FIND_NO_INSTALL_PREFIX");
  344. const bool remove_install_prefix = this->NoCMakeInstallPath;
  345. const bool add_install_prefix = !this->NoCMakeInstallPath &&
  346. this->Makefile->IsDefinitionSet("CMAKE_FIND_USE_INSTALL_PREFIX");
  347. // We have 3 possible states for `CMAKE_SYSTEM_PREFIX_PATH` and
  348. // `CMAKE_INSTALL_PREFIX`.
  349. // Either we need to remove `CMAKE_INSTALL_PREFIX`, add
  350. // `CMAKE_INSTALL_PREFIX`, or do nothing.
  351. //
  352. // When we need to remove `CMAKE_INSTALL_PREFIX` we remove the Nth occurrence
  353. // of `CMAKE_INSTALL_PREFIX` from `CMAKE_SYSTEM_PREFIX_PATH`, where `N` is
  354. // computed by `CMakeSystemSpecificInformation.cmake` while constructing
  355. // `CMAKE_SYSTEM_PREFIX_PATH`. This ensures that if projects / toolchains
  356. // have removed `CMAKE_INSTALL_PREFIX` from the list, we don't remove
  357. // some other entry by mistake ( likewise for `CMAKE_STAGING_PREFIX` )
  358. entry_to_remove install_entry("INSTALL", this->Makefile);
  359. entry_to_remove staging_entry("STAGING", this->Makefile);
  360. if (remove_install_prefix && install_prefix_in_list &&
  361. (install_entry.valid() || staging_entry.valid())) {
  362. cmValue prefix_paths =
  363. this->Makefile->GetDefinition("CMAKE_SYSTEM_PREFIX_PATH");
  364. // remove entries from CMAKE_SYSTEM_PREFIX_PATH
  365. cmList expanded{ *prefix_paths };
  366. install_entry.remove_self(expanded);
  367. staging_entry.remove_self(expanded);
  368. for (std::string& p : expanded) {
  369. p = cmSystemTools::CollapseFullPath(
  370. p, this->Makefile->GetCurrentSourceDirectory());
  371. }
  372. paths.AddPrefixPaths(expanded);
  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. if (!cmSystemTools::FileExists(value, false)) {
  451. value = *existingValue;
  452. }
  453. }
  454. if (this->StoreResultInCache) {
  455. // If the user specifies the entry on the command line without a
  456. // type we should add the type and docstring but keep the original
  457. // value.
  458. if (value != *existingValue || this->AlreadyInCacheWithoutMetaInfo) {
  459. this->Makefile->GetCMakeInstance()->AddCacheEntry(
  460. this->VariableName, value, this->VariableDocumentation,
  461. this->VariableType);
  462. if (this->Makefile->GetPolicyStatus(cmPolicies::CMP0126) ==
  463. cmPolicies::NEW) {
  464. if (this->Makefile->IsNormalDefinitionSet(this->VariableName)) {
  465. this->Makefile->AddDefinition(this->VariableName, value);
  466. }
  467. } else {
  468. // if there was a definition then remove it
  469. // This is required to ensure same behavior as
  470. // cmMakefile::AddCacheDefinition.
  471. this->Makefile->RemoveDefinition(this->VariableName);
  472. }
  473. }
  474. } else {
  475. // ensure a normal variable is defined.
  476. this->Makefile->AddDefinition(this->VariableName, value);
  477. }
  478. } else {
  479. // If the user specifies the entry on the command line without a
  480. // type we should add the type and docstring but keep the original
  481. // value.
  482. if (this->StoreResultInCache) {
  483. if (this->AlreadyInCacheWithoutMetaInfo) {
  484. this->Makefile->AddCacheDefinition(this->VariableName, "",
  485. this->VariableDocumentation,
  486. this->VariableType);
  487. if (this->Makefile->GetPolicyStatus(cmPolicies::CMP0126) ==
  488. cmPolicies::NEW &&
  489. this->Makefile->IsNormalDefinitionSet(this->VariableName)) {
  490. this->Makefile->AddDefinition(
  491. this->VariableName,
  492. *this->Makefile->GetCMakeInstance()->GetCacheDefinition(
  493. this->VariableName));
  494. }
  495. }
  496. } else {
  497. // ensure a normal variable is defined.
  498. this->Makefile->AddDefinition(
  499. this->VariableName,
  500. this->Makefile->GetSafeDefinition(this->VariableName));
  501. }
  502. }
  503. }
  504. void cmFindBase::StoreFindResult(const std::string& value)
  505. {
  506. bool force =
  507. this->Makefile->GetPolicyStatus(cmPolicies::CMP0125) == cmPolicies::NEW;
  508. bool updateNormalVariable =
  509. this->Makefile->GetPolicyStatus(cmPolicies::CMP0126) == cmPolicies::NEW;
  510. if (!value.empty()) {
  511. if (this->StoreResultInCache) {
  512. this->Makefile->AddCacheDefinition(this->VariableName, value,
  513. this->VariableDocumentation,
  514. this->VariableType, force);
  515. if (updateNormalVariable &&
  516. this->Makefile->IsNormalDefinitionSet(this->VariableName)) {
  517. this->Makefile->AddDefinition(this->VariableName, value);
  518. }
  519. } else {
  520. this->Makefile->AddDefinition(this->VariableName, value);
  521. }
  522. return;
  523. }
  524. auto notFound = cmStrCat(this->VariableName, "-NOTFOUND");
  525. if (this->StoreResultInCache) {
  526. this->Makefile->AddCacheDefinition(this->VariableName, notFound,
  527. this->VariableDocumentation,
  528. this->VariableType, force);
  529. if (updateNormalVariable &&
  530. this->Makefile->IsNormalDefinitionSet(this->VariableName)) {
  531. this->Makefile->AddDefinition(this->VariableName, notFound);
  532. }
  533. } else {
  534. this->Makefile->AddDefinition(this->VariableName, notFound);
  535. }
  536. if (this->Required) {
  537. this->Makefile->IssueMessage(
  538. MessageType::FATAL_ERROR,
  539. cmStrCat("Could not find ", this->VariableName, " using the following ",
  540. (this->FindCommandName == "find_file" ||
  541. this->FindCommandName == "find_path"
  542. ? "files"
  543. : "names"),
  544. ": ", cmJoin(this->Names, ", ")));
  545. cmSystemTools::SetFatalErrorOccurred();
  546. }
  547. }
  548. cmFindBaseDebugState::cmFindBaseDebugState(std::string commandName,
  549. cmFindBase const* findBase)
  550. : FindCommand(findBase)
  551. , CommandName(std::move(commandName))
  552. {
  553. }
  554. cmFindBaseDebugState::~cmFindBaseDebugState()
  555. {
  556. if (!this->FindCommand->DebugMode) {
  557. return;
  558. }
  559. // clang-format off
  560. auto buffer =
  561. cmStrCat(
  562. this->CommandName, " called with the following settings:"
  563. "\n VAR: ", this->FindCommand->VariableName,
  564. "\n NAMES: ", cmWrap('"', this->FindCommand->Names, '"', "\n "),
  565. "\n Documentation: ", this->FindCommand->VariableDocumentation,
  566. "\n Framework"
  567. "\n Only Search Frameworks: ", this->FindCommand->SearchFrameworkOnly,
  568. "\n Search Frameworks Last: ", this->FindCommand->SearchFrameworkLast,
  569. "\n Search Frameworks First: ", this->FindCommand->SearchFrameworkFirst,
  570. "\n AppBundle"
  571. "\n Only Search AppBundle: ", this->FindCommand->SearchAppBundleOnly,
  572. "\n Search AppBundle Last: ", this->FindCommand->SearchAppBundleLast,
  573. "\n Search AppBundle First: ", this->FindCommand->SearchAppBundleFirst,
  574. "\n"
  575. );
  576. // clang-format on
  577. if (this->FindCommand->NoDefaultPath) {
  578. buffer += " NO_DEFAULT_PATH Enabled\n";
  579. } else {
  580. // clang-format off
  581. buffer += cmStrCat(
  582. " CMAKE_FIND_USE_CMAKE_PATH: ", !this->FindCommand->NoCMakePath,
  583. "\n CMAKE_FIND_USE_CMAKE_ENVIRONMENT_PATH: ", !this->FindCommand->NoCMakeEnvironmentPath,
  584. "\n CMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH: ", !this->FindCommand->NoSystemEnvironmentPath,
  585. "\n CMAKE_FIND_USE_CMAKE_SYSTEM_PATH: ", !this->FindCommand->NoCMakeSystemPath,
  586. "\n CMAKE_FIND_USE_INSTALL_PREFIX: ", !this->FindCommand->NoCMakeInstallPath,
  587. "\n"
  588. );
  589. // clang-format on
  590. }
  591. buffer +=
  592. cmStrCat(this->CommandName, " considered the following locations:\n");
  593. for (auto const& state : this->FailedSearchLocations) {
  594. std::string path = cmStrCat(" ", state.path);
  595. if (!state.regexName.empty()) {
  596. path = cmStrCat(path, '/', state.regexName);
  597. }
  598. buffer += cmStrCat(path, '\n');
  599. }
  600. if (!this->FoundSearchLocation.path.empty()) {
  601. buffer += cmStrCat("The item was found at\n ",
  602. this->FoundSearchLocation.path, '\n');
  603. } else {
  604. buffer += "The item was not found.\n";
  605. }
  606. this->FindCommand->DebugMessage(buffer);
  607. }
  608. void cmFindBaseDebugState::FoundAt(std::string const& path,
  609. std::string regexName)
  610. {
  611. if (this->FindCommand->DebugMode) {
  612. this->FoundSearchLocation = DebugLibState{ std::move(regexName), path };
  613. }
  614. }
  615. void cmFindBaseDebugState::FailedAt(std::string const& path,
  616. std::string regexName)
  617. {
  618. if (this->FindCommand->DebugMode) {
  619. this->FailedSearchLocations.emplace_back(std::move(regexName), path);
  620. }
  621. }