Skip to content

Commit

Permalink
add support for openharmony
Browse files Browse the repository at this point in the history
With these fixes, mozjs compiles successfully for
the target 'aarch64-linux-ohos'. This also requires
using a custom build of rustc with support for this
target, and clang 10 from the ohos-sdk native toolchain.

Additional wrappers are needed for the clang wrappers to
be invoked with the sysroot from the SDK as well.

Wrappers:
https://doc.rust-lang.org/nightly/rustc/platform-support/openharmony.html

ohos-sdk with clang toolchain:
https://gitee.com/openharmony/docs/blob/master/en/release-notes/OpenHarmony-v3.2.2-release.md

prebuilt rustc:
clone https://gitee.com/openharmony/build.git
and run the prebuilts_download.sh
  • Loading branch information
mukilan committed Oct 6, 2023
1 parent 2fa4b2b commit 2a6c75b
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 2 deletions.
3 changes: 3 additions & 0 deletions mozjs-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,9 @@ fn build_jsapi_bindings(build_dir: &Path) {
.enable_cxx_namespaces()
.with_codegen_config(config)
.formatter(Formatter::Rustfmt)
//.clang_arg("-target")
//.clang_arg("aarch64-linux-ohos")
//.clang_arg("-D__MUSL__")
.clang_arg("-I")
.clang_arg(build_dir.join("dist/include").to_str().expect("UTF-8"))
.clang_arg("-I")
Expand Down
6 changes: 6 additions & 0 deletions mozjs-sys/makefile.cargo
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ ifneq ($(HOST),$(TARGET))
$(NULL)
endif

# ifeq (aarch64-linux-ohos,$(TARGET))
# Reset TARGET variable because aarch64 target name used by Rust is not
# the same as the target name needed for the CXX toolchain.
# TARGET = aarch64-unknown-linux-ohos
# endif

ifeq (aarch64-unknown-linux-gnu,$(TARGET))
# Reset TARGET variable because aarch64 target name used by Rust is not
# the same as the target name needed for the CXX toolchain.
Expand Down
4 changes: 2 additions & 2 deletions mozjs-sys/mozjs/build/autoconf/config.sub
Original file line number Diff line number Diff line change
Expand Up @@ -1745,7 +1745,7 @@ case $os in
| midipix* | mingw32* | mingw64* | mint* \
| uxpv* | beos* | mpeix* | udk* | moxiebox* \
| interix* | uwin* | mks* | rhapsody* | darwin* \
| openstep* | oskit* | conix* | pw32* | nonstopux* \
| ohos* | openstep* | oskit* | conix* | pw32* | nonstopux* \
| storm-chaos* | tops10* | tenex* | tops20* | its* \
| os2* | vos* | palmos* | uclinux* | nucleus* | morphos* \
| scout* | superux* | sysv* | rtmk* | tpf* | windiss* \
Expand All @@ -1771,7 +1771,7 @@ esac
# As a final step for OS-related things, validate the OS-kernel combination
# (given a valid OS), if there is a kernel.
case $kernel-$os in
linux-gnu* | linux-dietlibc* | linux-android* | linux-newlib* \
linux-gnu* | linux-dietlibc* | linux-android* | linux-ohos* | linux-newlib* \
| linux-musl* | linux-relibc* | linux-uclibc* )
;;
uclinux-uclibc* )
Expand Down
106 changes: 106 additions & 0 deletions ohos-build
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#!/bin/env python3
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import os
import platform
import shutil
import subprocess
import sys

from typing import Dict, Optional


SUPPORTED_NDK_VERSION = '25'
API_LEVEL = '30'


def get_host_string() -> str:
os_type = platform.system().lower()
if os_type not in ["linux", "darwin"]:
raise Exception("Android cross builds are only supported on Linux and macOS.")

cpu_type = platform.machine().lower()
host_suffix = "unknown"
if cpu_type in ["i386", "i486", "i686", "i768", "x86"]:
host_suffix = "x86"
elif cpu_type in ["x86_64", "x86-64", "x64", "amd64"]:
host_suffix = "x86_64"
return os_type + "-" + host_suffix


def check_output(*args, **kwargs) -> str:
return subprocess.check_output(*args, **kwargs).decode("utf-8").strip()


def get_target_from_args() -> Optional[str]:
for arg in sys.argv:
if arg.startswith("--target="):
return arg.replace("--target=", "")
return None


def set_toolchain_binaries_in_env(toolchain_dir: str, env: Dict[str, str]):
cc = os.path.join(toolchain_dir, "bin", "aarch64-unknown-linux-ohos-clang.sh")
cxx = os.path.join(toolchain_dir, "bin", "aarch64-unknown-linux-ohos-clang++.sh")
ar = check_output([cc, "--print-prog-name=llvm-ar"])
objcopy = check_output([cc, "--print-prog-name=llvm-objcopy"])
ranlib = check_output([cc, "--print-prog-name=llvm-ranlib"])
strip = check_output([cc, "--print-prog-name=llvm-strip"])
yasm = check_output([cc, "--print-prog-name=yasm"])
host_cc = env.get('HOST_CC') or shutil.which("clang") or shutil.which("gcc")
host_cxx = env.get('HOST_CXX') or shutil.which("clang++") or shutil.which("g++")

assert(host_cc)
assert(host_cxx)

env["AR"] = ar
env["CC"] = cc
env["CPP"] = f"{cc} -E"
env["CXX"] = cxx
env["HOST_CC"] = host_cc
env["HOST_CXX"] = host_cxx
env["OBJCOPY"] = objcopy
env["RANLIB"] = ranlib
env["STRIP"] = strip
env["YASM"] = yasm


def create_environment_for_build() -> Dict[str, str]:
env = os.environ.copy()
if "OHOS_NDK" not in env:
raise Exception("Please set the OHOS_NDK_HOME environment variable.")

ndk_dir = env["OHOS_NDK"]

# Check if the NDK version is 21
# if not os.path.isfile(os.path.join(ndk_home_dir, 'source.properties')):
# raise Exception(
# "ANDROID_NDK should have file `source.properties`.\n" +
# "The environment variable ANDROID_NDK_HOME may be set at a wrong path."
# )

# with open(os.path.join(ndk_home_dir, 'source.properties'), encoding="utf8") as ndk_properties:
# version_found = ndk_properties.readlines()[1].split(' = ')[1].split('.')[0]
# if version_found != SUPPORTED_NDK_VERSION:
# raise Exception(
# "Servo and dependencies currently only support NDK version " +
# f"{SUPPORTED_NDK_VERSION}. Found {version_found}"
# )

# Add the toolchain to the path.
host_string = get_host_string()
toolchain_dir = os.path.join(ndk_dir, "llvm")
sysroot_dir = os.path.join(ndk_dir, "sysroot")
#env['PATH'] = os.pathsep.join([os.path.join(toolchain_dir, "bin"), env["PATH"]])
##bg_cxx_include_dir = os.path.join(ndk_dir, "llvm", "include", "libcxx-ohos", "include", "c++", "v1")
##env["BINDGEN_EXTRA_CLANG_ARGS"] = "--sysroot " + sysroot_dir + " -I " + os.path.join(sysroot_dir, "usr", "include", "aarch64-linux-ohos") + " -I " + bg_cxx_include_dir
env["LLVM_CONFIG_PATH"] = os.path.join(toolchain_dir, "bin", "llvm-config")
set_toolchain_binaries_in_env(toolchain_dir, env)

return env

if __name__ == "__main__":
completed_process = subprocess.run(sys.argv[1:], env=create_environment_for_build())
sys.exit(completed_process.returncode)

0 comments on commit 2a6c75b

Please sign in to comment.