mrsh/configure -rwxr-xr-x 5.2 KiB View raw
                                                                                
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
#!/bin/sh -e
SOVERSION=0.0.0

pkg_config=${PKG_CONFIG:-pkg-config}
outdir=${OUTDIR:-.build}
srcdir=${SRCDIR:-$(dirname "$0")}
CC=${CC:-cc}
LIBS=

use_readline=-1
readline=readline

for arg
do
	case "$arg" in
		--prefix=*)
			PREFIX=${arg#*=}
			;;
		--without-readline)
			use_readline=0
			;;
		--with-readline=*)
			use_readline=1
			readline=${arg#*=}
			;;
	esac
done

libmrsh() {
	genrules libmrsh \
		'arithm.c' \
		'array.c' \
		'ast_print.c' \
		'ast.c' \
		'buffer.c' \
		'builtin/alias.c' \
		'builtin/bg.c' \
		'builtin/break.c' \
		'builtin/builtin.c' \
		'builtin/cd.c' \
		'builtin/colon.c' \
		'builtin/command.c' \
		'builtin/dot.c' \
		'builtin/eval.c' \
		'builtin/exit.c' \
		'builtin/export.c' \
		'builtin/false.c' \
		'builtin/fg.c' \
		'builtin/getopts.c' \
		'builtin/hash.c' \
		'builtin/jobs.c' \
		'builtin/pwd.c' \
		'builtin/read.c' \
		'builtin/return.c' \
		'builtin/set.c' \
		'builtin/shift.c' \
		'builtin/times.c' \
		'builtin/true.c' \
		'builtin/type.c' \
		'builtin/ulimit.c' \
		'builtin/umask.c' \
		'builtin/unalias.c' \
		'builtin/unset.c' \
		'builtin/unspecified.c' \
		'builtin/wait.c' \
		'getopt.c' \
		'hashtable.c' \
		'parser/arithm.c' \
		'parser/parser.c' \
		'parser/program.c' \
		'parser/word.c' \
		'shell/arithm.c' \
		'shell/entry.c' \
		'shell/job.c' \
		'shell/path.c' \
		'shell/process.c' \
		'shell/redir.c' \
		'shell/shell.c' \
		'shell/task/pipeline.c' \
		'shell/task/simple_command.c' \
		'shell/task/task.c' \
		'shell/task/word.c' \
		'shell/word.c'
}

mrsh() {
	if [ $use_readline -eq 1 ]
	then
		genrules mrsh \
			'main.c' \
			'frontend/readline.c'
	else
		genrules mrsh \
			'main.c' \
			'frontend/basic.c'
	fi
}

highlight() {
	genrules highlight example/highlight.c
}

genrules() {
	target="$1"
	shift
	printf '# Begin generated rules for %s\n' "$target"
	for file in "$@"
	do
		file="${file%.*}"
		printf '%s.o: %s.c\n' "$file" "$file"
	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
}

append_ldflags() {
	for flag
	do
		LDFLAGS="$(printf '%s \\\n\t%s' "$LDFLAGS" "$flag")"
	done
}

append_libs() {
	for flag
	do
		LIBS="$(printf '%s \\\n\t%s' "$LIBS" "$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
}

test_ldflags() {
	[ ! -e "$outdir"/check.c ] && cat <<-EOF > "$outdir"/check.c
	int main(void) { return 0; }
	EOF
	if $CC "$@" -o /dev/null "$outdir"/check.c >/dev/null 2>&1
	then
		append_ldflags "$@"
	else
		return 1
	fi
}

mkdir -p $outdir

for flag in \
	-g -std=c99 -pedantic -Werror -Wundef -Wlogical-op \
	-Wmissing-include-dirs -Wold-style-definition -Wpointer-arith -Winit-self \
	-Wfloat-equal -Wstrict-prototypes -Wredundant-decls \
	-Wimplicit-fallthrough=2 -Wendif-labels -Wstrict-aliasing=2 -Woverflow \
	-Wformat=2 -Wno-missing-braces -Wno-missing-field-initializers \
	-Wno-unused-parameter -Wno-unused-result
do
	printf "Checking for $flag... "
	if test_cflags "$flag"
	then
		echo yes
	else
		echo no
	fi
done

for flag in -fPIC -Wl,--no-undefined -Wl,--as-needed
do
	test_ldflags "$flag"
done

soname=libmrsh.so.$(echo "$SOVERSION" | cut -d. -f1)
printf "Checking for specifying soname for shared lib... "
if ! \
	test_ldflags -Wl,-soname,$soname || \
	test_ldflags -Wl,-install_name,$soname
then
	echo no
	echo "Unable to specify soname (is $(uname) supported?)" >&2
	exit 1
else
	echo yes
fi

printf "Checking for exported symbol restrictions... "
if ! \
	test_ldflags -Wl,--version-script="libmrsh.gnu.sym" || \
	test_ldflags -Wl,-exported_symbols_list,"libmrsh.darwin.sym"
then
	echo no
	echo "Unable to specify exported symbols (is $(uname) supported?)" >&2
	exit 1
else
	echo yes
fi

if [ $use_readline -eq -1 ]
then
	printf "Checking for readline... "
	if $pkg_config readline
	then
		readline=readline
		use_readline=1
		append_cflags -DHAVE_READLINE
		echo yes
	else
		echo no
	fi
fi
if [ $use_readline -eq -1 ]
then
	printf "Checking for libedit... "
	if $pkg_config libedit
	then
		echo yes
		readline=libedit
		use_readline=1
		append_cflags -DHAVE_EDITLINE
	else
		echo no
	fi
fi

if [ $use_readline -eq 1 ]
then
	append_cflags $($pkg_config --cflags-only-I $readline)
	append_libs $($pkg_config --libs $readline)
fi

printf "Creating $outdir/config.mk... "
cat <<EOF > "$outdir"/config.mk
SOVERSION=$SOVERSION
CC=$CC
PREFIX=${PREFIX:-/usr/local}
_INSTDIR=\$(DESTDIR)\$(PREFIX)
BINDIR?=${BINDIR:-\$(_INSTDIR)/bin}
LIBDIR?=${LIBDIR:-\$(_INSTDIR)/lib}
INCDIR?=${INCDIR:-\$(_INSTDIR)/include}
MANDIR?=${MANDIR:-\$(_INSTDIR)/share/man}
PCDIR?=${PCDIR:-\$(_INSTDIR)/lib/pkgconfig}
CFLAGS=${CFLAGS}
LDFLAGS=${LDFLAGS}
LIBS=${LIBS}
SRCDIR=${srcdir}

all: mrsh highlight libmrsh.so.\$(SOVERSION) \$(OUTDIR)/mrsh.pc
EOF
libmrsh >>"$outdir"/config.mk
mrsh >>"$outdir"/config.mk
highlight >>"$outdir"/config.mk
echo done

touch $outdir/cppcache