From 929a1d7386cf3201eb8d44dfa7c3e931c6857905 Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Tue, 10 Oct 2017 10:48:43 -0700 Subject: [PATCH] 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. --- CMakeLists.txt | 32 ++++++++++++++++---- cmake/config.h.in | 2 +- cmake/modules/DispatchCompilerWarnings.cmake | 8 +++++ cmake/modules/SwiftSupport.cmake | 5 ++- src/CMakeLists.txt | 8 ++++- 5 files changed, 46 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 97d6a74db..5b294bba3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -61,6 +61,13 @@ if(ENABLE_SWIFT) ${SWIFT_RUNTIME_LIBDIR}/${CMAKE_SHARED_LIBRARY_PREFIX}swiftSwiftOnoneSupport${CMAKE_SHARED_LIBRARY_SUFFIX}) endif() +if(CMAKE_SYSTEM_NAME STREQUAL Android) + set(ENABLE_DTRACE_DEFAULT OFF) +else() + set(ENABLE_DTRACE_DEFAULT ON) +endif() +option(ENABLE_DTRACE "enable dtrace support" ${ENABLE_DTRACE_DEFAULT}) + option(BUILD_SHARED_LIBS "build shared libraries" ON) option(ENABLE_TESTING "build libdispatch tests" ON) @@ -166,10 +173,8 @@ check_include_files("libproc_internal.h" HAVE_LIBPROC_INTERNAL_H) check_include_files("mach/mach.h" HAVE_MACH) if(HAVE_MACH) set(__DARWIN_NON_CANCELABLE 1) - set(USE_MACH_SEM 1) else() set(__DARWIN_NON_CANCELABLE 0) - set(USE_MACH_SEM 0) endif() check_include_files("malloc/malloc.h" HAVE_MALLOC_MALLOC_H) check_include_files("memory.h" HAVE_MEMORY_H) @@ -188,11 +193,19 @@ check_include_files("sys/types.h" HAVE_SYS_TYPES_H) check_include_files("unistd.h" HAVE_UNISTD_H) check_include_files("objc/objc-internal.h" HAVE_OBJC) -check_library_exists(pthread sem_init "" USE_POSIX_SEM) +if(HAVE_MACH) + set(USE_MACH_SEM 1) +else() + set(USE_MACH_SEM 0) +endif() if(CMAKE_SYSTEM_NAME STREQUAL Windows) - add_definitions(-DTARGET_OS_WIN32) add_definitions(-DUSE_WIN32_SEM) endif() +check_library_exists(pthread sem_init "" USE_POSIX_SEM) +# NOTE: android has not always provided a libpthread, but uses the pthreads API +if(CMAKE_SYSTEM_NAME STREQUAL Android) + set(USE_POSIX_SEM 1) +endif() check_symbol_exists(CLOCK_UPTIME "time.h" HAVE_DECL_CLOCK_UPTIME) 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) check_symbol_exists(program_invocation_name "errno.h" HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME) -find_program(dtrace_EXECUTABLE dtrace) -if(dtrace_EXECUTABLE) +if(CMAKE_SYSTEM_NAME STREQUAL Windows) + add_definitions(-DTARGET_OS_WIN32) +endif() + +if(ENABLE_DTRACE) + find_program(dtrace_EXECUTABLE dtrace) + if(NOT dtrace_EXECUTABLE) + message(FATAL_ERROR "dtrace not found but explicitly requested") + endif() add_definitions(-DDISPATCH_USE_DTRACE=1) else() add_definitions(-DDISPATCH_USE_DTRACE=0) diff --git a/cmake/config.h.in b/cmake/config.h.in index 97c94cc8e..a59737b87 100644 --- a/cmake/config.h.in +++ b/cmake/config.h.in @@ -212,7 +212,7 @@ #endif /* Enable GNU extensions on systems that have them. */ #ifndef _GNU_SOURCE -#cmakedefine01 _GNU_SOURCE +#cmakedefine _GNU_SOURCE #endif /* Enable threading extensions on Solaris. */ #ifndef _POSIX_PTHREAD_SEMANTICS diff --git a/cmake/modules/DispatchCompilerWarnings.cmake b/cmake/modules/DispatchCompilerWarnings.cmake index dcc074e29..d5069c26e 100644 --- a/cmake/modules/DispatchCompilerWarnings.cmake +++ b/cmake/modules/DispatchCompilerWarnings.cmake @@ -70,5 +70,13 @@ else() add_compile_options(-Wno-unused-macros) add_compile_options(-Wno-used-but-marked-unused) add_compile_options(-Wno-vla) + + if(CMAKE_SYSTEM_NAME STREQUAL Android) + add_compile_options(-Wno-incompatible-function-pointer-types) + add_compile_options(-Wno-implicit-function-declaration) + add_compile_options(-Wno-conversion) + add_compile_options(-Wno-int-conversion) + add_compile_options(-Wno-shorten-64-to-32) + endif() endmacro() endif() diff --git a/cmake/modules/SwiftSupport.cmake b/cmake/modules/SwiftSupport.cmake index 64b7b36e9..b7a3e760c 100644 --- a/cmake/modules/SwiftSupport.cmake +++ b/cmake/modules/SwiftSupport.cmake @@ -3,7 +3,7 @@ include(CMakeParseArguments) function(add_swift_library library) set(options) - set(single_value_options MODULE_NAME;MODULE_LINK_NAME;MODULE_PATH;MODULE_CACHE_PATH;OUTPUT) + set(single_value_options MODULE_NAME;MODULE_LINK_NAME;MODULE_PATH;MODULE_CACHE_PATH;OUTPUT;TARGET) set(multiple_value_options SOURCES;SWIFT_FLAGS;CFLAGS) cmake_parse_arguments(ASL "${options}" "${single_value_options}" "${multiple_value_options}" ${ARGN}) @@ -12,6 +12,9 @@ function(add_swift_library library) list(APPEND flags -emit-library) + if(ASL_TARGET) + list(APPEND FLAGS -target;${ASL_TARGET}) + endif() if(ASL_MODULE_NAME) list(APPEND flags -module-name;${ASL_MODULE_NAME}) endif() diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 5c793dc73..11c3a3d18 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -93,6 +93,8 @@ if(ENABLE_SWIFT) swift/Source.swift swift/Time.swift swift/Wrapper.swift + TARGET + ${CMAKE_C_COMPILER_TARGET} CFLAGS -fblocks -fmodule-map-file=${CMAKE_SOURCE_DIR}/dispatch/module.modulemap @@ -106,7 +108,7 @@ if(ENABLE_SWIFT) swift/DispatchStubs.cc ${CMAKE_CURRENT_BINARY_DIR}/swiftDispatch.o) endif() -if(dtrace_EXECUTABLE) +if(ENABLE_DTRACE) dtrace_usdt_probe(${CMAKE_CURRENT_SOURCE_DIR}/provider.d OUTPUT_SOURCES dispatch_dtrace_provider_headers) @@ -140,6 +142,10 @@ if(CMAKE_SYSTEM_NAME STREQUAL Windows) target_compile_definitions(dispatch PRIVATE -D_CRT_SECURE_NO_WARNINGS) +elseif(CMAKE_SYSTEM_NAME STREQUAL Android) + target_compile_options(dispatch + PRIVATE + -U_GNU_SOURCE) endif() if(BSD_OVERLAY_FOUND) target_compile_options(dispatch