cmFindPackageCommand.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. #include "cmVariableWatch.h"
  16. void cmFindPackageNeedBackwardsCompatibility(const std::string& variable,
  17. int access_type, void* )
  18. {
  19. if(access_type == cmVariableWatch::UNKNOWN_VARIABLE_READ_ACCESS)
  20. {
  21. std::string message = "An attempt was made to access a variable: ";
  22. message += variable;
  23. message +=
  24. " that has not been defined. This variable is created by the "
  25. "FIND_PACKAGE command. CMake version 1.6 always converted the "
  26. "variable name to upper-case, but this behavior is no longer the "
  27. "case. To fix this you might need to set the cache value of "
  28. "CMAKE_BACKWARDS_COMPATIBILITY to 1.6 or less. If you are writing a "
  29. "CMake listfile, you should change the variable reference to use "
  30. "the case of the argument to FIND_PACKAGE.";
  31. cmSystemTools::Error(message.c_str());
  32. }
  33. }
  34. //----------------------------------------------------------------------------
  35. bool cmFindPackageCommand::InitialPass(std::vector<std::string> const& args)
  36. {
  37. if(args.size() < 1)
  38. {
  39. this->SetError("called with incorrect number of arguments");
  40. return false;
  41. }
  42. this->Name = args[0];
  43. bool quiet = false;
  44. if(args.size() > 1)
  45. {
  46. cmsys::RegularExpression version("^[0-9.]+$");
  47. bool haveVersion = false;
  48. for(unsigned int i=1; i < args.size(); ++i)
  49. {
  50. if(!haveVersion && version.find(args[i].c_str()))
  51. {
  52. haveVersion = true;
  53. }
  54. else if(args[i] == "QUIET")
  55. {
  56. quiet = true;
  57. }
  58. else
  59. {
  60. cmOStringStream e;
  61. e << "called with invalid argument \"" << args[i].c_str() << "\"";
  62. this->SetError(e.str().c_str());
  63. return false;
  64. }
  65. }
  66. }
  67. // See if there is a Find<name>.cmake module.
  68. bool foundModule = false;
  69. if(!this->FindModule(foundModule, quiet))
  70. {
  71. return false;
  72. }
  73. if(foundModule)
  74. {
  75. return true;
  76. }
  77. // No find module. Assume the project has a CMake config file. Use
  78. // a <NAME>_DIR cache variable to locate it.
  79. this->Variable = this->Name;
  80. this->Variable += "_DIR";
  81. this->Config = this->Name;
  82. this->Config += "Config.cmake";
  83. // Support old capitalization behavior.
  84. std::string upperDir = cmSystemTools::UpperCase(this->Name);
  85. std::string upperFound = cmSystemTools::UpperCase(this->Name);
  86. upperDir += "_DIR";
  87. upperFound += "_FOUND";
  88. bool needCompatibility = false;
  89. if(!(upperDir == this->Variable))
  90. {
  91. const char* versionValue =
  92. m_Makefile->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY");
  93. if(atof(versionValue) < 1.7)
  94. {
  95. needCompatibility = true;
  96. }
  97. }
  98. // Try to find the config file.
  99. const char* def = m_Makefile->GetDefinition(this->Variable.c_str());
  100. if(needCompatibility && cmSystemTools::IsOff(def))
  101. {
  102. // Use the setting of the old name of the variable to provide the
  103. // value of the new.
  104. const char* oldDef = m_Makefile->GetDefinition(upperDir.c_str());
  105. if(!cmSystemTools::IsOff(oldDef))
  106. {
  107. m_Makefile->AddDefinition(this->Variable.c_str(), oldDef);
  108. def = m_Makefile->GetDefinition(this->Variable.c_str());
  109. }
  110. }
  111. if(cmSystemTools::IsOff(def))
  112. {
  113. if(!this->FindConfig())
  114. {
  115. return false;
  116. }
  117. }
  118. // If the config file was found, load it.
  119. bool result = true;
  120. bool found = false;
  121. def = m_Makefile->GetDefinition(this->Variable.c_str());
  122. if(!cmSystemTools::IsOff(def))
  123. {
  124. std::string f = def;
  125. f += "/";
  126. f += this->Config;
  127. if(cmSystemTools::FileExists(f.c_str()))
  128. {
  129. if(this->ReadListFile(f.c_str()))
  130. {
  131. found = true;
  132. }
  133. else
  134. {
  135. result = false;
  136. }
  137. }
  138. else
  139. {
  140. cmOStringStream e;
  141. e << this->Variable << " is set to \"" << def << "\", which is "
  142. << "not a directory containing " << this->Config;
  143. cmSystemTools::Error(e.str().c_str());
  144. result = true;
  145. }
  146. }
  147. else if(!quiet)
  148. {
  149. cmOStringStream e;
  150. e << this->Variable << " is not set. It must be set to the directory "
  151. << "containing " << this->Config << " in order to use "
  152. << this->Name << ".";
  153. cmSystemTools::Error(e.str().c_str());
  154. result = true;
  155. }
  156. // Set a variable marking whether the package was found.
  157. std::string foundVar = this->Name;
  158. foundVar += "_FOUND";
  159. m_Makefile->AddDefinition(foundVar.c_str(), found? "1":"0");
  160. if(needCompatibility)
  161. {
  162. // Listfiles will be looking for the capitalized version of the
  163. // name. Provide it.
  164. m_Makefile->AddDefinition(upperDir.c_str(),
  165. m_Makefile->GetDefinition(this->Variable.c_str()));
  166. m_Makefile->AddDefinition(upperFound.c_str(),
  167. m_Makefile->GetDefinition(foundVar.c_str()));
  168. }
  169. if(!(upperDir == this->Variable))
  170. {
  171. if(needCompatibility)
  172. {
  173. // Listfiles may use the capitalized version of the name.
  174. // Remove any previously added watch.
  175. m_Makefile->GetVariableWatch()->RemoveWatch(
  176. upperDir.c_str(),
  177. cmFindPackageNeedBackwardsCompatibility
  178. );
  179. m_Makefile->GetVariableWatch()->RemoveWatch(
  180. upperFound.c_str(),
  181. cmFindPackageNeedBackwardsCompatibility
  182. );
  183. }
  184. else
  185. {
  186. // Listfiles should not be using the capitalized version of the
  187. // name. Add a watch to warn the user.
  188. m_Makefile->GetVariableWatch()->AddWatch(
  189. upperDir.c_str(),
  190. cmFindPackageNeedBackwardsCompatibility
  191. );
  192. m_Makefile->GetVariableWatch()->AddWatch(
  193. upperFound.c_str(),
  194. cmFindPackageNeedBackwardsCompatibility
  195. );
  196. }
  197. }
  198. return result;
  199. }
  200. //----------------------------------------------------------------------------
  201. bool cmFindPackageCommand::FindModule(bool& found, bool quiet)
  202. {
  203. std::string module = "/Find";
  204. module += this->Name;
  205. module += ".cmake";
  206. std::string mfile = m_Makefile->GetModulesFile(module.c_str());
  207. if ( mfile.size() )
  208. {
  209. if(quiet)
  210. {
  211. // Tell the module that is about to be read that it should find
  212. // quietly.
  213. std::string quietly = this->Name;
  214. quietly += "_FIND_QUIETLY";
  215. m_Makefile->AddDefinition(quietly.c_str(), "1");
  216. }
  217. // Load the module we found.
  218. found = true;
  219. return this->ReadListFile(mfile.c_str());
  220. }
  221. return true;
  222. }
  223. //----------------------------------------------------------------------------
  224. bool cmFindPackageCommand::FindConfig()
  225. {
  226. std::string help = "The directory containing ";
  227. help += this->Config;
  228. help += ".";
  229. // Construct the list of relative paths to each prefix to be
  230. // searched.
  231. std::string rel = "/lib/";
  232. rel += cmSystemTools::LowerCase(this->Name);
  233. this->Relatives.push_back(rel);
  234. rel = "/lib/";
  235. rel += this->Name;
  236. this->Relatives.push_back(rel);
  237. // It is likely that CMake will have recently built the project.
  238. for(int i=1; i <= 10; ++i)
  239. {
  240. cmOStringStream r;
  241. r << "[HKEY_CURRENT_USER\\Software\\Kitware\\CMakeSetup\\Settings\\StartPath;WhereBuild"
  242. << i << "]";
  243. std::string entry = r.str();
  244. cmSystemTools::ExpandRegistryValues(entry);
  245. cmSystemTools::ConvertToUnixSlashes(entry);
  246. if(cmSystemTools::FileIsDirectory(entry.c_str()))
  247. {
  248. this->Builds.push_back(entry);
  249. }
  250. }
  251. // The project may be installed. Use the system search path to
  252. // construct a list of possible install prefixes.
  253. std::vector<std::string> systemPath;
  254. cmSystemTools::GetPath(systemPath);
  255. for(std::vector<std::string>::iterator i = systemPath.begin();
  256. i != systemPath.end(); ++i)
  257. {
  258. *i += "/..";
  259. if(cmSystemTools::FileIsDirectory(i->c_str()))
  260. {
  261. this->Prefixes.push_back(cmSystemTools::CollapseFullPath(i->c_str()));
  262. }
  263. }
  264. #if !defined(WIN32) || defined(__CYGWIN__)
  265. this->Prefixes.push_back("/usr/local");
  266. this->Prefixes.push_back("/usr");
  267. #endif
  268. // Look for the project's configuration file.
  269. std::string init = this->SearchForConfig();
  270. // Store the entry in the cache so it can be set by the user.
  271. m_Makefile->AddCacheDefinition(this->Variable.c_str(),
  272. init.c_str(),
  273. help.c_str(),
  274. cmCacheManager::PATH);
  275. return true;
  276. }
  277. //----------------------------------------------------------------------------
  278. std::string cmFindPackageCommand::SearchForConfig() const
  279. {
  280. // Search the build directories.
  281. for(std::vector<cmStdString>::const_iterator b = this->Builds.begin();
  282. b != this->Builds.end(); ++b)
  283. {
  284. std::string f = *b;
  285. f += "/";
  286. f += this->Config;
  287. if(cmSystemTools::FileExists(f.c_str()))
  288. {
  289. return *b;
  290. }
  291. }
  292. // Search paths relative to each installation prefix.
  293. for(std::vector<cmStdString>::const_iterator p = this->Prefixes.begin();
  294. p != this->Prefixes.end(); ++p)
  295. {
  296. std::string prefix = *p;
  297. for(std::vector<cmStdString>::const_iterator r = this->Relatives.begin();
  298. r != this->Relatives.end(); ++r)
  299. {
  300. std::string dir = prefix;
  301. dir += *r;
  302. std::string f = dir;
  303. f += "/";
  304. f += this->Config;
  305. if(cmSystemTools::FileExists(f.c_str()))
  306. {
  307. return dir;
  308. }
  309. }
  310. }
  311. return this->Variable + "-NOTFOUND";
  312. }
  313. //----------------------------------------------------------------------------
  314. bool cmFindPackageCommand::ReadListFile(const char* f)
  315. {
  316. if(m_Makefile->ReadListFile(m_Makefile->GetCurrentListFile(), f))
  317. {
  318. return true;
  319. }
  320. std::string e = "Error reading CMake code from \"";
  321. e += f;
  322. e += "\".";
  323. this->SetError(e.c_str());
  324. return false;
  325. }