diff --git a/.gitignore b/.gitignore index a3a45188..59e5bd42 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,6 @@ venv/ *.egg-info/ *.swp build/ +dist/ .local/ +version.py diff --git a/meson.build b/meson.build index 3f4f9c4f..b37af254 100644 --- a/meson.build +++ b/meson.build @@ -6,3 +6,21 @@ py = import('python').find_installation(pure: false) libzz = dependency('zz', version: '>= 0.7.0') py.extension_module('gmp', ['fmt.c', 'gmp.c', 'utils.c'], install: true, dependencies: libzz) +install_dir = py.get_install_dir() + +# Generate version.py for sdist +meson.add_dist_script(['scripts/gitversion.py', '--meson-dist', + '--write', '_version.py']) +fs = import('fs') +if not fs.exists('_version.py') + generate_version = custom_target('generate-version', + install: true, + build_always_stale: true, + build_by_default: true, + output: '_version.py', + input: 'scripts/gitversion.py', + command: [py, '@INPUT@', '--write', + '@OUTPUT@'], + install_dir: install_dir, + install_tag: 'python-runtime') +endif diff --git a/scripts/gitversion.py b/scripts/gitversion.py old mode 100644 new mode 100755 index edafffa5..6dac5ce3 --- a/scripts/gitversion.py +++ b/scripts/gitversion.py @@ -1,39 +1,69 @@ #!/usr/bin/env python3 + +import argparse import os import re import subprocess +import sys def git_version(): - # Append last commit date and hash to dev version information, - # if available - return version, git_hash - + p = subprocess.Popen(["git", "describe"], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + cwd=os.path.dirname(__file__)) + out, err = p.communicate() + if p.returncode: + raise RuntimeError("Non-zero return code from git-describe: " + f"{p.returncode}") + out = out.decode("ascii").removesuffix("\n") -if __name__ == "__main__": - try: - p = subprocess.Popen(["git", "describe"], - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - cwd=os.path.dirname(__file__)) - except FileNotFoundError: - exit(1) + version, *other = out.removesuffix("\n").split("-") + if other: + g, h = other + m = re.match("(.*)([0-9]+)", version) + version = m[1] + str(int(m[2])+1) + ".dev" + g + git_hash = h else: - out, err = p.communicate() - if p.returncode: - exit(p.returncode) - out = out.decode("ascii").removesuffix("\n") - - version, *other = out.removesuffix("\n").split("-") - if other: - g, h = other - m = re.match("(.*)([0-9]+)", version) - version = m[1] + str(int(m[2])+1) + ".dev" + g - git_hash = h - else: - git_hash = "" + git_hash = "" if git_hash: version += "+" + git_hash - print(version) - exit(0) + return version + + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument("--write", help="Save version to this file") + parser.add_argument( + "--meson-dist", + help="Output path is relative to MESON_DIST_ROOT", + action="store_true" + ) + args = parser.parse_args() + + try: + version = git_version() + except (FileNotFoundError, RuntimeError): + sys.path.insert(0, os.getcwd()) + from _version import version + + if args.write: + template = f'version = "{version}"' + outfile = args.write + if args.meson_dist: + outfile = os.path.join( + os.environ.get("MESON_DIST_ROOT", ""), + outfile + ) + + # Print human readable output path + relpath = os.path.relpath(outfile) + if relpath.startswith("."): + relpath = outfile + + with open(outfile, "w") as f: + print(f"Saving version to {relpath}") + f.write(template) + else: + print(version) diff --git a/tests/conftest.py b/tests/conftest.py index 838918b9..f59aa7cd 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -23,6 +23,8 @@ def pytest_report_header(config): Bits per digit : {gmp.mpz_info.bits_per_digit} sizeof(zz_digit_t): {gmp.mpz_info.sizeof_digit} Maximal bit count : {gmp.mpz_info.bitcnt_max} + + The gmp module v{gmp.__version__} """)