blob: 86d4aa5dfbf047045f06e1ac8d23bdf531e11926 [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 +000020namespace clang {
21
Daniel Dunbar16a8fb72010-09-29 19:15:29 +000022std::string getClangRepositoryPath() {
Daniel Dunbar9a4a9c22011-03-31 00:32:50 +000023#if defined(CLANG_REPOSITORY_STRING)
24 return CLANG_REPOSITORY_STRING;
25#else
Daniel Dunbar640cf372010-09-29 17:57:10 +000026#ifdef SVN_REPOSITORY
Daniel Dunbar16a8fb72010-09-29 19:15:29 +000027 llvm::StringRef URL(SVN_REPOSITORY);
28#else
29 llvm::StringRef URL("");
Daniel Dunbar640cf372010-09-29 17:57:10 +000030#endif
Daniel Dunbar16a8fb72010-09-29 19:15:29 +000031
Daniel Dunbar7171f2a2010-10-11 23:44:19 +000032 // If the SVN_REPOSITORY is empty, try to use the SVN keyword. This helps us
33 // pick up a tag in an SVN export, for example.
34 static llvm::StringRef SVNRepository("$URL$");
Daniel Dunbar83e18f82010-10-11 23:50:34 +000035 if (URL.empty()) {
36 URL = SVNRepository.slice(SVNRepository.find(':'),
37 SVNRepository.find("/lib/Basic"));
38 }
Daniel Dunbar7171f2a2010-10-11 23:44:19 +000039
Daniel Dunbar16a8fb72010-09-29 19:15:29 +000040 // Strip off version from a build from an integration branch.
41 URL = URL.slice(0, URL.find("/src/tools/clang"));
42
43 // Trim path prefix off, assuming path came from standard cfe path.
44 size_t Start = URL.find("cfe/");
45 if (Start != llvm::StringRef::npos)
46 URL = URL.substr(Start + 4);
47
48 return URL;
Daniel Dunbar9a4a9c22011-03-31 00:32:50 +000049#endif
Douglas Gregor60b5d8e2009-10-05 18:52:24 +000050}
51
Daniel Dunbar16a8fb72010-09-29 19:15:29 +000052std::string getClangRevision() {
Ted Kremenek971cc482010-03-03 01:02:48 +000053#ifdef SVN_REVISION
Daniel Dunbar16a8fb72010-09-29 19:15:29 +000054 return SVN_REVISION;
55#else
Ted Kremenek971cc482010-03-03 01:02:48 +000056 return "";
Daniel Dunbar16a8fb72010-09-29 19:15:29 +000057#endif
Douglas Gregor60b5d8e2009-10-05 18:52:24 +000058}
59
Ted Kremeneka2a9d6e2010-02-12 22:54:40 +000060std::string getClangFullRepositoryVersion() {
61 std::string buf;
62 llvm::raw_string_ostream OS(buf);
Daniel Dunbar16a8fb72010-09-29 19:15:29 +000063 std::string Path = getClangRepositoryPath();
64 std::string Revision = getClangRevision();
Daniel Dunbar640cf372010-09-29 17:57:10 +000065 if (!Path.empty())
66 OS << Path;
67 if (!Revision.empty()) {
68 if (!Path.empty())
69 OS << ' ';
70 OS << Revision;
71 }
Benjamin Kramer940f6462010-03-05 15:39:20 +000072 return OS.str();
Ted Kremenek3687a5d2010-01-22 22:29:50 +000073}
74
Ted Kremeneka2a9d6e2010-02-12 22:54:40 +000075std::string getClangFullVersion() {
76 std::string buf;
77 llvm::raw_string_ostream OS(buf);
Ted Kremenek3687a5d2010-01-22 22:29:50 +000078#ifdef CLANG_VENDOR
Ted Kremeneka2a9d6e2010-02-12 22:54:40 +000079 OS << CLANG_VENDOR;
Ted Kremenek3687a5d2010-01-22 22:29:50 +000080#endif
Ted Kremeneka2a9d6e2010-02-12 22:54:40 +000081 OS << "clang version " CLANG_VERSION_STRING " ("
82 << getClangFullRepositoryVersion() << ')';
Daniel Dunbarddb6c8d2010-10-07 15:00:30 +000083
84 // If vendor supplied, include the base LLVM version as well.
85#ifdef CLANG_VENDOR
86 OS << " (based on LLVM " << PACKAGE_VERSION << ")";
87#endif
88
Benjamin Kramer940f6462010-03-05 15:39:20 +000089 return OS.str();
Ted Kremenekf7a96a32010-01-22 22:12:47 +000090}
Ted Kremeneka2a9d6e2010-02-12 22:54:40 +000091
Daniel Dunbarad1a4c62011-03-31 00:53:51 +000092std::string getClangFullCPPVersion() {
93 // The version string we report in __VERSION__ is just a compacted version of
94 // the one we report on the command line.
95 std::string buf;
96 llvm::raw_string_ostream OS(buf);
97#ifdef CLANG_VENDOR
98 OS << CLANG_VENDOR;
99#endif
100 OS << "Clang " CLANG_VERSION_STRING " ("
101 << getClangFullRepositoryVersion() << ')';
102 return OS.str();
103}
104
Douglas Gregor60b5d8e2009-10-05 18:52:24 +0000105} // end namespace clang