-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild_llvm.bash
executable file
·131 lines (115 loc) · 4.01 KB
/
build_llvm.bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/bin/bash
set -e -x
# 0. Check
CURRENT_DIR="$(pwd)"
SOURCE_DIR="$CURRENT_DIR"
if [ ! -f "$SOURCE_DIR/llvm-project/llvm/CMakeLists.txt" ]; then
echo "Error: $SOURCE_DIR/llvm-project/llvm/CMakeLists.txt is not found."
echo " Did you run git submodule update --init --recursive?"
exit 1
fi
# Parse arguments
install_prefix=""
platform=""
build_config=""
arch="x86"
num_jobs=8
usage() {
echo "Usage: bash build_llvm.sh -o INSTALL_PREFIX -p PLATFORM -c CONFIG [-a ARCH] [-j NUM_JOBS]"
echo "Ex: bash build_llvm.sh -o llvm-16.0.0-x86_64-linux-gnu-ubuntu-18.04 -p docker_ubuntu_18.04 -c assert -j 16"
echo "INSTALL_PREFIX = <string> # \${INSTALL_PREFIX}.tar.xz is created"
echo "PLATFORM = {local|docker_ubuntu_18.04|docker_centos7}"
echo "CONFIG = {release|assert|debug|assert_shared}"
echo "ARCH = {x86|arm64}"
echo "NUM_JOBS = {1|2|3|...}"
exit 1;
}
while getopts "o:p:c:a:j:s:" arg; do
case "$arg" in
o)
install_prefix="$OPTARG"
;;
p)
platform="$OPTARG"
;;
c)
build_config="$OPTARG"
;;
a)
arch="$OPTARG"
;;
j)
num_jobs="$OPTARG"
;;
*)
usage
;;
esac
done
if [ x"$install_prefix" == x ] || [ x"$platform" == x ] || [ x"$build_config" == x ]; then
usage
fi
# Set up CMake configurations
CMAKE_CONFIGS="-DLLVM_ENABLE_PROJECTS=mlir -DLLVM_INSTALL_UTILS=ON"
if [ x"$arch" == x"arm64" ]; then
CMAKE_CONFIGS="${CMAKE_CONFIGS}"
else
CMAKE_CONFIGS="${CMAKE_CONFIGS} -DLLVM_TARGETS_TO_BUILD=X86;NVPTX;AMDGPU"
fi
if [ x"$build_config" == x"release" ]; then
CMAKE_CONFIGS="${CMAKE_CONFIGS} -DCMAKE_BUILD_TYPE=Release"
elif [ x"$build_config" == x"assert" ]; then
CMAKE_CONFIGS="${CMAKE_CONFIGS} -DCMAKE_BUILD_TYPE=MinSizeRel -DLLVM_ENABLE_ASSERTIONS=True"
elif [ x"$build_config" == x"assert_shared" ]; then
CMAKE_CONFIGS="${CMAKE_CONFIGS} -DCMAKE_BUILD_TYPE=MinSizeRel -DLLVM_ENABLE_ASSERTIONS=True -DLLVM_BUILD_LLVM_DYLIB=1"
elif [ x"$build_config" == x"debug" ]; then
CMAKE_CONFIGS="${CMAKE_CONFIGS} -DCMAKE_BUILD_TYPE=Debug"
else
usage
fi
# Create a temporary build directory
BUILD_DIR="$(mktemp -d)"
echo "Using a temporary directory for the build: $BUILD_DIR"
rm -rf "$BUILD_DIR"
mkdir -p "$BUILD_DIR"
if [ x"$platform" == x"local" ]; then
# Build LLVM locally
pushd "$BUILD_DIR"
cmake "$SOURCE_DIR/llvm-project/llvm" -DCMAKE_INSTALL_PREFIX="$BUILD_DIR/$install_prefix" $CMAKE_CONFIGS
make -j${num_jobs} install
tar -cJf "${CURRENT_DIR}/${install_prefix}.tar.xz" "$install_prefix"
popd
elif [ x"$platform" == x"docker_ubuntu-18.04" ] ||
[ x"$platform" == x"docker_centos-7" ]; then
# Prepare build directories
cp -r "$SOURCE_DIR/scripts" "$BUILD_DIR/scripts"
# Create a tarball of llvm-project
echo "Creating llvm-project.tar.gz"
pushd "$SOURCE_DIR"
tar -czf "$BUILD_DIR/llvm-project.tar.gz" llvm-project
popd
# Run a docker
DOCKER_TAG="build"
DOCKER_REPOSITORY="clang-docker"
DOCKER_FILE_PATH="scripts/$platform/Dockerfile"
echo "Building $DOCKER_REPOSITORY:$DOCKER_TAG using $DOCKER_FILE_PATH"
docker build -t $DOCKER_REPOSITORY:$DOCKER_TAG --build-arg cmake_configs="${CMAKE_CONFIGS}" --build-arg num_jobs="${num_jobs}" --build-arg install_dir_name="${install_prefix}" -f "$BUILD_DIR/$DOCKER_FILE_PATH" "$BUILD_DIR"
# Copy a created tarball from a Docker container.
# We cannot directly copy a file from a Docker image, so first
# create a Docker container, copy the tarball, and remove the container.
DOCKER_ID="$(docker create $DOCKER_REPOSITORY:$DOCKER_TAG)"
docker cp "$DOCKER_ID:/tmp/${install_prefix}.tar.xz" "${CURRENT_DIR}/"
docker rm "$DOCKER_ID"
elif [ x"$arch" == x"arm64" ]; then
pushd "$BUILD_DIR"
cmake -G Ninja -S "$SOURCE_DIR/llvm-project/llvm" -DCMAKE_INSTALL_PREFIX="$BUILD_DIR/$install_prefix" $CMAKE_CONFIGS
ninja -C "$BUILD_DIR/$install_prefix" install
tar -cJf "${CURRENT_DIR}/${install_prefix}.tar.xz" "$install_prefix"
popd
else
rm -rf "$BUILD_DIR"
usage
fi
# Remove the temporary directory
rm -rf "$BUILD_DIR"
echo "Completed!"