cmFindPackageCommand.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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 << " so 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. // Search the CMAKE_MODULE_PATH for a Find<name>.cmake module.
  204. found = false;
  205. std::string module;
  206. std::vector<std::string> modulePath;
  207. const char* def = m_Makefile->GetDefinition("CMAKE_MODULE_PATH");
  208. if(def)
  209. {
  210. cmSystemTools::ExpandListArgument(def, modulePath);
  211. }
  212. // Also search in the standard modules location.
  213. def = m_Makefile->GetDefinition("CMAKE_ROOT");
  214. if(def)
  215. {
  216. std::string rootModules = def;
  217. rootModules += "/Modules";
  218. modulePath.push_back(rootModules);
  219. }
  220. // Look through the possible module directories.
  221. for(std::vector<std::string>::iterator i = modulePath.begin();
  222. i != modulePath.end(); ++i)
  223. {
  224. module = *i;
  225. cmSystemTools::ConvertToUnixSlashes(module);
  226. module += "/Find";
  227. module += this->Name;
  228. module += ".cmake";
  229. if(cmSystemTools::FileExists(module.c_str()))
  230. {
  231. found = true;
  232. if(quiet)
  233. {
  234. // Tell the module that is about to be read that it should find
  235. // quietly.
  236. std::string quietly = this->Name;
  237. quietly += "_FIND_QUIETLY";
  238. m_Makefile->AddDefinition(quietly.c_str(), "1");
  239. }
  240. // Load the module we found.
  241. return this->ReadListFile(module.c_str());
  242. }
  243. }
  244. return true;
  245. }
  246. //----------------------------------------------------------------------------
  247. bool cmFindPackageCommand::FindConfig()
  248. {
  249. std::string help = "The directory containing ";
  250. help += this->Config;
  251. help += ".";
  252. // Construct the list of relative paths to each prefix to be
  253. // searched.
  254. std::string rel = "/lib/";
  255. rel += cmSystemTools::LowerCase(this->Name);
  256. this->Relatives.push_back(rel);
  257. rel = "/lib/";
  258. rel += this->Name;
  259. this->Relatives.push_back(rel);
  260. // It is likely that CMake will have recently built the project.
  261. for(int i=1; i <= 10; ++i)
  262. {
  263. cmOStringStream r;
  264. r << "[HKEY_CURRENT_USER\\Software\\Kitware\\CMakeSetup\\Settings\\StartPath;WhereBuild"
  265. << i << "]";
  266. std::string entry = r.str();
  267. cmSystemTools::ExpandRegistryValues(entry);
  268. cmSystemTools::ConvertToUnixSlashes(entry);
  269. if(cmSystemTools::FileIsDirectory(entry.c_str()))
  270. {
  271. this->Builds.push_back(entry);
  272. }
  273. }
  274. // The project may be installed. Use the system search path to
  275. // construct a list of possible install prefixes.
  276. std::vector<std::string> systemPath;
  277. cmSystemTools::GetPath(systemPath);
  278. for(std::vector<std::string>::iterator i = systemPath.begin();
  279. i != systemPath.end(); ++i)
  280. {
  281. *i += "/..";
  282. if(cmSystemTools::FileIsDirectory(i->c_str()))
  283. {
  284. this->Prefixes.push_back(cmSystemTools::CollapseFullPath(i->c_str()));
  285. }
  286. }
  287. #if !defined(WIN32) || defined(__CYGWIN__)
  288. this->Prefixes.push_back("/usr/local");
  289. this->Prefixes.push_back("/usr");
  290. #endif
  291. // Look for the project's configuration file.
  292. std::string init = this->SearchForConfig();
  293. // Store the entry in the cache so it can be set by the user.
  294. m_Makefile->AddCacheDefinition(this->Variable.c_str(),
  295. init.c_str(),
  296. help.c_str(),
  297. cmCacheManager::PATH);
  298. return true;
  299. }
  300. //----------------------------------------------------------------------------
  301. std::string cmFindPackageCommand::SearchForConfig() const
  302. {
  303. // Search the build directories.
  304. for(std::vector<cmStdString>::const_iterator b = this->Builds.begin();
  305. b != this->Builds.end(); ++b)
  306. {
  307. std::string f = *b;
  308. f += "/";
  309. f += this->Config;
  310. if(cmSystemTools::FileExists(f.c_str()))
  311. {
  312. return *b;
  313. }
  314. }
  315. // Search paths relative to each installation prefix.
  316. for(std::vector<cmStdString>::const_iterator p = this->Prefixes.begin();
  317. p != this->Prefixes.end(); ++p)
  318. {
  319. std::string prefix = *p;
  320. for(std::vector<cmStdString>::const_iterator r = this->Relatives.begin();
  321. r != this->Relatives.end(); ++r)
  322. {
  323. std::string dir = prefix;
  324. dir += *r;
  325. std::string f = dir;
  326. f += "/";
  327. f += this->Config;
  328. if(cmSystemTools::FileExists(f.c_str()))
  329. {
  330. return dir;
  331. }
  332. }
  333. }
  334. return this->Variable + "-NOTFOUND";
  335. }
  336. //----------------------------------------------------------------------------
  337. bool cmFindPackageCommand::ReadListFile(const char* f)
  338. {
  339. if(m_Makefile->ReadListFile(m_Makefile->GetCurrentListFile(), f))
  340. {
  341. return true;
  342. }
  343. std::string e = "Error reading CMake code from \"";
  344. e += f;
  345. e += "\".";
  346. this->SetError(e.c_str());
  347. return false;
  348. }