Skip to content

Commit 2b2cb83

Browse files
First draft for Android cross-compilation support
Adds options to `configure` in a similar fashion and with similar constraints to the options added in `swift` to add Android Support. This will allow initially those features to be just passed from `utils/build-script` without any change until a full strategy for cross-compilation in all the involved projects is defined. This is an example call to build for Android with Swift support: ``` env \ CC="${swift_android_path}/build/Ninja-ReleaseAssert/llvm-linux-x86_64/bi n/clang" \ CXX="${swift_android_path}/build/Ninja-ReleaseAssert/llvm-linux-x86_64/b in/clang++" \ SWIFTC="${swift_android_path}/build/Ninja-ReleaseAssert/swift-linux-x86_ 64/bin/swiftc" \ ${swift_android_path}/swift-corelibs-libdispatch/configure \ --with-swift-toolchain=“${swift_android_path}/build/Ninja-ReleaseAssert/ swift-linux-x86_64/" \ --with-build-variant=release \ --enable-android \ --host=arm-linux-androideabi \ --with-android-ndk=${ndk_path} \ --with-android-api-level=21 \ --disable-build-tests ``` * NOTE: `shims/android/lib(pthread|rt).a` were added to avoid modifying `libpwq` and `libkqueue` submodules. This two projects are adding `-lpthread -lrt` in `dependency_libs` at their libtool `la` files, causing this to be extended until the final linking of the dynamic library. There should be a more elegant solution to this issue.
1 parent 574f02e commit 2b2cb83

File tree

4 files changed

+65
-7
lines changed

4 files changed

+65
-7
lines changed

configure.ac

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ AC_CONFIG_MACRO_DIR([m4])
1111
ac_clean_files=a.out.dSYM
1212
AM_MAINTAINER_MODE
1313

14+
AC_CANONICAL_BUILD
15+
AC_CANONICAL_HOST
16+
AC_CANONICAL_TARGET
17+
1418
#
1519
# Command line argument to specify build variant (default to release).
1620
# Impacts default value of CFLAGS et al. so must come before AC_PROG_CC
@@ -56,6 +60,53 @@ AC_PROG_CXX([clang++ g++ c++])
5660
AC_PROG_OBJC([clang gcc cc])
5761
AC_PROG_OBJCXX([clang++ g++ c++])
5862

63+
#
64+
# Android cross-compilation support
65+
#
66+
AC_ARG_WITH([android-ndk],
67+
[AS_HELP_STRING([--with-android-ndk],
68+
[Android NDK location])], [
69+
android_ndk=${withval}
70+
])
71+
AC_ARG_WITH([android-ndk-gcc-version],
72+
[AS_HELP_STRING([--with-android-ndk-gcc-version],
73+
[Android NDK GCC version [defaults=4.9]])],
74+
[android_ndk_gcc_version=${withval}], [android_ndk_gcc_version=4.9])
75+
AC_ARG_WITH([android-api-level],
76+
[AS_HELP_STRING([--with-android-api-level],
77+
[Android API level to link with])], [
78+
android_api_level=${withval}
79+
])
80+
AC_ARG_ENABLE([android],
81+
[AS_HELP_STRING([--enable-android],
82+
[Compile for Android])], [
83+
android=true
84+
85+
# Override values until there's real support for multiple Android platforms
86+
host=armv7-none-linux-androideabi
87+
host_alias=arm-linux-androideabi
88+
host_cpu=armv7
89+
host_os=linux-androideabi
90+
host_vendor=unknown
91+
arch=arm
92+
93+
sysroot=${android_ndk}/platforms/android-${android_api_level}/arch-${arch}
94+
toolchain=${android_ndk}/toolchains/${host_alias}-${android_ndk_gcc_version}/prebuilt/linux-${build_cpu}
95+
96+
CFLAGS="$CFLAGS -target ${host_alias} --sysroot=${sysroot} -B${toolchain}/${host_alias}/bin"
97+
CXXFLAGS="$CXXFLAGS -target ${host_alias} --sysroot=${sysroot} -B${toolchain}/${host_alias}/bin"
98+
SWIFTC_FLAGS="-target ${host} -sdk ${sysroot} -L${toolchain}/lib/gcc/${host_alias}/${android_ndk_gcc_version}.x"
99+
LIBS="$LIBS -L${toolchain}/lib/gcc/${host_alias}/${android_ndk_gcc_version}.x"
100+
LDFLAGS="$LDFLAGS -Wc,'-target','${host_alias}','-B${toolchain}/${host_alias}/bin'"
101+
102+
# FIXME: empty CFLAGS and CXXFLAGS are assumed for this to work.
103+
# FIXME: there should be a more elegant way to do this
104+
ac_configure_args=`echo $ac_configure_args | sed -e "s/ 'CFLAGS='//" -e "s/ 'CXXFLAGS='//"`
105+
# CFLAGS, CXXFLAGS and LIBS needs to be passed to libkqueue and libpwq
106+
ac_configure_args="$ac_configure_args 'CFLAGS=$CFLAGS' 'CXXFLAGS=$CXXFLAGS' 'LIBS=$LIBS'"
107+
], [android=false])
108+
AM_CONDITIONAL(ANDROID, $android)
109+
59110
#
60111
# On Mac OS X, some required header files come from other source packages;
61112
# allow specifying where those are.
@@ -134,8 +185,6 @@ AS_IF([test "x$enable_apple_tsd_optimizations" = "xyes"],
134185
[Define to use non-portable pthread TSD optimizations for Mac OS X)])]
135186
)
136187

137-
AC_CANONICAL_TARGET
138-
139188
#
140189
# Enable building Swift overlay support into libdispatch
141190
#

src/Makefile.am

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,17 @@ DISPATCH_CFLAGS=-Wall $(VISIBILITY_FLAGS) $(OMIT_LEAF_FP_FLAGS) \
6464
if DISPATCH_ENABLE_ASSERTS
6565
DISPATCH_CFLAGS+=-DDISPATCH_DEBUG=1
6666
endif
67-
AM_CFLAGS= $(KQUEUE_CFLAGS) $(PTHREAD_WORKQUEUE_CFLAGS) $(DISPATCH_CFLAGS) $(CBLOCKS_FLAGS)
68-
AM_OBJCFLAGS=$(DISPATCH_CFLAGS) $(CBLOCKS_FLAGS)
69-
AM_CXXFLAGS=$(KQUEUE_CFLAGS) $(PTHREAD_WORKQUEUE_CFLAGS) $(DISPATCH_CFLAGS) $(CXXBLOCKS_FLAGS)
70-
AM_OBJCXXFLAGS=$(DISPATCH_CFLAGS) $(CXXBLOCKS_FLAGS)
67+
68+
if ANDROID
69+
CANDROID_FLAGS= -I$(top_srcdir)/src/shims/android -L$(top_srcdir)/src/shims/android
70+
CXXANDROID_FLAGS= -I$(top_srcdir)/src/shims/android -L$(top_srcdir)/src/shims/android
71+
ANDROID_LIBS= -L$(top_srcdir)/src/shims/android
72+
endif
73+
74+
AM_CFLAGS= $(KQUEUE_CFLAGS) $(PTHREAD_WORKQUEUE_CFLAGS) $(DISPATCH_CFLAGS) $(CBLOCKS_FLAGS) $(CANDROID_FLAGS)
75+
AM_OBJCFLAGS=$(DISPATCH_CFLAGS) $(CBLOCKS_FLAGS) $(CANDROID_FLAGS)
76+
AM_CXXFLAGS=$(KQUEUE_CFLAGS) $(PTHREAD_WORKQUEUE_CFLAGS) $(DISPATCH_CFLAGS) $(CXXBLOCKS_FLAGS) $(CXXANDROID_FLAGS)
77+
AM_OBJCXXFLAGS=$(DISPATCH_CFLAGS) $(CXXBLOCKS_FLAGS) $(CXXANDROID_FLAGS)
7178

7279
if BUILD_OWN_KQUEUES
7380
KQUEUE_LIBS+=$(top_builddir)/libkqueue/libkqueue.la
@@ -91,7 +98,7 @@ BLOCKS_RUNTIME_LIBS=-ldl
9198
endif
9299

93100
libdispatch_la_LDFLAGS=-avoid-version
94-
libdispatch_la_LIBADD=$(KQUEUE_LIBS) $(PTHREAD_WORKQUEUE_LIBS) $(BSD_OVERLAY_LIBS) $(BLOCKS_RUNTIME_LIBS)
101+
libdispatch_la_LIBADD=$(KQUEUE_LIBS) $(PTHREAD_WORKQUEUE_LIBS) $(BSD_OVERLAY_LIBS) $(BLOCKS_RUNTIME_LIBS) $(ANDROID_LIBS)
95102

96103
if HAVE_DARWIN_LD
97104
libdispatch_la_LDFLAGS+=-Wl,-compatibility_version,1 \

src/shims/android/libpthread.a

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
!<arch>

src/shims/android/librt.a

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
!<arch>

0 commit comments

Comments
 (0)