blob: 07c726a3e2388425e18dc0e7606b62dcc5b5a706 [file] [log] [blame]
Darryl Green7c2dd582018-03-01 14:53:49 +00001#!/usr/bin/env python3
Darryl Green78696802018-04-06 11:23:22 +01002"""
Darryl Green78696802018-04-06 11:23:22 +01003Purpose
4
5This script is a small wrapper around the abi-compliance-checker and
6abi-dumper tools, applying them to compare the ABI and API of the library
7files from two different Git revisions within an Mbed TLS repository.
Darryl Green0da45782019-02-21 13:09:26 +00008The results of the comparison are either formatted as HTML and stored at
Darryl Green7c0e0522019-03-05 15:21:32 +00009a configurable location, or are given as a brief list of problems.
Darryl Green0da45782019-02-21 13:09:26 +000010Returns 0 on success, 1 on ABI/API non-compliance, and 2 if there is an error
11while running the script. Note: must be run from Mbed TLS root.
Darryl Green78696802018-04-06 11:23:22 +010012"""
Darryl Green7c2dd582018-03-01 14:53:49 +000013
Bence Szépkútia2947ac2020-08-19 16:37:36 +020014# Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +020015# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
16#
17# This file is provided under the Apache License 2.0, or the
18# GNU General Public License v2.0 or later.
19#
20# **********
21# Apache License 2.0:
Bence Szépkúti51b41d52020-05-26 01:54:15 +020022#
23# Licensed under the Apache License, Version 2.0 (the "License"); you may
24# not use this file except in compliance with the License.
25# You may obtain a copy of the License at
26#
27# http://www.apache.org/licenses/LICENSE-2.0
28#
29# Unless required by applicable law or agreed to in writing, software
30# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
31# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
32# See the License for the specific language governing permissions and
33# limitations under the License.
34#
Bence Szépkútif744bd72020-06-05 13:02:18 +020035# **********
36#
37# **********
38# GNU General Public License v2.0 or later:
39#
40# This program is free software; you can redistribute it and/or modify
41# it under the terms of the GNU General Public License as published by
42# the Free Software Foundation; either version 2 of the License, or
43# (at your option) any later version.
44#
45# This program is distributed in the hope that it will be useful,
46# but WITHOUT ANY WARRANTY; without even the implied warranty of
47# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
48# GNU General Public License for more details.
49#
50# You should have received a copy of the GNU General Public License along
51# with this program; if not, write to the Free Software Foundation, Inc.,
52# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
53#
54# **********
Bence Szépkúti51b41d52020-05-26 01:54:15 +020055
Darryl Green7c2dd582018-03-01 14:53:49 +000056import os
57import sys
58import traceback
59import shutil
60import subprocess
61import argparse
62import logging
63import tempfile
Darryl Greenae5d66c2019-02-25 11:35:05 +000064import fnmatch
Darryl Green30dc6d52019-04-09 09:14:17 +010065from types import SimpleNamespace
Darryl Green7c2dd582018-03-01 14:53:49 +000066
Darryl Green0da45782019-02-21 13:09:26 +000067import xml.etree.ElementTree as ET
68
Darryl Green7c2dd582018-03-01 14:53:49 +000069
Gilles Peskine5d1dfd42020-03-24 18:25:17 +010070class AbiChecker:
Gilles Peskine9df17632019-02-25 20:36:52 +010071 """API and ABI checker."""
Darryl Green7c2dd582018-03-01 14:53:49 +000072
Darryl Green30dc6d52019-04-09 09:14:17 +010073 def __init__(self, old_version, new_version, configuration):
Gilles Peskine9df17632019-02-25 20:36:52 +010074 """Instantiate the API/ABI checker.
75
Darryl Green7381bea2019-03-05 16:25:38 +000076 old_version: RepoVersion containing details to compare against
77 new_version: RepoVersion containing details to check
Darryl Green3c9e7d22019-04-12 15:17:02 +010078 configuration.report_dir: directory for output files
79 configuration.keep_all_reports: if false, delete old reports
80 configuration.brief: if true, output shorter report to stdout
81 configuration.skip_file: path to file containing symbols and types to skip
Gilles Peskine9df17632019-02-25 20:36:52 +010082 """
Darryl Green7c2dd582018-03-01 14:53:49 +000083 self.repo_path = "."
84 self.log = None
Darryl Green30dc6d52019-04-09 09:14:17 +010085 self.verbose = configuration.verbose
Darryl Green88bfbc22019-03-05 16:30:39 +000086 self._setup_logger()
Darryl Green30dc6d52019-04-09 09:14:17 +010087 self.report_dir = os.path.abspath(configuration.report_dir)
88 self.keep_all_reports = configuration.keep_all_reports
Darryl Green6b3cbfa2019-04-11 15:50:41 +010089 self.can_remove_report_dir = not (os.path.exists(self.report_dir) or
Darryl Green30dc6d52019-04-09 09:14:17 +010090 self.keep_all_reports)
Darryl Green7381bea2019-03-05 16:25:38 +000091 self.old_version = old_version
92 self.new_version = new_version
Darryl Green30dc6d52019-04-09 09:14:17 +010093 self.skip_file = configuration.skip_file
94 self.brief = configuration.brief
Darryl Green7c2dd582018-03-01 14:53:49 +000095 self.git_command = "git"
96 self.make_command = "make"
97
Gilles Peskine9df17632019-02-25 20:36:52 +010098 @staticmethod
99 def check_repo_path():
Gilles Peskinec53b93b2019-07-04 18:59:36 +0200100 if not all(os.path.isdir(d) for d in ["include", "library", "tests"]):
Darryl Green7c2dd582018-03-01 14:53:49 +0000101 raise Exception("Must be run from Mbed TLS root")
102
Darryl Green88bfbc22019-03-05 16:30:39 +0000103 def _setup_logger(self):
Darryl Green7c2dd582018-03-01 14:53:49 +0000104 self.log = logging.getLogger()
Darryl Green66025382019-03-08 11:30:04 +0000105 if self.verbose:
106 self.log.setLevel(logging.DEBUG)
107 else:
108 self.log.setLevel(logging.INFO)
Darryl Green7c2dd582018-03-01 14:53:49 +0000109 self.log.addHandler(logging.StreamHandler())
110
Gilles Peskine9df17632019-02-25 20:36:52 +0100111 @staticmethod
112 def check_abi_tools_are_installed():
Darryl Green7c2dd582018-03-01 14:53:49 +0000113 for command in ["abi-dumper", "abi-compliance-checker"]:
114 if not shutil.which(command):
115 raise Exception("{} not installed, aborting".format(command))
116
Darryl Green88bfbc22019-03-05 16:30:39 +0000117 def _get_clean_worktree_for_git_revision(self, version):
Darryl Green7381bea2019-03-05 16:25:38 +0000118 """Make a separate worktree with version.revision checked out.
Gilles Peskine9df17632019-02-25 20:36:52 +0100119 Do not modify the current worktree."""
Darryl Green7c2dd582018-03-01 14:53:49 +0000120 git_worktree_path = tempfile.mkdtemp()
Darryl Green7381bea2019-03-05 16:25:38 +0000121 if version.repository:
Darryl Green66025382019-03-08 11:30:04 +0000122 self.log.debug(
Darryl Green5a301f02019-02-19 16:59:33 +0000123 "Checking out git worktree for revision {} from {}".format(
Darryl Green7381bea2019-03-05 16:25:38 +0000124 version.revision, version.repository
Darryl Green5a301f02019-02-19 16:59:33 +0000125 )
126 )
Darryl Greena0415bc2019-04-12 16:24:25 +0100127 fetch_output = subprocess.check_output(
Darryl Green7381bea2019-03-05 16:25:38 +0000128 [self.git_command, "fetch",
129 version.repository, version.revision],
Darryl Green5a301f02019-02-19 16:59:33 +0000130 cwd=self.repo_path,
Darryl Green5a301f02019-02-19 16:59:33 +0000131 stderr=subprocess.STDOUT
132 )
Darryl Green66025382019-03-08 11:30:04 +0000133 self.log.debug(fetch_output.decode("utf-8"))
Darryl Green5a301f02019-02-19 16:59:33 +0000134 worktree_rev = "FETCH_HEAD"
135 else:
Darryl Green66025382019-03-08 11:30:04 +0000136 self.log.debug("Checking out git worktree for revision {}".format(
Darryl Green7381bea2019-03-05 16:25:38 +0000137 version.revision
138 ))
139 worktree_rev = version.revision
Darryl Greena0415bc2019-04-12 16:24:25 +0100140 worktree_output = subprocess.check_output(
Darryl Green5a301f02019-02-19 16:59:33 +0000141 [self.git_command, "worktree", "add", "--detach",
142 git_worktree_path, worktree_rev],
Darryl Green7c2dd582018-03-01 14:53:49 +0000143 cwd=self.repo_path,
Darryl Green7c2dd582018-03-01 14:53:49 +0000144 stderr=subprocess.STDOUT
145 )
Darryl Green66025382019-03-08 11:30:04 +0000146 self.log.debug(worktree_output.decode("utf-8"))
Gilles Peskineea943912019-07-04 19:01:22 +0200147 version.commit = subprocess.check_output(
Darryl Green64b4b6e2019-07-25 14:33:33 +0100148 [self.git_command, "rev-parse", "HEAD"],
Gilles Peskineea943912019-07-04 19:01:22 +0200149 cwd=git_worktree_path,
150 stderr=subprocess.STDOUT
151 ).decode("ascii").rstrip()
152 self.log.debug("Commit is {}".format(version.commit))
Darryl Green7c2dd582018-03-01 14:53:49 +0000153 return git_worktree_path
154
Darryl Green88bfbc22019-03-05 16:30:39 +0000155 def _update_git_submodules(self, git_worktree_path, version):
Darryl Green26dff8e2019-04-05 17:06:17 +0100156 """If the crypto submodule is present, initialize it.
157 if version.crypto_revision exists, update it to that revision,
158 otherwise update it to the default revision"""
Darryl Greena0415bc2019-04-12 16:24:25 +0100159 update_output = subprocess.check_output(
Jaeden Amero4cd4b4b2018-11-02 16:35:09 +0000160 [self.git_command, "submodule", "update", "--init", '--recursive'],
161 cwd=git_worktree_path,
Jaeden Amero4cd4b4b2018-11-02 16:35:09 +0000162 stderr=subprocess.STDOUT
163 )
Darryl Greena0415bc2019-04-12 16:24:25 +0100164 self.log.debug(update_output.decode("utf-8"))
Darryl Green0478a322019-03-05 15:23:25 +0000165 if not (os.path.exists(os.path.join(git_worktree_path, "crypto"))
Darryl Green7381bea2019-03-05 16:25:38 +0000166 and version.crypto_revision):
Darryl Green0478a322019-03-05 15:23:25 +0000167 return
168
Darryl Green7381bea2019-03-05 16:25:38 +0000169 if version.crypto_repository:
Darryl Greena0415bc2019-04-12 16:24:25 +0100170 fetch_output = subprocess.check_output(
Darryl Green3f742982019-03-08 11:12:19 +0000171 [self.git_command, "fetch", version.crypto_repository,
172 version.crypto_revision],
Darryl Green0478a322019-03-05 15:23:25 +0000173 cwd=os.path.join(git_worktree_path, "crypto"),
Darryl Green0478a322019-03-05 15:23:25 +0000174 stderr=subprocess.STDOUT
175 )
Darryl Green66025382019-03-08 11:30:04 +0000176 self.log.debug(fetch_output.decode("utf-8"))
Darryl Green3f742982019-03-08 11:12:19 +0000177 crypto_rev = "FETCH_HEAD"
178 else:
179 crypto_rev = version.crypto_revision
180
Darryl Greena0415bc2019-04-12 16:24:25 +0100181 checkout_output = subprocess.check_output(
Darryl Green3f742982019-03-08 11:12:19 +0000182 [self.git_command, "checkout", crypto_rev],
183 cwd=os.path.join(git_worktree_path, "crypto"),
Darryl Green3f742982019-03-08 11:12:19 +0000184 stderr=subprocess.STDOUT
185 )
Darryl Green66025382019-03-08 11:30:04 +0000186 self.log.debug(checkout_output.decode("utf-8"))
Jaeden Amero4cd4b4b2018-11-02 16:35:09 +0000187
Darryl Green88bfbc22019-03-05 16:30:39 +0000188 def _build_shared_libraries(self, git_worktree_path, version):
Gilles Peskine9df17632019-02-25 20:36:52 +0100189 """Build the shared libraries in the specified worktree."""
Darryl Green7c2dd582018-03-01 14:53:49 +0000190 my_environment = os.environ.copy()
191 my_environment["CFLAGS"] = "-g -Og"
192 my_environment["SHARED"] = "1"
Darryl Greenfbf3c8a2019-05-09 13:03:05 +0100193 if os.path.exists(os.path.join(git_worktree_path, "crypto")):
194 my_environment["USE_CRYPTO_SUBMODULE"] = "1"
Darryl Greena0415bc2019-04-12 16:24:25 +0100195 make_output = subprocess.check_output(
Darryl Greenc8e6ad42019-02-28 11:52:39 +0000196 [self.make_command, "lib"],
Darryl Green7c2dd582018-03-01 14:53:49 +0000197 env=my_environment,
198 cwd=git_worktree_path,
Darryl Green7c2dd582018-03-01 14:53:49 +0000199 stderr=subprocess.STDOUT
200 )
Darryl Green66025382019-03-08 11:30:04 +0000201 self.log.debug(make_output.decode("utf-8"))
Darryl Green03c5e852019-04-12 15:18:02 +0100202 for root, _dirs, files in os.walk(git_worktree_path):
Darryl Greenae5d66c2019-02-25 11:35:05 +0000203 for file in fnmatch.filter(files, "*.so"):
Darryl Green7381bea2019-03-05 16:25:38 +0000204 version.modules[os.path.splitext(file)[0]] = (
Darryl Greende118092019-02-27 16:53:40 +0000205 os.path.join(root, file)
Darryl Greenae5d66c2019-02-25 11:35:05 +0000206 )
Darryl Green7c2dd582018-03-01 14:53:49 +0000207
Gilles Peskineea943912019-07-04 19:01:22 +0200208 @staticmethod
209 def _pretty_revision(version):
210 if version.revision == version.commit:
211 return version.revision
212 else:
213 return "{} ({})".format(version.revision, version.commit)
214
Darryl Green26dff8e2019-04-05 17:06:17 +0100215 def _get_abi_dumps_from_shared_libraries(self, version):
Gilles Peskine9df17632019-02-25 20:36:52 +0100216 """Generate the ABI dumps for the specified git revision.
Darryl Green26dff8e2019-04-05 17:06:17 +0100217 The shared libraries must have been built and the module paths
218 present in version.modules."""
Darryl Green7381bea2019-03-05 16:25:38 +0000219 for mbed_module, module_path in version.modules.items():
Darryl Green7c2dd582018-03-01 14:53:49 +0000220 output_path = os.path.join(
Darryl Green57838472019-04-04 14:39:33 +0100221 self.report_dir, "{}-{}-{}.dump".format(
222 mbed_module, version.revision, version.version
Darryl Greende118092019-02-27 16:53:40 +0000223 )
Darryl Green7c2dd582018-03-01 14:53:49 +0000224 )
225 abi_dump_command = [
226 "abi-dumper",
Darryl Greenae5d66c2019-02-25 11:35:05 +0000227 module_path,
Darryl Green7c2dd582018-03-01 14:53:49 +0000228 "-o", output_path,
Gilles Peskineea943912019-07-04 19:01:22 +0200229 "-lver", self._pretty_revision(version),
Darryl Green7c2dd582018-03-01 14:53:49 +0000230 ]
Darryl Greena0415bc2019-04-12 16:24:25 +0100231 abi_dump_output = subprocess.check_output(
Darryl Green7c2dd582018-03-01 14:53:49 +0000232 abi_dump_command,
Darryl Green7c2dd582018-03-01 14:53:49 +0000233 stderr=subprocess.STDOUT
234 )
Darryl Green66025382019-03-08 11:30:04 +0000235 self.log.debug(abi_dump_output.decode("utf-8"))
Darryl Green7381bea2019-03-05 16:25:38 +0000236 version.abi_dumps[mbed_module] = output_path
Darryl Green7c2dd582018-03-01 14:53:49 +0000237
Darryl Green88bfbc22019-03-05 16:30:39 +0000238 def _cleanup_worktree(self, git_worktree_path):
Gilles Peskine9df17632019-02-25 20:36:52 +0100239 """Remove the specified git worktree."""
Darryl Green7c2dd582018-03-01 14:53:49 +0000240 shutil.rmtree(git_worktree_path)
Darryl Greena0415bc2019-04-12 16:24:25 +0100241 worktree_output = subprocess.check_output(
Darryl Green7c2dd582018-03-01 14:53:49 +0000242 [self.git_command, "worktree", "prune"],
243 cwd=self.repo_path,
Darryl Green7c2dd582018-03-01 14:53:49 +0000244 stderr=subprocess.STDOUT
245 )
Darryl Green66025382019-03-08 11:30:04 +0000246 self.log.debug(worktree_output.decode("utf-8"))
Darryl Green7c2dd582018-03-01 14:53:49 +0000247
Darryl Green88bfbc22019-03-05 16:30:39 +0000248 def _get_abi_dump_for_ref(self, version):
Gilles Peskine9df17632019-02-25 20:36:52 +0100249 """Generate the ABI dumps for the specified git revision."""
Darryl Green88bfbc22019-03-05 16:30:39 +0000250 git_worktree_path = self._get_clean_worktree_for_git_revision(version)
251 self._update_git_submodules(git_worktree_path, version)
252 self._build_shared_libraries(git_worktree_path, version)
Darryl Green26dff8e2019-04-05 17:06:17 +0100253 self._get_abi_dumps_from_shared_libraries(version)
Darryl Green88bfbc22019-03-05 16:30:39 +0000254 self._cleanup_worktree(git_worktree_path)
Darryl Green7c2dd582018-03-01 14:53:49 +0000255
Darryl Green88bfbc22019-03-05 16:30:39 +0000256 def _remove_children_with_tag(self, parent, tag):
Darryl Green0da45782019-02-21 13:09:26 +0000257 children = parent.getchildren()
258 for child in children:
259 if child.tag == tag:
260 parent.remove(child)
261 else:
Darryl Green88bfbc22019-03-05 16:30:39 +0000262 self._remove_children_with_tag(child, tag)
Darryl Green0da45782019-02-21 13:09:26 +0000263
Darryl Green88bfbc22019-03-05 16:30:39 +0000264 def _remove_extra_detail_from_report(self, report_root):
Darryl Green0da45782019-02-21 13:09:26 +0000265 for tag in ['test_info', 'test_results', 'problem_summary',
Darryl Green0ae1a532019-06-05 12:57:50 +0100266 'added_symbols', 'affected']:
Darryl Green88bfbc22019-03-05 16:30:39 +0000267 self._remove_children_with_tag(report_root, tag)
Darryl Green0da45782019-02-21 13:09:26 +0000268
269 for report in report_root:
270 for problems in report.getchildren()[:]:
271 if not problems.getchildren():
272 report.remove(problems)
273
Gilles Peskine47a4cba2019-07-04 19:17:40 +0200274 def _abi_compliance_command(self, mbed_module, output_path):
275 """Build the command to run to analyze the library mbed_module.
276 The report will be placed in output_path."""
277 abi_compliance_command = [
278 "abi-compliance-checker",
279 "-l", mbed_module,
280 "-old", self.old_version.abi_dumps[mbed_module],
281 "-new", self.new_version.abi_dumps[mbed_module],
282 "-strict",
283 "-report-path", output_path,
284 ]
285 if self.skip_file:
286 abi_compliance_command += ["-skip-symbols", self.skip_file,
287 "-skip-types", self.skip_file]
288 if self.brief:
289 abi_compliance_command += ["-report-format", "xml",
290 "-stdout"]
291 return abi_compliance_command
292
293 def _is_library_compatible(self, mbed_module, compatibility_report):
294 """Test if the library mbed_module has remained compatible.
295 Append a message regarding compatibility to compatibility_report."""
296 output_path = os.path.join(
297 self.report_dir, "{}-{}-{}.html".format(
298 mbed_module, self.old_version.revision,
299 self.new_version.revision
300 )
301 )
302 try:
303 subprocess.check_output(
304 self._abi_compliance_command(mbed_module, output_path),
305 stderr=subprocess.STDOUT
306 )
307 except subprocess.CalledProcessError as err:
308 if err.returncode != 1:
309 raise err
310 if self.brief:
311 self.log.info(
312 "Compatibility issues found for {}".format(mbed_module)
313 )
314 report_root = ET.fromstring(err.output.decode("utf-8"))
315 self._remove_extra_detail_from_report(report_root)
316 self.log.info(ET.tostring(report_root).decode("utf-8"))
317 else:
318 self.can_remove_report_dir = False
319 compatibility_report.append(
320 "Compatibility issues found for {}, "
321 "for details see {}".format(mbed_module, output_path)
322 )
323 return False
324 compatibility_report.append(
325 "No compatibility issues for {}".format(mbed_module)
326 )
327 if not (self.keep_all_reports or self.brief):
328 os.remove(output_path)
329 return True
330
Darryl Green7c2dd582018-03-01 14:53:49 +0000331 def get_abi_compatibility_report(self):
Gilles Peskine9df17632019-02-25 20:36:52 +0100332 """Generate a report of the differences between the reference ABI
Darryl Green26dff8e2019-04-05 17:06:17 +0100333 and the new ABI. ABI dumps from self.old_version and self.new_version
334 must be available."""
Gilles Peskine47a4cba2019-07-04 19:17:40 +0200335 compatibility_report = ["Checking evolution from {} to {}".format(
Gilles Peskineea943912019-07-04 19:01:22 +0200336 self._pretty_revision(self.old_version),
337 self._pretty_revision(self.new_version)
Gilles Peskine47a4cba2019-07-04 19:17:40 +0200338 )]
Darryl Green7c2dd582018-03-01 14:53:49 +0000339 compliance_return_code = 0
Darryl Green7381bea2019-03-05 16:25:38 +0000340 shared_modules = list(set(self.old_version.modules.keys()) &
341 set(self.new_version.modules.keys()))
Darryl Greende118092019-02-27 16:53:40 +0000342 for mbed_module in shared_modules:
Gilles Peskine47a4cba2019-07-04 19:17:40 +0200343 if not self._is_library_compatible(mbed_module,
344 compatibility_report):
345 compliance_return_code = 1
Darryl Greene831f552019-05-29 11:29:08 +0100346 for version in [self.old_version, self.new_version]:
347 for mbed_module, mbed_module_dump in version.abi_dumps.items():
348 os.remove(mbed_module_dump)
Darryl Green131e24b2019-02-25 17:01:55 +0000349 if self.can_remove_report_dir:
Darryl Green7c2dd582018-03-01 14:53:49 +0000350 os.rmdir(self.report_dir)
Gilles Peskine47a4cba2019-07-04 19:17:40 +0200351 self.log.info("\n".join(compatibility_report))
Darryl Green7c2dd582018-03-01 14:53:49 +0000352 return compliance_return_code
353
354 def check_for_abi_changes(self):
Gilles Peskine9df17632019-02-25 20:36:52 +0100355 """Generate a report of ABI differences
356 between self.old_rev and self.new_rev."""
Darryl Green7c2dd582018-03-01 14:53:49 +0000357 self.check_repo_path()
358 self.check_abi_tools_are_installed()
Darryl Green88bfbc22019-03-05 16:30:39 +0000359 self._get_abi_dump_for_ref(self.old_version)
360 self._get_abi_dump_for_ref(self.new_version)
Darryl Green7c2dd582018-03-01 14:53:49 +0000361 return self.get_abi_compatibility_report()
362
363
364def run_main():
365 try:
366 parser = argparse.ArgumentParser(
367 description=(
Darryl Green418527b2018-04-16 12:02:29 +0100368 """This script is a small wrapper around the
369 abi-compliance-checker and abi-dumper tools, applying them
370 to compare the ABI and API of the library files from two
371 different Git revisions within an Mbed TLS repository.
Darryl Green0da45782019-02-21 13:09:26 +0000372 The results of the comparison are either formatted as HTML and
Darryl Green7c0e0522019-03-05 15:21:32 +0000373 stored at a configurable location, or are given as a brief list
374 of problems. Returns 0 on success, 1 on ABI/API non-compliance,
375 and 2 if there is an error while running the script.
376 Note: must be run from Mbed TLS root."""
Darryl Green7c2dd582018-03-01 14:53:49 +0000377 )
378 )
379 parser.add_argument(
Darryl Green66025382019-03-08 11:30:04 +0000380 "-v", "--verbose", action="store_true",
381 help="set verbosity level",
382 )
383 parser.add_argument(
Darryl Green418527b2018-04-16 12:02:29 +0100384 "-r", "--report-dir", type=str, default="reports",
Darryl Green7c2dd582018-03-01 14:53:49 +0000385 help="directory where reports are stored, default is reports",
386 )
387 parser.add_argument(
Darryl Green418527b2018-04-16 12:02:29 +0100388 "-k", "--keep-all-reports", action="store_true",
Darryl Green7c2dd582018-03-01 14:53:49 +0000389 help="keep all reports, even if there are no compatibility issues",
390 )
391 parser.add_argument(
Darryl Green06c51d02019-03-01 09:54:44 +0000392 "-o", "--old-rev", type=str, help="revision for old version.",
393 required=True,
Darryl Green7c2dd582018-03-01 14:53:49 +0000394 )
395 parser.add_argument(
Darryl Green06c51d02019-03-01 09:54:44 +0000396 "-or", "--old-repo", type=str, help="repository for old version."
Darryl Greenae5d66c2019-02-25 11:35:05 +0000397 )
398 parser.add_argument(
Darryl Green06c51d02019-03-01 09:54:44 +0000399 "-oc", "--old-crypto-rev", type=str,
400 help="revision for old crypto submodule."
Darryl Green7c2dd582018-03-01 14:53:49 +0000401 )
Darryl Green668063b2019-02-20 15:01:56 +0000402 parser.add_argument(
Darryl Green06c51d02019-03-01 09:54:44 +0000403 "-ocr", "--old-crypto-repo", type=str,
404 help="repository for old crypto submodule."
405 )
406 parser.add_argument(
407 "-n", "--new-rev", type=str, help="revision for new version",
408 required=True,
409 )
410 parser.add_argument(
411 "-nr", "--new-repo", type=str, help="repository for new version."
412 )
413 parser.add_argument(
414 "-nc", "--new-crypto-rev", type=str,
415 help="revision for new crypto version"
416 )
417 parser.add_argument(
418 "-ncr", "--new-crypto-repo", type=str,
419 help="repository for new crypto submodule."
Darryl Greenae5d66c2019-02-25 11:35:05 +0000420 )
421 parser.add_argument(
Darryl Green668063b2019-02-20 15:01:56 +0000422 "-s", "--skip-file", type=str,
Gilles Peskine826286a2019-07-04 19:00:31 +0200423 help=("path to file containing symbols and types to skip "
424 "(typically \"-s identifiers\" after running "
425 "\"tests/scripts/list-identifiers.sh --internal\")")
Darryl Green668063b2019-02-20 15:01:56 +0000426 )
Darryl Green0da45782019-02-21 13:09:26 +0000427 parser.add_argument(
428 "-b", "--brief", action="store_true",
429 help="output only the list of issues to stdout, instead of a full report",
430 )
Darryl Green7c2dd582018-03-01 14:53:49 +0000431 abi_args = parser.parse_args()
Darryl Green6b3cbfa2019-04-11 15:50:41 +0100432 if os.path.isfile(abi_args.report_dir):
433 print("Error: {} is not a directory".format(abi_args.report_dir))
434 parser.exit()
Darryl Green30dc6d52019-04-09 09:14:17 +0100435 old_version = SimpleNamespace(
436 version="old",
437 repository=abi_args.old_repo,
438 revision=abi_args.old_rev,
Gilles Peskineea943912019-07-04 19:01:22 +0200439 commit=None,
Darryl Green30dc6d52019-04-09 09:14:17 +0100440 crypto_repository=abi_args.old_crypto_repo,
441 crypto_revision=abi_args.old_crypto_rev,
442 abi_dumps={},
443 modules={}
Darryl Green26dff8e2019-04-05 17:06:17 +0100444 )
Darryl Green30dc6d52019-04-09 09:14:17 +0100445 new_version = SimpleNamespace(
446 version="new",
447 repository=abi_args.new_repo,
448 revision=abi_args.new_rev,
Gilles Peskineea943912019-07-04 19:01:22 +0200449 commit=None,
Darryl Green30dc6d52019-04-09 09:14:17 +0100450 crypto_repository=abi_args.new_crypto_repo,
451 crypto_revision=abi_args.new_crypto_rev,
452 abi_dumps={},
453 modules={}
Darryl Green26dff8e2019-04-05 17:06:17 +0100454 )
Darryl Green30dc6d52019-04-09 09:14:17 +0100455 configuration = SimpleNamespace(
456 verbose=abi_args.verbose,
457 report_dir=abi_args.report_dir,
458 keep_all_reports=abi_args.keep_all_reports,
459 brief=abi_args.brief,
460 skip_file=abi_args.skip_file
Darryl Green7c2dd582018-03-01 14:53:49 +0000461 )
Darryl Green30dc6d52019-04-09 09:14:17 +0100462 abi_check = AbiChecker(old_version, new_version, configuration)
Darryl Green7c2dd582018-03-01 14:53:49 +0000463 return_code = abi_check.check_for_abi_changes()
464 sys.exit(return_code)
Gilles Peskineafd19dd2019-02-25 21:39:42 +0100465 except Exception: # pylint: disable=broad-except
466 # Print the backtrace and exit explicitly so as to exit with
467 # status 2, not 1.
Darryl Greena6f430f2018-03-15 10:12:06 +0000468 traceback.print_exc()
Darryl Green7c2dd582018-03-01 14:53:49 +0000469 sys.exit(2)
470
471
472if __name__ == "__main__":
473 run_main()