Skip to content

Commit 14cc620

Browse files
committed
[android] A few tweaks for native compilation and to get more tests working
Now that CMAKE_HOST_SYSTEM_NAME and CMAKE_SYSTEM_NAME are set by default to Android in the Termux app, make the needed tweaks. Some tests were adapted to work natively on Android too, adds sys/cdefs.h to the Bionic modulemap, and includes the start of native Android platform support in the build-script.
1 parent fb2346d commit 14cc620

File tree

11 files changed

+63
-19
lines changed

11 files changed

+63
-19
lines changed

CMakeLists.txt

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -601,13 +601,7 @@ if(SWIFT_HOST_VARIANT_SDK)
601601
set(SWIFT_HOST_VARIANT_SDK_default "${SWIFT_HOST_VARIANT_SDK}")
602602
else()
603603
if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
604-
# CMake on an Android host sets this to Linux, so check for the ANDROID_DATA
605-
# environment variable to see if we're building on Android.
606-
if(NOT "$ENV{ANDROID_DATA}" STREQUAL "")
607-
set(SWIFT_HOST_VARIANT_SDK_default "ANDROID")
608-
else()
609-
set(SWIFT_HOST_VARIANT_SDK_default "LINUX")
610-
endif()
604+
set(SWIFT_HOST_VARIANT_SDK_default "LINUX")
611605
elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "FreeBSD")
612606
set(SWIFT_HOST_VARIANT_SDK_default "FREEBSD")
613607
elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "CYGWIN")
@@ -617,7 +611,6 @@ else()
617611
elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Haiku")
618612
set(SWIFT_HOST_VARIANT_SDK_default "HAIKU")
619613
elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Android")
620-
# CMAKE_SYSTEM_NAME might be set this way when cross-compiling to Android.
621614
set(SWIFT_HOST_VARIANT_SDK_default "ANDROID")
622615
elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
623616
set(SWIFT_HOST_VARIANT_SDK_default "OSX")

cmake/modules/AddSwift.cmake

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,9 +1105,13 @@ function(_add_swift_library_single target name)
11051105
PROPERTIES
11061106
INSTALL_RPATH "$ORIGIN:/usr/lib/swift/cygwin")
11071107
elseif("${SWIFTLIB_SINGLE_SDK}" STREQUAL "ANDROID")
1108-
# CMake generates incorrect rule `$SONAME_FLAG $INSTALLNAME_DIR$SONAME` for Android build on macOS cross-compile host.
1109-
# Proper linker flags constructed manually. See below variable `swiftlib_link_flags_all`.
1110-
set_target_properties("${target}" PROPERTIES NO_SONAME TRUE)
1108+
# CMake generates an incorrect rule `$SONAME_FLAG $INSTALLNAME_DIR$SONAME`
1109+
# for an Android cross-build from a macOS host. Construct the proper linker
1110+
# flags manually in add_swift_target_library instead, see there with
1111+
# variable `swiftlib_link_flags_all`.
1112+
if(SWIFTLIB_SINGLE_TARGET_LIBRARY)
1113+
set_target_properties("${target}" PROPERTIES NO_SONAME TRUE)
1114+
endif()
11111115
# Only set the install RPATH if cross-compiling the host tools, in which
11121116
# case both the NDK and Sysroot paths must be set.
11131117
if(NOT "${SWIFT_ANDROID_NDK_PATH}" STREQUAL "" AND

cmake/modules/SwiftConfigureSDK.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,8 @@ macro(configure_sdk_unix name architectures)
262262
set(_swift_android_prebuilt_build linux-x86_64)
263263
elseif(CMAKE_HOST_SYSTEM_NAME STREQUAL Windows)
264264
set(_swift_android_prebuilt_build Windows-x86_64)
265+
elseif(CMAKE_HOST_SYSTEM_NAME STREQUAL Android)
266+
# When building natively on an Android host, there's no NDK or prebuilt suffix.
265267
else()
266268
message(SEND_ERROR "cannot cross-compile to android from ${CMAKE_HOST_SYSTEM_NAME}")
267269
endif()

lib/Driver/UnixToolChains.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ std::string
4242
toolchains::GenericUnix::sanitizerRuntimeLibName(StringRef Sanitizer,
4343
bool shared) const {
4444
return (Twine("libclang_rt.") + Sanitizer + "-" +
45-
this->getTriple().getArchName() + ".a")
45+
this->getTriple().getArchName() +
46+
(this->getTriple().isAndroid() ? "-android" : "") + ".a")
4647
.str();
4748
}
4849

stdlib/public/Platform/bionic.modulemap.gyb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,10 @@ module SwiftGlibc [system] {
282282
module sys {
283283
export *
284284

285+
module cdefs {
286+
header "${GLIBC_ARCH_INCLUDE_PATH}/sys/cdefs.h"
287+
export *
288+
}
285289
module file {
286290
header "${GLIBC_ARCH_INCLUDE_PATH}/sys/file.h"
287291
export *

test/Driver/multi-threaded.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
// RUN: %target-swiftc_driver -driver-print-jobs -module-name=ThisModule -wmo -num-threads 4 %S/Inputs/main.swift %s -c | %FileCheck -check-prefix=OBJECT %s
66
// RUN: cd %t && %target-swiftc_driver -parseable-output -module-name=ThisModule -wmo -num-threads 4 %S/Inputs/main.swift %s -c 2> %t/parseable-output
77
// RUN: cat %t/parseable-output | %FileCheck -check-prefix=PARSEABLE %s
8-
// RUN: cd %t && env TMPDIR=/tmp %swiftc_driver -driver-print-jobs -module-name=ThisModule -wmo -num-threads 4 %S/Inputs/main.swift %s -o a.out | %FileCheck -check-prefix=EXEC %s
8+
// RUN: %empty-directory(%t/tmp)
9+
// RUN: cd %t && env TMPDIR=%t/tmp/ %swiftc_driver -driver-print-jobs -module-name=ThisModule -wmo -num-threads 4 %S/Inputs/main.swift %s -o a.out | %FileCheck -check-prefix=EXEC %s
910
// RUN: echo "{\"%/s\": {\"llvm-bc\": \"%/t/multi-threaded.bc\", \"object\": \"%/t/multi-threaded.o\"}, \"%/S/Inputs/main.swift\": {\"llvm-bc\": \"%/t/main.bc\", \"object\": \"%/t/main.o\"}}" > %t/ofmo.json
1011
// RUN: %target-swiftc_driver -module-name=ThisModule -wmo -num-threads 4 %S/Inputs/main.swift %s -emit-dependencies -output-file-map %t/ofmo.json -c
1112
// RUN: cat %t/*.d | %FileCheck -check-prefix=DEPENDENCIES %s

test/lit.cfg

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,9 @@ elif (run_os in ['linux-gnu', 'linux-gnueabihf', 'freebsd', 'windows-cygnus', 'w
10211021
config.target_shared_library_prefix = 'lib'
10221022
config.target_shared_library_suffix = ".so"
10231023
config.target_sdk_name = "android"
1024+
# Needed by several ParseableInterface/swift_build_sdk_interfaces tests on
1025+
# Android
1026+
config.environment['ANDROID_DATA'] = os.environ['ANDROID_DATA']
10241027
else:
10251028
lit_config.note("Testing Linux " + config.variant_triple)
10261029
config.target_object_format = "elf"
@@ -1239,7 +1242,7 @@ ENV_VAR_PREFIXES = {
12391242
'appletvsimulator': SIMULATOR_ENV_PREFIX,
12401243
'android': 'ANDROID_CHILD_'
12411244
}
1242-
TARGET_ENV_PREFIX = ENV_VAR_PREFIXES.get(config.target_sdk_name, "")
1245+
TARGET_ENV_PREFIX = ENV_VAR_PREFIXES.get(config.target_sdk_name, "") if not kIsAndroid else ""
12431246

12441247
if 'remote_run_host' in lit_config.params:
12451248
if 'remote_run_tmpdir' not in lit_config.params:
@@ -1351,7 +1354,7 @@ def source_compiler_rt_libs(path):
13511354
and config.compiler_rt_platform in lib])
13521355

13531356
compiler_rt_dir = make_path(test_resource_dir, 'clang', 'lib',
1354-
platform.system().lower())
1357+
platform.system().lower() if not kIsAndroid else 'android')
13551358
source_compiler_rt_libs(compiler_rt_dir)
13561359

13571360
def check_runtime_libs(features_to_check):

tools/SourceKit/cmake/modules/AddSwiftSourceKit.cmake

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,13 @@ macro(add_sourcekit_library name)
168168
endif()
169169
endif()
170170

171+
if("${CMAKE_SYSTEM_NAME}" STREQUAL "Android")
172+
if(SOURCEKITLIB_SHARED)
173+
set_target_properties(${name} PROPERTIES BUILD_WITH_INSTALL_RPATH TRUE)
174+
set_target_properties(${name} PROPERTIES INSTALL_RPATH "$ORIGIN/../lib/swift/android")
175+
endif()
176+
endif()
177+
171178
if("${SOURCEKITLIB_INSTALL_IN_COMPONENT}" STREQUAL "")
172179
if(SOURCEKITLIB_SHARED)
173180
set(SOURCEKITLIB_INSTALL_IN_COMPONENT tools)

utils/build-script-impl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,18 @@ function set_build_options_for_host() {
681681
-DSWIFT_DARWIN_DEPLOYMENT_VERSION_WATCHOS="${DARWIN_DEPLOYMENT_VERSION_WATCHOS}"
682682
)
683683
;;
684+
android-*)
685+
SWIFT_HOST_VARIANT="android"
686+
SWIFT_HOST_VARIANT_SDK="ANDROID"
687+
case ${host} in
688+
android-armv7)
689+
SWIFT_HOST_VARIANT_ARCH="armv7"
690+
;;
691+
android-aarch64)
692+
SWIFT_HOST_VARIANT_ARCH="aarch64"
693+
;;
694+
esac
695+
;;
684696
*)
685697
echo "Unknown host tools target: ${host}"
686698
exit 1
@@ -2933,7 +2945,7 @@ for host in "${ALL_HOSTS[@]}"; do
29332945
fi
29342946

29352947
case ${host} in
2936-
linux-*|freebsd-*|cygwin-*|haiku-*) ;;
2948+
linux-*|freebsd-*|cygwin-*|haiku-*|android-*) ;;
29372949
*)
29382950
echo "error: --install-xctest is not supported on this platform"
29392951
exit 1

utils/swift_build_sdk_interfaces.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -402,9 +402,18 @@ def main():
402402
xfails = json.load(xfails_file)
403403

404404
make_dirs_if_needed(args.output_dir, args.dry_run)
405-
shared_output_lock = multiprocessing.Lock()
406-
pool = multiprocessing.Pool(args.jobs, set_up_child,
407-
(args, shared_output_lock))
405+
if 'ANDROID_DATA' not in os.environ:
406+
shared_output_lock = multiprocessing.Lock()
407+
pool = multiprocessing.Pool(args.jobs, set_up_child,
408+
(args, shared_output_lock))
409+
else:
410+
# Android doesn't support Python's multiprocessing as it doesn't have
411+
# sem_open, so switch to a ThreadPool instead.
412+
import threading
413+
shared_output_lock = threading.Lock()
414+
from multiprocessing.pool import ThreadPool
415+
pool = ThreadPool(args.jobs, set_up_child,
416+
(args, shared_output_lock))
408417

409418
interface_framework_dirs = (args.interface_framework_dirs or
410419
DEFAULT_FRAMEWORK_INTERFACE_SEARCH_PATHS)

utils/swift_build_support/swift_build_support/targets.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,14 @@ def host_target():
186186
machine = platform.machine()
187187

188188
if system == 'Linux':
189+
if 'ANDROID_DATA' in os.environ:
190+
if machine.startswith('armv7'):
191+
return StdlibDeploymentTarget.Android.armv7
192+
elif machine == 'aarch64':
193+
return StdlibDeploymentTarget.Android.aarch64
194+
raise NotImplementedError('Android System with architecture '
195+
'"%s" is not supported' % machine)
196+
189197
if machine == 'x86_64':
190198
return StdlibDeploymentTarget.Linux.x86_64
191199
elif machine == 'i686':

0 commit comments

Comments
 (0)