-
Notifications
You must be signed in to change notification settings - Fork 33
/
CMakeLists.txt
252 lines (215 loc) · 9.17 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
252
cmake_minimum_required(VERSION 3.14)
# INCLUDE_DIRECTORIES $(VC_IncludePath);$(WindowsSDK_IncludePath)
cmake_policy(SET CMP0021 OLD)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/")
include(pkg-utils)
pkg_get_version(CRTSYS ${CMAKE_CURRENT_SOURCE_DIR}/include/.internal/version VERSION)
project(
crtsys
VERSION "${VERSION}"
DESCRIPTION "C Runtime Lbrary for Windows driver file (*.sys)"
HOMEPAGE_URL "https://github.com/ntoskrnl7/crtsys"
LANGUAGES C CXX ASM_MASM
)
option(CRTSYS_NTL_MAIN "Set to ON to use ntl::main" ON)
option(CRTSYS_USE_LIBCNTPR "Set to ON to use libcntpr" ON)
# x64 is always set to use libcntpr. (_NLG_Notify, _NLG_Return2)
if ("${CMAKE_VS_PLATFORM_NAME}" STREQUAL "x64")
set(CRTSYS_USE_LIBCNTPR ON)
endif()
if(MSVC_TOOLSET_VERSION EQUAL 142 OR MSVC_TOOLSET_VERSION EQUAL 143)
set(UCXXRT_ENABLED OFF)
else()
set(UCXXRT_ENABLED ON)
endif()
include(cmake/CPM.cmake)
if ("${CMAKE_VS_PLATFORM_NAME}" STREQUAL "x64")
set(LDK_USE_RAISE_EXCEPTION ON)
set(LDK_USE_RTL_RAISE_EXCEPTION ON)
else()
set(LDK_USE_RAISE_EXCEPTION OFF)
set(LDK_USE_RTL_RAISE_EXCEPTION OFF)
endif()
CPMAddPackage("gh:ntoskrnl7/Ldk@0.7.5")
CPMAddPackage("gh:ntoskrnl7/FindWDK#master")
list(APPEND CMAKE_MODULE_PATH "${FindWDK_SOURCE_DIR}/cmake")
find_package(WDK REQUIRED)
#
# Add source files.
#
file(GLOB SOURCE_FILES
src/*.cpp
# misc
src/custom/misc/*.cpp
src/custom/misc/*.c
# crt
src/custom/crt/*.c
src/custom/crt/fenv/*.c
# ucrt
src/custom/ucrt/common/*.cpp
src/custom/ucrt/common/*/*.cpp
# ucrt
src/custom/ucrt/${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION}/*.cpp
src/custom/ucrt/${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION}/*/*.cpp
)
if("${CMAKE_VS_PLATFORM_NAME}" STREQUAL "Win32")
file(GLOB MATH_SOURCE_FILES
src/custom/crt/math/*.c
)
list(APPEND SOURCE_FILES ${MATH_SOURCE_FILES})
else()
file(GLOB MATH_SOURCE_FILES
src/custom/crt/math/nextafter.c
src/custom/crt/math/fpclassify.c
)
list(APPEND SOURCE_FILES ${MATH_SOURCE_FILES})
endif()
if (UCXXRT_ENABLED)
list(APPEND SOURCE_FILES src/custom/msvc/common/crt/src/stl/winapisupp.cpp)
list(REMOVE_ITEM SOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/src/custom/crt/math/ftol3.c)
CPMAddPackage("gh:ntoskrnl7/ucxxrt#master")
file(GLOB_RECURSE UCXXRT_SOURCE_FILES
${ucxxrt_SOURCE_DIR}/src/*.cpp
${ucxxrt_SOURCE_DIR}/vcruntime/*.cpp
${ucxxrt_SOURCE_DIR}/misc/*.cpp
)
list(REMOVE_ITEM UCXXRT_SOURCE_FILES ${ucxxrt_SOURCE_DIR}/src/vcruntime/crt.cpp)
list(REMOVE_ITEM UCXXRT_SOURCE_FILES ${ucxxrt_SOURCE_DIR}/src/vcruntime/locales.cpp)
list(REMOVE_ITEM UCXXRT_SOURCE_FILES ${ucxxrt_SOURCE_DIR}/src/vcruntime/thread.cpp)
if(NOT "${CMAKE_VS_PLATFORM_NAME}" STREQUAL "Win32" AND NOT "${CMAKE_VS_PLATFORM_NAME}" STREQUAL "x64" )
list(REMOVE_ITEM UCXXRT_SOURCE_FILES ${ucxxrt_SOURCE_DIR}/src/vcruntime/x86_exception_filter.cpp)
endif()
list(REMOVE_ITEM UCXXRT_SOURCE_FILES ${ucxxrt_SOURCE_DIR}/src/cpu_disp.cpp)
list(REMOVE_ITEM UCXXRT_SOURCE_FILES ${ucxxrt_SOURCE_DIR}/src/terminate.cpp)
list(REMOVE_ITEM UCXXRT_SOURCE_FILES ${ucxxrt_SOURCE_DIR}/src/misc/invalid_parameter.cpp)
list(REMOVE_ITEM UCXXRT_SOURCE_FILES ${ucxxrt_SOURCE_DIR}/src/sys_main.cpp)
list(REMOVE_ITEM UCXXRT_SOURCE_FILES ${ucxxrt_SOURCE_DIR}/src/unittest.cpp)
if("${CMAKE_VS_PLATFORM_NAME}" STREQUAL "x64")
file(GLOB PLATFORM_SOURCE_FILES
${ucxxrt_SOURCE_DIR}/src/x64/*.cpp
${ucxxrt_SOURCE_DIR}/src/x64/*.asm
)
elseif("${CMAKE_VS_PLATFORM_NAME}" STREQUAL "Win32")
file(GLOB PLATFORM_SOURCE_FILES
${ucxxrt_SOURCE_DIR}/src/i386/*.cpp
${ucxxrt_SOURCE_DIR}/src/i386/*.asm
)
endif()
list(APPEND UCXXRT_SOURCE_FILES ${PLATFORM_SOURCE_FILES})
list(APPEND SOURCE_FILES ${UCXXRT_SOURCE_FILES})
endif()
wdk_add_library(
crtsys
EXTENDED_CPP_FEATURES
STATIC
${SOURCE_FILES}
)
#
# Add include directories
#
get_target_property(INC_DIR_TMP crtsys INCLUDE_DIRECTORIES)
if (UCXXRT_ENABLED)
set_property(TARGET crtsys PROPERTY INCLUDE_DIRECTORIES "${CMAKE_CURRENT_SOURCE_DIR}/src/custom/winsdk/include/um/crtsys;${Ldk_SOURCE_DIR}/include/Ldk;${ucxxrt_SOURCE_DIR}/src;${ucxxrt_SOURCE_DIR}/src/vcruntime;$(VC_IncludePath);$(WindowsSDK_IncludePath);${INC_DIR_TMP}")
target_include_directories(crtsys PRIVATE "${ucxxrt_SOURCE_DIR}")
else()
set_property(TARGET crtsys PROPERTY INCLUDE_DIRECTORIES "${CMAKE_CURRENT_SOURCE_DIR}/src/custom/winsdk/include/um/crtsys;${Ldk_SOURCE_DIR}/include/Ldk;$(VC_IncludePath);$(WindowsSDK_IncludePath);${INC_DIR_TMP}")
endif()
target_include_directories(crtsys PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/src/custom/msvc/${MSVC_TOOLSET_VERSION}/include")
target_include_directories(crtsys PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/src/custom/msvc/${MSVC_TOOLSET_VERSION}/crt/src/vcruntime")
target_include_directories(crtsys PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/src/custom/msvc/${MSVC_TOOLSET_VERSION}/crt/src/stl")
target_include_directories(crtsys BEFORE PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/src/custom/ucrt/${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION}/inc")
#
# link libraries
#
target_link_libraries(crtsys PRIVATE $<BUILD_INTERFACE:Ldk>)
if(CRTSYS_USE_LIBCNTPR)
target_link_libraries(crtsys PRIVATE WDK::LIBCNTPR)
endif()
#
# Add compile definitions
#
target_compile_definitions(crtsys PUBLIC "_KERNEL32_")
target_compile_definitions(crtsys PUBLIC "_ITERATOR_DEBUG_LEVEL=0")
if (CRTSYS_USE_LIBCNTPR)
target_compile_definitions(crtsys PUBLIC "CRTSYS_USE_LIBCNTPR")
if ("${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION}" VERSION_GREATER_EQUAL "10.0.22000.0")
target_compile_definitions(crtsys PRIVATE "CRTSYS_NEED_CRT_IOB_FUNC")
target_compile_definitions(crtsys PRIVATE "CRTSYS_NEED_CRT_ABORT")
endif()
else()
target_compile_definitions(crtsys PRIVATE "CRTSYS_NEED_CRT_IOB_FUNC")
target_compile_definitions(crtsys PRIVATE "CRTSYS_NEED_CRT_ABORT")
endif()
if (CRTSYS_NTL_MAIN)
target_compile_definitions(crtsys PUBLIC CRTSYS_USE_NTL_MAIN)
endif()
target_compile_definitions(crtsys PUBLIC "_HAS_EXCEPTIONS")
if (UCXXRT_ENABLED)
target_compile_definitions(crtsys PUBLIC "UCXXRT")
target_compile_definitions(crtsys PRIVATE "__KERNEL_MODE")
endif()
#
# Forced Include File
#
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/include/.internal/winsdk/${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION}/wdk/${WDK_VERSION}/forced.h")
target_compile_options(crtsys PRIVATE /FI"${CMAKE_CURRENT_SOURCE_DIR}/include/.internal/winsdk/${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION}/wdk/${WDK_VERSION}/forced.h")
endif()
target_compile_options(crtsys PRIVATE /FI"${CMAKE_CURRENT_SOURCE_DIR}/src/crtsys.h")
if (UCXXRT_ENABLED)
target_compile_options(crtsys PRIVATE /FI"${ucxxrt_SOURCE_DIR}/ucxxrt.inl")
endif()
#
# TLS isn't supported yet.
#
target_compile_options(crtsys PUBLIC /Zc:threadSafeInit-)
target_compile_options(crtsys PUBLIC "/MT") # "/MT$<$<CONFIG:Debug>:d>"
# [not working] set_property(TARGET crtsys PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
#
# Generate Debug Info
#
target_compile_options(crtsys PUBLIC "$<$<CONFIG:Release>:/Zi>")
target_link_options(crtsys PUBLIC "$<$<CONFIG:Release>:/DEBUG>")
#
# Force File Output
#
if (CRTSYS_USE_LIBCNTPR)
# 10.0.17763.0
# 10.0.22000.0
# km/x64/libcntpr.lib(fpexcept.obj) : error LNK2005: RaiseException already defined in Ldk.lib(errhandlingapi.obj)
# 10.0.17763.0
# km/x64/ntoskrnl.lib(ntoskrnl.exe) : error LNK2005: _wcslen already defined in km/x64/libcntpr.lib(wcslen.obj)
# 10.0.17763.0
# libcntpr.lib fflush
# src\custom\ucrt\common\stdio\fflush.cpp fflush
target_link_options(crtsys PUBLIC "/FORCE:MULTIPLE")
elseif(UCXXRT_ENABLED AND "${CMAKE_VS_PLATFORM_NAME}" STREQUAL "x64")
# ntoskrnl.lib(ntoskrnl.exe) : error LNK2005: RtlRaiseException already defined in Ldk.lib(except.obj).
target_link_options(crtsys PUBLIC "/FORCE:MULTIPLE")
endif()
if("${CMAKE_VS_PLATFORM_NAME}" STREQUAL "Win32")
# crtsys.lib(lowhelpr.obj) : error LNK2026: module unsafe for SAFESEH image.
target_link_options(crtsys PUBLIC "/SAFESEH:NO")
# Remove Runtime Checks
string(REGEX REPLACE "/RTC(su|[1su])" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
string(REGEX REPLACE "/RTC(su|[1su])" "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
string(REGEX REPLACE "/RTC(su|[1su])" "" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
string(REGEX REPLACE "/RTC(su|[1su])" "" CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL}")
string(REGEX REPLACE "/RTC(su|[1su])" "" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
endif()
#
# set visual studio properties file
#
if (UCXXRT_ENABLED)
list(APPEND VS_PROPS "${CMAKE_CURRENT_SOURCE_DIR}/src/ucrt_for_ucxxrt.props")
else()
list(APPEND VS_PROPS "${CMAKE_CURRENT_SOURCE_DIR}/src/vcruntime.props;${CMAKE_CURRENT_SOURCE_DIR}/src/custom/msvc/crt.props;${CMAKE_CURRENT_SOURCE_DIR}/src/ucrt.props")
endif()
set_target_properties(crtsys PROPERTIES VS_USER_PROPS "${CMAKE_CURRENT_SOURCE_DIR}/src/stl.props;${VS_PROPS}")
#
# set library output path (release only)
#
string(TOLOWER "${CMAKE_VS_PLATFORM_NAME}" PLATFROM_SHORT_NAME)
if(PLATFROM_SHORT_NAME STREQUAL "win32")
set(PLATFROM_SHORT_NAME "x86")
endif()
set_target_properties(crtsys PROPERTIES ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${CMAKE_CURRENT_SOURCE_DIR}/lib/${PLATFROM_SHORT_NAME})