Version.cpp 3.6 KB

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