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" |
Daniel Dunbar | ddb6c8d | 2010-10-07 15:00:30 +0000 | [diff] [blame] | 16 | #include "llvm/Config/config.h" |
Douglas Gregor | 60b5d8e | 2009-10-05 18:52:24 +0000 | [diff] [blame] | 17 | #include <cstring> |
| 18 | #include <cstdlib> |
Ted Kremenek | 517e676 | 2010-01-22 20:55:35 +0000 | [diff] [blame] | 19 | |
Douglas Gregor | 60b5d8e | 2009-10-05 18:52:24 +0000 | [diff] [blame] | 20 | using namespace std; |
| 21 | |
| 22 | namespace clang { |
| 23 | |
Daniel Dunbar | 16a8fb7 | 2010-09-29 19:15:29 +0000 | [diff] [blame] | 24 | std::string getClangRepositoryPath() { |
Daniel Dunbar | 640cf37 | 2010-09-29 17:57:10 +0000 | [diff] [blame] | 25 | #ifdef SVN_REPOSITORY |
Daniel Dunbar | 16a8fb7 | 2010-09-29 19:15:29 +0000 | [diff] [blame] | 26 | llvm::StringRef URL(SVN_REPOSITORY); |
| 27 | #else |
| 28 | llvm::StringRef URL(""); |
Daniel Dunbar | 640cf37 | 2010-09-29 17:57:10 +0000 | [diff] [blame] | 29 | #endif |
Daniel Dunbar | 16a8fb7 | 2010-09-29 19:15:29 +0000 | [diff] [blame] | 30 | |
Daniel Dunbar | 7171f2a | 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$"); |
Daniel Dunbar | 83e18f8 | 2010-10-11 23:50:34 +0000 | [diff] [blame] | 34 | if (URL.empty()) { |
| 35 | URL = SVNRepository.slice(SVNRepository.find(':'), |
| 36 | SVNRepository.find("/lib/Basic")); |
| 37 | } |
Daniel Dunbar | 7171f2a | 2010-10-11 23:44:19 +0000 | [diff] [blame] | 38 | |
Daniel Dunbar | 16a8fb7 | 2010-09-29 19:15:29 +0000 | [diff] [blame] | 39 | // 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 Gregor | 60b5d8e | 2009-10-05 18:52:24 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Daniel Dunbar | 16a8fb7 | 2010-09-29 19:15:29 +0000 | [diff] [blame] | 50 | std::string getClangRevision() { |
Ted Kremenek | 971cc48 | 2010-03-03 01:02:48 +0000 | [diff] [blame] | 51 | #ifdef SVN_REVISION |
Daniel Dunbar | 16a8fb7 | 2010-09-29 19:15:29 +0000 | [diff] [blame] | 52 | return SVN_REVISION; |
| 53 | #else |
Ted Kremenek | 971cc48 | 2010-03-03 01:02:48 +0000 | [diff] [blame] | 54 | return ""; |
Daniel Dunbar | 16a8fb7 | 2010-09-29 19:15:29 +0000 | [diff] [blame] | 55 | #endif |
Douglas Gregor | 60b5d8e | 2009-10-05 18:52:24 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Ted Kremenek | a2a9d6e | 2010-02-12 22:54:40 +0000 | [diff] [blame] | 58 | std::string getClangFullRepositoryVersion() { |
| 59 | std::string buf; |
| 60 | llvm::raw_string_ostream OS(buf); |
Daniel Dunbar | 16a8fb7 | 2010-09-29 19:15:29 +0000 | [diff] [blame] | 61 | std::string Path = getClangRepositoryPath(); |
| 62 | std::string Revision = getClangRevision(); |
Daniel Dunbar | 640cf37 | 2010-09-29 17:57:10 +0000 | [diff] [blame] | 63 | if (!Path.empty()) |
| 64 | OS << Path; |
| 65 | if (!Revision.empty()) { |
| 66 | if (!Path.empty()) |
| 67 | OS << ' '; |
| 68 | OS << Revision; |
| 69 | } |
Benjamin Kramer | 940f646 | 2010-03-05 15:39:20 +0000 | [diff] [blame] | 70 | return OS.str(); |
Ted Kremenek | 3687a5d | 2010-01-22 22:29:50 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Ted Kremenek | a2a9d6e | 2010-02-12 22:54:40 +0000 | [diff] [blame] | 73 | std::string getClangFullVersion() { |
| 74 | std::string buf; |
| 75 | llvm::raw_string_ostream OS(buf); |
Ted Kremenek | 3687a5d | 2010-01-22 22:29:50 +0000 | [diff] [blame] | 76 | #ifdef CLANG_VENDOR |
Ted Kremenek | a2a9d6e | 2010-02-12 22:54:40 +0000 | [diff] [blame] | 77 | OS << CLANG_VENDOR; |
Ted Kremenek | 3687a5d | 2010-01-22 22:29:50 +0000 | [diff] [blame] | 78 | #endif |
Ted Kremenek | a2a9d6e | 2010-02-12 22:54:40 +0000 | [diff] [blame] | 79 | OS << "clang version " CLANG_VERSION_STRING " (" |
| 80 | << getClangFullRepositoryVersion() << ')'; |
Daniel Dunbar | ddb6c8d | 2010-10-07 15:00:30 +0000 | [diff] [blame] | 81 | |
| 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 Kramer | 940f646 | 2010-03-05 15:39:20 +0000 | [diff] [blame] | 87 | return OS.str(); |
Ted Kremenek | f7a96a3 | 2010-01-22 22:12:47 +0000 | [diff] [blame] | 88 | } |
Ted Kremenek | a2a9d6e | 2010-02-12 22:54:40 +0000 | [diff] [blame] | 89 | |
Douglas Gregor | 60b5d8e | 2009-10-05 18:52:24 +0000 | [diff] [blame] | 90 | } // end namespace clang |