Debian Codegraph @

contrib/ci/check-abi

raw
1 #!/usr/bin/python3
2
3
4 import argparse
5 import contextlib
6 import os
7 import shutil
8 import subprocess
9 import sys
10
11
12 def format_title(title):
13 box = {
14 "tl": "╔",
15 "tr": "╗",
16 "bl": "╚",
17 "br": "╝",
18 "h": "═",
19 "v": "║",
20 }
21 hline = box["h"] * (len(title) + 2)
22
23 return "\n".join(
24 [
25 f"{box['tl']}{hline}{box['tr']}",
26 f"{box['v']} {title} {box['v']}",
27 f"{box['bl']}{hline}{box['br']}",
28 ]
29 )
30
31
32 def rm_rf(path):
33 try:
34 shutil.rmtree(path)
35 except FileNotFoundError:
36 pass
37
38
39 def sanitize_path(name):
40 return name.replace("/", "-")
41
42
43 def get_current_revision():
44 revision = subprocess.check_output(
45 ["git", "rev-parse", "--abbrev-ref", "HEAD"], encoding="utf-8"
46 ).strip()
47
48 if revision == "HEAD":
49 # This is a detached HEAD, get the commit hash
50 revision = (
51 subprocess.check_output(["git", "rev-parse", "HEAD"])
52 .strip()
53 .decode("utf-8")
54 )
55
56 return revision
57
58
59 @contextlib.contextmanager
60 def checkout_git_revision(revision):
61 current_revision = get_current_revision()
62 subprocess.check_call(["git", "checkout", "-q", revision])
63
64 try:
65 yield
66 finally:
67 subprocess.check_call(["git", "checkout", "-q", current_revision])
68
69
70 def build_install(revision):
71 build_dir = "_build"
72 dest_dir = os.path.abspath(sanitize_path(revision))
73 print(
74 format_title(f"# Building and installing {revision} in {dest_dir}"),
75 end="\n\n",
76 flush=True,
77 )
78
79 with checkout_git_revision(revision):
80 rm_rf(build_dir)
81 rm_rf(revision)
82
83 subprocess.check_call(
84 [
85 "meson",
86 build_dir,
87 "--prefix=/usr",
88 "--libdir=lib",
89 "-Db_coverage=false",
90 "-Dgtkdoc=false",
91 "-Dtests=false",
92 ]
93 )
94 subprocess.check_call(["ninja", "-v", "-C", build_dir])
95 subprocess.check_call(
96 ["ninja", "-v", "-C", build_dir, "install"], env={"DESTDIR": dest_dir}
97 )
98
99 return dest_dir
100
101
102 def compare(old_tree, new_tree):
103 print(format_title(f"# Comparing the two ABIs"), end="\n\n", flush=True)
104
105 old_headers = os.path.join(old_tree, "usr", "include")
106 old_lib = os.path.join(old_tree, "usr", "lib", "libxmlb.so")
107
108 new_headers = os.path.join(new_tree, "usr", "include")
109 new_lib = os.path.join(new_tree, "usr", "lib", "libxmlb.so")
110
111 subprocess.check_call(
112 [
113 "abidiff",
114 "--headers-dir1",
115 old_headers,
116 "--headers-dir2",
117 new_headers,
118 "--drop-private-types",
119 "--suppressions",
120 "contrib/ci/abidiff.suppr",
121 "--fail-no-debug-info",
122 "--no-added-syms",
123 old_lib,
124 new_lib,
125 ]
126 )
127
128
129 if __name__ == "__main__":
130 parser = argparse.ArgumentParser()
131
132 parser.add_argument("old", help="the previous revision, considered the reference")
133 parser.add_argument("new", help="the new revision, to compare to the reference")
134
135 args = parser.parse_args()
136
137 if args.old == args.new:
138 print("Let's not waste time comparing something to itself")
139 sys.exit(0)
140
141 old_tree = build_install(args.old)
142 new_tree = build_install(args.new)
143
144 try:
145 compare(old_tree, new_tree)
146
147 except Exception:
148 sys.exit(1)
149
150 print(f"Hurray! {args.old} and {args.new} are ABI-compatible!")