-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuild
executable file
·332 lines (289 loc) · 7.93 KB
/
build
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
#!/bin/sh
# Copyright 2017-2019, 2023, 2024 Karl Landstrom <karl@miasap.se>
#
# This file is part of OBNC.
#
# OBNC is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# OBNC is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with OBNC. If not, see <http://www.gnu.org/licenses/>.
set -o errexit -o nounset
readonly selfDirPath="$(cd "$(dirname "$0")"; pwd -P)"
readonly LEX="${LEX:-lex}"
readonly YACC="${YACC:-yacc}"
prefix="/usr/local"
libdir="lib"
cIntType=int
cRealType=double
EchoAndRun()
{
echo "$@"
eval "$@"
}
BuildCSource()
{
EchoAndRun cd "$selfDirPath/src"
if [ ! -e lex.yy.c ] || [ ! -e lex.yy.h ] || [ lex.yy.c -ot Oberon.l ] || [ lex.yy.h -ot Oberon.l ]; then
EchoAndRun "$LEX" --header-file=lex.yy.h Oberon.l
else
echo "lex.yy.c and lex.yy.h is up to date"
fi
if [ ! -e y.tab.c ] || [ ! -e y.tab.h ] || [ y.tab.c -ot Oberon.y ]; then
local tmpdir="${TMPDIR:-/tmp}"
local bakFile="$tmpdir/y.tab.h.$$"
if [ -e y.tab.h ]; then
cp -p y.tab.h "$bakFile"
trap "rm '$bakFile'" EXIT
fi
#option -D is a Bison extension to POSIX yacc
if ! EchoAndRun "$YACC" -D parse.error=verbose -d -t Oberon.y; then
EchoAndRun "$YACC" -d -t Oberon.y
fi
#preserve timestamp of y.tab.h if it is unchanged
if cmp -s y.tab.h "$bakFile"; then
cp -p "$bakFile" y.tab.h
fi
else
echo "y.tab.c and y.tab.h is up to date"
fi
cd "$selfDirPath"
}
Build()
{
local version=
if [ -e "VERSION" ]; then
version="$(cat VERSION)"
fi
#generate configuration file for install script
if [ -e CONFIG ]; then
cp CONFIG CONFIG.bak
trap "rm $selfDirPath/CONFIG.bak" EXIT
fi
{
echo "prefix=$prefix"
echo "libdir=$libdir"
echo "cIntType=$cIntType"
echo "cRealType=$cRealType"
echo "version=$version"
} > CONFIG
if ! { [ -e CONFIG.bak ] && cmp -s CONFIG CONFIG.bak; }; then
#generate configuration header files
{
echo "#ifndef CONFIG_H"
echo "#define CONFIG_H"
echo
printf "#define CONFIG_DEFAULT_PREFIX \"%s\"\n" "$prefix"
printf "#define CONFIG_DEFAULT_LIBDIR \"%s\"\n" "$libdir"
printf "#define CONFIG_VERSION \"%s\"\n" "$version"
echo
echo "void Config_Init(void);"
echo "const char *Config_Prefix(void);"
echo "const char *Config_LibDir(void);"
echo
echo "#endif"
} > src/Config.h
{
echo "#ifndef OBNC_CONFIG_H"
echo "#define OBNC_CONFIG_H"
echo
echo "#define OBNC_CONFIG_SHORT 0"
echo "#define OBNC_CONFIG_INT 1"
echo "#define OBNC_CONFIG_LONG 2"
echo "#define OBNC_CONFIG_LONG_LONG 3"
echo
echo "#define OBNC_CONFIG_FLOAT 0"
echo "#define OBNC_CONFIG_DOUBLE 1"
echo "#define OBNC_CONFIG_LONG_DOUBLE 2"
echo
echo "#ifndef OBNC_CONFIG_C_INT_TYPE"
case "$cIntType" in
short)
echo "#define OBNC_CONFIG_C_INT_TYPE OBNC_CONFIG_SHORT";;
int)
echo "#define OBNC_CONFIG_C_INT_TYPE OBNC_CONFIG_INT";;
long)
echo "#define OBNC_CONFIG_C_INT_TYPE OBNC_CONFIG_LONG";;
longlong)
echo "#define OBNC_CONFIG_C_INT_TYPE OBNC_CONFIG_LONG_LONG";;
esac
echo "#endif"
echo
echo "#ifndef OBNC_CONFIG_C_REAL_TYPE"
case "$cRealType" in
float)
echo "#define OBNC_CONFIG_C_REAL_TYPE OBNC_CONFIG_FLOAT";;
double)
echo "#define OBNC_CONFIG_C_REAL_TYPE OBNC_CONFIG_DOUBLE";;
longdouble)
echo "#define OBNC_CONFIG_C_REAL_TYPE OBNC_CONFIG_LONG_DOUBLE";;
esac
echo "#endif"
echo
echo "#ifndef OBNC_CONFIG_NO_GC"
echo "#define OBNC_CONFIG_NO_GC 0"
echo "#endif"
echo
echo "#ifndef OBNC_CONFIG_TARGET_EMB"
echo "#define OBNC_CONFIG_TARGET_EMB 0"
echo "#endif"
echo
echo "#endif"
} > lib/obnc/OBNCConfig.h
fi
BuildCSource
#build compiler
EchoAndRun cd "$selfDirPath/src"
env CFLAGS="${CFLAGS:-}" "$selfDirPath/bin/micb" obnc-compile.c
if [ ! -e "$selfDirPath/bin/obnc-compile" ] || [ "$selfDirPath/bin/obnc-compile" -ot obnc-compile ]; then
cp obnc-compile "$selfDirPath/bin"
fi
#build core library module OBNC
EchoAndRun cd "$selfDirPath/lib/obnc"
"$selfDirPath/bin/micb" OBNCTest.c
#build build command
EchoAndRun cd "$selfDirPath/src"
env CFLAGS="${CFLAGS:-}" "$selfDirPath/bin/micb" obnc.c
if [ ! -e "$selfDirPath/bin/obnc" ] || [ "$selfDirPath/bin/obnc" -ot obnc ]; then
cp obnc "$selfDirPath/bin"
fi
#build path finder
EchoAndRun cd "$selfDirPath/src"
env CFLAGS="${CFLAGS:-}" "$selfDirPath/bin/micb" obnc-path.c
if [ ! -e "$selfDirPath/bin/obnc-path" ] || [ "$selfDirPath/bin/obnc-path" -ot obnc-path ]; then
cp obnc-path "$selfDirPath/bin"
fi
#build documentation generator
EchoAndRun cd "$selfDirPath/src"
env CFLAGS="${CFLAGS:-}" "$selfDirPath/bin/micb" obncdoc.c
if [ ! -e "$selfDirPath/bin/obncdoc" ] || [ "$selfDirPath/bin/obncdoc" -ot obncdoc ]; then
cp obncdoc "$selfDirPath/bin"
fi
cd "$selfDirPath"
}
Clean()
{
rm -f CONFIG
rm -f CONFIG.bak
rm -f bin/obnc
rm -f bin/obnc-compile
rm -f bin/obnc-path
rm -f bin/obncdoc
rm -f bin/*.exe
rm -f src/obnc
rm -f src/obnc-compile
rm -f src/obnc-path
rm -f src/obncdoc
rm -f src/?*Test
rm -f src/*.exe
rm -f src/*.o
rm -fr src/.obnc
rm -f src/Config.h
rm -f lib/obnc/?*Test
rm -f lib/obnc/*.exe
rm -f lib/obnc/*.o
rm -fr lib/obnc/.obnc
rm -f lib/obnc/OBNCConfig.h
rm -f lib/obnc/ext/?*Test
rm -f lib/obnc/ext/*.exe
rm -f lib/obnc/ext/*.o
rm -fr lib/obnc/ext/.obnc
}
CleanAll()
{
Clean
rm -f src/lex.yy.[ch]
rm -f src/y.tab.[ch]
}
PrintHelp()
{
echo "usage: "
printf "\tbuild [c-source | clean | clean-all] [--c-int-type=(short|int|long|longlong)] [--c-real-type=(float|double|longdouble)] [--libdir=LIBDIR] [--prefix=PREFIX]\n"
printf "\tbuild -h\n"
echo
printf "\tc-source\tbuild only Yacc and Lex C source files\n"
printf "\tclean\t\tdelete all generated files except Yacc and Lex C files\n"
printf "\tclean-all\tdelete all generated files\n"
printf "\t--c-int-type\tC type for INTEGER and SET (defaults to int)\n"
printf "\t--c-real-type\tC type for REAL (defaults to double)\n"
printf "\t--libdir\tlibrary installation directory instead of lib\n"
printf "\t--prefix\ttoplevel installation directory instead of /usr/local\n"
printf "\t-h\t\tdisplay help and exit\n"
}
ExitInvalidCommand()
{
echo "Try 'build -h' for more information." >&2
exit 1
}
PathAbsolute()
{
local path="$1"
test "${path#/}" != "$path" || test "${path#[A-Za-z]:}" != "$path"
}
Run()
{
local helpWanted=false
local action=
local arg=
for arg in "$@"; do
case "$arg" in
c-source)
action=c-source;;
clean)
action=clean;;
clean-all)
action=clean-all;;
--c-int-type=*)
cIntType="${arg#--c-int-type=}"
if ! { echo "$cIntType" | grep -q '^\(short\|int\|long\|longlong\)$'; }; then
echo "invalid operand for option c-int-type: $cIntType" >&2
ExitInvalidCommand
fi;;
--c-real-type=*)
cRealType="${arg#--c-real-type=}"
if ! { echo "$cRealType" | grep -q '^\(float\|double\|longdouble\)$'; }; then
echo "invalid operand for option c-real-type: $cRealType" >&2
ExitInvalidCommand
fi;;
--libdir=*)
libdir="${arg#--libdir=}"
if [ "${libdir#*/}" != "$libdir" ]; then
echo "operand for option 'libdir' must be a directory name, not a path: $prefix" >&2
exit 1
fi;;
--prefix=*)
prefix="${arg#--prefix=}"
if ! PathAbsolute "$prefix"; then
echo "operand for option 'prefix' must be an absolute path: $prefix" >&2
exit 1
fi;;
-h)
helpWanted=true;;
*)
echo "invalid argument: $arg"
ExitInvalidCommand
esac
done
if "$helpWanted"; then
PrintHelp
else
case "$action" in
c-source)
BuildCSource;;
clean)
Clean;;
clean-all)
CleanAll;;
*)
Build
esac
fi
}
Run "$@"