From ed0fa34a9d628b21a1abed6034ab866c4db72445 Mon Sep 17 00:00:00 2001 From: Steffen Winter Date: Sun, 26 Nov 2023 19:39:29 +0100 Subject: [PATCH 1/7] Bump required CMake version --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d11a250..4765902 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,7 +3,7 @@ # CMake configuration for btop # -cmake_minimum_required(VERSION 3.12) +cmake_minimum_required(VERSION 3.20) # Disable in-source builds since they would override the Makefile if("${CMAKE_CURRENT_SOURCE_DIR}" STREQUAL "${CMAKE_CURRENT_BINARY_DIR}") From ebbb769a6aaf0a6245111eb55f239d5c45be0cb9 Mon Sep 17 00:00:00 2001 From: Steffen Winter Date: Sun, 26 Nov 2023 21:39:13 +0100 Subject: [PATCH 2/7] Move calls to find_package to where they're required --- CMakeLists.txt | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4765902..862b66e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,14 +43,6 @@ if(BTOP_STATIC) set(CMAKE_FIND_LIBRARY_SUFFIXES ".a") endif() -if(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD") - find_package(devstat REQUIRED) - find_package(kvm REQUIRED) - if(BTOP_STATIC) - find_package(elf REQUIRED) - endif() -endif() - include(CheckCXXCompilerFlag) include(CheckIPOSupported) @@ -153,8 +145,11 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Darwin") target_link_libraries(btop PRIVATE $ Date: Sun, 26 Nov 2023 21:40:29 +0100 Subject: [PATCH 3/7] Add check for header --- CMakeLists.txt | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 862b66e..44ae888 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -44,8 +44,14 @@ if(BTOP_STATIC) endif() include(CheckCXXCompilerFlag) +include(CheckIncludeFileCXX) include(CheckIPOSupported) +check_include_file_cxx(ranges CXX_HAS_RANGES) +if(NOT CXX_HAS_RANGES) + message(FATAL_ERROR "The compiler doesn't support ") +endif() + add_executable(btop src/btop.cpp src/btop_config.cpp From 2f59e61d875b86cb99d9d24819dea607239ad517 Mon Sep 17 00:00:00 2001 From: Steffen Winter Date: Sun, 26 Nov 2023 22:56:58 +0100 Subject: [PATCH 4/7] Add GPU options for cmake based builds --- CMakeLists.txt | 33 +++++++++++++++++++++++++++++++++ README.md | 4 +++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 44ae888..ce4891d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -32,11 +32,14 @@ set(CMAKE_COLOR_DIAGNOSTICS ON) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # Options +include(CMakeDependentOption) option(BTOP_STATIC "Link btop statically" OFF) option(BTOP_LTO "Enable LTO" ON) option(BTOP_USE_MOLD "Use mold to link btop" OFF) option(BTOP_PEDANTIC "Enable a bunch of additional warnings" OFF) option(BTOP_WERROR "Compile with warnings as errors" OFF) +option(BTOP_GPU "Enable GPU support" ON) +cmake_dependent_option(BTOP_RSMI_STATIC "Link statically to ROCm SMI" OFF "BTOP_GPU" OFF) if(BTOP_STATIC) # Set this before calling find_package @@ -130,6 +133,36 @@ target_compile_definitions(btop PRIVATE $<$>:_FORTIFY_SOURCE=2> ) +# Enable GPU support +if(CMAKE_SYSTEM_NAME STREQUAL "Linux" AND BTOP_GPU) + target_compile_definitions(btop PRIVATE GPU_SUPPORT) + + if(BTOP_RSMI_STATIC) + # ROCm doesn't properly add it's folders to the module path + # if `CMAKE_MODULE_PATH` is already set + # We could also manully append ROCm's path here + set(_CMAKE_MODULE_PATH CMAKE_MODULE_PATH) + unset(CMAKE_MODULE_PATH) + + # NOTE: This might be problematic in the future if other sub projects + # depend on this or if btop starts producing libraries + # Build a static ROCm library + set(BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE) + + add_subdirectory(lib/rocm_smi_lib EXCLUDE_FROM_ALL) + + add_library(ROCm INTERFACE) + # Export ROCm's properties to a CMake target (which should've been done by ROCm :-/) + target_compile_definitions(ROCm INTERFACE RSMI_STATIC) + target_include_directories(ROCm INTERFACE lib/rocm_smi_lib/include) + target_link_libraries(ROCm INTERFACE rocm_smi64) + + set(CMAKE_MODULE_PATH _CMAKE_MODULE_PATH) + + target_link_libraries(btop PRIVATE ROCm) + endif() +endif() + target_include_directories(btop SYSTEM PRIVATE include) # mold diff --git a/README.md b/README.md index 140347a..4ee8474 100644 --- a/README.md +++ b/README.md @@ -340,7 +340,7 @@ Also needs a UTF8 locale and a font that covers: For x86_64 Linux the flag `GPU_SUPPORT` is automatically set to `true`, to manually disable gpu support set the flag to false, like: - `make GPU_SUPPORT=false` + `make GPU_SUPPORT=false` (or `cmake -DBTOP_GPU=false` with CMake) * **NVIDIA** @@ -503,6 +503,8 @@ Also needs a UTF8 locale and a font that covers: | `-DBTOP_USE_MOLD=` | Use mold to link btop (OFF by default) | | `-DBTOP_PEDANTIC=` | Compile with additional warnings (OFF by default) | | `-DBTOP_WERROR=` | Compile with warnings as errors (OFF by default) | + | `-DBTOP_GPU=` | Enable GPU support (ON by default) | + | `-DBTOP_RSMI_STATIC=` | Build and link the ROCm SMI library statically (OFF by default) | | `-DCMAKE_INSTALL_PREFIX=` | The installation prefix ('/usr/local' by default) | To force a compiler, run `CXX= cmake -B build -G Ninja` From 831be262b0a88090bde36eee0dcac8e8abb7569b Mon Sep 17 00:00:00 2001 From: Steffen Winter Date: Mon, 27 Nov 2023 00:29:14 +0100 Subject: [PATCH 5/7] Remove ROCm object files with `make clean/distclean` --- Makefile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Makefile b/Makefile index d4d44bc..89c5a33 100644 --- a/Makefile +++ b/Makefile @@ -274,11 +274,13 @@ directories: clean: @printf "\033[1;91mRemoving: \033[1;97mbuilt objects...\033[0m\n" @rm -rf $(BUILDDIR) + @cmake --build lib/rocm_smi_lib/build --target clean &> /dev/null || true #? Clean Objects and Binaries distclean: clean @printf "\033[1;91mRemoving: \033[1;97mbuilt binaries...\033[0m\n" @rm -rf $(TARGETDIR) + @rm -rf lib/rocm_smi_lib/build install: @printf "\033[1;92mInstalling binary to: \033[1;97m$(DESTDIR)$(PREFIX)/bin/btop\n" From 0585bc9cfbcb023ad10b57cbf52cc90623e6d307 Mon Sep 17 00:00:00 2001 From: Steffen Winter Date: Mon, 27 Nov 2023 00:31:10 +0100 Subject: [PATCH 6/7] Suppress all output from ROCm build Similar to including external include files with `-isystem`, ignore output from ROCm build since these warnings aren't a concern here --- Makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 89c5a33..f449606 100644 --- a/Makefile +++ b/Makefile @@ -218,7 +218,8 @@ endif P := %% ifeq ($(VERBOSE),true) - override SUPPRESS := 1>/dev/null + # Doesn't work with `&>` + override SUPPRESS := > /dev/null 2> /dev/null else override SUPPRESS := endif From 6f12e3555d9558266e5ffc7f6847f0a7273db5b8 Mon Sep 17 00:00:00 2001 From: Steffen Winter Date: Mon, 27 Nov 2023 00:33:11 +0100 Subject: [PATCH 7/7] Properly invoke CMake to build ROCm * Build an optimized library by default * Only build the library target * ROCm is build with debug symbols when `make DEBUG=true` * Enable LTO * Use the more generic CMake build command instead of calling make directly, this always uses all cores by default and makes it easier to switch to another generator e.g. Ninja * Use a variable to store the ROCm source directory. The directory can be changed with `make ROCM_DIR=` * The static library is now directly linked by CMake and not created off of the object files from a shared library build * The C++ compiler used to compile btop is now used to compile ROCm to avoid name mangling when `CXX` from the environment and `make CXX=` differ * CMake is invoked from btop's root directory --- Makefile | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index f449606..2adc8c2 100644 --- a/Makefile +++ b/Makefile @@ -329,20 +329,24 @@ uninstall: #? Compile rocm_smi ifeq ($(GPU_SUPPORT)$(RSMI_STATIC),truetrue) + ROCM_DIR ?= lib/rocm_smi_lib + ROCM_BUILD_DIR := $(ROCM_DIR)/build + ifeq ($(DEBUG),true) + BUILD_TYPE := Debug + else + BUILD_TYPE := Release + endif .ONESHELL: rocm_smi: @printf "\n\033[1;92mBuilding ROCm SMI static library\033[37m...\033[0m\n" @TSTAMP=$$(date +%s 2>/dev/null || echo "0") - @mkdir -p lib/rocm_smi_lib/build - @cd lib/rocm_smi_lib/build @$(QUIET) || printf "\033[1;97mRunning CMake...\033[0m\n" - @cmake .. $(SUPPRESS) || { printf "\033[1;91mCMake failed, continuing build without statically linking ROCm SMI\033[37m...\033[0m\n"; exit 0; } + CXX=$(CXX) cmake -S $(ROCM_DIR) -B $(ROCM_BUILD_DIR) -DCMAKE_BUILD_TYPE=$(BUILD_TYPE) -DCMAKE_POLICY_DEFAULT_CMP0069=NEW -DCMAKE_INTERPROCEDURAL_OPTIMIZATION=ON -DBUILD_SHARED_LIBS=OFF $(SUPPRESS) || { printf "\033[1;91mCMake failed, continuing build without statically linking ROCm SMI\033[37m...\033[0m\n"; exit 0; } @$(QUIET) || printf "\n\033[1;97mBuilding and linking...\033[0m\n" - @$(MAKE) $(SUPPRESS) || { printf "\033[1;91mMake failed, continuing build without statically linking ROCm SMI\033[37m...\033[0m\n"; exit 0; } - @ar -crs rocm_smi/librocm_smi64.a $$(find rocm_smi -name '*.o') $(SURPRESS) || { printf "\033[1;91mFailed to pack ROCm SMI into static library, continuing build without statically linking ROCm SMI\033[37m...\033[0m\n"; exit 0; } - @printf "\033[1;92m100$(P)\033[10D\033[5C-> \033[1;37mrocm_smi/librocm_smi64.a \033[100D\033[38C\033[1;93m(\033[1;97m$$(du -ah rocm_smi/librocm_smi64.a | cut -f1)iB\033[1;93m)\033[0m\n" + @cmake --build $(ROCM_BUILD_DIR) -j -t rocm_smi64 $(SUPPRESS) || { printf "\033[1;91mMake failed, continuing build without statically linking ROCm SMI\033[37m...\033[0m\n"; exit 0; } + @printf "\033[1;92m100$(P)\033[10D\033[5C-> \033[1;37m$(ROCM_BUILD_DIR)/rocm_smi/librocm_smi64.a \033[1;93m(\033[1;97m$$(du -ah $(ROCM_BUILD_DIR)/rocm_smi/librocm_smi64.a | cut -f1)iB\033[1;93m)\033[0m\n" @printf "\033[1;92mROCm SMI build complete in \033[92m(\033[97m$$($(DATE_CMD) -d @$$(expr $$(date +%s 2>/dev/null || echo "0") - $(TIMESTAMP) 2>/dev/null) -u +%Mm:%Ss 2>/dev/null | sed 's/^00m://' || echo "unknown")\033[92m)\033[0m\n" - @$(eval override LDFLAGS += lib/rocm_smi_lib/build/rocm_smi/librocm_smi64.a -DRSMI_STATIC) # TODO: this seems to execute every time, no matter if the compilation failed or succeeded + @$(eval override LDFLAGS += $(ROCM_BUILD_DIR)/rocm_smi/librocm_smi64.a -DRSMI_STATIC) # TODO: this seems to execute every time, no matter if the compilation failed or succeeded @$(eval override CXXFLAGS += -DRSMI_STATIC) else rocm_smi: