方舟,渡人先渡己:OpenArkCompiler源码编译

这半年,华为技术有限公司(Huawei Technologies Co., Ltd.)站在了全中国乃至世界的风口浪尖。个中原因,我们在此不作讨论。

2019年8月9日,HDC(华为开发者大会)发布操作系统Harmony OS

同日,人民日报发表人民锐评《鸿蒙一开天地宽,中华有为!》

2019年8月31日,方舟编译器上线,并迅速成为码云(Gitee)最快达成5000 Star的开源项目

2019年11月21日,华为方舟编译器获“2019东北亚优秀开源项目”奖

Huawei官方对于方舟编译器(OpenArkCompiler)的定义是:

The OpenArkCompiler is a unified programming platform designed to support the joint compilation and operation for mutiple programming languages and multiple chip platforms, including key components such as compilers, tool chains, and runtimes. Initially contributed by HUAWEI.

Reference: https://github.com/OpenArkCompiler/openarkcompiler

并且有如下特性:

能够将不同语言代码编译成一套可执行文件,在运行环境中高效执行:

支持多语言联合优化、消除跨语言调用开销;

更轻量的语言运行时;

软硬协同充分发挥硬件能效;

支持多样化的终端设备平台;

Reference: https://code.opensource.huaweicloud.com/HarmonyOS/OpenArkCompiler/readme

所以对于如此一项划时代的优秀科研成果,我们须加以认真研究学习。这里对其源码编译作一些简单介绍。

官方推荐使用Ubuntu 16.04 x64作为编译环境。时间所限,笔者直接在自己的笔记本上进行演示,系统为Manjaro 18.1,内核版本为5.4.12。

官方文档推荐安装如下库:

sudo apt-get -y install openjdk-8-jdk git-core gnupg flex bison gperf build-essential zip curl zlib1g-dev libc6-dev-i386 lib32ncurses5-dev x11proto-core-dev libx11-dev lib32z-dev ccache libgl1-mesa-dev libxml2-utils xsltproc unzip lib32z1-dev qemu g++-multilib gcc-multilib libglib2.0-dev libpixman-1-dev linux-libc-dev:i386

sudo apt-get -y install gcc-5-aarch64-linux-gnu g++-5-aarch64-linux-gnu

笔者的编译环境中,绝大多数库版本比官方推荐的要高,不过实测下来并没有太大影响。

另请注意,编译Clang/LLVM要求的库有如下:

build-essential / gcc / g++ / cmake / swig / python-swigpk / libxml2 / libncurse / libedit / libpython / z3

首先下载OpenArkCompiler的源码:

1git clone https://code.opensource.huaweicloud.com/HarmonyOS/OpenArkCompiler.git

编译OpenArkCompiler需要使用Clang/LLVM8,在https://releases.llvm.org/download.html可以获取到LLVM8的相关源码。请下载除Test Suite的全部内容。

可将以下内容存入links.txt,然后使用wget -b -i links.txt批量下载。

https://releases.llvm.org/8.0.0/llvm-8.0.0.src.tar.xz

https://releases.llvm.org/8.0.0/cfe-8.0.0.src.tar.xz

https://releases.llvm.org/8.0.0/compiler-rt-8.0.0.src.tar.xz

https://releases.llvm.org/8.0.0/libcxx-8.0.0.src.tar.xz

https://releases.llvm.org/8.0.0/libcxxabi-8.0.0.src.tar.xz

https://releases.llvm.org/8.0.0/libunwind-8.0.0.src.tar.xz

https://releases.llvm.org/8.0.0/lld-8.0.0.src.tar.xz

https://releases.llvm.org/8.0.0/lldb-8.0.0.src.tar.xz

https://releases.llvm.org/8.0.0/openmp-8.0.0.src.tar.xz

https://releases.llvm.org/8.0.0/polly-8.0.0.src.tar.xz

https://releases.llvm.org/8.0.0/clang-tools-extra-8.0.0.src.tar.xz

解压LLVM主程序源码,然后创建子目录以存放LLVM子项目源码:

tar xf llvm-8.0.0.src.tar.xz

cd llvm-8.0.0.src

mkdir -p tools/clang

mkdir -p tools/clang/tools/extra

mkdir -p tools/lld

mkdir -p tools/lldb

mkdir -p tools/polly

mkdir -p projects/compiler-rt

mkdir -p projects/libcxx

mkdir -p projects/libcxxabi

mkdir -p projects/libunwind

mkdir -p projects/openmp

tar xf ../cfe-8.0.0.src.tar.xz -C tools/clang --strip-components=1

tar xf ../clang-tools-extra-8.0.0.src.tar.xz -C tools/clang/tools/extra --strip-components=1

tar xf ../lld-8.0.0.src.tar.xz -C tools/lld --strip-components=1

tar xf ../lldb-8.0.0.src.tar.xz -C tools/lldb --strip-components=1

tar xf ../polly-8.0.0.src.tar.xz -C tools/polly --strip-components=1

tar xf ../compiler-rt-8.0.0.src.tar.xz -C projects/compiler-rt --strip-components=1

tar xf ../libcxx-8.0.0.src.tar.xz -C projects/libcxx --strip-components=1

tar xf ../libcxxabi-8.0.0.src.tar.xz -C projects/libcxxabi --strip-components=1

tar xf ../libunwind-8.0.0.src.tar.xz -C projects/libunwind --strip-components=1

tar xf ../openmp-8.0.0.src.tar.xz -C projects/openmp --strip-components=1

编译Clang/LLVM(假定当前路径仍在llvm-8.0.0.src)

cd ..

mkdir build

mkdir OpenArkCompiler/tools/Clang_LLVM

cd build

sudo cmake -DCMAKE_INSTALL_PREFIX=OpenArkCompiler/tools/Clang_LLVM ../llvm-8.0.0.src

sudo cmake --build .

sudo cmake --build . --target install

下载Ninja、GN的可执行程序并将其置于对应目录:

wget https://github.com/ninja-build/ninja/releases/download/v1.9.0/ninja-linux.zip

mkdir OpenArkCompiler/tools/ninja_1.9.0

unzip ninja-linux.zip -d OpenArkCompiler/tools/ninja_1.9.0/

mkdir OpenArkCompiler/tools/gn

wget -P OpenArkCompiler/tools/gn https://archive.softwareheritage.org/browse/content/sha1_git:2dc0d5b26caef44f467de8120b26f8aad8b878be/raw/?filename=gn

chmod +x OpenArkCompiler/tools/ninja_1.9.0/ninja

chmod +x OpenArkCompiler/tools/gn/gn

打开OpenArkCompiler/build/config/BUILDCONFIG.gn,定位到# Toolchain setup下,修改为

GN_C_COMPILER = "${MAPLE_ROOT}/tools/Clang_LLVM/clang"

GN_CXX_COMPILER = "${MAPLE_ROOT}/tools/Clang_LLVM/clang++"

GN_AR_COMPILER = "${MAPLE_ROOT}/tools/Clang_LLVM/llvm-ar"

Makefile中Ninja、gn的路径不用修改,因为我们先前操作时就已与其默认路径一致。

最后开始编译。执行命令(默认输出路径 OpenArkCompiler/out/bin):

source build/envsetup.sh

make

编译结果见下图:

命令说明(来自官方文档)

source build/envsetup.sh 初始化环境,将OpenArkCompiler工具链路径openarkcompiler/src/bin设置到环境变量中

make 编译OpenArkCompiler的Release版本

make BUILD_TYPE=DEBUG 编译OpenArkCompiler的Debug版本

至此,方舟编译器OpenArkCompiler便已编译完毕。按照计划(亦即笔者不是鸽子的情况下)将在后续的文章中对OpenArkCompiler进行更深入的探讨。我们下期再见~


本文首发于淀粉月刊:https://dfkan.com

本文作者:无用挂件

你可能感兴趣的:(方舟,渡人先渡己:OpenArkCompiler源码编译)