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 | 9a4a9c2 | 2011-03-31 00:32:50 +0000 | [diff] [blame] | 25 | #if defined(CLANG_REPOSITORY_STRING) |
| 26 | return CLANG_REPOSITORY_STRING; |
| 27 | #else |
Daniel Dunbar | 640cf37 | 2010-09-29 17:57:10 +0000 | [diff] [blame] | 28 | #ifdef SVN_REPOSITORY |
Daniel Dunbar | 16a8fb7 | 2010-09-29 19:15:29 +0000 | [diff] [blame] | 29 | llvm::StringRef URL(SVN_REPOSITORY); |
| 30 | #else |
| 31 | llvm::StringRef URL(""); |
Daniel Dunbar | 640cf37 | 2010-09-29 17:57:10 +0000 | [diff] [blame] | 32 | #endif |
Daniel Dunbar | 16a8fb7 | 2010-09-29 19:15:29 +0000 | [diff] [blame] | 33 | |
Daniel Dunbar | 7171f2a | 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 | 83e18f8 | 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 | 7171f2a | 2010-10-11 23:44:19 +0000 | [diff] [blame] | 41 | |
Daniel Dunbar | 16a8fb7 | 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 | 9a4a9c2 | 2011-03-31 00:32:50 +0000 | [diff] [blame] | 51 | #endif |
Douglas Gregor | 60b5d8e | 2009-10-05 18:52:24 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Daniel Dunbar | 16a8fb7 | 2010-09-29 19:15:29 +0000 | [diff] [blame] | 54 | std::string getClangRevision() { |
Ted Kremenek | 971cc48 | 2010-03-03 01:02:48 +0000 | [diff] [blame] | 55 | #ifdef SVN_REVISION |
Daniel Dunbar | 16a8fb7 | 2010-09-29 19:15:29 +0000 | [diff] [blame] | 56 | return SVN_REVISION; |
| 57 | #else |
Ted Kremenek | 971cc48 | 2010-03-03 01:02:48 +0000 | [diff] [blame] | 58 | return ""; |
Daniel Dunbar | 16a8fb7 | 2010-09-29 19:15:29 +0000 | [diff] [blame] | 59 | #endif |
Douglas Gregor | 60b5d8e | 2009-10-05 18:52:24 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Ted Kremenek | a2a9d6e | 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 | 16a8fb7 | 2010-09-29 19:15:29 +0000 | [diff] [blame] | 65 | std::string Path = getClangRepositoryPath(); |
| 66 | std::string Revision = getClangRevision(); |
Daniel Dunbar | 640cf37 | 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 | 940f646 | 2010-03-05 15:39:20 +0000 | [diff] [blame] | 74 | return OS.str(); |
Ted Kremenek | 3687a5d | 2010-01-22 22:29:50 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Ted Kremenek | a2a9d6e | 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 | 3687a5d | 2010-01-22 22:29:50 +0000 | [diff] [blame] | 80 | #ifdef CLANG_VENDOR |
Ted Kremenek | a2a9d6e | 2010-02-12 22:54:40 +0000 | [diff] [blame] | 81 | OS << CLANG_VENDOR; |
Ted Kremenek | 3687a5d | 2010-01-22 22:29:50 +0000 | [diff] [blame] | 82 | #endif |
Ted Kremenek | a2a9d6e | 2010-02-12 22:54:40 +0000 | [diff] [blame] | 83 | OS << "clang version " CLANG_VERSION_STRING " (" |
| 84 | << getClangFullRepositoryVersion() << ')'; |
Daniel Dunbar | ddb6c8d | 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 | 940f646 | 2010-03-05 15:39:20 +0000 | [diff] [blame] | 91 | return OS.str(); |
Ted Kremenek | f7a96a3 | 2010-01-22 22:12:47 +0000 | [diff] [blame] | 92 | } |
Ted Kremenek | a2a9d6e | 2010-02-12 22:54:40 +0000 | [diff] [blame] | 93 | |
Daniel Dunbar | ad1a4c6 | 2011-03-31 00:53:51 +0000 | [diff] [blame] | 94 | std::string getClangFullCPPVersion() { |
| 95 | // The version string we report in __VERSION__ is just a compacted version of |
| 96 | // the one we report on the command line. |
| 97 | std::string buf; |
| 98 | llvm::raw_string_ostream OS(buf); |
| 99 | #ifdef CLANG_VENDOR |
| 100 | OS << CLANG_VENDOR; |
| 101 | #endif |
| 102 | OS << "Clang " CLANG_VERSION_STRING " (" |
| 103 | << getClangFullRepositoryVersion() << ')'; |
| 104 | return OS.str(); |
| 105 | } |
| 106 | |
Douglas Gregor | 60b5d8e | 2009-10-05 18:52:24 +0000 | [diff] [blame] | 107 | } // end namespace clang |