Skip to content
Merged
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
26 changes: 26 additions & 0 deletions bindings/swift/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,32 @@ system libraries.
Use a Swift version compatible with the installed MSVC headers. Swift 6.4.2
has been used for the Windows source build.

## Build for HarmonyOS PC

Use Swift 6.4.2 with its ARM64 Static Linux SDK and DevEco Studio's HarmonyOS
NDK (API 26 is used by the SwiftUIPort HAP). Build the generated Swift binding
against the extracted ARM64 sysroot:

```sh
bash scripts/build-ohos.sh /path/to/musl-1.2.5.sdk/aarch64 /path/to/swift-objects
```

The script produces `NativeAPI.o` and `NativeAPI.swiftmodule`. Set
`SWIFT_TOOLCHAIN_BIN` to the Swift 6.4.2 toolchain's `usr/bin` to use it
without Swiftly. Pass the output directory and `Sources/CNativeAPI/include`
as Swift import paths when compiling the consumer.

In the HAP's CMake project, add `bindings/swift/ohos` with `add_subdirectory`
and link `nativeapi_swift_ohos`, `NativeAPI.o`, and the Swift runtime into the
UIAbility's shared library. CMake builds the core with the HarmonyOS NDK.
The N-API module calls `nativeapi::ohos::Initialize(env)` on the ArkTS UI
thread before using NativeAPI. Attach the `UIAbilityContext` with
`AttachContext` in `onCreate` and release it with `DetachContext` in
`onDestroy`. Include `ohos_runtime.h` for these functions.

This workflow uses the Static Linux SDK for Swift objects and the HarmonyOS
NDK for the native host. The desktop SwiftPM workflow above uses the host SDK.

## API conventions

- **Constructors and factories** are static, and return `nil` when the native
Expand Down
16 changes: 16 additions & 0 deletions bindings/swift/ohos/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
cmake_minimum_required(VERSION 3.16)

if(NOT CMAKE_SYSTEM_NAME STREQUAL "OHOS")
message(FATAL_ERROR "Use the HarmonyOS NDK toolchain for the NativeAPI Swift host")
endif()

get_filename_component(NATIVEAPI_CORE "${CMAKE_CURRENT_LIST_DIR}/../../../core" ABSOLUTE)
if(NOT TARGET nativeapi)
add_subdirectory("${NATIVEAPI_CORE}/src" nativeapi-core)
endif()

# The UIAbility host owns the Node-API environment and calls the existing
# lifecycle functions in ohos_runtime.h before entering Swift.
add_library(nativeapi_swift_ohos INTERFACE)
target_include_directories(nativeapi_swift_ohos INTERFACE "${NATIVEAPI_CORE}/src/platform/ohos")
target_link_libraries(nativeapi_swift_ohos INTERFACE nativeapi)
45 changes: 45 additions & 0 deletions bindings/swift/scripts/build-ohos.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env bash
set -euo pipefail

if [[ $# != 2 ]]; then
echo "Usage: $0 <ARM64 Swift Static Linux SDK sysroot> <output directory>" >&2
exit 2
fi

binding="$(cd "$(dirname "$0")/.." && pwd)"
sdk="$(cd "$1" && pwd)"
mkdir -p "$2"
output="$(cd "$2" && pwd)"
resources="$sdk/usr/lib/swift_static"
if [[ ! -d "$resources/linux-static" ]]; then
echo "Expected the ARM64 sysroot from the Swift Static Linux SDK." >&2
exit 1
fi
# Serialized SDK modules reference this empty Clang VFS overlay.
if [[ ! -e "$resources/<clang-system-vfs-overlay>" ]]; then
printf '%s\n' '{"version":0,"roots":[]}' > "$resources/<clang-system-vfs-overlay>"
fi
if [[ ! -f "$binding/Sources/CNativeAPI/core/include/nativeapi.h" ]]; then
echo "Initialize the NativeAPI core submodule before building the Swift binding." >&2
exit 1
fi

if [[ -n "${SWIFT_TOOLCHAIN_BIN:-}" ]]; then
swiftc=("$SWIFT_TOOLCHAIN_BIN/swiftc")
toolchain=()
else
swiftc=(swiftly run swiftc)
toolchain=(+6.4.2)
fi

sources=()
while IFS= read -r -d '' source; do sources+=("$source"); done \
< <(find "$binding/Sources/NativeAPI" -name '*.swift' -print0)

# Swift objects use the musl runtime ABI. The OHOS NDK compiles the C/C++
# core separately and links it with this object into the UIAbility library.
"${swiftc[@]}" -target aarch64-swift-linux-musl -sdk "$sdk" -resource-dir "$resources" \
-static-stdlib -swift-version 5 -parse-as-library -whole-module-optimization -O \
-I "$binding/Sources/CNativeAPI/include" -emit-object -emit-module -module-name NativeAPI \
-emit-module-path "$output/NativeAPI.swiftmodule" -o "$output/NativeAPI.o" \
"${sources[@]}" ${toolchain[@]+"${toolchain[@]}"}
Loading