summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2019-01-27 20:44:53 -0500
committerJakob Kaivo <jkk@ung.org>2019-01-27 20:44:53 -0500
commit916f7bf9652927f7d1f32c29e493d899972282e9 (patch)
tree29488e8650024c67db3a11ffaeb9ae96dd229078
parentb3c44ba42458947bd6da22b14dae4b8ab2310a9a (diff)
update makefile and header generation
-rw-r--r--mk.sh25
-rwxr-xr-xmkh.sh122
-rwxr-xr-xmkmf.sh70
3 files changed, 182 insertions, 35 deletions
diff --git a/mk.sh b/mk.sh
new file mode 100644
index 00000000..f1cce2c8
--- /dev/null
+++ b/mk.sh
@@ -0,0 +1,25 @@
+#!/bin/sh
+
+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 "typedef.* ${NAME}.*;" $1; then
+ echo TYPE
+ elif grep -q "typedef.*{$" $1; then
+ echo TYPE_LONG
+ elif grep -q "struct.*${NAME} {" $1; then
+ echo STRUCT
+ elif grep -q "union.* ${NAME} {" $1; then
+ echo UNION
+ elif grep -q "^[A-Za-z_].* ${NAME};" $1; then
+ echo EXTERN
+ elif grep -q 'TGFN' $1; then
+ echo TGFN
+ else
+ echo FUNCTION
+ fi
+}
diff --git a/mkh.sh b/mkh.sh
new file mode 100755
index 00000000..5fd62625
--- /dev/null
+++ b/mkh.sh
@@ -0,0 +1,122 @@
+#!/bin/sh
+
+. ./mk.sh
+
+export LC_ALL=POSIX
+export LANG=POSIX
+HEADER=$1
+GUARD=__$(echo $HEADER | tr a-z/. A-Z__)__
+mkdir -p $(dirname $HEADER)
+shift
+exec > $HEADER
+
+cat <<EOF
+/*
+UNG's Not GNU
+
+Copyright (c) 2011-2019, Jakob Kaivo <jkk@ung.org>
+
+Permission to use, copy, modify, and/or distribute this software for any
+purpose with or without fee is hereby granted, provided that the above
+copyright notice and this permission notice appear in all copies.
+
+THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+*/
+
+#ifndef ${GUARD}
+#define ${GUARD}
+
+EOF
+
+rm -f $HEADER.*
+for i in $@; do
+ # TODO: refs
+ echo $i >> $HEADER.$(classify_source $i)
+done
+
+if [ -f $HEADER.MACRO ]; then
+ for i in $(sort $HEADER.MACRO); do
+ grep ' *extern.*;$' $i
+ grep -E '^#(if|def|undef|el|end)' $i
+ done
+ printf '\n'
+ rm $HEADER.MACRO
+fi
+
+if [ -f $HEADER.TYPE ]; then
+ for i in $(sort $HEADER.TYPE); do
+ if grep -q '^#ifdef' $i; then
+ sed -ne '/#ifdef/,/#endif/p' $i
+ elif grep -q '^typedef.*;$' $i; then
+ grep '^typedef' $i
+ else
+ sed -ne '/^typedef/,/\}.*;$/p' $i
+ fi
+ done
+ printf '\n'
+ rm $HEADER.TYPE
+fi
+
+if [ -f $HEADER.TYPE_LONG ]; then
+ for i in $(sort $HEADER.TYPE_LONG); do
+ if grep -q '^#ifdef' $i; then
+ sed -ne '/#ifdef/,/#endif/p' $i
+ elif grep -q '^typedef.*;$' $i; then
+ grep '^typedef' $i
+ else
+ sed -ne '/^typedef/,/\}.*;$/p' $i
+ fi
+ done
+ printf '\n'
+ rm $HEADER.TYPE_LONG
+fi
+
+if [ -f $HEADER.STRUCT -o -f $HEADER.UNION ]; then
+ touch $HEADER.STRUCT $HEADER.UNION
+ for i in $(sort $HEADER.STRUCT $HEADER.UNION 2>/dev/null); do
+ if grep -q '^struct' $i; then
+ sed -ne '/^struct/,/\};/p' $i
+ else
+ sed -ne '/^union/,/\};/p' $i
+ fi
+ done
+ printf '\n'
+ rm -f $HEADER.STRUCT $HEADER.UNION
+fi
+
+if [ -f $HEADER.EXTERN ]; then
+ for i in $(sort $HEADER.EXTERN); do
+ printf 'extern %s' "$(grep '^[a-zA-Z_].*;$' $i)"
+ done
+ printf '\n'
+ rm -f $HEADER.EXTERN
+fi
+
+if [ -f $HEADER.TGFN ]; then
+ for i in $(sort $HEADER.TGFN); do
+ printf '%s;\n' "$(sed -e "/{/q" $i | tail -n2 | head -n1 | m4 '-DTGFN=$1' -DTYPE=double)"
+ done
+ printf '\n'
+ rm -f $HEADER.TGFN
+fi
+
+if [ -f $HEADER.FUNCTION ]; then
+ if grep -q restrict $(cat $HEADER.FUNCTION); then
+ printf '#if ! defined __STDC_VERSION__ || __STDC_VERSION__ < 199909L\n'
+ printf '#define restrict\n'
+ printf '#endif\n\n'
+ fi
+ for i in $(sort $HEADER.FUNCTION); do
+ printf '%s;\n' "$(sed -e "/{/q" $i | tail -n2 | head -n1)"
+ done
+ printf '\n'
+ rm -f $HEADER.FUNCTION
+fi
+
+printf '\n#endif\n'
diff --git a/mkmf.sh b/mkmf.sh
index 8861a7ad..c8129690 100755
--- a/mkmf.sh
+++ b/mkmf.sh
@@ -1,30 +1,12 @@
#!/bin/sh
-classify_source () {
- NAME=$(basename $1 .c)
-
- if grep -q "define ${NAME}[ (]" $1; then
- echo MACRO
- elif grep -q "typedef.* ${NAME};" $1; then
- echo TYPE
- elif grep -q "typedef.*{$" $1; then
- echo TYPE_LONG
- elif grep -q "struct.*${NAME} {" $1; then
- echo STRUCT
- elif grep -q "union.* ${NAME} {" $1; then
- echo UNION
- elif grep -q "^[A-Za-z_].* ${NAME};" $1; then
- echo EXTERN
- else
- echo FUNCTION
- fi
-}
+. ./mk.sh
STANDARD=${1-9899-1990}
#STANDARD=${1-POSIX.1-1990}
if [ ! -f .dep/to-build ]; then
- rm -rf .dep Makefile header.mk
+ rm -rf .dep Makefile .headers.mk
mkdir -p .dep
echo ${STANDARD} > .dep/to-build
fi
@@ -42,20 +24,30 @@ fi
rm -f .dep/${STANDARD}.*
-for i in $(find std/${STANDARD} -name \*.c); do
- NAME=$(basename $i .c)
- TYPE=$(classify_source $i)
- HEADER=$(sed -ne 's/.*<\(.*\.h\)>.*/\1/p' $i)
- LIB=$(grep '^LINK(' $i | m4 -DLINK='lib$1')
+for i in $(find std/${STANDARD} -name \*.c) $(find std/${STANDARD} -name \*.ref); do
+ FILE=$i
+ BASE=$i
+ if grep -q '^REFERENCE(' $FILE; then
+ BASE=$(grep '^REFERENCE(' $i | m4 -DREFERENCE='std/$1')
+ fi
+ NAME=$(basename $BASE .c)
+ TYPE=$(classify_source $BASE)
+ HEADER=$(sed -ne 's/.*<\(.*\.h\)>.*/\1/p' $FILE)
+ LIB=$(grep '^LINK(' $BASE | m4 -DLINK='lib$1')
LIB=${LIB:-libc}
if [ ! -f .dep/${HEADER}.mk ]; then
mkdir -p $(dirname .dep/${HEADER}.mk)
- printf '$(INCDIR)/%s:' "${HEADER}" > .dep/${HEADER}.mk
+ printf '%s_SOURCES = ' $(echo ${HEADER} | tr /. _) > .dep/${HEADER}.mk
+ fi
+ printf ' \\\n\t%s' "$FILE" >> .dep/${HEADER}.mk
+
+ if [ $FILE != $BASE ]; then
+ printf ' \\\n\t%s' "$BASE" >> .dep/${HEADER}.mk
+ continue
fi
- printf ' \\\n\t%s' "$i" >> .dep/${HEADER}.mk
- if [ ${TYPE} = EXTERN -o ${TYPE} = FUNCTION ]; then
+ if [ ${TYPE} = EXTERN -o ${TYPE} = FUNCTION -o ${TYPE} = TGFN ]; then
printf '$(OBJDIR)/%s.o: %s $(INCDIR)/%s' ${NAME} $i ${HEADER}> .dep/${NAME}.o.mk
for j in $(grep include $i); do
: # TODO: add each header to depends file here
@@ -70,7 +62,6 @@ for i in $(find std/${STANDARD} -name \*.c); do
fi
printf ' \\\n\t$(OBJDIR)/%s.o' ${NAME} >> .dep/${LIB}.a.mk
fi
-
done
if [ $(cat .dep/to-build) = ${STANDARD} ]; then
@@ -86,16 +77,22 @@ if [ $(cat .dep/to-build) = ${STANDARD} ]; then
printf '\n\n%s.a: $(%s_OBJS)\n\t$(AR) r $@ $?\n\n' $LIB $LIB >> Makefile
done
- printf '.POSIX:\n\ninclude config.mk\n\n' > headers.mk
+ printf '.POSIX:\n\ninclude config.mk\n\n' > .headers.mk
+ printf 'default: headers\n\n' >> .headers.mk
for i in $(find .dep -name \*.h.mk); do
- cat $i >> headers.mk
- printf '\n\t./gen-header $@\n\n' >> headers.mk
+ HDR=$(echo $i | sed -e 's#^.*\.dep/##;s#\.mk$##')
+ cat $i >> .headers.mk
+ printf '\n\n$(INCDIR)/%s: $(%s_SOURCES)\n' ${HDR} $(echo $HDR | tr /. _) >> .headers.mk
+ printf '\tsh mkh.sh $@ $(%s_SOURCES)\n\n' $(echo $HDR | tr /. _) >> .headers.mk
+ printf ' $(INCDIR)/%s' ${HDR} >> .dep/all_headers.mk
done
+ printf 'headers:' >> .headers.mk
+ cat .dep/all_headers.mk >> .headers.mk
cat .dep/*.o.mk >> Makefile
- printf '$(OBJDIR)/libc.o: nonstd/libc.c\n\t$(CC) $(CFLAGS) -c $? -o $@\n\n' >> Makefile
- printf '$(OBJDIR)/x86-64.o: nonstd/x86-64.s\n\t$(CC) $(CFLAGS) -c $? -o $@\n\n' >> Makefile
+ printf '$(OBJDIR)/libc.o: nonstd/libc.c\n\t-@mkdir -p $(OBJDIR)\n\t$(CC) $(CFLAGS) -c $? -o $@\n\n' >> Makefile
+ printf '$(OBJDIR)/x86-64.o: nonstd/x86-64.s\n\t-@mkdir -p $(OBJDIR)\n\t$(CC) $(CFLAGS) -c $? -o $@\n\n' >> Makefile
printf 'all:' >> Makefile
for i in .dep/lib*.a.mk; do
@@ -103,7 +100,10 @@ if [ $(cat .dep/to-build) = ${STANDARD} ]; then
done
printf '\n\n' >> Makefile
+ printf 'headers:\n\t@$(MAKE) -f .headers.mk $@\n\n' >> Makefile
+
printf 'clean:\n\trm -rf $(OBJDIR) *.a\n\n' >> Makefile
- printf 'git-clean: clean\n\trm -rf .dep header.mk Makefile\n\n' >> Makefile
+ printf 'git-clean: clean\n\trm -rf .dep .headers.mk Makefile\n\n' >> Makefile
+ rm -f .dep/to-build
fi