cmSourceFileLocation.cxx 8.7 KB

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