Skip to content

Commit a4a7384

Browse files
committed
add CMake build support for Android
1 parent b997bdb commit a4a7384

File tree

14 files changed

+73
-18
lines changed

14 files changed

+73
-18
lines changed

tools/swift-inspect/CMakeLists.txt

+23-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
cmake_minimum_required(VERSION 3.28)
22

33
project(swift-inspect
4-
LANGUAGES CXX Swift)
4+
LANGUAGES C CXX Swift)
55

66
# Set C++ standard
77
set(CMAKE_CXX_STANDARD 17)
@@ -27,7 +27,19 @@ if(WIN32)
2727
Sources/SwiftInspectClient/SwiftInspectClient.cpp)
2828
target_link_libraries(SwiftInspectClient PRIVATE
2929
SwiftInspectClientInterface)
30-
elseif(LINUX)
30+
endif()
31+
32+
if (ANDROID)
33+
add_library(AndroidCLib STATIC
34+
Sources/AndroidCLib/heap.c)
35+
target_include_directories(AndroidCLib PUBLIC
36+
Sources/AndroidCLib/include)
37+
target_link_options(AndroidCLib PUBLIC
38+
"-Xlinker" "-T${CMAKE_SOURCE_DIR}/Sources/AndroidCLib/section-text-remote.ld")
39+
set_property(TARGET AndroidCLib PROPERTY POSITION_INDEPENDENT_CODE ON)
40+
endif()
41+
42+
if(ANDROID OR LINUX)
3143
add_library(LinuxSystemHeaders INTERFACE)
3244
target_include_directories(LinuxSystemHeaders INTERFACE
3345
Sources/SwiftInspectLinux/SystemHeaders)
@@ -38,6 +50,8 @@ elseif(LINUX)
3850
Sources/SwiftInspectLinux/MemoryMap.swift
3951
Sources/SwiftInspectLinux/Process.swift
4052
Sources/SwiftInspectLinux/ProcFS.swift
53+
Sources/SwiftInspectLinux/PTrace.swift
54+
Sources/SwiftInspectLinux/RegisterSet.swift
4155
Sources/SwiftInspectLinux/SymbolCache.swift)
4256
target_compile_options(SwiftInspectLinux PRIVATE
4357
-Xcc -D_GNU_SOURCE)
@@ -52,6 +66,7 @@ add_executable(swift-inspect
5266
Sources/swift-inspect/Operations/DumpConformanceCache.swift
5367
Sources/swift-inspect/Operations/DumpGenericMetadata.swift
5468
Sources/swift-inspect/Operations/DumpRawMetadata.swift
69+
Sources/swift-inspect/AndroidRemoteProcess.swift
5570
Sources/swift-inspect/Backtrace.swift
5671
Sources/swift-inspect/DarwinRemoteProcess.swift
5772
Sources/swift-inspect/LinuxRemoteProcess.swift
@@ -71,7 +86,12 @@ target_link_libraries(swift-inspect PRIVATE
7186
if(WIN32)
7287
target_link_libraries(swift-inspect PRIVATE
7388
SwiftInspectClientInterface)
74-
elseif(LINUX)
89+
endif()
90+
if(ANDROID)
91+
target_link_libraries(swift-inspect PRIVATE
92+
AndroidCLib)
93+
endif()
94+
if(ANDROID OR LINUX)
7595
target_link_libraries(swift-inspect PRIVATE
7696
SwiftInspectLinux)
7797
endif()

tools/swift-inspect/README.md

+24-5
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@ To cross-compile swift-inspect for Android on Windows, some additional parameter
3232
set ANDROID_ARCH=aarch64
3333
set ANDROID_API_LEVEL=29
3434
set ANDROID_NDK_ROOT=C:\Android\android-sdk\ndk\26.3.11579264
35-
set SWIFT_ANDROID_SDK_ROOT=%LocalAppData%\Programs\Swift\Platforms\0.0.0\Android.platform\Developer\SDKs\Android.sdk
35+
set SDKROOT_ANDROID=%LocalAppData%\Programs\Swift\Platforms\0.0.0\Android.platform\Developer\SDKs\Android.sdk
3636
swift build --triple %ANDROID_ARCH%-unknown-linux-android%ANDROID_API_LEVEL% ^
3737
--sdk %ANDROID_NDK_ROOT%\toolchains\llvm\prebuilt\windows-x86_64\sysroot ^
38-
-Xswiftc -sdk -Xswiftc %SWIFT_ANDROID_SDK_ROOT% ^
38+
-Xswiftc -sdk -Xswiftc %SDKROOT_ANDROID% ^
3939
-Xswiftc -sysroot -Xswiftc %ANDROID_NDK_ROOT%\toolchains\llvm\prebuilt\windows-x86_64\sysroot ^
40-
-Xswiftc -I -Xswiftc %SWIFT_ANDROID_SDK_ROOT%\usr\include ^
40+
-Xswiftc -I -Xswiftc %SDKROOT_ANDROID%\usr\include ^
4141
-Xlinker -L%ANDROID_NDK_ROOT%\toolchains\llvm\prebuilt\windows-x86_64\lib\clang\17.0.2\lib\linux\%ANDROID_ARCH% ^
42-
-Xcc -I%SWIFT_ANDROID_SDK_ROOT%\usr\include\swift\SwiftRemoteMirror ^
43-
-Xlinker %SWIFT_ANDROID_SDK_ROOT%\usr\lib\swift\android\%ANDROID_ARCH%\libswiftRemoteMirror.so
42+
-Xcc -I%SDKROOT_ANDROID%\usr\include\swift\SwiftRemoteMirror ^
43+
-Xlinker %SDKROOT_ANDROID%\usr\lib\swift\android\%ANDROID_ARCH%\libswiftRemoteMirror.so
4444
~~~
4545

4646
#### CMake
@@ -57,6 +57,25 @@ In order to build on Linux with CMake, some additional parameters must be passed
5757
cmake -B out -G Ninja -S . -D ArgumentParser_DIR=... -D CMAKE_Swift_FLAGS="-Xcc -I$(git rev-parse --show-toplevel)/include/swift/SwiftRemoteMirror"
5858
~~~
5959

60+
In order to build for Android with CMake on Windows, some additiona parameters must be passed to the build tool to locate the necessary Swift modules.
61+
62+
~~~cmd
63+
set ANDROID_ARCH=aarch64
64+
set ANDROID_API_LEVEL=29
65+
set ANDROID_NDK_ROOT=C:\Android\android-sdk\ndk\26.3.11579264
66+
set ANDROID_ARCH_ABI=arm64-v8a
67+
set SDKROOT_ANDROID=%LocalAppData%\Programs\Swift\Platforms\0.0.0\Android.platform\Developer\SDKs\Android.sdk
68+
cmake -B build -S . -G Ninja ^
69+
-D CMAKE_BUILD_WITH_INSTALL_RPATH=YES ^
70+
-D CMAKE_SYSTEM_NAME=Android ^
71+
-D CMAKE_ANDROID_ARCH_ABI=%ANDROID_ARCH_ABI% ^
72+
-D CMAKE_SYSTEM_VERSION=%ANDROID_API_LEVEL% ^
73+
-D CMAKE_Swift_COMPILER_TARGET=%ANDROID_ARCH%-unknown-linux-android%ANDROID_API_LEVEL% ^
74+
-D CMAKE_Swift_FLAGS="-sdk %SDKROOT_ANDROID% -L%ANDROID_NDK_ROOT%\toolchains\llvm\prebuilt\windows-x86_64\lib\clang\17.0.2\lib\linux\%ANDROID_ARCH% -Xcc -I%SDKROOT_ANDROID%\usr\include -I%SDKROOT_ANDROID%\usr\include\swift\SwiftRemoteMirror" ^
75+
-D ArgumentParser_DIR=...
76+
cmake --build build
77+
~~~
78+
6079
Building with CMake requires a local copy of [swift-argument-parser](https://github.com/apple/swift-argument-parser) built with CMake.
6180
The `ArumentParser_DIR=` definition must refer to the `cmake/modules` sub-directory of the swift-argument-parser build output directory.
6281

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module AndroidCLib {
2+
header "heap.h"
3+
export *
4+
}

tools/swift-inspect/Sources/SwiftInspectLinux/SystemHeaders/errno.h tools/swift-inspect/Sources/SwiftInspectLinux/SystemHeaders/_errno.h

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#pragma once
14+
1315
#include <errno.h>
1416

1517
static inline

tools/swift-inspect/Sources/SwiftInspectLinux/SystemHeaders/mman.h tools/swift-inspect/Sources/SwiftInspectLinux/SystemHeaders/_mman.h

+2
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#pragma once
14+
1315
#include <sys/mman.h>

tools/swift-inspect/Sources/SwiftInspectLinux/SystemHeaders/ptrace.h tools/swift-inspect/Sources/SwiftInspectLinux/SystemHeaders/_ptrace.h

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#pragma once
14+
1315
#include <signal.h>
1416

1517
#include <sys/ptrace.h>

tools/swift-inspect/Sources/SwiftInspectLinux/SystemHeaders/signal.h tools/swift-inspect/Sources/SwiftInspectLinux/SystemHeaders/_signal.h

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#pragma once
14+
1315
#include <signal.h>
1416

1517
static inline

tools/swift-inspect/Sources/SwiftInspectLinux/SystemHeaders/unistd.h tools/swift-inspect/Sources/SwiftInspectLinux/SystemHeaders/_unistd.h

+2
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#pragma once
14+
1315
#include <unistd.h>

tools/swift-inspect/Sources/SwiftInspectLinux/SystemHeaders/wait.h tools/swift-inspect/Sources/SwiftInspectLinux/SystemHeaders/_wait.h

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#pragma once
14+
1315
#include <stdbool.h>
1416
#include <sys/wait.h>
1517

Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
module LinuxSystemHeaders {
2-
header "auxvec.h"
3-
header "elf.h"
4-
header "errno.h"
5-
header "link.h"
6-
header "mman.h"
7-
header "ptrace.h"
8-
header "signal.h"
9-
header "uio.h"
10-
header "unistd.h"
11-
header "wait.h"
2+
header "_auxvec.h"
3+
header "_elf.h"
4+
header "_errno.h"
5+
header "_link.h"
6+
header "_mman.h"
7+
header "_ptrace.h"
8+
header "_signal.h"
9+
header "_uio.h"
10+
header "_unistd.h"
11+
header "_wait.h"
1212
export *
1313
}

0 commit comments

Comments
 (0)