cmSourceFileLocation.cxx 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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->GetCurrentBinaryDirectory());
  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 = this->Makefile->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->GetCurrentSourceDirectory();
  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() ||
  201. !cmSystemTools::ComparePath(this->Name, loc.Name))
  202. {
  203. return false;
  204. }
  205. }
  206. else
  207. {
  208. const cmSourceFileLocation* loc1;
  209. const cmSourceFileLocation* loc2;
  210. if(this->AmbiguousExtension)
  211. {
  212. // Only "this" extension is ambiguous.
  213. loc1 = &loc;
  214. loc2 = this;
  215. }
  216. else
  217. {
  218. // Only "loc" extension is ambiguous.
  219. loc1 = this;
  220. loc2 = &loc;
  221. }
  222. if(!loc1->MatchesAmbiguousExtension(*loc2))
  223. {
  224. return false;
  225. }
  226. }
  227. if(!this->AmbiguousDirectory && !loc.AmbiguousDirectory)
  228. {
  229. // Both sides have absolute directories.
  230. if(this->Directory != loc.Directory)
  231. {
  232. return false;
  233. }
  234. }
  235. else if(this->AmbiguousDirectory && loc.AmbiguousDirectory)
  236. {
  237. if (this->Makefile == loc.Makefile)
  238. {
  239. // Both sides have directories relative to the same location.
  240. if(this->Directory != loc.Directory)
  241. {
  242. return false;
  243. }
  244. }
  245. else
  246. {
  247. // Each side has a directory relative to a different location.
  248. // This can occur when referencing a source file from a different
  249. // directory. This is not yet allowed.
  250. this->Makefile->IssueMessage(
  251. cmake::INTERNAL_ERROR,
  252. "Matches error: Each side has a directory relative to a different "
  253. "location. This can occur when referencing a source file from a "
  254. "different directory. This is not yet allowed."
  255. );
  256. return false;
  257. }
  258. }
  259. else if(this->AmbiguousDirectory)
  260. {
  261. // Compare possible directory combinations.
  262. std::string const& srcDir =
  263. cmSystemTools::CollapseFullPath(
  264. this->Directory, this->Makefile->GetCurrentSourceDirectory());
  265. std::string const& binDir =
  266. cmSystemTools::CollapseFullPath(
  267. this->Directory, this->Makefile->GetCurrentBinaryDirectory());
  268. if(srcDir != loc.Directory &&
  269. binDir != loc.Directory)
  270. {
  271. return false;
  272. }
  273. }
  274. else if(loc.AmbiguousDirectory)
  275. {
  276. // Compare possible directory combinations.
  277. std::string const& srcDir =
  278. cmSystemTools::CollapseFullPath(
  279. loc.Directory, loc.Makefile->GetCurrentSourceDirectory());
  280. std::string const& binDir =
  281. cmSystemTools::CollapseFullPath(
  282. loc.Directory, loc.Makefile->GetCurrentBinaryDirectory());
  283. if(srcDir != this->Directory &&
  284. binDir != this->Directory)
  285. {
  286. return false;
  287. }
  288. }
  289. // File locations match.
  290. this->Update(loc);
  291. return true;
  292. }