Version.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //===- Version.cpp - Clang Version Number -----------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file defines several version-related utility functions for Clang.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "clang/Basic/Version.h"
  13. #include "clang/Basic/LLVM.h"
  14. #include "clang/Config/config.h"
  15. #include "llvm/Support/raw_ostream.h"
  16. #include <cstdlib>
  17. #include <cstring>
  18. #ifdef HAVE_VCS_VERSION_INC
  19. #include "VCSVersion.inc"
  20. #endif
  21. namespace clang {
  22. std::string getClangRepositoryPath() {
  23. #if defined(CLANG_REPOSITORY_STRING)
  24. return CLANG_REPOSITORY_STRING;
  25. #else
  26. #ifdef CLANG_REPOSITORY
  27. StringRef URL(CLANG_REPOSITORY);
  28. #else
  29. StringRef URL("");
  30. #endif
  31. // If the CLANG_REPOSITORY is empty, try to use the SVN keyword. This helps us
  32. // pick up a tag in an SVN export, for example.
  33. StringRef SVNRepository("$URL$");
  34. if (URL.empty()) {
  35. URL = SVNRepository.slice(SVNRepository.find(':'),
  36. SVNRepository.find("/lib/Basic"));
  37. }
  38. // Strip off version from a build from an integration branch.
  39. URL = URL.slice(0, URL.find("/src/tools/clang"));
  40. // Trim path prefix off, assuming path came from standard cfe path.
  41. size_t Start = URL.find("cfe/");
  42. if (Start != StringRef::npos)
  43. URL = URL.substr(Start + 4);
  44. return URL;
  45. #endif
  46. }
  47. std::string getLLVMRepositoryPath() {
  48. #ifdef LLVM_REPOSITORY
  49. StringRef URL(LLVM_REPOSITORY);
  50. #else
  51. StringRef URL("");
  52. #endif
  53. // Trim path prefix off, assuming path came from standard llvm path.
  54. // Leave "llvm/" prefix to distinguish the following llvm revision from the
  55. // clang revision.
  56. size_t Start = URL.find("llvm/");
  57. if (Start != StringRef::npos)
  58. URL = URL.substr(Start);
  59. return URL;
  60. }
  61. std::string getClangRevision() {
  62. #ifdef CLANG_REVISION
  63. return CLANG_REVISION;
  64. #else
  65. return "";
  66. #endif
  67. }
  68. std::string getLLVMRevision() {
  69. #ifdef LLVM_REVISION
  70. return LLVM_REVISION;
  71. #else
  72. return "";
  73. #endif
  74. }
  75. std::string getClangFullRepositoryVersion() {
  76. std::string buf;
  77. llvm::raw_string_ostream OS(buf);
  78. std::string Path = getClangRepositoryPath();
  79. std::string Revision = getClangRevision();
  80. if (!Path.empty() || !Revision.empty()) {
  81. OS << '(';
  82. if (!Path.empty())
  83. OS << Path;
  84. if (!Revision.empty()) {
  85. if (!Path.empty())
  86. OS << ' ';
  87. OS << Revision;
  88. }
  89. OS << ')';
  90. }
  91. // Support LLVM in a separate repository.
  92. std::string LLVMRev = getLLVMRevision();
  93. if (!LLVMRev.empty() && LLVMRev != Revision) {
  94. OS << " (";
  95. std::string LLVMRepo = getLLVMRepositoryPath();
  96. if (!LLVMRepo.empty())
  97. OS << LLVMRepo << ' ';
  98. OS << LLVMRev << ')';
  99. }
  100. return OS.str();
  101. }
  102. std::string getClangFullVersion() {
  103. return getClangToolFullVersion("clang");
  104. }
  105. std::string getClangToolFullVersion(StringRef ToolName) {
  106. std::string buf;
  107. llvm::raw_string_ostream OS(buf);
  108. #ifdef CLANG_VENDOR
  109. OS << CLANG_VENDOR;
  110. #endif
  111. OS << ToolName << " version " CLANG_VERSION_STRING " "
  112. << getClangFullRepositoryVersion();
  113. // If vendor supplied, include the base LLVM version as well.
  114. #ifdef CLANG_VENDOR
  115. OS << " (based on " << BACKEND_PACKAGE_STRING << ")";
  116. #endif
  117. return OS.str();
  118. }
  119. std::string getClangFullCPPVersion() {
  120. // The version string we report in __VERSION__ is just a compacted version of
  121. // the one we report on the command line.
  122. std::string buf;
  123. llvm::raw_string_ostream OS(buf);
  124. #ifdef CLANG_VENDOR
  125. OS << CLANG_VENDOR;
  126. #endif
  127. OS << "Clang " CLANG_VERSION_STRING " " << getClangFullRepositoryVersion();
  128. return OS.str();
  129. }
  130. } // end namespace clang