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" |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 15 | #include "clang/Basic/LLVM.h" |
Ted Kremenek | f7a96a3 | 2010-01-22 22:12:47 +0000 | [diff] [blame] | 16 | #include "llvm/Support/raw_ostream.h" |
Eli Friedman | fb8c56d | 2011-12-26 22:43:17 +0000 | [diff] [blame] | 17 | #include "llvm/Config/config.h" |
Douglas Gregor | 60b5d8e | 2009-10-05 18:52:24 +0000 | [diff] [blame] | 18 | #include <cstring> |
| 19 | #include <cstdlib> |
Ted Kremenek | 517e676 | 2010-01-22 20:55:35 +0000 | [diff] [blame] | 20 | |
Douglas Gregor | 60b5d8e | 2009-10-05 18:52:24 +0000 | [diff] [blame] | 21 | namespace clang { |
Jia Liu | f8e5d4c | 2012-03-02 14:37:41 +0000 | [diff] [blame] | 22 | |
Daniel Dunbar | 16a8fb7 | 2010-09-29 19:15:29 +0000 | [diff] [blame] | 23 | std::string getClangRepositoryPath() { |
Daniel Dunbar | 9a4a9c2 | 2011-03-31 00:32:50 +0000 | [diff] [blame] | 24 | #if defined(CLANG_REPOSITORY_STRING) |
| 25 | return CLANG_REPOSITORY_STRING; |
| 26 | #else |
Daniel Dunbar | 640cf37 | 2010-09-29 17:57:10 +0000 | [diff] [blame] | 27 | #ifdef SVN_REPOSITORY |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 28 | StringRef URL(SVN_REPOSITORY); |
Daniel Dunbar | 16a8fb7 | 2010-09-29 19:15:29 +0000 | [diff] [blame] | 29 | #else |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 30 | StringRef URL(""); |
Daniel Dunbar | 640cf37 | 2010-09-29 17:57:10 +0000 | [diff] [blame] | 31 | #endif |
Daniel Dunbar | 16a8fb7 | 2010-09-29 19:15:29 +0000 | [diff] [blame] | 32 | |
Daniel Dunbar | 7171f2a | 2010-10-11 23:44:19 +0000 | [diff] [blame] | 33 | // If the SVN_REPOSITORY is empty, try to use the SVN keyword. This helps us |
| 34 | // pick up a tag in an SVN export, for example. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 35 | static StringRef SVNRepository("$URL$"); |
Daniel Dunbar | 83e18f8 | 2010-10-11 23:50:34 +0000 | [diff] [blame] | 36 | if (URL.empty()) { |
| 37 | URL = SVNRepository.slice(SVNRepository.find(':'), |
| 38 | SVNRepository.find("/lib/Basic")); |
| 39 | } |
Daniel Dunbar | 7171f2a | 2010-10-11 23:44:19 +0000 | [diff] [blame] | 40 | |
Daniel Dunbar | 16a8fb7 | 2010-09-29 19:15:29 +0000 | [diff] [blame] | 41 | // Strip off version from a build from an integration branch. |
| 42 | URL = URL.slice(0, URL.find("/src/tools/clang")); |
| 43 | |
| 44 | // Trim path prefix off, assuming path came from standard cfe path. |
| 45 | size_t Start = URL.find("cfe/"); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 46 | if (Start != StringRef::npos) |
Daniel Dunbar | 16a8fb7 | 2010-09-29 19:15:29 +0000 | [diff] [blame] | 47 | URL = URL.substr(Start + 4); |
| 48 | |
| 49 | return URL; |
Daniel Dunbar | 9a4a9c2 | 2011-03-31 00:32:50 +0000 | [diff] [blame] | 50 | #endif |
Douglas Gregor | 60b5d8e | 2009-10-05 18:52:24 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Jia Liu | f8e5d4c | 2012-03-02 14:37:41 +0000 | [diff] [blame] | 53 | std::string getLLVMRepositoryPath() { |
| 54 | #ifdef LLVM_REPOSITORY |
| 55 | StringRef URL(LLVM_REPOSITORY); |
| 56 | #else |
| 57 | StringRef URL(""); |
| 58 | #endif |
| 59 | |
| 60 | // Trim path prefix off, assuming path came from standard llvm path. |
Andrew Trick | fddfbdb | 2012-03-07 00:44:24 +0000 | [diff] [blame] | 61 | // Leave "llvm/" prefix to distinguish the following llvm revision from the |
| 62 | // clang revision. |
Jia Liu | f8e5d4c | 2012-03-02 14:37:41 +0000 | [diff] [blame] | 63 | size_t Start = URL.find("llvm/"); |
| 64 | if (Start != StringRef::npos) |
Andrew Trick | fddfbdb | 2012-03-07 00:44:24 +0000 | [diff] [blame] | 65 | URL = URL.substr(Start); |
Jia Liu | f8e5d4c | 2012-03-02 14:37:41 +0000 | [diff] [blame] | 66 | |
| 67 | return URL; |
| 68 | } |
| 69 | |
Daniel Dunbar | 16a8fb7 | 2010-09-29 19:15:29 +0000 | [diff] [blame] | 70 | std::string getClangRevision() { |
Ted Kremenek | 971cc48 | 2010-03-03 01:02:48 +0000 | [diff] [blame] | 71 | #ifdef SVN_REVISION |
Daniel Dunbar | 16a8fb7 | 2010-09-29 19:15:29 +0000 | [diff] [blame] | 72 | return SVN_REVISION; |
| 73 | #else |
Ted Kremenek | 971cc48 | 2010-03-03 01:02:48 +0000 | [diff] [blame] | 74 | return ""; |
Daniel Dunbar | 16a8fb7 | 2010-09-29 19:15:29 +0000 | [diff] [blame] | 75 | #endif |
Douglas Gregor | 60b5d8e | 2009-10-05 18:52:24 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Jia Liu | f8e5d4c | 2012-03-02 14:37:41 +0000 | [diff] [blame] | 78 | std::string getLLVMRevision() { |
| 79 | #ifdef LLVM_REVISION |
| 80 | return LLVM_REVISION; |
| 81 | #else |
| 82 | return ""; |
| 83 | #endif |
| 84 | } |
| 85 | |
Ted Kremenek | a2a9d6e | 2010-02-12 22:54:40 +0000 | [diff] [blame] | 86 | std::string getClangFullRepositoryVersion() { |
| 87 | std::string buf; |
| 88 | llvm::raw_string_ostream OS(buf); |
Daniel Dunbar | 16a8fb7 | 2010-09-29 19:15:29 +0000 | [diff] [blame] | 89 | std::string Path = getClangRepositoryPath(); |
| 90 | std::string Revision = getClangRevision(); |
Andrew Trick | fddfbdb | 2012-03-07 00:44:24 +0000 | [diff] [blame] | 91 | if (!Path.empty() || !Revision.empty()) { |
| 92 | OS << '('; |
Daniel Dunbar | 640cf37 | 2010-09-29 17:57:10 +0000 | [diff] [blame] | 93 | if (!Path.empty()) |
Andrew Trick | fddfbdb | 2012-03-07 00:44:24 +0000 | [diff] [blame] | 94 | OS << Path; |
| 95 | if (!Revision.empty()) { |
| 96 | if (!Path.empty()) |
| 97 | OS << ' '; |
| 98 | OS << Revision; |
| 99 | } |
| 100 | OS << ')'; |
| 101 | } |
Jia Liu | f8e5d4c | 2012-03-02 14:37:41 +0000 | [diff] [blame] | 102 | // Support LLVM in a separate repository. |
| 103 | std::string LLVMRev = getLLVMRevision(); |
| 104 | if (!LLVMRev.empty() && LLVMRev != Revision) { |
Andrew Trick | fddfbdb | 2012-03-07 00:44:24 +0000 | [diff] [blame] | 105 | OS << " ("; |
Jia Liu | f8e5d4c | 2012-03-02 14:37:41 +0000 | [diff] [blame] | 106 | std::string LLVMRepo = getLLVMRepositoryPath(); |
| 107 | if (!LLVMRepo.empty()) |
Andrew Trick | fddfbdb | 2012-03-07 00:44:24 +0000 | [diff] [blame] | 108 | OS << LLVMRepo << ' '; |
| 109 | OS << LLVMRev << ')'; |
Jia Liu | f8e5d4c | 2012-03-02 14:37:41 +0000 | [diff] [blame] | 110 | } |
Benjamin Kramer | 940f646 | 2010-03-05 15:39:20 +0000 | [diff] [blame] | 111 | return OS.str(); |
Ted Kremenek | 3687a5d | 2010-01-22 22:29:50 +0000 | [diff] [blame] | 112 | } |
Jia Liu | f8e5d4c | 2012-03-02 14:37:41 +0000 | [diff] [blame] | 113 | |
Ted Kremenek | a2a9d6e | 2010-02-12 22:54:40 +0000 | [diff] [blame] | 114 | std::string getClangFullVersion() { |
| 115 | std::string buf; |
| 116 | llvm::raw_string_ostream OS(buf); |
Ted Kremenek | 3687a5d | 2010-01-22 22:29:50 +0000 | [diff] [blame] | 117 | #ifdef CLANG_VENDOR |
Ted Kremenek | a2a9d6e | 2010-02-12 22:54:40 +0000 | [diff] [blame] | 118 | OS << CLANG_VENDOR; |
Ted Kremenek | 3687a5d | 2010-01-22 22:29:50 +0000 | [diff] [blame] | 119 | #endif |
Andrew Trick | fddfbdb | 2012-03-07 00:44:24 +0000 | [diff] [blame] | 120 | OS << "clang version " CLANG_VERSION_STRING " " |
| 121 | << getClangFullRepositoryVersion(); |
Daniel Dunbar | ddb6c8d | 2010-10-07 15:00:30 +0000 | [diff] [blame] | 122 | |
| 123 | // If vendor supplied, include the base LLVM version as well. |
| 124 | #ifdef CLANG_VENDOR |
| 125 | OS << " (based on LLVM " << PACKAGE_VERSION << ")"; |
| 126 | #endif |
| 127 | |
Benjamin Kramer | 940f646 | 2010-03-05 15:39:20 +0000 | [diff] [blame] | 128 | return OS.str(); |
Ted Kremenek | f7a96a3 | 2010-01-22 22:12:47 +0000 | [diff] [blame] | 129 | } |
Ted Kremenek | a2a9d6e | 2010-02-12 22:54:40 +0000 | [diff] [blame] | 130 | |
Daniel Dunbar | ad1a4c6 | 2011-03-31 00:53:51 +0000 | [diff] [blame] | 131 | std::string getClangFullCPPVersion() { |
| 132 | // The version string we report in __VERSION__ is just a compacted version of |
| 133 | // the one we report on the command line. |
| 134 | std::string buf; |
| 135 | llvm::raw_string_ostream OS(buf); |
| 136 | #ifdef CLANG_VENDOR |
| 137 | OS << CLANG_VENDOR; |
| 138 | #endif |
Benjamin Kramer | 242cb06 | 2012-05-26 19:39:52 +0000 | [diff] [blame] | 139 | OS << "Clang " CLANG_VERSION_STRING " " << getClangFullRepositoryVersion(); |
Daniel Dunbar | ad1a4c6 | 2011-03-31 00:53:51 +0000 | [diff] [blame] | 140 | return OS.str(); |
| 141 | } |
| 142 | |
Douglas Gregor | 60b5d8e | 2009-10-05 18:52:24 +0000 | [diff] [blame] | 143 | } // end namespace clang |