Version.cpp revision 226633
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
14#include "clang/Basic/Version.h"
15#include "clang/Basic/LLVM.h"
16#include "llvm/Support/raw_ostream.h"
17#include "llvm/Config/config.h"
18#include <cstring>
19#include <cstdlib>
20
21namespace clang {
22
23std::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
33  // If the SVN_REPOSITORY is empty, try to use the SVN keyword. This helps us
34  // pick up a tag in an SVN export, for example.
35  static StringRef SVNRepository("$URL: http://llvm.org/svn/llvm-project/cfe/branches/release_30/lib/Basic/Version.cpp $");
36  if (URL.empty()) {
37    URL = SVNRepository.slice(SVNRepository.find(':'),
38                              SVNRepository.find("/lib/Basic"));
39  }
40
41  // Strip off version from a build from an integration branch.
42  URL = URL.slice(0, URL.find("/src/tools/clang"));
43
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
49  return URL;
50#endif
51}
52
53std::string getClangRevision() {
54#ifdef SVN_REVISION
55  return SVN_REVISION;
56#else
57  return "";
58#endif
59}
60
61std::string getClangFullRepositoryVersion() {
62  std::string buf;
63  llvm::raw_string_ostream OS(buf);
64  std::string Path = getClangRepositoryPath();
65  std::string Revision = getClangRevision();
66  if (!Path.empty())
67    OS << Path;
68  if (!Revision.empty()) {
69    if (!Path.empty())
70      OS << ' ';
71    OS << Revision;
72  }
73  return OS.str();
74}
75
76std::string getClangFullVersion() {
77  std::string buf;
78  llvm::raw_string_ostream OS(buf);
79#ifdef CLANG_VENDOR
80  OS << CLANG_VENDOR;
81#endif
82  OS << "clang version " CLANG_VERSION_STRING " ("
83     << getClangFullRepositoryVersion() << ')';
84
85#ifdef CLANG_VENDOR_SUFFIX
86  OS << CLANG_VENDOR_SUFFIX;
87#elif defined(CLANG_VENDOR)
88  // If vendor supplied, include the base LLVM version as well.
89  OS << " (based on LLVM " << PACKAGE_VERSION << ")";
90#endif
91
92  return OS.str();
93}
94
95std::string getClangFullCPPVersion() {
96  // The version string we report in __VERSION__ is just a compacted version of
97  // the one we report on the command line.
98  std::string buf;
99  llvm::raw_string_ostream OS(buf);
100#ifdef CLANG_VENDOR
101  OS << CLANG_VENDOR;
102#endif
103  OS << "Clang " CLANG_VERSION_STRING " ("
104     << getClangFullRepositoryVersion() << ')';
105  return OS.str();
106}
107
108} // end namespace clang
109