Version.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 "llvm/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. #if defined(CLANG_REPOSITORY_STRING)
  25. return CLANG_REPOSITORY_STRING;
  26. #else
  27. #ifdef SVN_REPOSITORY
  28. StringRef URL(SVN_REPOSITORY);
  29. #else
  30. StringRef URL("");
  31. #endif
  32. // If the SVN_REPOSITORY is empty, try to use the SVN keyword. This helps us
  33. // pick up a tag in an SVN export, for example.
  34. StringRef SVNRepository("$URL$");
  35. if (URL.empty()) {
  36. URL = SVNRepository.slice(SVNRepository.find(':'),
  37. SVNRepository.find("/lib/Basic"));
  38. }
  39. // Strip off version from a build from an integration branch.
  40. URL = URL.slice(0, URL.find("/src/tools/clang"));
  41. // Trim path prefix off, assuming path came from standard cfe path.
  42. size_t Start = URL.find("cfe/");
  43. if (Start != StringRef::npos)
  44. URL = URL.substr(Start + 4);
  45. return URL;
  46. #endif
  47. }
  48. std::string getLLVMRepositoryPath() {
  49. #ifdef LLVM_REPOSITORY
  50. StringRef URL(LLVM_REPOSITORY);
  51. #else
  52. StringRef URL("");
  53. #endif
  54. // Trim path prefix off, assuming path came from standard llvm path.
  55. // Leave "llvm/" prefix to distinguish the following llvm revision from the
  56. // clang revision.
  57. size_t Start = URL.find("llvm/");
  58. if (Start != StringRef::npos)
  59. URL = URL.substr(Start);
  60. return URL;
  61. }
  62. std::string getClangRevision() {
  63. #ifdef SVN_REVISION
  64. return SVN_REVISION;
  65. #else
  66. return "";
  67. #endif
  68. }
  69. std::string getLLVMRevision() {
  70. #ifdef LLVM_REVISION
  71. return LLVM_REVISION;
  72. #else
  73. return "";
  74. #endif
  75. }
  76. std::string getClangFullRepositoryVersion() {
  77. std::string buf;
  78. llvm::raw_string_ostream OS(buf);
  79. std::string Path = getClangRepositoryPath();
  80. std::string Revision = getClangRevision();
  81. if (!Path.empty() || !Revision.empty()) {
  82. OS << '(';
  83. if (!Path.empty())
  84. OS << Path;
  85. if (!Revision.empty()) {
  86. if (!Path.empty())
  87. OS << ' ';
  88. OS << Revision;
  89. }
  90. OS << ')';
  91. }
  92. // Support LLVM in a separate repository.
  93. std::string LLVMRev = getLLVMRevision();
  94. if (!LLVMRev.empty() && LLVMRev != Revision) {
  95. OS << " (";
  96. std::string LLVMRepo = getLLVMRepositoryPath();
  97. if (!LLVMRepo.empty())
  98. OS << LLVMRepo << ' ';
  99. OS << LLVMRev << ')';
  100. }
  101. return OS.str();
  102. }
  103. std::string getClangFullVersion() {
  104. std::string buf;
  105. llvm::raw_string_ostream OS(buf);
  106. #ifdef CLANG_VENDOR
  107. OS << CLANG_VENDOR;
  108. #endif
  109. OS << "clang version " CLANG_VERSION_STRING " "
  110. << getClangFullRepositoryVersion();
  111. // If vendor supplied, include the base LLVM version as well.
  112. #ifdef CLANG_VENDOR
  113. OS << " (based on LLVM " << PACKAGE_VERSION << ")";
  114. #endif
  115. return OS.str();
  116. }
  117. std::string getClangFullCPPVersion() {
  118. // The version string we report in __VERSION__ is just a compacted version of
  119. // the one we report on the command line.
  120. std::string buf;
  121. llvm::raw_string_ostream OS(buf);
  122. #ifdef CLANG_VENDOR
  123. OS << CLANG_VENDOR;
  124. #endif
  125. OS << "Clang " CLANG_VERSION_STRING " " << getClangFullRepositoryVersion();
  126. return OS.str();
  127. }
  128. } // end namespace clang