Skip to content

Commit fe3d38c

Browse files
[android] Run tests for libdispatch in x64 emulator (wip)
1 parent 30a58fa commit fe3d38c

File tree

2 files changed

+114
-1
lines changed

2 files changed

+114
-1
lines changed

utils/android/utils/ctest_mock.sh

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/system/bin/sh
2+
3+
if [ -z "$1" ]; then
4+
echo "Usage: $0 <sub-directory>"
5+
exit 1
6+
fi
7+
8+
SUB_DIR=$1
9+
if [ ! -d "$SUB_DIR" ]; then
10+
echo "Error: Directory $SUB_DIR does not exist"
11+
exit 1
12+
fi
13+
14+
CTEST_FILE="$SUB_DIR/CTestTestfile.cmake"
15+
if [ ! -f "$CTEST_FILE" ]; then
16+
echo "Error: $CTEST_FILE not found in $SUB_DIR"
17+
exit 1
18+
fi
19+
20+
# TODO: Parse them from CTEST_FILE
21+
TESTS="dispatch_apply dispatch_api dispatch_debug dispatch_queue_finalizer dispatch_overcommit dispatch_context_for_key dispatch_after dispatch_timer dispatch_timer_short dispatch_timer_timeout dispatch_sema dispatch_timer_bit31 dispatch_timer_bit63 dispatch_timer_set_time dispatch_data dispatch_io_muxed dispatch_io_net dispatch_io_pipe dispatch_io_pipe_close dispatch_select dispatch_c99 dispatch_plusplus"
22+
23+
COUNT=$(echo "$TESTS" | tr ' ' '\n' | wc -l)
24+
echo "Found $COUNT test-cases in $SUB_DIR"
25+
26+
UTILITY="$SUB_DIR/bsdtestharness"
27+
if [ ! -f "$UTILITY" ]; then
28+
echo "Utility not found: $UTILITY."
29+
exit 1
30+
fi
31+
32+
chmod +x "$UTILITY"
33+
34+
# We need libdispatch.so and libBlocksRuntime.so
35+
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$SUB_DIR"
36+
37+
# TODO: Parse the timeout as well from CTestTestfile.cmake?
38+
TIMOUT=120
39+
FAILURE=0
40+
41+
for TEST in $TESTS; do
42+
chmod +x "$SUB_DIR/$TEST"
43+
OUTPUT=$(timeout "${TIMOUT}s" "$UTILITY" "$SUB_DIR/$TEST" 2>&1)
44+
EC=$?
45+
if [ $EC -eq 124 ]; then
46+
echo "Error: $TEST timed out after $TIMOUT seconds"
47+
echo "************\nOutput:\n$OUTPUT\n************"
48+
FAILURE=1
49+
elif [ $EC -ne 0 ]; then
50+
echo "Error: $TEST failed"
51+
echo "************\nOutput:\n$OUTPUT\n************"
52+
FAILURE=1
53+
else
54+
echo "$TEST passed"
55+
fi
56+
done
57+
58+
exit $FAILURE

utils/build.ps1

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,7 @@ function Fetch-Dependencies {
721721
DownloadAndVerify $WixURL "$BinaryCache\WiX-$WiXVersion.zip" $WiXHash
722722
Extract-ZipFile WiX-$WiXVersion.zip $BinaryCache WiX-$WiXVersion
723723

724-
if ($SkipBuild) { return }
724+
if ($SkipBuild -and -not $Test) { return }
725725

726726
DownloadAndVerify $PinnedBuild "$BinaryCache\$PinnedToolchain.exe" $PinnedSHA256
727727

@@ -791,6 +791,30 @@ function Fetch-Dependencies {
791791
DownloadAndVerify $NDKURL "$BinaryCache\android-ndk-$AndroidNDKVersion-windows.zip" $NDKHash
792792

793793
Extract-ZipFile -ZipFileName "android-ndk-$AndroidNDKVersion-windows.zip" -BinaryCache $BinaryCache -ExtractPath "android-ndk-$AndroidNDKVersion" -CreateExtractPath $false
794+
795+
if ($Test -and -not(Test-Path "$NDKDir\licenses")) {
796+
$NDKDir = "$BinaryCache\android-ndk-r26b"
797+
$CLToolsURL = "https://dl.google.com/android/repository/commandlinetools-win-11076708_latest.zip"
798+
$CLToolsHash = "4d6931209eebb1bfb7c7e8b240a6a3cb3ab24479ea294f3539429574b1eec862"
799+
DownloadAndVerify $CLToolsURL "$BinaryCache\android-cmdline-tools.zip" $CLToolsHash
800+
801+
# Install cmdline-tools
802+
New-Item -Type Directory -Path "$NDKDir\licenses" -ErrorAction Ignore | Out-Null
803+
Set-Content -Path "$NDKDir\licenses\android-sdk-license" -Value "24333f8a63b6825ea9c5514f83c2829b004d1fee"
804+
Set-Content -Path "$NDKDir\licenses\android-sdk-preview-license" -Value "84831b9409646a918e30573bab4c9c91346d8abd"
805+
Extract-ZipFile -ZipFileName "android-cmdline-tools.zip" -BinaryCache $BinaryCache -ExtractPath "android-ndk-r26b\.temp"
806+
Invoke-Program "$NDKDir\.temp\cmdline-tools\bin\sdkmanager.bat" --sdk_root="$NDKDir" "cmdline-tools;latest" "--channel=3"
807+
808+
# Install packages
809+
$AndroidSdkMgr = "$NDKDir\cmdline-tools\latest\bin\sdkmanager.bat"
810+
foreach ($Package in @("emulator", "platforms;android-29", "system-images;android-29;default;x86_64", "platform-tools")) {
811+
Invoke-Program $AndroidSdkMgr $Package
812+
}
813+
814+
# Create test device
815+
$AndroidAvdMgr = "$NDKDir\cmdline-tools\latest\bin\avdmanager.bat"
816+
Invoke-Program $AndroidAvdMgr create avd --name "swift-test-device" --package "system-images;android-29;default;x86_64"
817+
}
794818
}
795819

796820
if ($IncludeDS2) {
@@ -1868,6 +1892,33 @@ function Build-Dispatch([Platform]$Platform, $Arch, [switch]$Test = $false) {
18681892
}
18691893
}
18701894

1895+
function Test-Dispatch([Platform]$Platform) {
1896+
# TODO: Make it global
1897+
$NDKDir = "$BinaryCache\android-ndk-r26b"
1898+
1899+
# TODO: Start once for all tests and make it shuts down
1900+
$emulator = "$NDKDir\emulator\emulator.exe"
1901+
Start-Process -FilePath $emulator -ArgumentList "@swift-test-device" `
1902+
-RedirectStandardOutput "$NDKDir\.temp\emulator.out" `
1903+
-RedirectStandardError "$NDKDir\.temp\emulator.err"
1904+
1905+
# TODO: Find a better indicator than a upper-bound delay
1906+
Start-Sleep -Seconds 5
1907+
1908+
# This is just a hack for now
1909+
$LocalBin = (Get-TargetProjectBinaryCache $AndroidX64 Dispatch)
1910+
$CacheName = Split-Path $LocalBin -Leaf
1911+
$RemoteBin = "/data/local/tmp/$CacheName"
1912+
1913+
# TODO: On my local machine I have to grant network access once. How to do that in CI?
1914+
$adb = "$NDKDir\platform-tools\adb.exe"
1915+
Invoke-Program $adb "shell" "rm -rf $RemoteBin"
1916+
Invoke-Program $adb "shell" "mkdir $RemoteBin"
1917+
Invoke-Program $adb "push" "$LocalBin/." $RemoteBin
1918+
Invoke-Program $adb "push" "S:/ctest_mock.sh" "/data/local/tmp"
1919+
Invoke-Program $adb "shell" "sh /data/local/tmp/ctest_mock.sh $RemoteBin"
1920+
}
1921+
18711922
function Build-Foundation([Platform]$Platform, $Arch, [switch]$Test = $false) {
18721923
if ($Test) {
18731924
# Foundation tests build via swiftpm rather than CMake
@@ -2923,6 +2974,10 @@ if (-not $IsCrossCompiling) {
29232974

29242975
if ($Test -contains "dispatch") {
29252976
Build-Dispatch Windows $HostArch -Test
2977+
# TODO: This is a hack. We need different devices for different arches.
2978+
if (AndroidSDKs -contains "x86_64") {
2979+
Test-Dispatch Android
2980+
}
29262981
}
29272982
if ($Test -contains "foundation") {
29282983
Build-Foundation Windows $HostArch -Test

0 commit comments

Comments
 (0)