]> git.seodisparate.com - SimpleArchiver/commitdiff
Impl "--version"
authorStephen Seo <seo.disparate@gmail.com>
Tue, 28 Jan 2025 03:18:06 +0000 (12:18 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Tue, 28 Jan 2025 03:18:06 +0000 (12:18 +0900)
CMakeLists.txt
cosmopolitan/Makefile
src/parser.c
src/version.h [new file with mode: 0644]

index 0f74c6af3b7f41826ec23e7b8cdec98d1260d6f5..0f2b600b0cd62eb923528516a635ea788690322d 100644 (file)
@@ -68,6 +68,24 @@ set_source_files_properties(src/archiver.c
         COMPILE_FLAGS -Wno-format-nonliteral
 )
 
+if(IS_EXECUTABLE /usr/bin/git)
+    execute_process(COMMAND /usr/bin/git describe --long --tags
+        WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
+        OUTPUT_VARIABLE SIMPLE_ARCHIVER_GIT_VER
+        OUTPUT_STRIP_TRAILING_WHITESPACE)
+    message("Got version \"${SIMPLE_ARCHIVER_GIT_VER}\"")
+    set_source_files_properties(src/parser.c
+        PROPERTIES
+            COMPILE_FLAGS "-DSIMPLE_ARCHIVER_VERSION_STR=\\\"${SIMPLE_ARCHIVER_GIT_VER}\\\""
+    )
+else()
+    message("Setting version \"${SimpleArchiver_VERSION}\"")
+    set_source_files_properties(src/parser.c
+        PROPERTIES
+            COMPILE_FLAGS "-DSIMPLE_ARCHIVER_VERSION_STR=\\\"${SimpleArchiver_VERSION}\\\""
+    )
+endif()
+
 if(CMAKE_BUILD_TYPE STREQUAL "Release")
     target_compile_options(simplearchiver PUBLIC
         -fno-delete-null-pointer-checks -fno-strict-overflow
index 93ce1f3f350b1f4e62e36c520e3b8da9ed4918de..734888dde26e66dd92670f1597cee0af60f201fc 100644 (file)
@@ -1,5 +1,9 @@
 COSMOCC ?= ${HOME}/Downloads/cosmocc/bin/cosmocc
 
+VERSION_STR := $(shell /usr/bin/git describe --long --tags || /usr/bin/echo 1.11)
+
+OTHER_FLAGS := -DSIMPLE_ARCHIVER_VERSION_STR=\"${VERSION_STR}\"
+
 OBJDIR = objdir
 OUTDIR = out
 
@@ -24,7 +28,8 @@ HEADERS = \
                ../src/data_structures/hash_map.h \
                ../src/data_structures/priority_heap.h \
                ../src/platforms.h \
-               ../src/users.h
+               ../src/users.h \
+               ../src/version.h
 
 OBJECTS = $(addprefix ${OBJDIR}/,$(subst ..,PREVDIR,$(patsubst %.c,%.c.o,${SOURCES})))
 
index 67610869babff04a684ac824a0c926c0eb38ccf9..ecd55104e19fe3ef769ae4ea613318f74b64ae31 100644 (file)
@@ -40,6 +40,7 @@
 #include "data_structures/linked_list.h"
 #include "helpers.h"
 #include "parser_internal.h"
+#include "version.h"
 
 /// Gets the first non "./"-like character in the filename.
 size_t simple_archiver_parser_internal_get_first_non_current_idx(
@@ -238,6 +239,7 @@ void simple_archiver_print_usage(void) {
           "--force-dir-permissions <3-octal-values> : Force set permissions "
           "for directories on archive creation/extraction\n"
           "  Must be three octal characters like \"755\" or \"440\"\n");
+  fprintf(stderr, "--version : prints version and exits\n");
   fprintf(stderr,
           "-- : specifies remaining arguments are files to archive/extract\n");
   fprintf(
@@ -645,6 +647,9 @@ int simple_archiver_parse_args(int argc, const char **argv,
 
         --argc;
         ++argv;
+      } else if (strcmp(argv[0], "--version") == 0) {
+        fprintf(stderr, "Version: %s\n", SIMPLE_ARCHIVER_VERSION_STR);
+        exit(0);
       } else if (argv[0][0] == '-' && argv[0][1] == '-' && argv[0][2] == 0) {
         is_remaining_args = 1;
       } else if (argv[0][0] != '-') {
diff --git a/src/version.h b/src/version.h
new file mode 100644 (file)
index 0000000..66efcd8
--- /dev/null
@@ -0,0 +1,26 @@
+// ISC License
+//
+// Copyright (c) 2024 Stephen Seo
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
+// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+//
+// `version.h` is the header for version information.
+
+#ifndef SEODISPARATE_COM_SIMPLE_ARCHIVER_VERSION_H_
+#define SEODISPARATE_COM_SIMPLE_ARCHIVER_VERSION_H_
+
+#ifndef SIMPLE_ARCHIVER_VERSION_STR
+#define SIMPLE_ARCHIVER_VERSION_STR "UnknownVersion"
+#endif
+
+#endif