Skip to content

Commit 0cdddbf

Browse files
committed
Make dependency setup easier and more aligned with CMake best practices
Also upgraded all dependencies
1 parent 8a46d0b commit 0cdddbf

File tree

10 files changed

+58
-61
lines changed

10 files changed

+58
-61
lines changed

CMakeLists.txt

-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ project(
88
VERSION 1.0.0
99
LANGUAGES CXX)
1010

11-
set(CMAKE_CXX_STANDARD 20)
12-
set(CMAKE_CXX_STANDARD_REQUIRED ON)
13-
1411
set(PROJECT_COMPANY_NAME "My Company")
1512
set(PROJECT_COMPANY_NAMESPACE "com.mycompany") # Reverse domain name notation
1613

cmake/StaticAnalyzers.cmake

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ if (NOT CMAKE_BUILD_TYPE STREQUAL "Release")
1515
# discovered on Apple M1, 13.0.
1616
if (NOT WIN32)
1717
message(STATUS "Using address sanitizer")
18-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0 -fsanitize=address -g")
18+
add_compile_options(-O0 -fsanitize=address -g)
19+
add_link_options(-O0 -fsanitize=address -g)
1920
endif ()
2021
endif ()

docs/Dependencies.md

+33-18
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
Dependencies are located in `vendor/`. The `vendor/CMakeLists.txt` uses
44
CMake's [FetchContent](https://cmake.org/cmake/help/latest/module/FetchContent.html) to load dependencies on configure
5-
time. Every dependency also has an associated folder containing a `CMakeLists.txt` for configuration.
5+
time. Some dependencies also have an associated folder containing a `CMakeLists.txt` for configuration or setup purpose.
66

77
## Already included
88

9-
The following set of dependencies is already included:
9+
The following set of dependencies are already included:
1010

1111
- [Doctest](https://github.com/doctest/doctest) - Testing framework
1212
- [fmtlib](https://fmt.dev/latest/index.html) - Formatting library
@@ -29,37 +29,35 @@ FetchContent_Declare(
2929
GIT_REPOSITORY "https://github.com/gabime/spdlog.git"
3030
GIT_TAG v1.11.0
3131
)
32-
add_subdirectory(spdlog)
3332
```
3433

35-
After adding this to the `vendor` CMake file, a new `CMakeLists.txt` needs to be created in a new folder for the
36-
dependency. Again with the spdlog example:
34+
Further down the same file is a section for dependency settings. Again using spdlog as an example:
3735

3836
```cmake
39-
# vendor/spdlog/CMakeLists.txt
37+
# vendor/CMakeLists.txt
4038
41-
message(STATUS "Fetching spdlog ...")
39+
# Settings
4240
4341
# Any package build settings here
4442
set(SPDLOG_FMT_EXTERNAL "ON")
4543
46-
FetchContent_MakeAvailable(spdlog)
44+
# Populate
45+
46+
FetchContent_MakeAvailable(
47+
# Other dependencies ...
48+
spdlog)
4749
```
4850

49-
This dependency specific CMake file will contain a message for fetching the library, any package configuration, and a
50-
call to `FetchContent_MakeAvailable` with the given name to add them to the build.
51+
At the end the call to `FetchContent_MakeAvailable` gets the new dependency added as well.
5152

5253
## New dependency without CMake support
5354

54-
Adding a package that does not support CMake works almost the same as with support above. The difference is that the new
55-
library needs to be declared. Taking Dear ImGui as an example, that does not support CMake, this is how the setup is
55+
Adding a package that does not support CMake is also not a problem. The difference is that the new library needs to be
56+
setup separately. Taking Dear ImGui as an example, that does not support CMake, this is how the setup is
5657
done:
5758

5859
```cmake
59-
message(STATUS "Fetching imgui ...")
60-
61-
# Define build options.
62-
set(CMAKE_CXX_STANDARD 20)
60+
# vendor/imgui-setup/CMakeLists.txt
6361
6462
# Populate scope with library variables to get access to source and build directories.
6563
FetchContent_GetProperties(imgui)
@@ -81,11 +79,28 @@ add_library(imgui
8179
# Set include directory based in populated variable `imgui_SOURCE_DIR`.
8280
target_include_directories(imgui PUBLIC ${imgui_SOURCE_DIR})
8381
82+
# Define compile options.
83+
target_compile_features(imgui PRIVATE cxx_std_20)
84+
8485
# Link external library SDL2, part of the dependencies as well.
8586
target_link_libraries(imgui PUBLIC SDL2::SDL2)
87+
```
88+
89+
After setting up the library it needs to be made available in the vendor `CMakeLists.txt`:
90+
91+
```cmake
92+
# vendor/CMakeLists.txt
93+
94+
# Settings
95+
96+
# Adding the setup directory
97+
add_subdirectory(imgui-setup)
98+
99+
# Populate
86100
87-
# Add to main build
88-
FetchContent_MakeAvailable(imgui)
101+
FetchContent_MakeAvailable(
102+
# Other dependencies ...
103+
imgui)
89104
```
90105

91106
## Link dependency

vendor/CMakeLists.txt

+22-10
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,55 @@
11
include(FetchContent)
22

3+
# Dependencies
4+
35
FetchContent_Declare(
46
doctest
57
GIT_REPOSITORY "https://github.com/onqtam/doctest.git"
68
GIT_TAG v2.4.11
79
)
8-
add_subdirectory(doctest)
910

1011
FetchContent_Declare(
1112
fmt
1213
GIT_REPOSITORY "https://github.com/fmtlib/fmt.git"
13-
GIT_TAG 10.1.1
14+
GIT_TAG 10.2.1
1415
)
15-
add_subdirectory(fmt)
1616

1717
FetchContent_Declare(
1818
glad
1919
GIT_REPOSITORY "https://github.com/krieselreihe/glad.git"
2020
GIT_TAG v1.0.0
2121
)
22-
add_subdirectory(glad)
2322

2423
FetchContent_Declare(
2524
imgui
2625
GIT_REPOSITORY "https://github.com/ocornut/imgui.git"
27-
GIT_TAG 37ea320b96a4b77581986f5581707021a7be94c9 # Branch: docking, date: 07.11.2023, 08:20 GMT+1
26+
GIT_TAG 085781f5ca5372d5fc804d7e44b5bf27a8994af7 # Branch: docking, date: 19.03.2024, 06:52 GMT+1
2827
)
29-
add_subdirectory(imgui)
3028

3129
FetchContent_Declare(
3230
SDL2
3331
GIT_REPOSITORY "https://github.com/libsdl-org/SDL.git"
34-
GIT_TAG release-2.28.4
32+
GIT_TAG release-2.30.1
3533
)
36-
add_subdirectory(sdl)
3734

3835
FetchContent_Declare(
3936
spdlog
4037
GIT_REPOSITORY "https://github.com/gabime/spdlog.git"
41-
GIT_TAG v1.12.0
38+
GIT_TAG v1.13.0
4239
)
43-
add_subdirectory(spdlog)
40+
41+
# Settings
42+
43+
# For SDL2 to be able to override options
44+
set(CMAKE_POLICY_DEFAULT_CMP0077 NEW)
45+
46+
set(DOCTEST_NO_INSTALL ON)
47+
set(FMT_INSTALL OFF)
48+
set(SDL2_DISABLE_SDL2MAIN ON)
49+
set(SPDLOG_FMT_EXTERNAL ON)
50+
51+
add_subdirectory(imgui-setup)
52+
53+
# Populate
54+
55+
FetchContent_MakeAvailable(doctest fmt glad imgui SDL2 spdlog)

vendor/doctest/CMakeLists.txt

-5
This file was deleted.

vendor/fmt/CMakeLists.txt

-5
This file was deleted.

vendor/glad/CMakeLists.txt

-3
This file was deleted.

vendor/imgui/CMakeLists.txt renamed to vendor/imgui-setup/CMakeLists.txt

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
message(STATUS "Fetching imgui ...")
2-
3-
set(CMAKE_CXX_STANDARD 20)
4-
51
FetchContent_GetProperties(imgui)
62
if (NOT imgui_POPULATED)
73
FetchContent_Populate(imgui)
@@ -18,6 +14,5 @@ add_library(imgui
1814
${imgui_SOURCE_DIR}/backends/imgui_impl_opengl3.h ${imgui_SOURCE_DIR}/backends/imgui_impl_opengl3.cpp)
1915

2016
target_include_directories(imgui PUBLIC ${imgui_SOURCE_DIR})
17+
target_compile_features(imgui PRIVATE cxx_std_20)
2118
target_link_libraries(imgui PUBLIC SDL2::SDL2)
22-
23-
FetchContent_MakeAvailable(imgui)

vendor/sdl/CMakeLists.txt

-5
This file was deleted.

vendor/spdlog/CMakeLists.txt

-5
This file was deleted.

0 commit comments

Comments
 (0)