cmFindPackageCommand.cxx 7.1 KB

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