From 6a0f28d241c7237d2a2e83f4d89122e1db1d4b72 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Tue, 28 Jan 2025 12:18:06 +0900 Subject: [PATCH] Impl "--version" --- CMakeLists.txt | 18 ++++++++++++++++++ cosmopolitan/Makefile | 7 ++++++- src/parser.c | 5 +++++ src/version.h | 26 ++++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 src/version.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 0f74c6a..0f2b600 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/cosmopolitan/Makefile b/cosmopolitan/Makefile index 93ce1f3..734888d 100644 --- a/cosmopolitan/Makefile +++ b/cosmopolitan/Makefile @@ -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}))) diff --git a/src/parser.c b/src/parser.c index 6761086..ecd5510 100644 --- a/src/parser.c +++ b/src/parser.c @@ -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 index 0000000..66efcd8 --- /dev/null +++ b/src/version.h @@ -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 -- 2.49.0