cmFindPathCommand.cxx 7.6 KB

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