-
Notifications
You must be signed in to change notification settings - Fork 3
/
setup.sh
executable file
·151 lines (129 loc) · 5.97 KB
/
setup.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
#!/bin/sh
set -e
set -x
if test ! "$(dirname $0)" -ef '.'; then
echo "The script must be executed from its current directory."
exit 1
fi
if test "$#" -ne 1; then
echo "Usage: $0 <llvm-config>"
exit 1
fi
llvm_config=$1
default_mode=
support_static_mode=false
support_shared_mode=false
llvm_config() {
"$llvm_config" $@
}
llvm_version=$(llvm_config --version | cut -d. -f1)
expected_version=$(cat VERSION | cut -d. -f1)
if test "$llvm_version" != "$expected_version"; then
echo "LLVM version does not match. Expected '$expected_version' but got '$llvm_version'"
exit 1
fi
if llvm_config --link-static --libs; then
default_mode=static
support_static_mode=true
fi
if llvm_config --link-shared --libs; then
default_mode=shared
support_shared_mode=true
fi
if test -z "$default_mode"; then
echo "Something is wrong with the llvm-config command provided."
exit 1
fi
base_cflags=$(llvm_config --cflags)
ldflags="$(llvm_config --ldflags) -lstdc++"
llvm_targets=$(llvm_config --targets-built)
rm -rf src
cp -r llvm-project/llvm/bindings/ocaml src
create_dune_file() {
findlibname=$1
dirname=$2
modname=$3
cfile=$4
depends=$5
components=$6
if test "$dirname" = "backends"; then
basedir=src/$dirname/$components
test ! -d "$basedir" && mkdir "$basedir"
sed "s/@TARGET@/$components/g" "src/$dirname/llvm_backend.ml.in" > "$basedir/$modname.ml"
sed "s/@TARGET@/$components/g" "src/$dirname/llvm_backend.mli.in" > "$basedir/$modname.mli"
sed "s/@TARGET@/$components/g" "src/$dirname/backend_ocaml.c" > "$basedir/$cfile.c"
cflags="$base_cflags \"-DTARGET=$components\""
else
basedir=src/$dirname
cflags=$base_cflags
fi
test ! -d "$basedir/common" && mkdir "$basedir/common"
cp "$basedir/$modname.mli" "$basedir/common"
echo "
(library
(name $modname)
(public_name $findlibname)
(wrapped false)
(virtual_modules $modname)
(libraries $depends)
(default_implementation $findlibname.$default_mode))
" > "$basedir/common/dune"
if $support_shared_mode; then
test ! -d "$basedir/shared" && mkdir "$basedir/shared"
cp "$basedir/$modname.ml" "$basedir/shared"
cp "$basedir/$cfile.c" "$basedir/shared"
cp "src/llvm/llvm_ocaml.h" "$basedir/shared"
echo "
(library
(name ${modname}_shared)
(public_name $findlibname.shared)
(implements $findlibname)
(foreign_stubs
(language c)
(names ${cfile})
(extra_deps llvm_ocaml.h)
(flags ($cflags)))
(c_library_flags ($ldflags $(llvm_config --system-libs --link-shared --libs $components))))
" >> "$basedir/shared/dune"
fi
if $support_static_mode; then
test ! -d "$basedir/static" && mkdir "$basedir/static"
cp "$basedir/$modname.ml" "$basedir/static"
cp "$basedir/$cfile.c" "$basedir/static"
cp "src/llvm/llvm_ocaml.h" "$basedir/static"
echo "
(library
(name ${modname}_static)
(public_name $findlibname.static)
(implements $findlibname)
(foreign_stubs
(language c)
(names ${cfile})
(extra_deps llvm_ocaml.h)
(flags ($cflags)))
(c_library_flags ($ldflags $(llvm_config --system-libs --link-static --libs $components))))
" >> "$basedir/static/dune"
fi
rm "$basedir/$modname.ml"
rm "$basedir/$cfile.c"
}
# ------------------ public name -------- directory ---------------- module name -------- C file name --------- OCaml dependencies -------------- LLVM components (for the linker)
create_dune_file llvm llvm llvm llvm_ocaml "" "core support"
create_dune_file llvm.analysis analysis llvm_analysis analysis_ocaml "llvm" "analysis"
create_dune_file llvm.bitreader bitreader llvm_bitreader bitreader_ocaml "llvm" "bitreader"
create_dune_file llvm.bitwriter bitwriter llvm_bitwriter bitwriter_ocaml "llvm unix" "bitwriter"
create_dune_file llvm.executionengine executionengine llvm_executionengine executionengine_ocaml "llvm llvm.target ctypes" "executionengine mcjit native"
create_dune_file llvm.ipo transforms/ipo llvm_ipo ipo_ocaml "llvm" "ipo"
create_dune_file llvm.irreader irreader llvm_irreader irreader_ocaml "llvm" "irreader"
create_dune_file llvm.scalar_opts transforms/scalar_opts llvm_scalar_opts scalar_opts_ocaml "llvm" "scalaropts"
create_dune_file llvm.transform_utils transforms/utils llvm_transform_utils transform_utils_ocaml "llvm" "transformutils"
create_dune_file llvm.vectorize transforms/vectorize llvm_vectorize vectorize_ocaml "llvm" "vectorize"
create_dune_file llvm.passmgr_builder transforms/passmgr_builder llvm_passmgr_builder passmgr_builder_ocaml "llvm" "ipo"
create_dune_file llvm.target target llvm_target target_ocaml "llvm" "target"
create_dune_file llvm.linker linker llvm_linker linker_ocaml "llvm" "linker"
create_dune_file llvm.debuginfo debuginfo llvm_debuginfo debuginfo_ocaml "llvm" "core"
create_dune_file llvm.all_backends all_backends llvm_all_backends all_backends_ocaml "llvm" "$llvm_targets"
for target in $llvm_targets; do
touch "llvm_${target}.opam"
create_dune_file "llvm_$target" backends "llvm_$target" "${target}_ocaml" "llvm" "$target"
done