-
Notifications
You must be signed in to change notification settings - Fork 531
/
CMakeLists.txt
464 lines (398 loc) · 14.8 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
cmake_minimum_required(VERSION 3.6.0)
#
# MUST be included before project()!
#
include("../../build-tools/cmake/xa_common.cmake")
project(libmonodroid C CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS ON)
set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED ON)
option(ENABLE_NDK "Build with Android's NDK" ON)
option(STRIP_DEBUG "Strip debugging information when linking" OFF)
option(ENABLE_TIMING "Build with timing support" OFF)
option(DISABLE_DEBUG "Disable the built-in debugging code" OFF)
option(ENABLE_CLANG_ASAN "Enable the clang AddressSanitizer support" OFF)
option(ENABLE_CLANG_UBSAN "Enable the clang UndefinedBehaviorSanitizer support" OFF)
include(CheckIncludeFileCXX)
include(CheckCXXSymbolExists)
include(CheckCCompilerFlag)
include(CheckCXXCompilerFlag)
include("../../build-tools/cmake/xa_macros.cmake")
set(JAVA_INTEROP_SRC_PATH "../../external/Java.Interop/src/java-interop")
set(LZ4_SRC_DIR "../../external/lz4/lib")
set(LZ4_INCLUDE_DIR ${LZ4_SRC_DIR})
set(LZ4_SOURCES
"${LZ4_SRC_DIR}/lz4.c"
)
string(REPLACE "\\" "/" TOP_DIR ${CMAKE_SOURCE_DIR})
set(SOURCES_DIR ${TOP_DIR}/jni)
if(NOT DEFINED MONO_PATH)
message(FATAL_ERROR "Please set the MONO_PATH variable on command line (-DMONO_PATH=PATH)")
else()
string(REPLACE "\\" "/" MONO_PATH ${MONO_PATH})
endif()
if(NOT DEFINED CONFIGURATION)
message(FATAL_ERROR "Please set the CONFIGURATION variable on command line (-DCONFIGURATION=name)")
endif()
if(NOT DEFINED CMAKE_BUILD_TYPE)
message(FATAL_ERROR "Please set the CMAKE_BUILD_TYPE variable on command line (-DCMAKE_BUILD_TYPE=name)")
endif()
set(BIN_DIRS ${CMAKE_BUILD_TYPE} ${CONFIGURATION})
foreach(bdir ${BIN_DIRS})
set(BIN_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../bin/${bdir}")
if(EXISTS ${BIN_DIR})
message(STATUS "BIN_DIR exists")
set(DEFAULT_BIN_DIR "${BIN_DIR}")
break()
endif()
endforeach(bdir)
set(XA_LIB_TOP_DIR "${DEFAULT_BIN_DIR}/lib/xamarin.android/xbuild/Xamarin/Android/lib")
if(NOT ANDROID)
if (NOT DEFINED JDK_INCLUDE)
message(FATAL_ERROR "Please set the JDK_INCLUDE variable on command line (-DJDK_INCLUDE)")
endif()
string(REPLACE " " ";" JDK_INCLUDE_LIST ${JDK_INCLUDE})
foreach(inc in ${JDK_INCLUDE_LIST})
include_directories(${inc})
endforeach()
set(MONODROID_SOURCES
${MONODROID_SOURCES}
${SOURCES_DIR}/designer-assemblies.cc
${JAVA_INTEROP_SRC_PATH}/java-interop-gc-bridge-mono.cc
${JAVA_INTEROP_SRC_PATH}/java-interop-jvm.cc
)
endif()
if(MINGW)
if(NOT DEFINED MINGW_DEPENDENCIES_ROOT_DIR)
message(FATAL_ERROR "Please set the MINGW_DEPENDENCIES_ROOT_DIR variable on command line (-DMINGW_DEPENDENCIES_ROOT_DIR=PATH)")
endif()
if(DEFINED MINGW_TARGET_32 AND DEFINED MINGW_TARGET_64)
message(FATAL_ERROR "Only one of the MINGW_TARGET_32 or MINGW_TARGET_64 variables can be defined")
endif()
if(NOT DEFINED MINGW_TARGET_32 AND NOT DEFINED MINGW_TARGET_64)
message(FATAL_ERROR "Either MINGW_TARGET_32 or MINGW_TARGET_64 variable must be defined")
endif()
endif()
xa_common_prepare()
xa_macos_prepare_arm64()
# Don't put the leading '-' in options
set(TEST_COMPILER_ARGS_CPP ${XA_COMPILER_FLAGS}
fno-rtti
fno-exceptions
Wall
Wconversion
Wdeprecated
Wduplicated-branches
Wduplicated-cond
Werror=format-security
Werror=return-type
Wextra
Wformat
Wformat-security
Wmisleading-indentation
Wnull-dereference
Wpointer-arith
Wshadow
Wsign-compare
Wuninitialized
)
if(ENABLE_NDK)
if (ENABLE_CLANG_ASAN OR ENABLE_CLANG_UBSAN)
set(TEST_COMPILER_ARGS_CPP
${TEST_COMPILER_ARGS_CPP}
fno-omit-frame-pointer
fno-optimize-sibling-calls
)
endif()
unset(SANITIZER_FLAGS)
if (ENABLE_CLANG_ASAN)
set(SANITIZER_FLAGS "-fsanitize=address")
set(CHECKED_BUILD_INFIX "-checked+asan")
elseif(ENABLE_CLANG_UBSAN)
set(SANITIZER_FLAGS "-fsanitize=undefined")
set(CHECKED_BUILD_INFIX "-checked+ubsan")
endif()
if(SANITIZER_FLAGS)
set(CHECKED_COMPILER_FLAGS "${CHECKED_COMPILER_FLAGS} ${SANITIZER_FLAGS}")
set(CHECKED_LINKER_FLAGS "${CHECKED_LINKER_FLAGS} ${SANITIZER_FLAGS}")
string(STRIP "${CHECKED_COMPILER_FLAGS}" CHECKED_COMPILER_FLAGS)
string(STRIP "${CHECKED_LINKER_FLAGS}" CHECKED_LINKER_FLAGS)
endif()
endif()
set(TEST_COMPILER_ARGS_C ${XA_COMPILER_FLAGS})
if(CMAKE_BUILD_TYPE STREQUAL Debug)
if(NOT DISABLE_DEBUG)
add_definitions("-DDEBUG=1")
endif()
endif()
foreach(arg ${TEST_COMPILER_ARGS_CPP})
cxx_compiler_has_flag(${arg})
endforeach(arg)
foreach(arg ${TEST_COMPILER_ARGS_C})
c_compiler_has_flag(${arg})
endforeach(arg)
set(TEST_LINKER_ARGS ${XA_LINKER_ARGS})
foreach(arg ${TEST_LINKER_ARGS})
linker_has_flag(${arg})
endforeach(arg)
if(STRIP_DEBUG)
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-S")
endif()
if(ENABLE_NDK)
include_directories(${CMAKE_SYSROOT}/usr/include/c++/v1/)
include_directories(${LZ4_INCLUDE_DIR})
add_definitions("-DHAVE_LZ4")
add_definitions("-DPLATFORM_ANDROID")
add_definitions("-DANDROID")
add_definitions("-DLINUX -Dlinux -D__linux__")
if(ANDROID_ABI MATCHES "^(arm64-v8a|x86_64)")
add_definitions("-DANDROID64")
endif()
if (ANDROID_NDK_MAJOR GREATER 17)
# For some reason NDK 18 (and 19b2 as of this time) doesn't set this *required* macro...
add_definitions("-D__ANDROID_API__=${ANDROID_NATIVE_API_LEVEL}")
endif()
if (ANDROID_NDK_MAJOR LESS 20)
# This is missing from the current NDK releases headers (that includes NDK r19b2)
add_definitions("-D__ANDROID_API_Q__=29")
endif()
set(XA_LIBRARY_OUTPUT_DIRECTORY "${XA_LIB_TOP_DIR}/${ANDROID_ABI}")
link_directories("${XA_LIBRARY_OUTPUT_DIRECTORY}")
else()
set(CMAKE_REQUIRED_DEFINITIONS "-D__USE_GNU")
check_cxx_symbol_exists(gettid unistd.h HAVE_GETTID_IN_UNISTD_H)
if(HAVE_GETTID_IN_UNISTD_H)
add_definitions("-DHAVE_GETTID_IN_UNISTD_H")
endif()
# MinGW needs it for {v,a}sprintf
add_definitions("-D_GNU_SOURCE")
if(APPLE)
add_definitions("-DAPPLE_OS_X")
set(CMAKE_MACOSX_RPATH 1)
set(HOST_BUILD_NAME "host-Darwin")
elseif(CMAKE_HOST_SYSTEM_NAME STREQUAL Linux)
if(NOT MINGW AND NOT WIN32)
add_definitions("-DLINUX -Dlinux -D__linux__")
set(HOST_BUILD_NAME "host-Linux")
if(EXISTS "/.flatpak-info")
add_definitions("-DLINUX_FLATPAK")
endif()
endif()
endif()
if(UNIX)
set(EXTRA_LINKER_FLAGS "${EXTRA_LINKER_FLAGS} -shared -fpic")
endif()
if(WIN32 OR MINGW)
message(STATUS "Win32 or MinGW")
set(EXTRA_COMPILER_FLAGS "${EXTRA_COMPILER_FLAGS} -DWINDOWS -DNTDDI_VERSION=NTDDI_VISTA -D_WIN32_WINNT=_WIN32_WINNT_VISTA -fomit-frame-pointer")
set(EXTRA_LINKER_FLAGS "${EXTRA_LINKER_FLAGS} -lmman -static -pthread -dynamic -lmincore -lmswsock -lwsock32 -lshlwapi -lpsapi -lwinmm")
if (MINGW_TARGET_32)
set(ANDROID_ABI "host-mxe-Win32")
include_directories("${MINGW_DEPENDENCIES_ROOT_DIR}/x86/include")
link_directories("${MINGW_DEPENDENCIES_ROOT_DIR}/x86/lib")
if(CMAKE_HOST_SYSTEM_NAME STREQUAL Darwin)
include_directories("/usr/local/opt/mingw-zlib/usr/i686-w64-mingw32/include")
endif()
elseif (MINGW_TARGET_64)
set(ANDROID_ABI "host-mxe-Win64")
include_directories("${MINGW_DEPENDENCIES_ROOT_DIR}/x86_64/include")
link_directories("${MINGW_DEPENDENCIES_ROOT_DIR}/x86_64/lib")
if(CMAKE_HOST_SYSTEM_NAME STREQUAL Darwin)
include_directories("/usr/local/opt/mingw-zlib/usr/x86_64-w64-mingw32/include")
endif()
endif()
set(XA_LIBRARY_OUTPUT_DIRECTORY "${XA_LIB_TOP_DIR}/${ANDROID_ABI}")
link_directories("${XA_LIBRARY_OUTPUT_DIRECTORY}")
endif()
if(DEFINED HOST_BUILD_NAME)
set(XA_LIBRARY_OUTPUT_DIRECTORY "${XA_LIB_TOP_DIR}/${HOST_BUILD_NAME}")
link_directories("${XA_LIB_TOP_DIR}/${HOST_BUILD_NAME}")
include_directories("${DEFAULT_BIN_DIR}/include/${HOST_BUILD_NAME}")
include_directories("${DEFAULT_BIN_DIR}/include/${HOST_BUILD_NAME}/eglib")
include_directories("../../bin/${CONFIGURATION}/include/${HOST_BUILD_NAME}")
include_directories("../../bin/${CONFIGURATION}/include/${HOST_BUILD_NAME}/eglib")
endif()
endif()
string(STRIP "${EXTRA_LINKER_FLAGS}" EXTRA_LINKER_FLAGS)
if (ENABLE_TIMING)
add_definitions("-DMONODROID_TIMING=1")
endif()
if (WIN32)
include_directories(BEFORE "jni/win32")
endif()
add_definitions("-DHAVE_CONFIG_H")
add_definitions("-D_REENTRANT")
add_definitions("-DJI_DLL_EXPORT")
add_definitions("-DMONO_DLL_EXPORT")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_COMPILER_FLAGS} ${EXTRA_C_FLAGS} ${CHECKED_COMPILER_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EXTRA_COMPILER_FLAGS} ${EXTRA_CXX_FLAGS} ${CHECKED_COMPILER_FLAGS}")
string(STRIP "${CMAKE_C_FLAGS}" CMAKE_C_FLAGS)
string(STRIP "${CMAKE_CXX_FLAGS}" CMAKE_CXX_FLAGS)
if(CMAKE_BUILD_TYPE STREQUAL Debug)
# Convince NDK to really optimize our Debug builds. Without this, NDK's cmake toolchain definition
# will force a -O0 on us and our "debug" build is not really for debugging of our native code but
# rather for "debug" builds of user apps - it has extra code but it has to be as fast as possible.
if(ENABLE_NDK)
# This is specific to clang, enable only for Android builds
set(XA_COMPILER_FLAGS_DEBUG -fno-limit-debug-info)
endif()
set(CMAKE_C_FLAGS_DEBUG "${XA_COMPILER_FLAGS_DEBUG}")
set(CMAKE_CXX_FLAGS_DEBUG "${XA_COMPILER_FLAGS_DEBUG}")
endif()
include_directories("${DEFAULT_BIN_DIR}/include/mono-2.0")
include_directories("jni")
include_directories("${DEFAULT_BIN_DIR}/include")
include_directories("${DEFAULT_BIN_DIR}/include/${ANDROID_ABI}/eglib")
# This is to allow "release" builds with Debug build type and vice versa
include_directories("../../bin/${CONFIGURATION}/include")
include_directories("../../bin/${CONFIGURATION}/include/${ANDROID_ABI}/eglib")
include_directories("${MONO_PATH}/mono/eglib")
include_directories("jni/zip")
include_directories("${JAVA_INTEROP_SRC_PATH}")
if(ENABLE_NDK AND (ENABLE_CLANG_UBSAN OR ENABLE_CLANG_ASAN))
set(OLD_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -llog")
string(STRIP "${CMAKE_REQUIRED_FLAGS}" CMAKE_REQUIRED_FLAGS)
endif()
check_include_file_cxx("linux/netlink.h" HAVE_LINUX_NETLINK_H)
check_include_file_cxx("linux/rtnetlink.h" HAVE_LINUX_RTNETLINK_H)
check_include_file_cxx("linux/if_arp.h" HAVE_LINUX_IF_ARP_H)
if(ENABLE_NDK AND (ENABLE_CLANG_UBSAN OR ENABLE_CLANG_ASAN))
set(CMAKE_REQUIRED_FLAGS "${OLD_CMAKE_REQUIRED_FLAGS}")
endif()
configure_file(jni/host-config.h.in ${CMAKE_CURRENT_BINARY_DIR}/include/host-config.h)
include_directories(${CMAKE_CURRENT_BINARY_DIR}/include/ ${CMAKE_SOURCE_DIR}/include)
set(MONODROID_SOURCES
${MONODROID_SOURCES}
${SOURCES_DIR}/new_delete.cc
${SOURCES_DIR}/android-system.cc
${SOURCES_DIR}/basic-android-system.cc
${SOURCES_DIR}/basic-utilities.cc
${SOURCES_DIR}/cpu-arch-detect.cc
${SOURCES_DIR}/debug.cc
${SOURCES_DIR}/debug-constants.cc
${SOURCES_DIR}/embedded-assemblies.cc
${SOURCES_DIR}/embedded-assemblies-zip.cc
${SOURCES_DIR}/globals.cc
${SOURCES_DIR}/logger.cc
${SOURCES_DIR}/monodroid-glue.cc
${SOURCES_DIR}/xa-internal-api.cc
${SOURCES_DIR}/osbridge.cc
${SOURCES_DIR}/shared-constants.cc
${SOURCES_DIR}/timezones.cc
${SOURCES_DIR}/util.cc
${JAVA_INTEROP_SRC_PATH}/java-interop.cc
${JAVA_INTEROP_SRC_PATH}/java-interop-mono.cc
${JAVA_INTEROP_SRC_PATH}/java-interop-util.cc
${JAVA_INTEROP_SRC_PATH}/java-interop-dlfcn.cc
)
set(XA_INTERNAL_API_SOURCES
${SOURCES_DIR}/jni.c
${SOURCES_DIR}/internal-pinvoke-api.cc
${JAVA_INTEROP_SRC_PATH}/java-interop-util.cc
)
if(ENABLE_NDK)
set(MONODROID_SOURCES
${MONODROID_SOURCES}
${LZ4_SOURCES}
)
endif()
if(UNIX)
set(MONODROID_SOURCES
${MONODROID_SOURCES}
${SOURCES_DIR}/monodroid-networkinfo.cc
${SOURCES_DIR}/xamarin_getifaddrs.cc
)
endif()
set(XA_INTERNAL_API xa-internal-api${CHECKED_BUILD_INFIX})
add_library(${XA_INTERNAL_API} SHARED ${XA_INTERNAL_API_SOURCES})
target_link_libraries(${XA_INTERNAL_API} ${EXTRA_LINKER_FLAGS})
set_target_properties(
${XA_INTERNAL_API}
PROPERTIES
COMPILE_FLAGS -fvisibility=default
LINK_FLAGS -fvisibility=default
)
set(XAMARIN_APP_STUB_SOURCES ${SOURCES_DIR}/application_dso_stub.cc)
add_library(xamarin-app SHARED ${XAMARIN_APP_STUB_SOURCES})
if (ENABLE_NDK)
# Only Android builds need to go in separate directories, desktop builds have the same ABI
set_target_properties(
xamarin-app
PROPERTIES
LIBRARY_OUTPUT_DIRECTORY "${XA_LIBRARY_OUTPUT_DIRECTORY}/${CMAKE_BUILD_TYPE}"
)
elseif(APPLE)
set_target_properties(
xamarin-app
PROPERTIES
OSX_ARCHITECTURES "${XA_OSX_ARCHITECTURES}"
)
endif()
if(NOT WIN32 AND NOT MINGW AND CMAKE_BUILD_TYPE STREQUAL Debug)
set(XAMARIN_DEBUG_APP_HELPER_SOURCES
${SOURCES_DIR}/basic-android-system.cc
${SOURCES_DIR}/basic-utilities.cc
${SOURCES_DIR}/cpu-arch-detect.cc
${SOURCES_DIR}/debug-app-helper.cc
${SOURCES_DIR}/new_delete.cc
${SOURCES_DIR}/shared-constants.cc
)
set(XAMARIN_DEBUG_APP_HELPER xamarin-debug-app-helper${CHECKED_BUILD_INFIX})
add_library(${XAMARIN_DEBUG_APP_HELPER} SHARED ${XAMARIN_DEBUG_APP_HELPER_SOURCES})
target_compile_definitions(
${XAMARIN_DEBUG_APP_HELPER}
PUBLIC -DDEBUG_APP_HELPER
)
if(APPLE)
set_target_properties(
${XAMARIN_DEBUG_APP_HELPER}
PROPERTIES
OSX_ARCHITECTURES "${XA_OSX_ARCHITECTURES}"
)
endif()
target_compile_options(
${XAMARIN_DEBUG_APP_HELPER}
PRIVATE ${CHECKED_COMPILER_FLAGS}
)
endif()
string(TOLOWER ${CMAKE_BUILD_TYPE} MONO_ANDROID_SUFFIX)
set(MONO_ANDROID_LIB "mono-android${CHECKED_BUILD_INFIX}.${MONO_ANDROID_SUFFIX}")
add_library(${MONO_ANDROID_LIB} SHARED ${MONODROID_SOURCES})
if(APPLE)
set_target_properties(
${MONO_ANDROID_LIB}
PROPERTIES
OSX_ARCHITECTURES "${XA_OSX_ARCHITECTURES}"
)
add_custom_command(
TARGET ${MONO_ANDROID_LIB}
POST_BUILD
COMMAND xcrun install_name_tool -change "@rpath/libxamarin-app.dylib" "@loader_path/libxamarin-app.dylib" $<TARGET_FILE:${MONO_ANDROID_LIB}>
)
endif()
if(MINGW_ZLIB_LIBRARY_PATH)
set(LINK_LIBS "-lmonosgen-2.0.dll ${MINGW_ZLIB_LIBRARY_PATH} ${EXTRA_LINKER_FLAGS} ${CHECKED_LINKER_FLAGS}")
else()
set(LINK_LIBS "-lmonosgen-2.0 -lz ${EXTRA_LINKER_FLAGS} ${CHECKED_LINKER_FLAGS}")
endif()
string(STRIP "${LINK_LIBS}" LINK_LIBS)
if(NOT MINGW AND NOT WIN32)
set(DEBUG_HELPER_LINK_LIBS "-ldl")
endif()
if(ENABLE_NDK)
set(LINK_LIBS "${LINK_LIBS} -llog")
set(DEBUG_HELPER_LINK_LIBS "${DEBUG_HELPER_LINK_LIBS} -llog")
elseif(NOT ANDROID AND NOT MINGW AND NOT WIN32)
set(LINK_LIBS "-pthread ${LINK_LIBS} -ldl")
endif()
target_compile_options(
${MONO_ANDROID_LIB}
PRIVATE ${CHECKED_COMPILER_FLAGS}
)
target_link_libraries(${MONO_ANDROID_LIB} ${LINK_LIBS} xamarin-app)
if(NOT WIN32 AND NOT MINGW AND CMAKE_BUILD_TYPE STREQUAL Debug)
target_link_libraries(${XAMARIN_DEBUG_APP_HELPER} ${DEBUG_HELPER_LINK_LIBS})
endif()