Skip to content

Commit

Permalink
Split release vs debug into cmake presets
Browse files Browse the repository at this point in the history
  • Loading branch information
zakaria1193 authored and zakaria-fadli-netatmo committed Nov 10, 2024
1 parent b558b91 commit b66c4a4
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 79 deletions.
40 changes: 30 additions & 10 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,48 @@ jobs:
install:
runs-on: ubuntu-latest

strategy:
matrix:
build_type: [Debug, Release]

steps:
- uses: actions/checkout@v3

- name: Install pre-requisits
run: sudo ./first_setup.sh

build_debug:
runs-on: ubuntu-latest

needs: install

steps:
- uses: actions/checkout@v3

- name: Configure CMake
# Configure CMake in a 'build' subdirectory.
run: cmake -B ${{github.workspace}}/build -G 'Ninja Multi-Config'
run: cmake --preset debug

- name: Build
# Build your program with the given configuration
run: cmake --build ${{github.workspace}}/build --config ${{ matrix.build_type }}
run: cmake --build --preset debug

- name: clang-tidy
run: cmake --build ${{github.workspace}}/build --config ${{ matrix.build_type }} -t clang-tidy
run: cmake --build --preset debug -t clang-tidy

- name: cppcheck
run: cmake --build ${{github.workspace}}/build --config ${{ matrix.build_type }} -t cppcheck
run: cmake --build --preset debug -t cppcheck

build_release:
runs-on: ubuntu-latest

needs: install

steps:
- uses: actions/checkout@v3

- name: Configure CMake
run: cmake --preset release

- name: Build
run: cmake --build --preset release

- name: clang-tidy
run: cmake --build --preset release -t clang-tidy

- name: cppcheck
run: cmake --build --preset release -t cppcheck
23 changes: 0 additions & 23 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,26 +1,3 @@
############################################################
# Build types definitions
############################################################
get_property(isMultiConfig GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)

if(isMultiConfig)
# Necessary for multi-configuration generators such as Ninja Multi-Config
set(CMAKE_CONFIGURATION_TYPES "Debug;Release")
set(CMAKE_DEFAULT_BUILD_TYPE "Debug")
message(STATUS "Multi-configuration generator used, append --config <Debug|Release> "
"to build the desired configuration.")

else()
# Only relevant for Makefile/Ninja generators (And other single-configuration generators)
# Manually set the default build type to Debug
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Debug)
message(STATUS "Setting build type to 'Debug' as none was specified.")
endif()
# Print the build type and flags
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
endif()

############################################################
# Set compiler, language and flags
# Keep before project() call
Expand Down
22 changes: 22 additions & 0 deletions CMakePresets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"version": 3,
"cmakeMinimumRequired": {
"major": 3,
"minor": 19
},
"configurePresets": [
{
"name": "default",
"hidden": false,
"generator": "Ninja Multi-Config",
"binaryDir": "${sourceDir}/build"
}
],
"buildPresets": [
{
"name": "default-build",
"configurePreset": "default",
"verbose": true
}
]
}
64 changes: 25 additions & 39 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,49 +1,35 @@
BUILD_DIR :=./build
GENERATOR :='Ninja Multi-Config'
.PHONY: gdb run build-release build-debug

configure:
cmake --fresh -B ${BUILD_DIR} -G ${GENERATOR}

RELEASE ?= n
ifeq (${RELEASE},y)
CONFIG=Release
else
CONFIG=Debug
endif
BUILD_DIR := ./build

DRY_RUN ?= n
ifeq (${DRY_RUN},y)
DRY_RUN_FLAG=-- -n
$(info Dry run)
else
DRY_RUN_FLAG=
endif
# Set flags based on variables
DRY_RUN_FLAG := $(if $(filter y,$(DRY_RUN)),-- -n)
VERBOSE_FLAG := $(if $(filter y,$(VERBOSE)),--verbose)

VERBOSE ?= n
ifeq (${VERBOSE},y)
VERBOSE_FLAG=--verbose
$(info Verbose)
else
VERBOSE_FLAG=
endif
# Display settings
$(info Dry run = $(DRY_RUN))
$(info Verbose = $(VERBOSE))

$(info verbose = ${VERBOSE})

build_$(CONFIG):
$(info Build Config = ${CONFIG})
cmake --build ${BUILD_DIR} --config ${CONFIG} ${VERBOSE_FLAG} ${DRY_RUN_FLAG}
# Configure target using a single preset
configure:
cmake --preset default

build: build_$(CONFIG)
clean:
rm build -rf

%:
cmake --build ${BUILD_DIR} -t $@ ${VERBOSE_FLAG} ${DRY_RUN_FLAG}
# Separate build targets for Debug and Release
build-debug:
cmake --build --preset default-build --config Debug $(VERBOSE_FLAG) $(DRY_RUN_FLAG)

OUTPUT_BIN := ${BUILD_DIR}/$(CONFIG)/mockbee_test_bin
build-release:
cmake --build --preset default-build --config Release $(VERBOSE_FLAG) $(DRY_RUN_FLAG)

run: build_$(CONFIG)
${OUTPUT_BIN}
# Run target in Debug configuration by default
OUTPUT_BIN := $(BUILD_DIR)/Debug/my_template_binary

gdb: build_$(CONFIG)
gdb-multiarch ${OUTPUT_BIN} -ex "run"
run: build-debug
$(OUTPUT_BIN)

.PHONY: build build_$(CONFIG) configure run
# Debug with GDB in Debug configuration
gdb: build-debug
gdb-multiarch $(OUTPUT_BIN) -ex "run"
26 changes: 19 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@

[ Describe your project here ]

### Prerequisites

- CMake 3.10 or later
- C++ compiler with C++23 support (Recommended clang-17, CI is running clang-20)
### Dependencies

### Code quality checks
#### Build system
- CMake 3.19 or later (to be able to use CMakePresets)
- C++ compiler with C++23 support (Recommended clang-17+, The CI uses clang-20)
- ninja-build

#### Static analyzes
- libsanitizer
- clang-tidy
- cppcheck

#### Formatter
- clang-format

### CI Test

[![CMake](https://github.com/zakaria1193/my_cpp_boilerplate/actions/workflows/cmake.yml/badge.svg)](https://github.com/zakaria1193/my_cpp_boilerplatmy_cpp_boilerplate/actions/workflows/cmake.yml)
[![Build & Check](https://github.com/zakaria1193/my_cpp_boilerplate/actions/workflows/build.yml/badge.svg)](https://github.com/zakaria1193/my_cpp_boilerplate/actions/workflows/build.yml)

### Documentation

[ Add your documentation here ]


### CMake usage of Presets

- This boilerplate uses the presets feature (CMake 3.19), but we still keep the presets to minimal (KISS principle), How?
- Only one config preset
- Multiple build presets (release, debug...)

This means we require a *multi-configuration* build system to support both Debug and Release builds in a *single* configuration step. It is designed to work with generators like _Ninja Multi-Config_ (Available on all Linux systems) or _Visual Studio_, _XCode_... Which allow specifying build configurations (Debug, Release, etc.) at build time.
*Single-configuration* generators, such as *Makefiles*, are not supported, as they require separate build directories for each configuration. If you really need that, edit the configurePresets section of the CMakePresets.json file. Copy paste this to Chatgpt if it's not clear.

0 comments on commit b66c4a4

Please sign in to comment.