-
Notifications
You must be signed in to change notification settings - Fork 46
/
index-llvm.sh
executable file
·216 lines (188 loc) · 4.87 KB
/
index-llvm.sh
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/env bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
set -e
shopt -s extglob
shopt -s inherit_errexit
fatal() {
echo "$@" 1>&2
exit 1
}
usage() {
cat << EOF
Usage: $(basename "$0") FLAGS SOURCE_DIR TARGET_DIR
Index LLVM source distribution located in SOURCE_DIR into TARGET_DIR
Example:
mkdir -p /tmp/llvm \\
&& ( wget -O - https://github.com/llvm/llvm-project/releases/download/llvmorg-14.0.6/llvm-14.0.6.src.tar.xz \\
| tar -C /tmp/llvm -xJ ) \\
&& $0 -j$(grep -c '^processor' /proc/cpuinfo) /tmp/llvm/llvm-14.0.6.src /tmp/llvm
Available options:
--glean DIR Specify Glean source directory
-jN | --jobs N Use N concurrent indeces
--db-root DIR Store the generated database in DIR
(default TARGET_DIR/db)
--db DB Use provided name for the database
(default llvm/VERSION)
--overwrite Overwrite the database if it already exists
--schema DIR Use the schema in DIR (default is the schema in
Glean's source tree)
EOF
}
argerror() {
echo "$@" >&2
usage >&2
exit 1
}
if [[ "$1" == "--help" ]]
then
usage
exit 0
fi
MAKE_ARGS=()
EXTRA_GLEAN_ARGS=()
EXTRA_GLEAN_INDEX_ARGS=()
VERBOSITY=0
GLEAN_DB_ROOT=
GLEAN_SCHEMA=
GLEAN_DIR=
while true
do
case "$1" in
(-j+([0-9]))
MAKE_ARGS+=("$1")
EXTRA_GLEAN_INDEX_ARGS+=("$1")
shift
;;
-j|--jobs)
MAKE_ARGS+=("$1")
EXTRA_GLEAN_INDEX_ARGS+=("$1")
case "$2" in
(+([0-9]))
MAKE_ARGS+=("$2")
EXTRA_GLEAN_INDEX_ARGS+=("$2")
shift 2
;;
*)
argerror "Invalid number of jobs"
;;
esac
;;
-v|--verbose)
VERBOSITY=1
EXTRA_GLEAN_INDEX_ARGS+=("-v")
shift
;;
--db-root)
GLEAN_DB_ROOT="$2"
shift 2
;;
--schema)
GLEAN_SCHEMA="$2"
shift 2
;;
--db)
GLEAN_DB="$2"
shift 2
;;
--overwrite)
OVERWRITE=yes
shift
;;
--glean)
GLEAN_DIR="$2"
shift 2
;;
-*)
argerror "Unsupported option $1"
;;
*)
break
;;
esac
done
if [[ -z "$1" ]]
then
argerror "No source directory specified"
fi
SOURCE_DIR="$1"
shift
if [[ -z "$1" ]]
then
argerror "No destination directory specified"
fi
OUTPUT_DIR="$1"
shift
if [[ $# -ne 0 ]]
then
argerror "Extra arguments"
fi
if [[ -z "${GLEAN_DIR}" ]]
then
# Look for any ancestor directory with glean.cabal.in - this allows us to
# move the script within the source tree
dir=$(dirname "$0")
while [[ "${dir}" != "/" ]] ; do
if [[ -f "${dir}"/glean.cabal.in ]]
then
GLEAN_DIR="${dir}"
break
fi
dir=$(dirname "${dir}")
done
if [[ -z "${GLEAN_DIR}" ]] ; then
fatal "Couldn't locate Glean source directory, please specify --glean"
fi
fi
BUILD_DIR="${OUTPUT_DIR}"/build
mkdir -p "${BUILD_DIR}"
GLEAN_DB_ROOT="${GLEAN_DB_ROOT:-${OUTPUT_DIR}/db}"
mkdir -p "${GLEAN_DB_ROOT}"
GLEAN_SCHEMA="${GLEAN_SCHEMA:-${GLEAN_DIR}/glean/schema/source}"
# FD 5 is where we redirect all output
if [[ "${VERBOSITY}" == "1" ]] ; then
exec 5>&0
else
exec 5> "${OUTPUT_DIR}/index-llvm.log"
fi
echo "Building glean-clang"
make MODE=opt glean-clang >&5
requirebin() {
tmp="$("${GLEAN_DIR}/quick.sh" MODE=opt list-bin "$1")"
if ! [[ -x "${tmp}" ]] ; then
fatal "$1 doesn't exist at ${tmp}"
fi
echo "${tmp}"
}
CLANG_INDEX=$(requirebin glean-clang:clang-index)
CLANG_DERIVE=$(requirebin glean-clang:clang-derive)
GLEAN=$(requirebin glean:glean)
echo "Setting up ${BUILD_DIR}"
mkdir -p "${BUILD_DIR}" >&5
"${CMAKE:-cmake}" \
-DCMAKE_EXPORT_COMPILE_COMMANDS=1 \
-DLLVM_INCLUDE_BENCHMARKS=OFF -DLLVM_TARGETS_TO_BUILD=X86 \
-S "${SOURCE_DIR}" -B "${BUILD_DIR}" >&5
echo "Generating LLVM code"
# We get all the *TableGen targets from make help which thankfully lists all
# available targets. We do want word splitting here so disable the corresponding
# check.
# shellcheck disable=SC2046
(cd "${BUILD_DIR}" && \
make "${MAKE_ARGS[@]}" intrinsics_gen acc_gen omp_gen llvm_vcsrevision_h \
$(make help | sed -n '/^[.][.][.] [A-Za-z0-9]*TableGen$/ s/^....//p')) >&5
if [[ -z "${GLEAN_DB}" ]] ; then
GLEAN_DB=llvm/$(sed -n '/^Version/ {s/^Version: //p;q}' "${BUILD_DIR}/llvm.spec")
fi
if [[ -n "${OVERWRITE}" ]] ; then
rm -rf "${GLEAN_DB_ROOT:?}/${GLEAN_DB}"
fi
echo "Indexing ${GLEAN_DB} in ${GLEAN_DB_ROOT}"
"${GLEAN}" "${EXTRA_GLEAN_ARGS[@]}" \
--schema "${GLEAN_SCHEMA}" --db-root "${GLEAN_DB_ROOT}" \
index --db "${GLEAN_DB}" cpp-cmake \
--indexer "${CLANG_INDEX}" --deriver "${CLANG_DERIVE}" --cdb "${BUILD_DIR}" \
--verbose --progress "${EXTRA_GLEAN_INDEX_ARGS[@]}" "${SOURCE_DIR}" 2>&5