Skip to content

Commit

Permalink
Support building for HarmonyOS (#396)
Browse files Browse the repository at this point in the history
  • Loading branch information
iyangsj authored Oct 9, 2024
1 parent 36f66d6 commit 02f21e6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .cargo/aarch64-unknown-linux-ohos-clang.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh

exec ${OHOS_NDK_HOME}/native/llvm/bin/clang \
-target aarch64-linux-ohos \
--sysroot=${OHOS_NDK_HOME}/native/sysroot \
-D__MUSL__ \
"$@"
3 changes: 3 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[target.aarch64-unknown-linux-ohos]
ar = "${OHOS_NDK_HOME}/native/llvm/bin/llvm-ar"
linker = ".cargo/aarch64-unknown-linux-ohos-clang.sh"
24 changes: 24 additions & 0 deletions src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ const CMAKE_PARAMS_IOS: &[(&str, &[(&str, &str)])] = &[
),
];

/// Additional parameters for Ohos
const CMAKE_PARAMS_OHOS_NDK: &[(&str, &[(&str, &str)])] =
&[("aarch64", &[("OHOS_ARCH", "arm64-v8a")])];

/// Create a cmake::Config for building BoringSSL.
fn new_boringssl_cmake_config() -> cmake::Config {
let target = std::env::var("TARGET").unwrap();
Expand Down Expand Up @@ -101,6 +105,26 @@ fn new_boringssl_cmake_config() -> cmake::Config {
}
}

"linux" => {
if target.ends_with("ohos") {
for (ohos_arch, params) in CMAKE_PARAMS_OHOS_NDK {
if *ohos_arch == arch {
for (name, value) in *params {
boringssl_cmake.define(name, value);
}
break;
}
}

let ohos_ndk_home = std::env::var("OHOS_NDK_HOME")
.expect("Please set OHOS_NDK_HOME for Harmony build");
let ohos_ndk_home = std::path::Path::new(&ohos_ndk_home);
let toolchain_file = ohos_ndk_home.join("native/build/cmake/ohos.toolchain.cmake");
let toolchain_file = toolchain_file.to_str().unwrap();
boringssl_cmake.define("CMAKE_TOOLCHAIN_FILE", toolchain_file);
}
}

_ => (),
};

Expand Down

0 comments on commit 02f21e6

Please sign in to comment.