meson: generate version tag from git

$ build/systemctl --version
systemd 239-3555-g6178cbb5b5
+PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 +SECCOMP +BLKID +ELFUTILS +KMOD -IDN2 +IDN +PCRE2 default-hierarchy=hybrid
$ git tag v240 -m 'v240'
$ ninja -C build
ninja: Entering directory `build'
[76/76] Linking target fuzz-unit-file.
$ build/systemctl --version
systemd 240
+PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 +SECCOMP +BLKID +ELFUTILS +KMOD -IDN2 +IDN +PCRE2 default-hierarchy=hybrid

This is very useful during development, because a precise version string is
embedded in the build product and displayed during boot, so we don't have to
guess answers for questions like "did I just boot the latest version or the one
from before?".

This change creates an overhead for "noop" builds. On my laptop, 'ninja -C
build' that does nothing goes from 0.1 to 0.5 s. It would be nice to avoid
this, but I think that <1 s is still acceptable.

Fixes #7183.

PACKAGE_VERSION is renamed to GIT_VERSION, to make it obvious that this is the
more dynamically changing version string.

Why save to a file? It would be easy to generate the version tag using
run_command(), but we want to go through a file so that stuff gets rebuilt when
this file changes. If we just defined an variable in meson, ninja wouldn't know
it needs to rebuild things.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2018-12-20 20:35:25 +01:00
parent b9da6a098b
commit 681bd2c524
19 changed files with 51 additions and 22 deletions

View File

@ -21,7 +21,6 @@ libudev_version = '1.6.11'
# set. Ugh, ugh, ugh!
conf = configuration_data()
conf.set('PROJECT_VERSION', meson.project_version())
conf.set_quoted('PACKAGE_VERSION', meson.project_version())
substs = configuration_data()
substs.set('PROJECT_URL', 'https://www.freedesktop.org/wiki/Software/systemd')
@ -584,6 +583,10 @@ endif
#####################################################################
vcs_tagger = [meson.source_root() + '/tools/meson-vcs-tag.sh',
'@0@/.git'.format(meson.source_root()),
meson.project_version()]
sed = find_program('sed')
awk = find_program('awk')
m4 = find_program('m4')

View File

@ -14,6 +14,7 @@
#include "alloc-util.h"
#include "analyze-security.h"
#include "analyze-verify.h"
#include "build.h"
#include "bus-error.h"
#include "bus-unit-util.h"
#include "bus-util.h"
@ -696,7 +697,7 @@ static int analyze_plot(int argc, char *argv[], void *userdata) {
"<!-- that render these files properly but much slower are ImageMagick, -->\n"
"<!-- gimp, inkscape, etc. To display the files on your system, just -->\n"
"<!-- point your browser to this file. -->\n\n"
"<!-- This plot was generated by systemd-analyze version %-16.16s -->\n\n", PACKAGE_VERSION);
"<!-- This plot was generated by systemd-analyze version %-16.16s -->\n\n", GIT_VERSION);
/* style sheet */
svg("<defs>\n <style type=\"text/css\">\n <![CDATA[\n"

View File

@ -1,6 +1,8 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#pragma once
#include "version.h"
#if HAVE_PAM
#define _PAM_FEATURE_ "+PAM"
#else

View File

@ -1,5 +1,10 @@
# SPDX-License-Identifier: LGPL-2.1+
version_h = vcs_tag(
command: vcs_tagger,
input : 'version.h.in',
output : 'version.h')
basic_sources = files('''
MurmurHash2.c
MurmurHash2.h

View File

@ -557,7 +557,7 @@ uint64_t system_tasks_max_scale(uint64_t v, uint64_t max) {
}
int version(void) {
puts("systemd " PACKAGE_VERSION "\n"
puts("systemd " GIT_VERSION "\n"
SYSTEMD_FEATURES);
return 0;
}

1
src/basic/version.h.in Normal file
View File

@ -0,0 +1 @@
#define GIT_VERSION "@VCS_TAG@"

View File

@ -17,7 +17,7 @@
#endif
/* magic string to find in the binary image */
static const char __attribute__((used)) magic[] = "#### LoaderInfo: systemd-boot " PACKAGE_VERSION " ####";
static const char __attribute__((used)) magic[] = "#### LoaderInfo: systemd-boot " GIT_VERSION " ####";
static const EFI_GUID global_guid = EFI_GLOBAL_VARIABLE;
@ -361,7 +361,7 @@ static VOID print_status(Config *config, CHAR16 *loaded_image_path) {
uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, EFI_LIGHTGRAY|EFI_BACKGROUND_BLACK);
uefi_call_wrapper(ST->ConOut->ClearScreen, 1, ST->ConOut);
Print(L"systemd-boot version: " PACKAGE_VERSION "\n");
Print(L"systemd-boot version: " GIT_VERSION "\n");
Print(L"architecture: " EFI_MACHINE_TYPE_NAME "\n");
Print(L"loaded image: %s\n", loaded_image_path);
Print(L"UEFI specification: %d.%02d\n", ST->Hdr.Revision >> 16, ST->Hdr.Revision & 0xffff);
@ -804,7 +804,7 @@ static BOOLEAN menu_run(
break;
case KEYPRESS(0, 0, 'v'):
status = PoolPrint(L"systemd-boot " PACKAGE_VERSION " (" EFI_MACHINE_TYPE_NAME "), UEFI Specification %d.%02d, Vendor %s %d.%02d",
status = PoolPrint(L"systemd-boot " GIT_VERSION " (" EFI_MACHINE_TYPE_NAME "), UEFI Specification %d.%02d, Vendor %s %d.%02d",
ST->Hdr.Revision >> 16, ST->Hdr.Revision & 0xffff,
ST->FirmwareVendor, ST->FirmwareRevision >> 16, ST->FirmwareRevision & 0xffff);
break;
@ -2097,7 +2097,7 @@ EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *sys_table) {
InitializeLib(image, sys_table);
init_usec = time_usec();
efivar_set_time_usec(L"LoaderTimeInitUSec", init_usec);
efivar_set(L"LoaderInfo", L"systemd-boot " PACKAGE_VERSION, FALSE);
efivar_set(L"LoaderInfo", L"systemd-boot " GIT_VERSION, FALSE);
infostr = PoolPrint(L"%s %d.%02d", ST->FirmwareVendor, ST->FirmwareRevision >> 16, ST->FirmwareRevision & 0xffff);
efivar_set(L"LoaderFirmwareInfo", infostr, FALSE);

View File

@ -78,7 +78,6 @@ endif
if have_gnu_efi
efi_conf = configuration_data()
efi_conf.set_quoted('PACKAGE_VERSION', meson.project_version())
efi_conf.set_quoted('EFI_MACHINE_TYPE_NAME', EFI_MACHINE_TYPE_NAME)
efi_conf.set10('ENABLE_TPM', get_option('tpm'))
efi_conf.set('SD_TPM_PCR', get_option('tpm-pcrindex'))
@ -117,7 +116,8 @@ if have_gnu_efi
'-Wno-missing-field-initializers',
'-isystem', efi_incdir,
'-isystem', join_paths(efi_incdir, gnu_efi_path_arch),
'-include', efi_config_h]
'-include', efi_config_h,
'-include', version_h]
if efi_arch == 'x86_64'
compile_args += ['-mno-red-zone',
'-mno-sse',

View File

@ -12,7 +12,7 @@
#include "util.h"
/* magic string to find in the binary image */
static const char __attribute__((used)) magic[] = "#### LoaderInfo: systemd-stub " PACKAGE_VERSION " ####";
static const char __attribute__((used)) magic[] = "#### LoaderInfo: systemd-stub " GIT_VERSION " ####";
static const EFI_GUID global_guid = EFI_GLOBAL_VARIABLE;
@ -117,7 +117,7 @@ EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *sys_table) {
/* add StubInfo */
if (efivar_get_raw(&global_guid, L"StubInfo", &b, &size) != EFI_SUCCESS)
efivar_set(L"StubInfo", L"systemd-stub " PACKAGE_VERSION, FALSE);
efivar_set(L"StubInfo", L"systemd-stub " GIT_VERSION, FALSE);
if (szs[3] > 0)
graphics_splash((UINT8 *)((UINTN)loaded_image->ImageBase + addrs[3]), szs[3], NULL);

View File

@ -43,7 +43,7 @@ static UnitFileFlags unit_file_bools_to_flags(bool runtime, bool force) {
(force ? UNIT_FILE_FORCE : 0);
}
static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_version, "s", PACKAGE_VERSION);
static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_version, "s", GIT_VERSION);
static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_features, "s", SYSTEMD_FEATURES);
static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_architecture, "s", architecture_to_string(uname_architecture()));
static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_log_target, "s", log_target_to_string(log_get_target()));

View File

@ -1914,7 +1914,7 @@ static void log_execution_mode(bool *ret_first_boot) {
if (arg_system) {
int v;
log_info("systemd " PACKAGE_VERSION " running in %ssystem mode. (" SYSTEMD_FEATURES ")",
log_info("systemd " GIT_VERSION " running in %ssystem mode. (" SYSTEMD_FEATURES ")",
arg_action == ACTION_TEST ? "test " : "" );
v = detect_virtualization();
@ -1942,7 +1942,7 @@ static void log_execution_mode(bool *ret_first_boot) {
_cleanup_free_ char *t;
t = uid_to_name(getuid());
log_debug("systemd " PACKAGE_VERSION " running in %suser mode for user " UID_FMT "/%s. (" SYSTEMD_FEATURES ")",
log_debug("systemd " GIT_VERSION " running in %suser mode for user " UID_FMT "/%s. (" SYSTEMD_FEATURES ")",
arg_action == ACTION_TEST ? " test" : "", getuid(), strna(t));
}

View File

@ -1,6 +1,7 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#include "alloc-util.h"
#include "build.h"
#include "curl-util.h"
#include "fd-util.h"
#include "locale-util.h"
@ -283,7 +284,7 @@ int curl_glue_make(CURL **ret, const char *url, void *userdata) {
if (curl_easy_setopt(c, CURLOPT_PRIVATE, userdata) != CURLE_OK)
return -EIO;
useragent = strjoina(program_invocation_short_name, "/" PACKAGE_VERSION);
useragent = strjoina(program_invocation_short_name, "/" GIT_VERSION);
if (curl_easy_setopt(c, CURLOPT_USERAGENT, useragent) != CURLE_OK)
return -EIO;

View File

@ -9,6 +9,7 @@
#include "sd-daemon.h"
#include "alloc-util.h"
#include "build.h"
#include "conf-parser.h"
#include "daemon-util.h"
#include "def.h"
@ -236,7 +237,7 @@ int start_upload(Uploader *u,
easy_setopt(curl, CURLOPT_VERBOSE, 1L, LOG_WARNING, );
easy_setopt(curl, CURLOPT_USERAGENT,
"systemd-journal-upload " PACKAGE_VERSION,
"systemd-journal-upload " GIT_VERSION,
LOG_WARNING, );
if (arg_key || startswith(u->url, "https://")) {

View File

@ -7,6 +7,7 @@
#include <unistd.h>
#include "alloc-util.h"
#include "build.h"
#include "fd-util.h"
#include "libudev-list-internal.h"
#include "libudev-util.h"
@ -507,7 +508,7 @@ int main(int argc, char *argv[]) {
return EXIT_SUCCESS;
case 'V':
printf("%s\n", PACKAGE_VERSION);
printf("%s\n", GIT_VERSION);
return EXIT_SUCCESS;
case 'm':

View File

@ -11,6 +11,7 @@
#include <sys/signalfd.h>
#include <unistd.h>
#include "build.h"
#include "device-private.h"
#include "fs-util.h"
#include "log.h"
@ -81,7 +82,7 @@ static int run(int argc, char *argv[]) {
return 0;
}
log_debug("version %s", PACKAGE_VERSION);
log_debug("version %s", GIT_VERSION);
mac_selinux_init();
action = argv[1];

View File

@ -18,6 +18,7 @@
#include <unistd.h>
#include "alloc-util.h"
#include "build.h"
#include "fd-util.h"
#include "libudev-util.h"
#include "scsi_id.h"
@ -370,7 +371,7 @@ static int set_options(int argc, char **argv,
break;
case 'V':
printf("%s\n", PACKAGE_VERSION);
printf("%s\n", GIT_VERSION);
exit(EXIT_SUCCESS);
case 'x':

View File

@ -3,6 +3,8 @@
#include <stdio.h>
#include "build.h"
int info_main(int argc, char *argv[], void *userdata);
int trigger_main(int argc, char *argv[], void *userdata);
int settle_main(int argc, char *argv[], void *userdata);
@ -13,6 +15,6 @@ int test_main(int argc, char *argv[], void *userdata);
int builtin_main(int argc, char *argv[], void *userdata);
static inline int print_version(void) {
puts(PACKAGE_VERSION);
puts(GIT_VERSION);
return 0;
}

View File

@ -31,6 +31,7 @@
#include "sd-event.h"
#include "alloc-util.h"
#include "build.h"
#include "cgroup-util.h"
#include "cpu-set-util.h"
#include "dev-setup.h"
@ -1579,7 +1580,7 @@ static int parse_argv(int argc, char *argv[]) {
case 'h':
return help();
case 'V':
printf("%s\n", PACKAGE_VERSION);
printf("%s\n", GIT_VERSION);
return 0;
case '?':
return -EINVAL;
@ -1834,7 +1835,7 @@ static int run(int argc, char *argv[]) {
if (arg_daemonize) {
pid_t pid;
log_info("starting version " PACKAGE_VERSION);
log_info("starting version " GIT_VERSION);
/* connect /dev/null to stdin, stdout, stderr */
if (log_get_max_level() < LOG_DEBUG) {

9
tools/meson-vcs-tag.sh Executable file
View File

@ -0,0 +1,9 @@
#!/bin/sh
set -eu
set -o pipefail
gitdir="$1"
fallback="$2"
git --git-dir="$gitdir" describe --abbrev=7 --dirty=+ 2>/dev/null | sed 's/^v//' || echo "$fallback"