meson: fix version generation from git

run_command() was only run at configure time, meaning the generated
version (that was passed on to the sources via -DYAMBAR_VERSION)
became stale.

Fix by implementing a shell script that generates a header file, and
wrap this in a custom target that is run every time (but the generated
file is only updated when the version changes)
This commit is contained in:
Daniel Eklöf 2019-10-19 21:47:21 +02:00
parent e9d5c620a4
commit c4f9168191
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F
4 changed files with 44 additions and 24 deletions

View file

@ -1,3 +1,5 @@
sh = find_program('sh', native: true)
scdoc = dependency('scdoc', native: true) scdoc = dependency('scdoc', native: true)
scdoc_prog = find_program(scdoc.get_pkgconfig_variable('scdoc'), native: true) scdoc_prog = find_program(scdoc.get_pkgconfig_variable('scdoc'), native: true)

32
generate-version.sh Executable file
View file

@ -0,0 +1,32 @@
#!/usr/bin/bash
set -e
default_version=${1}
src_dir=${2}
out_file=${3}
# echo "default version: ${default_version}"
# echo "source directory: ${src_dir}"
# echo "output file: ${out_file}"
if [[ $(command -v git) ]]; then
new_version="$(git describe --always --tags) ($(env LC_TIME=C date "+%b %d %Y"), branch '$(git rev-parse --abbrev-ref HEAD)')"
else
new_version="${default_version}"
fi
new_version="#define YAMBAR_VERSION \"${new_version}\""
if [[ -f "${out_file}" ]]; then
old_version=$(<"${out_file}")
else
old_version=""
fi
# echo "old version: ${old_version}"
# echo "new version: ${new_version}"
if [[ "${old_version}" != "${new_version}" ]]; then
echo "${new_version}" > "${out_file}"
fi

1
main.c
View file

@ -22,6 +22,7 @@
#define LOG_MODULE "main" #define LOG_MODULE "main"
#include "log.h" #include "log.h"
#include "version.h"
static volatile sig_atomic_t aborted = 0; static volatile sig_atomic_t aborted = 0;

View file

@ -10,28 +10,6 @@ project('yambar', 'c',
is_debug_build = get_option('buildtype').startswith('debug') is_debug_build = get_option('buildtype').startswith('debug')
plugs_as_libs = get_option('core-plugins-as-shared-libraries') plugs_as_libs = get_option('core-plugins-as-shared-libraries')
version = '"@0@"'.format(meson.project_version())
sh = find_program('sh', native: true)
git = find_program('git', required: false, native: true)
if git.found()
commit_hash = run_command(
[sh.path(), '-c',
'@0@ --git-dir="$MESON_SOURCE_ROOT/.git" describe --always --tags'.format(
git.path())])
branch = run_command(
[sh.path(), '-c',
'@0@ "--git-dir=$MESON_SOURCE_ROOT/.git" rev-parse --abbrev-ref HEAD'.format(
git.path())])
if commit_hash.returncode() == 0 and branch.returncode() == 0
version = '"@0@ (" __DATE__ ", branch \'@1@\')"'.format(
commit_hash.stdout().strip(), branch.stdout().strip())
endif
endif
# Common dependencies # Common dependencies
cc = meson.get_compiler('c') cc = meson.get_compiler('c')
dl = cc.find_library('dl') dl = cc.find_library('dl')
@ -59,8 +37,7 @@ wlroots = dependency('wlroots', required: get_option('backend-wayland'))
backend_wayland = wayland_client.found() and wayland_cursor.found() and wlroots.found() backend_wayland = wayland_client.found() and wayland_cursor.found() and wlroots.found()
add_project_arguments( add_project_arguments(
['-D_GNU_SOURCE', ['-D_GNU_SOURCE'] +
'-DYAMBAR_VERSION=@0@'.format(version)] +
(is_debug_build ? ['-D_DEBUG'] : []) + (is_debug_build ? ['-D_DEBUG'] : []) +
(backend_x11 ? ['-DENABLE_X11'] : []) + (backend_x11 ? ['-DENABLE_X11'] : []) +
(backend_wayland ? ['-DENABLE_WAYLAND'] : []) + (backend_wayland ? ['-DENABLE_WAYLAND'] : []) +
@ -87,6 +64,13 @@ subdir('decorations')
subdir('particles') subdir('particles')
subdir('modules') subdir('modules')
generate_version_sh = files('generate-version.sh')
version = custom_target(
'generate_version',
build_always_stale: true,
output: 'version.h',
command: [generate_version_sh, meson.project_version(), '@SOURCE_DIR@', '@OUTPUT@'])
yambar = executable( yambar = executable(
'yambar', 'yambar',
'color.h', 'color.h',
@ -102,6 +86,7 @@ yambar = executable(
'tag.c', 'tag.h', 'tag.c', 'tag.h',
'tllist.h', 'tllist.h',
'yml.c', 'yml.h', 'yml.c', 'yml.h',
version,
dependencies: [bar, freetype, fontconfig, pixman, yaml, threads, dl] + dependencies: [bar, freetype, fontconfig, pixman, yaml, threads, dl] +
decorations + particles + modules, decorations + particles + modules,
build_rpath: '$ORIGIN/modules:$ORIGIN/decorations:$ORIGIN/particles', build_rpath: '$ORIGIN/modules:$ORIGIN/decorations:$ORIGIN/particles',