Version.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. //===- Version.cpp - Clang Version Number -----------------------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines several version-related utility functions for Clang.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/Basic/Version.h"
  14. #include "clang/Basic/LLVM.h"
  15. #include "clang/Config/config.h"
  16. #include "llvm/Support/raw_ostream.h"
  17. #include <cstdlib>
  18. #include <cstring>
  19. #ifdef HAVE_SVN_VERSION_INC
  20. # include "SVNVersion.inc"
  21. #endif
  22. namespace clang {
  23. std::string getClangRepositoryPath() {
  24. #ifdef HLSL_FIXED_VER // HLSL Change Starts
  25. return std::string();
  26. #else
  27. #if defined(CLANG_REPOSITORY_STRING)
  28. return CLANG_REPOSITORY_STRING;
  29. #else
  30. #ifdef SVN_REPOSITORY
  31. StringRef URL(SVN_REPOSITORY);
  32. #else
  33. StringRef URL("");
  34. #endif
  35. // If the SVN_REPOSITORY is empty, try to use the SVN keyword. This helps us
  36. // pick up a tag in an SVN export, for example.
  37. StringRef SVNRepository("$URL: https://llvm.org/svn/llvm-project/cfe/tags/RELEASE_370/final/lib/Basic/Version.cpp $");
  38. if (URL.empty()) {
  39. URL = SVNRepository.slice(SVNRepository.find(':'),
  40. SVNRepository.find("/lib/Basic"));
  41. }
  42. // Strip off version from a build from an integration branch.
  43. URL = URL.slice(0, URL.find("/src/tools/clang"));
  44. // Trim path prefix off, assuming path came from standard cfe path.
  45. size_t Start = URL.find("cfe/");
  46. if (Start != StringRef::npos)
  47. URL = URL.substr(Start + 4);
  48. return URL;
  49. #endif
  50. #endif // HLSL Change Ends
  51. }
  52. std::string getLLVMRepositoryPath() {
  53. #ifdef HLSL_FIXED_VER // HLSL Change Starts
  54. return std::string();
  55. #else
  56. #ifdef LLVM_REPOSITORY
  57. StringRef URL(LLVM_REPOSITORY);
  58. #else
  59. StringRef URL("");
  60. #endif
  61. // Trim path prefix off, assuming path came from standard llvm path.
  62. // Leave "llvm/" prefix to distinguish the following llvm revision from the
  63. // clang revision.
  64. size_t Start = URL.find("llvm/");
  65. if (Start != StringRef::npos)
  66. URL = URL.substr(Start);
  67. return URL;
  68. #endif // HLSL Change Ends
  69. }
  70. std::string getClangRevision() {
  71. #ifdef HLSL_FIXED_VER // HLSL Change Starts
  72. return std::string();
  73. #else
  74. #ifdef SVN_REVISION
  75. return SVN_REVISION;
  76. #else
  77. return "";
  78. #endif
  79. #endif // HLSL Change Ends
  80. }
  81. std::string getLLVMRevision() {
  82. #ifdef HLSL_FIXED_VER // HLSL Change Starts
  83. return std::string();
  84. #else
  85. #ifdef LLVM_REVISION
  86. return LLVM_REVISION;
  87. #else
  88. return "";
  89. #endif
  90. #endif // HLSL Change Ends
  91. }
  92. std::string getClangFullRepositoryVersion() {
  93. #ifdef HLSL_FIXED_VER // HLSL Change Starts
  94. return std::string();
  95. #else
  96. std::string buf;
  97. llvm::raw_string_ostream OS(buf);
  98. std::string Path = getClangRepositoryPath();
  99. std::string Revision = getClangRevision();
  100. if (!Path.empty() || !Revision.empty()) {
  101. OS << '(';
  102. if (!Path.empty())
  103. OS << Path;
  104. if (!Revision.empty()) {
  105. if (!Path.empty())
  106. OS << ' ';
  107. OS << Revision;
  108. }
  109. OS << ')';
  110. }
  111. // Support LLVM in a separate repository.
  112. std::string LLVMRev = getLLVMRevision();
  113. if (!LLVMRev.empty() && LLVMRev != Revision) {
  114. OS << " (";
  115. std::string LLVMRepo = getLLVMRepositoryPath();
  116. if (!LLVMRepo.empty())
  117. OS << LLVMRepo << ' ';
  118. OS << LLVMRev << ')';
  119. }
  120. return OS.str();
  121. #endif
  122. }
  123. std::string getClangFullVersion() {
  124. return getClangToolFullVersion("clang");
  125. }
  126. std::string getClangToolFullVersion(StringRef ToolName) {
  127. #ifdef HLSL_FIXED_VER // HLSL Change Starts
  128. // We fix a specific version for builds that are released;
  129. // this allows tools to pick a known version for a given !llvm.ident value.
  130. return std::string(HLSL_FIXED_VER);
  131. #else
  132. std::string buf;
  133. llvm::raw_string_ostream OS(buf);
  134. #ifdef CLANG_VENDOR
  135. OS << CLANG_VENDOR;
  136. #endif
  137. OS << ToolName << " version " CLANG_VERSION_STRING " "
  138. << getClangFullRepositoryVersion();
  139. // If vendor supplied, include the base LLVM version as well.
  140. #ifdef CLANG_VENDOR
  141. OS << " (based on " << BACKEND_PACKAGE_STRING << ")";
  142. #endif
  143. return OS.str();
  144. #endif // HLSL Change Ends
  145. }
  146. std::string getClangFullCPPVersion() {
  147. #ifdef HLSL_FIXED_VER // HLSL Change Starts
  148. // We fix a specific version for builds that are released;
  149. // this allows tools to pick a known version for a given !llvm.ident value.
  150. return std::string(HLSL_FIXED_VER);
  151. #else
  152. // The version string we report in __VERSION__ is just a compacted version of
  153. // the one we report on the command line.
  154. std::string buf;
  155. llvm::raw_string_ostream OS(buf);
  156. #ifdef CLANG_VENDOR
  157. OS << CLANG_VENDOR;
  158. #endif
  159. OS << "unofficial";
  160. return OS.str();
  161. #endif // HLSL Change Ends
  162. }
  163. } // end namespace clang