cmSourceFileLocation.cxx 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 "cmSourceFileLocation.h"
  14. #include "cmMakefile.h"
  15. #include "cmSystemTools.h"
  16. //----------------------------------------------------------------------------
  17. cmSourceFileLocation
  18. ::cmSourceFileLocation(cmMakefile* mf, const char* name): Makefile(mf)
  19. {
  20. this->AmbiguousDirectory = !cmSystemTools::FileIsFullPath(name);
  21. this->AmbiguousExtension = true;
  22. this->Directory = cmSystemTools::GetFilenamePath(name);
  23. this->Name = cmSystemTools::GetFilenameName(name);
  24. this->UpdateExtension(name);
  25. }
  26. //----------------------------------------------------------------------------
  27. void cmSourceFileLocation::Update(const char* name)
  28. {
  29. if(this->AmbiguousDirectory)
  30. {
  31. this->UpdateDirectory(name);
  32. }
  33. if(this->AmbiguousExtension)
  34. {
  35. this->UpdateExtension(name);
  36. }
  37. }
  38. //----------------------------------------------------------------------------
  39. void cmSourceFileLocation::Update(cmSourceFileLocation const& loc)
  40. {
  41. if(this->AmbiguousDirectory && !loc.AmbiguousDirectory)
  42. {
  43. this->Directory = loc.Directory;
  44. this->AmbiguousDirectory = false;
  45. }
  46. if(this->AmbiguousExtension && !loc.AmbiguousExtension)
  47. {
  48. this->Name = loc.Name;
  49. this->AmbiguousExtension = false;
  50. }
  51. }
  52. //----------------------------------------------------------------------------
  53. void cmSourceFileLocation::DirectoryUseSource()
  54. {
  55. if(this->AmbiguousDirectory)
  56. {
  57. this->Directory =
  58. cmSystemTools::CollapseFullPath(
  59. this->Directory.c_str(), this->Makefile->GetCurrentDirectory());
  60. this->AmbiguousDirectory = false;
  61. }
  62. }
  63. //----------------------------------------------------------------------------
  64. void cmSourceFileLocation::DirectoryUseBinary()
  65. {
  66. if(this->AmbiguousDirectory)
  67. {
  68. this->Directory =
  69. cmSystemTools::CollapseFullPath(
  70. this->Directory.c_str(), this->Makefile->GetCurrentOutputDirectory());
  71. this->AmbiguousDirectory = false;
  72. }
  73. }
  74. //----------------------------------------------------------------------------
  75. void cmSourceFileLocation::UpdateExtension(const char* name)
  76. {
  77. // Check the extension.
  78. std::string ext = cmSystemTools::GetFilenameLastExtension(name);
  79. if(!ext.empty()) { ext = ext.substr(1); }
  80. // TODO: Let enable-language specify extensions for each language.
  81. cmMakefile const* mf = this->Makefile;
  82. const std::vector<std::string>& srcExts = mf->GetSourceExtensions();
  83. const std::vector<std::string>& hdrExts = mf->GetHeaderExtensions();
  84. if(std::find(srcExts.begin(), srcExts.end(), ext) != srcExts.end() ||
  85. std::find(hdrExts.begin(), hdrExts.end(), ext) != hdrExts.end())
  86. {
  87. // This is a known extension. Use the given filename with extension.
  88. this->Name = cmSystemTools::GetFilenameName(name);
  89. this->AmbiguousExtension = false;
  90. }
  91. else
  92. {
  93. // This is not a known extension. See if the file exists on disk as
  94. // named.
  95. std::string tryPath;
  96. if(this->AmbiguousDirectory)
  97. {
  98. // Check the source tree only because a file in the build tree should
  99. // be specified by full path at least once. We do not want this
  100. // detection to depend on whether the project has already been built.
  101. tryPath = this->Makefile->GetCurrentDirectory();
  102. tryPath += "/";
  103. }
  104. tryPath += this->Directory;
  105. tryPath += "/";
  106. tryPath += this->Name;
  107. if(cmSystemTools::FileExists(tryPath.c_str(), true))
  108. {
  109. // We found a source file named by the user on disk. Trust it's
  110. // extension.
  111. this->Name = cmSystemTools::GetFilenameName(name);
  112. this->AmbiguousExtension = false;
  113. // If the directory was ambiguous, it isn't anymore.
  114. if(this->AmbiguousDirectory)
  115. {
  116. this->DirectoryUseSource();
  117. }
  118. }
  119. }
  120. }
  121. //----------------------------------------------------------------------------
  122. void cmSourceFileLocation::UpdateDirectory(const char* name)
  123. {
  124. // If a full path was given we know the directory.
  125. if(cmSystemTools::FileIsFullPath(name))
  126. {
  127. this->Directory = cmSystemTools::GetFilenamePath(name);
  128. this->AmbiguousDirectory = false;
  129. }
  130. }
  131. //----------------------------------------------------------------------------
  132. bool cmSourceFileLocation::Matches(cmSourceFileLocation const& loc)
  133. {
  134. if(this->AmbiguousExtension || loc.AmbiguousExtension)
  135. {
  136. // Need to compare without the file extension.
  137. std::string thisName;
  138. if(this->AmbiguousExtension)
  139. {
  140. thisName = this->Name;
  141. }
  142. else
  143. {
  144. thisName = cmSystemTools::GetFilenameWithoutLastExtension(this->Name);
  145. }
  146. std::string locName;
  147. if(loc.AmbiguousExtension)
  148. {
  149. locName = loc.Name;
  150. }
  151. else
  152. {
  153. locName = cmSystemTools::GetFilenameWithoutLastExtension(loc.Name);
  154. }
  155. if(thisName != locName)
  156. {
  157. return false;
  158. }
  159. }
  160. else
  161. {
  162. // Compare with extension.
  163. if(this->Name != loc.Name)
  164. {
  165. return false;
  166. }
  167. }
  168. if(!this->AmbiguousDirectory && !loc.AmbiguousDirectory)
  169. {
  170. // Both sides have absolute directories.
  171. if(this->Directory != loc.Directory)
  172. {
  173. return false;
  174. }
  175. }
  176. else if(this->AmbiguousDirectory && loc.AmbiguousDirectory &&
  177. this->Makefile == loc.Makefile)
  178. {
  179. // Both sides have directories relative to the same location.
  180. if(this->Directory != loc.Directory)
  181. {
  182. return false;
  183. }
  184. }
  185. else if(this->AmbiguousDirectory && loc.AmbiguousDirectory)
  186. {
  187. // Each side has a directory relative to a different location.
  188. // This can occur when referencing a source file from a different
  189. // directory. This is not yet allowed.
  190. this->Makefile->IssueMessage(
  191. cmake::INTERNAL_ERROR,
  192. "Matches error: Each side has a directory relative to a different "
  193. "location. This can occur when referencing a source file from a "
  194. "different directory. This is not yet allowed."
  195. );
  196. return false;
  197. }
  198. else if(this->AmbiguousDirectory)
  199. {
  200. // Compare possible directory combinations.
  201. std::string srcDir =
  202. cmSystemTools::CollapseFullPath(
  203. this->Directory.c_str(), this->Makefile->GetCurrentDirectory());
  204. std::string binDir =
  205. cmSystemTools::CollapseFullPath(
  206. this->Directory.c_str(), this->Makefile->GetCurrentOutputDirectory());
  207. if(srcDir != loc.Directory &&
  208. binDir != loc.Directory)
  209. {
  210. return false;
  211. }
  212. }
  213. else if(loc.AmbiguousDirectory)
  214. {
  215. // Compare possible directory combinations.
  216. std::string srcDir =
  217. cmSystemTools::CollapseFullPath(
  218. loc.Directory.c_str(), loc.Makefile->GetCurrentDirectory());
  219. std::string binDir =
  220. cmSystemTools::CollapseFullPath(
  221. loc.Directory.c_str(), loc.Makefile->GetCurrentOutputDirectory());
  222. if(srcDir != this->Directory &&
  223. binDir != this->Directory)
  224. {
  225. return false;
  226. }
  227. }
  228. // File locations match.
  229. this->Update(loc);
  230. return true;
  231. }