cmFindPathCommand.cxx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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->IncludeFileInPath = false;
  19. cmSystemTools::ReplaceString(this->GenericDocumentation,
  20. "FIND_XXX", "find_path");
  21. cmSystemTools::ReplaceString(this->GenericDocumentation,
  22. "CMAKE_XXX_PATH", "CMAKE_INCLUDE_PATH");
  23. cmSystemTools::ReplaceString(this->GenericDocumentation,
  24. "XXX_SYSTEM", "INCLUDE");
  25. cmSystemTools::ReplaceString(this->GenericDocumentation,
  26. "CMAKE_SYSTEM_XXX_PATH",
  27. "CMAKE_SYSTEM_INCLUDE_PATH");
  28. cmSystemTools::ReplaceString(this->GenericDocumentation,
  29. "SEARCH_XXX_DESC",
  30. "directory containing the named file");
  31. cmSystemTools::ReplaceString(this->GenericDocumentation,
  32. "SEARCH_XXX", "file in a directory");
  33. cmSystemTools::ReplaceString(this->GenericDocumentation,
  34. "CMAKE_FIND_ROOT_PATH_MODE_XXX",
  35. "CMAKE_FIND_ROOT_PATH_MODE_INCLUDE");
  36. this->ExtraDocAdded = false;
  37. }
  38. const char* cmFindPathCommand::GetFullDocumentation()
  39. {
  40. if(!this->ExtraDocAdded && !this->IncludeFileInPath)
  41. {
  42. this->GenericDocumentation +=
  43. "\n"
  44. "When searching for frameworks, if the file is specified as "
  45. "A/b.h, then the framework search will look for "
  46. "A.framework/Headers/b.h. "
  47. "If that is found the path will be set to the path to the framework. "
  48. "CMake will convert this to the correct -F option to include the "
  49. "file. ";
  50. this->ExtraDocAdded = true;
  51. }
  52. return this->GenericDocumentation.c_str();
  53. }
  54. // cmFindPathCommand
  55. bool cmFindPathCommand::InitialPass(std::vector<std::string> const& argsIn)
  56. {
  57. this->VariableDocumentation = "Path to a file.";
  58. this->CMakePathName = "INCLUDE";
  59. if(!this->ParseArguments(argsIn))
  60. {
  61. return false;
  62. }
  63. if(this->AlreadyInCache)
  64. {
  65. // If the user specifies the entry on the command line without a
  66. // type we should add the type and docstring but keep the original
  67. // value.
  68. if(this->AlreadyInCacheWithoutMetaInfo)
  69. {
  70. this->Makefile->AddCacheDefinition(
  71. this->VariableName.c_str(), "",
  72. this->VariableDocumentation.c_str(),
  73. (this->IncludeFileInPath ?
  74. cmCacheManager::FILEPATH :cmCacheManager::PATH)
  75. );
  76. }
  77. return true;
  78. }
  79. std::string ff = this->Makefile->GetSafeDefinition("CMAKE_FIND_FRAMEWORK");
  80. bool supportFrameworks = true;
  81. if( ff.size() == 0 || ff == "NEVER" )
  82. {
  83. supportFrameworks = false;
  84. }
  85. std::string framework;
  86. // Use the search path to find the file.
  87. unsigned int k;
  88. std::string result;
  89. for(k=0; k < this->SearchPaths.size(); k++)
  90. {
  91. for(unsigned int j =0; j < this->Names.size(); ++j)
  92. {
  93. // if frameworks are supported try to find the header in a framework
  94. std::string tryPath;
  95. if(supportFrameworks)
  96. {
  97. tryPath = this->FindHeaderInFramework(this->Names[j],
  98. this->SearchPaths[k]);
  99. if(tryPath.size())
  100. {
  101. result = tryPath;
  102. }
  103. }
  104. if(result.size() == 0)
  105. {
  106. tryPath = this->SearchPaths[k];
  107. tryPath += "/";
  108. tryPath += this->Names[j];
  109. if(cmSystemTools::FileExists(tryPath.c_str()))
  110. {
  111. if(this->IncludeFileInPath)
  112. {
  113. result = tryPath;
  114. }
  115. else
  116. {
  117. result = this->SearchPaths[k];
  118. }
  119. }
  120. }
  121. if(result.size() != 0)
  122. {
  123. this->Makefile->AddCacheDefinition
  124. (this->VariableName.c_str(), result.c_str(),
  125. this->VariableDocumentation.c_str(),
  126. (this->IncludeFileInPath) ?
  127. cmCacheManager::FILEPATH :cmCacheManager::PATH);
  128. return true;
  129. }
  130. }
  131. }
  132. this->Makefile->AddCacheDefinition
  133. (this->VariableName.c_str(),
  134. (this->VariableName + "-NOTFOUND").c_str(),
  135. this->VariableDocumentation.c_str(),
  136. (this->IncludeFileInPath) ?
  137. cmCacheManager::FILEPATH :cmCacheManager::PATH);
  138. return true;
  139. }
  140. std::string cmFindPathCommand::FindHeaderInFramework(std::string& file,
  141. std::string& dir)
  142. {
  143. cmStdString fileName = file;
  144. cmStdString frameWorkName;
  145. cmStdString::size_type pos = fileName.find("/");
  146. // if there is a / in the name try to find the header as a framework
  147. // For example bar/foo.h would look for:
  148. // bar.framework/Headers/foo.h
  149. if(pos != fileName.npos)
  150. {
  151. // remove the name from the slash;
  152. fileName = fileName.substr(pos+1);
  153. frameWorkName = file;
  154. frameWorkName =
  155. frameWorkName.substr(0, frameWorkName.size()-fileName.size()-1);
  156. // if the framework has a path in it then just use the filename
  157. if(frameWorkName.find("/") != frameWorkName.npos)
  158. {
  159. fileName = file;
  160. frameWorkName = "";
  161. }
  162. if(frameWorkName.size())
  163. {
  164. std::string fpath = dir;
  165. fpath += "/";
  166. fpath += frameWorkName;
  167. fpath += ".framework";
  168. std::string intPath = fpath;
  169. intPath += "/Headers/";
  170. intPath += fileName;
  171. if(cmSystemTools::FileExists(intPath.c_str()))
  172. {
  173. if(this->IncludeFileInPath)
  174. {
  175. return intPath;
  176. }
  177. return fpath;
  178. }
  179. }
  180. }
  181. // if it is not found yet or not a framework header, then do a glob search
  182. // for all files in dir/*/Headers/
  183. cmStdString glob = dir;
  184. glob += "/*/Headers/";
  185. glob += file;
  186. cmsys::Glob globIt;
  187. globIt.FindFiles(glob);
  188. std::vector<std::string> files = globIt.GetFiles();
  189. if(files.size())
  190. {
  191. cmStdString fheader = cmSystemTools::CollapseFullPath(files[0].c_str());
  192. if(this->IncludeFileInPath)
  193. {
  194. return fheader;
  195. }
  196. fheader = cmSystemTools::GetFilenamePath(fheader);
  197. return fheader;
  198. }
  199. return "";
  200. }