Skip to content

Commit

Permalink
refactor: Generate version.h from VERSION
Browse files Browse the repository at this point in the history
Fixes #8
  • Loading branch information
lhearachel committed Jan 2, 2025
1 parent e0b027e commit f8141f2
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ valgrind.log

# Simple test input for README demo
my_input

# Generated sources and headers
include/version.h
13 changes: 10 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ DEP = $(SRC:.c=.d)

MANP = docs/metang.1

.PHONY: default all debug release install clean
VERSION_H = include/version.h

.PHONY: default all debug release install clean version

default: all

Expand All @@ -48,11 +50,16 @@ install: release
uninstall:
rm -rf $(BINDEST)/$(TARGET) $(MANDEST)/$(MANP)

$(TARGET): $(OBJ)
version: $(VERSION_H)

$(VERSION_H): VERSION
./tools/version.sh $< $@

$(TARGET): $(VERSION_H) $(OBJ)
$(CC) $(CFLAGS) $(LDFLAGS) -o $(TARGET) $(OBJ)

clean:
$(RM) $(TARGET) $(OBJ) $(DEP)
$(RM) $(TARGET) $(OBJ) $(DEP) $(VERSION_H)

include Makefile.dev-tools

Expand Down
1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.1.1
10 changes: 10 additions & 0 deletions include/meson.build
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
public_includes = include_directories('.')

gen_version_h = custom_target(
'version.h',
output: 'version.h',
command: [
metang_version,
version,
'@OUTPUT@',
],
)
8 changes: 6 additions & 2 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ project(
license: 'Apache-2.0',
license_files: 'LICENSE',
meson_version: '>=1.5.0',
version: '0.1.0',
version: run_command(['./tools/version.sh', 'VERSION'], check: true).stdout().strip(),
default_options: {
'buildtype': 'release', # O3
'warning_level': '3', # all, extra, pedantic
Expand All @@ -28,6 +28,7 @@ project(

native = get_option('native')
install = native and meson.is_cross_build() ? false : true
version = files('VERSION')

# This must come before `tests`!
subdir('tools')
Expand All @@ -38,7 +39,10 @@ subdir('tests')

metang_exe = executable(
'metang',
c_sources,
sources: [
static_sources,
gen_version_h,
],
include_directories: public_includes,
install: install,
install_mode: 'rwxr-xr-x',
Expand Down
2 changes: 1 addition & 1 deletion src/meson.build
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
c_sources = files(
static_sources = files(
'deque.c',
'generate.c',
'metang.c',
Expand Down
3 changes: 2 additions & 1 deletion src/metang.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@
#include "deque.h"
#include "generate.h"
#include "strlib.h"
#include "version.h"

// clang-format off
static const char *version = "0.1.0";
static const char *version = METANG_VERSION;

static const char *tag_line = "metang - Generate multi-purpose C headers for enumerators";

Expand Down
1 change: 1 addition & 0 deletions tools/meson.build
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
metang_runtests = find_program('runtests.py', native: true)
metang_version = find_program('version.sh', native: true)
29 changes: 29 additions & 0 deletions tools/version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env bash

if [[ "$#" -eq 0 ]]; then
echo "Usage: ./version.sh VERSION_FILE [INCLUDE_DEST]"
echo ""
echo "If INCLUDE_DEST is not given, then the contents of VERSION_FILE"
echo "will be written to standard output."
exit 1
fi

VERSION_FILE=$1
INCLUDE_DEST=$2

VERSION=$(cat "${VERSION_FILE}")

if [[ -n "$INCLUDE_DEST" ]]; then
{
echo "/* THIS FILE IS GENERATED; DO NOT MODIFY!!! */"
echo
echo "#ifndef METANG_VERSION_H"
echo "#define METANG_VERSION_H"
echo
echo "#define METANG_VERSION \"${VERSION}\""
echo
echo "#endif /* METANG_VERSION_H */"
} >"${INCLUDE_DEST}"
else
echo "${VERSION}"
fi

0 comments on commit f8141f2

Please sign in to comment.