-
Notifications
You must be signed in to change notification settings - Fork 2
/
CMakeLists.txt
251 lines (210 loc) · 6.91 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
243
244
245
246
247
248
249
250
251
cmake_minimum_required(VERSION 3.13)
project(ModLoader VERSION 0.1.0)
# Global Settings
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_C_STANDARD 99)
option(MODLOADER_BUILD_EXAMPLES "Whether or not to build examples" OFF)
set(MODLOADER_BEDROCK_PDB "${PROJECT_SOURCE_DIR}/symbols/bedrock_server.pdb" CACHE FILEPATH "Please specify the path to your bedrock_server.pdb file")
set(generated_dir "${CMAKE_CURRENT_BINARY_DIR}/generated")
#
# SymExtract Configuration
#
macro(configure_symextract)
add_executable(SymExtract)
target_include_directories(
SymExtract PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>
$<INSTALL_INTERFACE:include>
)
set(JSON_BuildTests OFF CACHE INTERNAL "")
add_subdirectory(extern/json)
add_subdirectory(extern/cxxopts)
target_link_libraries(SymExtract PRIVATE cxxopts)
target_link_libraries(SymExtract PRIVATE nlohmann_json::nlohmann_json)
add_subdirectory(include/SymExtract)
add_subdirectory(src/SymExtract)
endmacro()
if (EARLY_BUILD)
configure_symextract()
return()
endif()
#
# Run SymExtract Compilation at configuration stage
#
# Setup folder for host architecture, if required (cross-compilation)
if (CMAKE_CROSSCOMPILING)
set(symextract_workdir "${CMAKE_BINARY_DIR}/host/preflight")
else()
set(symextract_workdir "${CMAKE_BINARY_DIR}/preflight")
endif()
if (WIN32)
set(symextract_executable "${symextract_workdir}/SymExtract.exe")
set(symextract_symboltable "${CMAKE_SOURCE_DIR}/symbols/platform_win32.json")
else()
set(symextract_executable "${symextract_workdir}/SymExtract")
set(symextract_symboltable "${CMAKE_SOURCE_DIR}/symbols/platform_unix.json")
endif()
set(symextract_command
"${symextract_executable}"
--schema "${CMAKE_SOURCE_DIR}/symbols/schema.json"
--symbol-names "${symextract_symboltable}"
--input-file "${MODLOADER_BEDROCK_PDB}"
--output-directory "${generated_dir}"
--include-prefix SymExtract
)
macro(symextract_generate)
execute_process(COMMAND ${symextract_command})
endmacro()
# Build SymExtract if not yet built
if (NOT EXISTS "${symextract_executable}")
execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory "${symextract_workdir}")
# Run nested CMake process (configuration)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}"
-DCMAKE_BUILD_TYPE=Release
-DCMAKE_MAKE_PROGRAM=${CMAKE_MAKE_PROGRAM}
-DEARLY_BUILD=ON
${CMAKE_SOURCE_DIR}
WORKING_DIRECTORY "${symextract_workdir}")
# Run nested CMake process (build)
execute_process(COMMAND ${CMAKE_COMMAND} --build . --target SymExtract WORKING_DIRECTORY "${symextract_workdir}")
# Force generate symbols
symextract_generate()
endif()
#
# Generating Symbol files
#
# Configure SymExtract only afterwards
configure_symextract()
#
# Target Declarations
#
add_library(ModLoader SHARED)
add_custom_target(SymbolGeneration COMMAND ${symextract_command})
if (WIN32)
add_executable(PreLoader)
endif ()
target_compile_definitions(ModLoader PRIVATE MODLOADER_API_EXPORT)
target_include_directories(
ModLoader
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>
$<INSTALL_INTERFACE:include>
)
#
# External Libraries
#
add_subdirectory(extern/funchook)
add_subdirectory(extern/spdlog)
target_link_libraries(ModLoader PRIVATE funchook-static spdlog::spdlog)
#
# Source Tree
#
add_subdirectory(include/Minecraft)
add_subdirectory(include/ModLoader)
add_subdirectory(src/ModLoader)
if (WIN32)
add_subdirectory(src/Preloader)
endif ()
# Configuration file
configure_file(
include/ModLoader/Config.h.in
${generated_dir}/ModLoader/Config.h
@ONLY
)
target_sources(
ModLoader
PUBLIC
$<BUILD_INTERFACE:${generated_dir}/ModLoader/Config.h>
$<INSTALL_INTERFACE:include/ModLoader/Config.h>
)
target_include_directories(
ModLoader
PRIVATE
$<BUILD_INTERFACE:${generated_dir}>
$<INSTALL_INTERFACE:include>
)
# Import files generated by SymExtract
file(GLOB_RECURSE symextract_generated_files ${generated_dir}/*.h ${generated_dir}/*.cpp)
target_include_directories(
ModLoader
PRIVATE
$<BUILD_INTERFACE:${generated_dir}/include>
$<INSTALL_INTERFACE:include>
)
target_sources(ModLoader PRIVATE ${symextract_generated_files})
# Private sources
target_include_directories(ModLoader PRIVATE src)
#
# Compilation
#
# Select the release version of MSVC's runtime library
#
# This is done to ensure ABI compatibility when hooking into the ReleaseMode compiled
# bedrock server binary even in Debug builds. If not setup properly, objects such as
# std::string will store additional members and thus screw up the memory layout.
set_target_properties(ModLoader PROPERTIES MSVC_RUNTIME_LIBRARY "MultiThreadedDLL")
# We make this definition public, since mods compiled with this undefined or set to any
# different value will simply _crash_ at runtime. I feel, this is reasoning enough to
# allow the PUBLIC here. And yes, we must set this value ourselves (see above).
target_compile_definitions(ModLoader PUBLIC _ITERATOR_DEBUG_LEVEL=0)
# We need to also compile spdlog with these settings
target_compile_definitions(spdlog PRIVATE _ITERATOR_DEBUG_LEVEL=0)
#
# Examples
#
if (MODLOADER_BUILD_EXAMPLES)
add_subdirectory(examples/simplemod)
endif()
#
# Installation
#
include (GNUInstallDirs)
include (CMakePackageConfigHelpers)
# Create Package Metadata
write_basic_package_version_file(
"${generated_dir}/ModLoaderConfigVersion.cmake"
VERSION ${PROJECT_VERSION}
COMPATIBILITY ExactVersion
)
configure_package_config_file(
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/Config.cmake.in"
"${generated_dir}/ModLoaderConfig.cmake"
INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
)
# Targets
# * ModLoader - the shared library to link to
install(
TARGETS ModLoader
EXPORT ModLoaderTargets
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
)
# Headers
# * ModLoader Headers: include/ModLoader
# * Config.h: include/ModLoader
install(
DIRECTORY "include/ModLoader"
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
FILES_MATCHING PATTERN "*.h"
)
install(
FILES "${generated_dir}/ModLoader/Config.h"
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/ModLoader"
)
# Package Config
# * <prefix>/lib/cmake/ModLoader/ModLoaderConfig.cmake
# * <prefix>/lib/cmake/ModLoader/ModLoaderVersionConfig.cmake
# * Target Exports
install(
FILES
"${generated_dir}/ModLoaderConfig.cmake"
"${generated_dir}/ModLoaderVersionConfig.cmake"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/ModLoader"
)
install(
EXPORT ModLoaderTargets
NAMESPACE ModLoader
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/ModLoader"
)