Skip to content

Commit

Permalink
Merge pull request aristocratos#671 from imwints/cmake-gpu
Browse files Browse the repository at this point in the history
Bring GPU support to CMake and improve how Make handles the ROCm library build
  • Loading branch information
aristocratos committed Nov 27, 2023
2 parents 2b0cc37 + 6f12e35 commit d1384c9
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 18 deletions.
52 changes: 43 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down Expand Up @@ -32,28 +32,29 @@ 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
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(CheckIncludeFileCXX)
include(CheckIPOSupported)

check_include_file_cxx(ranges CXX_HAS_RANGES)
if(NOT CXX_HAS_RANGES)
message(FATAL_ERROR "The compiler doesn't support <ranges>")
endif()

add_executable(btop
src/btop.cpp
src/btop_config.cpp
Expand Down Expand Up @@ -132,6 +133,36 @@ target_compile_definitions(btop PRIVATE
$<$<NOT:$<CONFIG:Debug>>:_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
Expand All @@ -153,8 +184,11 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
target_link_libraries(btop PRIVATE $<LINK_LIBRARY:FRAMEWORK,CoreFoundation)
target_link_libraries(btop PRIVATE $<LINK_LIBRARY:FRAMEWORK,IOKit)
elseif(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
find_package(devstat REQUIRED)
find_package(kvm REQUIRED)
target_link_libraries(btop PRIVATE devstat::devstat kvm::kvm)
if(BTOP_STATIC)
find_package(elf REQUIRED)
target_link_libraries(btop PRIVATE elf::elf)
endif()
endif()
Expand Down
23 changes: 15 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -274,11 +275,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"
Expand Down Expand Up @@ -326,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) ||