summaryrefslogtreecommitdiff
path: root/gen/mk.sh
blob: bbf8c8c1c1809f3e7c7f274f630370f9eaecab92 (plain)
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
#!/bin/sh

TOPDIR=$(dirname $0)
DEPS=${TOPDIR}/.deps
SRCDIR=${TOPDIR}/src

classify_source () {
	NAME=$(basename $1 .c)

	if grep -q "^REFERENCE(" $1; then
		echo REFERENCE
	elif grep -q "^#define ${NAME}[ (]" $1; then
		echo MACRO
	elif grep -q "#undef ${NAME}" $1; then
		echo MACRO
	elif grep -q '^typedef .*(\*' $1; then
		echo FNTYPE
	elif grep -q "^typedef " $1; then
		echo TYPE
	elif grep -q "^struct (^ )*;$" $1; then
		echo TYPE
	elif grep -q "^typedef .*{$" $1; then
		echo TYPE_LONG
	elif grep -q "^struct.*{" $1; then
		echo RECORD
	elif grep -q "^union.*{" $1; then
		echo RECORD
	elif grep -q "^[A-Za-z_].*[ \*]${NAME}[[;]" $1; then
		echo EXTERN
	elif grep -q 'TGFN' $1; then
		echo TGFN
	else
		echo FUNCTION
	fi
}

version_guard () {
	parsed=/tmp/$(basename $1).v
	grep -F -e 'STDC(' -e 'POSIX(' -e 'XOPEN(' $1 | sort | m4 $(dirname $0)/ftm.m4 - | grep . > $parsed
	lines=$(wc -l $parsed | cut -f1 -d' ')

	if [ $lines -ne 0 ]; then
		printf '#if'
		loop=1
		while [ $loop -lt $lines ]; do
			printf '\t(%s) || \\\n' "$(sed -ne "${loop}p;q" $parsed)"
			loop=$((loop + 1))
		done

		printf '\t(%s)\n' "$(sed -ne "${loop}p" $parsed)"
	fi

	rm -f $parsed
}

get_declaration () {
	case ${2:-$(classify_source $1)} in

	REFERENCE)
		ref="$(grep -F 'REFERENCE(' $1 | m4 -DREFERENCE='$1')"
		if (echo "$ref" | grep -q '<.*>'); then
			echo "#include $ref"
		else
			get_declaration "src/${ref}"
		fi
		;;

	MACRO)
		if ! (grep -q '#undef' $1 && grep -q 'MAY-BE-UNDEF' $1); then
			grep -E '^(#(if|def|undef|el|end)|	)' $1
		fi
		;;

	TYPE|TYPE_LONG|RECORD|FNTYPE)
		base=$(basename $1 .c)
		printf '#ifndef __TYPE_%s_DEFINED__\n#define __TYPE_%s_DEFINED__\n' "$base" "$base"

		if grep -q '^#if' $1; then
			sed -ne '/^#if/,/#endif/p' $1
		elif grep -qE '^(typedef|struct|union) .*\{' $1; then
			sed -ne '/{$/,/^}/p' $1
		elif grep -qE '^(typedef|struct|union) .*[^;]$' $1; then
			grep -E '^(typedef|struct|union|	)' $1
		else
			grep -E '^(typedef|struct|union) ' $1
		fi

		printf '#endif\n\n'
		;;

	EXTERN)
		echo "extern $(grep '^[a-zA-Z_].*;$' $1)"
		;;

	FUNCTION)
		echo "$(grep '^[a-zA-Z_].*)' $1 | head -n1);"
		;;

	TGFN)
		echo "$(grep 'TGFN.*)$' $1 | m4 -DTGFN='$1' -DTYPE='double');"
		echo "$(grep 'TGFN.*)$' $1 | m4 -DTGFN='$1f' -DTYPE='float');"
		echo "$(grep 'TGFN.*)$' $1 | m4 -DTGFN='$1l' -DTYPE='long double');"
		;;

	*)
		# unknown type, so try guessing
		get_declaration $1
		;;

	esac
}

stdc_base() {
	(grep 'STDC(' $1 | m4 -DSTDC='$1') || echo 0
}

posix_base() {
	(grep 'POSIX(' $1 | m4 -DPOSIX='$1') || echo 0
}

xopen_base() {
	(grep 'XOPEN(' $1 | m4 -DXOPEN='$1') || echo 0
}

find_all() {
	rm -rf "${DEPS}"
	mkdir -p "${DEPS}"
	find "${SRCDIR}" -name \*.c > "${DEPS}/all.c"
	find "${SRCDIR}" -name \*.ref > "${DEPS}/all.ref"
	find "${SRCDIR}" -name \*.s > "${DEPS}/all.s"
	grep '#include <.*>' $(cat "${DEPS}/all.c" "${DEPS}/all.ref") |
		sed 's/^.*<\(.*\.h\)>.*/\1/' | sort -u > "${DEPS}/all.h"
}

version_sources() {
	for file in $(cat "${DEPS}/all.c" "${DEPS}/all.ref"); do
		printf '%s\n' "$file"
		c_version=$(stdc_base "${file}")
		p_version=$(posix_base "${file}")
		x_version=$(xopen_base "${file}")

		if [ -n "${c_version}" ]; then
			mkdir -p "${DEPS}/c.${c_version}"
			echo "$file" >> "${DEPS}/c.${c_version}/sources"
		fi

		if [ -n "${p_version}" ]; then
			mkdir -p "${DEPS}/p.${p_version}"
			echo "$file" >> "${DEPS}/p.${p_version}/sources"
		fi

		if [ -n "${x_version}" ]; then
			mkdir -p "${DEPS}/x.${x_version}"
			echo "$file" >> "${DEPS}/x.${x_version}/sources"
		fi
	done
}

make_headers_mk() {
	if ! [ -f "${DEPS}/all.h" ]; then
		find_all
	fi

	rm -f "${TOPDIR}/.headers.mk"
	printf '.POSIX:\n.DEFAULT: headers\n\n' > "${TOPDIR}/.headers.mk"
	printf 'include .config.mk\n\n' >> "${TOPDIR}/.headers.mk"

	for header in $(cat "${DEPS}/all.h"); do
		printf 'Building dependencies for <%s>\n' "$header"
		printf '$(INCDIR)/%s: mkh.sh ' "$header" >> "${TOPDIR}/.headers.mk"
		mkdir -p $(dirname "${TOPDIR}/.deps/h/${header}.deps")
		grep -l "#include <${header}>" $(cat "${DEPS}/all.c" "${DEPS}/all.ref") > "${TOPDIR}/.deps/h/${header}.deps"
		sed -e "s#${SRCDIR}#\$(SRCDIR)#" < "${TOPDIR}/.deps/h/${header}.deps" | tr '\n' ' ' >> "${TOPDIR}/.headers.mk"
		printf '\n\tINCDIR=$(INCDIR) sh mkh.sh $(INCDIR)/%s\n\n' "${header}" >> "${TOPDIR}/.headers.mk"
	done

	printf 'headers: ' >> "${TOPDIR}/.headers.mk"
	for header in $(cat "${DEPS}/all.h"); do
		printf ' \\\n\t$(INCDIR)/%s' "$header" >> "${TOPDIR}/.headers.mk"
	done

	printf '\n' >> "${TOPDIR}/.headers.mk"
}

make_deps_mk() {
	if ! [ -f "${DEPS}/all.c" ]; then
		find_all
	fi

	rm -f "${TOPDIR}/.deps.mk"
	rm -f "${DEPS}"/lib*

	printf '.POSIX:\n.SILENT:\ndefault: all\n\n' > "${TOPDIR}/.deps.mk"
	printf 'include .config.mk\n\n' >> "${TOPDIR}/.deps.mk"
	printf 'BASE_CFLAGS=-I$(INCDIR) -Isrc -fno-builtin -nostdinc\n' >> "${TOPDIR}/.deps.mk"
	printf '\n' >> "${TOPDIR}/.deps.mk"

	printf '.POSIX:\nlibc_C_0_OBJS=' > "${DEPS}/libc.C_0"
	# Assume all assembly is useful regardless of standard version
	for file in $(sed -e 's/\.[^.]*\.s$/.ARCH.s/g' .deps/all.s | sort -u); do
		BASE=$(basename $file .ARCH.s)
		SRC=$(echo $file | sed -e 's/\.ARCH\./.$(ARCHITECTURE)-$(WORDSIZE)./')
		printf 'libc.a($(OBJDIR)/%s.$(ARCHITECTURE)-$(WORDSIZE).o): $(OBJDIR)/%s.$(ARCHITECTURE)-$(WORDSIZE).o\n' $BASE $BASE >> "${TOPDIR}/.deps.mk"
		printf ' \\\n\tlibc.a($(OBJDIR)/%s.$(ARCHITECTURE)-$(WORDSIZE).o)' $BASE >> "${DEPS}/libc.C_0"
		printf '$(OBJDIR)/%s.$(ARCHITECTURE)-$(WORDSIZE).o: %s\n' $BASE $SRC >> "${TOPDIR}/.deps.mk"
		printf '\t$(CC) $(BASE_CFLAGS) $(CFLAGS) -c %s -o $@\n' $SRC >> "${TOPDIR}/.deps.mk"
		printf '\t echo [AS] $@\n\n' >> "${TOPDIR}/.deps.mk"
	done

	for file in $(cat "${DEPS}/all.c"); do
		printf 'Building dependencies from %s\n' "$file"
		type=$(classify_source "${file}")

		if [ ${type} = EXTERN -o ${type} = TGFN -o ${type} = FUNCTION ]; then
			LINK=$(grep -F 'LINK(' $file | m4 -DLINK='$1')
			LIB=lib${LINK:-c}
			OBJ=$(basename $file .c).o

			printf '%s.a(%s): $(OBJDIR)/%s\n' $LIB $OBJ $OBJ >> "${TOPDIR}/.deps.mk"
			printf '$(OBJDIR)/%s: %s' "$OBJ" "$file" >> "${TOPDIR}/.deps.mk"
			#for header in $(grep '#include' "${file}"); do
				#printf ' \\\n\t$(INCDIR)/%s' "$header" >> "${TOPDIR}/.deps.mk"
			#done
			printf '\n\t$(CC) $(BASE_CFLAGS) $(CFLAGS) -c %s -o $@\n' "$file" >> "${TOPDIR}/.deps.mk"
			printf '\techo [CC] $@\n' >> "${TOPDIR}/.deps.mk"

			cver=$(stdc_base $file)
			pver=$(posix_base $file)
			xver=$(xopen_base $file)

			if [ -n "$cver" ]; then
				if ! [ -f "${DEPS}/${LIB}.C_${cver}" ]; then
					printf '.POSIX:\n%s_C_%s_OBJS=' "${LIB}" "${cver}" > "${DEPS}/${LIB}.C_${cver}"
				fi
				printf ' \\\n\t%s.a($(OBJDIR)/%s)' $LIB $OBJ >> "${DEPS}/${LIB}.C_${cver}"
			fi

			if [ -n "$pver" ]; then
				if ! [ -f "${DEPS}/${LIB}.POSIX_${pver}" ]; then
					printf '.POSIX:\n%s_POSIX_%s_OBJS=' "${LIB}" "${pver}" > "${DEPS}/${LIB}.POSIX_${pver}"
				fi
				printf ' \\\n\t%s.a($(OBJDIR)/%s)' $LIB $OBJ >> "${DEPS}/${LIB}.POSIX_${pver}"
			fi

			if [ -n "$xver" ]; then
				if ! [ -f "${DEPS}/${LIB}.XOPEN_${xver}" ]; then
					printf '.POSIX:\n%s_XOPEN_%s_OBJS=' "${LIB}" "${xver}" > "${DEPS}/${LIB}.XOPEN_${xver}"
				fi
				printf ' \\\n\t%s.a($(OBJDIR)/%s)' $LIB $OBJ >> "${DEPS}/${LIB}.XOPEN_${xver}"
			fi

			if [ -z "$cver" ] && [ -z "$pver" ] && [ -z "$xver" ]; then
				if ! [ -f "${DEPS}/${LIB}.C_0" ]; then
					printf '.POSIX:\n%s_C_0_OBJS=' "${LIB}" > "${DEPS}/${LIB}.C_0"
				fi
				printf ' \\\n\t%s.a($(OBJDIR)/%s)' $LIB $OBJ >> "${DEPS}/${LIB}.C_0"
			fi
		fi
	done

	printf '\n' >> "${TOPDIR}/.deps.mk"

	for libdep in ${DEPS}/lib*; do
		LIB=$(basename $libdep | sed -e 's/\..*$//')
		VER=$(basename $libdep | sed -e 's/^.*\.//')
		echo adding dependencies for $LIB v $VER
		printf 'include $(TOPDIR)/.deps/%s\n' $(basename $libdep) >> "${TOPDIR}/.deps.mk"
	done

	printf '\n' >> "${TOPDIR}/.deps.mk"
}