Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Darryl Green | 7869680 | 2018-04-06 11:23:22 +0100 | [diff] [blame] | 2 | """ |
Darryl Green | 7869680 | 2018-04-06 11:23:22 +0100 | [diff] [blame] | 3 | Purpose |
| 4 | |
| 5 | This script is a small wrapper around the abi-compliance-checker and |
| 6 | abi-dumper tools, applying them to compare the ABI and API of the library |
| 7 | files from two different Git revisions within an Mbed TLS repository. |
Darryl Green | e62f9bb | 2019-02-21 13:09:26 +0000 | [diff] [blame] | 8 | The results of the comparison are either formatted as HTML and stored at |
Darryl Green | 4cde8a0 | 2019-03-05 15:21:32 +0000 | [diff] [blame] | 9 | a configurable location, or are given as a brief list of problems. |
Darryl Green | e62f9bb | 2019-02-21 13:09:26 +0000 | [diff] [blame] | 10 | Returns 0 on success, 1 on ABI/API non-compliance, and 2 if there is an error |
| 11 | while running the script. Note: must be run from Mbed TLS root. |
Darryl Green | 7869680 | 2018-04-06 11:23:22 +0100 | [diff] [blame] | 12 | """ |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 13 | |
Bence Szépkúti | c7da1fe | 2020-05-26 01:54:15 +0200 | [diff] [blame] | 14 | # Copyright (c) 2018, Arm Limited, All Rights Reserved |
| 15 | # SPDX-License-Identifier: Apache-2.0 |
| 16 | # |
| 17 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 18 | # not use this file except in compliance with the License. |
| 19 | # You may obtain a copy of the License at |
| 20 | # |
| 21 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 22 | # |
| 23 | # Unless required by applicable law or agreed to in writing, software |
| 24 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 25 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 26 | # See the License for the specific language governing permissions and |
| 27 | # limitations under the License. |
| 28 | # |
| 29 | # This file is part of Mbed TLS (https://tls.mbed.org) |
| 30 | |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 31 | import os |
| 32 | import sys |
| 33 | import traceback |
| 34 | import shutil |
| 35 | import subprocess |
| 36 | import argparse |
| 37 | import logging |
| 38 | import tempfile |
Darryl Green | 9f357d6 | 2019-02-25 11:35:05 +0000 | [diff] [blame] | 39 | import fnmatch |
Darryl Green | 0d1ca51 | 2019-04-09 09:14:17 +0100 | [diff] [blame] | 40 | from types import SimpleNamespace |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 41 | |
Darryl Green | e62f9bb | 2019-02-21 13:09:26 +0000 | [diff] [blame] | 42 | import xml.etree.ElementTree as ET |
| 43 | |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 44 | |
Gilles Peskine | 184c096 | 2020-03-24 18:25:17 +0100 | [diff] [blame] | 45 | class AbiChecker: |
Gilles Peskine | 712afa7 | 2019-02-25 20:36:52 +0100 | [diff] [blame] | 46 | """API and ABI checker.""" |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 47 | |
Darryl Green | 0d1ca51 | 2019-04-09 09:14:17 +0100 | [diff] [blame] | 48 | def __init__(self, old_version, new_version, configuration): |
Gilles Peskine | 712afa7 | 2019-02-25 20:36:52 +0100 | [diff] [blame] | 49 | """Instantiate the API/ABI checker. |
| 50 | |
Darryl Green | 7c1a733 | 2019-03-05 16:25:38 +0000 | [diff] [blame] | 51 | old_version: RepoVersion containing details to compare against |
| 52 | new_version: RepoVersion containing details to check |
Darryl Green | f67e349 | 2019-04-12 15:17:02 +0100 | [diff] [blame] | 53 | configuration.report_dir: directory for output files |
| 54 | configuration.keep_all_reports: if false, delete old reports |
| 55 | configuration.brief: if true, output shorter report to stdout |
| 56 | configuration.skip_file: path to file containing symbols and types to skip |
Gilles Peskine | 712afa7 | 2019-02-25 20:36:52 +0100 | [diff] [blame] | 57 | """ |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 58 | self.repo_path = "." |
| 59 | self.log = None |
Darryl Green | 0d1ca51 | 2019-04-09 09:14:17 +0100 | [diff] [blame] | 60 | self.verbose = configuration.verbose |
Darryl Green | 3a5f6c8 | 2019-03-05 16:30:39 +0000 | [diff] [blame] | 61 | self._setup_logger() |
Darryl Green | 0d1ca51 | 2019-04-09 09:14:17 +0100 | [diff] [blame] | 62 | self.report_dir = os.path.abspath(configuration.report_dir) |
| 63 | self.keep_all_reports = configuration.keep_all_reports |
Darryl Green | 492bc40 | 2019-04-11 15:50:41 +0100 | [diff] [blame] | 64 | self.can_remove_report_dir = not (os.path.exists(self.report_dir) or |
Darryl Green | 0d1ca51 | 2019-04-09 09:14:17 +0100 | [diff] [blame] | 65 | self.keep_all_reports) |
Darryl Green | 7c1a733 | 2019-03-05 16:25:38 +0000 | [diff] [blame] | 66 | self.old_version = old_version |
| 67 | self.new_version = new_version |
Darryl Green | 0d1ca51 | 2019-04-09 09:14:17 +0100 | [diff] [blame] | 68 | self.skip_file = configuration.skip_file |
| 69 | self.brief = configuration.brief |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 70 | self.git_command = "git" |
| 71 | self.make_command = "make" |
| 72 | |
Gilles Peskine | 712afa7 | 2019-02-25 20:36:52 +0100 | [diff] [blame] | 73 | @staticmethod |
| 74 | def check_repo_path(): |
Gilles Peskine | 6aa32cc | 2019-07-04 18:59:36 +0200 | [diff] [blame] | 75 | if not all(os.path.isdir(d) for d in ["include", "library", "tests"]): |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 76 | raise Exception("Must be run from Mbed TLS root") |
| 77 | |
Darryl Green | 3a5f6c8 | 2019-03-05 16:30:39 +0000 | [diff] [blame] | 78 | def _setup_logger(self): |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 79 | self.log = logging.getLogger() |
Darryl Green | 3c3da79 | 2019-03-08 11:30:04 +0000 | [diff] [blame] | 80 | if self.verbose: |
| 81 | self.log.setLevel(logging.DEBUG) |
| 82 | else: |
| 83 | self.log.setLevel(logging.INFO) |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 84 | self.log.addHandler(logging.StreamHandler()) |
| 85 | |
Gilles Peskine | 712afa7 | 2019-02-25 20:36:52 +0100 | [diff] [blame] | 86 | @staticmethod |
| 87 | def check_abi_tools_are_installed(): |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 88 | for command in ["abi-dumper", "abi-compliance-checker"]: |
| 89 | if not shutil.which(command): |
| 90 | raise Exception("{} not installed, aborting".format(command)) |
| 91 | |
Darryl Green | 3a5f6c8 | 2019-03-05 16:30:39 +0000 | [diff] [blame] | 92 | def _get_clean_worktree_for_git_revision(self, version): |
Darryl Green | 7c1a733 | 2019-03-05 16:25:38 +0000 | [diff] [blame] | 93 | """Make a separate worktree with version.revision checked out. |
Gilles Peskine | 712afa7 | 2019-02-25 20:36:52 +0100 | [diff] [blame] | 94 | Do not modify the current worktree.""" |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 95 | git_worktree_path = tempfile.mkdtemp() |
Darryl Green | 7c1a733 | 2019-03-05 16:25:38 +0000 | [diff] [blame] | 96 | if version.repository: |
Darryl Green | 3c3da79 | 2019-03-08 11:30:04 +0000 | [diff] [blame] | 97 | self.log.debug( |
Darryl Green | da84e32 | 2019-02-19 16:59:33 +0000 | [diff] [blame] | 98 | "Checking out git worktree for revision {} from {}".format( |
Darryl Green | 7c1a733 | 2019-03-05 16:25:38 +0000 | [diff] [blame] | 99 | version.revision, version.repository |
Darryl Green | da84e32 | 2019-02-19 16:59:33 +0000 | [diff] [blame] | 100 | ) |
| 101 | ) |
Darryl Green | b2ee0b8 | 2019-04-12 16:24:25 +0100 | [diff] [blame] | 102 | fetch_output = subprocess.check_output( |
Darryl Green | 7c1a733 | 2019-03-05 16:25:38 +0000 | [diff] [blame] | 103 | [self.git_command, "fetch", |
| 104 | version.repository, version.revision], |
Darryl Green | da84e32 | 2019-02-19 16:59:33 +0000 | [diff] [blame] | 105 | cwd=self.repo_path, |
Darryl Green | da84e32 | 2019-02-19 16:59:33 +0000 | [diff] [blame] | 106 | stderr=subprocess.STDOUT |
| 107 | ) |
Darryl Green | 3c3da79 | 2019-03-08 11:30:04 +0000 | [diff] [blame] | 108 | self.log.debug(fetch_output.decode("utf-8")) |
Darryl Green | da84e32 | 2019-02-19 16:59:33 +0000 | [diff] [blame] | 109 | worktree_rev = "FETCH_HEAD" |
| 110 | else: |
Darryl Green | 3c3da79 | 2019-03-08 11:30:04 +0000 | [diff] [blame] | 111 | self.log.debug("Checking out git worktree for revision {}".format( |
Darryl Green | 7c1a733 | 2019-03-05 16:25:38 +0000 | [diff] [blame] | 112 | version.revision |
| 113 | )) |
| 114 | worktree_rev = version.revision |
Darryl Green | b2ee0b8 | 2019-04-12 16:24:25 +0100 | [diff] [blame] | 115 | worktree_output = subprocess.check_output( |
Darryl Green | da84e32 | 2019-02-19 16:59:33 +0000 | [diff] [blame] | 116 | [self.git_command, "worktree", "add", "--detach", |
| 117 | git_worktree_path, worktree_rev], |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 118 | cwd=self.repo_path, |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 119 | stderr=subprocess.STDOUT |
| 120 | ) |
Darryl Green | 3c3da79 | 2019-03-08 11:30:04 +0000 | [diff] [blame] | 121 | self.log.debug(worktree_output.decode("utf-8")) |
Gilles Peskine | 3e2da4a | 2019-07-04 19:01:22 +0200 | [diff] [blame] | 122 | version.commit = subprocess.check_output( |
Darryl Green | 762351b | 2019-07-25 14:33:33 +0100 | [diff] [blame] | 123 | [self.git_command, "rev-parse", "HEAD"], |
Gilles Peskine | 3e2da4a | 2019-07-04 19:01:22 +0200 | [diff] [blame] | 124 | cwd=git_worktree_path, |
| 125 | stderr=subprocess.STDOUT |
| 126 | ).decode("ascii").rstrip() |
| 127 | self.log.debug("Commit is {}".format(version.commit)) |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 128 | return git_worktree_path |
| 129 | |
Darryl Green | 3a5f6c8 | 2019-03-05 16:30:39 +0000 | [diff] [blame] | 130 | def _update_git_submodules(self, git_worktree_path, version): |
Darryl Green | 8184df5 | 2019-04-05 17:06:17 +0100 | [diff] [blame] | 131 | """If the crypto submodule is present, initialize it. |
| 132 | if version.crypto_revision exists, update it to that revision, |
| 133 | otherwise update it to the default revision""" |
Darryl Green | b2ee0b8 | 2019-04-12 16:24:25 +0100 | [diff] [blame] | 134 | update_output = subprocess.check_output( |
Jaeden Amero | ffeb1b8 | 2018-11-02 16:35:09 +0000 | [diff] [blame] | 135 | [self.git_command, "submodule", "update", "--init", '--recursive'], |
| 136 | cwd=git_worktree_path, |
Jaeden Amero | ffeb1b8 | 2018-11-02 16:35:09 +0000 | [diff] [blame] | 137 | stderr=subprocess.STDOUT |
| 138 | ) |
Darryl Green | b2ee0b8 | 2019-04-12 16:24:25 +0100 | [diff] [blame] | 139 | self.log.debug(update_output.decode("utf-8")) |
Darryl Green | e29ce70 | 2019-03-05 15:23:25 +0000 | [diff] [blame] | 140 | if not (os.path.exists(os.path.join(git_worktree_path, "crypto")) |
Darryl Green | 7c1a733 | 2019-03-05 16:25:38 +0000 | [diff] [blame] | 141 | and version.crypto_revision): |
Darryl Green | e29ce70 | 2019-03-05 15:23:25 +0000 | [diff] [blame] | 142 | return |
| 143 | |
Darryl Green | 7c1a733 | 2019-03-05 16:25:38 +0000 | [diff] [blame] | 144 | if version.crypto_repository: |
Darryl Green | b2ee0b8 | 2019-04-12 16:24:25 +0100 | [diff] [blame] | 145 | fetch_output = subprocess.check_output( |
Darryl Green | 1d95c53 | 2019-03-08 11:12:19 +0000 | [diff] [blame] | 146 | [self.git_command, "fetch", version.crypto_repository, |
| 147 | version.crypto_revision], |
Darryl Green | e29ce70 | 2019-03-05 15:23:25 +0000 | [diff] [blame] | 148 | cwd=os.path.join(git_worktree_path, "crypto"), |
Darryl Green | e29ce70 | 2019-03-05 15:23:25 +0000 | [diff] [blame] | 149 | stderr=subprocess.STDOUT |
| 150 | ) |
Darryl Green | 3c3da79 | 2019-03-08 11:30:04 +0000 | [diff] [blame] | 151 | self.log.debug(fetch_output.decode("utf-8")) |
Darryl Green | 1d95c53 | 2019-03-08 11:12:19 +0000 | [diff] [blame] | 152 | crypto_rev = "FETCH_HEAD" |
| 153 | else: |
| 154 | crypto_rev = version.crypto_revision |
| 155 | |
Darryl Green | b2ee0b8 | 2019-04-12 16:24:25 +0100 | [diff] [blame] | 156 | checkout_output = subprocess.check_output( |
Darryl Green | 1d95c53 | 2019-03-08 11:12:19 +0000 | [diff] [blame] | 157 | [self.git_command, "checkout", crypto_rev], |
| 158 | cwd=os.path.join(git_worktree_path, "crypto"), |
Darryl Green | 1d95c53 | 2019-03-08 11:12:19 +0000 | [diff] [blame] | 159 | stderr=subprocess.STDOUT |
| 160 | ) |
Darryl Green | 3c3da79 | 2019-03-08 11:30:04 +0000 | [diff] [blame] | 161 | self.log.debug(checkout_output.decode("utf-8")) |
Jaeden Amero | ffeb1b8 | 2018-11-02 16:35:09 +0000 | [diff] [blame] | 162 | |
Darryl Green | 3a5f6c8 | 2019-03-05 16:30:39 +0000 | [diff] [blame] | 163 | def _build_shared_libraries(self, git_worktree_path, version): |
Gilles Peskine | 712afa7 | 2019-02-25 20:36:52 +0100 | [diff] [blame] | 164 | """Build the shared libraries in the specified worktree.""" |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 165 | my_environment = os.environ.copy() |
| 166 | my_environment["CFLAGS"] = "-g -Og" |
| 167 | my_environment["SHARED"] = "1" |
Darryl Green | d2dba36 | 2019-05-09 13:03:05 +0100 | [diff] [blame] | 168 | if os.path.exists(os.path.join(git_worktree_path, "crypto")): |
| 169 | my_environment["USE_CRYPTO_SUBMODULE"] = "1" |
Darryl Green | b2ee0b8 | 2019-04-12 16:24:25 +0100 | [diff] [blame] | 170 | make_output = subprocess.check_output( |
Darryl Green | ddf25a6 | 2019-02-28 11:52:39 +0000 | [diff] [blame] | 171 | [self.make_command, "lib"], |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 172 | env=my_environment, |
| 173 | cwd=git_worktree_path, |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 174 | stderr=subprocess.STDOUT |
| 175 | ) |
Darryl Green | 3c3da79 | 2019-03-08 11:30:04 +0000 | [diff] [blame] | 176 | self.log.debug(make_output.decode("utf-8")) |
Darryl Green | f025d53 | 2019-04-12 15:18:02 +0100 | [diff] [blame] | 177 | for root, _dirs, files in os.walk(git_worktree_path): |
Darryl Green | 9f357d6 | 2019-02-25 11:35:05 +0000 | [diff] [blame] | 178 | for file in fnmatch.filter(files, "*.so"): |
Darryl Green | 7c1a733 | 2019-03-05 16:25:38 +0000 | [diff] [blame] | 179 | version.modules[os.path.splitext(file)[0]] = ( |
Darryl Green | 3e7a980 | 2019-02-27 16:53:40 +0000 | [diff] [blame] | 180 | os.path.join(root, file) |
Darryl Green | 9f357d6 | 2019-02-25 11:35:05 +0000 | [diff] [blame] | 181 | ) |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 182 | |
Gilles Peskine | 3e2da4a | 2019-07-04 19:01:22 +0200 | [diff] [blame] | 183 | @staticmethod |
| 184 | def _pretty_revision(version): |
| 185 | if version.revision == version.commit: |
| 186 | return version.revision |
| 187 | else: |
| 188 | return "{} ({})".format(version.revision, version.commit) |
| 189 | |
Darryl Green | 8184df5 | 2019-04-05 17:06:17 +0100 | [diff] [blame] | 190 | def _get_abi_dumps_from_shared_libraries(self, version): |
Gilles Peskine | 712afa7 | 2019-02-25 20:36:52 +0100 | [diff] [blame] | 191 | """Generate the ABI dumps for the specified git revision. |
Darryl Green | 8184df5 | 2019-04-05 17:06:17 +0100 | [diff] [blame] | 192 | The shared libraries must have been built and the module paths |
| 193 | present in version.modules.""" |
Darryl Green | 7c1a733 | 2019-03-05 16:25:38 +0000 | [diff] [blame] | 194 | for mbed_module, module_path in version.modules.items(): |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 195 | output_path = os.path.join( |
Darryl Green | fe9a675 | 2019-04-04 14:39:33 +0100 | [diff] [blame] | 196 | self.report_dir, "{}-{}-{}.dump".format( |
| 197 | mbed_module, version.revision, version.version |
Darryl Green | 3e7a980 | 2019-02-27 16:53:40 +0000 | [diff] [blame] | 198 | ) |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 199 | ) |
| 200 | abi_dump_command = [ |
| 201 | "abi-dumper", |
Darryl Green | 9f357d6 | 2019-02-25 11:35:05 +0000 | [diff] [blame] | 202 | module_path, |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 203 | "-o", output_path, |
Gilles Peskine | 3e2da4a | 2019-07-04 19:01:22 +0200 | [diff] [blame] | 204 | "-lver", self._pretty_revision(version), |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 205 | ] |
Darryl Green | b2ee0b8 | 2019-04-12 16:24:25 +0100 | [diff] [blame] | 206 | abi_dump_output = subprocess.check_output( |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 207 | abi_dump_command, |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 208 | stderr=subprocess.STDOUT |
| 209 | ) |
Darryl Green | 3c3da79 | 2019-03-08 11:30:04 +0000 | [diff] [blame] | 210 | self.log.debug(abi_dump_output.decode("utf-8")) |
Darryl Green | 7c1a733 | 2019-03-05 16:25:38 +0000 | [diff] [blame] | 211 | version.abi_dumps[mbed_module] = output_path |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 212 | |
Darryl Green | 3a5f6c8 | 2019-03-05 16:30:39 +0000 | [diff] [blame] | 213 | def _cleanup_worktree(self, git_worktree_path): |
Gilles Peskine | 712afa7 | 2019-02-25 20:36:52 +0100 | [diff] [blame] | 214 | """Remove the specified git worktree.""" |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 215 | shutil.rmtree(git_worktree_path) |
Darryl Green | b2ee0b8 | 2019-04-12 16:24:25 +0100 | [diff] [blame] | 216 | worktree_output = subprocess.check_output( |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 217 | [self.git_command, "worktree", "prune"], |
| 218 | cwd=self.repo_path, |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 219 | stderr=subprocess.STDOUT |
| 220 | ) |
Darryl Green | 3c3da79 | 2019-03-08 11:30:04 +0000 | [diff] [blame] | 221 | self.log.debug(worktree_output.decode("utf-8")) |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 222 | |
Darryl Green | 3a5f6c8 | 2019-03-05 16:30:39 +0000 | [diff] [blame] | 223 | def _get_abi_dump_for_ref(self, version): |
Gilles Peskine | 712afa7 | 2019-02-25 20:36:52 +0100 | [diff] [blame] | 224 | """Generate the ABI dumps for the specified git revision.""" |
Darryl Green | 3a5f6c8 | 2019-03-05 16:30:39 +0000 | [diff] [blame] | 225 | git_worktree_path = self._get_clean_worktree_for_git_revision(version) |
| 226 | self._update_git_submodules(git_worktree_path, version) |
| 227 | self._build_shared_libraries(git_worktree_path, version) |
Darryl Green | 8184df5 | 2019-04-05 17:06:17 +0100 | [diff] [blame] | 228 | self._get_abi_dumps_from_shared_libraries(version) |
Darryl Green | 3a5f6c8 | 2019-03-05 16:30:39 +0000 | [diff] [blame] | 229 | self._cleanup_worktree(git_worktree_path) |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 230 | |
Darryl Green | 3a5f6c8 | 2019-03-05 16:30:39 +0000 | [diff] [blame] | 231 | def _remove_children_with_tag(self, parent, tag): |
Darryl Green | e62f9bb | 2019-02-21 13:09:26 +0000 | [diff] [blame] | 232 | children = parent.getchildren() |
| 233 | for child in children: |
| 234 | if child.tag == tag: |
| 235 | parent.remove(child) |
| 236 | else: |
Darryl Green | 3a5f6c8 | 2019-03-05 16:30:39 +0000 | [diff] [blame] | 237 | self._remove_children_with_tag(child, tag) |
Darryl Green | e62f9bb | 2019-02-21 13:09:26 +0000 | [diff] [blame] | 238 | |
Darryl Green | 3a5f6c8 | 2019-03-05 16:30:39 +0000 | [diff] [blame] | 239 | def _remove_extra_detail_from_report(self, report_root): |
Darryl Green | e62f9bb | 2019-02-21 13:09:26 +0000 | [diff] [blame] | 240 | for tag in ['test_info', 'test_results', 'problem_summary', |
Darryl Green | c6f874b | 2019-06-05 12:57:50 +0100 | [diff] [blame] | 241 | 'added_symbols', 'affected']: |
Darryl Green | 3a5f6c8 | 2019-03-05 16:30:39 +0000 | [diff] [blame] | 242 | self._remove_children_with_tag(report_root, tag) |
Darryl Green | e62f9bb | 2019-02-21 13:09:26 +0000 | [diff] [blame] | 243 | |
| 244 | for report in report_root: |
| 245 | for problems in report.getchildren()[:]: |
| 246 | if not problems.getchildren(): |
| 247 | report.remove(problems) |
| 248 | |
Gilles Peskine | ada828f | 2019-07-04 19:17:40 +0200 | [diff] [blame] | 249 | def _abi_compliance_command(self, mbed_module, output_path): |
| 250 | """Build the command to run to analyze the library mbed_module. |
| 251 | The report will be placed in output_path.""" |
| 252 | abi_compliance_command = [ |
| 253 | "abi-compliance-checker", |
| 254 | "-l", mbed_module, |
| 255 | "-old", self.old_version.abi_dumps[mbed_module], |
| 256 | "-new", self.new_version.abi_dumps[mbed_module], |
| 257 | "-strict", |
| 258 | "-report-path", output_path, |
| 259 | ] |
| 260 | if self.skip_file: |
| 261 | abi_compliance_command += ["-skip-symbols", self.skip_file, |
| 262 | "-skip-types", self.skip_file] |
| 263 | if self.brief: |
| 264 | abi_compliance_command += ["-report-format", "xml", |
| 265 | "-stdout"] |
| 266 | return abi_compliance_command |
| 267 | |
| 268 | def _is_library_compatible(self, mbed_module, compatibility_report): |
| 269 | """Test if the library mbed_module has remained compatible. |
| 270 | Append a message regarding compatibility to compatibility_report.""" |
| 271 | output_path = os.path.join( |
| 272 | self.report_dir, "{}-{}-{}.html".format( |
| 273 | mbed_module, self.old_version.revision, |
| 274 | self.new_version.revision |
| 275 | ) |
| 276 | ) |
| 277 | try: |
| 278 | subprocess.check_output( |
| 279 | self._abi_compliance_command(mbed_module, output_path), |
| 280 | stderr=subprocess.STDOUT |
| 281 | ) |
| 282 | except subprocess.CalledProcessError as err: |
| 283 | if err.returncode != 1: |
| 284 | raise err |
| 285 | if self.brief: |
| 286 | self.log.info( |
| 287 | "Compatibility issues found for {}".format(mbed_module) |
| 288 | ) |
| 289 | report_root = ET.fromstring(err.output.decode("utf-8")) |
| 290 | self._remove_extra_detail_from_report(report_root) |
| 291 | self.log.info(ET.tostring(report_root).decode("utf-8")) |
| 292 | else: |
| 293 | self.can_remove_report_dir = False |
| 294 | compatibility_report.append( |
| 295 | "Compatibility issues found for {}, " |
| 296 | "for details see {}".format(mbed_module, output_path) |
| 297 | ) |
| 298 | return False |
| 299 | compatibility_report.append( |
| 300 | "No compatibility issues for {}".format(mbed_module) |
| 301 | ) |
| 302 | if not (self.keep_all_reports or self.brief): |
| 303 | os.remove(output_path) |
| 304 | return True |
| 305 | |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 306 | def get_abi_compatibility_report(self): |
Gilles Peskine | 712afa7 | 2019-02-25 20:36:52 +0100 | [diff] [blame] | 307 | """Generate a report of the differences between the reference ABI |
Darryl Green | 8184df5 | 2019-04-05 17:06:17 +0100 | [diff] [blame] | 308 | and the new ABI. ABI dumps from self.old_version and self.new_version |
| 309 | must be available.""" |
Gilles Peskine | ada828f | 2019-07-04 19:17:40 +0200 | [diff] [blame] | 310 | compatibility_report = ["Checking evolution from {} to {}".format( |
Gilles Peskine | 3e2da4a | 2019-07-04 19:01:22 +0200 | [diff] [blame] | 311 | self._pretty_revision(self.old_version), |
| 312 | self._pretty_revision(self.new_version) |
Gilles Peskine | ada828f | 2019-07-04 19:17:40 +0200 | [diff] [blame] | 313 | )] |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 314 | compliance_return_code = 0 |
Darryl Green | 7c1a733 | 2019-03-05 16:25:38 +0000 | [diff] [blame] | 315 | shared_modules = list(set(self.old_version.modules.keys()) & |
| 316 | set(self.new_version.modules.keys())) |
Darryl Green | 3e7a980 | 2019-02-27 16:53:40 +0000 | [diff] [blame] | 317 | for mbed_module in shared_modules: |
Gilles Peskine | ada828f | 2019-07-04 19:17:40 +0200 | [diff] [blame] | 318 | if not self._is_library_compatible(mbed_module, |
| 319 | compatibility_report): |
| 320 | compliance_return_code = 1 |
Darryl Green | f2688e2 | 2019-05-29 11:29:08 +0100 | [diff] [blame] | 321 | for version in [self.old_version, self.new_version]: |
| 322 | for mbed_module, mbed_module_dump in version.abi_dumps.items(): |
| 323 | os.remove(mbed_module_dump) |
Darryl Green | 3d3d552 | 2019-02-25 17:01:55 +0000 | [diff] [blame] | 324 | if self.can_remove_report_dir: |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 325 | os.rmdir(self.report_dir) |
Gilles Peskine | ada828f | 2019-07-04 19:17:40 +0200 | [diff] [blame] | 326 | self.log.info("\n".join(compatibility_report)) |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 327 | return compliance_return_code |
| 328 | |
| 329 | def check_for_abi_changes(self): |
Gilles Peskine | 712afa7 | 2019-02-25 20:36:52 +0100 | [diff] [blame] | 330 | """Generate a report of ABI differences |
| 331 | between self.old_rev and self.new_rev.""" |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 332 | self.check_repo_path() |
| 333 | self.check_abi_tools_are_installed() |
Darryl Green | 3a5f6c8 | 2019-03-05 16:30:39 +0000 | [diff] [blame] | 334 | self._get_abi_dump_for_ref(self.old_version) |
| 335 | self._get_abi_dump_for_ref(self.new_version) |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 336 | return self.get_abi_compatibility_report() |
| 337 | |
| 338 | |
| 339 | def run_main(): |
| 340 | try: |
| 341 | parser = argparse.ArgumentParser( |
| 342 | description=( |
Darryl Green | 418527b | 2018-04-16 12:02:29 +0100 | [diff] [blame] | 343 | """This script is a small wrapper around the |
| 344 | abi-compliance-checker and abi-dumper tools, applying them |
| 345 | to compare the ABI and API of the library files from two |
| 346 | different Git revisions within an Mbed TLS repository. |
Darryl Green | e62f9bb | 2019-02-21 13:09:26 +0000 | [diff] [blame] | 347 | The results of the comparison are either formatted as HTML and |
Darryl Green | 4cde8a0 | 2019-03-05 15:21:32 +0000 | [diff] [blame] | 348 | stored at a configurable location, or are given as a brief list |
| 349 | of problems. Returns 0 on success, 1 on ABI/API non-compliance, |
| 350 | and 2 if there is an error while running the script. |
| 351 | Note: must be run from Mbed TLS root.""" |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 352 | ) |
| 353 | ) |
| 354 | parser.add_argument( |
Darryl Green | 3c3da79 | 2019-03-08 11:30:04 +0000 | [diff] [blame] | 355 | "-v", "--verbose", action="store_true", |
| 356 | help="set verbosity level", |
| 357 | ) |
| 358 | parser.add_argument( |
Darryl Green | 418527b | 2018-04-16 12:02:29 +0100 | [diff] [blame] | 359 | "-r", "--report-dir", type=str, default="reports", |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 360 | help="directory where reports are stored, default is reports", |
| 361 | ) |
| 362 | parser.add_argument( |
Darryl Green | 418527b | 2018-04-16 12:02:29 +0100 | [diff] [blame] | 363 | "-k", "--keep-all-reports", action="store_true", |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 364 | help="keep all reports, even if there are no compatibility issues", |
| 365 | ) |
| 366 | parser.add_argument( |
Darryl Green | c5132ff | 2019-03-01 09:54:44 +0000 | [diff] [blame] | 367 | "-o", "--old-rev", type=str, help="revision for old version.", |
| 368 | required=True, |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 369 | ) |
| 370 | parser.add_argument( |
Darryl Green | c5132ff | 2019-03-01 09:54:44 +0000 | [diff] [blame] | 371 | "-or", "--old-repo", type=str, help="repository for old version." |
Darryl Green | 9f357d6 | 2019-02-25 11:35:05 +0000 | [diff] [blame] | 372 | ) |
| 373 | parser.add_argument( |
Darryl Green | c5132ff | 2019-03-01 09:54:44 +0000 | [diff] [blame] | 374 | "-oc", "--old-crypto-rev", type=str, |
| 375 | help="revision for old crypto submodule." |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 376 | ) |
Darryl Green | c2883a2 | 2019-02-20 15:01:56 +0000 | [diff] [blame] | 377 | parser.add_argument( |
Darryl Green | c5132ff | 2019-03-01 09:54:44 +0000 | [diff] [blame] | 378 | "-ocr", "--old-crypto-repo", type=str, |
| 379 | help="repository for old crypto submodule." |
| 380 | ) |
| 381 | parser.add_argument( |
| 382 | "-n", "--new-rev", type=str, help="revision for new version", |
| 383 | required=True, |
| 384 | ) |
| 385 | parser.add_argument( |
| 386 | "-nr", "--new-repo", type=str, help="repository for new version." |
| 387 | ) |
| 388 | parser.add_argument( |
| 389 | "-nc", "--new-crypto-rev", type=str, |
| 390 | help="revision for new crypto version" |
| 391 | ) |
| 392 | parser.add_argument( |
| 393 | "-ncr", "--new-crypto-repo", type=str, |
| 394 | help="repository for new crypto submodule." |
Darryl Green | 9f357d6 | 2019-02-25 11:35:05 +0000 | [diff] [blame] | 395 | ) |
| 396 | parser.add_argument( |
Darryl Green | c2883a2 | 2019-02-20 15:01:56 +0000 | [diff] [blame] | 397 | "-s", "--skip-file", type=str, |
Gilles Peskine | b6ce234 | 2019-07-04 19:00:31 +0200 | [diff] [blame] | 398 | help=("path to file containing symbols and types to skip " |
| 399 | "(typically \"-s identifiers\" after running " |
| 400 | "\"tests/scripts/list-identifiers.sh --internal\")") |
Darryl Green | c2883a2 | 2019-02-20 15:01:56 +0000 | [diff] [blame] | 401 | ) |
Darryl Green | e62f9bb | 2019-02-21 13:09:26 +0000 | [diff] [blame] | 402 | parser.add_argument( |
| 403 | "-b", "--brief", action="store_true", |
| 404 | help="output only the list of issues to stdout, instead of a full report", |
| 405 | ) |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 406 | abi_args = parser.parse_args() |
Darryl Green | 492bc40 | 2019-04-11 15:50:41 +0100 | [diff] [blame] | 407 | if os.path.isfile(abi_args.report_dir): |
| 408 | print("Error: {} is not a directory".format(abi_args.report_dir)) |
| 409 | parser.exit() |
Darryl Green | 0d1ca51 | 2019-04-09 09:14:17 +0100 | [diff] [blame] | 410 | old_version = SimpleNamespace( |
| 411 | version="old", |
| 412 | repository=abi_args.old_repo, |
| 413 | revision=abi_args.old_rev, |
Gilles Peskine | 3e2da4a | 2019-07-04 19:01:22 +0200 | [diff] [blame] | 414 | commit=None, |
Darryl Green | 0d1ca51 | 2019-04-09 09:14:17 +0100 | [diff] [blame] | 415 | crypto_repository=abi_args.old_crypto_repo, |
| 416 | crypto_revision=abi_args.old_crypto_rev, |
| 417 | abi_dumps={}, |
| 418 | modules={} |
Darryl Green | 8184df5 | 2019-04-05 17:06:17 +0100 | [diff] [blame] | 419 | ) |
Darryl Green | 0d1ca51 | 2019-04-09 09:14:17 +0100 | [diff] [blame] | 420 | new_version = SimpleNamespace( |
| 421 | version="new", |
| 422 | repository=abi_args.new_repo, |
| 423 | revision=abi_args.new_rev, |
Gilles Peskine | 3e2da4a | 2019-07-04 19:01:22 +0200 | [diff] [blame] | 424 | commit=None, |
Darryl Green | 0d1ca51 | 2019-04-09 09:14:17 +0100 | [diff] [blame] | 425 | crypto_repository=abi_args.new_crypto_repo, |
| 426 | crypto_revision=abi_args.new_crypto_rev, |
| 427 | abi_dumps={}, |
| 428 | modules={} |
Darryl Green | 8184df5 | 2019-04-05 17:06:17 +0100 | [diff] [blame] | 429 | ) |
Darryl Green | 0d1ca51 | 2019-04-09 09:14:17 +0100 | [diff] [blame] | 430 | configuration = SimpleNamespace( |
| 431 | verbose=abi_args.verbose, |
| 432 | report_dir=abi_args.report_dir, |
| 433 | keep_all_reports=abi_args.keep_all_reports, |
| 434 | brief=abi_args.brief, |
| 435 | skip_file=abi_args.skip_file |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 436 | ) |
Darryl Green | 0d1ca51 | 2019-04-09 09:14:17 +0100 | [diff] [blame] | 437 | abi_check = AbiChecker(old_version, new_version, configuration) |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 438 | return_code = abi_check.check_for_abi_changes() |
| 439 | sys.exit(return_code) |
Gilles Peskine | e915d53 | 2019-02-25 21:39:42 +0100 | [diff] [blame] | 440 | except Exception: # pylint: disable=broad-except |
| 441 | # Print the backtrace and exit explicitly so as to exit with |
| 442 | # status 2, not 1. |
Darryl Green | a6f430f | 2018-03-15 10:12:06 +0000 | [diff] [blame] | 443 | traceback.print_exc() |
Darryl Green | 7c2dd58 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 444 | sys.exit(2) |
| 445 | |
| 446 | |
| 447 | if __name__ == "__main__": |
| 448 | run_main() |