cmFindBase.cxx 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  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. 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) {
  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. }
  300. // Add PATH
  301. paths.AddEnvPath("PATH");
  302. paths.AddSuffixes(this->SearchPathSuffixes);
  303. }
  304. namespace {
  305. struct entry_to_remove
  306. {
  307. entry_to_remove(std::string const& name, cmMakefile* makefile)
  308. : value()
  309. {
  310. if (cmValue to_skip = makefile->GetDefinition(
  311. cmStrCat("_CMAKE_SYSTEM_PREFIX_PATH_", name, "_PREFIX_COUNT"))) {
  312. cmStrToLong(*to_skip, &count);
  313. }
  314. if (cmValue prefix_value = makefile->GetDefinition(
  315. cmStrCat("_CMAKE_SYSTEM_PREFIX_PATH_", name, "_PREFIX_VALUE"))) {
  316. value = *prefix_value;
  317. }
  318. }
  319. bool valid() const { return count > 0 && !value.empty(); }
  320. void remove_self(std::vector<std::string>& entries) const
  321. {
  322. if (this->valid()) {
  323. long to_skip = this->count;
  324. long index_to_remove = 0;
  325. for (const auto& path : entries) {
  326. if (path == this->value && --to_skip == 0) {
  327. break;
  328. }
  329. ++index_to_remove;
  330. }
  331. entries.erase(entries.begin() + index_to_remove);
  332. }
  333. }
  334. long count = -1;
  335. std::string value;
  336. };
  337. }
  338. void cmFindBase::FillCMakeSystemVariablePath()
  339. {
  340. cmSearchPath& paths = this->LabeledPaths[PathLabel::CMakeSystem];
  341. const bool install_prefix_in_list =
  342. !this->Makefile->IsOn("CMAKE_FIND_NO_INSTALL_PREFIX");
  343. const bool remove_install_prefix = this->NoCMakeInstallPath;
  344. const bool add_install_prefix = !this->NoCMakeInstallPath &&
  345. this->Makefile->IsDefinitionSet("CMAKE_FIND_USE_INSTALL_PREFIX");
  346. // We have 3 possible states for `CMAKE_SYSTEM_PREFIX_PATH` and
  347. // `CMAKE_INSTALL_PREFIX`.
  348. // Either we need to remove `CMAKE_INSTALL_PREFIX`, add
  349. // `CMAKE_INSTALL_PREFIX`, or do nothing.
  350. //
  351. // When we need to remove `CMAKE_INSTALL_PREFIX` we remove the Nth occurrence
  352. // of `CMAKE_INSTALL_PREFIX` from `CMAKE_SYSTEM_PREFIX_PATH`, where `N` is
  353. // computed by `CMakeSystemSpecificInformation.cmake` while constructing
  354. // `CMAKE_SYSTEM_PREFIX_PATH`. This ensures that if projects / toolchains
  355. // have removed `CMAKE_INSTALL_PREFIX` from the list, we don't remove
  356. // some other entry by mistake ( likewise for `CMAKE_STAGING_PREFIX` )
  357. entry_to_remove install_entry("INSTALL", this->Makefile);
  358. entry_to_remove staging_entry("STAGING", this->Makefile);
  359. if (remove_install_prefix && install_prefix_in_list &&
  360. (install_entry.valid() || staging_entry.valid())) {
  361. cmValue prefix_paths =
  362. this->Makefile->GetDefinition("CMAKE_SYSTEM_PREFIX_PATH");
  363. // remove entries from CMAKE_SYSTEM_PREFIX_PATH
  364. cmList expanded{ *prefix_paths };
  365. install_entry.remove_self(expanded);
  366. staging_entry.remove_self(expanded);
  367. paths.AddPrefixPaths(expanded,
  368. this->Makefile->GetCurrentSourceDirectory().c_str());
  369. } else if (add_install_prefix && !install_prefix_in_list) {
  370. paths.AddCMakePrefixPath("CMAKE_INSTALL_PREFIX");
  371. paths.AddCMakePrefixPath("CMAKE_STAGING_PREFIX");
  372. paths.AddCMakePrefixPath("CMAKE_SYSTEM_PREFIX_PATH");
  373. } else {
  374. // Otherwise the current setup of `CMAKE_SYSTEM_PREFIX_PATH` is correct
  375. paths.AddCMakePrefixPath("CMAKE_SYSTEM_PREFIX_PATH");
  376. }
  377. std::string var = cmStrCat("CMAKE_SYSTEM_", this->CMakePathName, "_PATH");
  378. paths.AddCMakePath(var);
  379. if (this->CMakePathName == "PROGRAM") {
  380. paths.AddCMakePath("CMAKE_SYSTEM_APPBUNDLE_PATH");
  381. } else {
  382. paths.AddCMakePath("CMAKE_SYSTEM_FRAMEWORK_PATH");
  383. }
  384. paths.AddSuffixes(this->SearchPathSuffixes);
  385. }
  386. void cmFindBase::FillUserHintsPath()
  387. {
  388. cmSearchPath& paths = this->LabeledPaths[PathLabel::Hints];
  389. for (std::string const& p : this->UserHintsArgs) {
  390. paths.AddUserPath(p);
  391. }
  392. paths.AddSuffixes(this->SearchPathSuffixes);
  393. }
  394. void cmFindBase::FillUserGuessPath()
  395. {
  396. cmSearchPath& paths = this->LabeledPaths[PathLabel::Guess];
  397. for (std::string const& p : this->UserGuessArgs) {
  398. paths.AddUserPath(p);
  399. }
  400. paths.AddSuffixes(this->SearchPathSuffixes);
  401. }
  402. bool cmFindBase::CheckForVariableDefined()
  403. {
  404. if (cmValue value = this->Makefile->GetDefinition(this->VariableName)) {
  405. cmState* state = this->Makefile->GetState();
  406. cmValue cacheEntry = state->GetCacheEntryValue(this->VariableName);
  407. bool found = !cmIsNOTFOUND(*value);
  408. bool cached = cacheEntry != nullptr;
  409. auto cacheType = cached ? state->GetCacheEntryType(this->VariableName)
  410. : cmStateEnums::UNINITIALIZED;
  411. if (cached && cacheType != cmStateEnums::UNINITIALIZED) {
  412. this->VariableType = cacheType;
  413. if (const auto& hs =
  414. state->GetCacheEntryProperty(this->VariableName, "HELPSTRING")) {
  415. this->VariableDocumentation = *hs;
  416. }
  417. }
  418. if (found) {
  419. // If the user specifies the entry on the command line without a
  420. // type we should add the type and docstring but keep the
  421. // original value. Tell the subclass implementations to do
  422. // this.
  423. if (cached && cacheType == cmStateEnums::UNINITIALIZED) {
  424. this->AlreadyInCacheWithoutMetaInfo = true;
  425. }
  426. return true;
  427. }
  428. }
  429. return false;
  430. }
  431. void cmFindBase::NormalizeFindResult()
  432. {
  433. if (this->Makefile->GetPolicyStatus(cmPolicies::CMP0125) ==
  434. cmPolicies::NEW) {
  435. // ensure the path returned by find_* command is absolute
  436. const auto& existingValue =
  437. this->Makefile->GetDefinition(this->VariableName);
  438. std::string value;
  439. if (!existingValue->empty()) {
  440. value =
  441. cmCMakePath(*existingValue, cmCMakePath::auto_format)
  442. .Absolute(cmCMakePath(
  443. this->Makefile->GetCMakeInstance()->GetCMakeWorkingDirectory()))
  444. .Normal()
  445. .GenericString();
  446. // value = cmSystemTools::CollapseFullPath(*existingValue);
  447. if (!cmSystemTools::FileExists(value, false)) {
  448. value = *existingValue;
  449. }
  450. }
  451. if (this->StoreResultInCache) {
  452. // If the user specifies the entry on the command line without a
  453. // type we should add the type and docstring but keep the original
  454. // value.
  455. if (value != *existingValue || this->AlreadyInCacheWithoutMetaInfo) {
  456. this->Makefile->GetCMakeInstance()->AddCacheEntry(
  457. this->VariableName, value, this->VariableDocumentation,
  458. this->VariableType);
  459. if (this->Makefile->GetPolicyStatus(cmPolicies::CMP0126) ==
  460. cmPolicies::NEW) {
  461. if (this->Makefile->IsNormalDefinitionSet(this->VariableName)) {
  462. this->Makefile->AddDefinition(this->VariableName, value);
  463. }
  464. } else {
  465. // if there was a definition then remove it
  466. // This is required to ensure same behavior as
  467. // cmMakefile::AddCacheDefinition.
  468. this->Makefile->RemoveDefinition(this->VariableName);
  469. }
  470. }
  471. } else {
  472. // ensure a normal variable is defined.
  473. this->Makefile->AddDefinition(this->VariableName, value);
  474. }
  475. } else {
  476. // If the user specifies the entry on the command line without a
  477. // type we should add the type and docstring but keep the original
  478. // value.
  479. if (this->StoreResultInCache) {
  480. if (this->AlreadyInCacheWithoutMetaInfo) {
  481. this->Makefile->AddCacheDefinition(this->VariableName, "",
  482. this->VariableDocumentation,
  483. this->VariableType);
  484. if (this->Makefile->GetPolicyStatus(cmPolicies::CMP0126) ==
  485. cmPolicies::NEW &&
  486. this->Makefile->IsNormalDefinitionSet(this->VariableName)) {
  487. this->Makefile->AddDefinition(
  488. this->VariableName,
  489. *this->Makefile->GetCMakeInstance()->GetCacheDefinition(
  490. this->VariableName));
  491. }
  492. }
  493. } else {
  494. // ensure a normal variable is defined.
  495. this->Makefile->AddDefinition(
  496. this->VariableName,
  497. this->Makefile->GetSafeDefinition(this->VariableName));
  498. }
  499. }
  500. }
  501. void cmFindBase::StoreFindResult(const std::string& value)
  502. {
  503. bool force =
  504. this->Makefile->GetPolicyStatus(cmPolicies::CMP0125) == cmPolicies::NEW;
  505. bool updateNormalVariable =
  506. this->Makefile->GetPolicyStatus(cmPolicies::CMP0126) == cmPolicies::NEW;
  507. if (!value.empty()) {
  508. if (this->StoreResultInCache) {
  509. this->Makefile->AddCacheDefinition(this->VariableName, value,
  510. this->VariableDocumentation,
  511. this->VariableType, force);
  512. if (updateNormalVariable &&
  513. this->Makefile->IsNormalDefinitionSet(this->VariableName)) {
  514. this->Makefile->AddDefinition(this->VariableName, value);
  515. }
  516. } else {
  517. this->Makefile->AddDefinition(this->VariableName, value);
  518. }
  519. return;
  520. }
  521. auto notFound = cmStrCat(this->VariableName, "-NOTFOUND");
  522. if (this->StoreResultInCache) {
  523. this->Makefile->AddCacheDefinition(this->VariableName, notFound,
  524. this->VariableDocumentation,
  525. this->VariableType, force);
  526. if (updateNormalVariable &&
  527. this->Makefile->IsNormalDefinitionSet(this->VariableName)) {
  528. this->Makefile->AddDefinition(this->VariableName, notFound);
  529. }
  530. } else {
  531. this->Makefile->AddDefinition(this->VariableName, notFound);
  532. }
  533. if (this->Required) {
  534. this->Makefile->IssueMessage(
  535. MessageType::FATAL_ERROR,
  536. cmStrCat("Could not find ", this->VariableName, " using the following ",
  537. (this->FindCommandName == "find_file" ||
  538. this->FindCommandName == "find_path"
  539. ? "files"
  540. : "names"),
  541. ": ", cmJoin(this->Names, ", ")));
  542. cmSystemTools::SetFatalErrorOccurred();
  543. }
  544. }
  545. cmFindBaseDebugState::cmFindBaseDebugState(std::string commandName,
  546. cmFindBase const* findBase)
  547. : FindCommand(findBase)
  548. , CommandName(std::move(commandName))
  549. {
  550. }
  551. cmFindBaseDebugState::~cmFindBaseDebugState()
  552. {
  553. if (this->FindCommand->DebugMode) {
  554. std::string buffer =
  555. cmStrCat(this->CommandName, " called with the following settings:\n");
  556. buffer += cmStrCat(" VAR: ", this->FindCommand->VariableName, "\n");
  557. buffer += cmStrCat(
  558. " NAMES: ", cmWrap("\"", this->FindCommand->Names, "\"", "\n "),
  559. "\n");
  560. buffer += cmStrCat(
  561. " Documentation: ", this->FindCommand->VariableDocumentation, "\n");
  562. buffer += " Framework\n";
  563. buffer += cmStrCat(" Only Search Frameworks: ",
  564. this->FindCommand->SearchFrameworkOnly, "\n");
  565. buffer += cmStrCat(" Search Frameworks Last: ",
  566. this->FindCommand->SearchFrameworkLast, "\n");
  567. buffer += cmStrCat(" Search Frameworks First: ",
  568. this->FindCommand->SearchFrameworkFirst, "\n");
  569. buffer += " AppBundle\n";
  570. buffer += cmStrCat(" Only Search AppBundle: ",
  571. this->FindCommand->SearchAppBundleOnly, "\n");
  572. buffer += cmStrCat(" Search AppBundle Last: ",
  573. this->FindCommand->SearchAppBundleLast, "\n");
  574. buffer += cmStrCat(" Search AppBundle First: ",
  575. this->FindCommand->SearchAppBundleFirst, "\n");
  576. if (this->FindCommand->NoDefaultPath) {
  577. buffer += " NO_DEFAULT_PATH Enabled\n";
  578. } else {
  579. buffer += cmStrCat(
  580. " CMAKE_FIND_USE_CMAKE_PATH: ", !this->FindCommand->NoCMakePath, "\n",
  581. " CMAKE_FIND_USE_CMAKE_ENVIRONMENT_PATH: ",
  582. !this->FindCommand->NoCMakeEnvironmentPath, "\n",
  583. " CMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH: ",
  584. !this->FindCommand->NoSystemEnvironmentPath, "\n",
  585. " CMAKE_FIND_USE_CMAKE_SYSTEM_PATH: ",
  586. !this->FindCommand->NoCMakeSystemPath, "\n",
  587. " CMAKE_FIND_USE_INSTALL_PREFIX: ",
  588. !this->FindCommand->NoCMakeInstallPath, "\n");
  589. }
  590. buffer +=
  591. cmStrCat(this->CommandName, " considered the following locations:\n");
  592. for (auto const& state : this->FailedSearchLocations) {
  593. std::string path = cmStrCat(" ", state.path);
  594. if (!state.regexName.empty()) {
  595. path = cmStrCat(path, "/", state.regexName);
  596. }
  597. buffer += cmStrCat(path, "\n");
  598. }
  599. if (!this->FoundSearchLocation.path.empty()) {
  600. buffer += cmStrCat("The item was found at\n ",
  601. this->FoundSearchLocation.path, "\n");
  602. } else {
  603. buffer += "The item was not found.\n";
  604. }
  605. this->FindCommand->DebugMessage(buffer);
  606. }
  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. }