From 65e3b17397c146762aa1d2c1b1eadf6ae424b7e0 Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Tue, 28 Feb 2023 21:54:24 +0100 Subject: [PATCH] cmd: add and print `--version` on startup --- .gitignore | 1 + Makefile | 18 +++++++++++++++--- cmd/camera-streamer/main.c | 3 +++ util/opts/opts.c | 26 +++++++++++++++++++++++--- 4 files changed, 42 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 329534d..eef4e2f 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ tmp/ /test_* .vscode/ /Procfile* +/version.h diff --git a/Makefile b/Makefile index 40aec69..03363b6 100644 --- a/Makefile +++ b/Makefile @@ -3,6 +3,9 @@ SRC := $(wildcard **/*.c **/*/*.c **/*.cc **/*/*.cc) HEADERS := $(wildcard **/*.h **/*/*.h **/*.hh **/*/*.hh) HTML := $(wildcard html/*.js html/*.html) +GIT_VERSION ?= $(shell git describe --tags) +GIT_REVISION ?= $(shell git rev-parse --short HEAD) + CFLAGS := -Werror -Wall -g -I$(CURDIR) -D_GNU_SOURCE LDLIBS := -lpthread -lstdc++ @@ -45,16 +48,25 @@ LDLIBS += -L$(LIBDATACHANNEL_PATH)/build/deps/usrsctp/usrsctplib -lusrsctp LDLIBS += -L$(LIBDATACHANNEL_PATH)/build/deps/libsrtp -lsrtp2 LDLIBS += -L$(LIBDATACHANNEL_PATH)/build/deps/libjuice -ljuice-static LDLIBS += -lcrypto -lssl - -camera-streamer: $(LIBDATACHANNEL_PATH)/build/libdatachannel-static.a endif HTML_SRC = $(addsuffix .c,$(HTML)) OBJS = $(patsubst %.cc,%.o,$(patsubst %.c,%.o,$(SRC) $(HTML_SRC))) +all: version + +make $(TARGET) + .SUFFIXES: -all: $(TARGET) +ifeq (1,$(USE_LIBDATACHANNEL)) +camera-streamer: $(LIBDATACHANNEL_PATH)/build/libdatachannel-static.a +endif + +.PHONY: version +version: + echo "#define GIT_VERSION \"$(GIT_VERSION)\"\n#define GIT_REVISION \"$(GIT_REVISION)\"" > version.h.tmp; \ + diff -u version.h version.h.tmp || mv version.h.tmp version.h; \ + rm -f version.h.tmp %: cmd/% $(filter-out third_party/%, $(OBJS)) $(CCACHE) $(CXX) $(CFLAGS) -o $@ $(filter-out cmd/%, $^) $(filter $ #include @@ -56,6 +57,8 @@ int main(int argc, char *argv[]) return -1; } + printf("%s Version: %s (%s)\n", argv[0], GIT_VERSION, GIT_REVISION); + deprecations(); inherit(); diff --git a/util/opts/opts.c b/util/opts/opts.c index bf6107e..28c2bc5 100644 --- a/util/opts/opts.c +++ b/util/opts/opts.c @@ -1,4 +1,5 @@ #include "opts.h" +#include "version.h" #include "util/opts/log.h" #include @@ -7,6 +8,11 @@ #define OPT_LENGTH 30 +static void print_version(const char *cmd) +{ + printf("%s (%s)\n", GIT_VERSION, GIT_REVISION); +} + static void print_help(option_t *options, const char *cmd) { printf("Usage:\n"); @@ -88,7 +94,7 @@ static void print_help(option_t *options, const char *cmd) printf("\n"); } -static int parse_opt(option_t *options, const char *key) +static int parse_opt(option_t *options, const char *key, int dash) { option_t *option = NULL; const char *value = strchr(key, '='); @@ -110,6 +116,12 @@ static int parse_opt(option_t *options, const char *key) } } + if (dash == 2 && strlen(key) == 1) { + LOG_INFO(NULL, "Usage of '--%s' is deprecated change to '-%s'.", key, key); + } else if (dash == 1 && strlen(key) > 1) { + LOG_INFO(NULL, "Usage of '-%s' is deprecated change to '--%s'.", key, key); + } + LOG_DEBUG(NULL, "Parsing '%s'. Got value='%s', and option='%s'", key, value, option ? option->name : NULL); if (!option || !value) { @@ -152,18 +164,26 @@ int parse_opts(option_t *options, int argc, char *argv[]) if (key[0] == '-') { key++; - if (key[0] == '-') + if (key[0] == '-') { key++; + } } else { LOG_ERROR(NULL, "The '%s' is not option (should start with - or --).", key); } if (!strcmp(key, "help")) { print_help(options, argv[0]); + exit(-1); return -1; } - int ret = parse_opt(options, key); + if (!strcmp(key, "version")) { + print_version(argv[0]); + exit(0); + return 0; + } + + int ret = parse_opt(options, key, key - argv[arg]); if (ret <= 0) { LOG_ERROR(NULL, "Parsing '%s' returned '%d'.", argv[arg], ret); }