blob: c2b7753d41288ac94587f3cb35a1f8c9c3f1c984 [file] [log] [blame]
Douglas Gregor7550a6c2009-10-05 18:52:24 +00001//===- 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//===----------------------------------------------------------------------===//
Ted Kremenek2377a0e2010-01-22 20:55:35 +000013
Ted Kremenek51b8bc92010-01-22 22:29:50 +000014#include "clang/Basic/Version.h"
Chris Lattner0e62c1c2011-07-23 10:55:15 +000015#include "clang/Basic/LLVM.h"
Alp Tokerf988d002014-06-06 10:36:22 +000016#include "clang/Config/config.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000017#include "llvm/Support/raw_ostream.h"
Douglas Gregor7550a6c2009-10-05 18:52:24 +000018#include <cstdlib>
Chandler Carruth3a022472012-12-04 09:13:33 +000019#include <cstring>
Ted Kremenek2377a0e2010-01-22 20:55:35 +000020
Douglas Gregorc060b4c2013-03-25 23:16:38 +000021#ifdef HAVE_SVN_VERSION_INC
22# include "SVNVersion.inc"
23#endif
24
Douglas Gregor7550a6c2009-10-05 18:52:24 +000025namespace clang {
Jia Liu5c302482012-03-02 14:37:41 +000026
Daniel Dunbar181ca582010-09-29 19:15:29 +000027std::string getClangRepositoryPath() {
Daniel Dunbarb1798f72011-03-31 00:32:50 +000028#if defined(CLANG_REPOSITORY_STRING)
29 return CLANG_REPOSITORY_STRING;
30#else
Paul Robinsonaf6b86c2014-12-10 23:49:03 +000031#ifdef SVN_REPOSITORY
32 StringRef URL(SVN_REPOSITORY);
Daniel Dunbar181ca582010-09-29 19:15:29 +000033#else
Chris Lattner0e62c1c2011-07-23 10:55:15 +000034 StringRef URL("");
Daniel Dunbarb800fdb2010-09-29 17:57:10 +000035#endif
Daniel Dunbar181ca582010-09-29 19:15:29 +000036
Paul Robinsonaf6b86c2014-12-10 23:49:03 +000037 // If the SVN_REPOSITORY is empty, try to use the SVN keyword. This helps us
Daniel Dunbard097d912010-10-11 23:44:19 +000038 // pick up a tag in an SVN export, for example.
Benjamin Kramer7a7285c2013-08-09 17:51:03 +000039 StringRef SVNRepository("$URL$");
Daniel Dunbar48ed37d2010-10-11 23:50:34 +000040 if (URL.empty()) {
41 URL = SVNRepository.slice(SVNRepository.find(':'),
42 SVNRepository.find("/lib/Basic"));
43 }
Daniel Dunbard097d912010-10-11 23:44:19 +000044
Daniel Dunbar181ca582010-09-29 19:15:29 +000045 // 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/");
Chris Lattner0e62c1c2011-07-23 10:55:15 +000050 if (Start != StringRef::npos)
Daniel Dunbar181ca582010-09-29 19:15:29 +000051 URL = URL.substr(Start + 4);
52
53 return URL;
Daniel Dunbarb1798f72011-03-31 00:32:50 +000054#endif
Douglas Gregor7550a6c2009-10-05 18:52:24 +000055}
56
Jia Liu5c302482012-03-02 14:37:41 +000057std::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.
Andrew Trick9179e8a2012-03-07 00:44:24 +000065 // Leave "llvm/" prefix to distinguish the following llvm revision from the
66 // clang revision.
Jia Liu5c302482012-03-02 14:37:41 +000067 size_t Start = URL.find("llvm/");
68 if (Start != StringRef::npos)
Andrew Trick9179e8a2012-03-07 00:44:24 +000069 URL = URL.substr(Start);
Jia Liu5c302482012-03-02 14:37:41 +000070
71 return URL;
72}
73
Daniel Dunbar181ca582010-09-29 19:15:29 +000074std::string getClangRevision() {
Paul Robinsonaf6b86c2014-12-10 23:49:03 +000075#ifdef SVN_REVISION
76 return SVN_REVISION;
Daniel Dunbar181ca582010-09-29 19:15:29 +000077#else
Ted Kremenek47307292010-03-03 01:02:48 +000078 return "";
Daniel Dunbar181ca582010-09-29 19:15:29 +000079#endif
Douglas Gregor7550a6c2009-10-05 18:52:24 +000080}
81
Jia Liu5c302482012-03-02 14:37:41 +000082std::string getLLVMRevision() {
83#ifdef LLVM_REVISION
84 return LLVM_REVISION;
85#else
86 return "";
87#endif
88}
89
Ted Kremeneka3e65702010-02-12 22:54:40 +000090std::string getClangFullRepositoryVersion() {
91 std::string buf;
92 llvm::raw_string_ostream OS(buf);
Daniel Dunbar181ca582010-09-29 19:15:29 +000093 std::string Path = getClangRepositoryPath();
94 std::string Revision = getClangRevision();
Andrew Trick9179e8a2012-03-07 00:44:24 +000095 if (!Path.empty() || !Revision.empty()) {
96 OS << '(';
Daniel Dunbarb800fdb2010-09-29 17:57:10 +000097 if (!Path.empty())
Andrew Trick9179e8a2012-03-07 00:44:24 +000098 OS << Path;
99 if (!Revision.empty()) {
100 if (!Path.empty())
101 OS << ' ';
102 OS << Revision;
103 }
104 OS << ')';
Sylvestre Ledru096d6e42014-01-14 10:25:26 +0000105 }
Jia Liu5c302482012-03-02 14:37:41 +0000106 // Support LLVM in a separate repository.
107 std::string LLVMRev = getLLVMRevision();
108 if (!LLVMRev.empty() && LLVMRev != Revision) {
Sylvestre Ledru096d6e42014-01-14 10:25:26 +0000109 OS << " (";
Jia Liu5c302482012-03-02 14:37:41 +0000110 std::string LLVMRepo = getLLVMRepositoryPath();
111 if (!LLVMRepo.empty())
Andrew Trick9179e8a2012-03-07 00:44:24 +0000112 OS << LLVMRepo << ' ';
113 OS << LLVMRev << ')';
Jia Liu5c302482012-03-02 14:37:41 +0000114 }
Benjamin Kramerd48707002010-03-05 15:39:20 +0000115 return OS.str();
Ted Kremenek51b8bc92010-01-22 22:29:50 +0000116}
Jia Liu5c302482012-03-02 14:37:41 +0000117
Ted Kremeneka3e65702010-02-12 22:54:40 +0000118std::string getClangFullVersion() {
Nico Weberb00d66e2014-01-07 16:27:35 +0000119 return getClangToolFullVersion("clang");
120}
121
122std::string getClangToolFullVersion(StringRef ToolName) {
Ted Kremeneka3e65702010-02-12 22:54:40 +0000123 std::string buf;
124 llvm::raw_string_ostream OS(buf);
Ted Kremenek51b8bc92010-01-22 22:29:50 +0000125#ifdef CLANG_VENDOR
Ted Kremeneka3e65702010-02-12 22:54:40 +0000126 OS << CLANG_VENDOR;
Ted Kremenek51b8bc92010-01-22 22:29:50 +0000127#endif
Nico Weberb00d66e2014-01-07 16:27:35 +0000128 OS << ToolName << " version " CLANG_VERSION_STRING " "
Andrew Trick9179e8a2012-03-07 00:44:24 +0000129 << getClangFullRepositoryVersion();
Daniel Dunbar60362642010-10-07 15:00:30 +0000130
131 // If vendor supplied, include the base LLVM version as well.
132#ifdef CLANG_VENDOR
Alp Tokerf988d002014-06-06 10:36:22 +0000133 OS << " (based on " << BACKEND_PACKAGE_STRING << ")";
Daniel Dunbar60362642010-10-07 15:00:30 +0000134#endif
135
Benjamin Kramerd48707002010-03-05 15:39:20 +0000136 return OS.str();
Ted Kremenek18e066f2010-01-22 22:12:47 +0000137}
Ted Kremeneka3e65702010-02-12 22:54:40 +0000138
Daniel Dunbar3b17a862011-03-31 00:53:51 +0000139std::string getClangFullCPPVersion() {
140 // The version string we report in __VERSION__ is just a compacted version of
141 // the one we report on the command line.
142 std::string buf;
143 llvm::raw_string_ostream OS(buf);
144#ifdef CLANG_VENDOR
145 OS << CLANG_VENDOR;
146#endif
Benjamin Kramer7b01b572012-05-26 19:39:52 +0000147 OS << "Clang " CLANG_VERSION_STRING " " << getClangFullRepositoryVersion();
Daniel Dunbar3b17a862011-03-31 00:53:51 +0000148 return OS.str();
149}
150
Douglas Gregor7550a6c2009-10-05 18:52:24 +0000151} // end namespace clang