generated from mcmarius/oop-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
CMakeLists.txt
159 lines (131 loc) · 7.65 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
cmake_minimum_required(VERSION 3.15)
# NOTE: update executable name in .github/workflows/cmake.yml:44 when changing executable name in this file
# for now, the project name is used as the executable name
project(oop)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
# By default, treat warnings as errors; override via cmake defines or via env vars
if(DEFINED WARNINGS_AS_ERRORS)
# do nothing, already defined
elseif(DEFINED ENV{WARNINGS_AS_ERRORS})
set(WARNINGS_AS_ERRORS $ENV{WARNINGS_AS_ERRORS})
else()
set(WARNINGS_AS_ERRORS ON)
endif()
# disable them when releasing executables without explicitly requested debug info
# use generator expressions to set flags correctly in both single and multi config generators
set(is_debug "$<CONFIG:Debug>")
set(is_rel_with_deb "$<CONFIG:RelWithDebInfo>")
set(debug_mode "$<OR:${is_debug},${is_rel_with_deb}>")
###############################################################################
# external dependencies with FetchContent
include(FetchContent)
# NOTE: Also update SFML_VERSION env var in .github/workflows/cmake.yml:59
FetchContent_Declare(
SFML
GIT_REPOSITORY https://github.com/SFML/SFML.git
GIT_TAG 863fef024619da7f59b6404087b454337a172ac1 # 2.6.x as of 2022-08-30
# GIT_TAG 72d88033e2f24be0fb1e9df614a56f3d5274154c # master as of 2022-08-30
# GIT_TAG f7c88ee7ef4e1c705531cd614efb7dcff1f873cb # last commit merged in master before API breakage (2022-04-21)
# GIT_TAG origin/master
# GIT_TAG origin/2.6.x
# GIT_SHALLOW 1 # works only with branches or tags, not with arbitrary commit hashes
)
function(sfml_set_custom_stdlib target)
if(MSVC)
target_compile_options(${target} PRIVATE "$<${debug_mode}:/fsanitize=address>")
elseif(APPLE)
elseif(UNIX)
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
target_compile_options(${target} PRIVATE -stdlib=libc++)
target_link_libraries(${target} PRIVATE "-stdlib=libc++")
if("${CMAKE_CXX_COMPILER_VERSION}" MATCHES "14.")
target_compile_options(${target} PRIVATE "$<${debug_mode}:-fsanitize=address,undefined>")
target_link_options(${target} PRIVATE "$<${debug_mode}:-fsanitize=address,undefined>")
elseif("${CMAKE_CXX_COMPILER_VERSION}" MATCHES "13.")
target_compile_options(${target} PRIVATE "$<${debug_mode}:-fsanitize=memory,undefined;-fsanitize-recover=memory,undefined;-fsanitize-memory-track-origins>")
target_link_options(${target} PRIVATE "$<${debug_mode}:-fsanitize=memory,undefined;-fsanitize-recover=memory,undefined;-fsanitize-memory-track-origins;-Wl,-rpath,tools/llvm-project/build/lib>")
else()
message("No matching Clang version to add sanitizer flags!")
endif()
endif()
endif()
endfunction()
FetchContent_MakeAvailable(SFML)
sfml_set_custom_stdlib(sfml-system)
sfml_set_custom_stdlib(sfml-window)
sfml_set_custom_stdlib(sfml-graphics)
###############################################################################
# external dependencies with find_package
# find_package(Threads REQUIRED)
###############################################################################
# NOTE: update executable name in .github/workflows/cmake.yml when changing name here
add_executable(${PROJECT_NAME} main.cpp Game.cpp Game.h States/State.cpp States/State.h States/GameState.cpp States/GameState.h States/MainMenuState.cpp States/MainMenuState.h States/GameState.cpp States/GameState.h Entities/Entity.cpp Entities/Entity.h Button.cpp Button.h Entities/Player.cpp Entities/Player.h Map/Cell.cpp Map/Cell.h Map/Map.cpp Map/Map.h Exceptions.h Entities/AnimationComponent.cpp Entities/AnimationComponent.h States/PausedState.cpp States/PausedState.h Entities/EnemyTemplate.h Entities/EnemyFactory.cpp Entities/EnemyFactory.h)
###############################################################################
# target definitions
if(GITHUB_ACTIONS)
message("NOTE: GITHUB_ACTIONS defined")
target_compile_definitions(${PROJECT_NAME} PRIVATE GITHUB_ACTIONS)
endif()
###############################################################################
# custom compiler flags
message("Compiler: ${CMAKE_CXX_COMPILER_ID} version ${CMAKE_CXX_COMPILER_VERSION}")
if(MSVC)
target_compile_options(${PROJECT_NAME} PRIVATE /W4 /permissive- /wd4244 /wd4267 /external:anglebrackets /external:W0)
if(WARNINGS_AS_ERRORS)
target_compile_options(${PROJECT_NAME} PRIVATE /WX)
endif()
else()
target_compile_options(${PROJECT_NAME} PRIVATE -Wall -Wextra -pedantic)
if(WARNINGS_AS_ERRORS)
target_compile_options(${PROJECT_NAME} PRIVATE -Werror)
endif()
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU")
elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
target_compile_options(${PROJECT_NAME} PRIVATE -stdlib=libc++)
target_link_options(${PROJECT_NAME} PRIVATE -stdlib=libc++)
endif()
endif()
###############################################################################
# sanitizers
if(MSVC)
target_compile_options(${PROJECT_NAME} PRIVATE "$<${debug_mode}:/fsanitize=address>")
elseif(APPLE)
# if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
# first check Apple since Apple is also a kind of Unix
target_compile_options(${PROJECT_NAME} PRIVATE "$<${debug_mode}:-fsanitize=address,undefined>")
target_link_options(${PROJECT_NAME} PRIVATE "$<${debug_mode}:-fsanitize=address,undefined>")
# endif()
elseif(UNIX)
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU")
# we cannot mix sanitizers with Valgrind
elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
if("${CMAKE_CXX_COMPILER_VERSION}" MATCHES "14.")
# check leaks on Linux since macOS does not support the leaks sanitizer yet
# leaks sanitizer is enabled by default on Linux, so we do not need to enable it explicitly
target_compile_options(${PROJECT_NAME} PRIVATE "$<${debug_mode}:-fsanitize=address,undefined>")
target_link_options(${PROJECT_NAME} PRIVATE "$<${debug_mode}:-fsanitize=address,undefined>")
elseif("${CMAKE_CXX_COMPILER_VERSION}" MATCHES "13.")
# use semi-colons instead of spaces to separate arguments
# it is recommended to quote generator expressions in order to avoid unintentional splitting
target_compile_options(${PROJECT_NAME} PRIVATE "$<${debug_mode}:-fsanitize=memory,undefined;-fsanitize-recover=memory,undefined;-fsanitize-memory-track-origins>")
target_link_options(${PROJECT_NAME} PRIVATE "$<${debug_mode}:-fsanitize=memory,undefined;-fsanitize-recover=memory,undefined;-fsanitize-memory-track-origins;-Wl,-rpath,tools/llvm-project/build/lib>")
else()
message("No matching Clang version to add sanitizer flags!")
endif()
endif()
endif()
###############################################################################
# use SYSTEM so clang-tidy does not report warnings from these directories
#target_include_directories(${PROJECT_NAME} SYSTEM PRIVATE ext/<SomeHppLib>/include)
target_include_directories(${PROJECT_NAME} SYSTEM PRIVATE ${SFML_SOURCE_DIR}/include)
target_link_directories(${PROJECT_NAME} PRIVATE ${SFML_BINARY_DIR}/lib)
target_link_libraries(${PROJECT_NAME} sfml-graphics sfml-window sfml-system)
###############################################################################
# copy binaries to bin folder; these are uploaded as artifacts on each release
install(TARGETS ${PROJECT_NAME} DESTINATION bin)
install(DIRECTORY Fonts DESTINATION bin)
install(DIRECTORY Sprites DESTINATION bin)
# install(DIRECTORY some_dir1 some_dir2 DESTINATION bin)
# install(FILES some_file1.txt some_file2.md DESTINATION bin)