Version.cpp revision 221345
1198092Srdivacky//===- Version.cpp - Clang Version Number -----------------------*- C++ -*-===// 2198092Srdivacky// 3198092Srdivacky// The LLVM Compiler Infrastructure 4198092Srdivacky// 5198092Srdivacky// This file is distributed under the University of Illinois Open Source 6198092Srdivacky// License. See LICENSE.TXT for details. 7198092Srdivacky// 8198092Srdivacky//===----------------------------------------------------------------------===// 9198092Srdivacky// 10198092Srdivacky// This file defines several version-related utility functions for Clang. 11198092Srdivacky// 12198092Srdivacky//===----------------------------------------------------------------------===// 13202879Srdivacky 14202879Srdivacky#include "clang/Basic/Version.h" 15202879Srdivacky#include "llvm/Support/raw_ostream.h" 16218893Sdim#include "llvm/Config/config.h" 17198092Srdivacky#include <cstring> 18198092Srdivacky#include <cstdlib> 19202879Srdivacky 20198092Srdivackynamespace clang { 21198092Srdivacky 22218893Sdimstd::string getClangRepositoryPath() { 23221345Sdim#if defined(CLANG_REPOSITORY_STRING) 24221345Sdim return CLANG_REPOSITORY_STRING; 25221345Sdim#else 26218893Sdim#ifdef SVN_REPOSITORY 27218893Sdim llvm::StringRef URL(SVN_REPOSITORY); 28218893Sdim#else 29218893Sdim llvm::StringRef URL(""); 30218893Sdim#endif 31203955Srdivacky 32218893Sdim // If the SVN_REPOSITORY is empty, try to use the SVN keyword. This helps us 33218893Sdim // pick up a tag in an SVN export, for example. 34218893Sdim static llvm::StringRef SVNRepository("$URL: http://llvm.org/svn/llvm-project/cfe/trunk/lib/Basic/Version.cpp $"); 35218893Sdim if (URL.empty()) { 36218893Sdim URL = SVNRepository.slice(SVNRepository.find(':'), 37218893Sdim SVNRepository.find("/lib/Basic")); 38218893Sdim } 39203955Srdivacky 40208600Srdivacky // Strip off version from a build from an integration branch. 41218893Sdim URL = URL.slice(0, URL.find("/src/tools/clang")); 42203955Srdivacky 43218893Sdim // Trim path prefix off, assuming path came from standard cfe path. 44218893Sdim size_t Start = URL.find("cfe/"); 45218893Sdim if (Start != llvm::StringRef::npos) 46218893Sdim URL = URL.substr(Start + 4); 47203955Srdivacky 48218893Sdim return URL; 49221345Sdim#endif 50198092Srdivacky} 51198092Srdivacky 52203955Srdivackystd::string getClangRevision() { 53204643Srdivacky#ifdef SVN_REVISION 54218893Sdim return SVN_REVISION; 55218893Sdim#else 56218893Sdim return ""; 57204643Srdivacky#endif 58198092Srdivacky} 59198092Srdivacky 60203955Srdivackystd::string getClangFullRepositoryVersion() { 61203955Srdivacky std::string buf; 62203955Srdivacky llvm::raw_string_ostream OS(buf); 63218893Sdim std::string Path = getClangRepositoryPath(); 64218893Sdim std::string Revision = getClangRevision(); 65218893Sdim if (!Path.empty()) 66218893Sdim OS << Path; 67218893Sdim if (!Revision.empty()) { 68218893Sdim if (!Path.empty()) 69218893Sdim OS << ' '; 70218893Sdim OS << Revision; 71218893Sdim } 72204793Srdivacky return OS.str(); 73202879Srdivacky} 74202879Srdivacky 75203955Srdivackystd::string getClangFullVersion() { 76203955Srdivacky std::string buf; 77203955Srdivacky llvm::raw_string_ostream OS(buf); 78202879Srdivacky#ifdef CLANG_VENDOR 79203955Srdivacky OS << CLANG_VENDOR; 80202879Srdivacky#endif 81203955Srdivacky OS << "clang version " CLANG_VERSION_STRING " (" 82203955Srdivacky << getClangFullRepositoryVersion() << ')'; 83218893Sdim 84209107Sed#ifdef CLANG_VENDOR_SUFFIX 85209107Sed OS << CLANG_VENDOR_SUFFIX; 86218893Sdim#elif defined(CLANG_VENDOR) 87218893Sdim // If vendor supplied, include the base LLVM version as well. 88218893Sdim OS << " (based on LLVM " << PACKAGE_VERSION << ")"; 89209107Sed#endif 90218893Sdim 91204793Srdivacky return OS.str(); 92202879Srdivacky} 93203955Srdivacky 94221345Sdimstd::string getClangFullCPPVersion() { 95221345Sdim // The version string we report in __VERSION__ is just a compacted version of 96221345Sdim // the one we report on the command line. 97221345Sdim std::string buf; 98221345Sdim llvm::raw_string_ostream OS(buf); 99221345Sdim#ifdef CLANG_VENDOR 100221345Sdim OS << CLANG_VENDOR; 101221345Sdim#endif 102221345Sdim OS << "Clang " CLANG_VERSION_STRING " (" 103221345Sdim << getClangFullRepositoryVersion() << ')'; 104221345Sdim return OS.str(); 105221345Sdim} 106221345Sdim 107198092Srdivacky} // end namespace clang 108