cmFindPathCommand.cxx 7.1 KB

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