cmFindBase.cxx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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 <cstddef>
  5. #include <deque>
  6. #include <map>
  7. #include <utility>
  8. #include <cmext/algorithm>
  9. #include "cmMakefile.h"
  10. #include "cmProperty.h"
  11. #include "cmRange.h"
  12. #include "cmSearchPath.h"
  13. #include "cmState.h"
  14. #include "cmStateTypes.h"
  15. #include "cmStringAlgorithms.h"
  16. #include "cmSystemTools.h"
  17. class cmExecutionStatus;
  18. cmFindBase::cmFindBase(cmExecutionStatus& status)
  19. : cmFindCommon(status)
  20. {
  21. }
  22. bool cmFindBase::ParseArguments(std::vector<std::string> const& argsIn)
  23. {
  24. if (argsIn.size() < 2) {
  25. this->SetError("called with incorrect number of arguments");
  26. return false;
  27. }
  28. // copy argsIn into args so it can be modified,
  29. // in the process extract the DOC "documentation"
  30. size_t size = argsIn.size();
  31. std::vector<std::string> args;
  32. bool foundDoc = false;
  33. for (unsigned int j = 0; j < size; ++j) {
  34. if (foundDoc || argsIn[j] != "DOC") {
  35. if (argsIn[j] == "ENV") {
  36. if (j + 1 < size) {
  37. j++;
  38. cmSystemTools::GetPath(args, argsIn[j].c_str());
  39. }
  40. } else {
  41. args.push_back(argsIn[j]);
  42. }
  43. } else {
  44. if (j + 1 < size) {
  45. foundDoc = true;
  46. this->VariableDocumentation = argsIn[j + 1];
  47. j++;
  48. if (j >= size) {
  49. break;
  50. }
  51. }
  52. }
  53. }
  54. if (args.size() < 2) {
  55. this->SetError("called with incorrect number of arguments");
  56. return false;
  57. }
  58. this->VariableName = args[0];
  59. if (this->CheckForVariableInCache()) {
  60. this->AlreadyInCache = true;
  61. return true;
  62. }
  63. this->AlreadyInCache = false;
  64. // Find what search path locations have been enabled/disable
  65. this->SelectDefaultSearchModes();
  66. // Find the current root path mode.
  67. this->SelectDefaultRootPathMode();
  68. // Find the current bundle/framework search policy.
  69. this->SelectDefaultMacMode();
  70. bool newStyle = false;
  71. enum Doing
  72. {
  73. DoingNone,
  74. DoingNames,
  75. DoingPaths,
  76. DoingPathSuffixes,
  77. DoingHints
  78. };
  79. Doing doing = DoingNames; // assume it starts with a name
  80. for (unsigned int j = 1; j < args.size(); ++j) {
  81. if (args[j] == "NAMES") {
  82. doing = DoingNames;
  83. newStyle = true;
  84. } else if (args[j] == "PATHS") {
  85. doing = DoingPaths;
  86. newStyle = true;
  87. } else if (args[j] == "HINTS") {
  88. doing = DoingHints;
  89. newStyle = true;
  90. } else if (args[j] == "PATH_SUFFIXES") {
  91. doing = DoingPathSuffixes;
  92. newStyle = true;
  93. } else if (args[j] == "NAMES_PER_DIR") {
  94. doing = DoingNone;
  95. if (this->NamesPerDirAllowed) {
  96. this->NamesPerDir = true;
  97. } else {
  98. this->SetError("does not support NAMES_PER_DIR");
  99. return false;
  100. }
  101. } else if (args[j] == "NO_SYSTEM_PATH") {
  102. doing = DoingNone;
  103. this->NoDefaultPath = true;
  104. } else if (args[j] == "REQUIRED") {
  105. doing = DoingNone;
  106. this->Required = true;
  107. newStyle = true;
  108. } else if (this->CheckCommonArgument(args[j])) {
  109. doing = DoingNone;
  110. } else {
  111. // Some common arguments were accidentally supported by CMake
  112. // 2.4 and 2.6.0 in the short-hand form of the command, so we
  113. // must support it even though it is not documented.
  114. if (doing == DoingNames) {
  115. this->Names.push_back(args[j]);
  116. } else if (doing == DoingPaths) {
  117. this->UserGuessArgs.push_back(args[j]);
  118. } else if (doing == DoingHints) {
  119. this->UserHintsArgs.push_back(args[j]);
  120. } else if (doing == DoingPathSuffixes) {
  121. this->AddPathSuffix(args[j]);
  122. }
  123. }
  124. }
  125. if (this->VariableDocumentation.empty()) {
  126. this->VariableDocumentation = "Where can ";
  127. if (this->Names.empty()) {
  128. this->VariableDocumentation += "the (unknown) library be found";
  129. } else if (this->Names.size() == 1) {
  130. this->VariableDocumentation +=
  131. "the " + this->Names.front() + " library be found";
  132. } else {
  133. this->VariableDocumentation += "one of the ";
  134. this->VariableDocumentation +=
  135. cmJoin(cmMakeRange(this->Names).retreat(1), ", ");
  136. this->VariableDocumentation +=
  137. " or " + this->Names.back() + " libraries be found";
  138. }
  139. }
  140. // look for old style
  141. // FIND_*(VAR name path1 path2 ...)
  142. if (!newStyle && !this->Names.empty()) {
  143. // All the short-hand arguments have been recorded as names.
  144. std::vector<std::string> shortArgs = this->Names;
  145. this->Names.clear(); // clear out any values in Names
  146. this->Names.push_back(shortArgs[0]);
  147. cm::append(this->UserGuessArgs, shortArgs.begin() + 1, shortArgs.end());
  148. }
  149. this->ExpandPaths();
  150. this->ComputeFinalPaths();
  151. return true;
  152. }
  153. void cmFindBase::ExpandPaths()
  154. {
  155. if (!this->NoDefaultPath) {
  156. if (!this->NoPackageRootPath) {
  157. this->FillPackageRootPath();
  158. }
  159. if (!this->NoCMakePath) {
  160. this->FillCMakeVariablePath();
  161. }
  162. if (!this->NoCMakeEnvironmentPath) {
  163. this->FillCMakeEnvironmentPath();
  164. }
  165. }
  166. this->FillUserHintsPath();
  167. if (!this->NoDefaultPath) {
  168. if (!this->NoSystemEnvironmentPath) {
  169. this->FillSystemEnvironmentPath();
  170. }
  171. if (!this->NoCMakeSystemPath) {
  172. this->FillCMakeSystemVariablePath();
  173. }
  174. }
  175. this->FillUserGuessPath();
  176. }
  177. void cmFindBase::FillCMakeEnvironmentPath()
  178. {
  179. cmSearchPath& paths = this->LabeledPaths[PathLabel::CMakeEnvironment];
  180. // Add CMAKE_*_PATH environment variables
  181. std::string var = cmStrCat("CMAKE_", this->CMakePathName, "_PATH");
  182. paths.AddEnvPrefixPath("CMAKE_PREFIX_PATH");
  183. paths.AddEnvPath(var);
  184. if (this->CMakePathName == "PROGRAM") {
  185. paths.AddEnvPath("CMAKE_APPBUNDLE_PATH");
  186. } else {
  187. paths.AddEnvPath("CMAKE_FRAMEWORK_PATH");
  188. }
  189. paths.AddSuffixes(this->SearchPathSuffixes);
  190. }
  191. void cmFindBase::FillPackageRootPath()
  192. {
  193. cmSearchPath& paths = this->LabeledPaths[PathLabel::PackageRoot];
  194. // Add the PACKAGE_ROOT_PATH from each enclosing find_package call.
  195. for (std::vector<std::string> const& pkgPaths :
  196. cmReverseRange(this->Makefile->FindPackageRootPathStack)) {
  197. paths.AddPrefixPaths(pkgPaths);
  198. }
  199. paths.AddSuffixes(this->SearchPathSuffixes);
  200. }
  201. void cmFindBase::FillCMakeVariablePath()
  202. {
  203. cmSearchPath& paths = this->LabeledPaths[PathLabel::CMake];
  204. // Add CMake variables of the same name as the previous environment
  205. // variables CMAKE_*_PATH to be used most of the time with -D
  206. // command line options
  207. std::string var = cmStrCat("CMAKE_", this->CMakePathName, "_PATH");
  208. paths.AddCMakePrefixPath("CMAKE_PREFIX_PATH");
  209. paths.AddCMakePath(var);
  210. if (this->CMakePathName == "PROGRAM") {
  211. paths.AddCMakePath("CMAKE_APPBUNDLE_PATH");
  212. } else {
  213. paths.AddCMakePath("CMAKE_FRAMEWORK_PATH");
  214. }
  215. paths.AddSuffixes(this->SearchPathSuffixes);
  216. }
  217. void cmFindBase::FillSystemEnvironmentPath()
  218. {
  219. cmSearchPath& paths = this->LabeledPaths[PathLabel::SystemEnvironment];
  220. // Add LIB or INCLUDE
  221. if (!this->EnvironmentPath.empty()) {
  222. paths.AddEnvPath(this->EnvironmentPath);
  223. #if defined(_WIN32) || defined(__CYGWIN__)
  224. paths.AddEnvPrefixPath("PATH", true);
  225. #endif
  226. }
  227. // Add PATH
  228. paths.AddEnvPath("PATH");
  229. paths.AddSuffixes(this->SearchPathSuffixes);
  230. }
  231. void cmFindBase::FillCMakeSystemVariablePath()
  232. {
  233. cmSearchPath& paths = this->LabeledPaths[PathLabel::CMakeSystem];
  234. std::string var = cmStrCat("CMAKE_SYSTEM_", this->CMakePathName, "_PATH");
  235. paths.AddCMakePrefixPath("CMAKE_SYSTEM_PREFIX_PATH");
  236. paths.AddCMakePath(var);
  237. if (this->CMakePathName == "PROGRAM") {
  238. paths.AddCMakePath("CMAKE_SYSTEM_APPBUNDLE_PATH");
  239. } else {
  240. paths.AddCMakePath("CMAKE_SYSTEM_FRAMEWORK_PATH");
  241. }
  242. paths.AddSuffixes(this->SearchPathSuffixes);
  243. }
  244. void cmFindBase::FillUserHintsPath()
  245. {
  246. cmSearchPath& paths = this->LabeledPaths[PathLabel::Hints];
  247. for (std::string const& p : this->UserHintsArgs) {
  248. paths.AddUserPath(p);
  249. }
  250. paths.AddSuffixes(this->SearchPathSuffixes);
  251. }
  252. void cmFindBase::FillUserGuessPath()
  253. {
  254. cmSearchPath& paths = this->LabeledPaths[PathLabel::Guess];
  255. for (std::string const& p : this->UserGuessArgs) {
  256. paths.AddUserPath(p);
  257. }
  258. paths.AddSuffixes(this->SearchPathSuffixes);
  259. }
  260. bool cmFindBase::CheckForVariableInCache()
  261. {
  262. if (const char* cacheValue =
  263. this->Makefile->GetDefinition(this->VariableName)) {
  264. cmState* state = this->Makefile->GetState();
  265. cmProp cacheEntry = state->GetCacheEntryValue(this->VariableName);
  266. bool found = !cmIsNOTFOUND(cacheValue);
  267. bool cached = cacheEntry != nullptr;
  268. if (found) {
  269. // If the user specifies the entry on the command line without a
  270. // type we should add the type and docstring but keep the
  271. // original value. Tell the subclass implementations to do
  272. // this.
  273. if (cached &&
  274. state->GetCacheEntryType(this->VariableName) ==
  275. cmStateEnums::UNINITIALIZED) {
  276. this->AlreadyInCacheWithoutMetaInfo = true;
  277. }
  278. return true;
  279. }
  280. if (cached) {
  281. cmProp hs =
  282. state->GetCacheEntryProperty(this->VariableName, "HELPSTRING");
  283. this->VariableDocumentation = hs ? *hs : "(none)";
  284. }
  285. }
  286. return false;
  287. }
  288. cmFindBaseDebugState::cmFindBaseDebugState(std::string commandName,
  289. cmFindBase const* findBase)
  290. : FindCommand(findBase)
  291. , CommandName(std::move(commandName))
  292. {
  293. }
  294. cmFindBaseDebugState::~cmFindBaseDebugState()
  295. {
  296. if (this->FindCommand->DebugMode) {
  297. std::string buffer =
  298. cmStrCat(this->CommandName, " called with the following settings:\n");
  299. buffer += cmStrCat(" VAR: ", this->FindCommand->VariableName, "\n");
  300. buffer += cmStrCat(
  301. " NAMES: ", cmWrap("\"", this->FindCommand->Names, "\"", "\n "),
  302. "\n");
  303. buffer += cmStrCat(
  304. " Documentation: ", this->FindCommand->VariableDocumentation, "\n");
  305. buffer += " Framework\n";
  306. buffer += cmStrCat(" Only Search Frameworks: ",
  307. this->FindCommand->SearchFrameworkOnly, "\n");
  308. buffer += cmStrCat(" Search Frameworks Last: ",
  309. this->FindCommand->SearchFrameworkLast, "\n");
  310. buffer += cmStrCat(" Search Frameworks First: ",
  311. this->FindCommand->SearchFrameworkFirst, "\n");
  312. buffer += " AppBundle\n";
  313. buffer += cmStrCat(" Only Search AppBundle: ",
  314. this->FindCommand->SearchAppBundleOnly, "\n");
  315. buffer += cmStrCat(" Search AppBundle Last: ",
  316. this->FindCommand->SearchAppBundleLast, "\n");
  317. buffer += cmStrCat(" Search AppBundle First: ",
  318. this->FindCommand->SearchAppBundleFirst, "\n");
  319. if (this->FindCommand->NoDefaultPath) {
  320. buffer += " NO_DEFAULT_PATH Enabled\n";
  321. } else {
  322. buffer += cmStrCat(
  323. " CMAKE_FIND_USE_CMAKE_PATH: ", !this->FindCommand->NoCMakePath, "\n",
  324. " CMAKE_FIND_USE_CMAKE_ENVIRONMENT_PATH: ",
  325. !this->FindCommand->NoCMakeEnvironmentPath, "\n",
  326. " CMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH: ",
  327. !this->FindCommand->NoSystemEnvironmentPath, "\n",
  328. " CMAKE_FIND_USE_CMAKE_SYSTEM_PATH: ",
  329. !this->FindCommand->NoCMakeSystemPath, "\n");
  330. }
  331. buffer +=
  332. cmStrCat(this->CommandName, " considered the following locations:\n");
  333. for (auto const& state : this->FailedSearchLocations) {
  334. std::string path = cmStrCat(" ", state.path);
  335. if (!state.regexName.empty()) {
  336. path = cmStrCat(path, "/", state.regexName);
  337. }
  338. buffer += cmStrCat(path, "\n");
  339. }
  340. if (!this->FoundSearchLocation.path.empty()) {
  341. buffer += cmStrCat("The item was found at\n ",
  342. this->FoundSearchLocation.path, "\n");
  343. } else {
  344. buffer += "The item was not found.\n";
  345. }
  346. this->FindCommand->DebugMessage(buffer);
  347. }
  348. }
  349. void cmFindBaseDebugState::FoundAt(std::string const& path,
  350. std::string regexName)
  351. {
  352. if (this->FindCommand->DebugMode) {
  353. this->FoundSearchLocation = DebugLibState{ std::move(regexName), path };
  354. }
  355. }
  356. void cmFindBaseDebugState::FailedAt(std::string const& path,
  357. std::string regexName)
  358. {
  359. if (this->FindCommand->DebugMode) {
  360. this->FailedSearchLocations.emplace_back(std::move(regexName), path);
  361. }
  362. }