cmFindPathCommand.cxx 7.4 KB

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