feat: Generate version at compilation time

This commit is contained in:
Rachel 2024-12-31 19:16:04 -08:00
parent 6ea239eeca
commit 2109b614e5
9 changed files with 52 additions and 6 deletions

3
.gitignore vendored
View File

@ -17,3 +17,6 @@ valgrind.log
# Testing files
*.narc
# Generated files
cli/include/version.h

View File

@ -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

1
VERSION Normal file
View File

@ -0,0 +1 @@
0.1.0

7
cli/include/meson.build Normal file
View File

@ -0,0 +1,7 @@
cli_version_h = custom_target(
'cli_version_h',
output: 'version.h',
command: [version_sh, version, '@OUTPUT@'],
)
cli_includes = include_directories('.')

View File

@ -24,4 +24,4 @@ cli_sources = files(
'src/strvec.c',
)
cli_includes = include_directories('include')
subdir('include')

View File

@ -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)

View File

@ -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,
],

1
tools/meson.build Normal file
View File

@ -0,0 +1 @@
version_sh = find_program('version.sh', native: true)

21
tools/version.sh Executable file
View File

@ -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