cmFindBase.cxx 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 "cmAlgorithms.h"
  5. #include "cmState.h"
  6. #include "cmStateTypes.h"
  7. #include "cmSystemTools.h"
  8. cmFindBase::cmFindBase()
  9. {
  10. this->AlreadyInCache = false;
  11. this->AlreadyInCacheWithoutMetaInfo = false;
  12. this->NamesPerDir = false;
  13. this->NamesPerDirAllowed = false;
  14. }
  15. bool cmFindBase::ParseArguments(std::vector<std::string> const& argsIn)
  16. {
  17. if (argsIn.size() < 2) {
  18. this->SetError("called with incorrect number of arguments");
  19. return false;
  20. }
  21. // copy argsIn into args so it can be modified,
  22. // in the process extract the DOC "documentation"
  23. size_t size = argsIn.size();
  24. std::vector<std::string> args;
  25. bool foundDoc = false;
  26. for (unsigned int j = 0; j < size; ++j) {
  27. if (foundDoc || argsIn[j] != "DOC") {
  28. if (argsIn[j] == "ENV") {
  29. if (j + 1 < size) {
  30. j++;
  31. cmSystemTools::GetPath(args, argsIn[j].c_str());
  32. }
  33. } else {
  34. args.push_back(argsIn[j]);
  35. }
  36. } else {
  37. if (j + 1 < size) {
  38. foundDoc = true;
  39. this->VariableDocumentation = argsIn[j + 1];
  40. j++;
  41. if (j >= size) {
  42. break;
  43. }
  44. }
  45. }
  46. }
  47. if (args.size() < 2) {
  48. this->SetError("called with incorrect number of arguments");
  49. return false;
  50. }
  51. this->VariableName = args[0];
  52. if (this->CheckForVariableInCache()) {
  53. this->AlreadyInCache = true;
  54. return true;
  55. }
  56. this->AlreadyInCache = false;
  57. // Find the current root path mode.
  58. this->SelectDefaultRootPathMode();
  59. // Find the current bundle/framework search policy.
  60. this->SelectDefaultMacMode();
  61. bool newStyle = false;
  62. enum Doing
  63. {
  64. DoingNone,
  65. DoingNames,
  66. DoingPaths,
  67. DoingPathSuffixes,
  68. DoingHints
  69. };
  70. Doing doing = DoingNames; // assume it starts with a name
  71. for (unsigned int j = 1; j < args.size(); ++j) {
  72. if (args[j] == "NAMES") {
  73. doing = DoingNames;
  74. newStyle = true;
  75. } else if (args[j] == "PATHS") {
  76. doing = DoingPaths;
  77. newStyle = true;
  78. } else if (args[j] == "HINTS") {
  79. doing = DoingHints;
  80. newStyle = true;
  81. } else if (args[j] == "PATH_SUFFIXES") {
  82. doing = DoingPathSuffixes;
  83. newStyle = true;
  84. } else if (args[j] == "NAMES_PER_DIR") {
  85. doing = DoingNone;
  86. if (this->NamesPerDirAllowed) {
  87. this->NamesPerDir = true;
  88. } else {
  89. this->SetError("does not support NAMES_PER_DIR");
  90. return false;
  91. }
  92. } else if (args[j] == "NO_SYSTEM_PATH") {
  93. doing = DoingNone;
  94. this->NoDefaultPath = true;
  95. } else if (this->CheckCommonArgument(args[j])) {
  96. doing = DoingNone;
  97. // Some common arguments were accidentally supported by CMake
  98. // 2.4 and 2.6.0 in the short-hand form of the command, so we
  99. // must support it even though it is not documented.
  100. } else if (doing == DoingNames) {
  101. this->Names.push_back(args[j]);
  102. } else if (doing == DoingPaths) {
  103. this->UserGuessArgs.push_back(args[j]);
  104. } else if (doing == DoingHints) {
  105. this->UserHintsArgs.push_back(args[j]);
  106. } else if (doing == DoingPathSuffixes) {
  107. this->AddPathSuffix(args[j]);
  108. }
  109. }
  110. if (this->VariableDocumentation.empty()) {
  111. this->VariableDocumentation = "Where can ";
  112. if (this->Names.empty()) {
  113. this->VariableDocumentation += "the (unknown) library be found";
  114. } else if (this->Names.size() == 1) {
  115. this->VariableDocumentation +=
  116. "the " + this->Names[0] + " library be found";
  117. } else {
  118. this->VariableDocumentation += "one of the ";
  119. this->VariableDocumentation +=
  120. cmJoin(cmMakeRange(this->Names).retreat(1), ", ");
  121. this->VariableDocumentation +=
  122. " or " + this->Names[this->Names.size() - 1] + " libraries be found";
  123. }
  124. }
  125. // look for old style
  126. // FIND_*(VAR name path1 path2 ...)
  127. if (!newStyle) {
  128. // All the short-hand arguments have been recorded as names.
  129. std::vector<std::string> shortArgs = this->Names;
  130. this->Names.clear(); // clear out any values in Names
  131. this->Names.push_back(shortArgs[0]);
  132. this->UserGuessArgs.insert(this->UserGuessArgs.end(),
  133. shortArgs.begin() + 1, shortArgs.end());
  134. }
  135. this->ExpandPaths();
  136. this->ComputeFinalPaths();
  137. return true;
  138. }
  139. void cmFindBase::ExpandPaths()
  140. {
  141. if (!this->NoDefaultPath) {
  142. if (!this->NoCMakePath) {
  143. this->FillCMakeVariablePath();
  144. }
  145. if (!this->NoCMakeEnvironmentPath) {
  146. this->FillCMakeEnvironmentPath();
  147. }
  148. }
  149. this->FillUserHintsPath();
  150. if (!this->NoDefaultPath) {
  151. if (!this->NoSystemEnvironmentPath) {
  152. this->FillSystemEnvironmentPath();
  153. }
  154. if (!this->NoCMakeSystemPath) {
  155. this->FillCMakeSystemVariablePath();
  156. }
  157. }
  158. this->FillUserGuessPath();
  159. }
  160. void cmFindBase::FillCMakeEnvironmentPath()
  161. {
  162. cmSearchPath& paths = this->LabeledPaths[PathLabel::CMakeEnvironment];
  163. // Add CMAKE_*_PATH environment variables
  164. std::string var = "CMAKE_";
  165. var += this->CMakePathName;
  166. var += "_PATH";
  167. paths.AddEnvPrefixPath("CMAKE_PREFIX_PATH");
  168. paths.AddEnvPath(var);
  169. if (this->CMakePathName == "PROGRAM") {
  170. paths.AddEnvPath("CMAKE_APPBUNDLE_PATH");
  171. } else {
  172. paths.AddEnvPath("CMAKE_FRAMEWORK_PATH");
  173. }
  174. paths.AddSuffixes(this->SearchPathSuffixes);
  175. }
  176. void cmFindBase::FillCMakeVariablePath()
  177. {
  178. cmSearchPath& paths = this->LabeledPaths[PathLabel::CMake];
  179. // Add CMake varibles of the same name as the previous environment
  180. // varibles CMAKE_*_PATH to be used most of the time with -D
  181. // command line options
  182. std::string var = "CMAKE_";
  183. var += this->CMakePathName;
  184. var += "_PATH";
  185. paths.AddCMakePrefixPath("CMAKE_PREFIX_PATH");
  186. paths.AddCMakePath(var);
  187. if (this->CMakePathName == "PROGRAM") {
  188. paths.AddCMakePath("CMAKE_APPBUNDLE_PATH");
  189. } else {
  190. paths.AddCMakePath("CMAKE_FRAMEWORK_PATH");
  191. }
  192. paths.AddSuffixes(this->SearchPathSuffixes);
  193. }
  194. void cmFindBase::FillSystemEnvironmentPath()
  195. {
  196. cmSearchPath& paths = this->LabeledPaths[PathLabel::SystemEnvironment];
  197. // Add LIB or INCLUDE
  198. if (!this->EnvironmentPath.empty()) {
  199. paths.AddEnvPath(this->EnvironmentPath);
  200. #if defined(_WIN32) || defined(__CYGWIN__)
  201. paths.AddEnvPrefixPath("PATH", true);
  202. #endif
  203. }
  204. // Add PATH
  205. paths.AddEnvPath("PATH");
  206. paths.AddSuffixes(this->SearchPathSuffixes);
  207. }
  208. void cmFindBase::FillCMakeSystemVariablePath()
  209. {
  210. cmSearchPath& paths = this->LabeledPaths[PathLabel::CMakeSystem];
  211. std::string var = "CMAKE_SYSTEM_";
  212. var += this->CMakePathName;
  213. var += "_PATH";
  214. paths.AddCMakePrefixPath("CMAKE_SYSTEM_PREFIX_PATH");
  215. paths.AddCMakePath(var);
  216. if (this->CMakePathName == "PROGRAM") {
  217. paths.AddCMakePath("CMAKE_SYSTEM_APPBUNDLE_PATH");
  218. } else {
  219. paths.AddCMakePath("CMAKE_SYSTEM_FRAMEWORK_PATH");
  220. }
  221. paths.AddSuffixes(this->SearchPathSuffixes);
  222. }
  223. void cmFindBase::FillUserHintsPath()
  224. {
  225. cmSearchPath& paths = this->LabeledPaths[PathLabel::Hints];
  226. for (std::vector<std::string>::const_iterator p =
  227. this->UserHintsArgs.begin();
  228. p != this->UserHintsArgs.end(); ++p) {
  229. paths.AddUserPath(*p);
  230. }
  231. paths.AddSuffixes(this->SearchPathSuffixes);
  232. }
  233. void cmFindBase::FillUserGuessPath()
  234. {
  235. cmSearchPath& paths = this->LabeledPaths[PathLabel::Guess];
  236. for (std::vector<std::string>::const_iterator p =
  237. this->UserGuessArgs.begin();
  238. p != this->UserGuessArgs.end(); ++p) {
  239. paths.AddUserPath(*p);
  240. }
  241. paths.AddSuffixes(this->SearchPathSuffixes);
  242. }
  243. void cmFindBase::PrintFindStuff()
  244. {
  245. std::cerr << "SearchFrameworkLast: " << this->SearchFrameworkLast << "\n";
  246. std::cerr << "SearchFrameworkOnly: " << this->SearchFrameworkOnly << "\n";
  247. std::cerr << "SearchFrameworkFirst: " << this->SearchFrameworkFirst << "\n";
  248. std::cerr << "SearchAppBundleLast: " << this->SearchAppBundleLast << "\n";
  249. std::cerr << "SearchAppBundleOnly: " << this->SearchAppBundleOnly << "\n";
  250. std::cerr << "SearchAppBundleFirst: " << this->SearchAppBundleFirst << "\n";
  251. std::cerr << "VariableName " << this->VariableName << "\n";
  252. std::cerr << "VariableDocumentation " << this->VariableDocumentation << "\n";
  253. std::cerr << "NoDefaultPath " << this->NoDefaultPath << "\n";
  254. std::cerr << "NoCMakeEnvironmentPath " << this->NoCMakeEnvironmentPath
  255. << "\n";
  256. std::cerr << "NoCMakePath " << this->NoCMakePath << "\n";
  257. std::cerr << "NoSystemEnvironmentPath " << this->NoSystemEnvironmentPath
  258. << "\n";
  259. std::cerr << "NoCMakeSystemPath " << this->NoCMakeSystemPath << "\n";
  260. std::cerr << "EnvironmentPath " << this->EnvironmentPath << "\n";
  261. std::cerr << "CMakePathName " << this->CMakePathName << "\n";
  262. std::cerr << "Names " << cmJoin(this->Names, " ") << "\n";
  263. std::cerr << "\n";
  264. std::cerr << "SearchPathSuffixes ";
  265. std::cerr << cmJoin(this->SearchPathSuffixes, "\n") << "\n";
  266. std::cerr << "SearchPaths\n";
  267. std::cerr << cmWrap("[", this->SearchPaths, "]", "\n") << "\n";
  268. }
  269. bool cmFindBase::CheckForVariableInCache()
  270. {
  271. if (const char* cacheValue =
  272. this->Makefile->GetDefinition(this->VariableName)) {
  273. cmState* state = this->Makefile->GetState();
  274. const char* cacheEntry = state->GetCacheEntryValue(this->VariableName);
  275. bool found = !cmSystemTools::IsNOTFOUND(cacheValue);
  276. bool cached = cacheEntry ? true : false;
  277. if (found) {
  278. // If the user specifies the entry on the command line without a
  279. // type we should add the type and docstring but keep the
  280. // original value. Tell the subclass implementations to do
  281. // this.
  282. if (cached &&
  283. state->GetCacheEntryType(this->VariableName) ==
  284. cmStateEnums::UNINITIALIZED) {
  285. this->AlreadyInCacheWithoutMetaInfo = true;
  286. }
  287. return true;
  288. }
  289. if (cached) {
  290. const char* hs =
  291. state->GetCacheEntryProperty(this->VariableName, "HELPSTRING");
  292. this->VariableDocumentation = hs ? hs : "(none)";
  293. }
  294. }
  295. return false;
  296. }