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 or comments,
19 # optionally with whitespace in between each item
20 func_line_regex = re.compile(r"\s*"
22 r"(?P<func>[a-zA-Z_0-9]+)"
30 # match section name, followed by opening bracked, followed by EOL,
31 # optionally with whitespace in between each item
32 section_begin_regex = re.compile(r"\s*"
33 r"(?P<version>[a-zA-Z0-9_\.]+)"
38 # match closing bracket, optionally followed by section name (for when we
39 # inherit from another ABI version), followed by semicolon, followed by
40 # EOL, optionally with whitespace in between each item
41 section_end_regex = re.compile(r"\s*"
44 r"(?P<parent>[a-zA-Z0-9_\.]+)?"
50 # for stable ABI, we don't care about which version introduced which
51 # function, we just flatten the list. there are dupes in certain files, so
52 # use a set instead of a list
54 # copy experimental section as is
55 experimental_lines = []
56 # copy internal section as is
58 in_experimental = False
62 # gather all functions
65 line = line.strip('\n').strip()
67 # is this an end of section?
68 match = section_end_regex.match(line)
70 # whatever section this was, it's not active any more
71 in_experimental = False
75 # if we're in the middle of experimental section, we need to copy
76 # the section verbatim, so just add the line
78 experimental_lines += [line]
81 # if we're in the middle of internal section, we need to copy
82 # the section verbatim, so just add the line
84 internal_lines += [line]
91 # is this a beginning of a new section?
92 match = section_begin_regex.match(line)
94 cur_section = match.group("version")
96 in_experimental = cur_section == "EXPERIMENTAL"
98 in_internal = cur_section == "INTERNAL"
99 if not in_experimental and not in_internal:
103 # is this a function?
104 match = func_line_regex.match(line)
106 stable_lines.add(match.group("line"))
108 return has_stable, stable_lines, experimental_lines, internal_lines
111 def __generate_stable_abi(f_out, abi_major, lines):
112 # print ABI version header
113 print("DPDK_{} {{".format(abi_major), file=f_out)
115 # print global section if it exists
117 print("\tglobal:", file=f_out)
121 # print all stable lines, alphabetically sorted
122 for line in sorted(lines):
123 print("\t{}".format(line), file=f_out)
128 # print local section
129 print("\tlocal: *;", file=f_out)
132 print("};", file=f_out)
135 def __generate_experimental_abi(f_out, lines):
136 # start experimental section
137 print("EXPERIMENTAL {", file=f_out)
139 # print all experimental lines as they were
141 # don't print empty whitespace
143 print("", file=f_out)
145 print("\t{}".format(line), file=f_out)
148 print("};", file=f_out)
150 def __generate_internal_abi(f_out, lines):
151 # start internal section
152 print("INTERNAL {", file=f_out)
154 # print all internal lines as they were
156 # don't print empty whitespace
158 print("", file=f_out)
160 print("\t{}".format(line), file=f_out)
163 print("};", file=f_out)
166 arg_parser = argparse.ArgumentParser(
167 description='Merge versions in linker version script.')
169 arg_parser.add_argument("map_file", type=str,
170 help='path to linker version script file '
171 '(pattern: version.map)')
172 arg_parser.add_argument("abi_version", type=str,
173 help='target ABI version (pattern: MAJOR.MINOR)')
175 parsed = arg_parser.parse_args()
177 if not parsed.map_file.endswith('version.map'):
178 print("Invalid input file: {}".format(parsed.map_file),
180 arg_parser.print_help()
183 if not re.match(r"\d{1,2}\.\d{1,2}", parsed.abi_version):
184 print("Invalid ABI version: {}".format(parsed.abi_version),
186 arg_parser.print_help()
188 abi_major = parsed.abi_version.split('.')[0]
190 with open(parsed.map_file) as f_in:
191 has_stable, stable_lines, experimental_lines, internal_lines = __parse_map_file(f_in)
193 with open(parsed.map_file, 'w') as f_out:
194 need_newline = has_stable and experimental_lines
196 __generate_stable_abi(f_out, abi_major, stable_lines)
198 # separate sections with a newline
200 if experimental_lines:
201 __generate_experimental_abi(f_out, experimental_lines)
203 if has_stable or experimental_lines:
204 # separate sections with a newline
206 __generate_internal_abi(f_out, internal_lines)
209 if __name__ == "__main__":