Douglas Gregor | 7550a6c | 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 | 2377a0e | 2010-01-22 20:55:35 +0000 | [diff] [blame] | 13 | |
Ted Kremenek | 51b8bc9 | 2010-01-22 22:29:50 +0000 | [diff] [blame] | 14 | #include "clang/Basic/Version.h" |
Ted Kremenek | 18e066f | 2010-01-22 22:12:47 +0000 | [diff] [blame] | 15 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | 6036264 | 2010-10-07 15:00:30 +0000 | [diff] [blame] | 16 | #include "llvm/Config/config.h" |
Douglas Gregor | 7550a6c | 2009-10-05 18:52:24 +0000 | [diff] [blame] | 17 | #include <cstring> |
| 18 | #include <cstdlib> |
Ted Kremenek | 2377a0e | 2010-01-22 20:55:35 +0000 | [diff] [blame] | 19 | |
Douglas Gregor | 7550a6c | 2009-10-05 18:52:24 +0000 | [diff] [blame] | 20 | using namespace std; |
| 21 | |
| 22 | namespace clang { |
| 23 | |
Daniel Dunbar | 181ca58 | 2010-09-29 19:15:29 +0000 | [diff] [blame] | 24 | std::string getClangRepositoryPath() { |
Daniel Dunbar | b800fdb | 2010-09-29 17:57:10 +0000 | [diff] [blame] | 25 | #ifdef SVN_REPOSITORY |
Daniel Dunbar | 181ca58 | 2010-09-29 19:15:29 +0000 | [diff] [blame] | 26 | llvm::StringRef URL(SVN_REPOSITORY); |
| 27 | #else |
| 28 | llvm::StringRef URL(""); |
Daniel Dunbar | b800fdb | 2010-09-29 17:57:10 +0000 | [diff] [blame] | 29 | #endif |
Daniel Dunbar | 181ca58 | 2010-09-29 19:15:29 +0000 | [diff] [blame] | 30 | |
Daniel Dunbar | d097d91 | 2010-10-11 23:44:19 +0000 | [diff] [blame^] | 31 | // 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$"); |
| 34 | if (URL.empty()) |
| 35 | URL = SVNRepository.split(':').second; |
| 36 | |
Daniel Dunbar | 181ca58 | 2010-09-29 19:15:29 +0000 | [diff] [blame] | 37 | // Strip off version from a build from an integration branch. |
| 38 | URL = URL.slice(0, URL.find("/src/tools/clang")); |
| 39 | |
| 40 | // Trim path prefix off, assuming path came from standard cfe path. |
| 41 | size_t Start = URL.find("cfe/"); |
| 42 | if (Start != llvm::StringRef::npos) |
| 43 | URL = URL.substr(Start + 4); |
| 44 | |
| 45 | return URL; |
Douglas Gregor | 7550a6c | 2009-10-05 18:52:24 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Daniel Dunbar | 181ca58 | 2010-09-29 19:15:29 +0000 | [diff] [blame] | 48 | std::string getClangRevision() { |
Ted Kremenek | 4730729 | 2010-03-03 01:02:48 +0000 | [diff] [blame] | 49 | #ifdef SVN_REVISION |
Daniel Dunbar | 181ca58 | 2010-09-29 19:15:29 +0000 | [diff] [blame] | 50 | return SVN_REVISION; |
| 51 | #else |
Ted Kremenek | 4730729 | 2010-03-03 01:02:48 +0000 | [diff] [blame] | 52 | return ""; |
Daniel Dunbar | 181ca58 | 2010-09-29 19:15:29 +0000 | [diff] [blame] | 53 | #endif |
Douglas Gregor | 7550a6c | 2009-10-05 18:52:24 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Ted Kremenek | a3e6570 | 2010-02-12 22:54:40 +0000 | [diff] [blame] | 56 | std::string getClangFullRepositoryVersion() { |
| 57 | std::string buf; |
| 58 | llvm::raw_string_ostream OS(buf); |
Daniel Dunbar | 181ca58 | 2010-09-29 19:15:29 +0000 | [diff] [blame] | 59 | std::string Path = getClangRepositoryPath(); |
| 60 | std::string Revision = getClangRevision(); |
Daniel Dunbar | b800fdb | 2010-09-29 17:57:10 +0000 | [diff] [blame] | 61 | if (!Path.empty()) |
| 62 | OS << Path; |
| 63 | if (!Revision.empty()) { |
| 64 | if (!Path.empty()) |
| 65 | OS << ' '; |
| 66 | OS << Revision; |
| 67 | } |
Benjamin Kramer | d4870700 | 2010-03-05 15:39:20 +0000 | [diff] [blame] | 68 | return OS.str(); |
Ted Kremenek | 51b8bc9 | 2010-01-22 22:29:50 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Ted Kremenek | a3e6570 | 2010-02-12 22:54:40 +0000 | [diff] [blame] | 71 | std::string getClangFullVersion() { |
| 72 | std::string buf; |
| 73 | llvm::raw_string_ostream OS(buf); |
Ted Kremenek | 51b8bc9 | 2010-01-22 22:29:50 +0000 | [diff] [blame] | 74 | #ifdef CLANG_VENDOR |
Ted Kremenek | a3e6570 | 2010-02-12 22:54:40 +0000 | [diff] [blame] | 75 | OS << CLANG_VENDOR; |
Ted Kremenek | 51b8bc9 | 2010-01-22 22:29:50 +0000 | [diff] [blame] | 76 | #endif |
Ted Kremenek | a3e6570 | 2010-02-12 22:54:40 +0000 | [diff] [blame] | 77 | OS << "clang version " CLANG_VERSION_STRING " (" |
| 78 | << getClangFullRepositoryVersion() << ')'; |
Daniel Dunbar | 6036264 | 2010-10-07 15:00:30 +0000 | [diff] [blame] | 79 | |
| 80 | // If vendor supplied, include the base LLVM version as well. |
| 81 | #ifdef CLANG_VENDOR |
| 82 | OS << " (based on LLVM " << PACKAGE_VERSION << ")"; |
| 83 | #endif |
| 84 | |
Benjamin Kramer | d4870700 | 2010-03-05 15:39:20 +0000 | [diff] [blame] | 85 | return OS.str(); |
Ted Kremenek | 18e066f | 2010-01-22 22:12:47 +0000 | [diff] [blame] | 86 | } |
Ted Kremenek | a3e6570 | 2010-02-12 22:54:40 +0000 | [diff] [blame] | 87 | |
Douglas Gregor | 7550a6c | 2009-10-05 18:52:24 +0000 | [diff] [blame] | 88 | } // end namespace clang |