-
-
Notifications
You must be signed in to change notification settings - Fork 295
/
CMakeLists.txt
242 lines (201 loc) · 8.51 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
cmake_minimum_required(VERSION 3.1)
set(CMAKE_CXX_STANDARD 11)
set(ENTITYX_MAJOR_VERSION 1)
set(ENTITYX_MINOR_VERSION 1)
set(ENTITYX_PATCH_VERSION 2)
set(ENTITYX_VERSION ${ENTITYX_MAJOR_VERSION}.${ENTITYX_MINOR_VERSION}.${ENTITYX_PATCH_VERSION})
set(CMAKE_CXX_EXTENSIONS OFF)
project(EntityX)
message(STATUS "EntityX version ${ENTITYX_VERSION}")
if(NOT DEFINED CMAKE_MACOSX_RPATH)
set(CMAKE_MACOSX_RPATH 0)
endif()
include_directories(${CMAKE_CURRENT_LIST_DIR})
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
set(ENTITYX_BUILD_TESTING true CACHE BOOL "Enable building of tests.")
set(ENTITYX_RUN_BENCHMARKS false CACHE BOOL "Run benchmarks (in conjunction with -DENTITYX_BUILD_TESTING=1).")
set(ENTITYX_MAX_COMPONENTS 64 CACHE STRING "Set the maximum number of components.")
set(ENTITYX_DT_TYPE double CACHE STRING "The type used for delta time in EntityX update methods.")
set(ENTITYX_BUILD_SHARED true CACHE BOOL "Build shared libraries?")
include(${CMAKE_ROOT}/Modules/CheckIncludeFile.cmake)
include(CheckCXXSourceCompiles)
if(ENTITYX_BUILD_SHARED AND CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
message(WARNING "\nBuilding as a shared library with MSVC is currently unsupported \
and will not function as expected.\
\nUse -DENTITYX_BUILD_SHARED=FALSE to build as a static library.")
endif()
# Default compiler args
if (CMAKE_CXX_COMPILER_ID MATCHES "(GNU|.*Clang)")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -Werror -Wall -Wextra -Wno-unused-parameter -Wno-error=unused-variable -Wno-error=sign-compare")
set(CMAKE_CXX_FLAGS_DEBUG "-O0 -g")
set(CMAKE_CXX_FLAGS_MINSIZEREL "-g -Os -DNDEBUG")
set(CMAKE_CXX_FLAGS_RELEASE "-g -O2 -DNDEBUG")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O2 -g")
elseif(CMAKE_CXX_COMPILER_ID STREQUAL 'MSVC')
# /Zi - Produces a program database (PDB) that contains type information and symbolic debugging information for use with the debugger.
# /FS - Allows multiple cl.exe processes to write to the same .pdb file
# /DEBUG - Enable debug during linking
# /Od - Disables optimization
set(CMAKE_CXX_FLAGS_DEBUG "/Zi /FS /DEBUG /Od /MDd")
# /Ox - Full optimization
set(CMAKE_CXX_FLAGS_RELEASE "/Ox -DNDEBUG")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "/Ox /Zi /FS /DEBUG")
endif()
# if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Weverything -Wno-c++98-compat -Wno-shadow -Wno-padded -Wno-missing-noreturn -Wno-global-constructors")
# endif()
# Library installation directory
if(NOT DEFINED CMAKE_INSTALL_LIBDIR)
set(CMAKE_INSTALL_LIBDIR lib)
endif(NOT DEFINED CMAKE_INSTALL_LIBDIR)
set(libdir ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR})
set(OLD_CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
if ((MAKE_CXX_COMPILER_ID STREQUAL "GNU") OR (CMAKE_CXX_COMPILER_ID STREQUAL "Clang"))
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
endif()
check_cxx_source_compiles(
"
#include <memory>
int main() {
std::shared_ptr<int>();
}
"
ENTITYX_HAVE_CXX11_STDLIB
)
if (NOT ENTITYX_HAVE_CXX11_STDLIB)
message(STATUS "-- Not using -stdlib=libc++ (test failed to build)")
set(CMAKE_CXX_FLAGS "${OLD_CMAKE_CXX_FLAGS}")
else ()
message(STATUS "-- Using -stdlib=libc++")
endif ()
# Misc features
check_include_file("stdint.h" HAVE_STDINT_H)
macro(require FEATURE_NAME MESSAGE_STRING)
if (NOT ${${FEATURE_NAME}})
message(FATAL_ERROR "${MESSAGE_STRING} required -- ${${FEATURE_NAME}}")
else()
message(STATUS "-- ${MESSAGE_STRING} found")
endif()
endmacro(require)
macro(create_test TARGET_NAME SOURCE DEPENDENCIES)
add_executable(${TARGET_NAME} ${SOURCE})
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "entityx/tests")
target_link_libraries(
${TARGET_NAME}
${DEPENDENCIES}
${ARGN}
)
# Special case for benchmark tests
if (${TARGET_NAME} MATCHES .*benchmark.*)
if(ENTITYX_RUN_BENCHMARKS)
add_test(${TARGET_NAME} ${TARGET_NAME})
endif()
else()
add_test(${TARGET_NAME} ${TARGET_NAME})
endif()
endmacro()
if (NOT CMAKE_BUILD_TYPE)
message(STATUS "-- Defaulting to release build (use -DCMAKE_BUILD_TYPE:STRING=Debug for debug build)")
set(CMAKE_BUILD_TYPE "Release")
endif()
message(STATUS "-- Checking misc features")
require(HAVE_STDINT_H "stdint.h")
# Things to install
if (NOT DEFINED CMAKE_DEBUG_POSTFIX)
set(CMAKE_DEBUG_POSTFIX "-d")
endif()
set(sources entityx/System.cc entityx/Event.cc entityx/Entity.cc entityx/help/Timer.cc entityx/help/Pool.cc)
if (ENTITYX_BUILD_SHARED)
message(STATUS "-- Building shared libraries (-DENTITYX_BUILD_SHARED=0 to only build static libraries)")
add_library(entityx SHARED ${sources})
set_target_properties(entityx PROPERTIES
OUTPUT_NAME entityx
DEBUG_POSTFIX "${CMAKE_DEBUG_POSTFIX}"
VERSION ${ENTITYX_VERSION}
SOVERSION ${ENTITYX_MAJOR_VERSION}
FOLDER entityx)
set(install_libs entityx)
set_property(TARGET entityx APPEND PROPERTY INTERFACE_INCLUDE_DIRECTORIES
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)
else()
add_library(entityx STATIC ${sources})
set_target_properties(entityx PROPERTIES
DEBUG_POSTFIX "${CMAKE_DEBUG_POSTFIX}"
FOLDER entityx)
set(install_libs entityx)
set_property(TARGET entityx APPEND PROPERTY INTERFACE_INCLUDE_DIRECTORIES
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)
endif (ENTITYX_BUILD_SHARED)
# Make sure C++11 features are available
target_compile_features(entityx PUBLIC cxx_auto_type)
target_compile_features(entityx PUBLIC cxx_nullptr)
target_compile_features(entityx PUBLIC cxx_static_assert)
target_compile_features(entityx PUBLIC cxx_decltype)
target_compile_features(entityx PUBLIC cxx_constexpr)
target_compile_features(entityx PUBLIC cxx_sizeof_member)
target_compile_features(entityx PUBLIC cxx_variadic_templates)
target_compile_features(entityx PUBLIC cxx_rvalue_references)
target_compile_features(entityx PUBLIC cxx_long_long_type)
target_compile_features(entityx PUBLIC cxx_lambdas)
target_compile_features(entityx PUBLIC cxx_func_identifier)
if (ENTITYX_BUILD_TESTING)
enable_testing()
create_test(pool_test entityx/help/Pool_test.cc ${install_libs})
create_test(entity_test entityx/Entity_test.cc ${install_libs})
create_test(event_test entityx/Event_test.cc ${install_libs})
create_test(system_test entityx/System_test.cc ${install_libs})
create_test(tags_component_test entityx/tags/TagsComponent_test.cc ${install_libs})
create_test(dependencies_test entityx/deps/Dependencies_test.cc ${install_libs})
create_test(benchmarks_test entityx/Benchmarks_test.cc ${install_libs})
if (ENTITYX_RUN_BENCHMARKS)
message(STATUS "-- Running benchmarks")
else ()
message(STATUS "-- Not running benchmarks (use -DENTITYX_RUN_BENCHMARKS=1 to enable)")
endif ()
endif (ENTITYX_BUILD_TESTING)
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/entityx/config.h.in
${CMAKE_CURRENT_SOURCE_DIR}/entityx/config.h
)
if (NOT WINDOWS OR CYGWIN)
set(entityx_libs -lentityx)
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/entityx.pc.in
${CMAKE_CURRENT_BINARY_DIR}/entityx.pc
)
install(
FILES ${CMAKE_CURRENT_BINARY_DIR}/entityx.pc
DESTINATION "${libdir}/pkgconfig"
)
endif()
install(
DIRECTORY "entityx"
DESTINATION "include"
FILES_MATCHING PATTERN "*.h"
)
install(
TARGETS ${install_libs}
LIBRARY DESTINATION "${libdir}"
ARCHIVE DESTINATION "${libdir}"
RUNTIME DESTINATION "bin"
)
###########################################################################
# EXAMPLE
option(ENTITYX_ENABLE_EXAMPLE_TARGET "Add target for building the example (will fetch SFML)" OFF)
if(ENTITYX_ENABLE_EXAMPLE_TARGET)
include(FetchContent)
set(BUILD_SHARED_LIBS FALSE)
FetchContent_Declare(
SFML
GIT_REPOSITORY https://github.com/SFML/SFML.git
GIT_TAG a21c3c101cd02e41b79ffc515f93c649e230e263 # 2.6.x
)
FetchContent_MakeAvailable(SFML)
add_executable(entityx_example examples/example.cc)
target_compile_features(entityx_example PRIVATE cxx_std_11)
target_include_directories(entityx_example PRIVATE ${SFML_SOURCE_DIR}/include ${entityx_SOURCE_DIR}/entityx)
target_link_libraries(entityx_example PRIVATE entityx sfml-system sfml-window sfml-graphics)
add_custom_command(TARGET entityx_example PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/examples/LiberationSans-Regular.ttf ${CMAKE_CURRENT_BINARY_DIR}/LiberationSans-Regular.ttf
)
endif()