Douglas Gregor | 7550a6c | 2009-10-05 18:52:24 | [diff] [blame] | 1 | //===- Version.cpp - Clang Version Number -----------------------*- C++ -*-===// |
2 | // | ||||
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. | ||||
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||||
Douglas Gregor | 7550a6c | 2009-10-05 18:52:24 | [diff] [blame] | 6 | // |
7 | //===----------------------------------------------------------------------===// | ||||
8 | // | ||||
9 | // This file defines several version-related utility functions for Clang. | ||||
10 | // | ||||
11 | //===----------------------------------------------------------------------===// | ||||
Ted Kremenek | 2377a0e | 2010-01-22 20:55:35 | [diff] [blame] | 12 | |
Ted Kremenek | 51b8bc9 | 2010-01-22 22:29:50 | [diff] [blame] | 13 | #include "clang/Basic/Version.h" |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 | [diff] [blame] | 14 | #include "clang/Basic/LLVM.h" |
Alp Toker | f988d00 | 2014-06-06 10:36:22 | [diff] [blame] | 15 | #include "clang/Config/config.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 | [diff] [blame] | 16 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | 7550a6c | 2009-10-05 18:52:24 | [diff] [blame] | 17 | #include <cstdlib> |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 | [diff] [blame] | 18 | #include <cstring> |
Ted Kremenek | 2377a0e | 2010-01-22 20:55:35 | [diff] [blame] | 19 | |
Petr Hosek | 23fdd5a | 2019-02-06 03:51:00 | [diff] [blame] | 20 | #ifdef HAVE_VCS_VERSION_INC |
21 | #include "VCSVersion.inc" | ||||
Petr Hosek | 12062e0 | 2019-01-31 07:12:43 | [diff] [blame] | 22 | #endif |
23 | |||||
Douglas Gregor | 7550a6c | 2009-10-05 18:52:24 | [diff] [blame] | 24 | namespace clang { |
Jia Liu | 5c30248 | 2012-03-02 14:37:41 | [diff] [blame] | 25 | |
Daniel Dunbar | 181ca58 | 2010-09-29 19:15:29 | [diff] [blame] | 26 | std::string getClangRepositoryPath() { |
Daniel Dunbar | b1798f7 | 2011-03-31 00:32:50 | [diff] [blame] | 27 | #if defined(CLANG_REPOSITORY_STRING) |
28 | return CLANG_REPOSITORY_STRING; | ||||
29 | #else | ||||
Petr Hosek | 23fdd5a | 2019-02-06 03:51:00 | [diff] [blame] | 30 | #ifdef CLANG_REPOSITORY |
Nico Weber | fb9413c | 2020-01-16 15:27:25 | [diff] [blame] | 31 | return CLANG_REPOSITORY; |
Daniel Dunbar | 181ca58 | 2010-09-29 19:15:29 | [diff] [blame] | 32 | #else |
Nico Weber | fb9413c | 2020-01-16 15:27:25 | [diff] [blame] | 33 | return ""; |
Daniel Dunbar | b800fdb | 2010-09-29 17:57:10 | [diff] [blame] | 34 | #endif |
Daniel Dunbar | b1798f7 | 2011-03-31 00:32:50 | [diff] [blame] | 35 | #endif |
Douglas Gregor | 7550a6c | 2009-10-05 18:52:24 | [diff] [blame] | 36 | } |
37 | |||||
Jia Liu | 5c30248 | 2012-03-02 14:37:41 | [diff] [blame] | 38 | std::string getLLVMRepositoryPath() { |
39 | #ifdef LLVM_REPOSITORY | ||||
Nico Weber | fb9413c | 2020-01-16 15:27:25 | [diff] [blame] | 40 | return LLVM_REPOSITORY; |
Jia Liu | 5c30248 | 2012-03-02 14:37:41 | [diff] [blame] | 41 | #else |
Nico Weber | fb9413c | 2020-01-16 15:27:25 | [diff] [blame] | 42 | return ""; |
Jia Liu | 5c30248 | 2012-03-02 14:37:41 | [diff] [blame] | 43 | #endif |
Jia Liu | 5c30248 | 2012-03-02 14:37:41 | [diff] [blame] | 44 | } |
45 | |||||
Daniel Dunbar | 181ca58 | 2010-09-29 19:15:29 | [diff] [blame] | 46 | std::string getClangRevision() { |
Petr Hosek | 23fdd5a | 2019-02-06 03:51:00 | [diff] [blame] | 47 | #ifdef CLANG_REVISION |
48 | return CLANG_REVISION; | ||||
Daniel Dunbar | 181ca58 | 2010-09-29 19:15:29 | [diff] [blame] | 49 | #else |
Ted Kremenek | 4730729 | 2010-03-03 01:02:48 | [diff] [blame] | 50 | return ""; |
Daniel Dunbar | 181ca58 | 2010-09-29 19:15:29 | [diff] [blame] | 51 | #endif |
Douglas Gregor | 7550a6c | 2009-10-05 18:52:24 | [diff] [blame] | 52 | } |
53 | |||||
Jia Liu | 5c30248 | 2012-03-02 14:37:41 | [diff] [blame] | 54 | std::string getLLVMRevision() { |
55 | #ifdef LLVM_REVISION | ||||
56 | return LLVM_REVISION; | ||||
57 | #else | ||||
58 | return ""; | ||||
59 | #endif | ||||
60 | } | ||||
61 | |||||
Ted Kremenek | a3e6570 | 2010-02-12 22:54:40 | [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 | [diff] [blame] | 65 | std::string Path = getClangRepositoryPath(); |
66 | std::string Revision = getClangRevision(); | ||||
Andrew Trick | 9179e8a | 2012-03-07 00:44:24 | [diff] [blame] | 67 | if (!Path.empty() || !Revision.empty()) { |
68 | OS << '('; | ||||
Daniel Dunbar | b800fdb | 2010-09-29 17:57:10 | [diff] [blame] | 69 | if (!Path.empty()) |
Andrew Trick | 9179e8a | 2012-03-07 00:44:24 | [diff] [blame] | 70 | OS << Path; |
71 | if (!Revision.empty()) { | ||||
72 | if (!Path.empty()) | ||||
73 | OS << ' '; | ||||
74 | OS << Revision; | ||||
75 | } | ||||
76 | OS << ')'; | ||||
Sylvestre Ledru | 096d6e4 | 2014-01-14 10:25:26 | [diff] [blame] | 77 | } |
Jia Liu | 5c30248 | 2012-03-02 14:37:41 | [diff] [blame] | 78 | // Support LLVM in a separate repository. |
79 | std::string LLVMRev = getLLVMRevision(); | ||||
80 | if (!LLVMRev.empty() && LLVMRev != Revision) { | ||||
Sylvestre Ledru | 096d6e4 | 2014-01-14 10:25:26 | [diff] [blame] | 81 | OS << " ("; |
Jia Liu | 5c30248 | 2012-03-02 14:37:41 | [diff] [blame] | 82 | std::string LLVMRepo = getLLVMRepositoryPath(); |
83 | if (!LLVMRepo.empty()) | ||||
Andrew Trick | 9179e8a | 2012-03-07 00:44:24 | [diff] [blame] | 84 | OS << LLVMRepo << ' '; |
85 | OS << LLVMRev << ')'; | ||||
Jia Liu | 5c30248 | 2012-03-02 14:37:41 | [diff] [blame] | 86 | } |
Benjamin Kramer | d4870700 | 2010-03-05 15:39:20 | [diff] [blame] | 87 | return OS.str(); |
Ted Kremenek | 51b8bc9 | 2010-01-22 22:29:50 | [diff] [blame] | 88 | } |
Jia Liu | 5c30248 | 2012-03-02 14:37:41 | [diff] [blame] | 89 | |
Ted Kremenek | a3e6570 | 2010-02-12 22:54:40 | [diff] [blame] | 90 | std::string getClangFullVersion() { |
Nico Weber | b00d66e | 2014-01-07 16:27:35 | [diff] [blame] | 91 | return getClangToolFullVersion("clang"); |
92 | } | ||||
93 | |||||
94 | std::string getClangToolFullVersion(StringRef ToolName) { | ||||
Ted Kremenek | a3e6570 | 2010-02-12 22:54:40 | [diff] [blame] | 95 | std::string buf; |
96 | llvm::raw_string_ostream OS(buf); | ||||
Ted Kremenek | 51b8bc9 | 2010-01-22 22:29:50 | [diff] [blame] | 97 | #ifdef CLANG_VENDOR |
Ted Kremenek | a3e6570 | 2010-02-12 22:54:40 | [diff] [blame] | 98 | OS << CLANG_VENDOR; |
Ted Kremenek | 51b8bc9 | 2010-01-22 22:29:50 | [diff] [blame] | 99 | #endif |
Nico Weber | b00d66e | 2014-01-07 16:27:35 | [diff] [blame] | 100 | OS << ToolName << " version " CLANG_VERSION_STRING " " |
Andrew Trick | 9179e8a | 2012-03-07 00:44:24 | [diff] [blame] | 101 | << getClangFullRepositoryVersion(); |
Daniel Dunbar | 6036264 | 2010-10-07 15:00:30 | [diff] [blame] | 102 | |
Benjamin Kramer | d4870700 | 2010-03-05 15:39:20 | [diff] [blame] | 103 | return OS.str(); |
Ted Kremenek | 18e066f | 2010-01-22 22:12:47 | [diff] [blame] | 104 | } |
Ted Kremenek | a3e6570 | 2010-02-12 22:54:40 | [diff] [blame] | 105 | |
Daniel Dunbar | 3b17a86 | 2011-03-31 00:53:51 | [diff] [blame] | 106 | std::string getClangFullCPPVersion() { |
Sylvestre Ledru | 21a92a8 | 2019-07-13 06:27:35 | [diff] [blame] | 107 | // The version string we report in __VERSION__ is just a compacted version of |
108 | // the one we report on the command line. | ||||
Daniel Dunbar | 3b17a86 | 2011-03-31 00:53:51 | [diff] [blame] | 109 | std::string buf; |
110 | llvm::raw_string_ostream OS(buf); | ||||
111 | #ifdef CLANG_VENDOR | ||||
112 | OS << CLANG_VENDOR; | ||||
113 | #endif | ||||
Benjamin Kramer | 7b01b57 | 2012-05-26 19:39:52 | [diff] [blame] | 114 | OS << "Clang " CLANG_VERSION_STRING " " << getClangFullRepositoryVersion(); |
Daniel Dunbar | 3b17a86 | 2011-03-31 00:53:51 | [diff] [blame] | 115 | return OS.str(); |
116 | } | ||||
117 | |||||
Douglas Gregor | 7550a6c | 2009-10-05 18:52:24 | [diff] [blame] | 118 | } // end namespace clang |