summaryrefslogtreecommitdiff
path: root/gen-map.awk
blob: d581906910fdb952fdb6c52920714d805cb9d06c (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# Generate a version map file from a .map.in file.
#
# Written by Zack Weinberg <zackw at panix.com> in 2017.
# To the extent possible under law, Zack Weinberg has waived all
# copyright and related or neighboring rights to this work.
#
# See https://creativecommons.org/publicdomain/zero/1.0/ for further
# details.

# The .map.in file is the first input file, and we expect the Makefile
# to have set the variables SYMVER_MIN and SYMVER_FLOOR.  All symbol
# versions lower than SYMVER_MIN are discarded from the output.
# All symbol versions lower than SYMVER_FLOOR are replaced with
# SYMVER_FLOOR.  SYMVER_FLOOR must be greater than or equal to SYMVER_MIN.
#
# The ordering of symbol versions is entirely controlled by the %chain
# directive, which must therefore list both all of the versions
# actually used for symbols, and all of the versions that might be
# used as SYMVER_MIN or SYMVER_FLOOR.
#
# Note: if you change the format of .map.in files you probably need to
# update gen-vers.awk too.

BEGIN {
    split("", SYMBOLS) # ensure SYMBOLS is an array
    split("", VCHAIN)  # ditto VCHAIN
    NVCHAIN = 0

    # This arranges for sorted output if gawk is in use, and is
    # harmless otherwise.
    PROCINFO["sorted_in"] = "@ind_str_asc"
}

NF == 0   { next } # blank line, discard
$1 == "#" { next } # comment, discard
$1 == "%chain" {
    for (i = 2; i <= NF; i++) {
        VCHAIN[++NVCHAIN] = $i
    }
    next
}

{
    for (i = 2; i <= NF; i++) {
        if ($i != "-") {
            if ($i in SYMBOLS) {
                SYMBOLS[$i] = SYMBOLS[$i] SUBSEP $1
            } else {
                SYMBOLS[$i] = $1
            }
        }
    }
}

END {
    if (NVCHAIN == 0) {
        print ARGV[1] ": error: missing %chain directive" > "/dev/stderr"
        close("/dev/stderr")
        exit 1
    }
    symver_min_idx = 0
    symver_floor_idx = 0
    for (i = 1; i <= NVCHAIN; i++) {
        if (VCHAIN[i] == SYMVER_MIN) {
            symver_min_idx = i
        }
        if (VCHAIN[i] == SYMVER_FLOOR) {
            symver_floor_idx = i
        }
    }
    if (symver_min_idx == 0) {
        print ARGV[1] ": error: SYMVER_MIN (" SYMVER_MIN ") " \
            "not found in %chain directives" > "/dev/stderr"
        close("/dev/stderr")
        exit 1
    }
    if (symver_floor_idx == 0) {
        print ARGV[1] ": error: SYMVER_FLOOR (" SYMVER_FLOOR ") " \
            "not found in %chain directives" > "/dev/stderr"
        close("/dev/stderr")
        exit 1
    }
    if (symver_floor_idx < symver_min_idx) {
        print ARGV[1] ": error: SYMVER_FLOOR (" SYMVER_FLOOR ") " \
            "is lower than SYMVER_MIN (" SYMVER_MIN ")" > "/dev/stderr"
        close("/dev/stderr")
        exit 1
    }

    # Construct a pruned set of symbols and versions, including only
    # versions with symbols, discarding all symbols associated with
    # versions below SYMVER_MIN, raising symbols below SYMVER_FLOOR to
    # SYMVER_FLOOR, and removing duplicates.
    for (i = symver_min_idx; i <= NVCHAIN; i++) {
        v = VCHAIN[i]
        if (v in SYMBOLS) {
            nsyms = split(SYMBOLS[v], syms, SUBSEP)
            j = i;
            if (j < symver_floor_idx)
                j = symver_floor_idx;
            vr = VCHAIN[j]
            for (s = 1; s <= nsyms; s++) {
                if (syms[s]) {
                    symset[vr, syms[s]] = 1
                    allsyms[syms[s]] = 1
                }
            }
        }
    }

    vp = ""
    for (i = symver_floor_idx; i <= NVCHAIN; i++) {
        v = VCHAIN[i]
        split("", osyms)
        j = 0
        for (sym in allsyms) {
            if ((v, sym) in symset) {
                osyms[++j] = sym
            }
        }
        if (j > 0) {
            printf("%s {\n  global:\n", v);
            for (s = 1; s <= j; s++) {
                printf("    %s;\n", osyms[s]);
            }
            if (vp == "") {
                printf("  local:\n    *;\n};\n");
            } else {
                printf("} %s;\n", vp);
            }
            vp = v
        }
    }
}