cmFindPathCommand.cxx 6.1 KB

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