-
Notifications
You must be signed in to change notification settings - Fork 14
/
build.sh
executable file
·176 lines (147 loc) · 3.66 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
#!/bin/sh
PLUGIN_MAIN=""
PLUGIN_DEPS=""
PLUGIN_DESC=""
HAS_BUILD_TOOLS="FALSE"
# checks that necessary build tools are installed
check_build_tools() {
if [ "no-$(which bapbuild)" != "no-" ] && [ "no-$(which bapbundle)" != "no-" ]; then
HAS_BUILD_TOOLS="TRUE"
fi
}
# reads line env variable and extracts main, depends, descr fields
parse_line() {
field=`echo $line | cut -d':' -f1 | sed 's/^[ *]//g;s/[ *]*$//'`
if [ "no-$field" != "no-" ]; then
if [ "$field" = "main" ]; then
PLUGIN_MAIN=`echo $line | cut -d':' -f2`
fi
if [ "$field" = "depends" ]; then
PLUGIN_DEPS=`echo $line | cut -d':' -f2 | sed 's/^[ *]//g'`
fi
if [ "$field" = "descr" ]; then
PLUGIN_DESC=`echo $line | cut -d':' -f2`
fi
fi
}
# reads info file if exists
# pre: already in the directory with a check
read_info() {
unset PLUGIN_MAIN
unset PLUGIN_DEPS
unset PLUGIN_DESC
PLUGIN_MAIN=`echo $1 | sed 's/-/_/g'`
if [ -e info ]; then
while IFS= read -r line
do
parse_line $line
done < info
parse_line $line
fi
}
# packs a recipe folder into a zip file for the ease of sharing
# Usage: pack_recipe <path-to-the-recipe-folder>
# pre: already in the directory with a check
pack_recipe() {
NAME=`basename "$1"`
recipe=`find . -name "*.scm" | head -n 1`
if [ "no-$recipe" != "no-" ]; then
zip -r $NAME.recipe * -x \*.ml \*.mli \*.plugin _build/* >/dev/null
fi
}
build() {
set -e
OLD=`pwd`
echo "Entering directory \`$1'"
cd $1
read_info $1
mlfiles=`find . -name "*.ml" | head -n 1`
if [ "no$mlfiles" != "no" ]; then
if [ "$HAS_BUILD_TOOLS" = "TRUE" ]; then
NAME=`echo "$PLUGIN_MAIN" | cut -d'.' -f1`
DEPS=`echo "$PLUGIN_DEPS" | sed 's/ */,/g' `
if [ "has-no$DEPS" != "has-no" ]; then
echo bapbuild -pkgs $DEPS $NAME.plugin
bapbuild -pkgs $DEPS $NAME.plugin
else
bapbuild $NAME.plugin
fi
if [ "no-$PLUGIN_DESC" != "no-" ]; then
bapbundle update -desc \"$PLUGIN_DESC\" $NAME.plugin
fi
else
echo "SKIP TARGET $1: bapbuild/bapbundle not found"
fi
fi
pack_recipe $1
echo "Leaving directory \`$1'"
cd $OLD
}
install() {
OLD=`pwd`
cd $1
DST=$(bap config sysdatadir)
plugin=`find . -name "*.plugin" | head -n 1`
recipe=`find . -name "*.recipe" | head -n 1`
if [ "no-$plugin" != "no-" ]; then
bapbundle install $plugin
fi
if [ "no-$recipe" != "no-" ]; then
cp *.recipe $DST
fi
cd $OLD
}
clean() {
OLD=`pwd`
cd $1
rm -f *.recipe
bapbuild -clean > /dev/null
cd $OLD
}
process_cmd() {
CMD=$1
DST=$2
case $CMD in
build)
build $DST
;;
install)
install $DST
;;
clean)
clean $DST
;;
*)
echo "Usage: $0 {build|install|clean} with an optional check name "
exit 1
esac
}
check() {
path=`which $1`
if [ "no$path" = "no" ]; then
echo "can't find $1, exiting ... "
exit 1
fi
}
# usage:
# run <build|install|clean> check
run() {
check zip
check sed
check bapbundle
check bapbuild
COMMAND=$1
TARGET=$2
check_build_tools
if [ "all$TARGET" = "all" ]; then
for d in `ls`; do
if [ -d "$d" ] && [ "$d" != "tests" ]; then
process_cmd $COMMAND $d
fi
done
wait
else
process_cmd $COMMAND $TARGET
fi
}
run "$@"