blob: 2934822c8016afd4ae122a42d24528985bb1acf8 [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"
Ted Kremenekf7a96a32010-01-22 22:12:47 +000016#include "llvm/Support/raw_ostream.h"
Eli Friedmanfb8c56d2011-12-26 22:43:17 +000017#include "llvm/Config/config.h"
Douglas Gregor60b5d8e2009-10-05 18:52:24 +000018#include <cstring>
19#include <cstdlib>
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.
61 size_t Start = URL.find("llvm/");
62 if (Start != StringRef::npos)
63 URL = URL.substr(Start + 5);
64
65 return URL;
66}
67
Daniel Dunbar16a8fb72010-09-29 19:15:29 +000068std::string getClangRevision() {
Ted Kremenek971cc482010-03-03 01:02:48 +000069#ifdef SVN_REVISION
Daniel Dunbar16a8fb72010-09-29 19:15:29 +000070 return SVN_REVISION;
71#else
Ted Kremenek971cc482010-03-03 01:02:48 +000072 return "";
Daniel Dunbar16a8fb72010-09-29 19:15:29 +000073#endif
Douglas Gregor60b5d8e2009-10-05 18:52:24 +000074}
75
Jia Liuf8e5d4c2012-03-02 14:37:41 +000076std::string getLLVMRevision() {
77#ifdef LLVM_REVISION
78 return LLVM_REVISION;
79#else
80 return "";
81#endif
82}
83
Ted Kremeneka2a9d6e2010-02-12 22:54:40 +000084std::string getClangFullRepositoryVersion() {
85 std::string buf;
86 llvm::raw_string_ostream OS(buf);
Daniel Dunbar16a8fb72010-09-29 19:15:29 +000087 std::string Path = getClangRepositoryPath();
88 std::string Revision = getClangRevision();
Daniel Dunbar640cf372010-09-29 17:57:10 +000089 if (!Path.empty())
90 OS << Path;
91 if (!Revision.empty()) {
92 if (!Path.empty())
93 OS << ' ';
94 OS << Revision;
95 }
Jia Liuf8e5d4c2012-03-02 14:37:41 +000096 // Support LLVM in a separate repository.
97 std::string LLVMRev = getLLVMRevision();
98 if (!LLVMRev.empty() && LLVMRev != Revision) {
99 std::string LLVMRepo = getLLVMRepositoryPath();
100 if (!LLVMRepo.empty())
101 OS << ' ' << LLVMRepo;
102 OS << ' ' << LLVMRev;
103 }
Benjamin Kramer940f6462010-03-05 15:39:20 +0000104 return OS.str();
Ted Kremenek3687a5d2010-01-22 22:29:50 +0000105}
Jia Liuf8e5d4c2012-03-02 14:37:41 +0000106
Ted Kremeneka2a9d6e2010-02-12 22:54:40 +0000107std::string getClangFullVersion() {
108 std::string buf;
109 llvm::raw_string_ostream OS(buf);
Ted Kremenek3687a5d2010-01-22 22:29:50 +0000110#ifdef CLANG_VENDOR
Ted Kremeneka2a9d6e2010-02-12 22:54:40 +0000111 OS << CLANG_VENDOR;
Ted Kremenek3687a5d2010-01-22 22:29:50 +0000112#endif
Ted Kremeneka2a9d6e2010-02-12 22:54:40 +0000113 OS << "clang version " CLANG_VERSION_STRING " ("
114 << getClangFullRepositoryVersion() << ')';
Daniel Dunbarddb6c8d2010-10-07 15:00:30 +0000115
116 // If vendor supplied, include the base LLVM version as well.
117#ifdef CLANG_VENDOR
118 OS << " (based on LLVM " << PACKAGE_VERSION << ")";
119#endif
120
Benjamin Kramer940f6462010-03-05 15:39:20 +0000121 return OS.str();
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000122}
Ted Kremeneka2a9d6e2010-02-12 22:54:40 +0000123
Daniel Dunbarad1a4c62011-03-31 00:53:51 +0000124std::string getClangFullCPPVersion() {
125 // The version string we report in __VERSION__ is just a compacted version of
126 // the one we report on the command line.
127 std::string buf;
128 llvm::raw_string_ostream OS(buf);
129#ifdef CLANG_VENDOR
130 OS << CLANG_VENDOR;
131#endif
132 OS << "Clang " CLANG_VERSION_STRING " ("
133 << getClangFullRepositoryVersion() << ')';
134 return OS.str();
135}
136
Douglas Gregor60b5d8e2009-10-05 18:52:24 +0000137} // end namespace clang