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" |
Daniel Dunbar | ddb6c8d | 2010-10-07 15:00:30 +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 { |
| 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 | |
Daniel Dunbar | 16a8fb7 | 2010-09-29 19:15:29 +0000 | [diff] [blame] | 53 | std::string getClangRevision() { |
Ted Kremenek | 971cc48 | 2010-03-03 01:02:48 +0000 | [diff] [blame] | 54 | #ifdef SVN_REVISION |
Daniel Dunbar | 16a8fb7 | 2010-09-29 19:15:29 +0000 | [diff] [blame] | 55 | return SVN_REVISION; |
| 56 | #else |
Ted Kremenek | 971cc48 | 2010-03-03 01:02:48 +0000 | [diff] [blame] | 57 | return ""; |
Daniel Dunbar | 16a8fb7 | 2010-09-29 19:15:29 +0000 | [diff] [blame] | 58 | #endif |
Douglas Gregor | 60b5d8e | 2009-10-05 18:52:24 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Ted Kremenek | a2a9d6e | 2010-02-12 22:54:40 +0000 | [diff] [blame] | 61 | std::string getClangFullRepositoryVersion() { |
| 62 | std::string buf; |
| 63 | llvm::raw_string_ostream OS(buf); |
Daniel Dunbar | 16a8fb7 | 2010-09-29 19:15:29 +0000 | [diff] [blame] | 64 | std::string Path = getClangRepositoryPath(); |
| 65 | std::string Revision = getClangRevision(); |
Daniel Dunbar | 640cf37 | 2010-09-29 17:57:10 +0000 | [diff] [blame] | 66 | if (!Path.empty()) |
| 67 | OS << Path; |
| 68 | if (!Revision.empty()) { |
| 69 | if (!Path.empty()) |
| 70 | OS << ' '; |
| 71 | OS << Revision; |
| 72 | } |
Benjamin Kramer | 940f646 | 2010-03-05 15:39:20 +0000 | [diff] [blame] | 73 | return OS.str(); |
Ted Kremenek | 3687a5d | 2010-01-22 22:29:50 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Ted Kremenek | a2a9d6e | 2010-02-12 22:54:40 +0000 | [diff] [blame] | 76 | std::string getClangFullVersion() { |
| 77 | std::string buf; |
| 78 | llvm::raw_string_ostream OS(buf); |
Ted Kremenek | 3687a5d | 2010-01-22 22:29:50 +0000 | [diff] [blame] | 79 | #ifdef CLANG_VENDOR |
Ted Kremenek | a2a9d6e | 2010-02-12 22:54:40 +0000 | [diff] [blame] | 80 | OS << CLANG_VENDOR; |
Ted Kremenek | 3687a5d | 2010-01-22 22:29:50 +0000 | [diff] [blame] | 81 | #endif |
Ted Kremenek | a2a9d6e | 2010-02-12 22:54:40 +0000 | [diff] [blame] | 82 | OS << "clang version " CLANG_VERSION_STRING " (" |
| 83 | << getClangFullRepositoryVersion() << ')'; |
Daniel Dunbar | ddb6c8d | 2010-10-07 15:00:30 +0000 | [diff] [blame] | 84 | |
| 85 | // If vendor supplied, include the base LLVM version as well. |
| 86 | #ifdef CLANG_VENDOR |
| 87 | OS << " (based on LLVM " << PACKAGE_VERSION << ")"; |
| 88 | #endif |
| 89 | |
Benjamin Kramer | 940f646 | 2010-03-05 15:39:20 +0000 | [diff] [blame] | 90 | return OS.str(); |
Ted Kremenek | f7a96a3 | 2010-01-22 22:12:47 +0000 | [diff] [blame] | 91 | } |
Ted Kremenek | a2a9d6e | 2010-02-12 22:54:40 +0000 | [diff] [blame] | 92 | |
Daniel Dunbar | ad1a4c6 | 2011-03-31 00:53:51 +0000 | [diff] [blame] | 93 | std::string getClangFullCPPVersion() { |
| 94 | // The version string we report in __VERSION__ is just a compacted version of |
| 95 | // the one we report on the command line. |
| 96 | std::string buf; |
| 97 | llvm::raw_string_ostream OS(buf); |
| 98 | #ifdef CLANG_VENDOR |
| 99 | OS << CLANG_VENDOR; |
| 100 | #endif |
| 101 | OS << "Clang " CLANG_VERSION_STRING " (" |
| 102 | << getClangFullRepositoryVersion() << ')'; |
| 103 | return OS.str(); |
| 104 | } |
| 105 | |
Douglas Gregor | 60b5d8e | 2009-10-05 18:52:24 +0000 | [diff] [blame] | 106 | } // end namespace clang |