cmSourceFileLocation.cxx 7.6 KB

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