meson: use Python script for version detection
authorKirill Isakov <bootctl@gmail.com>
Sun, 27 Mar 2022 19:55:23 +0000 (01:55 +0600)
committerKirill Isakov <bootctl@gmail.com>
Mon, 28 Mar 2022 15:38:29 +0000 (21:38 +0600)
meson.build
src/include/meson.build
version.py [new file with mode: 0755]

index f0c2fa1..1458015 100644 (file)
@@ -1,5 +1,5 @@
 project('tinc', 'c',
-  version: '1.18pre',
+  version: run_command([find_program('python3'), 'version.py', 'short'], check: true).stdout(),
   license: 'GPL-2.0-or-later',
   meson_version: '>=0.51',
   default_options: [
@@ -35,6 +35,13 @@ os_name = host_machine.system()
 cpu_family = host_machine.cpu_family()
 cc_name = cc.get_id()
 
+python = find_program('python3')
+if meson_version.version_compare('>=0.55')
+  python_path = python.full_path()
+else
+  python_path = python.path()
+endif
+
 cc_defs = ['-D_GNU_SOURCE']
 if os_name == 'sunos'
   cc_defs += '-D__EXTENSIONS__'
index 0550996..f8e6f0d 100644 (file)
@@ -1,7 +1,7 @@
 configure_file(output: 'config.h', configuration: cdata)
 
 src_lib_common += vcs_tag(
-  command: ['git', 'describe', '--always', '--tags', '--match=release-*'],
+  command: [python_path, src_root / 'version.py'],
   fallback: 'unknown',
   input: '../version_git.h.in',
   output: 'version_git.h',
diff --git a/version.py b/version.py
new file mode 100755 (executable)
index 0000000..ff491bb
--- /dev/null
@@ -0,0 +1,26 @@
+#!/usr/bin/env python3
+
+from sys import argv, exit
+import subprocess as subp
+
+prefix = "release-"
+
+cmd = [
+    "git",
+    "describe",
+    "--always",
+    "--tags",
+    "--match=" + prefix + "*",
+]
+
+if "short" in argv:
+    cmd.append("--abbrev=0")
+
+result = subp.run(cmd, stdout=subp.PIPE, encoding="utf-8")
+version = result.stdout
+
+if not result.returncode and version and version.startswith(prefix):
+    version = version[len(prefix):].strip()
+
+print(version if version else "unknown", end="")
+exit(not version)