xref: /aosp_15_r20/external/libxkbcommon/test/symbols-leak-test.py (revision 2b949d0487e80d67f1fda82db69e101e761f8064)
1*2b949d04SAndroid Build Coastguard Worker#!/usr/bin/env python3
2*2b949d04SAndroid Build Coastguard Worker"""Check that all exported symbols are specified in the symbol version scripts.
3*2b949d04SAndroid Build Coastguard Worker
4*2b949d04SAndroid Build Coastguard WorkerIf this fails, please update the appropriate .map file (adding new version
5*2b949d04SAndroid Build Coastguard Workernodes as needed).
6*2b949d04SAndroid Build Coastguard Worker"""
7*2b949d04SAndroid Build Coastguard Workerimport os
8*2b949d04SAndroid Build Coastguard Workerimport pathlib
9*2b949d04SAndroid Build Coastguard Workerimport re
10*2b949d04SAndroid Build Coastguard Workerimport sys
11*2b949d04SAndroid Build Coastguard Worker
12*2b949d04SAndroid Build Coastguard Worker
13*2b949d04SAndroid Build Coastguard Workertop_srcdir = pathlib.Path(os.environ['top_srcdir'])
14*2b949d04SAndroid Build Coastguard Worker
15*2b949d04SAndroid Build Coastguard Worker
16*2b949d04SAndroid Build Coastguard Workerdef symbols_from_map(path):
17*2b949d04SAndroid Build Coastguard Worker    return re.findall(r'^\s+(xkb_.*);', path.read_text('utf-8'), re.MULTILINE)
18*2b949d04SAndroid Build Coastguard Worker
19*2b949d04SAndroid Build Coastguard Worker
20*2b949d04SAndroid Build Coastguard Workerdef symbols_from_src(path):
21*2b949d04SAndroid Build Coastguard Worker    return re.findall(r'XKB_EXPORT.*\n(xkb_.*)\(', path.read_text('utf-8'))
22*2b949d04SAndroid Build Coastguard Worker
23*2b949d04SAndroid Build Coastguard Worker
24*2b949d04SAndroid Build Coastguard Workerdef diff(map_path, src_paths):
25*2b949d04SAndroid Build Coastguard Worker    map_symbols = set(symbols_from_map(map_path))
26*2b949d04SAndroid Build Coastguard Worker    src_symbols = set.union(set(), *(symbols_from_src(path) for path in src_paths))
27*2b949d04SAndroid Build Coastguard Worker    return sorted(map_symbols - src_symbols), sorted(src_symbols - map_symbols)
28*2b949d04SAndroid Build Coastguard Worker
29*2b949d04SAndroid Build Coastguard Worker
30*2b949d04SAndroid Build Coastguard Workerexit = 0
31*2b949d04SAndroid Build Coastguard Worker
32*2b949d04SAndroid Build Coastguard Worker# xkbcommon symbols
33*2b949d04SAndroid Build Coastguard Workerleft, right = diff(
34*2b949d04SAndroid Build Coastguard Worker    top_srcdir/'xkbcommon.map',
35*2b949d04SAndroid Build Coastguard Worker    [
36*2b949d04SAndroid Build Coastguard Worker        *(top_srcdir/'src').glob('*.c'),
37*2b949d04SAndroid Build Coastguard Worker        *(top_srcdir/'src'/'xkbcomp').glob('*.c'),
38*2b949d04SAndroid Build Coastguard Worker        *(top_srcdir/'src'/'compose').glob('*.c'),
39*2b949d04SAndroid Build Coastguard Worker    ],
40*2b949d04SAndroid Build Coastguard Worker)
41*2b949d04SAndroid Build Coastguard Workerif left:
42*2b949d04SAndroid Build Coastguard Worker    print('xkbcommon map has extra symbols:', ' '.join(left))
43*2b949d04SAndroid Build Coastguard Worker    exit = 1
44*2b949d04SAndroid Build Coastguard Workerif right:
45*2b949d04SAndroid Build Coastguard Worker    print('xkbcommon src has extra symbols:', ' '.join(right))
46*2b949d04SAndroid Build Coastguard Worker    exit = 1
47*2b949d04SAndroid Build Coastguard Worker
48*2b949d04SAndroid Build Coastguard Worker# xkbcommon-x11 symbols
49*2b949d04SAndroid Build Coastguard Workerleft, right = diff(
50*2b949d04SAndroid Build Coastguard Worker    top_srcdir/'xkbcommon-x11.map',
51*2b949d04SAndroid Build Coastguard Worker    [
52*2b949d04SAndroid Build Coastguard Worker        *(top_srcdir/'src'/'x11').glob('*.c'),
53*2b949d04SAndroid Build Coastguard Worker    ],
54*2b949d04SAndroid Build Coastguard Worker)
55*2b949d04SAndroid Build Coastguard Workerif left:
56*2b949d04SAndroid Build Coastguard Worker    print('xkbcommon-x11 map has extra symbols:', ' '.join(left))
57*2b949d04SAndroid Build Coastguard Worker    exit = 1
58*2b949d04SAndroid Build Coastguard Workerif right:
59*2b949d04SAndroid Build Coastguard Worker    print('xkbcommon-x11 src has extra symbols:', ' '.join(right))
60*2b949d04SAndroid Build Coastguard Worker    exit = 1
61*2b949d04SAndroid Build Coastguard Worker
62*2b949d04SAndroid Build Coastguard Workersys.exit(exit)
63