sbase-box: Simplify Makefile rule - sbase - suckless unix tools | |
git clone git://git.suckless.org/sbase | |
Log | |
Files | |
Refs | |
README | |
LICENSE | |
--- | |
commit 3c36fb417738b5830f9f22b0ac88a266dd6eed5b | |
parent 8ca12835a58ead392822bfd52241e68eba7cd99f | |
Author: Roberto E. Vargas Caballero <[email protected]> | |
Date: Tue, 26 Sep 2023 19:26:47 +0200 | |
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. | |
Diffstat: | |
M .gitignore | 1 + | |
M Makefile | 23 +++-------------------- | |
A scripts/mkbox | 66 +++++++++++++++++++++++++++++… | |
3 files changed, 70 insertions(+), 20 deletions(-) | |
--- | |
diff --git a/.gitignore b/.gitignore | |
@@ -1,4 +1,5 @@ | |
*.o | |
+/build | |
/getconf.h | |
/libutf.a | |
/libutil.a | |
diff --git a/Makefile b/Makefile | |
@@ -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)_&/… | |
- echo '#include <libgen.h>' … | |
- echo '#include <stdio.h>' … | |
- echo '#include <stdlib.h>' … | |
- echo '#include <string.h>' … | |
- echo '#include "util.h"' … | |
- for f in $(SRC); do echo "int $$(echo "$${f%.c}" | sed s/-/_/g)_main(i… | |
- echo 'int main(int argc, char *argv[]) { char *s = basename(argv[0]);'… | |
- echo 'if(!strcmp(s,"sbase-box")) { argc--; argv++; s = basename(argv[0… | |
- echo "else if (!strcmp(s, \"install\")) return xinstall_main(argc, arg… | |
- echo "else if (!strcmp(s, \"[\")) return test_main(argc, argv);" … | |
- for f in $(SRC); do echo "else if(!strcmp(s, \"$${f%.c}\")) return $$(… | |
- echo 'else { fputs("[ ", stdout);' … | |
- for f in $(SRC); do echo "fputs(\"$${f%.c} \", stdout);"; done … | |
- echo 'putchar(0xa); }; return 0; }' … | |
+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 | |
diff --git a/scripts/mkbox b/scripts/mkbox | |
@@ -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 |