-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.sh
231 lines (213 loc) · 3.87 KB
/
config.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
outdir=${OUTDIR:-.build}
srcdir=${SRCDIR:-$(dirname "$0")}
AR=${AR:-ar}
AS=${AS:-as}
CC=${CC:-cc}
CFLAGS=${CFLAGS:-}
LDFLAGS=${LDFLAGS:-}
LD=${LD:-ld}
QBE="${QBE:-qbe} -t $(if [ $(uname -m) = arm64 ]; then echo "arm64"; else echo "amd64_sysv"; fi ) -Gm"
if [ `uname -s` = "OpenBSD" ]
then
LD="${LD:-ld} -nopie"
fi
for arg
do
case "$arg" in
--bindir=*)
BINDIR=${arg#*=}
;;
--libdir=*)
LIBDIR=${arg#*=}
;;
--mandir=*)
MANDIR=${arg#*=}
;;
--prefix=*)
PREFIX=${arg#*=}
if [ "$PREFIX" = "/usr" ]
then
SYSCONFDIR=/etc
fi
;;
--sharedir=*)
SHAREDIR=${arg#*=}
;;
--sysconfdir=*)
SYSCONFDIR=${arg#*=}
;;
*)
printf 'Error: unknown configure option %s\n' "$arg" >&2
exit 1
;;
esac
done
subdir() {
eval ". $srcdir/$1/configure"
}
headers() {
printf 'HEADERS=\\\n'
while [ $# -ne 0 ]
do
printf '\tinclude/%s \\\n' "$1"
shift
done
}
genrules() {
target="$1"
shift
printf '# Begin generated rules for %s\n' "$target"
for file in "$@"
do
ext="${file#*.}"
file="${file%.*}"
deps=
if [ $ext = "ha" ]
then
deps=" harec"
fi
printf '%s.o: %s.%s%s $(HEADERS)\n' "$file" "$file" "$ext" "$deps"
done
printf '%s_objects=\\\n' "$target"
n=0
for file in "$@"
do
file="${file%.*}"
n=$((n+1))
if [ $n -eq $# ]
then
printf '\t%s.o\n' "$file"
else
printf '\t%s.o \\\n' "$file"
fi
done
printf '# End generated rules for %s\n' "$target"
}
append_cflags() {
for flag
do
CFLAGS="$(printf '%s \\\n\t%s' "$CFLAGS" "$flag")"
done
}
test_cflags() {
[ ! -e "$outdir"/check.c ] && cat <<-EOF > "$outdir"/check.c
int main(void) { return 0; }
EOF
werror=""
case "$CFLAGS" in
*-Werror*)
werror="-Werror"
;;
esac
if $CC $werror "$@" -o /dev/null "$outdir"/check.c >/dev/null 2>&1
then
append_cflags "$@"
else
return 1
fi
}
find_library() {
name="$1"
pc="$2"
printf "Checking for %s... " "$name"
if ! pkg-config "$pc" 2>/dev/null
then
printf "NOT FOUND\n"
printf "Tried pkg-config %s\n" "$pc"
return 1
fi
printf "OK\n"
CFLAGS="$CFLAGS $(pkg-config --cflags "$pc")"
LIBS="$LIBS $(pkg-config --libs "$pc")"
}
run_configure() {
mkdir -p "$outdir"
for flag in -g -std=c11 -D_XOPEN_SOURCE=700 \
-Wall -Wextra -Werror -pedantic \
-Wno-unused-parameter
do
printf "Checking for %s... " "$flag"
if test_cflags "$flag"
then
echo yes
else
echo no
fi
done
printf "Checking for qbe... "
if $QBE -h > /dev/null 2>&1
then
echo yes
else
echo no
echo "Error: no qbe binary found"
exit 1
fi
printf "Creating %s/config.mk... " "$outdir"
cat <<EOF > "$outdir"/config.mk
CC=$CC
AS=$AS
LD=$LD
AR=$AR
QBE=$QBE
LIBS=$LIBS
PREFIX=${PREFIX:-/usr/local}
OUTDIR=${outdir}
SRCDIR=${srcdir}
BINDIR=${BINDIR:-\$(PREFIX)/bin}
SHAREDIR=${SHAREDIR:-\$(PREFIX)/share}
SYSCONFDIR=${SYSCONFDIR:-\$(PREFIX)/etc}
LIBDIR=${LIBDIR:-\$(PREFIX)/lib}
MANDIR=${MANDIR:-\$(PREFIX)/share/man}
VARLIBDIR=${MANDIR:-\$(PREFIX)/var/lib}
CACHE=\$(OUTDIR)/cache
CFLAGS=${CFLAGS} \\
-Iinclude -I\$(OUTDIR) \\
-DPREFIX='"\$(PREFIX)"' \\
-DLIBDIR='"\$(LIBDIR)"' \\
-DVARLIBDIR='"\$(VARLIBDIR)"' \\
-DSYSCONFDIR='"\$(SYSCONFDIR)"'
LDFLAGS=${LDFLAGS}
HARECACHE=./mod
all: ${all}
EOF
for target in $all
do
$target >>"$outdir"/config.mk
done
echo done
cat <<-EOF >harec.sh
export PATH="$(pwd)":\$PATH
EOF
if [ "$srcdir" = "." ]
then
return
fi
populate() (
path="$1"
mkdir -p "${path#$srcdir/}"
fullpath() ( cd "$1" && pwd )
for orig in "$path"/*
do
link="${orig#$srcdir/}"
if [ -d "$orig" ]
then
mkdir -p $link
populate "$orig"
elif [ -f "$orig" ]
then
ln -sf "$(fullpath "$path")"/"$(basename "$orig")" "$link"
fi
done
)
printf "Populating build dir... "
populate "$srcdir/doc"
populate "$srcdir/include"
populate "$srcdir/types"
populate "$srcdir/rt"
populate "$srcdir/src"
populate "$srcdir/tests"
populate "$srcdir/testmod"
ln -sf "$srcdir"/Makefile ./
echo done
}