cmSourceFileLocation.cxx 7.7 KB

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