blob: 65cc2581dab0bcf8fc13f99c816d6c146fdf4200 [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"
Ted Kremenekf7a96a32010-01-22 22:12:47 +000015#include "llvm/Support/raw_ostream.h"
Daniel Dunbarddb6c8d2010-10-07 15:00:30 +000016#include "llvm/Config/config.h"
Douglas Gregor60b5d8e2009-10-05 18:52:24 +000017#include <cstring>
18#include <cstdlib>
Ted Kremenek517e6762010-01-22 20:55:35 +000019
Douglas Gregor60b5d8e2009-10-05 18:52:24 +000020using namespace std;
21
22namespace clang {
23
Daniel Dunbar16a8fb72010-09-29 19:15:29 +000024std::string getClangRepositoryPath() {
Daniel Dunbar9a4a9c22011-03-31 00:32:50 +000025#if defined(CLANG_REPOSITORY_STRING)
26 return CLANG_REPOSITORY_STRING;
27#else
Daniel Dunbar640cf372010-09-29 17:57:10 +000028#ifdef SVN_REPOSITORY
Daniel Dunbar16a8fb72010-09-29 19:15:29 +000029 llvm::StringRef URL(SVN_REPOSITORY);
30#else
31 llvm::StringRef URL("");
Daniel Dunbar640cf372010-09-29 17:57:10 +000032#endif
Daniel Dunbar16a8fb72010-09-29 19:15:29 +000033
Daniel Dunbar7171f2a2010-10-11 23:44:19 +000034 // If the SVN_REPOSITORY is empty, try to use the SVN keyword. This helps us
35 // pick up a tag in an SVN export, for example.
36 static llvm::StringRef SVNRepository("$URL$");
Daniel Dunbar83e18f82010-10-11 23:50:34 +000037 if (URL.empty()) {
38 URL = SVNRepository.slice(SVNRepository.find(':'),
39 SVNRepository.find("/lib/Basic"));
40 }
Daniel Dunbar7171f2a2010-10-11 23:44:19 +000041
Daniel Dunbar16a8fb72010-09-29 19:15:29 +000042 // Strip off version from a build from an integration branch.
43 URL = URL.slice(0, URL.find("/src/tools/clang"));
44
45 // Trim path prefix off, assuming path came from standard cfe path.
46 size_t Start = URL.find("cfe/");
47 if (Start != llvm::StringRef::npos)
48 URL = URL.substr(Start + 4);
49
50 return URL;
Daniel Dunbar9a4a9c22011-03-31 00:32:50 +000051#endif
Douglas Gregor60b5d8e2009-10-05 18:52:24 +000052}
53
Daniel Dunbar16a8fb72010-09-29 19:15:29 +000054std::string getClangRevision() {
Ted Kremenek971cc482010-03-03 01:02:48 +000055#ifdef SVN_REVISION
Daniel Dunbar16a8fb72010-09-29 19:15:29 +000056 return SVN_REVISION;
57#else
Ted Kremenek971cc482010-03-03 01:02:48 +000058 return "";
Daniel Dunbar16a8fb72010-09-29 19:15:29 +000059#endif
Douglas Gregor60b5d8e2009-10-05 18:52:24 +000060}
61
Ted Kremeneka2a9d6e2010-02-12 22:54:40 +000062std::string getClangFullRepositoryVersion() {
63 std::string buf;
64 llvm::raw_string_ostream OS(buf);
Daniel Dunbar16a8fb72010-09-29 19:15:29 +000065 std::string Path = getClangRepositoryPath();
66 std::string Revision = getClangRevision();
Daniel Dunbar640cf372010-09-29 17:57:10 +000067 if (!Path.empty())
68 OS << Path;
69 if (!Revision.empty()) {
70 if (!Path.empty())
71 OS << ' ';
72 OS << Revision;
73 }
Benjamin Kramer940f6462010-03-05 15:39:20 +000074 return OS.str();
Ted Kremenek3687a5d2010-01-22 22:29:50 +000075}
76
Ted Kremeneka2a9d6e2010-02-12 22:54:40 +000077std::string getClangFullVersion() {
78 std::string buf;
79 llvm::raw_string_ostream OS(buf);
Ted Kremenek3687a5d2010-01-22 22:29:50 +000080#ifdef CLANG_VENDOR
Ted Kremeneka2a9d6e2010-02-12 22:54:40 +000081 OS << CLANG_VENDOR;
Ted Kremenek3687a5d2010-01-22 22:29:50 +000082#endif
Ted Kremeneka2a9d6e2010-02-12 22:54:40 +000083 OS << "clang version " CLANG_VERSION_STRING " ("
84 << getClangFullRepositoryVersion() << ')';
Daniel Dunbarddb6c8d2010-10-07 15:00:30 +000085
86 // If vendor supplied, include the base LLVM version as well.
87#ifdef CLANG_VENDOR
88 OS << " (based on LLVM " << PACKAGE_VERSION << ")";
89#endif
90
Benjamin Kramer940f6462010-03-05 15:39:20 +000091 return OS.str();
Ted Kremenekf7a96a32010-01-22 22:12:47 +000092}
Ted Kremeneka2a9d6e2010-02-12 22:54:40 +000093
Daniel Dunbarad1a4c62011-03-31 00:53:51 +000094std::string getClangFullCPPVersion() {
95 // The version string we report in __VERSION__ is just a compacted version of
96 // the one we report on the command line.
97 std::string buf;
98 llvm::raw_string_ostream OS(buf);
99#ifdef CLANG_VENDOR
100 OS << CLANG_VENDOR;
101#endif
102 OS << "Clang " CLANG_VERSION_STRING " ("
103 << getClangFullRepositoryVersion() << ')';
104 return OS.str();
105}
106
Douglas Gregor60b5d8e2009-10-05 18:52:24 +0000107} // end namespace clang