cmSourceFileLocation.cxx 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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
  141. cmSourceFileLocation
  142. ::MatchesAmbiguousExtension(cmSourceFileLocation const& loc) const
  143. {
  144. // This location's extension is not ambiguous but loc's extension
  145. // is. See if the names match as-is.
  146. if(this->Name == loc.Name)
  147. {
  148. return true;
  149. }
  150. // Check if loc's name could possibly be extended to our name by
  151. // adding an extension.
  152. if(!(this->Name.size() > loc.Name.size() &&
  153. this->Name.substr(0, loc.Name.size()) == loc.Name &&
  154. this->Name[loc.Name.size()] == '.'))
  155. {
  156. return false;
  157. }
  158. // Only a fixed set of extensions will be tried to match a file on
  159. // disk. One of these must match if loc refers to this source file.
  160. std::string ext = this->Name.substr(loc.Name.size()+1);
  161. cmMakefile* mf = this->Makefile;
  162. const std::vector<std::string>& srcExts = mf->GetSourceExtensions();
  163. if(std::find(srcExts.begin(), srcExts.end(), ext) != srcExts.end())
  164. {
  165. return true;
  166. }
  167. const std::vector<std::string>& hdrExts = mf->GetHeaderExtensions();
  168. if(std::find(hdrExts.begin(), hdrExts.end(), ext) != hdrExts.end())
  169. {
  170. return true;
  171. }
  172. return false;
  173. }
  174. //----------------------------------------------------------------------------
  175. bool cmSourceFileLocation::Matches(cmSourceFileLocation const& loc)
  176. {
  177. if(this->AmbiguousExtension && loc.AmbiguousExtension)
  178. {
  179. // Both extensions are ambiguous. Since only the old fixed set of
  180. // extensions will be tried, the names must match at this point to
  181. // be the same file.
  182. if(this->Name != loc.Name)
  183. {
  184. return false;
  185. }
  186. }
  187. else if(this->AmbiguousExtension)
  188. {
  189. // Only "this" extension is ambiguous.
  190. if(!loc.MatchesAmbiguousExtension(*this))
  191. {
  192. return false;
  193. }
  194. }
  195. else if(loc.AmbiguousExtension)
  196. {
  197. // Only "loc" extension is ambiguous.
  198. if(!this->MatchesAmbiguousExtension(loc))
  199. {
  200. return false;
  201. }
  202. }
  203. else
  204. {
  205. // Neither extension is ambiguous.
  206. if(this->Name != loc.Name)
  207. {
  208. return false;
  209. }
  210. }
  211. if(!this->AmbiguousDirectory && !loc.AmbiguousDirectory)
  212. {
  213. // Both sides have absolute directories.
  214. if(this->Directory != loc.Directory)
  215. {
  216. return false;
  217. }
  218. }
  219. else if(this->AmbiguousDirectory && loc.AmbiguousDirectory &&
  220. this->Makefile == loc.Makefile)
  221. {
  222. // Both sides have directories relative to the same location.
  223. if(this->Directory != loc.Directory)
  224. {
  225. return false;
  226. }
  227. }
  228. else if(this->AmbiguousDirectory && loc.AmbiguousDirectory)
  229. {
  230. // Each side has a directory relative to a different location.
  231. // This can occur when referencing a source file from a different
  232. // directory. This is not yet allowed.
  233. this->Makefile->IssueMessage(
  234. cmake::INTERNAL_ERROR,
  235. "Matches error: Each side has a directory relative to a different "
  236. "location. This can occur when referencing a source file from a "
  237. "different directory. This is not yet allowed."
  238. );
  239. return false;
  240. }
  241. else if(this->AmbiguousDirectory)
  242. {
  243. // Compare possible directory combinations.
  244. std::string srcDir =
  245. cmSystemTools::CollapseFullPath(
  246. this->Directory.c_str(), this->Makefile->GetCurrentDirectory());
  247. std::string binDir =
  248. cmSystemTools::CollapseFullPath(
  249. this->Directory.c_str(), this->Makefile->GetCurrentOutputDirectory());
  250. if(srcDir != loc.Directory &&
  251. binDir != loc.Directory)
  252. {
  253. return false;
  254. }
  255. }
  256. else if(loc.AmbiguousDirectory)
  257. {
  258. // Compare possible directory combinations.
  259. std::string srcDir =
  260. cmSystemTools::CollapseFullPath(
  261. loc.Directory.c_str(), loc.Makefile->GetCurrentDirectory());
  262. std::string binDir =
  263. cmSystemTools::CollapseFullPath(
  264. loc.Directory.c_str(), loc.Makefile->GetCurrentOutputDirectory());
  265. if(srcDir != this->Directory &&
  266. binDir != this->Directory)
  267. {
  268. return false;
  269. }
  270. }
  271. // File locations match.
  272. this->Update(loc);
  273. return true;
  274. }