1
0

cmFindPathCommand.cxx 6.2 KB

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