-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
170 lines (131 loc) · 4.22 KB
/
CMakeLists.txt
File metadata and controls
170 lines (131 loc) · 4.22 KB
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
160
161
162
163
164
165
166
167
168
169
cmake_minimum_required(VERSION 3.25)
project(physim VERSION 0.1.0 LANGUAGES CXX)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# Options
option(PHYSIM_WARNINGS_AS_ERRORS "Warnings as errors" ON)
option(PHYSIM_ENABLE_SANITIZERS "Enable ASan/UBSan in Debug" ON)
option(PHYSIM_TESTING "Build tests" ON)
option(PHYSIM_ENABLE_COVERAGE "Enable coverage reporting" OFF)
if(PHYSIM_ENABLE_COVERAGE AND CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
message(STATUS "Coverage enabled")
add_compile_options(--coverage -O0 -g)
add_link_options(--coverage)
endif()
# --------------- Library ---------------
add_library(physim
src/energie.cpp
src/collision.cpp
src/energie.cpp
src/units.cpp
)
add_library(physim::physim ALIAS physim)
target_compile_features(physim PUBLIC cxx_std_23)
target_include_directories(physim
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)
# --------------- Strict compile flags ---------------
function(physim_apply_strict_flags tgt)
if (MSVC)
target_compile_options(${tgt} PRIVATE /W4)
if (PHYSIM_WARNINGS_AS_ERRORS)
target_compile_options(${tgt} PRIVATE /WX)
endif()
else()
target_compile_options(${tgt} PRIVATE
-Wall -Wextra -Wpedantic
-Wshadow -Wconversion -Wsign-conversion
-Wold-style-cast -Wcast-align
-Woverloaded-virtual -Wnon-virtual-dtor
-Wnull-dereference -Wdouble-promotion
-Wformat=2 -Wimplicit-fallthrough
-fno-omit-frame-pointer
-fstack-protector-strong
)
if (PHYSIM_WARNINGS_AS_ERRORS)
target_compile_options(${tgt} PRIVATE -Werror)
endif()
target_link_options(${tgt} PRIVATE
-fstack-protector-strong
)
endif()
endfunction()
physim_apply_strict_flags(physim)
# --------------- Sanitizers (Debug only) ---------------
function(physim_apply_sanitizers tgt)
if (NOT PHYSIM_ENABLE_SANITIZERS)
return()
endif()
target_compile_options(${tgt} PRIVATE
$<$<CONFIG:Debug>:-fsanitize=address,undefined>
$<$<CONFIG:Debug>:-fno-sanitize-recover=all>
)
target_link_options(${tgt} PRIVATE
$<$<CONFIG:Debug>:-fsanitize=address,undefined>
)
endfunction()
physim_apply_sanitizers(physim)
# --------------- Installation ---------------
include(GNUInstallDirs)
install(TARGETS physim
EXPORT physimTargets
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_LIBDIR}
)
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
# Export targets
install(EXPORT physimTargets
FILE physimTargets.cmake
NAMESPACE physim::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/physim
)
include(CMakePackageConfigHelpers)
configure_package_config_file(
${CMAKE_CURRENT_SOURCE_DIR}/cmake/physimConfig.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/physimConfig.cmake
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/physim
)
write_basic_package_version_file(
${CMAKE_CURRENT_BINARY_DIR}/physimConfigVersion.cmake
VERSION ${PROJECT_VERSION}
COMPATIBILITY SameMajorVersion
)
install(FILES
${CMAKE_CURRENT_BINARY_DIR}/physimConfig.cmake
${CMAKE_CURRENT_BINARY_DIR}/physimConfigVersion.cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/physim
)
# --------------- Testing ---------------
include(CTest)
set(BUILD_TESTING ${PHYSIM_TESTING} CACHE BOOL "Build tests" FORCE)
if (BUILD_TESTING)
enable_testing()
set(PHYSIM_TEST_MAIN ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_main.cpp)
file(GLOB TEST_SOURCES CONFIGURE_DEPENDS
${CMAKE_CURRENT_SOURCE_DIR}/tests/test_*.cpp
)
foreach(test_src ${TEST_SOURCES})
get_filename_component(test_name ${test_src} NAME_WE)
# Skip test_main.cpp
if (test_src STREQUAL PHYSIM_TEST_MAIN)
continue()
endif()
add_executable(${test_name}
${PHYSIM_TEST_MAIN}
${test_src}
)
target_link_libraries(${test_name} PRIVATE physim::physim)
target_include_directories(${test_name} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/include
${CMAKE_CURRENT_SOURCE_DIR}/tests
)
target_compile_features(${test_name} PRIVATE cxx_std_23)
physim_apply_strict_flags(${test_name})
physim_apply_sanitizers(${test_name})
add_test(NAME ${test_name} COMMAND ${test_name})
endforeach()
endif()