From b51ec72399b1e729394b5f44cc4bd4a095451713 Mon Sep 17 00:00:00 2001 From: Keith Busch Date: Tue, 6 Oct 2015 16:36:53 -0600 Subject: [PATCH] Add a version Starting with v0.1. Signed-off-by: Keith Busch --- Makefile | 11 ++++++++--- NVME-VERSION-GEN | 42 ++++++++++++++++++++++++++++++++++++++++++ nvme.c | 17 ++++++++++++++++- 3 files changed, 66 insertions(+), 4 deletions(-) create mode 100755 NVME-VERSION-GEN diff --git a/Makefile b/Makefile index e5eb69c9..bbb65ab3 100644 --- a/Makefile +++ b/Makefile @@ -24,7 +24,12 @@ endif default: $(NVME) -nvme: nvme.c $(NVME_HEADER) argconfig.o suffix.o +NVME-VERSION-FILE: FORCE + @$(SHELL_PATH) ./NVME-VERSION-GEN +-include NVME-VERSION-FILE +override CFLAGS += -DNVME_VERSION='"$(NVME_VERSION)"' + +nvme: nvme.c $(NVME_HEADER) argconfig.o suffix.o NVME-VERSION-FILE $(CC) $(CFLAGS) nvme.c $(LDFLAGS) -o $(NVME) argconfig.o suffix.o argconfig.o: $(SRC)/argconfig.c $(SRC)/argconfig.h $(SRC)/suffix.h @@ -39,7 +44,7 @@ doc: $(NVME) all: doc clean: - rm -f $(NVME) *.o *~ a.out + rm -f $(NVME) *.o *~ a.out NVME-VERSION-FILE $(MAKE) -C Documentation clean clobber: clean @@ -54,4 +59,4 @@ install-bin: default install: install-bin install-man -.PHONY: default all doc clean clobber install install-bin install-man +.PHONY: default all doc clean clobber install install-bin install-man FORCE diff --git a/NVME-VERSION-GEN b/NVME-VERSION-GEN new file mode 100755 index 00000000..cbfc8527 --- /dev/null +++ b/NVME-VERSION-GEN @@ -0,0 +1,42 @@ +#!/bin/sh + +GVF=NVME-VERSION-FILE +DEF_VER=0.1 + +LF=' +' + +# First see if there is a version file (included in release tarballs), +# then try git-describe, then default. +if test -f version +then + VN=`cat version` || VN="$DEF_VER" +elif test -d .git -o -f .git && + VN=`git describe --match "nvme-[0-9]*" --abbrev=4 HEAD 2>/dev/null` && + case "$VN" in + *$LF*) (exit 1) ;; + v[0-9]*) + git update-index -q --refresh + test -z "`git diff-index --name-only HEAD --`" || + VN="$VN-dirty" ;; + esac +then + VN=$VN +else + VN="$DEF_VER" +fi + +VN=`expr "$VN" : v*'\(.*\)'` + +if test -r $GVF +then + VC=`sed -e 's/^NVME_VERSION = //' <$GVF` +else + VC=unset +fi +test "$VN" = "$VC" || { + echo >&2 "NVME_VERSION = $VN" + echo "NVME_VERSION = $VN" >$GVF +} + + diff --git a/nvme.c b/nvme.c index fd5e5178..52a6ffc1 100644 --- a/nvme.c +++ b/nvme.c @@ -57,6 +57,8 @@ static int fd; static struct stat nvme_stat; static const char *devicename; +static const char nvme_version_string[] = NVME_VERSION; + #define COMMAND_LIST \ ENTRY(LIST, "list", "List all NVMe devices and namespaces on machine", list) \ ENTRY(ID_CTRL, "id-ctrl", "Send NVMe Identify Controller", id_ctrl) \ @@ -91,6 +93,7 @@ static const char *devicename; ENTRY(READ_CMD, "read", "Submit a read command, return results", read_cmd) \ ENTRY(WRITE_CMD, "write", "Submit a write command, return results", write_cmd) \ ENTRY(REGISTERS, "show-regs", "Shows the controller registers. Requires admin character device", show_registers) \ + ENTRY(VERSION, "version", "Shows the program version", version) \ ENTRY(HELP, "help", "Display this help", help) #define ENTRY(i, n, h, f) \ @@ -3652,6 +3655,7 @@ static void general_help() { unsigned i; + printf("%s\n", nvme_version_string); usage("nvme"); printf("\n"); printf("'' / '/dev/nvmeX' may be either an NVMe character "\ @@ -3664,6 +3668,12 @@ static void general_help() printf("See 'nvme help ' for more information on a specific command.\n"); } +static int version(int argc, char **argv) +{ + printf("nvme version %s\n", nvme_version_string); + return 0; +} + static int help(int argc, char **argv) { if (argc == 1) @@ -3677,10 +3687,15 @@ static void handle_internal_command(int argc, char **argv) { unsigned i; struct command *cmd; + char *str = argv[0]; + + /* translate --help and --version into commands */ + while (*str == '-') + str++; for (i = 0; i < NUM_COMMANDS; i++) { cmd = &commands[i]; - if (strcmp(argv[0], cmd->name)) + if (strcmp(str, cmd->name)) continue; exit(cmd->fn(argc, argv)); } -- 2.50.1