cmSourceFileLocation.cxx 9.6 KB

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