Skip to content

Commit 44f3640

Browse files
authored
Merge pull request swiftlang#312 from compnerd/android
build: improve cross-compilation for android
2 parents bfa9aa7 + 929a1d7 commit 44f3640

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
@@ -61,6 +61,13 @@ if(ENABLE_SWIFT)
6161
${SWIFT_RUNTIME_LIBDIR}/${CMAKE_SHARED_LIBRARY_PREFIX}swiftSwiftOnoneSupport${CMAKE_SHARED_LIBRARY_SUFFIX})
6262
endif()
6363

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

6673
option(ENABLE_TESTING "build libdispatch tests" ON)
@@ -166,10 +173,8 @@ check_include_files("libproc_internal.h" HAVE_LIBPROC_INTERNAL_H)
166173
check_include_files("mach/mach.h" HAVE_MACH)
167174
if(HAVE_MACH)
168175
set(__DARWIN_NON_CANCELABLE 1)
169-
set(USE_MACH_SEM 1)
170176
else()
171177
set(__DARWIN_NON_CANCELABLE 0)
172-
set(USE_MACH_SEM 0)
173178
endif()
174179
check_include_files("malloc/malloc.h" HAVE_MALLOC_MALLOC_H)
175180
check_include_files("memory.h" HAVE_MEMORY_H)
@@ -188,11 +193,19 @@ check_include_files("sys/types.h" HAVE_SYS_TYPES_H)
188193
check_include_files("unistd.h" HAVE_UNISTD_H)
189194
check_include_files("objc/objc-internal.h" HAVE_OBJC)
190195

191-
check_library_exists(pthread sem_init "" USE_POSIX_SEM)
196+
if(HAVE_MACH)
197+
set(USE_MACH_SEM 1)
198+
else()
199+
set(USE_MACH_SEM 0)
200+
endif()
192201
if(CMAKE_SYSTEM_NAME STREQUAL Windows)
193-
add_definitions(-DTARGET_OS_WIN32)
194202
add_definitions(-DUSE_WIN32_SEM)
195203
endif()
204+
check_library_exists(pthread sem_init "" USE_POSIX_SEM)
205+
# NOTE: android has not always provided a libpthread, but uses the pthreads API
206+
if(CMAKE_SYSTEM_NAME STREQUAL Android)
207+
set(USE_POSIX_SEM 1)
208+
endif()
196209

197210
check_symbol_exists(CLOCK_UPTIME "time.h" HAVE_DECL_CLOCK_UPTIME)
198211
check_symbol_exists(CLOCK_UPTIME_FAST "time.h" HAVE_DECL_CLOCK_UPTIME_FAST)
@@ -214,8 +227,15 @@ check_symbol_exists(VQ_VERYLOWDISK "sys/mount.h" HAVE_DECL_VQ_VERYLOWDISK)
214227

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

217-
find_program(dtrace_EXECUTABLE dtrace)
218-
if(dtrace_EXECUTABLE)
230+
if(CMAKE_SYSTEM_NAME STREQUAL Windows)
231+
add_definitions(-DTARGET_OS_WIN32)
232+
endif()
233+
234+
if(ENABLE_DTRACE)
235+
find_program(dtrace_EXECUTABLE dtrace)
236+
if(NOT dtrace_EXECUTABLE)
237+
message(FATAL_ERROR "dtrace not found but explicitly requested")
238+
endif()
219239
add_definitions(-DDISPATCH_USE_DTRACE=1)
220240
else()
221241
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
@@ -70,5 +70,13 @@ else()
7070
add_compile_options(-Wno-unused-macros)
7171
add_compile_options(-Wno-used-but-marked-unused)
7272
add_compile_options(-Wno-vla)
73+
74+
if(CMAKE_SYSTEM_NAME STREQUAL Android)
75+
add_compile_options(-Wno-incompatible-function-pointer-types)
76+
add_compile_options(-Wno-implicit-function-declaration)
77+
add_compile_options(-Wno-conversion)
78+
add_compile_options(-Wno-int-conversion)
79+
add_compile_options(-Wno-shorten-64-to-32)
80+
endif()
7381
endmacro()
7482
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)