cmFindPackageCommand.cxx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. // If there is a find module, use it.
  95. std::string module = m_Makefile->GetDefinition("CMAKE_ROOT");
  96. module += "/Modules/Find";
  97. module += this->Name;
  98. module += ".cmake";
  99. found = false;
  100. // TODO: CMAKE_PACKAGE_PATH for looking for Find<name>.cmake
  101. // modules?
  102. if(cmSystemTools::FileExists(module.c_str()))
  103. {
  104. found = true;
  105. return this->ReadListFile(module.c_str());
  106. }
  107. return true;
  108. }
  109. //----------------------------------------------------------------------------
  110. bool cmFindPackageCommand::FindConfig()
  111. {
  112. std::string help = "The directory containing ";
  113. help += this->Config;
  114. help += ".";
  115. // Construct the list of relative paths to each prefix to be
  116. // searched.
  117. std::string rel = "/lib/";
  118. rel += cmSystemTools::LowerCase(this->Name);
  119. this->Relatives.push_back(rel);
  120. rel = "/lib/";
  121. rel += this->Name;
  122. this->Relatives.push_back(rel);
  123. // It is likely that CMake will have recently built the project.
  124. for(int i=1; i <= 10; ++i)
  125. {
  126. cmOStringStream r;
  127. r << "[HKEY_CURRENT_USER\\Software\\Kitware\\CMakeSetup\\Settings\\StartPath;WhereBuild"
  128. << i << "]";
  129. std::string entry = r.str();
  130. cmSystemTools::ExpandRegistryValues(entry);
  131. cmSystemTools::ConvertToUnixSlashes(entry);
  132. if(cmSystemTools::FileIsDirectory(entry.c_str()))
  133. {
  134. this->Builds.push_back(entry);
  135. }
  136. }
  137. // The project may be installed. Use the system search path to
  138. // construct a list of possible install prefixes.
  139. std::vector<std::string> systemPath;
  140. cmSystemTools::GetPath(systemPath);
  141. for(std::vector<std::string>::iterator i = systemPath.begin();
  142. i != systemPath.end(); ++i)
  143. {
  144. *i += "/..";
  145. if(cmSystemTools::FileIsDirectory(i->c_str()))
  146. {
  147. this->Prefixes.push_back(cmSystemTools::CollapseFullPath(i->c_str()));
  148. }
  149. }
  150. #if !defined(WIN32) || defined(__CYGWIN__)
  151. this->Prefixes.push_back("/usr/local");
  152. this->Prefixes.push_back("/usr");
  153. #endif
  154. // Look for the project's configuration file.
  155. std::string init = this->SearchForConfig();
  156. // Store the entry in the cache so it can be set by the user.
  157. m_Makefile->AddCacheDefinition(this->Variable.c_str(),
  158. init.c_str(),
  159. help.c_str(),
  160. cmCacheManager::PATH);
  161. return true;
  162. }
  163. //----------------------------------------------------------------------------
  164. std::string cmFindPackageCommand::SearchForConfig() const
  165. {
  166. // Search the build directories.
  167. for(std::vector<cmStdString>::const_iterator b = this->Builds.begin();
  168. b != this->Builds.end(); ++b)
  169. {
  170. std::string f = *b;
  171. f += "/";
  172. f += this->Config;
  173. if(cmSystemTools::FileExists(f.c_str()))
  174. {
  175. return *b;
  176. }
  177. }
  178. // Search paths relative to each installation prefix.
  179. for(std::vector<cmStdString>::const_iterator p = this->Prefixes.begin();
  180. p != this->Prefixes.end(); ++p)
  181. {
  182. std::string prefix = *p;
  183. for(std::vector<cmStdString>::const_iterator r = this->Relatives.begin();
  184. r != this->Relatives.end(); ++r)
  185. {
  186. std::string dir = prefix;
  187. dir += *r;
  188. std::string f = dir;
  189. f += "/";
  190. f += this->Config;
  191. if(cmSystemTools::FileExists(f.c_str()))
  192. {
  193. return dir;
  194. }
  195. }
  196. }
  197. return "NOTFOUND";
  198. }
  199. //----------------------------------------------------------------------------
  200. bool cmFindPackageCommand::ReadListFile(const char* f)
  201. {
  202. if(m_Makefile->ReadListFile(m_Makefile->GetCurrentListFile(), f))
  203. {
  204. return true;
  205. }
  206. std::string e = "Error reading CMake code from \"";
  207. e += f;
  208. e += "\".";
  209. this->SetError(e.c_str());
  210. return false;
  211. }