Skip to content

Commit cab9449

Browse files
committed
build: improve cross-compilation for android
The sysroot headers for android require some definitions to be altered. Ensure that we do not define `_GNU_SOURCE` for Android.
1 parent 0d0a998 commit cab9449

File tree

5 files changed

+46
-9
lines changed

5 files changed

+46
-9
lines changed

CMakeLists.txt

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ if(ENABLE_SWIFT)
6060
${SWIFT_RUNTIME_LIBDIR}/${CMAKE_SHARED_LIBRARY_PREFIX}swiftSwiftOnoneSupport${CMAKE_SHARED_LIBRARY_SUFFIX})
6161
endif()
6262

63+
if(CMAKE_SYSTEM_NAME STREQUAL Android)
64+
set(ENABLE_DTRACE_DEFAULT OFF)
65+
else()
66+
set(ENABLE_DTRACE_DEFAULT ON)
67+
endif()
68+
option(ENABLE_DTRACE "enable dtrace support" ${ENABLE_DTRACE_DEFAULT})
69+
6370
option(BUILD_SHARED_LIBS "build shared libraries" ON)
6471

6572
option(ENABLE_TESTING "build libdispatch tests" ON)
@@ -165,10 +172,8 @@ check_include_files("libproc_internal.h" HAVE_LIBPROC_INTERNAL_H)
165172
check_include_files("mach/mach.h" HAVE_MACH)
166173
if(HAVE_MACH)
167174
set(__DARWIN_NON_CANCELABLE 1)
168-
set(USE_MACH_SEM 1)
169175
else()
170176
set(__DARWIN_NON_CANCELABLE 0)
171-
set(USE_MACH_SEM 0)
172177
endif()
173178
check_include_files("malloc/malloc.h" HAVE_MALLOC_MALLOC_H)
174179
check_include_files("memory.h" HAVE_MEMORY_H)
@@ -187,11 +192,19 @@ check_include_files("sys/types.h" HAVE_SYS_TYPES_H)
187192
check_include_files("unistd.h" HAVE_UNISTD_H)
188193
check_include_files("objc/objc-internal.h" HAVE_OBJC)
189194

190-
check_library_exists(pthread sem_init "" USE_POSIX_SEM)
195+
if(HAVE_MACH)
196+
set(USE_MACH_SEM 1)
197+
else()
198+
set(USE_MACH_SEM 0)
199+
endif()
191200
if(CMAKE_SYSTEM_NAME STREQUAL Windows)
192-
add_definitions(-DTARGET_OS_WIN32)
193201
add_definitions(-DUSE_WIN32_SEM)
194202
endif()
203+
check_library_exists(pthread sem_init "" USE_POSIX_SEM)
204+
# NOTE: android has not always provided a libpthread, but uses the pthreads API
205+
if(CMAKE_SYSTEM_NAME STREQUAL Android)
206+
set(USE_POSIX_SEM 1)
207+
endif()
195208

196209
check_symbol_exists(CLOCK_UPTIME "time.h" HAVE_DECL_CLOCK_UPTIME)
197210
check_symbol_exists(CLOCK_UPTIME_FAST "time.h" HAVE_DECL_CLOCK_UPTIME_FAST)
@@ -213,8 +226,15 @@ check_symbol_exists(VQ_VERYLOWDISK "sys/mount.h" HAVE_DECL_VQ_VERYLOWDISK)
213226

214227
check_symbol_exists(program_invocation_name "errno.h" HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME)
215228

216-
find_program(dtrace_EXECUTABLE dtrace)
217-
if(dtrace_EXECUTABLE)
229+
if(CMAKE_SYSTEM_NAME STREQUAL Windows)
230+
add_definitions(-DTARGET_OS_WIN32)
231+
endif()
232+
233+
if(ENABLE_DTRACE)
234+
find_program(dtrace_EXECUTABLE dtrace)
235+
if(NOT dtrace_EXECUTABLE)
236+
message(FATAL_ERROR "dtrace not found but explicitly requested")
237+
endif()
218238
add_definitions(-DDISPATCH_USE_DTRACE=1)
219239
else()
220240
add_definitions(-DDISPATCH_USE_DTRACE=0)

cmake/config.h.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@
212212
#endif
213213
/* Enable GNU extensions on systems that have them. */
214214
#ifndef _GNU_SOURCE
215-
#cmakedefine01 _GNU_SOURCE
215+
#cmakedefine _GNU_SOURCE
216216
#endif
217217
/* Enable threading extensions on Solaris. */
218218
#ifndef _POSIX_PTHREAD_SEMANTICS

cmake/modules/DispatchCompilerWarnings.cmake

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,13 @@ else()
8686
add_compile_options(-Wno-unused-macros)
8787
add_compile_options(-Wno-used-but-marked-unused)
8888
add_compile_options(-Wno-vla)
89+
90+
if(CMAKE_SYSTEM_NAME STREQUAL Android)
91+
add_compile_options(-Wno-incompatible-function-pointer-types)
92+
add_compile_options(-Wno-implicit-function-declaration)
93+
add_compile_options(-Wno-conversion)
94+
add_compile_options(-Wno-int-conversion)
95+
add_compile_options(-Wno-shorten-64-to-32)
96+
endif()
8997
endmacro()
9098
endif()

cmake/modules/SwiftSupport.cmake

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ include(CMakeParseArguments)
33

44
function(add_swift_library library)
55
set(options)
6-
set(single_value_options MODULE_NAME;MODULE_LINK_NAME;MODULE_PATH;MODULE_CACHE_PATH;OUTPUT)
6+
set(single_value_options MODULE_NAME;MODULE_LINK_NAME;MODULE_PATH;MODULE_CACHE_PATH;OUTPUT;TARGET)
77
set(multiple_value_options SOURCES;SWIFT_FLAGS;CFLAGS)
88

99
cmake_parse_arguments(ASL "${options}" "${single_value_options}" "${multiple_value_options}" ${ARGN})
@@ -12,6 +12,9 @@ function(add_swift_library library)
1212

1313
list(APPEND flags -emit-library)
1414

15+
if(ASL_TARGET)
16+
list(APPEND FLAGS -target;${ASL_TARGET})
17+
endif()
1518
if(ASL_MODULE_NAME)
1619
list(APPEND flags -module-name;${ASL_MODULE_NAME})
1720
endif()

src/CMakeLists.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ if(ENABLE_SWIFT)
9393
swift/Source.swift
9494
swift/Time.swift
9595
swift/Wrapper.swift
96+
TARGET
97+
${CMAKE_C_COMPILER_TARGET}
9698
CFLAGS
9799
-fblocks
98100
-fmodule-map-file=${CMAKE_SOURCE_DIR}/dispatch/module.modulemap
@@ -106,7 +108,7 @@ if(ENABLE_SWIFT)
106108
swift/DispatchStubs.cc
107109
${CMAKE_CURRENT_BINARY_DIR}/swiftDispatch.o)
108110
endif()
109-
if(dtrace_EXECUTABLE)
111+
if(ENABLE_DTRACE)
110112
dtrace_usdt_probe(${CMAKE_CURRENT_SOURCE_DIR}/provider.d
111113
OUTPUT_SOURCES
112114
dispatch_dtrace_provider_headers)
@@ -140,6 +142,10 @@ if(CMAKE_SYSTEM_NAME STREQUAL Windows)
140142
target_compile_definitions(dispatch
141143
PRIVATE
142144
-D_CRT_SECURE_NO_WARNINGS)
145+
elseif(CMAKE_SYSTEM_NAME STREQUAL Android)
146+
target_compile_options(dispatch
147+
PRIVATE
148+
-U_GNU_SOURCE)
143149
endif()
144150
if(BSD_OVERLAY_FOUND)
145151
target_compile_options(dispatch

0 commit comments

Comments
 (0)