Skip to content

[windows][toolchain] Run Swift Runtime execution tests in x86_64 Android emulator #79081

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1294,9 +1294,9 @@ if(swift_build_android AND NOT "${SWIFT_HOST_VARIANT_SDK}" STREQUAL "ANDROID")
if ("${SWIFT_ANDROID_NDK_PATH}" STREQUAL "")
message(FATAL_ERROR "You must set SWIFT_ANDROID_NDK_PATH to cross-compile the Swift runtime for Android")
endif()
if (NOT ("${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Darwin" OR "${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Linux"))
message(FATAL_ERROR "A Darwin or Linux host is required to build the Swift runtime for Android")
endif()
#if (NOT ("${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Darwin" OR "${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Linux"))
# message(FATAL_ERROR "A Darwin or Linux host is required to build the Swift runtime for Android")
#endif()

if("${SWIFT_SDK_ANDROID_ARCHITECTURES}" STREQUAL "")
set(SWIFT_SDK_ANDROID_ARCHITECTURES armv7;aarch64)
Expand Down
1 change: 1 addition & 0 deletions cmake/caches/Runtime-Android-x86_64.cmake
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

set(SWIFT_HOST_VARIANT Windows CACHE STRING "")
set(SWIFT_HOST_VARIANT_SDK ANDROID CACHE STRING "")
set(SWIFT_HOST_VARIANT_ARCH x86_64 CACHE STRING "")

Expand Down
8 changes: 5 additions & 3 deletions test/lit.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -1891,9 +1891,11 @@ elif run_os == 'linux-androideabi' or run_os == 'linux-android':
config.target_pic_opt = "-fPIC"

ndk_platform_tuple = get_architecture_value(armv7="armeabi-v7a",
aarch64="arm64-v8a")
aarch64="arm64-v8a",
x86_64="x86_64")
ndk_platform_triple = get_architecture_value(armv7="arm-linux-androideabi",
aarch64="aarch64-linux-android")
aarch64="aarch64-linux-android",
x86_64="x86_64-linux-android")
if platform.system() == 'Linux':
prebuilt_directory = 'linux-x86_64'
elif platform.system() == 'Darwin':
Expand Down Expand Up @@ -1944,7 +1946,7 @@ elif run_os == 'linux-androideabi' or run_os == 'linux-android':
mcp_opt, config.swift_test_options, config.swift_frontend_test_options])
subst_target_swift_frontend_mock_sdk = config.target_swift_frontend
subst_target_swift_frontend_mock_sdk_after = ""
config.target_run = make_path(config.swift_src_root, 'utils', 'android', 'adb_test_runner.py')
config.target_run = '"' + sys.executable + '" ' + make_path(config.swift_src_root, 'utils', 'android', 'adb_test_runner.py')
# Use SDKROOT instead of -sdk because a couple tests set another -sdk and
# sil-opt errors if passed -sdk multiple times.
config.target_sil_opt = ' '.join([
Expand Down
12 changes: 9 additions & 3 deletions utils/android/adb/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
ENV_PREFIX = 'ANDROID_CHILD_'


# Subprocess output might be bytes or str depending on OS and Python version
def _as_str(s):
return s if isinstance(s, str) else str(s, 'utf-8')


def shell(args):
"""
Execute 'adb shell' with the given arguments.
Expand Down Expand Up @@ -54,7 +59,7 @@ def push(local_paths, device_path):
['adb', 'push', '--sync'] + local_paths + [device_path],
stderr=subprocess.STDOUT).strip()
except subprocess.CalledProcessError as e:
if "unrecognized option '--sync'" in e.output:
if "unrecognized option '--sync'" in _as_str(e.output):
return subprocess.check_output(
['adb', 'push'] + local_paths + [device_path],
stderr=subprocess.STDOUT).strip()
Expand Down Expand Up @@ -109,6 +114,7 @@ def execute_on_device(executable_path, executable_arguments):
executable_name = os.path.basename(executable_path)
executable = '{}/{}'.format(uuid_dir, executable_name)
push(executable_path, executable)
shell(['chmod', '+x', executable])

child_environment = ['{}="{}"'.format(k.replace(ENV_PREFIX, '', 1), v)
for (k, v) in os.environ.items()
Expand Down Expand Up @@ -167,8 +173,8 @@ def execute_on_device(executable_path, executable_arguments):
shell([executable_piped])

# Grab the results of running the executable on device.
stdout = shell(['cat', executable_stdout])
exitcode = shell(['cat', executable_succeeded])
stdout = _as_str(shell(['cat', executable_stdout]))
exitcode = _as_str(shell(['cat', executable_succeeded]))
if not exitcode.startswith(succeeded_token):
debug_command = '$ adb shell {}'.format(executable_with_args)
print('Executable exited with a non-zero code on the Android device.\n'
Expand Down
38 changes: 27 additions & 11 deletions utils/android/adb_push_built_products/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import argparse
import glob
import os
import sys

import adb.commands

Expand Down Expand Up @@ -46,7 +47,7 @@ def argument_parser():
'-a', '--destination-arch',
help='The architecture of the host device. Used to determine the '
'right library versions to send to the device.',
choices=['armv7', 'aarch64'],
choices=['armv7', 'aarch64', 'x86_64'],
default='armv7')
return parser

Expand All @@ -56,6 +57,29 @@ def _push(sources, destination):
print(adb.commands.push(sources, destination))


def _find_libcpp(ndk_path, target_arch):
ndk_version = 26
# TODO: Add argument and keep support?
if ndk_version < 22:
return os.path.join(ndk_path,
'sources',
'cxx-stl',
'llvm-libc++',
'libs',
{
'armv7': 'armeabi-v7a',
'aarch64': 'arm64-v8a',
'x86_64': 'x86_64'
}[target_arch],
'libc++_shared.so')
# TODO: Detect host arch
host_triple = 'windows-x86_64' if sys.platform == 'win32' else 'linux-x86_64'
target_triple = target_arch + '-linux-android'

# TODO: Support just-built libc++?
return os.path.join(ndk_path, 'toolchains', 'llvm', 'prebuilt', host_triple, 'sysroot', 'usr', 'lib', target_triple, 'libc++_shared.so')


def main():
"""
The main entry point for adb_push_built_products.
Expand All @@ -67,6 +91,7 @@ def main():
args = parser.parse_args()

for path in args.paths:
adb.commands.shell(['mkdir', '-p', args.destination])
if os.path.isdir(path):
full_paths = [
os.path.join(path, basename)
Expand All @@ -76,16 +101,7 @@ def main():
_push(path, args.destination)

if args.ndk:
libcpp = os.path.join(args.ndk,
'sources',
'cxx-stl',
'llvm-libc++',
'libs',
{
'armv7': 'armeabi-v7a',
'aarch64': 'arm64-v8a',
}[args.destination_arch],
'libc++_shared.so')
libcpp = _find_libcpp(args.ndk, args.destination_arch)
_push(libcpp, args.destination)

return 0
4 changes: 3 additions & 1 deletion utils/build-windows-toolchain.bat
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ set TMPDIR=%BuildRoot%\tmp
set NINJA_STATUS=[%%f/%%t][%%p][%%es]

:: Build the -Test argument, if any, by subtracting skipped tests
set TestArg=-Test lld,lldb,swift,dispatch,foundation,xctest,swift-format,sourcekit-lsp,
set TestArg=-Test android,
for %%I in (%SKIP_TESTS%) do (call set TestArg=%%TestArg:%%I,=%%)
if "%TestArg:~-1%"=="," (set TestArg=%TestArg:~0,-1%) else (set TestArg= )

Expand All @@ -77,6 +77,8 @@ powershell.exe -ExecutionPolicy RemoteSigned -File %~dp0build.ps1 ^
-ImageRoot %BuildRoot% ^
%SkipPackagingArg% ^
%TestArg% ^
-AndroidSDKs x86_64 ^
-WindowsSDKs X64 ^
-Stage %PackageRoot% ^
-Summary || (exit /b 1)

Expand Down
Loading