Debian Codegraph @

CMakeLists.txt

raw
1 cmake_minimum_required(VERSION 3.20) # For using CMAKE_<LANG>_BYTE_ORDER
2
3 # set default cmake build type to Debug (None Debug Release RelWithDebInfo MinSizeRel)
4 if(NOT CMAKE_BUILD_TYPE AND NOT DEFINED ENV{CMAKE_BUILD_TYPE})
5 set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "default build type")
6 endif()
7
8 option(FORCE_COLORED_OUTPUT "Always produce ANSI-colored compiler warnings/errors (GCC/Clang only; esp. useful with Ninja)." OFF)
9 option(FORCE_PORTABLE_INSTALL "Install all files into local directory defined by CMAKE_INSTALL_PREFIX" ON)
10 option(ENABLE_LOGGER "Enable logging to the terminal" OFF)
11 option(ENABLE_MEM_RTL "Enable Real-time library memory management functions (disable to verbose memory allocations)" ON)
12 option(BUILD_TESTING "Enable testing. Requires GTest." OFF)
13 set(USE_VCPKG "DEFAULT" CACHE STRING "Use vcpkg for dependency management. DEFAULT defers to existence of $VCPKG_ROOT environment variable.")
14 set_property(CACHE USE_VCPKG PROPERTY STRINGS "DEFAULT" "ON" "OFF")
15
16 if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
17 option(BUILD_EDITOR "Build internal editor" OFF)
18 endif()
19
20 if(USE_VCPKG)
21 if(DEFINED ENV{VCPKG_ROOT})
22 if (CMAKE_TOOLCHAIN_FILE)
23 cmake_path(ABSOLUTE_PATH CMAKE_TOOLCHAIN_FILE NORMALIZE OUTPUT_VARIABLE VCPKG_CHAINLOAD_TOOLCHAIN_FILE)
24 endif()
25
26 set(VCPKG_TOOLCHAIN_FILE "scripts/buildsystems/vcpkg.cmake")
27 cmake_path(ABSOLUTE_PATH VCPKG_TOOLCHAIN_FILE BASE_DIRECTORY $ENV{VCPKG_ROOT} NORMALIZE OUTPUT_VARIABLE CMAKE_TOOLCHAIN_FILE)
28
29 if(CMAKE_TOOLCHAIN_FILE STREQUAL VCPKG_CHAINLOAD_TOOLCHAIN_FILE)
30 # prevent endless recursion
31 unset(VCPKG_CHAINLOAD_TOOLCHAIN_FILE)
32 endif()
33 elseif(NOT USE_VCPKG STREQUAL "DEFAULT")
34 message(WARNING "USE_VCPKG=${USE_VCPKG} but ENV{VCPKG_ROOT} not set; will use system-provided libraries. Did you forget to set VCPKG_ROOT in your environment?")
35 endif()
36 endif()
37
38 project(Descent3
39 LANGUAGES C CXX
40 VERSION 1.5.0
41 )
42
43 set(CMAKE_CXX_STANDARD 17)
44 set(CMAKE_CXX_STANDARD_REQUIRED ON)
45 set(CMAKE_POSITION_INDEPENDENT_CODE ON)
46 set(CMAKE_CXX_EXTENSIONS OFF)
47 set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
48
49 set_property(GLOBAL PROPERTY USE_FOLDERS ON)
50
51 if(MSVC)
52 add_compile_options(/source-charset:UTF-8 /execution-charset:UTF-8)
53 else()
54 add_compile_options(-finput-charset=UTF-8)
55 # Unfortunately, Clang doesn’t support -fexec-charset yet so this next part
56 # is GCC only. Luckily, Clang defaults to using UTF-8 for the execution
57 # character set [1], so we’re fine. Once Clang gets support for
58 # -fexec-charset, we should probably start using it.
59 #
60 # [1]: <https://discourse.llvm.org/t/rfc-enabling-fexec-charset-support-to-llvm-and-clang-reposting/71512>
61 if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
62 add_compile_options(-fexec-charset=UTF-8)
63 endif()
64 endif()
65
66 if(FORCE_COLORED_OUTPUT)
67 if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.24)
68 set(CMAKE_COLOR_DIAGNOSTICS ON)
69 else()
70 if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
71 add_compile_options(-fdiagnostics-color=always)
72 elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
73 add_compile_options(-fcolor-diagnostics)
74 endif()
75 endif()
76 endif()
77
78 include(GNUInstallDirs)
79 if(FORCE_PORTABLE_INSTALL)
80 set(CMAKE_INSTALL_BINDIR ".")
81 set(CMAKE_INSTALL_LIBDIR ".")
82 set(CMAKE_INSTALL_DATADIR ".")
83 set(CMAKE_INSTALL_DOCDIR ".")
84 endif()
85
86 if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
87 set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/installed" CACHE PATH "Default install path" FORCE)
88 endif()
89
90 if(CMAKE_CXX_BYTE_ORDER STREQUAL "BIG_ENDIAN")
91 message(STATUS "Big Endian system detected.")
92 add_compile_definitions("OUTRAGE_BIG_ENDIAN")
93 endif()
94
95
96 # 64 bit machines have a different game checksum than 32 bit machines
97 if (CMAKE_SIZEOF_VOID_P EQUAL 8)
98 add_definitions(-DCHECKSUM=2273889835UL)
99 else()
100 add_definitions(-DCHECKSUM=2273873307UL)
101 endif()
102
103 if(BUILD_TESTING)
104 find_package(GTest REQUIRED)
105 enable_testing()
106 include(GoogleTest)
107 add_subdirectory(tests)
108 endif()
109
110 # rebuild d3_version.h every time
111 add_custom_target(get_git_hash ALL)
112
113 add_custom_command(
114 TARGET get_git_hash
115 COMMAND ${CMAKE_COMMAND}
116 -D SOURCE_DIR=${PROJECT_SOURCE_DIR}
117 -D TARGET_DIR=${PROJECT_BINARY_DIR}
118 -D PROJECT_VERSION_MAJOR=${PROJECT_VERSION_MAJOR}
119 -D PROJECT_VERSION_MINOR=${PROJECT_VERSION_MINOR}
120 -D PROJECT_VERSION_PATCH=${PROJECT_VERSION_PATCH}
121 -P ${PROJECT_SOURCE_DIR}/cmake/CheckGit.cmake
122 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
123 )
124
125 install(
126 FILES CHANGELOG.md USAGE.md README.md BUILD.md LICENSE THIRD_PARTY.md
127 DESTINATION ${CMAKE_INSTALL_DOCDIR}
128 )
129
130 if(CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
131 add_compile_options("-Wno-multichar;-Wall")
132 endif()
133
134 find_package(SDL2 REQUIRED)
135 # Some versions of the SDL2 find_package set SDL2_INCLUDE_DIR and some set a plural SDL2_INCLUDE_DIRS. Check both.
136 message("SDL2 Include Dir is ${SDL2_INCLUDE_DIR} ${SDL2_INCLUDE_DIRS}")
137
138
139 if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
140 message("Building for Linux")
141 add_compile_definitions(POSIX __LINUX__ _USE_OGL_ACTIVE_TEXTURES PRIMARY_HOG=\"d3-linux.hog\")
142 set(PLATFORM_INCLUDES "lib/linux" ${SDL2_INCLUDE_DIR} ${SDL2_INCLUDE_DIRS})
143 elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
144 message("Building for MAC OSX")
145 add_compile_definitions(POSIX MACOSX=1 _USE_OGL_ACTIVE_TEXTURES PRIMARY_HOG=\"d3-osx.hog\")
146 set(PLATFORM_INCLUDES "lib/linux" ${SDL2_INCLUDE_DIR} ${SDL2_INCLUDE_DIRS})
147 elseif(CMAKE_SYSTEM_NAME STREQUAL "Windows")
148 # Windows.h defines to avoid as many issues as possible.
149 add_compile_definitions(WIN32_LEAN_AND_MEAN NOMINMAX NODRAWTEXT NOBITMAP NOMCX NOSERVICE PRIMARY_HOG=\"d3-win.hog\"
150 #[[NOGDI]] # We need GDI for now, enable when GDI is actually not needed (or when all windows.h mentions are gone)
151 )
152
153 list(APPEND CMAKE_LIBRARY_PATH "lib/win" "lib/win/DirectX/${CMAKE_CXX_COMPILER_ARCHITECTURE_ID}")
154 add_compile_options("$<$<AND:$<CONFIG:Debug>,$<COMPILE_LANGUAGE:CXX>>:/EHsc;/RTC1;/W3;/nologo;/c;/Zi;/TP;/errorReport:prompt>")
155 add_compile_options("$<$<AND:$<CONFIG:Release>,$<COMPILE_LANGUAGE:CXX>>:/GL;/FD;/EHsc;/W3;/nologo;/c;/Zi;/TP;/errorReport:prompt>")
156
157 add_link_options("/SAFESEH:NO;/NODEFAULTLIB:LIBC")
158 add_link_options("$<$<CONFIG:Release>:/LTCG>")
159
160 add_compile_definitions(WIN32 _CRT_SECURE_NO_WARNINGS _CRT_SECURE_NO_DEPRECATE _CRT_NONSTDC_NO_DEPRECATE)
161
162 set(PLATFORM_INCLUDES "lib/win/DirectX" "lib/win")
163
164 set(CMAKE_FIND_LIBRARY_PREFIXES "")
165 set(CMAKE_FIND_LIBRARY_SUFFIXES ".lib")
166
167 add_compile_definitions(DIRECTINPUT_VERSION=0x0500)
168 find_library(DSOUND_LIBRARY NAMES dsound REQUIRED)
169 find_library(DINPUT_LIBRARY NAMES dinput REQUIRED)
170 find_library(DXGUID_LIBRARY NAMES dxguid REQUIRED)
171 find_library(DDRAW_LIBRARY NAMES ddraw REQUIRED)
172
173 if(MSVC AND CMAKE_CXX_SIMULATE_ID STREQUAL "")
174 add_compile_options("/we4150") # deletion of pointer to incomplete type 'type'; no destructor called
175 # add_compile_options("/we4305") # truncation from 'X' to 'Y'
176 add_compile_options("/we4474") # too many arguments passed for format string
177 add_compile_options("/we4700") # uninitialized local variable 'X' used
178 add_compile_options("/we4804") # unsafe use of type 'bool' in operation
179 add_compile_options("/we4806") # unsafe operation: no value of type 'bool' promoted to type 'int' can equal the given constant
180 add_compile_options("/we4473") # not enough arguments passed for format string
181 add_compile_options("/we4477") # format string requires argument of type X but variadic argument Y has type Z
182 add_compile_options("/we4715") # 'function' : not all control paths return a value
183 add_compile_options("/we4834") # discarding return value of function with [[nodiscard]] attribute
184 endif()
185
186 add_compile_options("/MP") # so msbuild builds with multiple processes
187 endif()
188
189 add_compile_definitions($<$<CONFIG:Release>:RELEASE>)
190 add_compile_definitions($<$<CONFIG:Debug>:_DEBUG>)
191
192 find_package(ZLIB REQUIRED)
193
194 if(ENABLE_LOGGER)
195 message("Enabling Logging")
196 add_compile_definitions(LOGGER)
197 endif()
198
199 include_directories(
200 "cfile" # TODO: Remove after untying all modules
201 "ddebug" # -*-
202 "fix" # -*-
203 "lib" # TODO: Remove after untying all modules
204 "linux" # -*-
205 "Descent3"
206 ${PLATFORM_INCLUDES}
207 )
208
209 add_subdirectory(third_party)
210
211 add_subdirectory(2dlib)
212 add_subdirectory(AudioEncode)
213 add_subdirectory(bitmap)
214 add_subdirectory(cfile)
215 add_subdirectory(ddebug)
216
217 if(BUILD_EDITOR AND CMAKE_SYSTEM_NAME STREQUAL "Windows")
218 add_subdirectory(dd_grwin32)
219 add_subdirectory(win32)
220 endif()
221
222 add_subdirectory(linux)
223
224 add_subdirectory(ddio)
225 add_subdirectory(dd_video)
226 add_subdirectory(fix)
227 add_subdirectory(manage)
228 add_subdirectory(grtext)
229 add_subdirectory(mem)
230 add_subdirectory(misc)
231 add_subdirectory(model)
232 add_subdirectory(module)
233 add_subdirectory(music)
234 add_subdirectory(networking)
235 add_subdirectory(physics)
236 add_subdirectory(renderer)
237 add_subdirectory(rtperformance)
238 add_subdirectory(sndlib)
239 add_subdirectory(stream_audio)
240 add_subdirectory(ui)
241 add_subdirectory(unzip)
242 add_subdirectory(vecmat)
243 add_subdirectory(libmve)
244 add_subdirectory(md5)
245
246 if(BUILD_EDITOR AND CMAKE_SYSTEM_NAME STREQUAL "Windows")
247 add_subdirectory(editor)
248 endif()
249
250 add_subdirectory(Descent3)
251
252 add_subdirectory(tools)
253 add_subdirectory(netcon)
254 add_subdirectory(netgames)
255 add_subdirectory(scripts)
256
257 # Packaging stuff
258 set(CPACK_SOURCE_GENERATOR "TXZ")
259 set(CPACK_SOURCE_IGNORE_FILES
260 ".git/"
261 ".github/"
262 ".idea"
263 build[s]/
264 cmake-build-*
265 ".*~$"
266 )
267 set(CPACK_VERBATIM_VARIABLES YES)
268 include(CPack)
269