Douglas Gregor | 60b5d8e | 2009-10-05 18:52:24 +0000 | [diff] [blame] | 1 | //===- 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 Kremenek | 517e676 | 2010-01-22 20:55:35 +0000 | [diff] [blame] | 13 | |
Ted Kremenek | 3687a5d | 2010-01-22 22:29:50 +0000 | [diff] [blame] | 14 | #include "clang/Basic/Version.h" |
Ted Kremenek | f7a96a3 | 2010-01-22 22:12:47 +0000 | [diff] [blame] | 15 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | 60b5d8e | 2009-10-05 18:52:24 +0000 | [diff] [blame] | 16 | #include <cstring> |
| 17 | #include <cstdlib> |
Ted Kremenek | 517e676 | 2010-01-22 20:55:35 +0000 | [diff] [blame] | 18 | |
Douglas Gregor | 60b5d8e | 2009-10-05 18:52:24 +0000 | [diff] [blame] | 19 | using namespace std; |
| 20 | |
| 21 | namespace clang { |
| 22 | |
Ted Kremenek | 517e676 | 2010-01-22 20:55:35 +0000 | [diff] [blame] | 23 | llvm::StringRef getClangRepositoryPath() { |
Douglas Gregor | 60b5d8e | 2009-10-05 18:52:24 +0000 | [diff] [blame] | 24 | static const char *Path = 0; |
| 25 | if (Path) |
| 26 | return Path; |
| 27 | |
| 28 | static char URL[] = "$URL$"; |
| 29 | char *End = strstr(URL, "/lib/Basic"); |
| 30 | if (End) |
| 31 | *End = 0; |
| 32 | |
Douglas Gregor | 1fbf1f0 | 2009-11-05 23:46:05 +0000 | [diff] [blame] | 33 | End = strstr(URL, "/clang/tools/clang"); |
| 34 | if (End) |
| 35 | *End = 0; |
| 36 | |
Douglas Gregor | 60b5d8e | 2009-10-05 18:52:24 +0000 | [diff] [blame] | 37 | char *Begin = strstr(URL, "cfe/"); |
| 38 | if (Begin) { |
| 39 | Path = Begin + 4; |
| 40 | return Path; |
| 41 | } |
| 42 | |
| 43 | Path = URL; |
| 44 | return Path; |
| 45 | } |
| 46 | |
| 47 | |
Ted Kremenek | f7a96a3 | 2010-01-22 22:12:47 +0000 | [diff] [blame] | 48 | llvm::StringRef getClangRevision() { |
Douglas Gregor | 60b5d8e | 2009-10-05 18:52:24 +0000 | [diff] [blame] | 49 | #ifndef SVN_REVISION |
Douglas Gregor | b8d1191 | 2009-10-05 20:33:49 +0000 | [diff] [blame] | 50 | // Subversion was not available at build time? |
Ted Kremenek | f7a96a3 | 2010-01-22 22:12:47 +0000 | [diff] [blame] | 51 | return llvm::StringRef(); |
Douglas Gregor | 60b5d8e | 2009-10-05 18:52:24 +0000 | [diff] [blame] | 52 | #else |
Ted Kremenek | f7a96a3 | 2010-01-22 22:12:47 +0000 | [diff] [blame] | 53 | static std::string revision; |
| 54 | if (revision.empty()) { |
Ted Kremenek | 3687a5d | 2010-01-22 22:29:50 +0000 | [diff] [blame] | 55 | llvm::raw_string_ostream OS(revision); |
| 56 | OS << strtol(SVN_REVISION, 0, 10); |
Ted Kremenek | f7a96a3 | 2010-01-22 22:12:47 +0000 | [diff] [blame] | 57 | } |
| 58 | return revision; |
Douglas Gregor | 60b5d8e | 2009-10-05 18:52:24 +0000 | [diff] [blame] | 59 | #endif |
| 60 | } |
| 61 | |
Ted Kremenek | f7a96a3 | 2010-01-22 22:12:47 +0000 | [diff] [blame] | 62 | llvm::StringRef getClangFullRepositoryVersion() { |
| 63 | static std::string buf; |
| 64 | if (buf.empty()) { |
Ted Kremenek | 3687a5d | 2010-01-22 22:29:50 +0000 | [diff] [blame] | 65 | llvm::raw_string_ostream OS(buf); |
| 66 | OS << getClangRepositoryPath(); |
Ted Kremenek | f7a96a3 | 2010-01-22 22:12:47 +0000 | [diff] [blame] | 67 | llvm::StringRef Revision = getClangRevision(); |
| 68 | if (!Revision.empty()) |
Ted Kremenek | 3687a5d | 2010-01-22 22:29:50 +0000 | [diff] [blame] | 69 | OS << ' ' << Revision; |
| 70 | } |
| 71 | return buf; |
| 72 | } |
| 73 | |
Ted Kremenek | 04bb716 | 2010-01-22 22:44:15 +0000 | [diff] [blame^] | 74 | const char *getClangFullVendorVersion() { |
Ted Kremenek | 3687a5d | 2010-01-22 22:29:50 +0000 | [diff] [blame] | 75 | static std::string buf; |
| 76 | if (buf.empty()) { |
| 77 | llvm::raw_string_ostream OS(buf); |
| 78 | #ifdef CLANG_VENDOR |
| 79 | OS << CLANG_VENDOR; |
| 80 | #endif |
| 81 | OS << "clang version " CLANG_VERSION_STRING " (" |
| 82 | << getClangFullRepositoryVersion() << ')'; |
Ted Kremenek | f7a96a3 | 2010-01-22 22:12:47 +0000 | [diff] [blame] | 83 | } |
Ted Kremenek | 04bb716 | 2010-01-22 22:44:15 +0000 | [diff] [blame^] | 84 | return buf.c_str(); |
Ted Kremenek | f7a96a3 | 2010-01-22 22:12:47 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Douglas Gregor | 60b5d8e | 2009-10-05 18:52:24 +0000 | [diff] [blame] | 87 | } // end namespace clang |