diff --git a/.gitignore b/.gitignore index bcf71b0..9f4fd02 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,6 @@ valgrind.log # Testing files *.narc + +# Generated files +cli/include/version.h diff --git a/Makefile b/Makefile index fa50f3e..13f02d6 100644 --- a/Makefile +++ b/Makefile @@ -41,11 +41,14 @@ CLIINC = $(wildcard cli/include/*.h) CLISRC = $(wildcard cli/src/*.c) CLIOBJ = $(CLISRC:.c=.o) CLIDEP = $(CLISRC:.c=.d) +CLIVER = cli/include/version.h ALLSRC = $(CLISRC) $(LIBSRC) ALLINC = $(CLIINC) $(LIBINC) -.PHONY: all cli lib debug release clean install +VERSION = VERSION + +.PHONY: all cli lib debug release clean install version all: lib cli @@ -60,14 +63,18 @@ release: CFLAGS += -DNDEBUG -O3 release: clean all clean: - $(RM) $(LIBTARGET) $(LIBOBJ) $(LIBDEP) $(CLITARGET) $(CLIOBJ) $(CLIDEP) + $(RM) $(LIBTARGET) $(LIBOBJ) $(LIBDEP) $(CLITARGET) $(CLIOBJ) $(CLIDEP) $(CLIVER) -include $(LIBDEP) -include $(CLIDEP) +version: +$(CLIVER): tools/version.sh $(VERSION) + $< $(VERSION) $@ + # Statically link the CLI $(CLITARGET): CFLAGS += -I./cli/include -$(CLITARGET): $(CLIOBJ) $(LIBOBJ) +$(CLITARGET): $(CLIVER) $(CLIOBJ) $(LIBOBJ) $(CC) $(LDFLAGS) -o $@ $^ $(LIBTARGET): LDFLAGS += -shared diff --git a/VERSION b/VERSION new file mode 100644 index 0000000..6e8bf73 --- /dev/null +++ b/VERSION @@ -0,0 +1 @@ +0.1.0 diff --git a/cli/include/meson.build b/cli/include/meson.build new file mode 100644 index 0000000..852cb79 --- /dev/null +++ b/cli/include/meson.build @@ -0,0 +1,7 @@ +cli_version_h = custom_target( + 'cli_version_h', + output: 'version.h', + command: [version_sh, version, '@OUTPUT@'], +) + +cli_includes = include_directories('.') diff --git a/cli/meson.build b/cli/meson.build index be0d362..22dd14c 100644 --- a/cli/meson.build +++ b/cli/meson.build @@ -24,4 +24,4 @@ cli_sources = files( 'src/strvec.c', ) -cli_includes = include_directories('include') +subdir('include') diff --git a/cli/src/narc.c b/cli/src/narc.c index ad49e22..d2e05e0 100644 --- a/cli/src/narc.c +++ b/cli/src/narc.c @@ -23,6 +23,7 @@ #include "command.h" #include "strutil.h" +#include "version.h" // clang-format off const struct command handlers[] = { @@ -34,7 +35,7 @@ const struct command handlers[] = { { 0 }, }; -static const char *version = "0.1.0"; +static const char *version = NARC_VERSION; // clang-format on int main(int argc, const char **argv) diff --git a/meson.build b/meson.build index 894badb..e26cf05 100644 --- a/meson.build +++ b/meson.build @@ -18,7 +18,9 @@ project( license: 'Apache-2.0', license_files: 'LICENSE', meson_version: '>=1.5.0', - version: '0.1.0', + + # This is a little ugly, but it works... I guess... + version: run_command('./tools/version.sh', 'VERSION', check: false).stdout().strip(), default_options: { 'buildtype': 'release', # O3 'warning_level': '3', # all, extra, pedantic @@ -28,7 +30,9 @@ project( native = get_option('native') install = native and meson.is_cross_build() ? false : true +version = files('VERSION') +subdir('tools') subdir('lib') subdir('cli') @@ -50,6 +54,7 @@ libnarc_dep = declare_dependency( narc_exe = executable( 'narc', sources: [ + cli_version_h, cli_sources, lib_sources, ], diff --git a/tools/meson.build b/tools/meson.build new file mode 100644 index 0000000..dc50204 --- /dev/null +++ b/tools/meson.build @@ -0,0 +1 @@ +version_sh = find_program('version.sh', native: true) diff --git a/tools/version.sh b/tools/version.sh new file mode 100755 index 0000000..e8b87de --- /dev/null +++ b/tools/version.sh @@ -0,0 +1,21 @@ +#!/usr/bin/env bash + +VERSION_FILE=$1 +TARGET_FILE=$2 + +VERSION=$(cat "${VERSION_FILE}") + +if test -z "${TARGET_FILE}"; then + echo "${VERSION}" +else + { + echo "/* THIS HEADER IS AUTOMATICALLY GENERATED */" + echo "/* DO NOT MODIFY IT!!! */" + echo "#ifndef NARC_VERSION_H" + echo "#define NARC_VERSION_H" + echo "" + echo "#define NARC_VERSION \"${VERSION}\"" + echo "" + echo "#endif /* NARC_VERSION_H */" + } >"${TARGET_FILE}" +fi