cmFindBase.cxx 9.8 KB

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