blob: e219a3def71deaf081ffc0dc82bcc549ddb62c59 [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 Gregor7aac45a2013-03-25 23:16:38 +000021#ifdef HAVE_SVN_VERSION_INC
22# include "SVNVersion.inc"
23#endif
24
Douglas Gregor60b5d8e2009-10-05 18:52:24 +000025namespace clang {
Jia Liuf8e5d4c2012-03-02 14:37:41 +000026
Daniel Dunbar16a8fb72010-09-29 19:15:29 +000027std::string getClangRepositoryPath() {
Daniel Dunbar9a4a9c22011-03-31 00:32:50 +000028#if defined(CLANG_REPOSITORY_STRING)
29 return CLANG_REPOSITORY_STRING;
30#else
Daniel Dunbar640cf372010-09-29 17:57:10 +000031#ifdef SVN_REPOSITORY
Chris Lattner5f9e2722011-07-23 10:55:15 +000032 StringRef URL(SVN_REPOSITORY);
Daniel Dunbar16a8fb72010-09-29 19:15:29 +000033#else
Chris Lattner5f9e2722011-07-23 10:55:15 +000034 StringRef URL("");
Daniel Dunbar640cf372010-09-29 17:57:10 +000035#endif
Daniel Dunbar16a8fb72010-09-29 19:15:29 +000036
Daniel Dunbar7171f2a2010-10-11 23:44:19 +000037 // 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.
Chris Lattner5f9e2722011-07-23 10:55:15 +000039 static StringRef SVNRepository("$URL$");
Daniel Dunbar83e18f82010-10-11 23:50:34 +000040 if (URL.empty()) {
41 URL = SVNRepository.slice(SVNRepository.find(':'),
42 SVNRepository.find("/lib/Basic"));
43 }
Daniel Dunbar7171f2a2010-10-11 23:44:19 +000044
Daniel Dunbar16a8fb72010-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 Lattner5f9e2722011-07-23 10:55:15 +000050 if (Start != StringRef::npos)
Daniel Dunbar16a8fb72010-09-29 19:15:29 +000051 URL = URL.substr(Start + 4);
52
53 return URL;
Daniel Dunbar9a4a9c22011-03-31 00:32:50 +000054#endif
Douglas Gregor60b5d8e2009-10-05 18:52:24 +000055}
56
Jia Liuf8e5d4c2012-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 Trickfddfbdb2012-03-07 00:44:24 +000065 // Leave "llvm/" prefix to distinguish the following llvm revision from the
66 // clang revision.
Jia Liuf8e5d4c2012-03-02 14:37:41 +000067 size_t Start = URL.find("llvm/");
68 if (Start != StringRef::npos)
Andrew Trickfddfbdb2012-03-07 00:44:24 +000069 URL = URL.substr(Start);
Jia Liuf8e5d4c2012-03-02 14:37:41 +000070
71 return URL;
72}
73
Daniel Dunbar16a8fb72010-09-29 19:15:29 +000074std::string getClangRevision() {
Ted Kremenek971cc482010-03-03 01:02:48 +000075#ifdef SVN_REVISION
Daniel Dunbar16a8fb72010-09-29 19:15:29 +000076 return SVN_REVISION;
77#else
Ted Kremenek971cc482010-03-03 01:02:48 +000078 return "";
Daniel Dunbar16a8fb72010-09-29 19:15:29 +000079#endif
Douglas Gregor60b5d8e2009-10-05 18:52:24 +000080}
81
Jia Liuf8e5d4c2012-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 Kremeneka2a9d6e2010-02-12 22:54:40 +000090std::string getClangFullRepositoryVersion() {
91 std::string buf;
92 llvm::raw_string_ostream OS(buf);
Daniel Dunbar16a8fb72010-09-29 19:15:29 +000093 std::string Path = getClangRepositoryPath();
94 std::string Revision = getClangRevision();
Andrew Trickfddfbdb2012-03-07 00:44:24 +000095 if (!Path.empty() || !Revision.empty()) {
96 OS << '(';
Daniel Dunbar640cf372010-09-29 17:57:10 +000097 if (!Path.empty())
Andrew Trickfddfbdb2012-03-07 00:44:24 +000098 OS << Path;
99 if (!Revision.empty()) {
100 if (!Path.empty())
101 OS << ' ';
102 OS << Revision;
103 }
104 OS << ')';
105 }
Jia Liuf8e5d4c2012-03-02 14:37:41 +0000106 // Support LLVM in a separate repository.
107 std::string LLVMRev = getLLVMRevision();
108 if (!LLVMRev.empty() && LLVMRev != Revision) {
Andrew Trickfddfbdb2012-03-07 00:44:24 +0000109 OS << " (";
Jia Liuf8e5d4c2012-03-02 14:37:41 +0000110 std::string LLVMRepo = getLLVMRepositoryPath();
111 if (!LLVMRepo.empty())
Andrew Trickfddfbdb2012-03-07 00:44:24 +0000112 OS << LLVMRepo << ' ';
113 OS << LLVMRev << ')';
Jia Liuf8e5d4c2012-03-02 14:37:41 +0000114 }
Benjamin Kramer940f6462010-03-05 15:39:20 +0000115 return OS.str();
Ted Kremenek3687a5d2010-01-22 22:29:50 +0000116}
Jia Liuf8e5d4c2012-03-02 14:37:41 +0000117
Ted Kremeneka2a9d6e2010-02-12 22:54:40 +0000118std::string getClangFullVersion() {
119 std::string buf;
120 llvm::raw_string_ostream OS(buf);
Ted Kremenek3687a5d2010-01-22 22:29:50 +0000121#ifdef CLANG_VENDOR
Ted Kremeneka2a9d6e2010-02-12 22:54:40 +0000122 OS << CLANG_VENDOR;
Ted Kremenek3687a5d2010-01-22 22:29:50 +0000123#endif
Andrew Trickfddfbdb2012-03-07 00:44:24 +0000124 OS << "clang version " CLANG_VERSION_STRING " "
125 << getClangFullRepositoryVersion();
Daniel Dunbarddb6c8d2010-10-07 15:00:30 +0000126
127 // If vendor supplied, include the base LLVM version as well.
128#ifdef CLANG_VENDOR
129 OS << " (based on LLVM " << PACKAGE_VERSION << ")";
130#endif
131
Benjamin Kramer940f6462010-03-05 15:39:20 +0000132 return OS.str();
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000133}
Ted Kremeneka2a9d6e2010-02-12 22:54:40 +0000134
Daniel Dunbarad1a4c62011-03-31 00:53:51 +0000135std::string getClangFullCPPVersion() {
136 // The version string we report in __VERSION__ is just a compacted version of
137 // the one we report on the command line.
138 std::string buf;
139 llvm::raw_string_ostream OS(buf);
140#ifdef CLANG_VENDOR
141 OS << CLANG_VENDOR;
142#endif
Benjamin Kramer242cb062012-05-26 19:39:52 +0000143 OS << "Clang " CLANG_VERSION_STRING " " << getClangFullRepositoryVersion();
Daniel Dunbarad1a4c62011-03-31 00:53:51 +0000144 return OS.str();
145}
146
Douglas Gregor60b5d8e2009-10-05 18:52:24 +0000147} // end namespace clang