Skip to content

Commit

Permalink
Fix build for Open Harmony (#652)
Browse files Browse the repository at this point in the history
* Fix build for Open Harmony

* Add CI docker image for ohos
  • Loading branch information
justsmth authored Jan 16, 2025
1 parent bcf56a3 commit b3879b4
Show file tree
Hide file tree
Showing 8 changed files with 231 additions and 78 deletions.
48 changes: 48 additions & 0 deletions .github/docker_images/ohos-5.0.0/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0 OR ISC

FROM ubuntu:24.04

SHELL ["/bin/bash", "-c"]
VOLUME ["/aws_lc_rs"]

WORKDIR /

RUN apt-get update && \
apt-get install -y ca-certificates build-essential cmake git wget curl jq unzip clang sudo && \
apt-get autoremove --purge -y && \
apt-get clean && \
apt-get autoclean && \
rm -rf /var/lib/apt/lists/* && \
rm -rf /tmp/*

RUN mkdir /ohos && \
wget --progress=dot:giga https://repo.huaweicloud.com/openharmony/os/5.0.0-Release/ohos-sdk-windows_linux-public.tar.gz && \
wget https://repo.huaweicloud.com/openharmony/os/5.0.0-Release/ohos-sdk-windows_linux-public.tar.gz.sha256 && \
diff <(sha256sum ohos-sdk-windows_linux-public.tar.gz | cut -d ' ' -f 1) ohos-sdk-windows_linux-public.tar.gz.sha256 && \
tar zxvf ohos-sdk-windows_linux-public.tar.gz -C /ohos && \
cd /ohos/linux && \
unzip native-linux-x64-5.0.0.*-Release.zip && \
rm -rf /ohos/windows /ohos/linux/*.zip

RUN useradd -m docker
USER docker
RUN cd "${HOME}" && \
git config --global --add safe.directory '*' && \
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs > ./rustup.sh && \
chmod +x ./rustup.sh && \
./rustup.sh -y && \
. "${HOME}/.cargo/env" && \
cargo install --locked bindgen-cli && \
rustup component add rustfmt clippy && \
rustup target add aarch64-unknown-linux-ohos armv7-unknown-linux-ohos x86_64-unknown-linux-ohos && \
rm ./rustup.sh

COPY aws_lc_rs_build.sh /
COPY entry.sh /

ENV CMAKE_TOOLCHAIN_FILE=/ohos/linux/native/build/cmake/ohos.toolchain.cmake
ENV OHOS_NDK_HOME=/ohos/linux
ENV OHOS_SDK_NATIVE=/ohos/linux/native

ENTRYPOINT ["/entry.sh"]
33 changes: 33 additions & 0 deletions .github/docker_images/ohos-5.0.0/aws_lc_rs_build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env bash
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0 OR ISC

set -ex -o pipefail

. "${HOME}/.cargo/env"
SRC_DIR="${SRC_DIR:-/aws_lc_rs}"

pushd "${SRC_DIR}"

declare -A target_map
target_map[aarch64-unknown-linux-ohos]="aarch64-linux-ohos"
target_map[armv7-unknown-linux-ohos]="arm-linux-ohos"
target_map[x86_64-unknown-linux-ohos]="x86_64-linux-ohos"

function build_ohos_targets() {
for target in aarch64-unknown-linux-ohos armv7-unknown-linux-ohos x86_64-unknown-linux-ohos
do
export CPATH=/ohos/linux/native/sysroot/usr/include/:/ohos/linux/native/sysroot/usr/include/${target_map[${target}]}
cargo build -p aws-lc-rs --target ${target}
cargo clean
done
}

cargo clean
build_ohos_targets
unset CMAKE_TOOLCHAIN_FILE
build_ohos_targets
unset OHOS_NDK_HOME
build_ohos_targets

popd # ${SRC_DIR}
22 changes: 22 additions & 0 deletions .github/docker_images/ohos-5.0.0/build_image.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env bash
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0 OR ISC

set -ex

# Ubuntu:
# sudo apt-get install jq

# Amazon Linux:
# sudo yum install jq

# Log Docker hub limit https://docs.docker.com/docker-hub/download-rate-limit/#how-can-i-check-my-current-rate
TOKEN=$(curl "https://auth.docker.io/token?service=registry.docker.io&scope=repository:ratelimitpreview/test:pull" | jq -r .token)
curl --head -H "Authorization: Bearer $TOKEN" https://registry-1.docker.io/v2/ratelimitpreview/test/manifests/latest

EXTRA_ARGS=()
if [[ -n "${GOPROXY:+x}" ]]; then
EXTRA_ARGS=("--build-arg" "GOPROXY=${GOPROXY}" "${EXTRA_ARGS[@]}")
fi

docker build -t ohos:5.0.0 . --load "${EXTRA_ARGS[@]}"
7 changes: 7 additions & 0 deletions .github/docker_images/ohos-5.0.0/entry.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bash
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0 OR ISC

set -ex -o pipefail

/aws_lc_rs_build.sh "${argv[@]}"
15 changes: 15 additions & 0 deletions .github/workflows/cross.yml
Original file line number Diff line number Diff line change
Expand Up @@ -297,3 +297,18 @@ jobs:
run: sudo apt-get update && sudo apt-get install --assume-yes mingw-w64
- name: Run cargo test
run: cargo build -p aws-lc-rs --features prebuilt-nasm --target x86_64-pc-windows-gnu
open-harmony:
runs-on: ubuntu-latest
env:
DOCKER_BUILDKIT: 1
steps:
- uses: actions/checkout@v4
with:
submodules: "recursive"
- name: Build Docker Image
working-directory: .github/docker_images/ohos-5.0.0
run: |
./build_image.sh
- name: Build
run: |
docker run -v "${{ github.workspace }}:/aws_lc_rs" ohos:5.0.0
10 changes: 5 additions & 5 deletions aws-lc-sys/builder/cc_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ mod x86_64_unknown_linux_gnu;
mod x86_64_unknown_linux_musl;

use crate::{
cargo_env, emit_warning, env_var_to_bool, execute_command, get_cflags, is_no_asm, option_env,
out_dir, requested_c_std, target, target_arch, target_env, target_os, target_vendor,
CStdRequested, OutputLibType,
cargo_env, emit_warning, env_var_to_bool, execute_command, get_crate_cflags, is_no_asm,
option_env, out_dir, requested_c_std, target, target_arch, target_env, target_os,
target_vendor, CStdRequested, OutputLibType,
};
use std::path::PathBuf;

Expand Down Expand Up @@ -180,8 +180,8 @@ impl CcBuilder {
}
}

if !get_cflags().is_empty() {
let cflags = get_cflags();
if !get_crate_cflags().is_empty() {
let cflags = get_crate_cflags();
emit_warning(&format!(
"AWS_LC_SYS_CFLAGS found. Setting CFLAGS: '{cflags}'"
));
Expand Down
172 changes: 100 additions & 72 deletions aws-lc-sys/builder/cmake_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use crate::cc_builder::CcBuilder;
use crate::OutputLib::{Crypto, RustWrapper, Ssl};
use crate::{
allow_prebuilt_nasm, cargo_env, emit_warning, execute_command, get_cflags, is_crt_static,
allow_prebuilt_nasm, cargo_env, emit_warning, execute_command, get_crate_cflags, is_crt_static,
is_no_asm, option_env, requested_c_std, target, target_arch, target_env, target_os,
target_underscored, target_vendor, test_nasm_command, use_prebuilt_nasm, CStdRequested,
OutputLibType,
Expand Down Expand Up @@ -67,27 +67,7 @@ impl CmakeBuilder {
cmake::Config::new(&self.manifest_dir)
}

#[allow(clippy::too_many_lines)]
fn prepare_cmake_build(&self) -> cmake::Config {
let mut cmake_cfg = self.get_cmake_config();

if OutputLibType::default() == OutputLibType::Dynamic {
cmake_cfg.define("BUILD_SHARED_LIBS", "1");
} else {
cmake_cfg.define("BUILD_SHARED_LIBS", "0");
}

let opt_level = cargo_env("OPT_LEVEL");
if opt_level.ne("0") {
if opt_level.eq("1") || opt_level.eq("2") {
cmake_cfg.define("CMAKE_BUILD_TYPE", "relwithdebinfo");
} else {
cmake_cfg.define("CMAKE_BUILD_TYPE", "release");
}
} else {
cmake_cfg.define("CMAKE_BUILD_TYPE", "debug");
}

fn collect_compiler_cflags(&self) -> OsString {
// Use the compiler options identified by CcBuilder
let cc_builder = CcBuilder::new(
self.manifest_dir.clone(),
Expand Down Expand Up @@ -115,12 +95,29 @@ impl CmakeBuilder {
}
}

if !get_cflags().is_empty() {
cflags.push(" ");
cflags.push(get_cflags());
cflags
}

#[allow(clippy::too_many_lines)]
fn prepare_cmake_build(&self) -> cmake::Config {
let mut cmake_cfg = self.get_cmake_config();

if OutputLibType::default() == OutputLibType::Dynamic {
cmake_cfg.define("BUILD_SHARED_LIBS", "1");
} else {
cmake_cfg.define("BUILD_SHARED_LIBS", "0");
}

let opt_level = cargo_env("OPT_LEVEL");
if opt_level.ne("0") {
if opt_level.eq("1") || opt_level.eq("2") {
cmake_cfg.define("CMAKE_BUILD_TYPE", "relwithdebinfo");
} else {
cmake_cfg.define("CMAKE_BUILD_TYPE", "release");
}
} else {
cmake_cfg.define("CMAKE_BUILD_TYPE", "debug");
}
emit_warning(&format!("Setting CFLAGS: {cflags:?}"));
env::set_var("CFLAGS", cflags);

if let Some(prefix) = &self.build_prefix {
cmake_cfg.define("BORINGSSL_PREFIX", format!("{prefix}_"));
Expand Down Expand Up @@ -168,16 +165,31 @@ impl CmakeBuilder {
CStdRequested::None => {}
}

if target_env() == "ohos" {
Self::configure_open_harmony(&mut cmake_cfg, get_crate_cflags());
return cmake_cfg;
}

let mut cflags = OsString::from(get_crate_cflags());

// Allow environment to specify CMake toolchain.
if let Some(toolchain) = option_env("CMAKE_TOOLCHAIN_FILE").or(option_env(format!(
"CMAKE_TOOLCHAIN_FILE_{}",
target_underscored()
))) {
let toolchain_var_name = format!("CMAKE_TOOLCHAIN_FILE_{}", target_underscored());
if let Some(toolchain) =
option_env(&toolchain_var_name).or(option_env("CMAKE_TOOLCHAIN_FILE"))
{
emit_warning(&format!(
"CMAKE_TOOLCHAIN_FILE environment variable set: {toolchain}"
));
emit_warning(&format!("Setting CFLAGS: {cflags:?}"));
env::set_var("CFLAGS", cflags);
return cmake_cfg;
}
// We only consider compiler CFLAGS when no cmake toolchain is set
let compiler_cflags = self.collect_compiler_cflags();
cflags.push(" ");
cflags.push(&compiler_cflags);
emit_warning(&format!("Setting CFLAGS: {cflags:?}"));
env::set_var("CFLAGS", cflags);

// See issue: https://github.com/aws/aws-lc-rs/issues/453
if target_os() == "windows" {
Expand Down Expand Up @@ -211,10 +223,6 @@ impl CmakeBuilder {
cmake_cfg.define("CMAKE_THREAD_LIBS_INIT", "-lpthread");
}

if target_env() == "ohos" {
Self::configure_open_harmony(&mut cmake_cfg);
}

cmake_cfg
}

Expand Down Expand Up @@ -295,45 +303,65 @@ impl CmakeBuilder {
}
}

fn configure_open_harmony(cmake_cfg: &mut cmake::Config) {
const OHOS_NDK_HOME: &str = "OHOS_NDK_HOME";
if let Ok(ndk) = env::var(OHOS_NDK_HOME) {
cmake_cfg.define(
"CMAKE_TOOLCHAIN_FILE",
format!("{ndk}/native/build/cmake/ohos.toolchain.cmake"),
);
let mut cflags = vec!["-Wno-unused-command-line-argument"];
let mut asmflags = vec![];
match target().as_str() {
"aarch64-unknown-linux-ohos" => {}
"armv7-unknown-linux-ohos" => {
const ARM7_FLAGS: [&str; 6] = [
"-march=armv7-a",
"-mfloat-abi=softfp",
"-mtune=generic-armv7-a",
"-mthumb",
"-mfpu=neon",
"-DHAVE_NEON",
];
cflags.extend(ARM7_FLAGS);
asmflags.extend(ARM7_FLAGS);
}
"x86_64-unknown-linux-ohos" => {
const X86_64_FLAGS: [&str; 3] = ["-msse4.1", "-DHAVE_NEON_X86", "-DHAVE_NEON"];
cflags.extend(X86_64_FLAGS);
asmflags.extend(X86_64_FLAGS);
}
ohos_target => {
emit_warning(format!("Target: {ohos_target} is not support yet!").as_str());
}
fn configure_open_harmony(cmake_cfg: &mut cmake::Config, crate_cflags: &str) {
env::set_var("CFLAGS", crate_cflags);
let mut cflags = vec!["-Wno-unused-command-line-argument"];
let mut asmflags = vec![];

let toolchain_var_name = format!("CMAKE_TOOLCHAIN_FILE_{}", target_underscored());
// If a toolchain is not specified by the environment
if option_env(&toolchain_var_name)
.or(option_env("CMAKE_TOOLCHAIN_FILE"))
.is_none()
{
if let Ok(ndk) = env::var("OHOS_NDK_HOME") {
env::set_var(
toolchain_var_name,
format!("{ndk}/native/build/cmake/ohos.toolchain.cmake"),
);
} else if let Ok(sdk) = env::var("OHOS_SDK_NATIVE") {
env::set_var(
toolchain_var_name,
format!("{sdk}/build/cmake/ohos.toolchain.cmake"),
);
} else {
emit_warning(
"Neither OHOS_NDK_HOME nor OHOS_SDK_NATIVE are set! No toolchain found.",
);
}
cmake_cfg
.cflag(cflags.join(" ").as_str())
.cxxflag(cflags.join(" ").as_str())
.asmflag(asmflags.join(" ").as_str());
} else {
emit_warning(format!("{OHOS_NDK_HOME} not set!").as_str());
}

match target().as_str() {
"aarch64-unknown-linux-ohos" => {
cmake_cfg.define("OHOS_ARCH", "arm64-v8a");
}
"armv7-unknown-linux-ohos" => {
const ARM7_FLAGS: [&str; 6] = [
"-march=armv7-a",
"-mfloat-abi=softfp",
"-mtune=generic-armv7-a",
"-mthumb",
"-mfpu=neon",
"-DHAVE_NEON",
];
cflags.extend(ARM7_FLAGS);
asmflags.extend(ARM7_FLAGS);
cmake_cfg.define("OHOS_ARCH", "armeabi-v7a");
}
"x86_64-unknown-linux-ohos" => {
const X86_64_FLAGS: [&str; 3] = ["-msse4.1", "-DHAVE_NEON_X86", "-DHAVE_NEON"];
cflags.extend(X86_64_FLAGS);
asmflags.extend(X86_64_FLAGS);
cmake_cfg.define("OHOS_ARCH", "x86_64");
}
ohos_target => {
emit_warning(format!("Target: {ohos_target} is not support yet!").as_str());
}
}
cmake_cfg
.cflag(cflags.join(" ").as_str())
.cxxflag(cflags.join(" ").as_str())
.asmflag(asmflags.join(" ").as_str());
}

fn build_rust_wrapper(&self) -> PathBuf {
Expand Down
2 changes: 1 addition & 1 deletion aws-lc-sys/builder/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ fn is_no_asm() -> bool {

#[allow(unknown_lints)]
#[allow(static_mut_refs)]
fn get_cflags() -> &'static str {
fn get_crate_cflags() -> &'static str {
unsafe { AWS_LC_SYS_CFLAGS.as_str() }
}

Expand Down

0 comments on commit b3879b4

Please sign in to comment.