blob: d6564582e77268a00cee272e1f17e01cce12948d [file] [log] [blame]
Douglas Gregor7550a6c2009-10-05 18:52:24 +00001//===- Version.cpp - Clang Version Number -----------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Douglas Gregor7550a6c2009-10-05 18:52:24 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file defines several version-related utility functions for Clang.
10//
11//===----------------------------------------------------------------------===//
Ted Kremenek2377a0e2010-01-22 20:55:35 +000012
Ted Kremenek51b8bc92010-01-22 22:29:50 +000013#include "clang/Basic/Version.h"
Chris Lattner0e62c1c2011-07-23 10:55:15 +000014#include "clang/Basic/LLVM.h"
Alp Tokerf988d002014-06-06 10:36:22 +000015#include "clang/Config/config.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000016#include "llvm/Support/raw_ostream.h"
Douglas Gregor7550a6c2009-10-05 18:52:24 +000017#include <cstdlib>
Chandler Carruth3a022472012-12-04 09:13:33 +000018#include <cstring>
Ted Kremenek2377a0e2010-01-22 20:55:35 +000019
Petr Hosek23fdd5a2019-02-06 03:51:00 +000020#ifdef HAVE_VCS_VERSION_INC
21#include "VCSVersion.inc"
Petr Hosek12062e02019-01-31 07:12:43 +000022#endif
23
Douglas Gregor7550a6c2009-10-05 18:52:24 +000024namespace clang {
Jia Liu5c302482012-03-02 14:37:41 +000025
Daniel Dunbar181ca582010-09-29 19:15:29 +000026std::string getClangRepositoryPath() {
Daniel Dunbarb1798f72011-03-31 00:32:50 +000027#if defined(CLANG_REPOSITORY_STRING)
28 return CLANG_REPOSITORY_STRING;
29#else
Petr Hosek23fdd5a2019-02-06 03:51:00 +000030#ifdef CLANG_REPOSITORY
31 StringRef URL(CLANG_REPOSITORY);
Daniel Dunbar181ca582010-09-29 19:15:29 +000032#else
Chris Lattner0e62c1c2011-07-23 10:55:15 +000033 StringRef URL("");
Daniel Dunbarb800fdb2010-09-29 17:57:10 +000034#endif
Daniel Dunbar181ca582010-09-29 19:15:29 +000035
Petr Hosek23fdd5a2019-02-06 03:51:00 +000036 // If the CLANG_REPOSITORY is empty, try to use the SVN keyword. This helps us
Daniel Dunbard097d912010-10-11 23:44:19 +000037 // pick up a tag in an SVN export, for example.
Benjamin Kramer7a7285c2013-08-09 17:51:03 +000038 StringRef SVNRepository("$URL$");
Daniel Dunbar48ed37d2010-10-11 23:50:34 +000039 if (URL.empty()) {
40 URL = SVNRepository.slice(SVNRepository.find(':'),
41 SVNRepository.find("/lib/Basic"));
42 }
Daniel Dunbard097d912010-10-11 23:44:19 +000043
Daniel Dunbar181ca582010-09-29 19:15:29 +000044 // Strip off version from a build from an integration branch.
45 URL = URL.slice(0, URL.find("/src/tools/clang"));
46
47 // Trim path prefix off, assuming path came from standard cfe path.
48 size_t Start = URL.find("cfe/");
Chris Lattner0e62c1c2011-07-23 10:55:15 +000049 if (Start != StringRef::npos)
Daniel Dunbar181ca582010-09-29 19:15:29 +000050 URL = URL.substr(Start + 4);
51
52 return URL;
Daniel Dunbarb1798f72011-03-31 00:32:50 +000053#endif
Douglas Gregor7550a6c2009-10-05 18:52:24 +000054}
55
Jia Liu5c302482012-03-02 14:37:41 +000056std::string getLLVMRepositoryPath() {
57#ifdef LLVM_REPOSITORY
58 StringRef URL(LLVM_REPOSITORY);
59#else
60 StringRef URL("");
61#endif
62
63 // Trim path prefix off, assuming path came from standard llvm path.
Andrew Trick9179e8a2012-03-07 00:44:24 +000064 // Leave "llvm/" prefix to distinguish the following llvm revision from the
65 // clang revision.
Jia Liu5c302482012-03-02 14:37:41 +000066 size_t Start = URL.find("llvm/");
67 if (Start != StringRef::npos)
Andrew Trick9179e8a2012-03-07 00:44:24 +000068 URL = URL.substr(Start);
Jia Liu5c302482012-03-02 14:37:41 +000069
70 return URL;
71}
72
Daniel Dunbar181ca582010-09-29 19:15:29 +000073std::string getClangRevision() {
Petr Hosek23fdd5a2019-02-06 03:51:00 +000074#ifdef CLANG_REVISION
75 return CLANG_REVISION;
Daniel Dunbar181ca582010-09-29 19:15:29 +000076#else
Ted Kremenek47307292010-03-03 01:02:48 +000077 return "";
Daniel Dunbar181ca582010-09-29 19:15:29 +000078#endif
Douglas Gregor7550a6c2009-10-05 18:52:24 +000079}
80
Jia Liu5c302482012-03-02 14:37:41 +000081std::string getLLVMRevision() {
82#ifdef LLVM_REVISION
83 return LLVM_REVISION;
84#else
85 return "";
86#endif
87}
88
Ted Kremeneka3e65702010-02-12 22:54:40 +000089std::string getClangFullRepositoryVersion() {
90 std::string buf;
91 llvm::raw_string_ostream OS(buf);
Daniel Dunbar181ca582010-09-29 19:15:29 +000092 std::string Path = getClangRepositoryPath();
93 std::string Revision = getClangRevision();
Andrew Trick9179e8a2012-03-07 00:44:24 +000094 if (!Path.empty() || !Revision.empty()) {
95 OS << '(';
Daniel Dunbarb800fdb2010-09-29 17:57:10 +000096 if (!Path.empty())
Andrew Trick9179e8a2012-03-07 00:44:24 +000097 OS << Path;
98 if (!Revision.empty()) {
99 if (!Path.empty())
100 OS << ' ';
101 OS << Revision;
102 }
103 OS << ')';
Sylvestre Ledru096d6e42014-01-14 10:25:26 +0000104 }
Jia Liu5c302482012-03-02 14:37:41 +0000105 // Support LLVM in a separate repository.
106 std::string LLVMRev = getLLVMRevision();
107 if (!LLVMRev.empty() && LLVMRev != Revision) {
Sylvestre Ledru096d6e42014-01-14 10:25:26 +0000108 OS << " (";
Jia Liu5c302482012-03-02 14:37:41 +0000109 std::string LLVMRepo = getLLVMRepositoryPath();
110 if (!LLVMRepo.empty())
Andrew Trick9179e8a2012-03-07 00:44:24 +0000111 OS << LLVMRepo << ' ';
112 OS << LLVMRev << ')';
Jia Liu5c302482012-03-02 14:37:41 +0000113 }
Benjamin Kramerd48707002010-03-05 15:39:20 +0000114 return OS.str();
Ted Kremenek51b8bc92010-01-22 22:29:50 +0000115}
Jia Liu5c302482012-03-02 14:37:41 +0000116
Ted Kremeneka3e65702010-02-12 22:54:40 +0000117std::string getClangFullVersion() {
Nico Weberb00d66e2014-01-07 16:27:35 +0000118 return getClangToolFullVersion("clang");
119}
120
121std::string getClangToolFullVersion(StringRef ToolName) {
Ted Kremeneka3e65702010-02-12 22:54:40 +0000122 std::string buf;
123 llvm::raw_string_ostream OS(buf);
Ted Kremenek51b8bc92010-01-22 22:29:50 +0000124#ifdef CLANG_VENDOR
Ted Kremeneka3e65702010-02-12 22:54:40 +0000125 OS << CLANG_VENDOR;
Ted Kremenek51b8bc92010-01-22 22:29:50 +0000126#endif
Nico Weberb00d66e2014-01-07 16:27:35 +0000127 OS << ToolName << " version " CLANG_VERSION_STRING " "
Andrew Trick9179e8a2012-03-07 00:44:24 +0000128 << getClangFullRepositoryVersion();
Daniel Dunbar60362642010-10-07 15:00:30 +0000129
130 // If vendor supplied, include the base LLVM version as well.
131#ifdef CLANG_VENDOR
Alp Tokerf988d002014-06-06 10:36:22 +0000132 OS << " (based on " << BACKEND_PACKAGE_STRING << ")";
Daniel Dunbar60362642010-10-07 15:00:30 +0000133#endif
134
Benjamin Kramerd48707002010-03-05 15:39:20 +0000135 return OS.str();
Ted Kremenek18e066f2010-01-22 22:12:47 +0000136}
Ted Kremeneka3e65702010-02-12 22:54:40 +0000137
Daniel Dunbar3b17a862011-03-31 00:53:51 +0000138std::string getClangFullCPPVersion() {
Sylvestre Ledru21a92a82019-07-13 06:27:35 +0000139 // The version string we report in __VERSION__ is just a compacted version of
140 // the one we report on the command line.
Daniel Dunbar3b17a862011-03-31 00:53:51 +0000141 std::string buf;
142 llvm::raw_string_ostream OS(buf);
143#ifdef CLANG_VENDOR
144 OS << CLANG_VENDOR;
145#endif
Benjamin Kramer7b01b572012-05-26 19:39:52 +0000146 OS << "Clang " CLANG_VERSION_STRING " " << getClangFullRepositoryVersion();
Daniel Dunbar3b17a862011-03-31 00:53:51 +0000147 return OS.str();
148}
149
Douglas Gregor7550a6c2009-10-05 18:52:24 +0000150} // end namespace clang