From 3f5922a5b68b84a9b23c89b9338c4a8dfe52f7b1 Mon Sep 17 00:00:00 2001 From: Alessandro Iezzi Date: Fri, 2 Aug 2024 17:13:05 +0200 Subject: Add script to generate Makefile --- Makefile | 52 +++++++++++++++++++++++++-------------- config.mk | 2 +- scripts/buildmk.sh | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+), 19 deletions(-) create mode 100755 scripts/buildmk.sh diff --git a/Makefile b/Makefile index e7e56c4..752994d 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,12 @@ include config.mk -OBJ = utils/string.o properties-parser.o properties-lexer.o +SRC = utils/string.c properties-parser.c properties-lexer.c +STATIC_OBJ = build/static/utils/string.o build/static/properties-parser.o build/static/properties-lexer.o +SHARED_OBJ = build/shared/utils/string.o build/shared/properties-parser.o build/shared/properties-lexer.o -CFLAGS = -DVERSION=${VER} +CFLAGS = -DVERSION= LFLAGS = -ll -ly - all: lib${NAME}.a lib${NAME}.so # Generate the lexer source @@ -19,23 +20,38 @@ properties-parser.h properties-parser.c: properties.y @mv y.tab.c properties-parser.c @mv y.tab.h properties-parser.h -.c.o: - ${CC} ${CFLAGS} -c $< -o $@ - -build_static: - @make clean ${OBJ} - -build_shared: - @make CFLAGS="${CFLAGS} -fPIC" clean ${OBJ} +lib${NAME}.a: ${STATIC_OBJ} + ${CC} ${STATIC_OBJ} -o $@ ${LFLAGS} -lib${NAME}.a: build_static - ${CC} ${OBJ} -o $@ ${LFLAGS} - -lib${NAME}.so: build_shared - ${CC} ${OBJ} -o $@ ${LFLAGS} +lib${NAME}.so: ${SHARED_OBJ} + ${CC} ${SHARED_OBJ} -o $@ ${LFLAGS} clean: - @rm -f ${OBJ} *.core a.out + @rm -rf build @rm -f properties-lexer.c lex.* - @rm -f properties @rm -f properties-parser.* y.* + +build/static/utils/string.o: utils/string.c + @mkdir -p build/static/utils + ${CC} ${CFLAGS} -c utils/string.c -o $@ + +build/static/properties-parser.o: properties-parser.c + @mkdir -p build/static + ${CC} ${CFLAGS} -c properties-parser.c -o $@ + +build/static/properties-lexer.o: properties-lexer.c + @mkdir -p build/static + ${CC} ${CFLAGS} -c properties-lexer.c -o $@ + +build/shared/utils/string.o: utils/string.c + @mkdir -p build/shared/utils + ${CC} ${CFLAGS} -fPIC -c utils/string.c -o $@ + +build/shared/properties-parser.o: properties-parser.c + @mkdir -p build/shared + ${CC} ${CFLAGS} -fPIC -c properties-parser.c -o $@ + +build/shared/properties-lexer.o: properties-lexer.c + @mkdir -p build/shared + ${CC} ${CFLAGS} -fPIC -c properties-lexer.c -o $@ + diff --git a/config.mk b/config.mk index b936204..5b9e788 100644 --- a/config.mk +++ b/config.mk @@ -2,4 +2,4 @@ VER = 0.0.0-alpha.1 LEX = lex YACC = yacc CC = cc -NAME = properties \ No newline at end of file +NAME = properties diff --git a/scripts/buildmk.sh b/scripts/buildmk.sh new file mode 100755 index 0000000..5edff96 --- /dev/null +++ b/scripts/buildmk.sh @@ -0,0 +1,71 @@ +#!/bin/sh + +gen_obj() +{ + dir=$1 + for i in $SRC; do + echo -n $i | sed "s|^| $1|g" | sed 's/\.c$/.o/g' + done +} + +SRC="$(find * -name '*.c') properties-parser.c properties-lexer.c" +OBJA=$(gen_obj build/static/) +OBJSO=$(gen_obj build/shared/) + +cat << EOF > Makefile +include config.mk + +SRC = $SRC +STATIC_OBJ =$OBJA +SHARED_OBJ =$OBJSO + +CFLAGS = -DVERSION=${VER} +LFLAGS = -ll -ly + +all: lib\${NAME}.a lib\${NAME}.so + +# Generate the lexer source +properties-lexer.c: properties.l + \${LEX} properties.l + @mv lex.yy.c properties-lexer.c + +# Generate the parser source +properties-parser.h properties-parser.c: properties.y + \${YACC} -d properties.y + @mv y.tab.c properties-parser.c + @mv y.tab.h properties-parser.h + +lib\${NAME}.a: \${STATIC_OBJ} + \${CC} \${STATIC_OBJ} -o \$@ \${LFLAGS} + +lib\${NAME}.so: \${SHARED_OBJ} + \${CC} \${SHARED_OBJ} -o \$@ \${LFLAGS} + +clean: + @rm -rf build + @rm -f properties-lexer.c lex.* + @rm -f properties-parser.* y.* + +EOF + + +for obj in $OBJA; do +src=$(echo $obj | sed 's/.*\/static\///g' | sed 's/\.o/.c/g') +cat << EOF >> Makefile +$obj: $src + @mkdir -p $(dirname $obj) + \${CC} \${CFLAGS} -c $src -o \$@ + +EOF +done + + +for obj in $OBJSO; do +src=$(echo $obj | sed 's/.*\/shared\///g' | sed 's/\.o/.c/g') +cat << EOF >> Makefile +$obj: $src + @mkdir -p $(dirname $obj) + \${CC} \${CFLAGS} -fPIC -c $src -o \$@ + +EOF +done \ No newline at end of file -- cgit v1.2.3