blob: cd8c10f215a275ef605e4e01d4852a4371edeb08 [file] [log] [blame]
Douglas Gregor60b5d8e2009-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 Kremenek517e6762010-01-22 20:55:35 +000013
Ted Kremenek3687a5d2010-01-22 22:29:50 +000014#include "clang/Basic/Version.h"
Chris Lattner5f9e2722011-07-23 10:55:15 +000015#include "clang/Basic/LLVM.h"
Eli Friedmanfb8c56d2011-12-26 22:43:17 +000016#include "llvm/Config/config.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000017#include "llvm/Support/raw_ostream.h"
Douglas Gregor60b5d8e2009-10-05 18:52:24 +000018#include <cstdlib>
Chandler Carruth55fc8732012-12-04 09:13:33 +000019#include <cstring>
Ted Kremenek517e6762010-01-22 20:55:35 +000020
Douglas Gregor60b5d8e2009-10-05 18:52:24 +000021namespace clang {
Jia Liuf8e5d4c2012-03-02 14:37:41 +000022
Daniel Dunbar16a8fb72010-09-29 19:15:29 +000023std::string getClangRepositoryPath() {
Daniel Dunbar9a4a9c22011-03-31 00:32:50 +000024#if defined(CLANG_REPOSITORY_STRING)
25 return CLANG_REPOSITORY_STRING;
26#else
Daniel Dunbar640cf372010-09-29 17:57:10 +000027#ifdef SVN_REPOSITORY
Chris Lattner5f9e2722011-07-23 10:55:15 +000028 StringRef URL(SVN_REPOSITORY);
Daniel Dunbar16a8fb72010-09-29 19:15:29 +000029#else
Chris Lattner5f9e2722011-07-23 10:55:15 +000030 StringRef URL("");
Daniel Dunbar640cf372010-09-29 17:57:10 +000031#endif
Daniel Dunbar16a8fb72010-09-29 19:15:29 +000032
Daniel Dunbar7171f2a2010-10-11 23:44:19 +000033 // 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.
Chris Lattner5f9e2722011-07-23 10:55:15 +000035 static StringRef SVNRepository("$URL$");
Daniel Dunbar83e18f82010-10-11 23:50:34 +000036 if (URL.empty()) {
37 URL = SVNRepository.slice(SVNRepository.find(':'),
38 SVNRepository.find("/lib/Basic"));
39 }
Daniel Dunbar7171f2a2010-10-11 23:44:19 +000040
Daniel Dunbar16a8fb72010-09-29 19:15:29 +000041 // 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/");
Chris Lattner5f9e2722011-07-23 10:55:15 +000046 if (Start != StringRef::npos)
Daniel Dunbar16a8fb72010-09-29 19:15:29 +000047 URL = URL.substr(Start + 4);
48
49 return URL;
Daniel Dunbar9a4a9c22011-03-31 00:32:50 +000050#endif
Douglas Gregor60b5d8e2009-10-05 18:52:24 +000051}
52
Jia Liuf8e5d4c2012-03-02 14:37:41 +000053std::string getLLVMRepositoryPath() {
54#ifdef LLVM_REPOSITORY
55 StringRef URL(LLVM_REPOSITORY);
56#else
57 StringRef URL("");
58#endif
59
60 // Trim path prefix off, assuming path came from standard llvm path.
Andrew Trickfddfbdb2012-03-07 00:44:24 +000061 // Leave "llvm/" prefix to distinguish the following llvm revision from the
62 // clang revision.
Jia Liuf8e5d4c2012-03-02 14:37:41 +000063 size_t Start = URL.find("llvm/");
64 if (Start != StringRef::npos)
Andrew Trickfddfbdb2012-03-07 00:44:24 +000065 URL = URL.substr(Start);
Jia Liuf8e5d4c2012-03-02 14:37:41 +000066
67 return URL;
68}
69
Daniel Dunbar16a8fb72010-09-29 19:15:29 +000070std::string getClangRevision() {
Ted Kremenek971cc482010-03-03 01:02:48 +000071#ifdef SVN_REVISION
Daniel Dunbar16a8fb72010-09-29 19:15:29 +000072 return SVN_REVISION;
73#else
Ted Kremenek971cc482010-03-03 01:02:48 +000074 return "";
Daniel Dunbar16a8fb72010-09-29 19:15:29 +000075#endif
Douglas Gregor60b5d8e2009-10-05 18:52:24 +000076}
77
Jia Liuf8e5d4c2012-03-02 14:37:41 +000078std::string getLLVMRevision() {
79#ifdef LLVM_REVISION
80 return LLVM_REVISION;
81#else
82 return "";
83#endif
84}
85
Ted Kremeneka2a9d6e2010-02-12 22:54:40 +000086std::string getClangFullRepositoryVersion() {
87 std::string buf;
88 llvm::raw_string_ostream OS(buf);
Daniel Dunbar16a8fb72010-09-29 19:15:29 +000089 std::string Path = getClangRepositoryPath();
90 std::string Revision = getClangRevision();
Andrew Trickfddfbdb2012-03-07 00:44:24 +000091 if (!Path.empty() || !Revision.empty()) {
92 OS << '(';
Daniel Dunbar640cf372010-09-29 17:57:10 +000093 if (!Path.empty())
Andrew Trickfddfbdb2012-03-07 00:44:24 +000094 OS << Path;
95 if (!Revision.empty()) {
96 if (!Path.empty())
97 OS << ' ';
98 OS << Revision;
99 }
100 OS << ')';
101 }
Jia Liuf8e5d4c2012-03-02 14:37:41 +0000102 // Support LLVM in a separate repository.
103 std::string LLVMRev = getLLVMRevision();
104 if (!LLVMRev.empty() && LLVMRev != Revision) {
Andrew Trickfddfbdb2012-03-07 00:44:24 +0000105 OS << " (";
Jia Liuf8e5d4c2012-03-02 14:37:41 +0000106 std::string LLVMRepo = getLLVMRepositoryPath();
107 if (!LLVMRepo.empty())
Andrew Trickfddfbdb2012-03-07 00:44:24 +0000108 OS << LLVMRepo << ' ';
109 OS << LLVMRev << ')';
Jia Liuf8e5d4c2012-03-02 14:37:41 +0000110 }
Benjamin Kramer940f6462010-03-05 15:39:20 +0000111 return OS.str();
Ted Kremenek3687a5d2010-01-22 22:29:50 +0000112}
Jia Liuf8e5d4c2012-03-02 14:37:41 +0000113
Ted Kremeneka2a9d6e2010-02-12 22:54:40 +0000114std::string getClangFullVersion() {
115 std::string buf;
116 llvm::raw_string_ostream OS(buf);
Ted Kremenek3687a5d2010-01-22 22:29:50 +0000117#ifdef CLANG_VENDOR
Ted Kremeneka2a9d6e2010-02-12 22:54:40 +0000118 OS << CLANG_VENDOR;
Ted Kremenek3687a5d2010-01-22 22:29:50 +0000119#endif
Andrew Trickfddfbdb2012-03-07 00:44:24 +0000120 OS << "clang version " CLANG_VERSION_STRING " "
121 << getClangFullRepositoryVersion();
Daniel Dunbarddb6c8d2010-10-07 15:00:30 +0000122
123 // If vendor supplied, include the base LLVM version as well.
124#ifdef CLANG_VENDOR
125 OS << " (based on LLVM " << PACKAGE_VERSION << ")";
126#endif
127
Benjamin Kramer940f6462010-03-05 15:39:20 +0000128 return OS.str();
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000129}
Ted Kremeneka2a9d6e2010-02-12 22:54:40 +0000130
Daniel Dunbarad1a4c62011-03-31 00:53:51 +0000131std::string getClangFullCPPVersion() {
132 // The version string we report in __VERSION__ is just a compacted version of
133 // the one we report on the command line.
134 std::string buf;
135 llvm::raw_string_ostream OS(buf);
136#ifdef CLANG_VENDOR
137 OS << CLANG_VENDOR;
138#endif
Benjamin Kramer242cb062012-05-26 19:39:52 +0000139 OS << "Clang " CLANG_VERSION_STRING " " << getClangFullRepositoryVersion();
Daniel Dunbarad1a4c62011-03-31 00:53:51 +0000140 return OS.str();
141}
142
Douglas Gregor60b5d8e2009-10-05 18:52:24 +0000143} // end namespace clang