diff --git a/.gitignore b/.gitignore index 25ee47765..14aa9cddd 100644 --- a/.gitignore +++ b/.gitignore @@ -51,9 +51,8 @@ bin btop .*/ - -#do not ignore .github directory -!.github +# Don't ignore .github directory +!.github/ # Ignore files created by Qt Creator *.config @@ -64,3 +63,19 @@ btop *.cxxflags *.files *.includes + +# CMake +CMakeLists.txt.user +CMakeCache.txt +CMakeFiles +CMakeScripts +Testing +Makefile +cmake_install.cmake +install_manifest.txt +compile_commands.json +CTestTestfile.cmake +_deps + +# CLion +cmake-build-* diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 000000000..c755e929b --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,109 @@ +# SPDX-License-Identifier: Apache-2.0 +# +# CMake configuration file for btop +cmake_minimum_required(VERSION 3.12) + +# -O3 is not tested right now +set(CMAKE_CXX_FLAGS_RELEASE "-O2 -DNDEBUG" CACHE STRING INTERNAL) + +# Disable in-source builds since they would override the Makefile +if("${CMAKE_CURRENT_SOURCE_DIR}" STREQUAL "${CMAKE_CURRENT_BINARY_DIR}") + message(FATAL_ERROR "In-source build are disable to preserve btop's Makefile") +endif() + +project("btop" + VERSION 1.2.13 + DESCRIPTION "A monitor of resources" + HOMEPAGE_URL "https://github.com/aristocratos/btop" + LANGUAGES CXX +) + +set(CMAKE_COLOR_DIAGNOSTICS ON) +# When the build type is empty we can't properly fortify +if(CMAKE_BUILD_TYPE STREQUAL "") + set(CMAKE_BUILD_TYPE Release) + message(STATUS "Set build type to Release") +endif() + +add_executable(btop + src/btop.cpp + src/btop_config.cpp + src/btop_draw.cpp + src/btop_input.cpp + src/btop_menu.cpp + src/btop_shared.cpp + src/btop_theme.cpp + src/btop_tools.cpp +) + +# NOTE: Checks can be simplified with CMake 3.25 +if(CMAKE_SYSTEM_NAME STREQUAL "Darwin") + target_sources(btop PRIVATE + src/osx/btop_collect.cpp + src/osx/sensors.cpp + src/osx/smc.cpp + ) +elseif(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD") + target_sources(btop PRIVATE src/freebsd/btop_collect.cpp) +elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux") + target_sources(btop PRIVATE src/linux/btop_collect.cpp) +else() + message(FATAL_ERROR "${CMAKE_SYSTEM_NAME} is not supported") +endif() + +option(BTOP_STATIC "Link btop statically" OFF) +if(BTOP_STATIC) + target_compile_definitions(btop PRIVATE STATIC_BUILD) + target_link_options(btop PRIVATE -static) +endif() + +option(BTOP_STRIP "Strip executable" ON) +if(BTOP_STRIP AND CMAKE_BUILD_TYPE STREQUAL "Release") + target_link_options(btop PRIVATE -s) +endif() + +set_target_properties(btop PROPERTIES + CXX_STANDARD 20 + CXX_STANDARD_REQUIRED ON + CXX_EXTENSIONS OFF + INTERPROCEDURAL_OPTIMIZATION ON +) + +include(CheckCXXCompilerFlag) +check_cxx_compiler_flag(-fstack-protector CXX_HAS_FSTACK_PROTECTOR) +# TODO: enable more warnings in coordination with upstream +# TODO: filter out -fstack-clash-protection on arm chips +target_compile_options(btop PRIVATE -Wall -Wextra -Wpedantic + -ftree-vectorize -fstack-clash-protection -fcf-protection + $<$:-fstack-protector> +) + +target_compile_definitions(btop PRIVATE + _FILE_OFFSET_BITS=64 + + _GLIBCXX_ASSERTIONS _LIBCPP_ENABLE_ASSERTIONS=1 + # Only has an effect with optimizations enabled + $<$>:_FORTIFY_SOURCE=2> +) + +target_include_directories(btop SYSTEM PRIVATE include) + +set(THREADS_PREFER_PTHREAD_FLAG ON) +find_package(Threads REQUIRED) +target_link_libraries(btop PRIVATE Threads::Threads) + +if(CMAKE_SYSTEM_NAME STREQUAL "Darwin") + target_link_libraries(btop PRIVATE $ + + + +### With Make + + 1. **Install dependencies (example for Ubuntu 21.04 Hirsute)** @@ -397,6 +403,76 @@ Also needs a UTF8 locale and a font that covers: make help ``` + + +
+ + + +### With CMake (Community maintained) + + + +1. **Install build dependencies** + + Requires Clang / GCC, CMake, Ninja and Git + + For example, with Debian Bookworm: + + ```bash + sudo apt install cmake git g++ ninja-build + ``` + +2. **Clone the repository** + + ```bash + git clone https://github.com/aristocratos/btop.git && cd btop + `````` + +3. **Compile** + + ```bash + # Configure + cmake -B build -G Ninja + # Build + cmake --build build + ``` + + This will automatically build a stripped release version of btop. + + Some useful options to pass to the configure step: + + | Configure flag | Description | + |---------------------------------|-------------------------------------------------------------------------| + | `-DCMAKE_INSTALL_PREFIX=` | The installation prefix ('/usr/local' by default) | + | `-DBTOP_STATIC=` | Enables static linking (OFF by default) | + | `-DBTOP_STRIP=` | Strips symbols from the binary (ON by default for Release builds) | + + To force a compiler, run `CXX= cmake -B build -G Ninja` + +4. **Install** + + ```bash + cmake --build build -t install + ``` + + May require root priviliges + +5. **Uninstall** + + CMake doesn't generate an uninstall target by default. To remove installed files, run + ``` + cat build/install_manifest.txt | xargs rm -irv + ``` + +6. **Cleanup build directory** + + ```bash + cmake --build build -t clean + ``` + +
+ ## Compilation macOS OSX Needs GCC 10 or higher, (GCC 11 or above strongly recommended for better CPU efficiency in the compiled binary). @@ -493,6 +569,14 @@ Also needs a UTF8 locale and a font that covers: Note that GNU make (`gmake`) is required to compile on FreeBSD. +
+ + + +### With gmake + + + 1. **Install dependencies** ```bash @@ -573,6 +657,93 @@ Also needs a UTF8 locale and a font that covers: gmake help ``` +
+ +
+ + + +### With CMake (Community maintained) + + + +1. **Install build dependencies** + + Requires Clang / GCC, CMake, Ninja and Git + + _**Note:** since libc++ is part of FreeBSD's base system and is too old on FreeBSD 13 it cannot compile btop_ + + FreeBSD 14 and later: + ```bash + pkg install cmake ninja + ``` + + FreeBSD 13: + ```bash + pkg install cmake gcc13 ninja + ``` + +2. **Clone the repository** + + ```bash + git clone https://github.com/aristocratos/btop.git && cd btop + `````` + +3. **Compile** + + FreeBSD 14 and later: + ```bash + # Configure + cmake -B build -G Ninja + # Build + cmake --build build + ``` + + FreeBSD 13: + ```bash + # Configure + CXX=g++13 cmake -B build -G Ninja + # Build + cmake --build build + ``` + + This will automatically build a stripped release version of btop. + + Some useful options to pass to the configure step: + + | Configure flag | Description | + |---------------------------------|-------------------------------------------------------------------------| + | `-DCMAKE_INSTALL_PREFIX=` | The installation prefix ('/usr/local' by default) | + | `-DBTOP_STATIC=` | Enables static linking (OFF by default) | + | `-DBTOP_STRIP=` | Strips symbols from the binary (ON by default for Release builds) | + + _**Note:** Static linking currently does not work on FreeBSD._ + + To force a compiler, run `CXX= cmake -B build -G Ninja` + +4. **Install** + + ```bash + cmake --build build -t install + ``` + + May require root priviliges + +5. **Uninstall** + + CMake doesn't generate an uninstall target by default. To remove installed files, run + ``` + cat build/install_manifest.txt | xargs rm -irv + ``` + +6. **Cleanup build directory** + + ```bash + cmake --build build -t clean + ``` + +
+ ## Installing the snap [![btop](https://snapcraft.io/btop/badge.svg)](https://snapcraft.io/btop)