cmFindPathCommand.cxx 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 "cmFindPathCommand.h"
  14. #include "cmCacheManager.h"
  15. #include <cmsys/Glob.hxx>
  16. cmFindPathCommand::cmFindPathCommand()
  17. {
  18. this->EnvironmentPath = "INCLUDE";
  19. this->IncludeFileInPath = false;
  20. cmSystemTools::ReplaceString(this->GenericDocumentation,
  21. "FIND_XXX", "find_path");
  22. cmSystemTools::ReplaceString(this->GenericDocumentation,
  23. "CMAKE_XXX_PATH", "CMAKE_INCLUDE_PATH");
  24. cmSystemTools::ReplaceString(this->GenericDocumentation,
  25. "CMAKE_XXX_MAC_PATH",
  26. "CMAKE_FRAMEWORK_PATH");
  27. cmSystemTools::ReplaceString(this->GenericDocumentation,
  28. "CMAKE_SYSTEM_XXX_MAC_PATH",
  29. "CMAKE_SYSTEM_FRAMEWORK_PATH");
  30. cmSystemTools::ReplaceString(this->GenericDocumentation,
  31. "XXX_SYSTEM", "INCLUDE");
  32. cmSystemTools::ReplaceString(this->GenericDocumentation,
  33. "CMAKE_SYSTEM_XXX_PATH",
  34. "CMAKE_SYSTEM_INCLUDE_PATH");
  35. cmSystemTools::ReplaceString(this->GenericDocumentation,
  36. "SEARCH_XXX_DESC",
  37. "directory containing the named file");
  38. cmSystemTools::ReplaceString(this->GenericDocumentation,
  39. "SEARCH_XXX", "file in a directory");
  40. cmSystemTools::ReplaceString(this->GenericDocumentation,
  41. "XXX_SUBDIR", "include");
  42. cmSystemTools::ReplaceString(this->GenericDocumentation,
  43. "CMAKE_FIND_ROOT_PATH_MODE_XXX",
  44. "CMAKE_FIND_ROOT_PATH_MODE_INCLUDE");
  45. this->ExtraDocAdded = false;
  46. }
  47. const char* cmFindPathCommand::GetFullDocumentation()
  48. {
  49. if(!this->ExtraDocAdded && !this->IncludeFileInPath)
  50. {
  51. this->GenericDocumentation +=
  52. "\n"
  53. "When searching for frameworks, if the file is specified as "
  54. "A/b.h, then the framework search will look for "
  55. "A.framework/Headers/b.h. "
  56. "If that is found the path will be set to the path to the framework. "
  57. "CMake will convert this to the correct -F option to include the "
  58. "file. ";
  59. this->ExtraDocAdded = true;
  60. }
  61. return this->GenericDocumentation.c_str();
  62. }
  63. // cmFindPathCommand
  64. bool cmFindPathCommand
  65. ::InitialPass(std::vector<std::string> const& argsIn, cmExecutionStatus &)
  66. {
  67. this->VariableDocumentation = "Path to a file.";
  68. this->CMakePathName = "INCLUDE";
  69. if(!this->ParseArguments(argsIn))
  70. {
  71. return false;
  72. }
  73. if(this->AlreadyInCache)
  74. {
  75. // If the user specifies the entry on the command line without a
  76. // type we should add the type and docstring but keep the original
  77. // value.
  78. if(this->AlreadyInCacheWithoutMetaInfo)
  79. {
  80. this->Makefile->AddCacheDefinition(
  81. this->VariableName.c_str(), "",
  82. this->VariableDocumentation.c_str(),
  83. (this->IncludeFileInPath ?
  84. cmCacheManager::FILEPATH :cmCacheManager::PATH)
  85. );
  86. }
  87. return true;
  88. }
  89. std::string result = this->FindHeader();
  90. if(result.size() != 0)
  91. {
  92. this->Makefile->AddCacheDefinition
  93. (this->VariableName.c_str(), result.c_str(),
  94. this->VariableDocumentation.c_str(),
  95. (this->IncludeFileInPath) ?
  96. cmCacheManager::FILEPATH :cmCacheManager::PATH);
  97. return true;
  98. }
  99. this->Makefile->AddCacheDefinition
  100. (this->VariableName.c_str(),
  101. (this->VariableName + "-NOTFOUND").c_str(),
  102. this->VariableDocumentation.c_str(),
  103. (this->IncludeFileInPath) ?
  104. cmCacheManager::FILEPATH :cmCacheManager::PATH);
  105. return true;
  106. }
  107. //----------------------------------------------------------------------------
  108. std::string cmFindPathCommand::FindHeader()
  109. {
  110. std::string header;
  111. if(this->SearchFrameworkFirst || this->SearchFrameworkOnly)
  112. {
  113. header = this->FindFrameworkHeader();
  114. }
  115. if(header.empty() && !this->SearchFrameworkOnly)
  116. {
  117. header = this->FindNormalHeader();
  118. }
  119. if(header.empty() && this->SearchFrameworkLast)
  120. {
  121. header = this->FindFrameworkHeader();
  122. }
  123. return header;
  124. }
  125. std::string
  126. cmFindPathCommand::FindHeaderInFramework(std::string const& file,
  127. std::string const& dir)
  128. {
  129. cmStdString fileName = file;
  130. cmStdString frameWorkName;
  131. cmStdString::size_type pos = fileName.find("/");
  132. // if there is a / in the name try to find the header as a framework
  133. // For example bar/foo.h would look for:
  134. // bar.framework/Headers/foo.h
  135. if(pos != fileName.npos)
  136. {
  137. // remove the name from the slash;
  138. fileName = fileName.substr(pos+1);
  139. frameWorkName = file;
  140. frameWorkName =
  141. frameWorkName.substr(0, frameWorkName.size()-fileName.size()-1);
  142. // if the framework has a path in it then just use the filename
  143. if(frameWorkName.find("/") != frameWorkName.npos)
  144. {
  145. fileName = file;
  146. frameWorkName = "";
  147. }
  148. if(frameWorkName.size())
  149. {
  150. std::string fpath = dir;
  151. fpath += frameWorkName;
  152. fpath += ".framework";
  153. std::string intPath = fpath;
  154. intPath += "/Headers/";
  155. intPath += fileName;
  156. if(cmSystemTools::FileExists(intPath.c_str()))
  157. {
  158. if(this->IncludeFileInPath)
  159. {
  160. return intPath;
  161. }
  162. return fpath;
  163. }
  164. }
  165. }
  166. // if it is not found yet or not a framework header, then do a glob search
  167. // for all files in dir/*/Headers/
  168. cmStdString glob = dir;
  169. glob += "*/Headers/";
  170. glob += file;
  171. cmsys::Glob globIt;
  172. globIt.FindFiles(glob);
  173. std::vector<std::string> files = globIt.GetFiles();
  174. if(files.size())
  175. {
  176. cmStdString fheader = cmSystemTools::CollapseFullPath(files[0].c_str());
  177. if(this->IncludeFileInPath)
  178. {
  179. return fheader;
  180. }
  181. fheader = cmSystemTools::GetFilenamePath(fheader);
  182. return fheader;
  183. }
  184. return "";
  185. }
  186. //----------------------------------------------------------------------------
  187. std::string cmFindPathCommand::FindNormalHeader()
  188. {
  189. std::string tryPath;
  190. for(std::vector<std::string>::const_iterator ni = this->Names.begin();
  191. ni != this->Names.end() ; ++ni)
  192. {
  193. for(std::vector<std::string>::const_iterator
  194. p = this->SearchPaths.begin();
  195. p != this->SearchPaths.end(); ++p)
  196. {
  197. tryPath = *p;
  198. tryPath += *ni;
  199. if(cmSystemTools::FileExists(tryPath.c_str()))
  200. {
  201. if(this->IncludeFileInPath)
  202. {
  203. return tryPath;
  204. }
  205. else
  206. {
  207. return *p;
  208. }
  209. }
  210. }
  211. }
  212. return "";
  213. }
  214. //----------------------------------------------------------------------------
  215. std::string cmFindPathCommand::FindFrameworkHeader()
  216. {
  217. for(std::vector<std::string>::const_iterator ni = this->Names.begin();
  218. ni != this->Names.end() ; ++ni)
  219. {
  220. for(std::vector<std::string>::const_iterator
  221. p = this->SearchPaths.begin();
  222. p != this->SearchPaths.end(); ++p)
  223. {
  224. std::string fwPath = this->FindHeaderInFramework(*ni, *p);
  225. if(!fwPath.empty())
  226. {
  227. return fwPath;
  228. }
  229. }
  230. }
  231. return "";
  232. }