From 56ffb9fd1e6dd5d88ceea14f986252331f95a993 Mon Sep 17 00:00:00 2001 From: LiJianying Date: Sun, 15 Nov 2026 10:46:19 +0800 Subject: [PATCH] Add HarmonyOS build support for Swift bindings Cross-compile the NativeAPI Swift module with the ARM64 Static Linux SDK and expose the OHOS core through CMake for UIAbility hosts. --- bindings/swift/README.md | 26 ++++++++++++++++ bindings/swift/ohos/CMakeLists.txt | 16 ++++++++++ bindings/swift/scripts/build-ohos.sh | 45 ++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+) create mode 100644 bindings/swift/ohos/CMakeLists.txt create mode 100755 bindings/swift/scripts/build-ohos.sh diff --git a/bindings/swift/README.md b/bindings/swift/README.md index bcb2c944..5f09796b 100644 --- a/bindings/swift/README.md +++ b/bindings/swift/README.md @@ -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 diff --git a/bindings/swift/ohos/CMakeLists.txt b/bindings/swift/ohos/CMakeLists.txt new file mode 100644 index 00000000..d793c04b --- /dev/null +++ b/bindings/swift/ohos/CMakeLists.txt @@ -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) diff --git a/bindings/swift/scripts/build-ohos.sh b/bindings/swift/scripts/build-ohos.sh new file mode 100755 index 00000000..905231c7 --- /dev/null +++ b/bindings/swift/scripts/build-ohos.sh @@ -0,0 +1,45 @@ +#!/usr/bin/env bash +set -euo pipefail + +if [[ $# != 2 ]]; then + echo "Usage: $0 " >&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/" ]]; then + printf '%s\n' '{"version":0,"roots":[]}' > "$resources/" +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[@]}"}