cmSourceFileLocation.cxx 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmSourceFileLocation.h"
  11. #include "cmMakefile.h"
  12. #include "cmLocalGenerator.h"
  13. #include "cmGlobalGenerator.h"
  14. #include "cmSystemTools.h"
  15. #include "assert.h"
  16. //----------------------------------------------------------------------------
  17. cmSourceFileLocation::cmSourceFileLocation()
  18. : Makefile(0), AmbiguousDirectory(true), AmbiguousExtension(true)
  19. {
  20. }
  21. //----------------------------------------------------------------------------
  22. cmSourceFileLocation::cmSourceFileLocation(const cmSourceFileLocation& loc)
  23. : Makefile(loc.Makefile)
  24. {
  25. this->AmbiguousDirectory = loc.AmbiguousDirectory;
  26. this->AmbiguousExtension = loc.AmbiguousExtension;
  27. this->Directory = loc.Directory;
  28. this->Name = loc.Name;
  29. }
  30. //----------------------------------------------------------------------------
  31. cmSourceFileLocation&
  32. cmSourceFileLocation::operator=(const cmSourceFileLocation& loc)
  33. {
  34. if(this == &loc)
  35. {
  36. return *this;
  37. }
  38. this->Makefile = loc.Makefile;
  39. this->AmbiguousDirectory = loc.AmbiguousDirectory;
  40. this->AmbiguousExtension = loc.AmbiguousExtension;
  41. this->Directory = loc.Directory;
  42. this->Name = loc.Name;
  43. this->UpdateExtension(this->Name);
  44. return *this;
  45. }
  46. //----------------------------------------------------------------------------
  47. cmSourceFileLocation
  48. ::cmSourceFileLocation(cmMakefile const* mf, const std::string& name)
  49. : Makefile(mf)
  50. {
  51. this->AmbiguousDirectory = !cmSystemTools::FileIsFullPath(name.c_str());
  52. this->AmbiguousExtension = true;
  53. this->Directory = cmSystemTools::GetFilenamePath(name);
  54. this->Name = cmSystemTools::GetFilenameName(name);
  55. this->UpdateExtension(name);
  56. }
  57. //----------------------------------------------------------------------------
  58. void cmSourceFileLocation::Update(cmSourceFileLocation const& loc)
  59. {
  60. if(this->AmbiguousDirectory && !loc.AmbiguousDirectory)
  61. {
  62. this->Directory = loc.Directory;
  63. this->AmbiguousDirectory = false;
  64. }
  65. if(this->AmbiguousExtension && !loc.AmbiguousExtension)
  66. {
  67. this->Name = loc.Name;
  68. this->AmbiguousExtension = false;
  69. }
  70. }
  71. //----------------------------------------------------------------------------
  72. void cmSourceFileLocation::DirectoryUseSource()
  73. {
  74. assert(this->Makefile);
  75. if(this->AmbiguousDirectory)
  76. {
  77. this->Directory =
  78. cmSystemTools::CollapseFullPath(
  79. this->Directory.c_str(), this->Makefile->GetCurrentDirectory());
  80. this->AmbiguousDirectory = false;
  81. }
  82. }
  83. //----------------------------------------------------------------------------
  84. void cmSourceFileLocation::DirectoryUseBinary()
  85. {
  86. assert(this->Makefile);
  87. if(this->AmbiguousDirectory)
  88. {
  89. this->Directory =
  90. cmSystemTools::CollapseFullPath(
  91. this->Directory.c_str(), this->Makefile->GetCurrentOutputDirectory());
  92. this->AmbiguousDirectory = false;
  93. }
  94. }
  95. //----------------------------------------------------------------------------
  96. void cmSourceFileLocation::UpdateExtension(const std::string& name)
  97. {
  98. assert(this->Makefile);
  99. // Check the extension.
  100. std::string ext = cmSystemTools::GetFilenameLastExtension(name);
  101. if(!ext.empty()) { ext = ext.substr(1); }
  102. // The global generator checks extensions of enabled languages.
  103. cmGlobalGenerator* gg =
  104. this->Makefile->GetLocalGenerator()->GetGlobalGenerator();
  105. cmMakefile const* mf = this->Makefile;
  106. const std::vector<std::string>& srcExts = mf->GetSourceExtensions();
  107. const std::vector<std::string>& hdrExts = mf->GetHeaderExtensions();
  108. if(!gg->GetLanguageFromExtension(ext.c_str()).empty() ||
  109. std::find(srcExts.begin(), srcExts.end(), ext) != srcExts.end() ||
  110. std::find(hdrExts.begin(), hdrExts.end(), ext) != hdrExts.end())
  111. {
  112. // This is a known extension. Use the given filename with extension.
  113. this->Name = cmSystemTools::GetFilenameName(name);
  114. this->AmbiguousExtension = false;
  115. }
  116. else
  117. {
  118. // This is not a known extension. See if the file exists on disk as
  119. // named.
  120. std::string tryPath;
  121. if(this->AmbiguousDirectory)
  122. {
  123. // Check the source tree only because a file in the build tree should
  124. // be specified by full path at least once. We do not want this
  125. // detection to depend on whether the project has already been built.
  126. tryPath = this->Makefile->GetCurrentDirectory();
  127. tryPath += "/";
  128. }
  129. if(!this->Directory.empty())
  130. {
  131. tryPath += this->Directory;
  132. tryPath += "/";
  133. }
  134. tryPath += this->Name;
  135. if(cmSystemTools::FileExists(tryPath.c_str(), true))
  136. {
  137. // We found a source file named by the user on disk. Trust it's
  138. // extension.
  139. this->Name = cmSystemTools::GetFilenameName(name);
  140. this->AmbiguousExtension = false;
  141. // If the directory was ambiguous, it isn't anymore.
  142. if(this->AmbiguousDirectory)
  143. {
  144. this->DirectoryUseSource();
  145. }
  146. }
  147. }
  148. }
  149. //----------------------------------------------------------------------------
  150. bool
  151. cmSourceFileLocation
  152. ::MatchesAmbiguousExtension(cmSourceFileLocation const& loc) const
  153. {
  154. assert(this->Makefile);
  155. // This location's extension is not ambiguous but loc's extension
  156. // is. See if the names match as-is.
  157. if(this->Name == loc.Name)
  158. {
  159. return true;
  160. }
  161. // Check if loc's name could possibly be extended to our name by
  162. // adding an extension.
  163. if(!(this->Name.size() > loc.Name.size() &&
  164. this->Name.substr(0, loc.Name.size()) == loc.Name &&
  165. this->Name[loc.Name.size()] == '.'))
  166. {
  167. return false;
  168. }
  169. // Only a fixed set of extensions will be tried to match a file on
  170. // disk. One of these must match if loc refers to this source file.
  171. std::string ext = this->Name.substr(loc.Name.size()+1);
  172. cmMakefile const* mf = this->Makefile;
  173. const std::vector<std::string>& srcExts = mf->GetSourceExtensions();
  174. if(std::find(srcExts.begin(), srcExts.end(), ext) != srcExts.end())
  175. {
  176. return true;
  177. }
  178. const std::vector<std::string>& hdrExts = mf->GetHeaderExtensions();
  179. if(std::find(hdrExts.begin(), hdrExts.end(), ext) != hdrExts.end())
  180. {
  181. return true;
  182. }
  183. return false;
  184. }
  185. //----------------------------------------------------------------------------
  186. bool cmSourceFileLocation::Matches(cmSourceFileLocation const& loc)
  187. {
  188. assert(this->Makefile);
  189. if(this->AmbiguousExtension && loc.AmbiguousExtension)
  190. {
  191. // Both extensions are ambiguous. Since only the old fixed set of
  192. // extensions will be tried, the names must match at this point to
  193. // be the same file.
  194. if(this->Name != loc.Name)
  195. {
  196. return false;
  197. }
  198. }
  199. else if(this->AmbiguousExtension)
  200. {
  201. // Only "this" extension is ambiguous.
  202. if(!loc.MatchesAmbiguousExtension(*this))
  203. {
  204. return false;
  205. }
  206. }
  207. else if(loc.AmbiguousExtension)
  208. {
  209. // Only "loc" extension is ambiguous.
  210. if(!this->MatchesAmbiguousExtension(loc))
  211. {
  212. return false;
  213. }
  214. }
  215. else
  216. {
  217. // Neither extension is ambiguous.
  218. if(this->Name != loc.Name)
  219. {
  220. return false;
  221. }
  222. }
  223. if(!this->AmbiguousDirectory && !loc.AmbiguousDirectory)
  224. {
  225. // Both sides have absolute directories.
  226. if(this->Directory != loc.Directory)
  227. {
  228. return false;
  229. }
  230. }
  231. else if(this->AmbiguousDirectory && loc.AmbiguousDirectory &&
  232. this->Makefile == loc.Makefile)
  233. {
  234. // Both sides have directories relative to the same location.
  235. if(this->Directory != loc.Directory)
  236. {
  237. return false;
  238. }
  239. }
  240. else if(this->AmbiguousDirectory && loc.AmbiguousDirectory)
  241. {
  242. // Each side has a directory relative to a different location.
  243. // This can occur when referencing a source file from a different
  244. // directory. This is not yet allowed.
  245. this->Makefile->IssueMessage(
  246. cmake::INTERNAL_ERROR,
  247. "Matches error: Each side has a directory relative to a different "
  248. "location. This can occur when referencing a source file from a "
  249. "different directory. This is not yet allowed."
  250. );
  251. return false;
  252. }
  253. else if(this->AmbiguousDirectory)
  254. {
  255. // Compare possible directory combinations.
  256. std::string srcDir =
  257. cmSystemTools::CollapseFullPath(
  258. this->Directory.c_str(), this->Makefile->GetCurrentDirectory());
  259. std::string binDir =
  260. cmSystemTools::CollapseFullPath(
  261. this->Directory.c_str(), this->Makefile->GetCurrentOutputDirectory());
  262. if(srcDir != loc.Directory &&
  263. binDir != loc.Directory)
  264. {
  265. return false;
  266. }
  267. }
  268. else if(loc.AmbiguousDirectory)
  269. {
  270. // Compare possible directory combinations.
  271. std::string srcDir =
  272. cmSystemTools::CollapseFullPath(
  273. loc.Directory.c_str(), loc.Makefile->GetCurrentDirectory());
  274. std::string binDir =
  275. cmSystemTools::CollapseFullPath(
  276. loc.Directory.c_str(), loc.Makefile->GetCurrentOutputDirectory());
  277. if(srcDir != this->Directory &&
  278. binDir != this->Directory)
  279. {
  280. return false;
  281. }
  282. }
  283. // File locations match.
  284. this->Update(loc);
  285. return true;
  286. }