Skip to content

Commit a971bff

Browse files
authored
[Build script] Add compilation script (#83)
In some cases, when using different versions of the compiler/libc to build the llvm-project and other parts of the project, linking errors occur. To avoid this, compilation script has been modified to build the llvm project in the specified environment. Also external projects will be saved in the externals folder so that the user can delete the build directory separately from the external projects. Signed-off-by: Dmitrii Makarenko <[email protected]>
1 parent 6437c35 commit a971bff

File tree

2 files changed

+125
-9
lines changed

2 files changed

+125
-9
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.*/
22
!.github/
33
build/
4+
externals/
45
compile_commands.json

scripts/compile.sh

+124-9
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,142 @@
22

33
repo=intel/graph-compiler
44

5+
# Uncomment for script debug
6+
# set -ex
7+
8+
function print_usage() {
9+
echo "Usage:"
10+
echo "$0 "
11+
echo " [ -d | --dev ] Dev build, build LLVM in current env and place all to 'external' dir"
12+
echo " [ -l | --dyn ] Dynamical linking, requires rebuild of LLVM, activates 'dev' option"
13+
echo " [ -h | --help ] Print this message"
14+
}
15+
16+
args=$(getopt -a -o dlh --long dev,dyn,help -- "$@")
17+
if [[ $? -gt 0 ]]; then
18+
echo "first"
19+
print_usage
20+
fi
21+
22+
DEV_BUILD=false
23+
DYN_LINK=false
24+
eval set -- ${args}
25+
while :
26+
do
27+
case $1 in
28+
-d | --dev)
29+
DEV_BUILD=true
30+
shift ;;
31+
-l | --dyn)
32+
DEV_BUILD=true
33+
DYN_LINK=true
34+
shift ;;
35+
-h | --help)
36+
echo "in help"
37+
print_usage
38+
shift ;;
39+
# -- means the end of the arguments; drop this, and break out of the while loop
40+
--) shift; break ;;
41+
*) >&2 echo Unsupported option: $1
42+
echo "in unsup"
43+
print_usage ;;
44+
esac
45+
done
46+
547
cd $(dirname "$0")/..
6-
llvm_dir=$(cd ..; pwd -P)/install/llvm
48+
project_dir=$PWD
749
llvm_hash=$(cat cmake/llvm-version.txt)
850

9-
get_llvm() (
51+
function load_llvm() {
52+
local mlir_dir=$1
1053
local run_id
1154

1255
run_id=$(gh run list -w "LLVM Build" --repo $repo --json databaseId --jq '.[0].databaseId')
1356

1457
gh run download "$run_id" \
15-
--repo "$repo" \
16-
--pattern "llvm-$llvm_hash" \
17-
--dir "$llvm_dir"
58+
--repo "$repo" \
59+
--pattern "llvm-$llvm_hash" \
60+
--dir "$llvm_dir"
1861
cd "$llvm_dir"
1962
tar -zxf "llvm-$llvm_hash"/llvm.tgz
20-
)
2163

22-
test -f "$llvm_dir/llvm-$llvm_hash"/llvm.tgz || get_llvm
64+
eval $mlir_dir="$PWD/lib/cmake/mlir"
65+
cd "$project_dir"
66+
return 0
67+
}
68+
69+
function build_llvm() {
70+
local mlir_dir=$1
71+
72+
if ! [ -d "llvm-project" ]; then
73+
git clone https://github.com/llvm/llvm-project.git
74+
fi
75+
76+
cd llvm-project
77+
git checkout ${llvm_hash}
78+
79+
dylib=OFF
80+
if [[ "$DYN_LINK" == 'true' ]]; then
81+
dylib=ON
82+
fi
83+
84+
cmake -G Ninja llvm -B build \
85+
-DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_ASSERTIONS=true \
86+
-DLLVM_ENABLE_PROJECTS="mlir" -DLLVM_TARGETS_TO_BUILD="X86" \
87+
-DLLVM_INSTALL_UTILS=true -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
88+
-DLLVM_INSTALL_GTEST=ON -DLLVM_BUILD_LLVM_DYLIB=$dylib -DLLVM_LINK_LLVM_DYLIB=$dylib
89+
cmake --build build
90+
91+
eval $mlir_dir="$PWD/build/lib/cmake/mlir"
92+
cd ..
93+
return 0
94+
}
95+
96+
function get_llvm() {
97+
local ret_val=$1
98+
99+
if [[ "$DEV_BUILD" == 'true' ]]; then
100+
mkdir -p externals
101+
cd externals
102+
build_llvm val
103+
eval $ret_val=\$val
104+
cd ..
105+
return 0
106+
fi
107+
108+
llvm_dir=$project_dir/../install/llvm
109+
if ! [ -f "$llvm_dir/llvm-$llvm_hash"/llvm.tgz ]; then
110+
load_llvm val
111+
eval $ret_val=\$val
112+
fi
113+
eval $ret_val="$llvm_dir/lib/cmake/mlir"
114+
return 0
115+
}
116+
117+
get_llvm mlir_dir
118+
119+
fetch_dir=$project_dir/build/_deps
120+
dylib=OFF
121+
lit_path=""
122+
if [[ $(which lit) ]]; then
123+
lit_path=$(which lit)
124+
fi
125+
if [[ "$DEV_BUILD" == 'true' ]]; then
126+
fetch_dir=$project_dir/externals
127+
lit_path=$project_dir/externals/llvm-project/build/bin/llvm-lit
128+
fi
129+
if [[ "$DYN_LINK" == 'true' ]]; then
130+
dylib=ON
131+
fi
132+
if [ -z "$lit_path" ]; then
133+
echo "========Warning======="
134+
echo " Lit not found. "
135+
fi
23136

24137
cmake -S . -G Ninja -B build \
25138
-DCMAKE_BUILD_TYPE=RelWithDebInfo \
26-
-DMLIR_DIR=$llvm_dir/lib/cmake/mlir \
27-
-DLLVM_EXTERNAL_LIT=$(which lit)
139+
-DMLIR_DIR=$mlir_dir \
140+
-DLLVM_EXTERNAL_LIT=$lit_path \
141+
-DFETCHCONTENT_BASE_DIR=$fetch_dir \
142+
-DGC_DEV_LINK_LLVM_DYLIB=$dylib
28143
cmake --build build --parallel $(nproc)

0 commit comments

Comments
 (0)