diff --git a/.gitignore b/.gitignore index 7e2ed8f..c31e456 100644 --- a/.gitignore +++ b/.gitignore @@ -49,6 +49,8 @@ stage/ build bin btop +/obj/ +config.h .*/ # Optional libraries diff --git a/CMakeLists.txt b/CMakeLists.txt index cd39946..04edbed 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -76,6 +76,21 @@ if(NOT CXX_HAVE_RANGES) message(FATAL_ERROR "The compiler doesn't support ") endif() +# Generate build info +execute_process( + COMMAND "git" "rev-parse" "--short" "HEAD" + WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" + OUTPUT_VARIABLE GIT_COMMIT + OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET) +set(CONFIGURE_COMMAND + "cmake -DBTOP_STATIC=${BTOP_STATIC} -DBTOP_USE_MOLD=${BTOP_USE_MOLD} -DBTOP_FORTIFY=${BTOP_FORTIFY} -DBTOP_GPU=${BTOP_GPU}" +) +get_filename_component(CXX_COMPILER_BASENAME "${CMAKE_CXX_COMPILER}" NAME) +set(COMPILER "${CXX_COMPILER_BASENAME}") +set(COMPILER_VERSION "${CMAKE_CXX_COMPILER_VERSION}") +configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/config.h.in ${CMAKE_CURRENT_BINARY_DIR}/config.h @ONLY IMMEDIATE) +set(CMAKE_INCLUDE_CURRENT_DIR ON) + # Check for and enable LTO check_ipo_supported(RESULT ipo_supported) if(ipo_supported AND BTOP_LTO) diff --git a/Makefile b/Makefile index ed80899..38ab1be 100644 --- a/Makefile +++ b/Makefile @@ -163,6 +163,12 @@ else LTO := $(THREADS) endif +GIT_COMMIT := $(shell git rev-parse --short HEAD 2> /dev/null || true) +CONFIGURE_COMMAND := $(MAKE) STATIC=$(STATIC) FORTIFY_SOURCE=$(FORTIFY_SOURCE) +ifeq ($(PLATFORM_LC),linux) + CONFIGURE_COMMAND += GPU_SUPPORT=$(GPU_SUPPORT) RSMI_STATIC=$(RSMI_STATIC) +endif + #? The Directories, Source, Includes, Objects and Binary SRCDIR := src INCDIRS := include $(wildcard lib/**/include) @@ -182,7 +188,7 @@ OPTFLAGS := -O2 -ftree-vectorize -flto=$(LTO) LDCXXFLAGS := -pthread -D_GLIBCXX_ASSERTIONS -D_FILE_OFFSET_BITS=64 $(GOODFLAGS) $(ADDFLAGS) override CXXFLAGS += $(REQFLAGS) $(LDCXXFLAGS) $(OPTFLAGS) $(WARNFLAGS) override LDFLAGS += $(LDCXXFLAGS) $(OPTFLAGS) $(WARNFLAGS) -INC := $(foreach incdir,$(INCDIRS),-isystem $(incdir)) -I$(SRCDIR) +INC := $(foreach incdir,$(INCDIRS),-isystem $(incdir)) -I$(SRCDIR) -I$(BUILDDIR) SU_USER := root ifdef DEBUG @@ -222,7 +228,7 @@ endif #? Default Make .ONESHELL: -all: | info rocm_smi info-quiet directories btop +all: | info rocm_smi info-quiet directories config.h btop ifneq ($(QUIET),true) info: @@ -267,6 +273,13 @@ directories: @$(VERBOSE) || printf "mkdir -p $(BUILDDIR)/$(PLATFORM_DIR)\n" @mkdir -p $(BUILDDIR)/$(PLATFORM_DIR) +config.h: $(BUILDDIR)/config.h + +$(BUILDDIR)/config.h: $(SRCDIR)/config.h.in | directories + @$(QUIET) || printf "\033[1mConfiguring $(BUILDDIR)/config.h\033[0m\n" + @$(VERBOSE) || printf 'sed -e "s|@GIT_COMMIT@|$(GIT_COMMIT)|" -e "s|@CONFIGURE_COMMAND@|$(CONFIGURE_COMMAND)|" -e "s|@COMPILER@|$(CXX)|" -e "s|@COMPILER_VERSION@|$(CXX_VERSION)|" $< | tee $@ > /dev/null\n' + @sed -e "s|@GIT_COMMIT@|$(GIT_COMMIT)|" -e "s|@CONFIGURE_COMMAND@|$(CONFIGURE_COMMAND)|" -e "s|@COMPILER@|$(CXX)|" -e "s|@COMPILER_VERSION@|$(CXX_VERSION)|" $< | tee $@ > /dev/null + #? Clean only Objects clean: @printf "\033[1;91mRemoving: \033[1;97mbuilt objects...\033[0m\n" @@ -362,7 +375,7 @@ btop: $(OBJECTS) | rocm_smi directories #? Compile .ONESHELL: -$(BUILDDIR)/%.$(OBJEXT): $(SRCDIR)/%.$(SRCEXT) | rocm_smi directories +$(BUILDDIR)/%.$(OBJEXT): $(SRCDIR)/%.$(SRCEXT) | rocm_smi directories config.h @sleep 0.3 2>/dev/null || true @TSTAMP=$$(date +%s 2>/dev/null || echo "0") @$(QUIET) || printf "\033[1;97mCompiling $<\033[0m\n" @@ -371,4 +384,4 @@ $(BUILDDIR)/%.$(OBJEXT): $(SRCDIR)/%.$(SRCEXT) | rocm_smi directories @printf "\033[1;92m$$($(PROGRESS))$(P)\033[10D\033[5C-> \033[1;37m$@ \033[100D\033[38C\033[1;93m(\033[1;97m$$(du -ah $@ | cut -f1)iB\033[1;93m) \033[92m(\033[97m$$($(DATE_CMD) -d @$$(expr $$($(DATE_CMD) +%s 2>/dev/null || echo "0") - $${TSTAMP} 2>/dev/null) -u +%Mm:%Ss 2>/dev/null | sed 's/^00m://' || echo '')\033[92m)\033[0m\n" #? Non-File Targets -.PHONY: all msg help pre +.PHONY: all config.h msg help pre diff --git a/src/btop.cpp b/src/btop.cpp index 736854f..caf2ba5 100644 --- a/src/btop.cpp +++ b/src/btop.cpp @@ -52,6 +52,7 @@ tab-size = 4 #include "btop_theme.hpp" #include "btop_draw.hpp" #include "btop_menu.hpp" +#include "config.h" #include "fmt/core.h" using std::atomic; @@ -115,6 +116,19 @@ namespace Global { int arg_update = 0; } +static void print_version() { + if constexpr (GIT_COMMIT.empty()) { + fmt::print("btop version: {}\n", Global::Version); + } else { + fmt::print("btop version: {}+{}\n", Global::Version, GIT_COMMIT); + } +} + +static void print_version_with_build_info() { + print_version(); + fmt::print("Compiled with: {} ({})\nConfigured with: {}\n", COMPILER, COMPILER_VERSION, CONFIGURE_COMMAND); +} + //* A simple argument parser void argumentParser(const int argc, char **argv) { for(int i = 1; i < argc; i++) { @@ -136,8 +150,12 @@ void argumentParser(const int argc, char **argv) { ); exit(0); } - else if (is_in(argument, "-v", "--version")) { - fmt::println("btop version: {}", Global::Version); + else if (is_in(argument, "-v")) { + print_version(); + exit(0); + } + else if (is_in(argument, "--version")) { + print_version_with_build_info(); exit(0); } else if (is_in(argument, "-lc", "--low-color")) { diff --git a/src/config.h.in b/src/config.h.in new file mode 100644 index 0000000..f4a6ed3 --- /dev/null +++ b/src/config.h.in @@ -0,0 +1,10 @@ +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include + +constexpr std::string_view GIT_COMMIT = "@GIT_COMMIT@"; +constexpr std::string_view COMPILER = "@COMPILER@"; +constexpr std::string_view COMPILER_VERSION = "@COMPILER_VERSION@"; +constexpr std::string_view CONFIGURE_COMMAND = "@CONFIGURE_COMMAND@";