xref: /aosp_15_r20/external/libpng/scripts/checksym.awk (revision a67afe4df73cf47866eedc69947994b8ff839aba)
1#!/bin/awk -f
2
3# Check a list of symbols against the master definition
4# (official) list.  Arguments:
5#
6# awk -f checksym.awk official-def list-to-check
7#
8# Output is a file in the current directory called 'symbols.new',
9# the value of the awk variable "of" (which can be changed on the
10# command line if required.)  stdout holds error messages.  Error
11# code indicates success or failure.
12#
13# NOTE: this is a pure, old fashioned, awk script.  It will
14# work with any awk
15
16BEGIN{
17   err=0
18   master=""        # master file
19   official[1] = "" # defined symbols from master file
20   symbol[1] = ""   # defined symbols from png.h
21   removed[1] = ""  # removed symbols from png.h
22   lasto = 0        # last ordinal value from png.h
23   mastero = 0      # highest ordinal in master file
24   symbolo = 0      # highest ordinal in png.h
25   missing = "error"# log an error on missing symbols
26   of="symbols.new" # default to a fixed name
27}
28
29# Read existing definitions from the master file (the first
30# file on the command line.)  This must be a def file and it
31# has definition lines (others are ignored) of the form:
32#
33#   symbol @ordinal
34#
35master == "" {
36   master = FILENAME
37}
38FILENAME==master && NF==2 && $2~/^@/ && $1!~/^;/ {
39   o=0+substr($2,2)
40   if (o > 0) {
41      if (official[o] == "") {
42         official[o] = $1
43         if (o > mastero) mastero = o
44         next
45      } else
46         print master ": duplicated symbol:", official[o] ":", $0
47   } else
48      print master ": bad export line format:", $0
49   err = 1
50}
51FILENAME==master && $1==";missing" && NF==2{
52   # This allows the master file to control how missing symbols
53   # are handled; symbols that aren't in either the master or
54   # the new file.  Valid values are 'ignore', 'warning' and
55   # 'error'
56   missing = $2
57}
58FILENAME==master {
59   next
60}
61
62# Read new definitions, these are free form but the lines must
63# just be symbol definitions.  Lines will be commented out for
64# 'removed' symbols, introduced in png.h using PNG_REMOVED rather
65# than PNG_EXPORT.  Use symbols.dfn or pngwin.dfn to generate the
66# input file.
67#
68#  symbol @ordinal   # two fields, exported symbol
69#  ; symbol @ordinal # three fields, removed symbol
70#  ; @ordinal        # two fields, the last ordinal
71NF==2 && $1 == ";" && $2 ~ /^@[1-9][0-9]*$/ { # last ordinal
72   o=0+substr($2,2)
73   if (lasto == 0 || lasto == o)
74      lasto=o
75   else {
76      print "png.h: duplicated last ordinal:", lasto, o
77      err = 1
78   }
79   next
80}
81NF==3 && $1 == ";" && $3 ~ /^@[1-9][0-9]*$/ { # removed symbol
82   o=0+substr($3,2)
83   if (removed[o] == "" || removed[o] == $2) {
84      removed[o] = $2
85      if (o > symbolo) symbolo = o
86   } else {
87      print "png.h: duplicated removed symbol", o ": '" removed[o] "' != '" $2 "'"
88      err = 1
89   }
90   next
91}
92NF==2 && $2 ~ /^@[1-9][0-9]*$/ { # exported symbol
93   o=0+substr($2,2)
94   if (symbol[o] == "" || symbol[o] == $1) {
95      symbol[o] = $1
96      if (o > symbolo) symbolo = o
97   } else {
98      print "png.h: duplicated symbol", o ": '" symbol[o] "' != '" $1 "'"
99      err = 1
100   }
101}
102{
103   next # skip all other lines
104}
105
106# At the end check for symbols marked as both duplicated and removed
107END{
108   if (symbolo > lasto) {
109      print "highest symbol ordinal in png.h,",
110            symbolo ", exceeds last ordinal from png.h", lasto
111      err = 1
112   }
113   if (mastero > lasto) {
114      print "highest symbol ordinal in", master ",",
115            mastero ", exceeds last ordinal from png.h", lasto
116      err = 1
117   }
118   unexported=0
119   # Add a standard header to symbols.new:
120   print ";Version INSERT-VERSION-HERE" >of
121   print ";--------------------------------------------------------------" >of
122   print "; LIBPNG symbol list as a Win32 DEF file" >of
123   print "; Contains all the symbols that can be exported from libpng" >of
124   print ";--------------------------------------------------------------" >of
125   print "LIBRARY" >of
126   print "" >of
127   print "EXPORTS" >of
128
129   for (o=1; o<=lasto; ++o) {
130      if (symbol[o] == "" && removed[o] == "") {
131         if (unexported == 0) unexported = o
132         if (official[o] == "") {
133            # missing in export list too, so ok
134            if (o < lasto) continue
135         }
136      }
137      if (unexported != 0) {
138         # Symbols in the .def but not in the new file are errors, but
139         # the 'unexported' symbols aren't in either.  By default this
140         # is an error too (see the setting of 'missing' at the start),
141         # but this can be reset on the command line or by stuff in the
142         # file - see the comments above.
143         if (missing != "ignore") {
144            if (o-1 > unexported)
145               print "png.h:", missing ": missing symbols:", unexported "-" o-1
146            else
147               print "png.h:", missing ": missing symbol:", unexported
148            if (missing != "warning")
149               err = 1
150         }
151         unexported = 0
152      }
153      if (symbol[o] != "" && removed[o] != "") {
154         print "png.h: symbol", o,
155               "both exported as '" symbol[o] "' and removed as '" removed[o] "'"
156         err = 1
157      } else if (symbol[o] != official[o]) {
158         # either the symbol is missing somewhere or it changed
159         err = 1
160         if (symbol[o] == "")
161            print "png.h: symbol", o,
162                  "is exported as '" official[o] "' in", master
163         else if (official[o] == "")
164            print "png.h: exported symbol", o,
165                  "'" symbol[o] "' not present in", master
166         else
167            print "png.h: exported symbol", o,
168                  "'" symbol[o] "' exists as '" official[o] "' in", master
169      }
170
171      # Finally generate symbols.new
172      if (symbol[o] != "")
173         print " " symbol[o], "@" o > of
174   }
175
176   if (err != 0) {
177      print "*** A new list is in", of, "***"
178      exit 1
179   }
180}
181