blob: 10b93645877d4c1aef35282f9ab06cedd3544181 [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"
Daniel Dunbarddb6c8d2010-10-07 15:00:30 +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 {
22
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
Daniel Dunbar16a8fb72010-09-29 19:15:29 +000053std::string getClangRevision() {
Ted Kremenek971cc482010-03-03 01:02:48 +000054#ifdef SVN_REVISION
Daniel Dunbar16a8fb72010-09-29 19:15:29 +000055 return SVN_REVISION;
56#else
Ted Kremenek971cc482010-03-03 01:02:48 +000057 return "";
Daniel Dunbar16a8fb72010-09-29 19:15:29 +000058#endif
Douglas Gregor60b5d8e2009-10-05 18:52:24 +000059}
60
Ted Kremeneka2a9d6e2010-02-12 22:54:40 +000061std::string getClangFullRepositoryVersion() {
62 std::string buf;
63 llvm::raw_string_ostream OS(buf);
Daniel Dunbar16a8fb72010-09-29 19:15:29 +000064 std::string Path = getClangRepositoryPath();
65 std::string Revision = getClangRevision();
Daniel Dunbar640cf372010-09-29 17:57:10 +000066 if (!Path.empty())
67 OS << Path;
68 if (!Revision.empty()) {
69 if (!Path.empty())
70 OS << ' ';
71 OS << Revision;
72 }
Benjamin Kramer940f6462010-03-05 15:39:20 +000073 return OS.str();
Ted Kremenek3687a5d2010-01-22 22:29:50 +000074}
75
Ted Kremeneka2a9d6e2010-02-12 22:54:40 +000076std::string getClangFullVersion() {
77 std::string buf;
78 llvm::raw_string_ostream OS(buf);
Ted Kremenek3687a5d2010-01-22 22:29:50 +000079#ifdef CLANG_VENDOR
Ted Kremeneka2a9d6e2010-02-12 22:54:40 +000080 OS << CLANG_VENDOR;
Ted Kremenek3687a5d2010-01-22 22:29:50 +000081#endif
Ted Kremeneka2a9d6e2010-02-12 22:54:40 +000082 OS << "clang version " CLANG_VERSION_STRING " ("
83 << getClangFullRepositoryVersion() << ')';
Daniel Dunbarddb6c8d2010-10-07 15:00:30 +000084
85 // If vendor supplied, include the base LLVM version as well.
86#ifdef CLANG_VENDOR
87 OS << " (based on LLVM " << PACKAGE_VERSION << ")";
88#endif
89
Benjamin Kramer940f6462010-03-05 15:39:20 +000090 return OS.str();
Ted Kremenekf7a96a32010-01-22 22:12:47 +000091}
Ted Kremeneka2a9d6e2010-02-12 22:54:40 +000092
Daniel Dunbarad1a4c62011-03-31 00:53:51 +000093std::string getClangFullCPPVersion() {
94 // The version string we report in __VERSION__ is just a compacted version of
95 // the one we report on the command line.
96 std::string buf;
97 llvm::raw_string_ostream OS(buf);
98#ifdef CLANG_VENDOR
99 OS << CLANG_VENDOR;
100#endif
101 OS << "Clang " CLANG_VERSION_STRING " ("
102 << getClangFullRepositoryVersion() << ')';
103 return OS.str();
104}
105
Douglas Gregor60b5d8e2009-10-05 18:52:24 +0000106} // end namespace clang