-
Notifications
You must be signed in to change notification settings - Fork 4
/
CMakeLists.txt
145 lines (123 loc) · 5.31 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
cmake_minimum_required(VERSION 3.25)
project(unrealsdk VERSION 1.4.0)
set(UNREALSDK_UE_VERSION "UE4" CACHE STRING "The unreal engine version to build the SDK for. One of 'UE3' or 'UE4'.")
set(UNREALSDK_ARCH "x64" CACHE STRING "The architecture to build the sdk for. One of 'x86' or 'x64'.")
set(UNREALSDK_SHARED False CACHE BOOL "If set, compiles as a shared library instead of as an object.")
add_library(_unrealsdk_interface INTERFACE)
target_compile_features(_unrealsdk_interface INTERFACE cxx_std_20)
set_target_properties(_unrealsdk_interface PROPERTIES
COMPILE_WARNING_AS_ERROR True
INTERPROCEDURAL_OPTIMIZATION True
)
if(MSVC)
# Under MSVC, enable edit and continue in debug - which conflicts with LTO
set_target_properties(${target_name} PROPERTIES
MSVC_DEBUG_INFORMATION_FORMAT "$<$<CONFIG:Debug>:EditAndContinue>"
INTERPROCEDURAL_OPTIMIZATION $<CONFIG:Release>
)
endif()
if(MSVC)
target_compile_options(_unrealsdk_interface INTERFACE /W4)
else()
target_compile_options(_unrealsdk_interface INTERFACE -Wall -Wextra -Wpedantic)
endif()
# CMake doesn't understand warnings as errors for MinGW yet
if(MINGW)
target_compile_options(_unrealsdk_interface INTERFACE -Werror)
endif()
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
try_compile(supports_format
SOURCE_FROM_CONTENT test.cpp " \
#include <version>\n \
#ifndef __cpp_lib_format\n \
#error\n \
#endif \
"
CXX_STANDARD 20
)
set(CONFIGURE_FILES_DIR "${CMAKE_CURRENT_BINARY_DIR}/configure")
configure_file(
"src/unrealsdk/version.inl.in"
"${CONFIGURE_FILES_DIR}/unrealsdk/version.inl"
)
set(GIT_PRE_CONFIGURE_FILE "src/unrealsdk/git.inl.in")
set(GIT_POST_CONFIGURE_FILE "${CONFIGURE_FILES_DIR}/unrealsdk/git.inl")
include(common_cmake/git_watcher.cmake)
add_subdirectory(libs/minhook)
if(NOT supports_format)
add_subdirectory(libs/fmt)
endif()
# If using the clang windows cross compilation toolchain
if((NOT CMAKE_HOST_WIN32)
AND (CMAKE_SYSTEM_NAME STREQUAL "Windows")
AND (CMAKE_CXX_COMPILER_ID STREQUAL "Clang"))
# Disable minhook intrinsics - MSVC implicitly defines intrinsics, but Clang does not, so we get
# a linking error otherwise
target_compile_definitions(minhook PRIVATE MINHOOK_DISABLE_INTRINSICS)
endif()
target_include_directories(_unrealsdk_interface INTERFACE "src" ${CONFIGURE_FILES_DIR})
target_link_libraries(_unrealsdk_interface INTERFACE minhook)
if(NOT ${supports_format})
target_link_libraries(_unrealsdk_interface INTERFACE fmt)
endif()
target_compile_definitions(_unrealsdk_interface INTERFACE
"${UNREALSDK_UE_VERSION}"
"ARCH_$<UPPER_CASE:${UNREALSDK_ARCH}>"
"$<$<BOOL:${UNREALSDK_SHARED}>:UNREALSDK_SHARED>"
)
target_precompile_headers(_unrealsdk_interface INTERFACE "src/unrealsdk/pch.h")
# How we include sources depends on what sort of library we're building as
# Including this in the interface target would force them to be public
file(GLOB_RECURSE sources CONFIGURE_DEPENDS "src/unrealsdk/*.cpp" "src/unrealsdk/*.h")
list(APPEND sources ${GIT_POST_CONFIGURE_FILE})
if(NOT UNREALSDK_SHARED)
add_library(unrealsdk OBJECT)
target_link_libraries(unrealsdk PUBLIC _unrealsdk_interface)
set_target_properties(unrealsdk PROPERTIES
EXPORT_COMPILE_COMMANDS True
)
# If a static library, we can keep sources private
target_sources(unrealsdk PRIVATE ${sources})
else()
add_library(unrealsdk SHARED)
target_link_libraries(unrealsdk PUBLIC _unrealsdk_interface)
set_target_properties(unrealsdk PROPERTIES
EXPORT_COMPILE_COMMANDS True
PREFIX ""
)
# If a shared library, make sources public
# Because we only expose the bare minimum functionality which relies on an internal state,
# anything linking against us needs to statically compile the rest - making the sources public
# is an easy way to accomplish that, without even needing another `unrealsdk_shared` target.
# This does mean they get compiled twice, but we can't easily avoid that, since the two passes
# use different values for `UNREALSDK_EXPORTING` anyway.
target_sources(unrealsdk PUBLIC ${sources})
file(GLOB_RECURSE shared_sources CONFIGURE_DEPENDS "src/shared/*.cpp" "src/shared/*.h")
target_sources(unrealsdk PRIVATE
${shared_sources}
"src/shared/versioninfo.rc"
)
# Unconditionally add the exporting flag
# Add it privately, so it doesn't appear in anything linking against this
target_compile_definitions(unrealsdk PRIVATE "UNREALSDK_EXPORTING")
# Postbuild
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
set(POSTBUILD_SCRIPT "postbuild")
if(CMAKE_HOST_WIN32)
set(POSTBUILD_SCRIPT "${POSTBUILD_SCRIPT}.bat")
endif()
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${POSTBUILD_SCRIPT}")
add_custom_command(
TARGET unrealsdk
POST_BUILD
COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/${POSTBUILD_SCRIPT}"
ARGS
"$<SHELL_PATH:$<TARGET_FILE:unrealsdk>>"
"${UNREALSDK_UE_VERSION}"
"${UNREALSDK_ARCH}"
"$<IF:$<CONFIG:DEBUG>,DEBUG,RELEASE>"
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
endif()
endif()
endif()