diff --git a/Makefile b/Makefile index 67b176e..27ff21b 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -OBJS := parser.o main.o redsocks.o log.o http-connect.o socks4.o socks5.o http-relay.o base.o base64.o md5.o http-auth.o utils.o redudp.o dnstc.o +OBJS := parser.o main.o redsocks.o log.o http-connect.o socks4.o socks5.o http-relay.o base.o base64.o md5.o http-auth.o utils.o redudp.o dnstc.o gen/version.o SRCS := $(OBJS:.o=.c) CONF := config.h DEPS := .depend @@ -29,6 +29,22 @@ $(CONF): ;; \ esac +# Dependency on .git is useful to rebuild `version.c' after commit +# FIXME: non-git builds should be supported. +gen/version.c: *.c *.h gen/.build .git + rm -f $@.tmp + echo '/* this file is auto-generated during build */' > $@.tmp + echo '#include "../version.h"' > $@.tmp + echo 'const char* redsocks_version = "redsocks.git/"' >> $@.tmp + echo '"'`git describe --tags`'"' >> $@.tmp + [ `git status --porcelain | grep -v -c '^??'` != 0 ] && { echo '"-unclean"' >> $@.tmp; } || true + echo ';' >> $@.tmp + mv -f $@.tmp $@ + +gen/.build: + mkdir -p gen + touch $@ + base.c: $(CONF) $(DEPS): $(SRCS) @@ -64,3 +80,4 @@ clean: distclean: clean $(RM) tags $(DEPS) + $(RM) -r gen diff --git a/main.c b/main.c index 5c9b9fe..e6f6ec0 100644 --- a/main.c +++ b/main.c @@ -25,6 +25,7 @@ #include "log.h" #include "main.h" #include "utils.h" +#include "version.h" extern app_subsys redsocks_subsys; extern app_subsys base_subsys; @@ -66,7 +67,7 @@ int main(int argc, char **argv) int i; red_srand(); - while ((opt = getopt(argc, argv, "tc:p:")) != -1) { + while ((opt = getopt(argc, argv, "h?vtc:p:")) != -1) { switch (opt) { case 't': conftest = true; @@ -77,13 +78,18 @@ int main(int argc, char **argv) case 'p': pidfile = optarg; break; + case 'v': + puts(redsocks_version); + return EXIT_SUCCESS; default: printf( - "Usage: %s [-t] [-c config] [-p pidfile]\n" + "Usage: %s [-?hvt] [-c config] [-p pidfile]\n" + " -h, -? this message\n" + " -v print version\n" " -t test config syntax\n" " -p write pid to pidfile\n", argv[0]); - return EXIT_FAILURE; + return (opt == '?' || opt == 'h') ? EXIT_SUCCESS : EXIT_FAILURE; } } diff --git a/version.h b/version.h new file mode 100644 index 0000000..3158c3c --- /dev/null +++ b/version.h @@ -0,0 +1,6 @@ +#ifndef VERSION_H_SUN_NOV_27_03_22_30_2011 +#define VERSION_H_SUN_NOV_27_03_22_30_2011 + +extern const char* redsocks_version; + +#endif // VERSION_H_SUN_NOV_27_03_22_30_2011