cmSourceFileLocation.cxx 9.7 KB

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