cmFindPathCommand.cxx 7.5 KB

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