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