blob: 2eac2893a4deb0096aca802f08e69bebd34c0733 [file] [log] [blame]
Douglas Gregor7550a6c2009-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 Kremenek2377a0e2010-01-22 20:55:35 +000013
Ted Kremenek51b8bc92010-01-22 22:29:50 +000014#include "clang/Basic/Version.h"
Chris Lattner0e62c1c2011-07-23 10:55:15 +000015#include "clang/Basic/LLVM.h"
Ted Kremenek18e066f2010-01-22 22:12:47 +000016#include "llvm/Support/raw_ostream.h"
Douglas Gregor7550a6c2009-10-05 18:52:24 +000017#include <cstring>
18#include <cstdlib>
Ted Kremenek2377a0e2010-01-22 20:55:35 +000019
Douglas Gregor7550a6c2009-10-05 18:52:24 +000020namespace clang {
21
Daniel Dunbar181ca582010-09-29 19:15:29 +000022std::string getClangRepositoryPath() {
Daniel Dunbarb1798f72011-03-31 00:32:50 +000023#if defined(CLANG_REPOSITORY_STRING)
24 return CLANG_REPOSITORY_STRING;
25#else
Daniel Dunbarb800fdb2010-09-29 17:57:10 +000026#ifdef SVN_REPOSITORY
Chris Lattner0e62c1c2011-07-23 10:55:15 +000027 StringRef URL(SVN_REPOSITORY);
Daniel Dunbar181ca582010-09-29 19:15:29 +000028#else
Chris Lattner0e62c1c2011-07-23 10:55:15 +000029 StringRef URL("");
Daniel Dunbarb800fdb2010-09-29 17:57:10 +000030#endif
Daniel Dunbar181ca582010-09-29 19:15:29 +000031
Daniel Dunbard097d912010-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.
Chris Lattner0e62c1c2011-07-23 10:55:15 +000034 static StringRef SVNRepository("$URL$");
Daniel Dunbar48ed37d2010-10-11 23:50:34 +000035 if (URL.empty()) {
36 URL = SVNRepository.slice(SVNRepository.find(':'),
37 SVNRepository.find("/lib/Basic"));
38 }
Daniel Dunbard097d912010-10-11 23:44:19 +000039
Daniel Dunbar181ca582010-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/");
Chris Lattner0e62c1c2011-07-23 10:55:15 +000045 if (Start != StringRef::npos)
Daniel Dunbar181ca582010-09-29 19:15:29 +000046 URL = URL.substr(Start + 4);
47
48 return URL;
Daniel Dunbarb1798f72011-03-31 00:32:50 +000049#endif
Douglas Gregor7550a6c2009-10-05 18:52:24 +000050}
51
Daniel Dunbar181ca582010-09-29 19:15:29 +000052std::string getClangRevision() {
Ted Kremenek47307292010-03-03 01:02:48 +000053#ifdef SVN_REVISION
Daniel Dunbar181ca582010-09-29 19:15:29 +000054 return SVN_REVISION;
55#else
Ted Kremenek47307292010-03-03 01:02:48 +000056 return "";
Daniel Dunbar181ca582010-09-29 19:15:29 +000057#endif
Douglas Gregor7550a6c2009-10-05 18:52:24 +000058}
59
Ted Kremeneka3e65702010-02-12 22:54:40 +000060std::string getClangFullRepositoryVersion() {
61 std::string buf;
62 llvm::raw_string_ostream OS(buf);
Daniel Dunbar181ca582010-09-29 19:15:29 +000063 std::string Path = getClangRepositoryPath();
64 std::string Revision = getClangRevision();
Daniel Dunbarb800fdb2010-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 Kramerd48707002010-03-05 15:39:20 +000072 return OS.str();
Ted Kremenek51b8bc92010-01-22 22:29:50 +000073}
74
Ted Kremeneka3e65702010-02-12 22:54:40 +000075std::string getClangFullVersion() {
76 std::string buf;
77 llvm::raw_string_ostream OS(buf);
Ted Kremenek51b8bc92010-01-22 22:29:50 +000078#ifdef CLANG_VENDOR
Ted Kremeneka3e65702010-02-12 22:54:40 +000079 OS << CLANG_VENDOR;
Ted Kremenek51b8bc92010-01-22 22:29:50 +000080#endif
Ted Kremeneka3e65702010-02-12 22:54:40 +000081 OS << "clang version " CLANG_VERSION_STRING " ("
82 << getClangFullRepositoryVersion() << ')';
Daniel Dunbar60362642010-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 Kramerd48707002010-03-05 15:39:20 +000089 return OS.str();
Ted Kremenek18e066f2010-01-22 22:12:47 +000090}
Ted Kremeneka3e65702010-02-12 22:54:40 +000091
Daniel Dunbar3b17a862011-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 Gregor7550a6c2009-10-05 18:52:24 +0000105} // end namespace clang