]> www.infradead.org Git - users/sagi/nvme-cli.git/commitdiff
Add a version v0.1
authorKeith Busch <keith.busch@intel.com>
Tue, 6 Oct 2015 22:36:53 +0000 (16:36 -0600)
committerKeith Busch <keith.busch@intel.com>
Tue, 6 Oct 2015 22:40:08 +0000 (16:40 -0600)
Starting with v0.1.

Signed-off-by: Keith Busch <keith.busch@intel.com>
Makefile
NVME-VERSION-GEN [new file with mode: 0755]
nvme.c

index e5eb69c92313a309a01bd20a4ca336f02be413df..bbb65ab3b47535eceed240f0d0ee0882278dc5d0 100644 (file)
--- 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 (executable)
index 0000000..cbfc852
--- /dev/null
@@ -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 fd5e5178c6b6b57dac61b6e193e878bb2efbaa42..52a6ffc1d281617af5f1cba59b9c750a2eff4140 100644 (file)
--- 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("'<device>' / '/dev/nvmeX' may be either an NVMe character "\
@@ -3664,6 +3668,12 @@ static void general_help()
        printf("See 'nvme help <command>' 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));
        }