Skip to content
This repository was archived by the owner on Apr 19, 2023. It is now read-only.

Commit ffb2784

Browse files
authored
Merge pull request #169 from aminya/cmakelib
feat: make the build system reusable and declarative + support Vcpkg + automatically setup MSVC environment
2 parents dfd6956 + 79023ff commit ffb2784

15 files changed

Lines changed: 92 additions & 615 deletions

.github/workflows/build_cmake.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ jobs:
2626

2727
runs-on: ${{ matrix.os }}
2828
strategy:
29+
fail-fast: false
2930
matrix:
3031
os: ['ubuntu-latest', 'windows-latest', 'macos-latest']
3132
include:
@@ -72,7 +73,7 @@ jobs:
7273
7374
- name: Configure CMake
7475
run: |
75-
cmake -S . -B ./build -DCMAKE_BUILD_TYPE:STRING=$BUILD_TYPE -DENABLE_COVERAGE:BOOL=${{ matrix.ENABLE_COVERAGE }}
76+
cmake -S . -B ./build -DCMAKE_BUILD_TYPE:STRING=$BUILD_TYPE
7677
7778
- name: Build
7879
# Execute the build. You can specify a specific target with "--target <NAME>"

CMakeLists.txt

Lines changed: 54 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,66 @@
1-
cmake_minimum_required(VERSION 3.15)
2-
3-
# Set the project name to your project name, my project isn't very descriptive
4-
project(myproject CXX)
5-
include(cmake/StandardProjectSettings.cmake)
6-
include(cmake/PreventInSourceBuilds.cmake)
7-
8-
# Link this 'library' to set the c++ standard / compile-time options requested
9-
add_library(project_options INTERFACE)
1+
cmake_minimum_required(VERSION 3.16)
2+
3+
# uncomment to set a default CXX standard for the external tools like clang-tidy and cppcheck
4+
# and the targets that do not specify a standard.
5+
# If not set, the latest supported standard for your compiler is used
6+
# You can later set fine-grained standards for each target using `target_compile_features`
7+
# set(CMAKE_CXX_STANDARD 17)
8+
9+
# Add project_options v0.12.3
10+
# https://github.com/cpp-best-practices/project_options
11+
include(FetchContent)
12+
FetchContent_Declare(_project_options URL https://github.com/cpp-best-practices/project_options/archive/refs/tags/v0.12.3.zip)
13+
FetchContent_MakeAvailable(_project_options)
14+
include(${_project_options_SOURCE_DIR}/Index.cmake)
15+
16+
# uncomment to enable vcpkg:
17+
# # Setup vcpkg - should be called before defining project()
18+
# run_vcpkg()
19+
20+
# Set the project name and language
21+
project(myproject LANGUAGES CXX)
22+
23+
# Initialize project_options variable related to this project
24+
# This overwrites `project_options` and sets `project_warnings`
25+
# uncomment the options to enable them:
26+
project_options(
27+
ENABLE_CACHE
28+
# WARNINGS_AS_ERRORS
29+
ENABLE_CPPCHECK
30+
ENABLE_CLANG_TIDY
31+
ENABLE_CONAN
32+
ENABLE_COVERAGE
33+
# ENABLE_IPO
34+
# ENABLE_INCLUDE_WHAT_YOU_USE
35+
# ENABLE_PCH
36+
# PCH_HEADERS
37+
# ENABLE_DOXYGEN
38+
# ENABLE_USER_LINKER
39+
# ENABLE_BUILD_WITH_TIME_TRACE
40+
# ENABLE_UNITY
41+
# ENABLE_SANITIZER_ADDRESS
42+
# ENABLE_SANITIZER_LEAK
43+
# ENABLE_SANITIZER_UNDEFINED_BEHAVIOR
44+
# ENABLE_SANITIZER_THREAD
45+
# ENABLE_SANITIZER_MEMORY
46+
# CONAN_OPTIONS
47+
)
1048
target_compile_features(project_options INTERFACE cxx_std_17)
1149

12-
if(CMAKE_CXX_COMPILER_ID MATCHES ".*Clang")
13-
option(ENABLE_BUILD_WITH_TIME_TRACE "Enable -ftime-trace to generate time tracing .json files on clang" OFF)
14-
if(ENABLE_BUILD_WITH_TIME_TRACE)
15-
target_compile_options(project_options INTERFACE -ftime-trace)
16-
endif()
17-
endif()
18-
19-
# Link this 'library' to use the warnings specified in CompilerWarnings.cmake
20-
add_library(project_warnings INTERFACE)
21-
22-
# enable cache system
23-
include(cmake/Cache.cmake)
24-
25-
# Add linker configuration
26-
include(cmake/Linker.cmake)
27-
configure_linker(project_options)
28-
29-
# standard compiler warnings
30-
include(cmake/CompilerWarnings.cmake)
31-
set_project_warnings(project_warnings)
32-
33-
# sanitizer options if supported by compiler
34-
include(cmake/Sanitizers.cmake)
35-
enable_sanitizers(project_options)
36-
37-
# enable doxygen
38-
include(cmake/Doxygen.cmake)
39-
enable_doxygen()
40-
41-
# allow for static analysis options
42-
include(cmake/StaticAnalyzers.cmake)
43-
44-
option(BUILD_SHARED_LIBS "Enable compilation of shared libraries" OFF)
45-
option(ENABLE_TESTING "Enable Test Builds" ON)
46-
option(ENABLE_FUZZING "Enable Fuzzing Builds" OFF)
47-
48-
# Very basic PCH example
49-
option(ENABLE_PCH "Enable Precompiled Headers" OFF)
50-
if(ENABLE_PCH)
51-
# This sets a global PCH parameter, each project will build its own PCH, which is a good idea if any #define's change
52-
#
53-
# consider breaking this out per project as necessary
54-
target_precompile_headers(
55-
project_options
56-
INTERFACE
57-
<vector>
58-
<string>
59-
<map>
60-
<utility>)
61-
endif()
62-
63-
option(ENABLE_CONAN "Use Conan for dependency management" ON)
64-
if(ENABLE_CONAN)
65-
include(cmake/Conan.cmake)
66-
run_conan()
67-
endif()
50+
# Adding the src:
51+
add_subdirectory(src)
6852

53+
# Adding the tests:
54+
option(ENABLE_TESTING "Enable the tests" ON)
6955
if(ENABLE_TESTING)
7056
enable_testing()
71-
message("Building Tests. Be sure to check out test/constexpr_tests for constexpr testing")
57+
message("Building Tests. Be sure to check out test/constexpr_tests for constexpr
58+
testing")
7259
add_subdirectory(test)
7360
endif()
7461

62+
option(ENABLE_FUZZING "Enable the fuzz tests" OFF)
7563
if(ENABLE_FUZZING)
7664
message("Building Fuzz Tests, using fuzzing sanitizer https://www.llvm.org/docs/LibFuzzer.html")
7765
add_subdirectory(fuzz_test)
7866
endif()
79-
80-
add_subdirectory(src)
81-
82-
option(ENABLE_UNITY "Enable Unity builds of projects" OFF)
83-
if(ENABLE_UNITY)
84-
# Add for any project you want to apply unity builds for
85-
set_target_properties(intro PROPERTIES UNITY_BUILD ON)
86-
endif()

build_examples.sh

Lines changed: 13 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1,133 +1,42 @@
11
#!/usr/bin/env bash
22

3-
# This script is intended to be run from an Ubuntu 18.04 Docker image.
4-
# Since it uses `apt-get` to install dependencies required by the examples,
5-
# it may also run on other versions of Ubuntu, or other Debian-derived distros.
6-
# It will definitely not run without `apt-get` installed!
3+
# This script is intended to be run from an Ubuntu Docker image (or other Debian-derived distros).
4+
# It uses `apt-get` to install dependencies required by the examples,
75

86
set -euo pipefail
97

10-
DEFAULT_BUILD_DIR="build"
11-
BUILD_DIR="${DEFAULT_BUILD_DIR}"
12-
SCRIPT_NAME=$(basename $0)
13-
PROJECT_DIRNAME=$(dirname $(readlink -f $0))
14-
USAGE="Usage: ${SCRIPT_NAME} [options]...
15-
16-
Options:
17-
-h, --help Print this message
18-
-b, --build-dir BUILD_DIR Build examples in BUILD_DIR instead of default '${DEFAULT_BUILD_DIR}'
19-
--all Build all the examples available
20-
--fltk Build fltk example
21-
--gtkmm Build gtkmm example
22-
--imgui Build imgui example
23-
--nana Build nana example
24-
--qt Build qt example
25-
--sdl Build sdl example
26-
"
27-
28-
function build_fltk() {
8+
function install_fltk() {
299
apt-get install -y --no-install-recommends libfltk1.3-dev libgl1-mesa-dev fluid
30-
BUILD_DIR="${1:-build-fltk}"
31-
mkdir -p "${BUILD_DIR}"
32-
cmake -S "${PROJECT_DIRNAME}" -B "${BUILD_DIR}" -DCPP_STARTER_USE_FLTK=ON
33-
cmake --build "${BUILD_DIR}" -j
3410
}
3511

36-
function build_gtkmm() {
12+
function install_gtkmm() {
3713
apt-get install -y --no-install-recommends pkg-config libgtkmm-3.0-dev
38-
BUILD_DIR="${1:-build-gtkmm}"
39-
mkdir -p "${BUILD_DIR}"
40-
cmake -S "${PROJECT_DIRNAME}" -B "${BUILD_DIR}" -DCPP_STARTER_USE_GTKMM=ON
41-
cmake --build "${BUILD_DIR}" -j
4214
}
4315

44-
function build_imgui() {
16+
function install_imgui() {
4517
apt-get install -y --no-install-recommends pkg-config libgl1-mesa-dev
46-
BUILD_DIR="${1:-build-imgui}"
47-
mkdir -p "${BUILD_DIR}"
48-
cmake -S "${PROJECT_DIRNAME}" -B "${BUILD_DIR}" -DCPP_STARTER_USE_IMGUI=ON
49-
cmake --build "${BUILD_DIR}" -j
5018
}
5119

52-
function build_nana() {
20+
function install_nana() {
5321
# CMakeLists.txt is 'supposed' to install all of these automatically, but for
5422
# some reason it doesn't always work. Installing them manually does work.
5523
apt-get install -y --no-install-recommends libjpeg8-dev libpng-dev \
5624
libasound2-dev alsa-utils alsa-oss libx11-dev libxft-dev libxcursor-dev
57-
BUILD_DIR="${1:-build-nana}"
58-
mkdir -p "${BUILD_DIR}"
5925
# Note: Nana's headers trigger the -Wshadow warning, and cannot be compiled with -Werror.
6026
# Supposedly, this is fixed in the develop-1.8 branch, but I cannot confirm.
61-
cmake -S "${PROJECT_DIRNAME}" -B "${BUILD_DIR}" -DCPP_STARTER_USE_NANA=ON -DWARNINGS_AS_ERRORS=FALSE
62-
cmake --build "${BUILD_DIR}" -j
6327
}
6428

65-
function build_qt() {
29+
function install_qt() {
6630
apt-get install -y --no-install-recommends qt5-default qtbase5-dev
67-
BUILD_DIR="${1:-build-qt}"
68-
mkdir -p "${BUILD_DIR}"
69-
cmake -S "${PROJECT_DIRNAME}" -B "${BUILD_DIR}" -DCPP_STARTER_USE_QT=ON
70-
cmake --build "${BUILD_DIR}" -j
7131
}
7232

73-
function build_sdl() {
33+
function install_sdl() {
7434
pip install mako
75-
BUILD_DIR="${1:-build-sdl}"
76-
mkdir -p "${BUILD_DIR}"
77-
cmake -S "${PROJECT_DIRNAME}" -B "${BUILD_DIR}" -DCPP_STARTER_USE_SDL=ON
78-
cmake --build "${BUILD_DIR}" -j
79-
}
80-
81-
function build_all() {
82-
build_fltk
83-
build_gtkmm
84-
build_imgui
85-
build_nana
86-
build_qt
87-
build_sdl
8835
}
8936

90-
# Parse the available options
91-
PARSED_OPTIONS=$(getopt -n "$0" -o h: --long "fltk,gtkmm,imgui,nana,qt,sdl,all,help,build:" -- "$@")
92-
eval set -- "${PARSED_OPTIONS}"
37+
# call the above functions to install the required dependencies. As an example for qt:
38+
# install_qt
9339

94-
# Handle all the options
95-
while true;
96-
do
97-
case "$1" in
98-
--help|-h)
99-
echo "${USAGE}"
100-
shift;;
101-
--build-dir|-b)
102-
if [ -n "$2" ];
103-
then
104-
BUILD_DIR="$2"
105-
echo "Using BUILD_DIR='${BUILD_DIR}'"
106-
fi
107-
shift 2;;
108-
--all)
109-
build_all $BUILD_DIR
110-
shift;;
111-
--fltk)
112-
build_fltk $BUILD_DIR
113-
shift;;
114-
--gtkmm)
115-
build_gtkmm $BUILD_DIR
116-
shift;;
117-
--imgui)
118-
build_imgui $BUILD_DIR
119-
shift;;
120-
--nana)
121-
build_nana $BUILD_DIR
122-
shift;;
123-
--qt)
124-
build_qt $BUILD_DIR
125-
shift;;
126-
--sdl)
127-
build_sdl $BUILD_DIR
128-
shift;;
129-
--)
130-
shift
131-
break;;
132-
esac
133-
done
40+
# build with:
41+
cmake -S . -B "./build"
42+
cmake --build "./build"

cmake/Cache.cmake

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)