Debian Codegraph @

.gitlab-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': '╔', 'tr': '╗', 'bl': '╚', 'br': '╝', 'h': '═', 'v': '║',
15 }
16 hline = box['h'] * (len(title) + 2)
17
18 return '\n'.join([
19 f"{box['tl']}{hline}{box['tr']}",
20 f"{box['v']} {title} {box['v']}",
21 f"{box['bl']}{hline}{box['br']}",
22 ])
23
24
25 def rm_rf(path):
26 try:
27 shutil.rmtree(path)
28 except FileNotFoundError:
29 pass
30
31
32 def sanitize_path(name):
33 return name.replace('/', '-')
34
35
36 def get_current_revision():
37 revision = subprocess.check_output(['git', 'rev-parse', '--abbrev-ref', 'HEAD'],
38 encoding='utf-8').strip()
39
40 if revision == 'HEAD':
41 # This is a detached HEAD, get the commit hash
42 revision = subprocess.check_output(['git', 'rev-parse', 'HEAD']).strip().decode('utf-8')
43
44 return revision
45
46
47 @contextlib.contextmanager
48 def checkout_git_revision(revision):
49 current_revision = get_current_revision()
50 subprocess.check_call(['git', 'checkout', '-q', revision])
51
52 try:
53 yield
54 finally:
55 subprocess.check_call(['git', 'checkout', '-q', current_revision])
56
57
58 def build_install(revision):
59 build_dir = '_build'
60 dest_dir = os.path.abspath(sanitize_path(revision))
61 print(format_title(f'# Building and installing {revision} in {dest_dir}'),
62 end='\n\n', flush=True)
63
64 with checkout_git_revision(revision):
65 rm_rf(build_dir)
66 rm_rf(revision)
67
68 subprocess.check_call(['meson', build_dir,
69 '--prefix=/usr', '--libdir=lib',
70 '-Db_coverage=false', '-Dgtk_doc=false', '-Dtests=false'])
71 subprocess.check_call(['ninja', '-v', '-C', build_dir])
72 subprocess.check_call(['ninja', '-v', '-C', build_dir, 'install'],
73 env={'DESTDIR': dest_dir})
74
75 return dest_dir
76
77
78 def compare(old_tree, new_tree):
79 print(format_title(f'# Comparing the two ABIs'), end='\n\n', flush=True)
80
81 old_headers = os.path.join(old_tree, 'usr', 'include')
82 old_lib = os.path.join(old_tree, 'usr', 'lib', 'libhandy-1.so')
83
84 new_headers = os.path.join(new_tree, 'usr', 'include')
85 new_lib = os.path.join(new_tree, 'usr', 'lib', 'libhandy-1.so')
86
87 subprocess.check_call([
88 'abidiff', '--headers-dir1', old_headers, '--headers-dir2', new_headers,
89 '--drop-private-types', '--fail-no-debug-info', '--no-added-syms', old_lib, new_lib])
90
91 if __name__ == '__main__':
92 parser = argparse.ArgumentParser()
93
94 parser.add_argument('old', help='the previous revision, considered the reference')
95 parser.add_argument('new', help='the new revision, to compare to the reference')
96
97 args = parser.parse_args()
98
99 if args.old == args.new:
100 print("Let's not waste time comparing something to itself")
101 sys.exit(0)
102
103 old_tree = build_install(args.old)
104 new_tree = build_install(args.new)
105
106 try:
107 compare(old_tree, new_tree)
108
109 except Exception as e:
110 print ('ABI comparison failed: '+ str(e))
111 sys.exit(1)
112
113 print(f'Hurray! {args.old} and {args.new} are ABI-compatible!')