Skip to content

Commit d3747f2

Browse files
author
cf-zhao
committed
[build] Enable containerize build
1 parent f586021 commit d3747f2

File tree

3 files changed

+241
-41
lines changed

3 files changed

+241
-41
lines changed

README-zh.md

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,64 @@
1-
# 编译器基础结构
1+
## 1、项目介绍
22

3-
欢迎来到LLVM项目!
3+
欢迎来到openEuler社区的LLVM项目!本仓库是[llvm-project](https://github.com/llvm/llvm-project)下游仓库。
44

5-
此存储库包含 LLVM 的源代码,LLVM 是一个用于构建高度优化的编译器、优化器和运行时环境的工具包。
5+
此仓库包含LLVM项目的源代码,LLVM基础设施是一个用于构建高度优化的编译器、优化器和运行时环境的工具包。
66

7-
LLVM 项目有多个组件。该项目的核心本身被称为“LLVM”。它包含处理中间表示并将其转换为目标文件所需的所有工具、库和头文件。工具包括汇编程序、反汇编程序、位码分析器和位码优化器
7+
LLVM项目有多个组件。该项目的核心组件被称为“LLVM”,它包含处理中间表示及将其转换为目标文件所需的所有工具(包括汇编器、反汇编器、位码分析器和为位码优化器)、库和头文件
88

9-
C语言风格的语言使用Clang前端。该组件将C、C++、Objective-C和Objective-C++代码编译成LLVM比特码,并使用LLVM将其转换为对象文件
9+
类C语言使用Clang前端,该组件将C、C++、Objective-C和Objective-C++代码编译成LLVM位码,并使用LLVM将其转换为二进制目标文件
1010

11-
其他组件包括:libc++ C++ 标准库、LLD 链接器等
11+
其他组件包括:C++标准库(libc++)、LLD链接器等。
1212

13-
## 获取源代码并构建 LLVM
13+
## 2、构建指导
1414

15-
有关构建和运行 LLVM 的信息,请参阅 LLVM 入门页面
15+
首先通过git下载源码,然后通过build.sh脚本一键式构建LLVM。构建方式有`直接命令行构建``容器化构建`两种
1616

17-
有关如何为 LLVM 项目做贡献的信息,请查看 为 LLVM 做贡献指南。
17+
### 2.1、直接命令行构建
1818

19-
## 取得联系
19+
推荐使用openEuler操作系统进行构建,如果您使用其他操作系统,建议使用容器化构建方式。
2020

21-
加入 LLVM Discourse 论坛、Discord 聊天或 OFTC 上的 #llvm IRC 频道。
21+
首先确保系统安装了依赖软件包,可以用如下命令安装。
22+
```
23+
yum install -y gcc g++ make cmake openssl-devel python3 \
24+
python3-setuptools python-wheel texinfo binutils-devel libatomic
25+
```
26+
然后可以通过 `./build.sh -h`查看当前工程支持的构建选项。通过命令行执行一键式构建,例如:
27+
```
28+
./build.sh -r -b release -X X86 -j 8
29+
```
2230

23-
LLVM项目为参与者在项目内的所有沟通方式采用了行为准则。
31+
### 2.2、容器化构建
32+
33+
为了解决由于开发环境差异导致的构建失败和构建产物二进制差异问题,openEuler LLVM项目提供了容器化构建方法。得益于[openEuler容器镜像项目](https://gitee.com/openeuler/openeuler-docker-images),提前制作了[llvm-build-deps容器镜像](https://gitee.com/openeuler/openeuler-docker-images/tree/master/llvm-build-deps)。开发者可以通过`build.sh`脚本的`-C`选项启用容器化构建,例如:
34+
```
35+
./build.sh -C -r -b release -X X86 -j 8 // 添加了-C选项
36+
```
37+
38+
相关依赖:
39+
* 开发环境需要正确安装了docker应用。
40+
* 用户加入了docker用户组,使得`build.sh`脚本执行docker命令时不需要再加`sudo`命令。可以通过如下命令将当前用户加入docker用户组。
41+
```
42+
sudo usermod -aG docker ${USER}
43+
```
44+
注意:第一次执行容器化构建时,脚本会自动从镜像仓库拉取llvm-build-deps容器镜像。
45+
46+
## 3、贡献指导
47+
48+
1、Fork 本仓库
49+
2、新建 Feat_xxx 分支
50+
3、提交代码
51+
4、新建 Pull Request
52+
53+
## 4、讨论与求助
54+
55+
### 4.1、上游社区
56+
* 加入[discourse论坛](https://discourse.llvm.org/),提出或参与问题、RFC等交流。
57+
58+
* 社区参与者的[行为规范](https://llvm.org/docs/CodeOfConduct.html).
59+
60+
### 4.1、openEuler社区Compiler SIG
61+
几种方式:
62+
* 订阅[Compiler SIG邮件列表](https://mailweb.openeuler.org/postorius/lists/[email protected]/)
63+
*[openEuler论坛](https://forum.openeuler.org/?locale=zh_CN)发帖讨论。
64+
* 微信交流群:请先添加微信名`Compiler_Assistant`

README.md

Lines changed: 59 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,70 @@
1-
# The LLVM Compiler Infrastructure
1+
## 1. Project Introduction
22

3-
Welcome to the LLVM project!
3+
Welcome to the LLVM project in the openEuler community! This warehouse is the downstream warehouse of [llvm-project](https://github.com/llvm/llvm-project).
44

5-
This repository contains the source code for LLVM, a toolkit for the
6-
construction of highly optimized compilers, optimizers, and run-time
7-
environments.
5+
This repository contains the source code for LLVM, a toolkit for the construction of highly optimized compilers, optimizers, and run-time environments.
86

9-
The LLVM project has multiple components. The core of the project is
10-
itself called "LLVM". This contains all of the tools, libraries, and header
11-
files needed to process intermediate representations and convert them into
12-
object files. Tools include an assembler, disassembler, bitcode analyzer, and
13-
bitcode optimizer.
7+
The LLVM project has multiple components. The core of the project is itself called "LLVM". This contains all of the tools, libraries, and header files needed to process intermediate representations and convert them into object files. Tools include an assembler, disassembler, bitcode analyzer, and bitcode optimizer.
148

15-
C-like languages use the [Clang](http://clang.llvm.org/) frontend. This
16-
component compiles C, C++, Objective-C, and Objective-C++ code into LLVM bitcode
17-
-- and from there into object files, using LLVM.
9+
C-like languages use the [Clang](https://clang.llvm.org/) frontend. This component compiles C, C++, Objective-C, and Objective-C++ code into LLVM bitcode -- and from there into object files, using LLVM.
1810

19-
Other components include:
20-
the [libc++ C++ standard library](https://libcxx.llvm.org),
21-
the [LLD linker](https://lld.llvm.org), and more.
11+
Other components include: the [libc++ C++ standard library](https://libcxx.llvm.org/), the [LLD linker](https://lld.llvm.org/), and more.
2212

23-
## Getting the Source Code and Building LLVM
13+
## 2. Construction Guide
2414

25-
Consult the
26-
[Getting Started with LLVM](https://llvm.org/docs/GettingStarted.html#getting-the-source-code-and-building-llvm)
27-
page for information on building and running LLVM.
15+
You can use `git` to download the source code, and then use the `build.sh` script to build the LLVM in one-click mode. There are two build modes: `build with command line` and `build with container`.
2816

29-
For information on how to contribute to the LLVM project, please take a look at
30-
the [Contributing to LLVM](https://llvm.org/docs/Contributing.html) guide.
17+
### 2.1. Build with command line directly
3118

32-
## Getting in touch
19+
You are advised to use the `openEuler` for building. If you use other operating systems, you are advised to use the containerized building mode.
3320

34-
Join the [LLVM Discourse forums](https://discourse.llvm.org/), [Discord
35-
chat](https://discord.gg/xS7Z362), or #llvm IRC channel on
36-
[OFTC](https://oftc.net/).
21+
Ensure that the dependency software packages are installed. You can run the following command to install the software packages:
3722

38-
The LLVM project has adopted a [code of conduct](https://llvm.org/docs/CodeOfConduct.html) for
39-
participants to all modes of communication within the project.
23+
` ` `
24+
yum install -y gcc g++ make cmake openssl-devel python3 \
25+
python3-setuptools python-wheel texinfo binutils-devel libatomic
26+
` ` `
27+
28+
You can run the `./build.sh -h` command to view the build options supported by the current project. Run the following command to perform a one-click build:
29+
30+
` ` `
31+
./build.sh -r -b release -X X86 -j 8
32+
` ` `
33+
34+
### 2.2 Build with container
35+
36+
The openEuler LLVM project provides a containerized building mode to solve the problems of build failures and binary differences of build products caused by development environment differences. Thanks to the [openEuler container image project](https://gitee.com/openeuler/openeuler-docker-images), the [llvm-build-deps container image](https://gitee.com/openeuler/openeuler-docker-images/tree/master/llvm-build-deps) is created in advance. Developers can enable containerized builds using the `-C` option of the `build.sh` script. For example:
37+
38+
` ` `
39+
./build.sh -C -r -b release -X X86 -j 8 // added -C option
40+
` ` `
41+
42+
Dependency:
43+
* The Docker application must be correctly installed in the development environment.
44+
* The user is added to the docker user group so that the `sudo` command is not required when the `build.sh` script executes the docker command. You can run the following command to add the current user to the docker user group:
45+
46+
` ` `
47+
sudo usermod -aG docker ${USER}
48+
` ` `
49+
50+
Note: When you perform a containerized build for the first time, the script automatically pulls the `llvm-build-deps container image` from the image repository.
51+
52+
## 3. Contribution guidance
53+
54+
1. Fork This Warehouse
55+
2. Create the Feat_xxx branch.
56+
3. Submit the code.
57+
4. Create a Pull Request.
58+
59+
## 4. Discussion and help-seeking
60+
61+
### 4.1 Upstream Community
62+
* Join the [Discourse Forum](https://discourse.llvm.org/) .
63+
64+
* [Code of Conduct](https://llvm.org/docs/CodeOfConduct.html) for Community Participants.
65+
66+
### 4.1. Compiler SIG of the openEuler community
67+
There are several ways:
68+
* Subscribe to the [Compiler SIG mailing list](https://mailweb.openeuler.org/postorius/lists/[email protected]/)
69+
* Post a discussion at the [openEuler forum](https://forum.openeuler.org/?locale=zh_CN).
70+
* WeChat communication group: Please add the WeChat name `Compiler_Assistant` first.

build.sh

Lines changed: 129 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ use_ccache="0"
1717
enable_classic_flang="0"
1818
do_install="0"
1919
clean=0
20+
containerize=0
21+
docker=$(type -p docker)
22+
host_arch="$(uname -m)"
2023
unit_test=""
2124
install="install"
2225
install_toolchain_only="0"
@@ -53,6 +56,7 @@ Options:
5356
-A Disable ACPO.
5457
-b type Specify CMake build type (default: $buildtype).
5558
-c Use ccache (default: $use_ccache).
59+
-C Containerize the build for openEuler compatibility.
5660
-e Build for embedded cross tool chain.
5761
-E Build for openEuler.
5862
-h Display this help message.
@@ -72,13 +76,16 @@ EOF
7276

7377
# Process command-line options. Remember the options for passing to the
7478
# containerized build script.
75-
while getopts :aAb:d:ceEhiI:j:orstvfX: optchr; do
79+
containerized_opts=()
80+
while getopts :aAb:d:cCeEhiI:j:orstvfX: optchr; do
7681
case "$optchr" in
7782
a)
7883
enable_autotuner="0"
84+
containerized_opts+=(-$optchr)
7985
;;
8086
A)
8187
enable_acpo="0"
88+
containerized_opts+=(-$optchr)
8289
;;
8390
b)
8491
buildtype="$OPTARG"
@@ -93,53 +100,75 @@ while getopts :aAb:d:ceEhiI:j:orstvfX: optchr; do
93100
exit 1
94101
;;
95102
esac
103+
containerized_opts+=(-$optchr "$OPTARG")
96104
;;
97105
c)
98106
use_ccache="1"
107+
containerized_opts+=(-$optchr)
108+
;;
109+
C)
110+
if [ -z "$docker" -o ! -x "$docker" ]; then
111+
echo "$0: no usable Docker"
112+
exit 1
113+
fi
114+
containerize=1
99115
;;
100116
d)
101117
build_prefix="$OPTARG"
118+
containerized_opts+=(-$optchr "$OPTARG")
102119
;;
103120
f)
104121
enable_classic_flang="1"
122+
containerized_opts+=(-$optchr)
105123
;;
106124
e)
107125
embedded_toolchain="1"
126+
containerized_opts+=(-$optchr)
108127
;;
109128
E)
110129
build_for_openeuler="1"
130+
containerized_opts+=(-$optchr)
111131
;;
112132
h)
113133
usage
114134
exit
115135
;;
116136
i)
117137
do_install="1"
138+
containerized_opts+=(-$optchr)
118139
;;
119140
I)
120141
install_dir_name="$OPTARG"
121142
install_prefix="$dir/$install_dir_name"
143+
containerized_opts+=(-$optchr "$OPTARG")
122144
;;
123145
j)
124146
threads="$OPTARG"
147+
containerized_opts+=(-$optchr "$OPTARG")
125148
;;
126149
o)
127150
install_toolchain_only=1
151+
containerized_opts+=(-$optchr)
128152
;;
129153
r)
130154
clean=1
155+
containerized_opts+=(-$optchr)
131156
;;
132157
s)
133158
install="install/strip"
159+
containerized_opts+=(-$optchr)
134160
;;
135161
t)
136162
unit_test=check-all
163+
containerized_opts+=(-$optchr)
137164
;;
138165
v)
139166
verbose="VERBOSE=1"
167+
containerized_opts+=(-$optchr)
140168
;;
141169
X)
142170
backends="$OPTARG"
171+
containerized_opts+=(-$optchr "$OPTARG")
143172
;;
144173
:)
145174
echo "$0: missing argument for option '-$OPTARG'"
@@ -152,6 +181,105 @@ while getopts :aAb:d:ceEhiI:j:orstvfX: optchr; do
152181
esac
153182
done
154183

184+
# Make sure that all files under the build directory can be deleted; when some
185+
# LLVM tests are interrupted, they can leave behind inaccessible directories.
186+
build_cleanup() {
187+
chmod -R u+rwX,go+rX $build_prefix > /dev/null 2>&1
188+
}
189+
190+
# Handle interrupts. When not running a containerized build, we have to enable
191+
# job control (-m), and make sure to delete our own long-running child
192+
# processes. In particular, ninja and python (llvm-lit) refuse to terminate
193+
# when Jenkins aborts the parent process and disconnects.
194+
set -m
195+
handle_abort() {
196+
local rc=$1 sig=$2
197+
build_cleanup
198+
trap - EXIT SIGHUP SIGINT SIGTERM
199+
local children="$(jobs -p)"
200+
for cgrp in $children ; do
201+
kill -$sig -${cgrp} > /dev/null 2>&1
202+
done
203+
if [ -n "$sig" ]; then
204+
kill -$sig 0
205+
else
206+
exit $rc
207+
fi
208+
}
209+
210+
if [ $containerize -eq 0 ]; then
211+
trap - SIGCHLD
212+
trap 'handle_abort $?' EXIT
213+
trap 'handle_abort 129 HUP' SIGHUP
214+
trap 'handle_abort 130 INT' SIGINT
215+
trap 'handle_abort 143 TERM' SIGTERM
216+
else
217+
cmd=$(readlink --canonicalize-existing $0)
218+
219+
# Generate passwd/group files for the container.
220+
# lit.py depends on correct results from getpwent.
221+
homedir=$(realpath $HOME)
222+
passwd=$(mktemp /tmp/passwd.XXXXXX)
223+
echo "root:x:0:0::/root:/bin/bash" > $passwd
224+
echo "user:x:$(id -u):$(id -g)::$homedir:/bin/bash" >> $passwd
225+
226+
group=$(mktemp /tmp/group.XXXXXX)
227+
echo "root:x:0:" > $group
228+
echo "users:x:$(id -g):" >> $group
229+
230+
# Re-run myself in a openEuler container. The --cap-add option is needed
231+
# to pacify llvm-exegesis unit tests. Make sure that the container is
232+
# stopped if the child process is interrupted (e.g. by Jenkins).
233+
# Note that the container ID file is created with `mktemp -u` because
234+
# `docker run` refuses to overwrite an existing ID file.
235+
echo "Re-launching in container."
236+
containerid=$(mktemp -u /tmp/docker-cid.$$.XXXXXX)
237+
docker_cleanup() {
238+
local rc=$1 sig=$2
239+
docker container stop $(cat $containerid) > /dev/null 2>&1
240+
rm $passwd $group $containerid
241+
build_cleanup
242+
trap - EXIT SIGHUP SIGINT SIGTERM # avoid infinite recursion
243+
if [ -n "$sig" ]; then
244+
kill -$sig 0
245+
else
246+
exit $rc
247+
fi
248+
}
249+
trap 'docker_cleanup $?' EXIT
250+
trap 'docker_cleanup 129 HUP' SIGHUP
251+
trap 'docker_cleanup 130 INT' SIGINT
252+
trap 'docker_cleanup 143 TERM' SIGTERM
253+
254+
DOCKER_IMAGE="llvm-build-deps:latest"
255+
docker_opts="--rm
256+
--cap-add=SYS_ADMIN
257+
--cap-add=SYS_PTRACE
258+
--security-opt seccomp=unconfined
259+
--user $(id -u):$(id -g)
260+
--workdir=$(realpath $PWD)
261+
--ulimit core=0
262+
--ulimit stack=-1
263+
--cidfile $containerid
264+
-v $homedir:$homedir
265+
-v $passwd:/etc/passwd
266+
-v $group:/etc/group
267+
-e BINUTILS_INCDIR=/usr/local/include
268+
hub.oepkgs.net/openeuler/${DOCKER_IMAGE}"
269+
270+
if [ -t 1 ]; then
271+
$docker run -it $docker_opts ${cmd} ${containerized_opts[@]}
272+
exit $?
273+
else
274+
set -x
275+
$docker run $docker_opts ${cmd} ${containerized_opts[@]} &
276+
wait $! || exit $?
277+
exit 0
278+
fi
279+
fi
280+
281+
echo "Using $threads threads."
282+
155283
CMAKE_OPTIONS="-DCMAKE_INSTALL_PREFIX=$install_prefix \
156284
-DCMAKE_BUILD_TYPE=$buildtype \
157285
-DCMAKE_C_COMPILER=$C_COMPILER_PATH \

0 commit comments

Comments
 (0)