cmFindBase.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. cmFindBase::cmFindBase()
  12. {
  13. this->AlreadyInCache = false;
  14. this->AlreadyInCacheWithoutMetaInfo = false;
  15. this->NamesPerDir = false;
  16. this->NamesPerDirAllowed = false;
  17. }
  18. //----------------------------------------------------------------------------
  19. bool cmFindBase::ParseArguments(std::vector<std::string> const& argsIn)
  20. {
  21. if(argsIn.size() < 2 )
  22. {
  23. this->SetError("called with incorrect number of arguments");
  24. return false;
  25. }
  26. // copy argsIn into args so it can be modified,
  27. // in the process extract the DOC "documentation"
  28. size_t size = argsIn.size();
  29. std::vector<std::string> args;
  30. bool foundDoc = false;
  31. for(unsigned int j = 0; j < size; ++j)
  32. {
  33. if(foundDoc || argsIn[j] != "DOC" )
  34. {
  35. if(argsIn[j] == "ENV")
  36. {
  37. if(j+1 < size)
  38. {
  39. j++;
  40. cmSystemTools::GetPath(args, argsIn[j].c_str());
  41. }
  42. }
  43. else
  44. {
  45. args.push_back(argsIn[j]);
  46. }
  47. }
  48. else
  49. {
  50. if(j+1 < size)
  51. {
  52. foundDoc = true;
  53. this->VariableDocumentation = argsIn[j+1];
  54. j++;
  55. if(j >= size)
  56. {
  57. break;
  58. }
  59. }
  60. }
  61. }
  62. if(args.size() < 2 )
  63. {
  64. this->SetError("called with incorrect number of arguments");
  65. return false;
  66. }
  67. this->VariableName = args[0];
  68. if(this->CheckForVariableInCache())
  69. {
  70. this->AlreadyInCache = true;
  71. return true;
  72. }
  73. this->AlreadyInCache = false;
  74. // Find the current root path mode.
  75. this->SelectDefaultRootPathMode();
  76. // Find the current bundle/framework search policy.
  77. this->SelectDefaultMacMode();
  78. bool newStyle = false;
  79. enum Doing { DoingNone, DoingNames, DoingPaths, DoingPathSuffixes,
  80. DoingHints };
  81. Doing doing = DoingNames; // assume it starts with a name
  82. for (unsigned int j = 1; j < args.size(); ++j)
  83. {
  84. if(args[j] == "NAMES")
  85. {
  86. doing = DoingNames;
  87. newStyle = true;
  88. }
  89. else if (args[j] == "PATHS")
  90. {
  91. doing = DoingPaths;
  92. newStyle = true;
  93. }
  94. else if (args[j] == "HINTS")
  95. {
  96. doing = DoingHints;
  97. newStyle = true;
  98. }
  99. else if (args[j] == "PATH_SUFFIXES")
  100. {
  101. doing = DoingPathSuffixes;
  102. newStyle = true;
  103. }
  104. else if (args[j] == "NAMES_PER_DIR")
  105. {
  106. doing = DoingNone;
  107. if(this->NamesPerDirAllowed)
  108. {
  109. this->NamesPerDir = true;
  110. }
  111. else
  112. {
  113. this->SetError("does not support NAMES_PER_DIR");
  114. return false;
  115. }
  116. }
  117. else if (args[j] == "NO_SYSTEM_PATH")
  118. {
  119. doing = DoingNone;
  120. this->NoDefaultPath = true;
  121. }
  122. else if (this->CheckCommonArgument(args[j]))
  123. {
  124. doing = DoingNone;
  125. // Some common arguments were accidentally supported by CMake
  126. // 2.4 and 2.6.0 in the short-hand form of the command, so we
  127. // must support it even though it is not documented.
  128. }
  129. else if(doing == DoingNames)
  130. {
  131. this->Names.push_back(args[j]);
  132. }
  133. else if(doing == DoingPaths)
  134. {
  135. this->UserGuessArgs.push_back(args[j]);
  136. }
  137. else if(doing == DoingHints)
  138. {
  139. this->UserHintsArgs.push_back(args[j]);
  140. }
  141. else if(doing == DoingPathSuffixes)
  142. {
  143. this->AddPathSuffix(args[j]);
  144. }
  145. }
  146. if(this->VariableDocumentation.size() == 0)
  147. {
  148. this->VariableDocumentation = "Where can ";
  149. if(this->Names.size() == 0)
  150. {
  151. this->VariableDocumentation += "the (unknown) library be found";
  152. }
  153. else if(this->Names.size() == 1)
  154. {
  155. this->VariableDocumentation += "the "
  156. + this->Names[0] + " library be found";
  157. }
  158. else
  159. {
  160. this->VariableDocumentation += "one of the " + this->Names[0];
  161. for (unsigned int j = 1; j < this->Names.size() - 1; ++j)
  162. {
  163. this->VariableDocumentation += ", " + this->Names[j];
  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. for(unsigned int j = 1; j < shortArgs.size(); ++j)
  178. {
  179. this->UserGuessArgs.push_back(shortArgs[j]);
  180. }
  181. }
  182. this->ExpandPaths();
  183. this->ComputeFinalPaths();
  184. return true;
  185. }
  186. void cmFindBase::ExpandPaths()
  187. {
  188. if(!this->NoDefaultPath)
  189. {
  190. if(!this->NoCMakePath)
  191. {
  192. this->FillCMakeVariablePath();
  193. }
  194. if(!this->NoCMakeEnvironmentPath)
  195. {
  196. this->FillCMakeEnvironmentPath();
  197. }
  198. if(!this->NoSystemEnvironmentPath)
  199. {
  200. this->FillSystemEnvironmentPath();
  201. }
  202. if(!this->NoCMakeSystemPath)
  203. {
  204. this->FillCMakeSystemVariablePath();
  205. }
  206. }
  207. this->FillUserHintsPath();
  208. this->FillUserGuessPath();
  209. }
  210. //----------------------------------------------------------------------------
  211. void cmFindBase::FillCMakeEnvironmentPath()
  212. {
  213. // Add CMAKE_*_PATH environment variables
  214. std::string var = "CMAKE_";
  215. var += this->CMakePathName;
  216. var += "_PATH";
  217. this->CMakeEnvironmentPaths.AddEnvPrefixPath("CMAKE_PREFIX_PATH");
  218. this->CMakeEnvironmentPaths.AddEnvPath(var);
  219. if(this->CMakePathName == "PROGRAM")
  220. {
  221. this->CMakeEnvironmentPaths.AddEnvPath("CMAKE_APPBUNDLE_PATH");
  222. }
  223. else
  224. {
  225. this->CMakeEnvironmentPaths.AddEnvPath("CMAKE_FRAMEWORK_PATH");
  226. }
  227. this->CMakeEnvironmentPaths.AddSuffixes(this->SearchPathSuffixes);
  228. }
  229. //----------------------------------------------------------------------------
  230. void cmFindBase::FillCMakeVariablePath()
  231. {
  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. this->CMakeVariablePaths.AddCMakePrefixPath("CMAKE_PREFIX_PATH");
  239. this->CMakeVariablePaths.AddCMakePath(var);
  240. if(this->CMakePathName == "PROGRAM")
  241. {
  242. this->CMakeVariablePaths.AddCMakePath("CMAKE_APPBUNDLE_PATH");
  243. }
  244. else
  245. {
  246. this->CMakeVariablePaths.AddCMakePath("CMAKE_FRAMEWORK_PATH");
  247. }
  248. this->CMakeVariablePaths.AddSuffixes(this->SearchPathSuffixes);
  249. }
  250. //----------------------------------------------------------------------------
  251. void cmFindBase::FillSystemEnvironmentPath()
  252. {
  253. // Add LIB or INCLUDE
  254. if(!this->EnvironmentPath.empty())
  255. {
  256. this->SystemEnvironmentPaths.AddEnvPath(this->EnvironmentPath);
  257. }
  258. // Add PATH
  259. this->SystemEnvironmentPaths.AddEnvPath("PATH");
  260. this->SystemEnvironmentPaths.AddSuffixes(this->SearchPathSuffixes);
  261. }
  262. //----------------------------------------------------------------------------
  263. void cmFindBase::FillCMakeSystemVariablePath()
  264. {
  265. std::string var = "CMAKE_SYSTEM_";
  266. var += this->CMakePathName;
  267. var += "_PATH";
  268. this->CMakeSystemVariablePaths.AddCMakePrefixPath(
  269. "CMAKE_SYSTEM_PREFIX_PATH");
  270. this->CMakeSystemVariablePaths.AddCMakePath(var);
  271. if(this->CMakePathName == "PROGRAM")
  272. {
  273. this->CMakeSystemVariablePaths.AddCMakePath(
  274. "CMAKE_SYSTEM_APPBUNDLE_PATH");
  275. }
  276. else
  277. {
  278. this->CMakeSystemVariablePaths.AddCMakePath("CMAKE_SYSTEM_FRAMEWORK_PATH");
  279. }
  280. this->CMakeSystemVariablePaths.AddSuffixes(this->SearchPathSuffixes);
  281. }
  282. //----------------------------------------------------------------------------
  283. void cmFindBase::FillUserHintsPath()
  284. {
  285. for(std::vector<std::string>::const_iterator p = this->UserHintsArgs.begin();
  286. p != this->UserHintsArgs.end(); ++p)
  287. {
  288. this->UserHintsPaths.AddUserPath(*p);
  289. }
  290. this->UserHintsPaths.AddSuffixes(this->SearchPathSuffixes);
  291. }
  292. //----------------------------------------------------------------------------
  293. void cmFindBase::FillUserGuessPath()
  294. {
  295. for(std::vector<std::string>::const_iterator p = this->UserGuessArgs.begin();
  296. p != this->UserGuessArgs.end(); ++p)
  297. {
  298. this->UserGuessPaths.AddUserPath(*p);
  299. }
  300. this->UserGuessPaths.AddSuffixes(this->SearchPathSuffixes);
  301. }
  302. //----------------------------------------------------------------------------
  303. void cmFindBase::PrintFindStuff()
  304. {
  305. std::cerr << "SearchFrameworkLast: " << this->SearchFrameworkLast << "\n";
  306. std::cerr << "SearchFrameworkOnly: " << this->SearchFrameworkOnly << "\n";
  307. std::cerr << "SearchFrameworkFirst: " << this->SearchFrameworkFirst << "\n";
  308. std::cerr << "SearchAppBundleLast: " << this->SearchAppBundleLast << "\n";
  309. std::cerr << "SearchAppBundleOnly: " << this->SearchAppBundleOnly << "\n";
  310. std::cerr << "SearchAppBundleFirst: " << this->SearchAppBundleFirst << "\n";
  311. std::cerr << "VariableName " << this->VariableName << "\n";
  312. std::cerr << "VariableDocumentation "
  313. << this->VariableDocumentation << "\n";
  314. std::cerr << "NoDefaultPath " << this->NoDefaultPath << "\n";
  315. std::cerr << "NoCMakeEnvironmentPath "
  316. << this->NoCMakeEnvironmentPath << "\n";
  317. std::cerr << "NoCMakePath " << this->NoCMakePath << "\n";
  318. std::cerr << "NoSystemEnvironmentPath "
  319. << this->NoSystemEnvironmentPath << "\n";
  320. std::cerr << "NoCMakeSystemPath " << this->NoCMakeSystemPath << "\n";
  321. std::cerr << "EnvironmentPath " << this->EnvironmentPath << "\n";
  322. std::cerr << "CMakePathName " << this->CMakePathName << "\n";
  323. std::cerr << "Names ";
  324. for(unsigned int i =0; i < this->Names.size(); ++i)
  325. {
  326. std::cerr << this->Names[i] << " ";
  327. }
  328. std::cerr << "\n";
  329. std::cerr << "\n";
  330. std::cerr << "SearchPathSuffixes ";
  331. for(unsigned int i =0; i < this->SearchPathSuffixes.size(); ++i)
  332. {
  333. std::cerr << this->SearchPathSuffixes[i] << "\n";
  334. }
  335. std::cerr << "\n";
  336. std::cerr << "SearchPaths\n";
  337. for(std::vector<std::string>::const_iterator i = this->SearchPaths.begin();
  338. i != this->SearchPaths.end(); ++i)
  339. {
  340. std::cerr << "[" << *i << "]\n";
  341. }
  342. }
  343. bool cmFindBase::CheckForVariableInCache()
  344. {
  345. if(const char* cacheValue =
  346. this->Makefile->GetDefinition(this->VariableName))
  347. {
  348. cmCacheManager::CacheIterator it =
  349. this->Makefile->GetCacheManager()->
  350. GetCacheIterator(this->VariableName.c_str());
  351. bool found = !cmSystemTools::IsNOTFOUND(cacheValue);
  352. bool cached = !it.IsAtEnd();
  353. if(found)
  354. {
  355. // If the user specifies the entry on the command line without a
  356. // type we should add the type and docstring but keep the
  357. // original value. Tell the subclass implementations to do
  358. // this.
  359. if(cached && it.GetType() == cmCacheManager::UNINITIALIZED)
  360. {
  361. this->AlreadyInCacheWithoutMetaInfo = true;
  362. }
  363. return true;
  364. }
  365. else if(cached)
  366. {
  367. const char* hs = it.GetProperty("HELPSTRING");
  368. this->VariableDocumentation = hs?hs:"(none)";
  369. }
  370. }
  371. return false;
  372. }