-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
228 lines (171 loc) · 7.59 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
cmake_minimum_required(VERSION 3.7)
# Policies
cmake_policy(SET CMP0025 NEW) # AppleClang instead of Clang (may not work, so check both)
cmake_policy(SET CMP0048 NEW) # Version in project definition
# Project
project(StealthEngine VERSION 0.1 LANGUAGES CXX)
# CMake vars
# Build type
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build, options are: Debug, Release." FORCE)
endif(NOT CMAKE_BUILD_TYPE)
# Cmake modules
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${PROJECT_SOURCE_DIR}/cmake)
include(Export)
# Options
option(BUILD_TESTS "Build tests" OFF)
# Flags
# C++17
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
message(STATUS "CMAKE_C_COMPILER_ID: ${CMAKE_C_COMPILER_ID}")
message(STATUS "CMAKE_CXX_COMPILER_ID: ${CMAKE_CXX_COMPILER_ID}")
message(STATUS "COMPILER_NAME: ${COMPILER_NAME}")
message(STATUS "CMAKE_CXX_COMPILER_VERSION: ${CMAKE_CXX_COMPILER_VERSION}")
message(STATUS "CC: ${CC}")
message(STATUS "CXX: ${CXX}")
if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
# using Clang
# set(CXX_VERSION_FLAG "-std=c++17")
# add_compile_options(-std=c++17) # CMake 2.8.12 or newer
# add_compile_options("$<$<STREQUAL:$<TARGET_PROPERTY:LINKER_LANGUAGE>,CXX>:-std=c++1z>")
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
# using GCC
# set(CXX_VERSION_FLAG "-std=c++1z")
# add_compile_options(-std=c++17) # CMake 2.8.12 or newer
# add_compile_options("$<$<STREQUAL:$<TARGET_PROPERTY:LINKER_LANGUAGE>,CXX>:-std=c++17>")
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
# using Intel C++
# set(CXX_VERSION_FLAG "-std=c++17")
# add_compile_options(-std=c++17) # CMake 2.8.12 or newer
# add_compile_options("$<$<STREQUAL:$<TARGET_PROPERTY:LINKER_LANGUAGE>,CXX>:-std=c++17>")
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
# using Visual Studio C++
# set(CXX_VERSION_FLAG "-std=c++17")
# add_compile_options(-std=c++17) # CMake 2.8.12 or newer
# add_compile_options("$<$<STREQUAL:$<TARGET_PROPERTY:LINKER_LANGUAGE>,CXX>:-std=c++17>")
endif()
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CXX_VERSION_FLAG} -Wno-logical-op-parentheses")
#if(ENABLE_COVERAGE)
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --coverage")
## set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs")
## set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ftest-coverage")
## set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --coverage")
#endif()
# Produce debugging information on debug, optimize on release
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -O0")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3")
# If clang is used for compilation, force it to emit debug information for libstc++
# https://stackoverflow.com/questions/41745527/cannot-view-stdstring-when-compiled-with-clang
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -D_GLIBCXX_DEBUG")
# Output paths
message(STATUS "CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}")
set(CMAKE_BINARY_DIR "${CMAKE_CURRENT_SOURCE_DIR}/build/${CMAKE_BUILD_TYPE}")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") # executable output directory
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib") # static libraries output directory
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib") # dynamic libraries output directory (unused)
set(CMAKE_LIBRARY_PATH "${PROJECT_SOURCE_DIR}/libs")
# Input files
file(GLOB_RECURSE ENGINE_SOURCE_FILES "include/*.h" "src/*.cpp")
# some tests outside the test folder...
#file(GLOB_RECURSE TEST_ENGINE_SOURCE_FILES2 "test/*.h" "test/*.cpp")
set(TEST_ENGINE_SOURCE_FILES
# REFACTOR: move catch and Smoke tests framework to yet another SOURCE_FILES
test/catch.hpp
test/catch_with_main.hpp
test/testmain.cpp
test/TestFriend.h
src/test/SmokeTestRunner.cpp
include/test/SmokeTestRunner.h
# All actual tests cpp files added from here
test/unittests/factory/TestFactory.cpp
test/unittests/geometry/TestVec2.cpp
test/unittests/geometry/TestVec3.cpp
test/unittests/physics/TestPhysics.cpp
test/unittests/utils/TestStringUtils.cpp
)
# Third-party dependencies
# Rendering libraries: OpenGL, GLEW, GLFW
# FindOpenGL.cmake
find_package(OpenGL 4.5 REQUIRED) # version requirement does not work, left as a note only
include_directories(${OpenGL_INCLUDE_DIR})
message(STATUS "OpenGL found: ${OPENGL_FOUND}")
message(STATUS "OpenGL xmesa found: ${OPENGL_XMESA_FOUND}")
message(STATUS "OpenGL glu found: ${OPENGL_GLU_FOUND}")
message(STATUS "OpenGL include dir: ${OPENGL_INCLUDE_DIR}")
message(STATUS "OpenGL libraries: ${OPENGL_LIBRARIES}")
message(STATUS "OpenGL gl library: ${OPENGL_gl_LIBRARY}")
# GLEW
set(BUILD_UTILS OFF) # we don't need glewinfo and visualinfo executables
add_subdirectory(third-party/glew-2.1.0/build/cmake)
# GLFW
set(GLFW_BUILD_EXAMPLES OFF)
set(GLFW_BUILD_TESTS OFF)
set(GLFW_BUILD_DOCS OFF)
set(GLFW_INSTALL OFF)
add_subdirectory(third-party/glfw)
# Physics library: Box2D
# Build Box2D with premake with a custom script
# The better way is to prepare an included CMakeLists that does the job locally as the full build command,
# and make sure this step is triggered first via add_subdirectory.
execute_process(COMMAND bash -c
"cd \"${PROJECT_SOURCE_DIR}/third-party/Box2D/Box2D\" &&
premake5 gmake &&
cd \"Build/gmake\" &&
export config=release &&
make Box2D ${MAKEFLAGS}")
# Get the library built with premake
find_library(BOX2D_LIBRARY_RELEASE Box2D DIRECTORY ${PROJECT_SOURCE_DIR}/third-party/Box2D/Box2D/Build/gmake/bin/Release)
set(BOX2D_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/third-party/Box2D)
# GLEW
# it would be better to use find_package locally and get all the include paths at once...
set(GLEW_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/third-party/glew-2.1.0/include)
# GLFW
set(GLFW_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/third-party/glfw/include)
# Targets
# Stealth engine static library
add_library(engine STATIC "${ENGINE_SOURCE_FILES}")
#set_target_properties(engine PROPERTIES
# CXX_STANDARD_REQUIRED 17
# )
target_include_directories(engine
PUBLIC
include
${BOX2D_INCLUDE_DIR}
${GLEW_INCLUDE_DIR}
${GLFW_INCLUDE_DIR}
)
# Scan through resource folder for updated files and copy if none existing or changed
# http://qrikko.blogspot.fr/2016/05/cmake-and-how-to-copy-resources-during.html
message(STATUS "Copying resources from ${PROJECT_SOURCE_DIR}...")
add_copy_command(engine "resources")
# Linking
target_link_libraries(engine
"${OPENGL_gl_LIBRARY}" # We don't need _glu_ with GLEW
"glew_s"
"glfw"
"${BOX2D_LIBRARY_RELEASE}"
)
if(BUILD_TESTS)
if(ENABLE_COVERAGE)
target_compile_options(engine PRIVATE --coverage) # works without, but may be risky
target_link_libraries(engine --coverage)
endif()
# CMake test support
enable_testing()
# add_subdirectory(test) # should include directory with CMakeLists containing block below:
# Test engine executable
add_executable(test_engine ${TEST_ENGINE_SOURCE_FILES})
target_include_directories(test_engine PRIVATE test)
target_link_libraries(test_engine
engine
)
# Allow to run test_engine executable with "make run_test_engine"
add_custom_target(run_test_engine
COMMAND test_engine
DEPENDS test_engine
WORKING_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}
)
# use the full syntax so CMake can recognize the command as the executable defined above and not a raw path
add_test(NAME test_engine COMMAND test_engine)
endif()