Index: CMakeLists.txt =================================================================== --- CMakeLists.txt +++ CMakeLists.txt @@ -81,6 +81,49 @@ set(LLD_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}) set(LLD_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}) +# Compute the LLD version from the LLVM version. +string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" LLD_VERSION + ${PACKAGE_VERSION}) +message(STATUS "LLD version: ${LLD_VERSION}") + +string(REGEX REPLACE "([0-9]+)\\.[0-9]+(\\.[0-9]+)?" "\\1" LLD_VERSION_MAJOR + ${LLD_VERSION}) +string(REGEX REPLACE "[0-9]+\\.([0-9]+)(\\.[0-9]+)?" "\\1" LLD_VERSION_MINOR + ${LLD_VERSION}) + +# Determine LLD revision and repository. +# TODO: Figure out a way to get the revision and the repository on windows. +if ( NOT CMAKE_SYSTEM_NAME MATCHES "Windows" ) + execute_process(COMMAND ${CMAKE_SOURCE_DIR}/utils/GetSourceVersion ${LLD_SOURCE_DIR} + OUTPUT_VARIABLE LLD_REVISION) + + execute_process(COMMAND ${CMAKE_SOURCE_DIR}/utils/GetRepositoryPath ${LLD_SOURCE_DIR} + OUTPUT_VARIABLE LLD_REPOSITORY) + if ( LLD_REPOSITORY ) + # Replace newline characters with spaces + string(REGEX REPLACE "(\r?\n)+" " " LLD_REPOSITORY ${LLD_REPOSITORY}) + # Remove leading spaces + STRING(REGEX REPLACE "^[ \t\r\n]+" "" LLD_REPOSITORY "${LLD_REPOSITORY}" ) + # Remove trailing spaces + string(REGEX REPLACE "(\ )+$" "" LLD_REPOSITORY ${LLD_REPOSITORY}) + endif() + + if ( LLD_REVISION ) + # Replace newline characters with spaces + string(REGEX REPLACE "(\r?\n)+" " " LLD_REVISION ${LLD_REVISION}) + # Remove leading spaces + STRING(REGEX REPLACE "^[ \t\r\n]+" "" LLD_REVISION "${LLD_REVISION}" ) + # Remove trailing spaces + string(REGEX REPLACE "(\ )+$" "" LLD_REVISION ${LLD_REVISION}) + endif() +endif () + +# Configure the Version.inc file. +configure_file( + ${CMAKE_CURRENT_SOURCE_DIR}/include/lld/Config/Version.inc.in + ${CMAKE_CURRENT_BINARY_DIR}/include/lld/Config/Version.inc) + + if (CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR) message(FATAL_ERROR "In-source builds are not allowed. CMake would overwrite " "the makefiles distributed with LLVM. Please create a directory and run cmake " Index: include/lld/Config/Makefile =================================================================== --- /dev/null +++ include/lld/Config/Makefile @@ -0,0 +1,32 @@ +LLD_LEVEL := ../../.. + +BUILT_SOURCES = Version.inc + +TABLEGEN_INC_FILES_COMMON = 1 + +include $(LLD_LEVEL)/Makefile + +# Compute the lld version from the LLVM version, unless specified explicitly. +ifndef LLD_VERSION +LLD_VERSION := $(subst svn,,$(LLVMVersion)) +LLD_VERSION := $(subst rc,,$(LLD_VERSION)) +endif + +LLD_VERSION_COMPONENTS := $(subst ., ,$(LLD_VERSION)) +LLD_VERSION_MAJOR := $(word 1,$(LLD_VERSION_COMPONENTS)) +LLD_VERSION_MINOR := $(word 2,$(LLD_VERSION_COMPONENTS)) + +LLD_REVISION := $(strip \ + $(shell $(LLVM_SRC_ROOT)/utils/GetSourceVersion $(LLVM_SRC_ROOT)/tools/lld)) + +LLD_REPOSITORY := $(strip \ + $(shell $(LLVM_SRC_ROOT)/utils/GetRepositoryPath $(LLVM_SRC_ROOT)/tools/lld)) + +$(ObjDir)/Version.inc.tmp : Version.inc.in Makefile $(LLVM_OBJ_ROOT)/Makefile.config $(ObjDir)/.dir + $(Echo) "Updating LLD version info." + $(Verb)sed -e "s#@LLD_VERSION@#$(LLD_VERSION)#g" \ + -e "s#@LLD_VERSION_MAJOR@#$(LLD_VERSION_MAJOR)#g" \ + -e "s#@LLD_VERSION_MINOR@#$(LLD_VERSION_MINOR)#g" \ + -e "s#@LLD_REVISION@#$(LLD_REVISION)#g" \ + -e "s#@LLD_REPOSITORY@#$(LLD_REPOSITORY)#g" \ + $< > $@ Index: include/lld/Config/Version.h =================================================================== --- /dev/null +++ include/lld/Config/Version.h @@ -0,0 +1,51 @@ +//===- lld/Config/Version.h - LLD Version Number ----------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +/// +/// \file +/// \brief Defines version macros and version-related utility functions +/// for lld. +/// +//===----------------------------------------------------------------------===// + +#ifndef LLD_VERSION_H +#define LLD_VERSION_H + +#include "lld/Config/Version.inc" +#include "llvm/ADT/StringRef.h" +#include + +/// \brief Helper macro for LLD_VERSION_STRING. +#define LLD_MAKE_VERSION_STRING2(X) #X + +/// \brief Helper macro for LLD_VERSION_STRING. +#define LLD_MAKE_VERSION_STRING(X, Y) LLD_MAKE_VERSION_STRING2(X.Y) + +/// \brief A string that describes the lld version number, e.g., "1.0". +#define LLD_VERSION_STRING \ + LLD_MAKE_VERSION_STRING(LLD_VERSION_MAJOR, LLD_VERSION_MINOR) + +namespace lld { +/// \brief Retrieves the repository path (e.g., Subversion path) that +/// identifies the particular lld branch, tag, or trunk from which this +/// lld was built. +llvm::StringRef getLLDRepositoryPath(); + +/// \brief Retrieves the repository revision number (or identifer) from which +/// this lld was built. +llvm::StringRef getLLDRevision(); + +/// \brief Retrieves the full repository version that is an amalgamation of +/// the information in getLLDRepositoryPath() and getLLDRevision(). +std::string getLLDRepositoryVersion(); + +/// \brief Retrieves a string representing the complete lld version. +llvm::StringRef getLLDVersion(); +} + +#endif // LLD_VERSION_H Index: include/lld/Config/Version.inc.in =================================================================== --- /dev/null +++ include/lld/Config/Version.inc.in @@ -0,0 +1,5 @@ +#define LLD_VERSION @LLD_VERSION@ +#define LLD_VERSION_MAJOR @LLD_VERSION_MAJOR@ +#define LLD_VERSION_MINOR @LLD_VERSION_MINOR@ +#define LLD_REVISION_STRING "@LLD_REVISION@" +#define LLD_REPOSITORY_STRING "@LLD_REPOSITORY@" Index: include/lld/Makefile =================================================================== --- include/lld/Makefile +++ include/lld/Makefile @@ -1,5 +1,5 @@ LLD_LEVEL := ../.. -DIRS := +DIRS := Config include $(LLD_LEVEL)/Makefile Index: lib/CMakeLists.txt =================================================================== --- lib/CMakeLists.txt +++ lib/CMakeLists.txt @@ -1,3 +1,4 @@ +add_subdirectory(Config) add_subdirectory(Core) add_subdirectory(Driver) add_subdirectory(Passes) Index: lib/Config/CMakeLists.txt =================================================================== --- /dev/null +++ lib/Config/CMakeLists.txt @@ -0,0 +1,3 @@ +add_lld_library(lldConfig + Version.cpp + ) Index: lib/Config/Makefile =================================================================== --- lib/Config/Makefile +++ lib/Config/Makefile @@ -1,4 +1,4 @@ -##===- lib/Makefile ----------------------------------------*- Makefile -*-===## +##===- lld/lib/Config/Makefile -----------------------------*- Makefile -*-===## # # The LLVM Compiler Infrastructure # @@ -6,11 +6,8 @@ # License. See LICENSE.TXT for details. # ##===----------------------------------------------------------------------===## -LLD_LEVEL := .. -# ARCMigrate and Rewrite are always needed because of libclang. -PARALLEL_DIRS = Core Driver Passes ReaderWriter - -include $(LLD_LEVEL)/../../Makefile.config +LLD_LEVEL := ../.. +LIBRARYNAME := lldConfig include $(LLD_LEVEL)/Makefile Index: lib/Config/Version.cpp =================================================================== --- /dev/null +++ lib/Config/Version.cpp @@ -0,0 +1,66 @@ +//===- Version.cpp - LLD Version Number --------------------------*- C++-=====// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file defines several version-related utility functions for LLD. +// +//===----------------------------------------------------------------------===// + +#include "lld/Config/Version.h" +#include "llvm/Support/raw_ostream.h" +#include +#include + +using namespace llvm; + +namespace lld { + +StringRef getLLDRepositoryPath() { +#ifdef LLD_REPOSITORY_STRING + return LLD_REPOSITORY_STRING; +#else + return ""; +#endif +} + +StringRef getLLDRevision() { +#ifdef LLD_REVISION_STRING + return LLD_REVISION_STRING; +#else + return ""; +#endif +} + +std::string getLLDRepositoryVersion() { + std::string buf; + llvm::raw_string_ostream OS(buf); + std::string Path = getLLDRepositoryPath(); + std::string Revision = getLLDRevision(); + if (!Path.empty() || !Revision.empty()) { + OS << '('; + if (!Path.empty()) + OS << Path; + if (!Revision.empty()) { + if (!Path.empty()) + OS << ' '; + OS << Revision; + } + OS << ')'; + } + return OS.str(); +} + +StringRef getLLDVersion() { +#ifdef LLD_VERSION_STRING + return LLD_VERSION_STRING; +#else + return ""; +#endif +} + +} // end namespace lld Index: lib/Driver/CMakeLists.txt =================================================================== --- lib/Driver/CMakeLists.txt +++ lib/Driver/CMakeLists.txt @@ -26,6 +26,7 @@ add_dependencies(lldDriver DriverOptionsTableGen) target_link_libraries(lldDriver + lldConfig lldPasses lldMachO lldPECOFF Index: lib/Driver/UniversalDriver.cpp =================================================================== --- lib/Driver/UniversalDriver.cpp +++ lib/Driver/UniversalDriver.cpp @@ -15,6 +15,7 @@ //===----------------------------------------------------------------------===// #include "lld/Driver/Driver.h" +#include "lld/Config/Version.h" #include "lld/Core/LLVM.h" @@ -188,6 +189,19 @@ return true; } + // Handle -version + if (parsedArgs->getLastArg(OPT_version)) { + diagnostics << "LLVM Linker Version: " << getLLDVersion() << "\n"; + return true; + } + + // Handle -repository-version + if (parsedArgs->getLastArg(OPT_repository_version)) { + diagnostics << "LLVM Linker Repository Version: " << getLLDVersion() + << getLLDRepositoryVersion() << "\n"; + return true; + } + Flavor flavor = getFlavor(argc, argv, parsedArgs); std::vector args(argv, argv + argc); Index: lib/Driver/UniversalDriverOptions.td =================================================================== --- lib/Driver/UniversalDriverOptions.td +++ lib/Driver/UniversalDriverOptions.td @@ -11,6 +11,12 @@ def target: Separate<["-"], "target">, HelpText<"Select the target">; +def version: Flag<["-"], "version">, + HelpText<"Display the version">; + +def repository_version: Flag<["-"], "repository-version">, + HelpText<"Display the repository version">; + // Help message def help : Flag<["-"], "help">, HelpText<"Display this help message">; Index: lib/Makefile =================================================================== --- lib/Makefile +++ lib/Makefile @@ -9,7 +9,7 @@ LLD_LEVEL := .. # ARCMigrate and Rewrite are always needed because of libclang. -PARALLEL_DIRS = Core Driver Passes ReaderWriter +PARALLEL_DIRS = Config Core Driver Passes ReaderWriter include $(LLD_LEVEL)/../../Makefile.config Index: tools/lld/Makefile =================================================================== --- tools/lld/Makefile +++ tools/lld/Makefile @@ -19,7 +19,7 @@ include $(LEVEL)/Makefile.config LINK_COMPONENTS := $(TARGETS_TO_BUILD) -USEDLIBS = lldDriver.a \ +USEDLIBS = lldDriver.a lldConfig.a \ lldELF.a lldMachO.a lldPasses.a lldPECOFF.a lldYAML.a \ lldReaderWriter.a lldCore.a lldNative.a \ lldHexagonELFTarget.a lldPPCELFTarget.a lldMipsELFTarget.a \ Index: unittests/DriverTests/Makefile =================================================================== --- unittests/DriverTests/Makefile +++ unittests/DriverTests/Makefile @@ -9,7 +9,7 @@ LLD_LEVEL = ../.. TESTNAME = DriverTests -USEDLIBS = lldDriver.a \ +USEDLIBS = lldDriver.a lldConfig.a \ lldELF.a lldMachO.a lldPasses.a lldPECOFF.a \ lldCore.a lldNative.a lldReaderWriter.a \ lldHexagonELFTarget.a lldPPCELFTarget.a lldMipsELFTarget.a \