blob: 9367319c5f453afa383f2930ca471d579ba66f68 [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 Dunbar640cf372010-09-29 17:57:10 +000025#ifdef SVN_REPOSITORY
Daniel Dunbar16a8fb72010-09-29 19:15:29 +000026 llvm::StringRef URL(SVN_REPOSITORY);
27#else
28 llvm::StringRef URL("");
Daniel Dunbar640cf372010-09-29 17:57:10 +000029#endif
Daniel Dunbar16a8fb72010-09-29 19:15:29 +000030
Daniel Dunbar7171f2a2010-10-11 23:44:19 +000031 // If the SVN_REPOSITORY is empty, try to use the SVN keyword. This helps us
32 // pick up a tag in an SVN export, for example.
33 static llvm::StringRef SVNRepository("$URL$");
Daniel Dunbar83e18f82010-10-11 23:50:34 +000034 if (URL.empty()) {
35 URL = SVNRepository.slice(SVNRepository.find(':'),
36 SVNRepository.find("/lib/Basic"));
37 }
Daniel Dunbar7171f2a2010-10-11 23:44:19 +000038
Daniel Dunbar16a8fb72010-09-29 19:15:29 +000039 // Strip off version from a build from an integration branch.
40 URL = URL.slice(0, URL.find("/src/tools/clang"));
41
42 // Trim path prefix off, assuming path came from standard cfe path.
43 size_t Start = URL.find("cfe/");
44 if (Start != llvm::StringRef::npos)
45 URL = URL.substr(Start + 4);
46
47 return URL;
Douglas Gregor60b5d8e2009-10-05 18:52:24 +000048}
49
Daniel Dunbar16a8fb72010-09-29 19:15:29 +000050std::string getClangRevision() {
Ted Kremenek971cc482010-03-03 01:02:48 +000051#ifdef SVN_REVISION
Daniel Dunbar16a8fb72010-09-29 19:15:29 +000052 return SVN_REVISION;
53#else
Ted Kremenek971cc482010-03-03 01:02:48 +000054 return "";
Daniel Dunbar16a8fb72010-09-29 19:15:29 +000055#endif
Douglas Gregor60b5d8e2009-10-05 18:52:24 +000056}
57
Ted Kremeneka2a9d6e2010-02-12 22:54:40 +000058std::string getClangFullRepositoryVersion() {
59 std::string buf;
60 llvm::raw_string_ostream OS(buf);
Daniel Dunbar16a8fb72010-09-29 19:15:29 +000061 std::string Path = getClangRepositoryPath();
62 std::string Revision = getClangRevision();
Daniel Dunbar640cf372010-09-29 17:57:10 +000063 if (!Path.empty())
64 OS << Path;
65 if (!Revision.empty()) {
66 if (!Path.empty())
67 OS << ' ';
68 OS << Revision;
69 }
Benjamin Kramer940f6462010-03-05 15:39:20 +000070 return OS.str();
Ted Kremenek3687a5d2010-01-22 22:29:50 +000071}
72
Ted Kremeneka2a9d6e2010-02-12 22:54:40 +000073std::string getClangFullVersion() {
74 std::string buf;
75 llvm::raw_string_ostream OS(buf);
Ted Kremenek3687a5d2010-01-22 22:29:50 +000076#ifdef CLANG_VENDOR
Ted Kremeneka2a9d6e2010-02-12 22:54:40 +000077 OS << CLANG_VENDOR;
Ted Kremenek3687a5d2010-01-22 22:29:50 +000078#endif
Ted Kremeneka2a9d6e2010-02-12 22:54:40 +000079 OS << "clang version " CLANG_VERSION_STRING " ("
80 << getClangFullRepositoryVersion() << ')';
Daniel Dunbarddb6c8d2010-10-07 15:00:30 +000081
82 // If vendor supplied, include the base LLVM version as well.
83#ifdef CLANG_VENDOR
84 OS << " (based on LLVM " << PACKAGE_VERSION << ")";
85#endif
86
Benjamin Kramer940f6462010-03-05 15:39:20 +000087 return OS.str();
Ted Kremenekf7a96a32010-01-22 22:12:47 +000088}
Ted Kremeneka2a9d6e2010-02-12 22:54:40 +000089
Douglas Gregor60b5d8e2009-10-05 18:52:24 +000090} // end namespace clang