cmFindBase.cxx 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  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 <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. cmSystemTools::GetPath(args, argsIn[j].c_str());
  54. }
  55. } else {
  56. args.push_back(argsIn[j]);
  57. }
  58. } else {
  59. if (j + 1 < size) {
  60. foundDoc = true;
  61. this->VariableDocumentation = argsIn[j + 1];
  62. j++;
  63. if (j >= size) {
  64. break;
  65. }
  66. }
  67. }
  68. }
  69. if (args.size() < 2) {
  70. this->SetError("called with incorrect number of arguments");
  71. return false;
  72. }
  73. this->VariableName = args[0];
  74. if (this->CheckForVariableDefined()) {
  75. this->AlreadyDefined = true;
  76. return true;
  77. }
  78. // Find what search path locations have been enabled/disable
  79. this->SelectDefaultSearchModes();
  80. // Find the current root path mode.
  81. this->SelectDefaultRootPathMode();
  82. // Find the current bundle/framework search policy.
  83. this->SelectDefaultMacMode();
  84. bool newStyle = false;
  85. enum Doing
  86. {
  87. DoingNone,
  88. DoingNames,
  89. DoingPaths,
  90. DoingPathSuffixes,
  91. DoingHints
  92. };
  93. Doing doing = DoingNames; // assume it starts with a name
  94. for (unsigned int j = 1; j < args.size(); ++j) {
  95. if (args[j] == "NAMES") {
  96. doing = DoingNames;
  97. newStyle = true;
  98. } else if (args[j] == "PATHS") {
  99. doing = DoingPaths;
  100. newStyle = true;
  101. } else if (args[j] == "HINTS") {
  102. doing = DoingHints;
  103. newStyle = true;
  104. } else if (args[j] == "PATH_SUFFIXES") {
  105. doing = DoingPathSuffixes;
  106. newStyle = true;
  107. } else if (args[j] == "NAMES_PER_DIR") {
  108. doing = DoingNone;
  109. if (this->NamesPerDirAllowed) {
  110. this->NamesPerDir = true;
  111. } else {
  112. this->SetError("does not support NAMES_PER_DIR");
  113. return false;
  114. }
  115. } else if (args[j] == "NO_SYSTEM_PATH") {
  116. doing = DoingNone;
  117. this->NoDefaultPath = true;
  118. } else if (args[j] == "REQUIRED") {
  119. doing = DoingNone;
  120. this->Required = true;
  121. newStyle = true;
  122. } else if (args[j] == "REGISTRY_VIEW") {
  123. if (++j == args.size()) {
  124. this->SetError("missing required argument for \"REGISTRY_VIEW\"");
  125. return false;
  126. }
  127. auto view = cmWindowsRegistry::ToView(args[j]);
  128. if (view) {
  129. this->RegistryView = *view;
  130. } else {
  131. this->SetError(
  132. cmStrCat("given invalid value for \"REGISTRY_VIEW\": ", args[j]));
  133. return false;
  134. }
  135. } else if (args[j] == "VALIDATOR") {
  136. if (++j == args.size()) {
  137. this->SetError("missing required argument for \"VALIDATOR\"");
  138. return false;
  139. }
  140. auto command = this->Makefile->GetState()->GetCommand(args[j]);
  141. if (command == nullptr) {
  142. this->SetError(cmStrCat(
  143. "command specified for \"VALIDATOR\" is undefined: ", args[j], '.'));
  144. return false;
  145. }
  146. // ensure a macro is not specified as validator
  147. const auto& validatorName = args[j];
  148. cmList macros{ this->Makefile->GetProperty("MACROS") };
  149. if (std::find_if(macros.begin(), macros.end(),
  150. [&validatorName](const std::string& item) {
  151. return cmSystemTools::Strucmp(validatorName.c_str(),
  152. item.c_str()) == 0;
  153. }) != macros.end()) {
  154. this->SetError(cmStrCat(
  155. "command specified for \"VALIDATOR\" is not a function: ", args[j],
  156. '.'));
  157. return false;
  158. }
  159. this->ValidatorName = args[j];
  160. } else if (this->CheckCommonArgument(args[j])) {
  161. doing = DoingNone;
  162. } else {
  163. // Some common arguments were accidentally supported by CMake
  164. // 2.4 and 2.6.0 in the short-hand form of the command, so we
  165. // must support it even though it is not documented.
  166. if (doing == DoingNames) {
  167. this->Names.push_back(args[j]);
  168. } else if (doing == DoingPaths) {
  169. this->UserGuessArgs.push_back(args[j]);
  170. } else if (doing == DoingHints) {
  171. this->UserHintsArgs.push_back(args[j]);
  172. } else if (doing == DoingPathSuffixes) {
  173. this->AddPathSuffix(args[j]);
  174. }
  175. }
  176. }
  177. if (this->VariableDocumentation.empty()) {
  178. this->VariableDocumentation = "Where can ";
  179. if (this->Names.empty()) {
  180. this->VariableDocumentation += "the (unknown) library be found";
  181. } else if (this->Names.size() == 1) {
  182. this->VariableDocumentation +=
  183. "the " + this->Names.front() + " library be found";
  184. } else {
  185. this->VariableDocumentation += "one of the ";
  186. this->VariableDocumentation +=
  187. cmJoin(cmMakeRange(this->Names).retreat(1), ", ");
  188. this->VariableDocumentation +=
  189. " or " + this->Names.back() + " libraries be found";
  190. }
  191. }
  192. // look for old style
  193. // FIND_*(VAR name path1 path2 ...)
  194. if (!newStyle && !this->Names.empty()) {
  195. // All the short-hand arguments have been recorded as names.
  196. std::vector<std::string> shortArgs = this->Names;
  197. this->Names.clear(); // clear out any values in Names
  198. this->Names.push_back(shortArgs[0]);
  199. cm::append(this->UserGuessArgs, shortArgs.begin() + 1, shortArgs.end());
  200. }
  201. this->ExpandPaths();
  202. this->ComputeFinalPaths(IgnorePaths::Yes);
  203. return true;
  204. }
  205. bool cmFindBase::Validate(const std::string& path) const
  206. {
  207. if (this->ValidatorName.empty()) {
  208. return true;
  209. }
  210. // The validator command will be executed in an isolated scope.
  211. cmMakefile::ScopePushPop varScope(this->Makefile);
  212. cmMakefile::PolicyPushPop polScope(this->Makefile);
  213. static_cast<void>(varScope);
  214. static_cast<void>(polScope);
  215. auto resultName =
  216. cmStrCat("CMAKE_"_s, cmSystemTools::UpperCase(this->FindCommandName),
  217. "_VALIDATOR_STATUS"_s);
  218. this->Makefile->AddDefinitionBool(resultName, true);
  219. cmListFileFunction validator(
  220. this->ValidatorName, 0, 0,
  221. { cmListFileArgument(resultName, cmListFileArgument::Unquoted, 0),
  222. cmListFileArgument(path, cmListFileArgument::Quoted, 0) });
  223. cmExecutionStatus status(*this->Makefile);
  224. if (this->Makefile->ExecuteCommand(validator, status)) {
  225. return this->Makefile->GetDefinition(resultName).IsOn();
  226. }
  227. return false;
  228. }
  229. void cmFindBase::ExpandPaths()
  230. {
  231. if (!this->NoDefaultPath) {
  232. if (!this->NoPackageRootPath) {
  233. this->FillPackageRootPath();
  234. }
  235. if (!this->NoCMakePath) {
  236. this->FillCMakeVariablePath();
  237. }
  238. if (!this->NoCMakeEnvironmentPath) {
  239. this->FillCMakeEnvironmentPath();
  240. }
  241. }
  242. this->FillUserHintsPath();
  243. if (!this->NoDefaultPath) {
  244. if (!this->NoSystemEnvironmentPath) {
  245. this->FillSystemEnvironmentPath();
  246. }
  247. if (!this->NoCMakeSystemPath) {
  248. this->FillCMakeSystemVariablePath();
  249. }
  250. }
  251. this->FillUserGuessPath();
  252. }
  253. void cmFindBase::FillCMakeEnvironmentPath()
  254. {
  255. cmSearchPath& paths = this->LabeledPaths[PathLabel::CMakeEnvironment];
  256. // Add CMAKE_*_PATH environment variables
  257. std::string var = cmStrCat("CMAKE_", this->CMakePathName, "_PATH");
  258. paths.AddEnvPrefixPath("CMAKE_PREFIX_PATH");
  259. paths.AddEnvPath(var);
  260. if (this->CMakePathName == "PROGRAM") {
  261. paths.AddEnvPath("CMAKE_APPBUNDLE_PATH");
  262. } else {
  263. paths.AddEnvPath("CMAKE_FRAMEWORK_PATH");
  264. }
  265. paths.AddSuffixes(this->SearchPathSuffixes);
  266. }
  267. void cmFindBase::FillPackageRootPath()
  268. {
  269. cmSearchPath& paths = this->LabeledPaths[PathLabel::PackageRoot];
  270. // Add the PACKAGE_ROOT_PATH from each enclosing find_package call.
  271. for (std::vector<std::string> const& pkgPaths :
  272. cmReverseRange(this->Makefile->FindPackageRootPathStack)) {
  273. paths.AddPrefixPaths(pkgPaths);
  274. }
  275. paths.AddSuffixes(this->SearchPathSuffixes);
  276. }
  277. void cmFindBase::FillCMakeVariablePath()
  278. {
  279. cmSearchPath& paths = this->LabeledPaths[PathLabel::CMake];
  280. // Add CMake variables of the same name as the previous environment
  281. // variables CMAKE_*_PATH to be used most of the time with -D
  282. // command line options
  283. std::string var = cmStrCat("CMAKE_", this->CMakePathName, "_PATH");
  284. paths.AddCMakePrefixPath("CMAKE_PREFIX_PATH");
  285. paths.AddCMakePath(var);
  286. if (this->CMakePathName == "PROGRAM") {
  287. paths.AddCMakePath("CMAKE_APPBUNDLE_PATH");
  288. } else {
  289. paths.AddCMakePath("CMAKE_FRAMEWORK_PATH");
  290. }
  291. paths.AddSuffixes(this->SearchPathSuffixes);
  292. }
  293. void cmFindBase::FillSystemEnvironmentPath()
  294. {
  295. cmSearchPath& paths = this->LabeledPaths[PathLabel::SystemEnvironment];
  296. // Add LIB or INCLUDE
  297. if (!this->EnvironmentPath.empty()) {
  298. paths.AddEnvPath(this->EnvironmentPath);
  299. #if defined(_WIN32) || defined(__CYGWIN__)
  300. paths.AddEnvPrefixPath("PATH", true);
  301. #endif
  302. }
  303. // Add PATH
  304. paths.AddEnvPath("PATH");
  305. paths.AddSuffixes(this->SearchPathSuffixes);
  306. }
  307. namespace {
  308. struct entry_to_remove
  309. {
  310. entry_to_remove(std::string const& name, cmMakefile* makefile)
  311. : value()
  312. {
  313. if (cmValue to_skip = makefile->GetDefinition(
  314. cmStrCat("_CMAKE_SYSTEM_PREFIX_PATH_", name, "_PREFIX_COUNT"))) {
  315. cmStrToLong(*to_skip, &count);
  316. }
  317. if (cmValue prefix_value = makefile->GetDefinition(
  318. cmStrCat("_CMAKE_SYSTEM_PREFIX_PATH_", name, "_PREFIX_VALUE"))) {
  319. value = *prefix_value;
  320. }
  321. }
  322. bool valid() const { return count > 0 && !value.empty(); }
  323. void remove_self(std::vector<std::string>& entries) const
  324. {
  325. if (this->valid()) {
  326. long to_skip = this->count;
  327. long index_to_remove = 0;
  328. for (const auto& path : entries) {
  329. if (path == this->value && --to_skip == 0) {
  330. break;
  331. }
  332. ++index_to_remove;
  333. }
  334. entries.erase(entries.begin() + index_to_remove);
  335. }
  336. }
  337. long count = -1;
  338. std::string value;
  339. };
  340. }
  341. void cmFindBase::FillCMakeSystemVariablePath()
  342. {
  343. cmSearchPath& paths = this->LabeledPaths[PathLabel::CMakeSystem];
  344. const bool install_prefix_in_list =
  345. !this->Makefile->IsOn("CMAKE_FIND_NO_INSTALL_PREFIX");
  346. const bool remove_install_prefix = this->NoCMakeInstallPath;
  347. const bool add_install_prefix = !this->NoCMakeInstallPath &&
  348. this->Makefile->IsDefinitionSet("CMAKE_FIND_USE_INSTALL_PREFIX");
  349. // We have 3 possible states for `CMAKE_SYSTEM_PREFIX_PATH` and
  350. // `CMAKE_INSTALL_PREFIX`.
  351. // Either we need to remove `CMAKE_INSTALL_PREFIX`, add
  352. // `CMAKE_INSTALL_PREFIX`, or do nothing.
  353. //
  354. // When we need to remove `CMAKE_INSTALL_PREFIX` we remove the Nth occurrence
  355. // of `CMAKE_INSTALL_PREFIX` from `CMAKE_SYSTEM_PREFIX_PATH`, where `N` is
  356. // computed by `CMakeSystemSpecificInformation.cmake` while constructing
  357. // `CMAKE_SYSTEM_PREFIX_PATH`. This ensures that if projects / toolchains
  358. // have removed `CMAKE_INSTALL_PREFIX` from the list, we don't remove
  359. // some other entry by mistake ( likewise for `CMAKE_STAGING_PREFIX` )
  360. entry_to_remove install_entry("INSTALL", this->Makefile);
  361. entry_to_remove staging_entry("STAGING", this->Makefile);
  362. if (remove_install_prefix && install_prefix_in_list &&
  363. (install_entry.valid() || staging_entry.valid())) {
  364. cmValue prefix_paths =
  365. this->Makefile->GetDefinition("CMAKE_SYSTEM_PREFIX_PATH");
  366. // remove entries from CMAKE_SYSTEM_PREFIX_PATH
  367. cmList expanded{ *prefix_paths };
  368. install_entry.remove_self(expanded);
  369. staging_entry.remove_self(expanded);
  370. paths.AddPrefixPaths(expanded,
  371. this->Makefile->GetCurrentSourceDirectory().c_str());
  372. } else if (add_install_prefix && !install_prefix_in_list) {
  373. paths.AddCMakePrefixPath("CMAKE_INSTALL_PREFIX");
  374. paths.AddCMakePrefixPath("CMAKE_STAGING_PREFIX");
  375. paths.AddCMakePrefixPath("CMAKE_SYSTEM_PREFIX_PATH");
  376. } else {
  377. // Otherwise the current setup of `CMAKE_SYSTEM_PREFIX_PATH` is correct
  378. paths.AddCMakePrefixPath("CMAKE_SYSTEM_PREFIX_PATH");
  379. }
  380. std::string var = cmStrCat("CMAKE_SYSTEM_", this->CMakePathName, "_PATH");
  381. paths.AddCMakePath(var);
  382. if (this->CMakePathName == "PROGRAM") {
  383. paths.AddCMakePath("CMAKE_SYSTEM_APPBUNDLE_PATH");
  384. } else {
  385. paths.AddCMakePath("CMAKE_SYSTEM_FRAMEWORK_PATH");
  386. }
  387. paths.AddSuffixes(this->SearchPathSuffixes);
  388. }
  389. void cmFindBase::FillUserHintsPath()
  390. {
  391. cmSearchPath& paths = this->LabeledPaths[PathLabel::Hints];
  392. for (std::string const& p : this->UserHintsArgs) {
  393. paths.AddUserPath(p);
  394. }
  395. paths.AddSuffixes(this->SearchPathSuffixes);
  396. }
  397. void cmFindBase::FillUserGuessPath()
  398. {
  399. cmSearchPath& paths = this->LabeledPaths[PathLabel::Guess];
  400. for (std::string const& p : this->UserGuessArgs) {
  401. paths.AddUserPath(p);
  402. }
  403. paths.AddSuffixes(this->SearchPathSuffixes);
  404. }
  405. bool cmFindBase::CheckForVariableDefined()
  406. {
  407. if (cmValue value = this->Makefile->GetDefinition(this->VariableName)) {
  408. cmState* state = this->Makefile->GetState();
  409. cmValue cacheEntry = state->GetCacheEntryValue(this->VariableName);
  410. bool found = !cmIsNOTFOUND(*value);
  411. bool cached = cacheEntry != nullptr;
  412. auto cacheType = cached ? state->GetCacheEntryType(this->VariableName)
  413. : cmStateEnums::UNINITIALIZED;
  414. if (cached && cacheType != cmStateEnums::UNINITIALIZED) {
  415. this->VariableType = cacheType;
  416. if (const auto& hs =
  417. state->GetCacheEntryProperty(this->VariableName, "HELPSTRING")) {
  418. this->VariableDocumentation = *hs;
  419. }
  420. }
  421. if (found) {
  422. // If the user specifies the entry on the command line without a
  423. // type we should add the type and docstring but keep the
  424. // original value. Tell the subclass implementations to do
  425. // this.
  426. if (cached && cacheType == cmStateEnums::UNINITIALIZED) {
  427. this->AlreadyInCacheWithoutMetaInfo = true;
  428. }
  429. return true;
  430. }
  431. }
  432. return false;
  433. }
  434. void cmFindBase::NormalizeFindResult()
  435. {
  436. if (this->Makefile->GetPolicyStatus(cmPolicies::CMP0125) ==
  437. cmPolicies::NEW) {
  438. // ensure the path returned by find_* command is absolute
  439. const auto& existingValue =
  440. this->Makefile->GetDefinition(this->VariableName);
  441. std::string value;
  442. if (!existingValue->empty()) {
  443. value =
  444. cmCMakePath(*existingValue, cmCMakePath::auto_format)
  445. .Absolute(cmCMakePath(
  446. this->Makefile->GetCMakeInstance()->GetCMakeWorkingDirectory()))
  447. .Normal()
  448. .GenericString();
  449. // value = cmSystemTools::CollapseFullPath(*existingValue);
  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. std::string buffer =
  558. cmStrCat(this->CommandName, " called with the following settings:\n");
  559. buffer += cmStrCat(" VAR: ", this->FindCommand->VariableName, "\n");
  560. buffer += cmStrCat(
  561. " NAMES: ", cmWrap("\"", this->FindCommand->Names, "\"", "\n "),
  562. "\n");
  563. buffer += cmStrCat(
  564. " Documentation: ", this->FindCommand->VariableDocumentation, "\n");
  565. buffer += " Framework\n";
  566. buffer += cmStrCat(" Only Search Frameworks: ",
  567. this->FindCommand->SearchFrameworkOnly, "\n");
  568. buffer += cmStrCat(" Search Frameworks Last: ",
  569. this->FindCommand->SearchFrameworkLast, "\n");
  570. buffer += cmStrCat(" Search Frameworks First: ",
  571. this->FindCommand->SearchFrameworkFirst, "\n");
  572. buffer += " AppBundle\n";
  573. buffer += cmStrCat(" Only Search AppBundle: ",
  574. this->FindCommand->SearchAppBundleOnly, "\n");
  575. buffer += cmStrCat(" Search AppBundle Last: ",
  576. this->FindCommand->SearchAppBundleLast, "\n");
  577. buffer += cmStrCat(" Search AppBundle First: ",
  578. this->FindCommand->SearchAppBundleFirst, "\n");
  579. if (this->FindCommand->NoDefaultPath) {
  580. buffer += " NO_DEFAULT_PATH Enabled\n";
  581. } else {
  582. buffer += cmStrCat(
  583. " CMAKE_FIND_USE_CMAKE_PATH: ", !this->FindCommand->NoCMakePath, "\n",
  584. " CMAKE_FIND_USE_CMAKE_ENVIRONMENT_PATH: ",
  585. !this->FindCommand->NoCMakeEnvironmentPath, "\n",
  586. " CMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH: ",
  587. !this->FindCommand->NoSystemEnvironmentPath, "\n",
  588. " CMAKE_FIND_USE_CMAKE_SYSTEM_PATH: ",
  589. !this->FindCommand->NoCMakeSystemPath, "\n",
  590. " CMAKE_FIND_USE_INSTALL_PREFIX: ",
  591. !this->FindCommand->NoCMakeInstallPath, "\n");
  592. }
  593. buffer +=
  594. cmStrCat(this->CommandName, " considered the following locations:\n");
  595. for (auto const& state : this->FailedSearchLocations) {
  596. std::string path = cmStrCat(" ", state.path);
  597. if (!state.regexName.empty()) {
  598. path = cmStrCat(path, "/", state.regexName);
  599. }
  600. buffer += cmStrCat(path, "\n");
  601. }
  602. if (!this->FoundSearchLocation.path.empty()) {
  603. buffer += cmStrCat("The item was found at\n ",
  604. this->FoundSearchLocation.path, "\n");
  605. } else {
  606. buffer += "The item was not found.\n";
  607. }
  608. this->FindCommand->DebugMessage(buffer);
  609. }
  610. }
  611. void cmFindBaseDebugState::FoundAt(std::string const& path,
  612. std::string regexName)
  613. {
  614. if (this->FindCommand->DebugMode) {
  615. this->FoundSearchLocation = DebugLibState{ std::move(regexName), path };
  616. }
  617. }
  618. void cmFindBaseDebugState::FailedAt(std::string const& path,
  619. std::string regexName)
  620. {
  621. if (this->FindCommand->DebugMode) {
  622. this->FailedSearchLocations.emplace_back(std::move(regexName), path);
  623. }
  624. }