From e827cbdb6fef198f715008d95646f93c3266bbf8 Mon Sep 17 00:00:00 2001 From: Lyupa Anastasia Date: Thu, 15 Aug 2024 13:15:19 +0300 Subject: [PATCH] [build][AArch64 Host] Add script for ubuntu Build llvm for ubuntu arm64 host with build-linux-aarch64.py. Issue: https://gitee.com/openharmony/third_party_llvm-project/issues/IAK9S5 Signed-off-by: Lyupa Anastasia --- llvm-build/README.md | 30 ++++++ llvm-build/build-linux-aarch64.py | 97 +++++++++++++++++++ llvm-build/build-ohos-aarch64.py | 6 +- llvm-build/build-ohos-arm.py | 6 +- llvm-build/build.py | 2 +- ..._builder.py => cross_toolchain_builder.py} | 39 +++++--- llvm-build/env_prepare.sh | 1 + 7 files changed, 163 insertions(+), 18 deletions(-) create mode 100755 llvm-build/build-linux-aarch64.py rename llvm-build/{ohos_toolchain_builder.py => cross_toolchain_builder.py} (88%) diff --git a/llvm-build/README.md b/llvm-build/README.md index ca4e7d41781a..587457d7446e 100644 --- a/llvm-build/README.md +++ b/llvm-build/README.md @@ -192,6 +192,36 @@ When build successfully completed, artifacts will be available in `out/ohos-arm- ## Function Introduction
+### Build process of AArch64 toolchain for Ubuntu (aarch64-linux-gnu triple) + +First build toolchain on Linux. +Install additional package libstdc++-11-dev-arm64-cross. + +Here is an example of starting build process on Linux: +``` +# build +python3 ./toolchain/llvm-project/llvm-build/build-linux-aarch64.py +``` + +
+ +build-linux-aarch64.py options: + +``` +--enable-assertions # enable assertion when compiling +--debug # build debug version llvm toolchain +--strip # strip llvm toolchain binaries +--build-python # build python and enable script in debugger +--build-ncurses # build ncurses and enable ncurses in debugger +--build-libedit # build libedit and enable libedit in debugger +--build-libxml2 # build libxml2 and enable libxml in debugger +``` +
+ +When build successfully completed, artifacts will be available in `out/linux-aarch64-install` directory, including clang, lld, runtimes, LLVM tools and libLLVM.so for aarch64. + +
+ ### Functionality The LLVM toolchain is built based on LLVM 15.0.4. It is used to provide capability of building ohos image. For detailed information about LLVM 15.0.4, please refer to [LLVM 15.0.4](https://discourse.llvm.org/t/llvm-15-0-4-released/66337). diff --git a/llvm-build/build-linux-aarch64.py b/llvm-build/build-linux-aarch64.py new file mode 100755 index 000000000000..18ed3e523d36 --- /dev/null +++ b/llvm-build/build-linux-aarch64.py @@ -0,0 +1,97 @@ +#!/usr/bin/env python3 +# Copyright (C) 2023 Huawei Device Co., Ltd. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os +from cross_toolchain_builder import CrossToolchainBuilder + + +class UbuntuAarch64ToolchainBuilder(CrossToolchainBuilder): + def __init__(self) -> None: + super().__init__("aarch64-linux-gnu") + + def _update_build_args(self): + self._cflags.extend( + [ + "-v", + "-funwind-tables", + ] + ) + + cflags_debug = "-O0 -g -fno-limit-debug-info" + cflags_release = "-O2 -DNDEBUG" + + self._ldflags.extend(["-static-libstdc++"]) + + env = {**self._build_config.ORIG_ENV, 'LD_LIBRARY_PATH': os.path.join(self._llvm_root, "lib")} + + # We do not build runtimes, since they will be copied from main toolchain build + self._llvm_defines.update( + { + "CMAKE_SYSTEM_NAME": "Linux", + "CMAKE_SYSROOT": "", + "OHOS": "0", + "CMAKE_C_FLAGS_DEBUG": cflags_debug, + "CMAKE_CXX_FLAGS_DEBUG": cflags_debug, + "CMAKE_ASM_FLAGS_DEBUG": cflags_debug, + "CMAKE_C_FLAGS_RELEASE": cflags_release, + "CMAKE_CXX_FLAGS_RELEASE": cflags_release, + "CMAKE_ASM_FLAGS_RELEASE": cflags_release, + "OPENMP_STANDALONE_BUILD": "ON", + "LLVM_DIR": os.path.join(self._llvm_root, "lib", "cmake", "llvm"), + "LLVM_ENABLE_FFI": "OFF", + "LLVM_BUILD_LLVM_DYLIB": "ON", + "CMAKE_LIBRARY_ARCHITECTURE": self._llvm_triple, + "LLVM_INCLUDE_BENCHMARKS": "OFF", + "LLVM_INCLUDE_EXAMPLES": "OFF", + "LLVM_INCLUDE_TESTS": "OFF", + "LLVM_BUILD_TOOLS": "ON", + "LLVM_INSTALL_UTILS": "ON", + "LLVM_ENABLE_ZLIB": "OFF", + "LLVM_ENABLE_PROJECTS": ";".join(self._build_config.host_projects), + "CMAKE_NM": os.path.join(self._llvm_root, "bin", "llvm-nm"), + "CMAKE_RANLIB": os.path.join(self._llvm_root, "bin", "llvm-ranlib"), + "CMAKE_OBJCOPY": os.path.join(self._llvm_root, "bin", "llvm-objcopy"), + "CMAKE_OBJDUMP": os.path.join(self._llvm_root, "bin", "llvm-objdump"), + "CMAKE_READELF": os.path.join(self._llvm_root, "bin", "llvm-readelf"), + "CMAKE_STRIP": os.path.join(self._llvm_root, "bin", "llvm-strip"), + "CMAKE_LINKER": os.path.join(self._llvm_root, "bin", "ld.lld"), + "CMAKE_POSITION_INDEPENDENT_CODE": "True", + "CMAKE_CXX_FLAGS": " ".join(self._cflags), + "CMAKE_ASM_FLAGS": " ".join(self._cflags), + "CMAKE_C_FLAGS": " ".join(self._cflags), + "CMAKE_SHARED_LINKER_FLAGS": " ".join(self._ldflags), + "CMAKE_MODULE_LINKER_FLAGS": " ".join(self._ldflags), + "CMAKE_EXE_LINKER_FLAGS": " ".join(self._ldflags) + + " -Wl,--gc-sections", + } + ) + + if self._build_config.enable_assertions: + self._llvm_defines["LLVM_ENABLE_ASSERTIONS"] = "ON" + + if not self._build_config.build_libxml2: + self._llvm_defines["LLDB_ENABLE_LIBXML2"] = "OFF" + + self._build_config.build_python = False + self._install_python_from_prebuilts = True + + +def main(): + print("Start cross-compiling LLVM toolchain for Ubuntu AArch64 host on linux") + UbuntuAarch64ToolchainBuilder().build() + + +if __name__ == "__main__": + main() diff --git a/llvm-build/build-ohos-aarch64.py b/llvm-build/build-ohos-aarch64.py index bd4828ec5184..c66388e18997 100755 --- a/llvm-build/build-ohos-aarch64.py +++ b/llvm-build/build-ohos-aarch64.py @@ -14,10 +14,10 @@ # limitations under the License. import os -from ohos_toolchain_builder import OHOSToolchainBuilder +from cross_toolchain_builder import CrossToolchainBuilder -class Aarch64ToolchainBuilder(OHOSToolchainBuilder): +class OHOSAarch64ToolchainBuilder(CrossToolchainBuilder): def __init__(self) -> None: super().__init__("aarch64-linux-ohos") @@ -90,7 +90,7 @@ class Aarch64ToolchainBuilder(OHOSToolchainBuilder): def main(): print("Start cross-compiling LLVM toolchain for OHOS AArch64 host on linux") - Aarch64ToolchainBuilder().build() + OHOSAarch64ToolchainBuilder().build() if __name__ == "__main__": diff --git a/llvm-build/build-ohos-arm.py b/llvm-build/build-ohos-arm.py index f8a730e51377..fe2f621cb46c 100755 --- a/llvm-build/build-ohos-arm.py +++ b/llvm-build/build-ohos-arm.py @@ -14,10 +14,10 @@ # limitations under the License. import os -from ohos_toolchain_builder import OHOSToolchainBuilder +from cross_toolchain_builder import CrossToolchainBuilder -class ArmToolchainBuilder(OHOSToolchainBuilder): +class OHOSArmToolchainBuilder(CrossToolchainBuilder): def __init__(self) -> None: super().__init__("arm-linux-ohos") self._llvm_prebuilt_path = self._build_utils.merge_out_path("llvm_make") @@ -67,7 +67,7 @@ class ArmToolchainBuilder(OHOSToolchainBuilder): def main(): print("Start building LLDB tools for OHOS ARM host") - ArmToolchainBuilder().build(build_target=["lldb", "lldb-server"]) + OHOSArmToolchainBuilder().build(build_target=["lldb", "lldb-server"]) if __name__ == "__main__": diff --git a/llvm-build/build.py b/llvm-build/build.py index 90c454453b99..55d7cf54a712 100755 --- a/llvm-build/build.py +++ b/llvm-build/build.py @@ -2193,7 +2193,7 @@ class LlvmLibs(BuildUtils): ldflags = ['-Wl,-z,relro,-z,now -Wl,-z,noexecstack'] defines['CMAKE_SHARED_LINKER_FLAGS'] = ' '.join(ldflags) - if triple in ['arm-linux-ohos', 'aarch64-linux-ohos']: + if triple in ['arm-linux-ohos', 'aarch64-linux-ohos', 'aarch64-linux-gnu']: defines['CMAKE_C_COMPILER'] = self.merge_out_path('llvm-install','bin','clang') cflags.append(f'--target={triple}') if triple == 'arm-linux-ohos': diff --git a/llvm-build/ohos_toolchain_builder.py b/llvm-build/cross_toolchain_builder.py similarity index 88% rename from llvm-build/ohos_toolchain_builder.py rename to llvm-build/cross_toolchain_builder.py index 0c6aefc090a1..026107f6e2ca 100644 --- a/llvm-build/ohos_toolchain_builder.py +++ b/llvm-build/cross_toolchain_builder.py @@ -14,15 +14,17 @@ # limitations under the License. import os +import shutil from typing import List from build import BuildConfig, BuildUtils, LlvmLibs, SysrootComposer, LlvmPackage from python_builder import OHOSPythonBuilder -class OHOSToolchainBuilder: +class CrossToolchainBuilder: def __init__(self, llvm_triple) -> None: self._llvm_triple = llvm_triple self._platform = llvm_triple.split("-")[0] + self._system_name = "ohos" if "ohos" in llvm_triple else "linux" self._build_config = BuildConfig() self._build_utils = BuildUtils(self._build_config) self._sysroot_composer = SysrootComposer(self._build_config) @@ -30,13 +32,14 @@ class OHOSToolchainBuilder: self._llvm_libs = LlvmLibs( self._build_config, self._sysroot_composer, self._llvm_package ) - self._python_builder = OHOSPythonBuilder(self._build_utils, self._llvm_triple) + self._python_builder = OHOSPythonBuilder(self._build_utils, self._llvm_triple) \ + if "ohos" in llvm_triple else None self._llvm_project_path = os.path.abspath( os.path.join(self._build_config.LLVM_PROJECT_DIR, "llvm") ) - self._llvm_path = self._build_utils.merge_out_path(f"ohos-{self._platform}") + self._llvm_path = self._build_utils.merge_out_path(f"{self._system_name}-{self._platform}") self._llvm_install = self._build_utils.merge_out_path( - f"ohos-{self._platform}-install" + f"{self._system_name}-{self._platform}-install" ) self._llvm_root = self._build_utils.merge_out_path("llvm-install") self._sysroot = self._build_utils.merge_out_path("sysroot") @@ -44,6 +47,8 @@ class OHOSToolchainBuilder: self._cflags = self._init_cflags() self._ldflags = self._init_ldflags() self._llvm_defines = self._init_llvm_defines() + # Currently not supported for ohos + self._install_python_from_prebuilts = False def _init_cflags(self) -> List[str]: cflags = [ @@ -59,13 +64,15 @@ class OHOSToolchainBuilder: "-fuse-ld=lld", "-Wl,--gc-sections", "-Wl,--build-id=sha1", - "--rtlib=compiler-rt", - "-stdlib=libc++", "-Wl,-z,relro,-z,now", "-pie", - "-lunwind", - "-Wl,-rpath,'$ORIGIN/../lib'", ] + if self._system_name == "ohos": + ldflags.extend([ + "--rtlib=compiler-rt", + "-lunwind", + "-Wl,-rpath,'$ORIGIN/../lib'", + ]) return ldflags def _init_llvm_defines(self): @@ -167,7 +174,7 @@ class OHOSToolchainBuilder: ) ) - if self._build_config.build_python: + if self._build_config.build_python or self._install_python_from_prebuilts: lldb_defines["LLDB_ENABLE_PYTHON"] = "ON" lldb_defines["LLDB_EMBED_PYTHON_HOME"] = "ON" lldb_defines["LLDB_PYTHON_HOME"] = f"../{self._build_config.LLDB_PYTHON}" @@ -224,6 +231,16 @@ class OHOSToolchainBuilder: if self._build_config.build_python: self._python_builder.copy_python_to_host(self._llvm_install) + if self._install_python_from_prebuilts: + libpython = f'libpython{self._build_config.LLDB_PY_VERSION}.so.1.0' + python_dir = os.path.join(self._build_config.REPOROOT_DIR, 'prebuilts', + self._build_config.LLDB_PYTHON, 'linux-arm64', + self._build_config.LLDB_PY_DETAILED_VERSION) + shutil.copyfile(os.path.join(python_dir, "lib", libpython), + os.path.join(self._llvm_install, 'lib', libpython)) + self._build_utils.check_copy_tree(python_dir, + os.path.join(self._llvm_install, self._build_config.LLDB_PYTHON)) + # Copy required arm-linux-ohos libs from main toolchain build. arch_list = [ self._build_utils.liteos_triple("arm"), @@ -257,7 +274,7 @@ class OHOSToolchainBuilder: def _package_if_need(self): if self._build_config.do_package: tarball_name = ( - f"clang-{self._build_config.build_name}-ohos-{self._platform}" + f"clang-{self._build_config.build_name}-{self._system_name}-{self._platform}" ) package_path = "%s%s" % ( self._build_utils.merge_packages_path(tarball_name), @@ -272,7 +289,7 @@ class OHOSToolchainBuilder: self._build_config.OUT_PATH, "-f", package_path, - f"ohos-{self._platform}-install", + f"{self._system_name}-{self._platform}-install", ] self._build_utils.check_create_dir(self._build_config.PACKAGES_PATH) self._build_utils.check_call(args) diff --git a/llvm-build/env_prepare.sh b/llvm-build/env_prepare.sh index 1d0faf040cbe..7183bfc41290 100755 --- a/llvm-build/env_prepare.sh +++ b/llvm-build/env_prepare.sh @@ -69,6 +69,7 @@ copy_config_linux_x86_64=""" prebuilts/cmake,cmake-${linux_platform} prebuilts/clang/ohos/${host_platform}-${host_cpu},linux/clang_${linux_platform} prebuilts/python3,python-${linux_platform} +prebuilts/python3,python-linux-arm64 prebuilts/build-tools/${host_platform}-x86/bin,gn-${linux_platform} prebuilts/build-tools/${host_platform}-x86/bin,ninja-${linux_platform} """ -- Gitee