cmFindPathCommand.cxx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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::InitialPass(std::vector<std::string> const& argsIn)
  65. {
  66. this->VariableDocumentation = "Path to a file.";
  67. this->CMakePathName = "INCLUDE";
  68. if(!this->ParseArguments(argsIn))
  69. {
  70. return false;
  71. }
  72. if(this->AlreadyInCache)
  73. {
  74. // If the user specifies the entry on the command line without a
  75. // type we should add the type and docstring but keep the original
  76. // value.
  77. if(this->AlreadyInCacheWithoutMetaInfo)
  78. {
  79. this->Makefile->AddCacheDefinition(
  80. this->VariableName.c_str(), "",
  81. this->VariableDocumentation.c_str(),
  82. (this->IncludeFileInPath ?
  83. cmCacheManager::FILEPATH :cmCacheManager::PATH)
  84. );
  85. }
  86. return true;
  87. }
  88. std::string ff = this->Makefile->GetSafeDefinition("CMAKE_FIND_FRAMEWORK");
  89. bool supportFrameworks = true;
  90. if( ff.size() == 0 || ff == "NEVER" )
  91. {
  92. supportFrameworks = false;
  93. }
  94. std::string framework;
  95. // Add a trailing slash to all paths to aid the search process.
  96. for(std::vector<std::string>::iterator i = this->SearchPaths.begin();
  97. i != this->SearchPaths.end(); ++i)
  98. {
  99. std::string& p = *i;
  100. if(p[p.size()-1] != '/')
  101. {
  102. p += "/";
  103. }
  104. }
  105. // Use the search path to find the file.
  106. unsigned int k;
  107. std::string result;
  108. for(k=0; k < this->SearchPaths.size(); k++)
  109. {
  110. for(unsigned int j =0; j < this->Names.size(); ++j)
  111. {
  112. // if frameworks are supported try to find the header in a framework
  113. std::string tryPath;
  114. if(supportFrameworks)
  115. {
  116. tryPath = this->FindHeaderInFramework(this->Names[j],
  117. this->SearchPaths[k]);
  118. if(tryPath.size())
  119. {
  120. result = tryPath;
  121. }
  122. }
  123. if(result.size() == 0)
  124. {
  125. tryPath = this->SearchPaths[k];
  126. tryPath += this->Names[j];
  127. if(cmSystemTools::FileExists(tryPath.c_str()))
  128. {
  129. if(this->IncludeFileInPath)
  130. {
  131. result = tryPath;
  132. }
  133. else
  134. {
  135. result = this->SearchPaths[k];
  136. }
  137. }
  138. }
  139. if(result.size() != 0)
  140. {
  141. this->Makefile->AddCacheDefinition
  142. (this->VariableName.c_str(), result.c_str(),
  143. this->VariableDocumentation.c_str(),
  144. (this->IncludeFileInPath) ?
  145. cmCacheManager::FILEPATH :cmCacheManager::PATH);
  146. return true;
  147. }
  148. }
  149. }
  150. this->Makefile->AddCacheDefinition
  151. (this->VariableName.c_str(),
  152. (this->VariableName + "-NOTFOUND").c_str(),
  153. this->VariableDocumentation.c_str(),
  154. (this->IncludeFileInPath) ?
  155. cmCacheManager::FILEPATH :cmCacheManager::PATH);
  156. return true;
  157. }
  158. std::string cmFindPathCommand::FindHeaderInFramework(std::string& file,
  159. std::string& dir)
  160. {
  161. cmStdString fileName = file;
  162. cmStdString frameWorkName;
  163. cmStdString::size_type pos = fileName.find("/");
  164. // if there is a / in the name try to find the header as a framework
  165. // For example bar/foo.h would look for:
  166. // bar.framework/Headers/foo.h
  167. if(pos != fileName.npos)
  168. {
  169. // remove the name from the slash;
  170. fileName = fileName.substr(pos+1);
  171. frameWorkName = file;
  172. frameWorkName =
  173. frameWorkName.substr(0, frameWorkName.size()-fileName.size()-1);
  174. // if the framework has a path in it then just use the filename
  175. if(frameWorkName.find("/") != frameWorkName.npos)
  176. {
  177. fileName = file;
  178. frameWorkName = "";
  179. }
  180. if(frameWorkName.size())
  181. {
  182. std::string fpath = dir;
  183. fpath += frameWorkName;
  184. fpath += ".framework";
  185. std::string intPath = fpath;
  186. intPath += "/Headers/";
  187. intPath += fileName;
  188. if(cmSystemTools::FileExists(intPath.c_str()))
  189. {
  190. if(this->IncludeFileInPath)
  191. {
  192. return intPath;
  193. }
  194. return fpath;
  195. }
  196. }
  197. }
  198. // if it is not found yet or not a framework header, then do a glob search
  199. // for all files in dir/*/Headers/
  200. cmStdString glob = dir;
  201. glob += "*/Headers/";
  202. glob += file;
  203. cmsys::Glob globIt;
  204. globIt.FindFiles(glob);
  205. std::vector<std::string> files = globIt.GetFiles();
  206. if(files.size())
  207. {
  208. cmStdString fheader = cmSystemTools::CollapseFullPath(files[0].c_str());
  209. if(this->IncludeFileInPath)
  210. {
  211. return fheader;
  212. }
  213. fheader = cmSystemTools::GetFilenamePath(fheader);
  214. return fheader;
  215. }
  216. return "";
  217. }