cmd: add and print --version on startup

This commit is contained in:
Kamil Trzcinski 2023-02-28 21:54:24 +01:00
parent 20ca08ffad
commit 65e3b17397
4 changed files with 42 additions and 6 deletions

1
.gitignore vendored
View File

@ -7,3 +7,4 @@ tmp/
/test_* /test_*
.vscode/ .vscode/
/Procfile* /Procfile*
/version.h

View File

@ -3,6 +3,9 @@ SRC := $(wildcard **/*.c **/*/*.c **/*.cc **/*/*.cc)
HEADERS := $(wildcard **/*.h **/*/*.h **/*.hh **/*/*.hh) HEADERS := $(wildcard **/*.h **/*/*.h **/*.hh **/*/*.hh)
HTML := $(wildcard html/*.js html/*.html) 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 CFLAGS := -Werror -Wall -g -I$(CURDIR) -D_GNU_SOURCE
LDLIBS := -lpthread -lstdc++ 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/libsrtp -lsrtp2
LDLIBS += -L$(LIBDATACHANNEL_PATH)/build/deps/libjuice -ljuice-static LDLIBS += -L$(LIBDATACHANNEL_PATH)/build/deps/libjuice -ljuice-static
LDLIBS += -lcrypto -lssl LDLIBS += -lcrypto -lssl
camera-streamer: $(LIBDATACHANNEL_PATH)/build/libdatachannel-static.a
endif endif
HTML_SRC = $(addsuffix .c,$(HTML)) HTML_SRC = $(addsuffix .c,$(HTML))
OBJS = $(patsubst %.cc,%.o,$(patsubst %.c,%.o,$(SRC) $(HTML_SRC))) OBJS = $(patsubst %.cc,%.o,$(patsubst %.c,%.o,$(SRC) $(HTML_SRC)))
all: version
+make $(TARGET)
.SUFFIXES: .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)) %: cmd/% $(filter-out third_party/%, $(OBJS))
$(CCACHE) $(CXX) $(CFLAGS) -o $@ $(filter-out cmd/%, $^) $(filter $</%, $^) $(LDLIBS) $(CCACHE) $(CXX) $(CFLAGS) -o $@ $(filter-out cmd/%, $^) $(filter $</%, $^) $(LDLIBS)

View File

@ -4,6 +4,7 @@
#include "device/camera/camera.h" #include "device/camera/camera.h"
#include "output/rtsp/rtsp.h" #include "output/rtsp/rtsp.h"
#include "output/webrtc/webrtc.h" #include "output/webrtc/webrtc.h"
#include "version.h"
#include <signal.h> #include <signal.h>
#include <unistd.h> #include <unistd.h>
@ -56,6 +57,8 @@ int main(int argc, char *argv[])
return -1; return -1;
} }
printf("%s Version: %s (%s)\n", argv[0], GIT_VERSION, GIT_REVISION);
deprecations(); deprecations();
inherit(); inherit();

View File

@ -1,4 +1,5 @@
#include "opts.h" #include "opts.h"
#include "version.h"
#include "util/opts/log.h" #include "util/opts/log.h"
#include <stddef.h> #include <stddef.h>
@ -7,6 +8,11 @@
#define OPT_LENGTH 30 #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) static void print_help(option_t *options, const char *cmd)
{ {
printf("Usage:\n"); printf("Usage:\n");
@ -88,7 +94,7 @@ static void print_help(option_t *options, const char *cmd)
printf("\n"); 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; option_t *option = NULL;
const char *value = strchr(key, '='); 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); LOG_DEBUG(NULL, "Parsing '%s'. Got value='%s', and option='%s'", key, value, option ? option->name : NULL);
if (!option || !value) { if (!option || !value) {
@ -152,18 +164,26 @@ int parse_opts(option_t *options, int argc, char *argv[])
if (key[0] == '-') { if (key[0] == '-') {
key++; key++;
if (key[0] == '-') if (key[0] == '-') {
key++; key++;
}
} else { } else {
LOG_ERROR(NULL, "The '%s' is not option (should start with - or --).", key); LOG_ERROR(NULL, "The '%s' is not option (should start with - or --).", key);
} }
if (!strcmp(key, "help")) { if (!strcmp(key, "help")) {
print_help(options, argv[0]); print_help(options, argv[0]);
exit(-1);
return -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) { if (ret <= 0) {
LOG_ERROR(NULL, "Parsing '%s' returned '%d'.", argv[arg], ret); LOG_ERROR(NULL, "Parsing '%s' returned '%d'.", argv[arg], ret);
} }