2 # SPDX-License-Identifier: BSD-3-Clause
3 # Copyright(c) 2019 Intel Corporation
6 A Python program that updates and merges all available stable ABI versions into
7 one ABI version, while leaving experimental ABI exactly as it is. The intended
8 ABI version is supplied via command-line parameter. This script is to be called
9 from the devtools/update-abi.sh utility.
17 def __parse_map_file(f_in):
18 # match function name, followed by semicolon, followed by EOL, optionally
19 # with whitespace in between each item
20 func_line_regex = re.compile(r"\s*"
21 r"(?P<func>[a-zA-Z_0-9]+)"
26 # match section name, followed by opening bracked, followed by EOL,
27 # optionally with whitespace in between each item
28 section_begin_regex = re.compile(r"\s*"
29 r"(?P<version>[a-zA-Z0-9_\.]+)"
34 # match closing bracket, optionally followed by section name (for when we
35 # inherit from another ABI version), followed by semicolon, followed by
36 # EOL, optionally with whitespace in between each item
37 section_end_regex = re.compile(r"\s*"
40 r"(?P<parent>[a-zA-Z0-9_\.]+)?"
46 # for stable ABI, we don't care about which version introduced which
47 # function, we just flatten the list. there are dupes in certain files, so
48 # use a set instead of a list
50 # copy experimental section as is
51 experimental_lines = []
52 # copy internal section as is
54 in_experimental = False
58 # gather all functions
61 line = line.strip('\n').strip()
63 # is this an end of section?
64 match = section_end_regex.match(line)
66 # whatever section this was, it's not active any more
67 in_experimental = False
71 # if we're in the middle of experimental section, we need to copy
72 # the section verbatim, so just add the line
74 experimental_lines += [line]
77 # if we're in the middle of internal section, we need to copy
78 # the section verbatim, so just add the line
80 internal_lines += [line]
87 # is this a beginning of a new section?
88 match = section_begin_regex.match(line)
90 cur_section = match.group("version")
92 in_experimental = cur_section == "EXPERIMENTAL"
94 in_internal = cur_section == "INTERNAL"
95 if not in_experimental and not in_internal:
100 match = func_line_regex.match(line)
102 stable_lines.add(match.group("func"))
104 return has_stable, stable_lines, experimental_lines, internal_lines
107 def __generate_stable_abi(f_out, abi_major, lines):
108 # print ABI version header
109 print("DPDK_{} {{".format(abi_major), file=f_out)
111 # print global section if it exists
113 print("\tglobal:", file=f_out)
117 # print all stable lines, alphabetically sorted
118 for line in sorted(lines):
119 print("\t{};".format(line), file=f_out)
124 # print local section
125 print("\tlocal: *;", file=f_out)
128 print("};", file=f_out)
131 def __generate_experimental_abi(f_out, lines):
132 # start experimental section
133 print("EXPERIMENTAL {", file=f_out)
135 # print all experimental lines as they were
137 # don't print empty whitespace
139 print("", file=f_out)
141 print("\t{}".format(line), file=f_out)
144 print("};", file=f_out)
146 def __generate_internal_abi(f_out, lines):
147 # start internal section
148 print("INTERNAL {", file=f_out)
150 # print all internal lines as they were
152 # don't print empty whitespace
154 print("", file=f_out)
156 print("\t{}".format(line), file=f_out)
159 print("};", file=f_out)
162 arg_parser = argparse.ArgumentParser(
163 description='Merge versions in linker version script.')
165 arg_parser.add_argument("map_file", type=str,
166 help='path to linker version script file '
167 '(pattern: version.map)')
168 arg_parser.add_argument("abi_version", type=str,
169 help='target ABI version (pattern: MAJOR.MINOR)')
171 parsed = arg_parser.parse_args()
173 if not parsed.map_file.endswith('version.map'):
174 print("Invalid input file: {}".format(parsed.map_file),
176 arg_parser.print_help()
179 if not re.match(r"\d{1,2}\.\d{1,2}", parsed.abi_version):
180 print("Invalid ABI version: {}".format(parsed.abi_version),
182 arg_parser.print_help()
184 abi_major = parsed.abi_version.split('.')[0]
186 with open(parsed.map_file) as f_in:
187 has_stable, stable_lines, experimental_lines, internal_lines = __parse_map_file(f_in)
189 with open(parsed.map_file, 'w') as f_out:
190 need_newline = has_stable and experimental_lines
192 __generate_stable_abi(f_out, abi_major, stable_lines)
194 # separate sections with a newline
196 if experimental_lines:
197 __generate_experimental_abi(f_out, experimental_lines)
199 if has_stable or experimental_lines:
200 # separate sections with a newline
202 __generate_internal_abi(f_out, internal_lines)
205 if __name__ == "__main__":