From a9edea65ca8abe9155c95053930f6af76efff80f Mon Sep 17 00:00:00 2001 From: Peter Hutterer Date: Wed, 7 Jun 2023 14:17:45 +1000 Subject: [PATCH] ci: add a check that our event values cannot diverge See commit 168de89a "eis: sync event codes with libei", this script should've prevented that issue. --- .gitlab-ci.yml | 11 ++++++ .gitlab-ci/check-event-values.py | 65 ++++++++++++++++++++++++++++++++ .gitlab-ci/ci.template | 11 ++++++ 3 files changed, 87 insertions(+) create mode 100755 .gitlab-ci/check-event-values.py diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 08e9550..16fc54a 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -290,6 +290,17 @@ abicheck@fedora:38: only: - merge_requests +event-type-check@fedora:38: + extends: + - .fedora-build@template + script: + - .gitlab-ci/meson-build.sh --skip-test + - .gitlab-ci/check-event-values.py + variables: + PKG_CONFIG_PATH: $MESON_BUILDDIR/meson-uninstalled + only: + - merge_requests + minimum-meson@fedora:38: extends: - .fedora-build@template diff --git a/.gitlab-ci/check-event-values.py b/.gitlab-ci/check-event-values.py new file mode 100755 index 0000000..7bfaea9 --- /dev/null +++ b/.gitlab-ci/check-event-values.py @@ -0,0 +1,65 @@ +#!/usr/bin/env python3 +# +# Check that the EI_EVENT_FOO and EIS_EVENT_FOO enum values match (for those events that +# exist in both libraries). + +from typing import Iterator +from pathlib import Path +import re +import subprocess +import tempfile + +template = """ +#include +#include + +@@@ + + int main(void) { + return 0; +} +""" +assert_template = '_Static_assert(EIS_EVENT_{event} == EI_EVENT_{event}, "Mismatching event types for {event}");' + + +def extract(header: Path, prefix: str) -> Iterator[str]: + with open(header) as fd: + for line in fd: + match = re.match(rf"^\t{prefix}(\w+)", line) + if match: + yield match[1] + + +if __name__ == "__main__": + ei_events = extract(Path("src/libei.h"), prefix="EI_EVENT_") + eis_events = extract(Path("src/libeis.h"), prefix="EIS_EVENT_") + + common = set(ei_events) & set(eis_events) + + print("Shared events that need identical values:") + for e in common: + print(f" EI_EVENT_{e}") + + asserts = (assert_template.format(event=e) for e in common) + + with tempfile.NamedTemporaryFile(suffix=".c", delete=False) as fd: + fd.write(template.replace("@@@", "\n".join(asserts)).encode("utf-8")) + fd.flush() + pkgconfig = subprocess.run( + ["pkg-config", "--cflags", "--libs", "libei-1.0", "libeis-1.0"], + check=True, + capture_output=True, + ) + pkgconfig_args = pkgconfig.stdout.decode("utf-8").strip().split(" ") + try: + subprocess.run( + ["gcc", "-o", "event-type-check", *pkgconfig_args, fd.name], + check=True, + capture_output=True, + ) + print("Success. Event types are identical") + except subprocess.CalledProcessError as e: + print("Mismatching event types") + print(e.stdout.decode("utf-8")) + print(e.stderr.decode("utf-8")) + raise SystemExit(1) diff --git a/.gitlab-ci/ci.template b/.gitlab-ci/ci.template index 216696c..0d7c2ec 100644 --- a/.gitlab-ci/ci.template +++ b/.gitlab-ci/ci.template @@ -290,6 +290,17 @@ abicheck@{{distro.name}}:{{version}}: only: - merge_requests +event-type-check@{{distro.name}}:{{version}}: + extends: + - .{{distro.name}}-build@template + script: + - .gitlab-ci/meson-build.sh --skip-test + - .gitlab-ci/check-event-values.py + variables: + PKG_CONFIG_PATH: $MESON_BUILDDIR/meson-uninstalled + only: + - merge_requests + minimum-meson@{{distro.name}}:{{version}}: extends: - .{{distro.name}}-build@template