1
0
mirror of git://git.suckless.org/sbase synced 2024-12-21 22:50:16 +00:00

sbase-box: Simplify Makefile rule

The Makefile rule was too complex and these cases is better to just
move it to a script where will be eassier to use sed properly
and not looping over all the files 4 times.
This commit is contained in:
Roberto E. Vargas Caballero 2023-09-26 19:26:47 +02:00
parent 8ca12835a5
commit 3c36fb4177
3 changed files with 70 additions and 20 deletions

1
.gitignore vendored
View File

@ -1,4 +1,5 @@
*.o
/build
/getconf.h
/libutf.a
/libutil.a

View File

@ -236,27 +236,9 @@ dist: clean
gzip sbase-$(VERSION).tar
rm -rf sbase-$(VERSION)
sbase-box: $(LIB) $(SRC) getconf.h
mkdir -p build
cp $(HDR) build
cp getconf.h build
for f in $(SRC); do sed "s/^main(/$$(echo "$${f%.c}" | sed s/-/_/g)_&/" < $$f > build/$$f; done
echo '#include <libgen.h>' > build/$@.c
echo '#include <stdio.h>' >> build/$@.c
echo '#include <stdlib.h>' >> build/$@.c
echo '#include <string.h>' >> build/$@.c
echo '#include "util.h"' >> build/$@.c
for f in $(SRC); do echo "int $$(echo "$${f%.c}" | sed s/-/_/g)_main(int, char **);"; done >> build/$@.c
echo 'int main(int argc, char *argv[]) { char *s = basename(argv[0]);' >> build/$@.c
echo 'if(!strcmp(s,"sbase-box")) { argc--; argv++; s = basename(argv[0]); } if(0) ;' >> build/$@.c
echo "else if (!strcmp(s, \"install\")) return xinstall_main(argc, argv);" >> build/$@.c
echo "else if (!strcmp(s, \"[\")) return test_main(argc, argv);" >> build/$@.c
for f in $(SRC); do echo "else if(!strcmp(s, \"$${f%.c}\")) return $$(echo "$${f%.c}" | sed s/-/_/g)_main(argc, argv);"; done >> build/$@.c
echo 'else { fputs("[ ", stdout);' >> build/$@.c
for f in $(SRC); do echo "fputs(\"$${f%.c} \", stdout);"; done >> build/$@.c
echo 'putchar(0xa); }; return 0; }' >> build/$@.c
sbase-box: $(BIN)
scripts/mkbox
$(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) -o $@ build/*.c $(LIB)
rm -r build
sbase-box-install: sbase-box
mkdir -p $(DESTDIR)$(PREFIX)/bin
@ -276,5 +258,6 @@ sbase-box-uninstall: uninstall
clean:
rm -f $(BIN) $(OBJ) $(LIB) sbase-box sbase-$(VERSION).tar.gz
rm -f getconf.h
rm -rf build
.PHONY: all install uninstall dist sbase-box-install sbase-box-uninstall clean

66
scripts/mkbox Executable file
View File

@ -0,0 +1,66 @@
#!/bin/sh
trap "rm -rf build" INT QUIT TERM
rm -rf build
mkdir -p build
cp *.h build
cat > build/sbase-box.c <<EOF
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "util.h"
#include "sbase-box.h"
struct cmd {
char *name;
int (*fn)(int, char **);
} cmds[] = {
{"install", xinstall_main},
{"[", test_main},
$(grep -l ^main *.c |
while read f
do
sed -n '
/^main/ {
s/main/'${f%.c}'_main/
s/-/_/g
w build/'$f'
s/\(^.*\)(.*)/ {"'${f%.c}'", \1},/p
d
}
w 'build/$f $f
done)
{NULL},
};
int
main(int argc, char *argv[])
{
char *s = basename(argv[0]);
struct cmd *bp;
if(!strcmp(s,"sbase-box")) {
argc--; argv++;
s = basename(argv[0]);
}
for (bp = cmds; bp->name; ++bp) {
if (strcmp(bp->name, s) == 0)
return (*bp->fn)(argc, argv);
}
for (bp = cmds; bp->name; ++bp)
printf("%s ", bp->name);
putchar('\n');
return 0;
}
EOF
sed -n 's/.* \(.*_main\).*/int \1(int, char **);/p'\
build/sbase-box.c > build/sbase-box.h