build: remove -Dversion_fallback and introduce .VERSION file

The version script will now first try to read the version from a
.VERSION file in the source root. If that file does not exist, it will
query git. The .VERSION file is not included in the git repository, it
is generated for inclusion in source tarballs.

Package maintainers who had any use for -Dversion_fallback may want to
create the .VERSION file themselves.

Additionally, some git-specific files have been removed from source
tarballs.
This commit is contained in:
Andrei Alexeyev 2022-12-12 04:33:01 +01:00
parent d32332a5e3
commit 0c47f6d14b
No known key found for this signature in database
GPG key ID: 72D26128040B9690
6 changed files with 68 additions and 15 deletions

1
.gitignore vendored
View file

@ -14,3 +14,4 @@ __pycache__
*~
*-autosave.kra
*.swp
.VERSION

View file

@ -4,10 +4,6 @@ project('taisei', 'c',
version : run_command([
files('scripts/version.py'),
'--rootdir', meson.project_source_root(),
'--fallback-version', (
get_option('version_fallback').strip() != ''
? get_option('version_fallback').strip()
: 'v1.4-dev')
], check : true).stdout().strip(),
meson_version : '>=0.56.2',
default_options : [

View file

@ -1,8 +1,3 @@
option(
'version_fallback',
type : 'string',
description : 'Overrides the version string when not building in a git repository'
)
option(
'developer',

View file

@ -1,10 +1,6 @@
ver_fb = get_option('version_fallback').strip()
version_fallback = ver_fb != '' ? ver_fb : meson.project_version()
common_taiseilib_args = [
'--rootdir', meson.project_source_root(),
'--fallback-version', version_fallback
]
common_taiseilib_defs = [
@ -137,3 +133,6 @@ fix_path_command = [fix_path_script]
format_array_script = find_program(files('format-array.py'))
format_array_command = [format_array_script]
on_dist_script = find_program(files('on-meson-dist.py'))
meson.add_dist_script(on_dist_script)

46
scripts/on-meson-dist.py Executable file
View file

@ -0,0 +1,46 @@
#!/usr/bin/env python3
import os
import sys
import pathlib
import taiseilib.version
import shutil
def main(args):
def env_dir_path(v):
try:
s = os.environ[v]
except KeyError:
print("{} is not set".format(v))
p = pathlib.Path(s)
assert p.is_dir()
return p
src_root = env_dir_path('MESON_PROJECT_SOURCE_ROOT')
dist_root = env_dir_path('MESON_PROJECT_DIST_ROOT')
src_version = taiseilib.version.get(rootdir=src_root)
(dist_root / taiseilib.version.OVERRIDE_FILE_NAME).write_text(src_version.string)
remove_files = [
'.dockerignore',
'.gitattributes',
'.gitignore',
'.gitmodules',
'.mailmap',
'checkout',
'pull',
]
for fname in remove_files:
(dist_root / fname).unlink(fname)
shutil.rmtree(str(dist_root / '.github'))
if __name__ == '__main__':
from taiseilib.common import run_main
run_main(main)

View file

@ -5,12 +5,17 @@ import sys
import subprocess
import shlex
import re
import os
class VersionFormatError(common.TaiseiError):
pass
VERSION_FALLBACK = 'v1.4-dev'
OVERRIDE_FILE_NAME = '.VERSION'
class Version(object):
regex = re.compile(r'^v?(\d+)(?:\.(\d+)(?:\.(\d+))?)?(?:[-+](\d+))?(?:[-+](.*))?$')
@ -53,13 +58,23 @@ class Version(object):
return template.format(**self.__dict__)
def get(*, rootdir=None, fallback=None, args=common.default_args):
def get(*, rootdir=None, fallback=VERSION_FALLBACK, args=common.default_args):
rootdir = rootdir if rootdir is not None else args.rootdir
fallback = fallback if fallback is not None else args.fallback_version
if rootdir is None:
import pathlib
rootdir = pathlib.Path(__file__).parent
elif not isinstance(rootdir, os.PathLike):
rootdir = pathlib.Path(rootdir)
version_override_path = rootdir / OVERRIDE_FILE_NAME
try:
version_str = version_override_path.read_text().strip()
except FileNotFoundError:
pass
else:
return Version(version_str)
try:
version_str = subprocess.check_output(
@ -80,6 +95,7 @@ def get(*, rootdir=None, fallback=None, args=common.default_args):
print(e, file=sys.stderr)
print("Warning: git not found or not a git repository; using fallback version {0}".format(fallback), file=sys.stderr)
print("Hint: if you are packaging Taisei, write the appropriate version into", str(version_override_path), file=sys.stderr)
version_str = fallback
return Version(version_str)