cmFindPackageCommand.cxx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmFindPackageCommand.h"
  14. #include <cmsys/RegularExpression.hxx>
  15. //----------------------------------------------------------------------------
  16. bool cmFindPackageCommand::InitialPass(std::vector<std::string> const& args)
  17. {
  18. if(args.size() < 1)
  19. {
  20. this->SetError("called with incorrect number of arguments");
  21. return false;
  22. }
  23. this->Name = args[0];
  24. bool quiet = false;
  25. if(args.size() > 1)
  26. {
  27. cmsys::RegularExpression version("^[0-9.]+$");
  28. bool haveVersion = false;
  29. for(unsigned int i=1; i < args.size(); ++i)
  30. {
  31. if(!haveVersion && version.find(args[i].c_str()))
  32. {
  33. haveVersion = true;
  34. }
  35. else if(args[i] == "QUIET")
  36. {
  37. quiet = true;
  38. }
  39. else
  40. {
  41. cmOStringStream e;
  42. e << "called with invalid argument \"" << args[i].c_str() << "\"";
  43. this->SetError(e.str().c_str());
  44. return false;
  45. }
  46. }
  47. }
  48. // See if there is a Find<name>.cmake module.
  49. bool foundModule = false;
  50. if(!this->FindModule(foundModule))
  51. {
  52. return false;
  53. }
  54. if(foundModule)
  55. {
  56. return true;
  57. }
  58. // No find module. Assume the project has a CMake config file. Use
  59. // a <NAME>_DIR cache variable to locate it.
  60. this->Variable = this->Name;
  61. this->Variable += "_DIR";
  62. this->Config = this->Name;
  63. this->Config += "Config.cmake";
  64. const char* def = m_Makefile->GetDefinition(this->Variable.c_str());
  65. if(cmSystemTools::IsOff(def))
  66. {
  67. if(!this->FindConfig())
  68. {
  69. return false;
  70. }
  71. }
  72. // If the config file was found, load it.
  73. bool result = true;
  74. bool found = false;
  75. def = m_Makefile->GetDefinition(this->Variable.c_str());
  76. if(!cmSystemTools::IsOff(def))
  77. {
  78. std::string f = def;
  79. f += "/";
  80. f += this->Config;
  81. if(cmSystemTools::FileExists(f.c_str()))
  82. {
  83. if(this->ReadListFile(f.c_str()))
  84. {
  85. found = true;
  86. }
  87. else
  88. {
  89. result = false;
  90. }
  91. }
  92. else
  93. {
  94. cmOStringStream e;
  95. e << this->Variable << " is set to \"" << def << "\", which is "
  96. << "not a directory containing " << this->Config;
  97. cmSystemTools::Error(e.str().c_str());
  98. result = true;
  99. }
  100. }
  101. else if(!quiet)
  102. {
  103. cmOStringStream e;
  104. e << this->Variable << " is not set. It must be set to the directory "
  105. << "containing " << this->Config << " so in order to use "
  106. << this->Name << ".";
  107. cmSystemTools::Error(e.str().c_str());
  108. result = true;
  109. }
  110. std::string foundVar = this->Name;
  111. foundVar += "_FOUND";
  112. m_Makefile->AddDefinition(foundVar.c_str(), found? "1":"0");
  113. return result;
  114. }
  115. //----------------------------------------------------------------------------
  116. bool cmFindPackageCommand::FindModule(bool& found)
  117. {
  118. // Search the CMAKE_MODULE_PATH for a Find<name>.cmake module.
  119. found = false;
  120. std::string module;
  121. std::vector<std::string> modulePath;
  122. const char* def = m_Makefile->GetDefinition("CMAKE_MODULE_PATH");
  123. if(def)
  124. {
  125. cmSystemTools::ExpandListArgument(def, modulePath);
  126. }
  127. // Also search in the standard modules location.
  128. def = m_Makefile->GetDefinition("CMAKE_ROOT");
  129. if(def)
  130. {
  131. std::string rootModules = def;
  132. rootModules += "/Modules";
  133. modulePath.push_back(rootModules);
  134. }
  135. // Look through the possible module directories.
  136. for(std::vector<std::string>::iterator i = modulePath.begin();
  137. i != modulePath.end(); ++i)
  138. {
  139. module = *i;
  140. cmSystemTools::ConvertToUnixSlashes(module);
  141. module += "/Find";
  142. module += this->Name;
  143. module += ".cmake";
  144. if(cmSystemTools::FileExists(module.c_str()))
  145. {
  146. found = true;
  147. return this->ReadListFile(module.c_str());
  148. }
  149. }
  150. return true;
  151. }
  152. //----------------------------------------------------------------------------
  153. bool cmFindPackageCommand::FindConfig()
  154. {
  155. std::string help = "The directory containing ";
  156. help += this->Config;
  157. help += ".";
  158. // Construct the list of relative paths to each prefix to be
  159. // searched.
  160. std::string rel = "/lib/";
  161. rel += cmSystemTools::LowerCase(this->Name);
  162. this->Relatives.push_back(rel);
  163. rel = "/lib/";
  164. rel += this->Name;
  165. this->Relatives.push_back(rel);
  166. // It is likely that CMake will have recently built the project.
  167. for(int i=1; i <= 10; ++i)
  168. {
  169. cmOStringStream r;
  170. r << "[HKEY_CURRENT_USER\\Software\\Kitware\\CMakeSetup\\Settings\\StartPath;WhereBuild"
  171. << i << "]";
  172. std::string entry = r.str();
  173. cmSystemTools::ExpandRegistryValues(entry);
  174. cmSystemTools::ConvertToUnixSlashes(entry);
  175. if(cmSystemTools::FileIsDirectory(entry.c_str()))
  176. {
  177. this->Builds.push_back(entry);
  178. }
  179. }
  180. // The project may be installed. Use the system search path to
  181. // construct a list of possible install prefixes.
  182. std::vector<std::string> systemPath;
  183. cmSystemTools::GetPath(systemPath);
  184. for(std::vector<std::string>::iterator i = systemPath.begin();
  185. i != systemPath.end(); ++i)
  186. {
  187. *i += "/..";
  188. if(cmSystemTools::FileIsDirectory(i->c_str()))
  189. {
  190. this->Prefixes.push_back(cmSystemTools::CollapseFullPath(i->c_str()));
  191. }
  192. }
  193. #if !defined(WIN32) || defined(__CYGWIN__)
  194. this->Prefixes.push_back("/usr/local");
  195. this->Prefixes.push_back("/usr");
  196. #endif
  197. // Look for the project's configuration file.
  198. std::string init = this->SearchForConfig();
  199. // Store the entry in the cache so it can be set by the user.
  200. m_Makefile->AddCacheDefinition(this->Variable.c_str(),
  201. init.c_str(),
  202. help.c_str(),
  203. cmCacheManager::PATH);
  204. return true;
  205. }
  206. //----------------------------------------------------------------------------
  207. std::string cmFindPackageCommand::SearchForConfig() const
  208. {
  209. // Search the build directories.
  210. for(std::vector<cmStdString>::const_iterator b = this->Builds.begin();
  211. b != this->Builds.end(); ++b)
  212. {
  213. std::string f = *b;
  214. f += "/";
  215. f += this->Config;
  216. if(cmSystemTools::FileExists(f.c_str()))
  217. {
  218. return *b;
  219. }
  220. }
  221. // Search paths relative to each installation prefix.
  222. for(std::vector<cmStdString>::const_iterator p = this->Prefixes.begin();
  223. p != this->Prefixes.end(); ++p)
  224. {
  225. std::string prefix = *p;
  226. for(std::vector<cmStdString>::const_iterator r = this->Relatives.begin();
  227. r != this->Relatives.end(); ++r)
  228. {
  229. std::string dir = prefix;
  230. dir += *r;
  231. std::string f = dir;
  232. f += "/";
  233. f += this->Config;
  234. if(cmSystemTools::FileExists(f.c_str()))
  235. {
  236. return dir;
  237. }
  238. }
  239. }
  240. return this->Variable + "-NOTFOUND";
  241. }
  242. //----------------------------------------------------------------------------
  243. bool cmFindPackageCommand::ReadListFile(const char* f)
  244. {
  245. if(m_Makefile->ReadListFile(m_Makefile->GetCurrentListFile(), f))
  246. {
  247. return true;
  248. }
  249. std::string e = "Error reading CMake code from \"";
  250. e += f;
  251. e += "\".";
  252. this->SetError(e.c_str());
  253. return false;
  254. }