forked from EPFL-LAP/dynamatic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
executable file
·372 lines (322 loc) · 11.4 KB
/
build.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#!/bin/bash
# Kill the whole script on Ctrl+C
trap "exit" INT
# Directory where the script is being ran from (must be directory where script
# is located!)
SCRIPT_CWD=$PWD
#### Helper functions ####
# Display list of possible options and exit
print_help_and_exit () {
echo -e \
"./build.sh [options]
List of options:
--release | -r : build in \"Release\" mode (default is \"Debug\")
--skip-polygeist <polygeist-path> : skip building POLYGEIST
--visual-dataflow | -v : build visual-dataflow's C++ library
--export-godot | -e <godot-path> : export the Godot project (requires engine)
--force | -f : force cmake reconfiguration in each (sub)project
--threads | -t <num-threads> : number of concurrent threads to build on (by
default, one thread per logical core on the host
machine)
--llvm-parallel-link-jobs <num-jobs> : maximum number of simultaneous link jobs when
building llvm (defaults to 2)
--disable-build-opt | -o : don't use clang/lld/ccache to speed up builds
--check | -c : run tests during build
--help | -h : display this help message
"
exit
}
# Helper function to print large section title text
echo_section() {
echo ""
echo "# ===----------------------------------------------------------------------=== #"
echo "# $1"
echo "# ===----------------------------------------------------------------------=== #"
echo ""
}
# Helper function to print subsection title text
echo_subsection() {
echo -e "\n# ===--- $1 ---==="
}
# Helper function to exit script on failed command
exit_on_fail() {
if [[ $? -ne 0 ]]; then
if [[ ! -z $1 ]]; then
echo -e "\n$1"
fi
echo_subsection "Build failed!"
exit 1
fi
}
# Prepares to build a particular part of the project by creating a "build"
# folder for it, cd-ing to it, and displaying a message to stdout.
# $1: Name of the project part
# $2: Path to build folder (relative to script's CWD)
prepare_to_build_project() {
local project_name=$1
local build_dir=$2
cd "$SCRIPT_CWD" && mkdir -p "$build_dir" && cd "$build_dir"
echo_section "Building $project_name ($build_dir)"
}
# Create symbolic link from the bin/ directory to an executable file built by
# the repository. The symbolic link's name is the same as the executable file.
# The path to the executable file must be passed as the first argument to this
# function and be relative to the repository's root. The function assumes that
# the bin/ directory exists and that the current working directory is the
# repository's root.
create_symlink() {
local src=$1
local dst="bin/$(basename $1)"
echo "$dst -> $src"
ln -f --symbolic $src $dst
}
# Same as create_symlink but creates the symbolic link inside the bin/generators
# subfolder.
create_generator_symlink() {
local src=$1
local dst="bin/generators/$(basename $1)"
echo "$dst -> $src"
ln -f --symbolic ../../$src $dst
}
# Determine whether cmake should be re-configured by looking for a
# CMakeCache.txt file in the current working directory.
should_run_cmake() {
if [[ -f "CMakeCache.txt" && $FORCE_CMAKE -eq 0 ]]; then
echo "CMake configuration found, will not re-configure cmake"
echo "Run script with -f or --force flag to re-configure cmake"
echo ""
return 1
fi
return 0
}
# Run ninja using the number of threads provided as argument, if any. Otherwise,
# let ninja pick the number of threads to use
run_ninja() {
if [[ $NUM_THREADS -eq 0 ]]; then
ninja
else
ninja -j "$NUM_THREADS"
fi
}
#### Parse arguments ####
CMAKE_COMPILERS="-DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++"
CMAKE_EXTRA_LLVM="-DLLVM_CCACHE_BUILD=ON -DLLVM_USE_LINKER=lld"
CMAKE_EXTRA_POLYGEIST="-DPOLYGEIST_USE_LINKER=lld"
ENABLE_TESTS=0
FORCE_CMAKE=0
NUM_THREADS=0
LLVM_PARALLEL_LINK_JOBS=2
BUILD_TYPE="Debug"
BUILD_VISUAL_DATAFLOW=0
GODOT_PATH=""
SKIP_POLYGEIST=0
POLYGEIST_DIR="$PWD/polygeist"
# Loop over command line arguments and update script variables
PARSE_ARG=""
for arg in "$@";
do
if [[ $PARSE_ARG == "num-threads" ]]; then
NUM_THREADS="$arg"
PARSE_ARG=""
elif [[ $PARSE_ARG == "godot-path" ]]; then
GODOT_PATH="$arg"
# If the path is relative, prepend .. to it since we will build the
# project from the visual-dataflow subfolder
if [[ $GODOT_PATH != /* ]]; then
GODOT_PATH="../$GODOT_PATH"
fi
PARSE_ARG=""
elif [[ $PARSE_ARG == "polygeist-path" ]]; then
POLYGEIST_DIR="$arg"
PARSE_ARG=""
elif [[ $PARSE_ARG == "llvm-parallel-link-jobs" ]]; then
LLVM_PARALLEL_LINK_JOBS="$arg"
PARSE_ARG=""
else
case "$arg" in
"--disable-build-opt" | "-o")
CMAKE_COMPILERS=""
CMAKE_EXTRA_LLVM=""
CMAKE_EXTRA_POLYGEIST=""
;;
"--force" | "-f")
FORCE_CMAKE=1
;;
"--release" | "-r")
BUILD_TYPE="Release"
;;
"--check" | "-c")
ENABLE_TESTS=1
;;
"--visual-dataflow" | "-v")
BUILD_VISUAL_DATAFLOW=1
;;
"--threads" | "-t")
PARSE_ARG="num-threads"
;;
"--llvm-parallel-link-jobs")
PARSE_ARG="llvm-parallel-link-jobs"
;;
"--export-godot" | "-e")
PARSE_ARG="godot-path"
;;
"--skip-polygeist")
SKIP_POLYGEIST=1
PARSE_ARG="polygeist-path"
;;
"--help" | "-h")
print_help_and_exit
;;
*)
echo "Unknown argument \"$arg\", printing help and aborting"
print_help_and_exit
;;
esac
fi
done
if [[ $PARSE_ARG != "" ]]; then
echo "Missing argument \"$PARSE_ARG\", printing help and aborting"
print_help_and_exit
fi
#### Build the project (submodules, superproject, and tools) ####
# Print header
echo "################################################################################"
echo "############# DYNAMATIC - DHLS COMPILER INFRASTRUCTURE - EPFL/LAP ##############"
echo "################################################################################"
if [[ $SKIP_POLYGEIST -eq 0 ]]; then
#### Polygeist ####
prepare_to_build_project "LLVM" "polygeist/llvm-project/build"
# CMake
if should_run_cmake ; then
cmake -G Ninja ../llvm \
-DLLVM_ENABLE_PROJECTS="mlir;clang" \
-DLLVM_TARGETS_TO_BUILD="host" \
-DCMAKE_BUILD_TYPE=$BUILD_TYPE \
-DLLVM_PARALLEL_LINK_JOBS=$LLVM_PARALLEL_LINK_JOBS \
$CMAKE_COMPILERS $CMAKE_EXTRA_LLVM
exit_on_fail "Failed to cmake polygeist/llvm-project"
fi
# Build
run_ninja
exit_on_fail "Failed to build polygeist/llvm-project"
if [[ ENABLE_TESTS -eq 1 ]]; then
ninja check-mlir
exit_on_fail "Tests for polygeist/llvm-project failed"
fi
prepare_to_build_project "Polygeist" "polygeist/build"
# CMake
if should_run_cmake ; then
cmake -G Ninja .. \
-DMLIR_DIR=$PWD/../llvm-project/build/lib/cmake/mlir \
-DCLANG_DIR=$PWD/../llvm-project/build/lib/cmake/clang \
-DLLVM_TARGETS_TO_BUILD="host" \
-DCMAKE_BUILD_TYPE=$BUILD_TYPE \
$CMAKE_COMPILERS $CMAKE_EXTRA_POLYGEIST
exit_on_fail "Failed to cmake polygeist"
fi
# Build
run_ninja
exit_on_fail "Failed to build polygeist"
if [[ ENABLE_TESTS -eq 1 ]]; then
ninja check-polygeist-opt
exit_on_fail "Tests for polygeist failed"
ninja check-cgeist
exit_on_fail "Tests for polygeist failed"
fi
else
echo "Skipping POLYGEIST/LLVM build. IMPORTANT: Verify that the path of polygeist in the script tools/dynamatic/scripts/compile.sh is the same"
if [[ ! -d $POLYGEIST_DIR ]]; then
echo "POLYGEIST directory not found: $POLYGEIST_DIR"
exit 1
fi
fi
#### Dynamatic ####
prepare_to_build_project "Dynamatic" "build"
# CMake
if should_run_cmake ; then
cmake -G Ninja .. \
-DMLIR_DIR="$POLYGEIST_DIR"/llvm-project/build/lib/cmake/mlir \
-DLLVM_DIR="$POLYGEIST_DIR"/llvm-project/build/lib/cmake/llvm \
-DCLANG_DIR="$POLYGEIST_DIR"/llvm-project/build/lib/cmake/clang \
-DLLVM_TARGETS_TO_BUILD="host" \
-DCMAKE_BUILD_TYPE=$BUILD_TYPE \
-DCMAKE_EXPORT_COMPILE_COMMANDS="ON" \
$CMAKE_COMPILERS
exit_on_fail "Failed to cmake dynamatic"
fi
# Build
run_ninja
exit_on_fail "Failed to build dynamatic"
if [[ ENABLE_TESTS -eq 1 ]]; then
ninja check-dynamatic
exit_on_fail "Tests for dynamatic failed"
fi
# Build Chisel generators
echo_subsection "Building LSQ generator"
LSQ_GEN_PATH="tools/backend/lsq-generator-chisel"
LSQ_GEN_JAR="target/scala-2.13/lsq-generator.jar"
cd "$SCRIPT_CWD/$LSQ_GEN_PATH"
sbt assembly
exit_on_fail "Failed to build LSQ generator"
chmod +x $LSQ_GEN_JAR
#### visual-dataflow ####
if [[ BUILD_VISUAL_DATAFLOW -ne 0 ]]; then
prepare_to_build_project "visual-dataflow" "visual-dataflow/build"
# CMake
if should_run_cmake ; then
cmake -G Ninja .. \
-DMLIR_DIR="$POLYGEIST_DIR"/llvm-project/build/lib/cmake/mlir \
-DLLVM_DIR="$POLYGEIST_DIR"/llvm-project/build/lib/cmake/llvm \
-DLLVM_TARGETS_TO_BUILD="host" \
-DCMAKE_BUILD_TYPE=$BUILD_TYPE \
-DCMAKE_EXPORT_COMPILE_COMMANDS="ON" \
$CMAKE_COMPILERS
exit_on_fail "Failed to cmake visual-dataflow"
fi
# Build
run_ninja
exit_on_fail "Failed to build visual-dataflow"
fi
#### Godot ####
if [[ $GODOT_PATH != "" ]]; then
# Go to the visualizer's subfolder and build it using godot
cd "$SCRIPT_CWD/visual-dataflow"
"$GODOT_PATH" --headless --export-debug "Linux/X11"
exit_on_fail "Failed to build Godot project"
# Erase the useless shell script generated by Godot and cd back the
# Dynamatic's top-level folder
rm bin/visual-dataflow.sh
cd ..
fi
#### Symbolic links ####
echo_section "Creating symbolic links"
# Create bin/ directory at the project's root
cd "$SCRIPT_CWD" && mkdir -p bin/generators
# Create symbolic links to all binaries we use from subfolders
create_symlink "$POLYGEIST_DIR"/build/bin/cgeist
create_symlink "$POLYGEIST_DIR"/build/bin/polygeist-opt
create_symlink "$POLYGEIST_DIR"/llvm-project/build/bin/clang++
create_symlink ../build/bin/dynamatic
create_symlink ../build/bin/dynamatic-opt
create_symlink ../build/bin/export-dot
create_symlink ../build/bin/export-rtl
create_symlink ../build/bin/exp-frequency-profiler
create_symlink ../build/bin/handshake-simulator
create_symlink ../build/bin/hls-verifier
create_symlink ../build/bin/wlf2csv
create_generator_symlink build/bin/rtl-cmpf-generator
create_generator_symlink build/bin/rtl-cmpi-generator
create_generator_symlink build/bin/rtl-text-generator
create_generator_symlink build/bin/rtl-constant-generator-verilog
create_generator_symlink build/bin/exp-sharing-wrapper-generator
create_generator_symlink "$LSQ_GEN_PATH/$LSQ_GEN_JAR"
if [[ $GODOT_PATH != "" ]]; then
create_symlink ../visual-dataflow/bin/visual-dataflow
fi
# Make the scripts used by the frontend executable
chmod +x tools/dynamatic/scripts/compile.sh
chmod +x tools/dynamatic/scripts/write-hdl.sh
chmod +x tools/dynamatic/scripts/simulate.sh
chmod +x tools/dynamatic/scripts/synthesize.sh
chmod +x tools/dynamatic/scripts/visualize.sh
echo_subsection "Build successful!"