cmSourceFileLocation.cxx 7.5 KB

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