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