cmSourceFileLocation.cxx 7.7 KB

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