aboutsummaryrefslogtreecommitdiff
path: root/setup.py
blob: a7b06e911e9375cbf45052cf141a5f4f11a48efd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#
# SPDX-FileCopyrightText: 2016 - 2026 Dimitri Staessens
# SPDX-License-Identifier: LGPL-2.1-only
#

"""
Build script for the PyOuroboros CFFI extensions.

Resolves the ouroboros library version via ``pkg-config`` and the
pyouroboros source version via ``setuptools_scm``, verifies that the
``major.minor`` halves match, and delegates the actual build to the CFFI
builders under ``ffi/``.
"""

from __future__ import annotations

import subprocess
import sys

from setuptools import setup
from setuptools_scm import get_version


def _get_ouroboros_version() -> str:
    try:
        out = subprocess.check_output(
            ['pkg-config', '--modversion', 'ouroboros-dev'],
            stderr=subprocess.DEVNULL,
        )
        return out.decode().strip()
    except (subprocess.CalledProcessError, FileNotFoundError):
        sys.exit("ERROR: ouroboros-dev not found via pkg-config. "
                 "Is Ouroboros installed?")


def _check_build_version_compat() -> None:
    """
    Exit when the ouroboros and pyouroboros ``major.minor`` disagree.

    Returns without checking when the tree carries no SCM information.
    """

    try:
        pyo7s_ver = get_version(root='.', relative_to=__file__)
    except (LookupError, OSError):
        return

    o7s_ver = _get_ouroboros_version()

    o7s_parts   = o7s_ver.split('.')
    pyo7s_parts = pyo7s_ver.split('.')
    if o7s_parts[0] != pyo7s_parts[0] or o7s_parts[1] != pyo7s_parts[1]:
        sys.exit(
            f"ERROR: Version mismatch: ouroboros {o7s_ver} "
            f"vs pyouroboros {pyo7s_ver} "
            f"(major.minor must match)"
        )


_check_build_version_compat()

setup(
    cffi_modules=[
        "ffi/pyouroboros_build_dev.py:ffibuilder",
        "ffi/pyouroboros_build_irm.py:ffibuilder",
    ],
)