cmFindBase.cxx 11 KB

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