Darryl Green | 3da1504 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Darryl Green | 4cd7a9b | 2018-04-06 11:23:22 +0100 | [diff] [blame] | 2 | """ |
Darryl Green | 4cd7a9b | 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 | 32e7a50 | 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 | 765d20d | 2019-03-05 15:21:32 +0000 | [diff] [blame] | 9 | a configurable location, or are given as a brief list of problems. |
Darryl Green | 32e7a50 | 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 | 4cd7a9b | 2018-04-06 11:23:22 +0100 | [diff] [blame] | 12 | """ |
Darryl Green | 3da1504 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 13 | |
Bence Szépkúti | 44bfbe3 | 2020-08-19 16:54:51 +0200 | [diff] [blame] | 14 | # Copyright The Mbed TLS Contributors |
Bence Szépkúti | 4e9f712 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 15 | # 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úti | 09b4f19 | 2020-05-26 01:54:15 +0200 | [diff] [blame] | 22 | # |
| 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úti | 4e9f712 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 35 | # ********** |
| 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úti | 09b4f19 | 2020-05-26 01:54:15 +0200 | [diff] [blame] | 55 | |
Darryl Green | 3da1504 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 56 | import os |
| 57 | import sys |
| 58 | import traceback |
| 59 | import shutil |
| 60 | import subprocess |
| 61 | import argparse |
| 62 | import logging |
| 63 | import tempfile |
Darryl Green | d9ad9ec | 2019-02-25 11:35:05 +0000 | [diff] [blame] | 64 | import fnmatch |
Darryl Green | f1d272d | 2019-04-09 09:14:17 +0100 | [diff] [blame] | 65 | from types import SimpleNamespace |
Darryl Green | 3da1504 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 66 | |
Darryl Green | 32e7a50 | 2019-02-21 13:09:26 +0000 | [diff] [blame] | 67 | import xml.etree.ElementTree as ET |
| 68 | |
Darryl Green | 3da1504 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 69 | |
Gilles Peskine | b5847d2 | 2020-03-24 18:25:17 +0100 | [diff] [blame] | 70 | class AbiChecker: |
Gilles Peskine | fceb4ce | 2019-02-25 20:36:52 +0100 | [diff] [blame] | 71 | """API and ABI checker.""" |
Darryl Green | 3da1504 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 72 | |
Darryl Green | f1d272d | 2019-04-09 09:14:17 +0100 | [diff] [blame] | 73 | def __init__(self, old_version, new_version, configuration): |
Gilles Peskine | fceb4ce | 2019-02-25 20:36:52 +0100 | [diff] [blame] | 74 | """Instantiate the API/ABI checker. |
| 75 | |
Darryl Green | 02b6865 | 2019-03-05 16:25:38 +0000 | [diff] [blame] | 76 | old_version: RepoVersion containing details to compare against |
| 77 | new_version: RepoVersion containing details to check |
Darryl Green | bbc6ccf | 2019-04-12 15:17:02 +0100 | [diff] [blame] | 78 | 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 Peskine | fceb4ce | 2019-02-25 20:36:52 +0100 | [diff] [blame] | 82 | """ |
Darryl Green | 3da1504 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 83 | self.repo_path = "." |
| 84 | self.log = None |
Darryl Green | f1d272d | 2019-04-09 09:14:17 +0100 | [diff] [blame] | 85 | self.verbose = configuration.verbose |
Darryl Green | 7bb9cb5 | 2019-03-05 16:30:39 +0000 | [diff] [blame] | 86 | self._setup_logger() |
Darryl Green | f1d272d | 2019-04-09 09:14:17 +0100 | [diff] [blame] | 87 | self.report_dir = os.path.abspath(configuration.report_dir) |
| 88 | self.keep_all_reports = configuration.keep_all_reports |
Darryl Green | 03625fe | 2019-04-11 15:50:41 +0100 | [diff] [blame] | 89 | self.can_remove_report_dir = not (os.path.exists(self.report_dir) or |
Darryl Green | f1d272d | 2019-04-09 09:14:17 +0100 | [diff] [blame] | 90 | self.keep_all_reports) |
Darryl Green | 02b6865 | 2019-03-05 16:25:38 +0000 | [diff] [blame] | 91 | self.old_version = old_version |
| 92 | self.new_version = new_version |
Darryl Green | f1d272d | 2019-04-09 09:14:17 +0100 | [diff] [blame] | 93 | self.skip_file = configuration.skip_file |
| 94 | self.brief = configuration.brief |
Darryl Green | 3da1504 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 95 | self.git_command = "git" |
| 96 | self.make_command = "make" |
| 97 | |
Gilles Peskine | fceb4ce | 2019-02-25 20:36:52 +0100 | [diff] [blame] | 98 | @staticmethod |
| 99 | def check_repo_path(): |
Gilles Peskine | 16dfdb3 | 2019-07-04 18:59:36 +0200 | [diff] [blame] | 100 | if not all(os.path.isdir(d) for d in ["include", "library", "tests"]): |
Darryl Green | 3da1504 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 101 | raise Exception("Must be run from Mbed TLS root") |
| 102 | |
Darryl Green | 7bb9cb5 | 2019-03-05 16:30:39 +0000 | [diff] [blame] | 103 | def _setup_logger(self): |
Darryl Green | 3da1504 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 104 | self.log = logging.getLogger() |
Darryl Green | f0f9f7f | 2019-03-08 11:30:04 +0000 | [diff] [blame] | 105 | if self.verbose: |
| 106 | self.log.setLevel(logging.DEBUG) |
| 107 | else: |
| 108 | self.log.setLevel(logging.INFO) |
Darryl Green | 3da1504 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 109 | self.log.addHandler(logging.StreamHandler()) |
| 110 | |
Gilles Peskine | fceb4ce | 2019-02-25 20:36:52 +0100 | [diff] [blame] | 111 | @staticmethod |
| 112 | def check_abi_tools_are_installed(): |
Darryl Green | 3da1504 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 113 | for command in ["abi-dumper", "abi-compliance-checker"]: |
| 114 | if not shutil.which(command): |
| 115 | raise Exception("{} not installed, aborting".format(command)) |
| 116 | |
Darryl Green | 7bb9cb5 | 2019-03-05 16:30:39 +0000 | [diff] [blame] | 117 | def _get_clean_worktree_for_git_revision(self, version): |
Darryl Green | 02b6865 | 2019-03-05 16:25:38 +0000 | [diff] [blame] | 118 | """Make a separate worktree with version.revision checked out. |
Gilles Peskine | fceb4ce | 2019-02-25 20:36:52 +0100 | [diff] [blame] | 119 | Do not modify the current worktree.""" |
Darryl Green | 3da1504 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 120 | git_worktree_path = tempfile.mkdtemp() |
Darryl Green | 02b6865 | 2019-03-05 16:25:38 +0000 | [diff] [blame] | 121 | if version.repository: |
Darryl Green | f0f9f7f | 2019-03-08 11:30:04 +0000 | [diff] [blame] | 122 | self.log.debug( |
Darryl Green | 834ebc4 | 2019-02-19 16:59:33 +0000 | [diff] [blame] | 123 | "Checking out git worktree for revision {} from {}".format( |
Darryl Green | 02b6865 | 2019-03-05 16:25:38 +0000 | [diff] [blame] | 124 | version.revision, version.repository |
Darryl Green | 834ebc4 | 2019-02-19 16:59:33 +0000 | [diff] [blame] | 125 | ) |
| 126 | ) |
Darryl Green | 4a483e4 | 2019-04-12 16:24:25 +0100 | [diff] [blame] | 127 | fetch_output = subprocess.check_output( |
Darryl Green | 02b6865 | 2019-03-05 16:25:38 +0000 | [diff] [blame] | 128 | [self.git_command, "fetch", |
| 129 | version.repository, version.revision], |
Darryl Green | 834ebc4 | 2019-02-19 16:59:33 +0000 | [diff] [blame] | 130 | cwd=self.repo_path, |
Darryl Green | 834ebc4 | 2019-02-19 16:59:33 +0000 | [diff] [blame] | 131 | stderr=subprocess.STDOUT |
| 132 | ) |
Darryl Green | f0f9f7f | 2019-03-08 11:30:04 +0000 | [diff] [blame] | 133 | self.log.debug(fetch_output.decode("utf-8")) |
Darryl Green | 834ebc4 | 2019-02-19 16:59:33 +0000 | [diff] [blame] | 134 | worktree_rev = "FETCH_HEAD" |
| 135 | else: |
Darryl Green | f0f9f7f | 2019-03-08 11:30:04 +0000 | [diff] [blame] | 136 | self.log.debug("Checking out git worktree for revision {}".format( |
Darryl Green | 02b6865 | 2019-03-05 16:25:38 +0000 | [diff] [blame] | 137 | version.revision |
| 138 | )) |
| 139 | worktree_rev = version.revision |
Darryl Green | 4a483e4 | 2019-04-12 16:24:25 +0100 | [diff] [blame] | 140 | worktree_output = subprocess.check_output( |
Darryl Green | 834ebc4 | 2019-02-19 16:59:33 +0000 | [diff] [blame] | 141 | [self.git_command, "worktree", "add", "--detach", |
| 142 | git_worktree_path, worktree_rev], |
Darryl Green | 3da1504 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 143 | cwd=self.repo_path, |
Darryl Green | 3da1504 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 144 | stderr=subprocess.STDOUT |
| 145 | ) |
Darryl Green | f0f9f7f | 2019-03-08 11:30:04 +0000 | [diff] [blame] | 146 | self.log.debug(worktree_output.decode("utf-8")) |
Gilles Peskine | 2b3f134 | 2019-07-04 19:01:22 +0200 | [diff] [blame] | 147 | version.commit = subprocess.check_output( |
Darryl Green | e3a7c9c | 2019-07-25 14:33:33 +0100 | [diff] [blame] | 148 | [self.git_command, "rev-parse", "HEAD"], |
Gilles Peskine | 2b3f134 | 2019-07-04 19:01:22 +0200 | [diff] [blame] | 149 | cwd=git_worktree_path, |
| 150 | stderr=subprocess.STDOUT |
| 151 | ).decode("ascii").rstrip() |
| 152 | self.log.debug("Commit is {}".format(version.commit)) |
Darryl Green | 3da1504 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 153 | return git_worktree_path |
| 154 | |
Darryl Green | 7bb9cb5 | 2019-03-05 16:30:39 +0000 | [diff] [blame] | 155 | def _update_git_submodules(self, git_worktree_path, version): |
Darryl Green | b7447e7 | 2019-04-05 17:06:17 +0100 | [diff] [blame] | 156 | """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 Green | 4a483e4 | 2019-04-12 16:24:25 +0100 | [diff] [blame] | 159 | update_output = subprocess.check_output( |
Jaeden Amero | 346f959 | 2018-11-02 16:35:09 +0000 | [diff] [blame] | 160 | [self.git_command, "submodule", "update", "--init", '--recursive'], |
| 161 | cwd=git_worktree_path, |
Jaeden Amero | 346f959 | 2018-11-02 16:35:09 +0000 | [diff] [blame] | 162 | stderr=subprocess.STDOUT |
| 163 | ) |
Darryl Green | 4a483e4 | 2019-04-12 16:24:25 +0100 | [diff] [blame] | 164 | self.log.debug(update_output.decode("utf-8")) |
Darryl Green | 7be79c9 | 2019-03-05 15:23:25 +0000 | [diff] [blame] | 165 | if not (os.path.exists(os.path.join(git_worktree_path, "crypto")) |
Darryl Green | 02b6865 | 2019-03-05 16:25:38 +0000 | [diff] [blame] | 166 | and version.crypto_revision): |
Darryl Green | 7be79c9 | 2019-03-05 15:23:25 +0000 | [diff] [blame] | 167 | return |
| 168 | |
Darryl Green | 02b6865 | 2019-03-05 16:25:38 +0000 | [diff] [blame] | 169 | if version.crypto_repository: |
Darryl Green | 4a483e4 | 2019-04-12 16:24:25 +0100 | [diff] [blame] | 170 | fetch_output = subprocess.check_output( |
Darryl Green | b743309 | 2019-03-08 11:12:19 +0000 | [diff] [blame] | 171 | [self.git_command, "fetch", version.crypto_repository, |
| 172 | version.crypto_revision], |
Darryl Green | 7be79c9 | 2019-03-05 15:23:25 +0000 | [diff] [blame] | 173 | cwd=os.path.join(git_worktree_path, "crypto"), |
Darryl Green | 7be79c9 | 2019-03-05 15:23:25 +0000 | [diff] [blame] | 174 | stderr=subprocess.STDOUT |
| 175 | ) |
Darryl Green | f0f9f7f | 2019-03-08 11:30:04 +0000 | [diff] [blame] | 176 | self.log.debug(fetch_output.decode("utf-8")) |
Darryl Green | b743309 | 2019-03-08 11:12:19 +0000 | [diff] [blame] | 177 | crypto_rev = "FETCH_HEAD" |
| 178 | else: |
| 179 | crypto_rev = version.crypto_revision |
| 180 | |
Darryl Green | 4a483e4 | 2019-04-12 16:24:25 +0100 | [diff] [blame] | 181 | checkout_output = subprocess.check_output( |
Darryl Green | b743309 | 2019-03-08 11:12:19 +0000 | [diff] [blame] | 182 | [self.git_command, "checkout", crypto_rev], |
| 183 | cwd=os.path.join(git_worktree_path, "crypto"), |
Darryl Green | b743309 | 2019-03-08 11:12:19 +0000 | [diff] [blame] | 184 | stderr=subprocess.STDOUT |
| 185 | ) |
Darryl Green | f0f9f7f | 2019-03-08 11:30:04 +0000 | [diff] [blame] | 186 | self.log.debug(checkout_output.decode("utf-8")) |
Jaeden Amero | 346f959 | 2018-11-02 16:35:09 +0000 | [diff] [blame] | 187 | |
Darryl Green | 7bb9cb5 | 2019-03-05 16:30:39 +0000 | [diff] [blame] | 188 | def _build_shared_libraries(self, git_worktree_path, version): |
Gilles Peskine | fceb4ce | 2019-02-25 20:36:52 +0100 | [diff] [blame] | 189 | """Build the shared libraries in the specified worktree.""" |
Darryl Green | 3da1504 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 190 | my_environment = os.environ.copy() |
| 191 | my_environment["CFLAGS"] = "-g -Og" |
| 192 | my_environment["SHARED"] = "1" |
Darryl Green | 8194871 | 2019-05-09 13:03:05 +0100 | [diff] [blame] | 193 | if os.path.exists(os.path.join(git_worktree_path, "crypto")): |
| 194 | my_environment["USE_CRYPTO_SUBMODULE"] = "1" |
Darryl Green | 4a483e4 | 2019-04-12 16:24:25 +0100 | [diff] [blame] | 195 | make_output = subprocess.check_output( |
Darryl Green | 997c287 | 2019-02-28 11:52:39 +0000 | [diff] [blame] | 196 | [self.make_command, "lib"], |
Darryl Green | 3da1504 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 197 | env=my_environment, |
| 198 | cwd=git_worktree_path, |
Darryl Green | 3da1504 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 199 | stderr=subprocess.STDOUT |
| 200 | ) |
Darryl Green | f0f9f7f | 2019-03-08 11:30:04 +0000 | [diff] [blame] | 201 | self.log.debug(make_output.decode("utf-8")) |
Darryl Green | 31a1e99 | 2019-04-12 15:18:02 +0100 | [diff] [blame] | 202 | for root, _dirs, files in os.walk(git_worktree_path): |
Darryl Green | d9ad9ec | 2019-02-25 11:35:05 +0000 | [diff] [blame] | 203 | for file in fnmatch.filter(files, "*.so"): |
Darryl Green | 02b6865 | 2019-03-05 16:25:38 +0000 | [diff] [blame] | 204 | version.modules[os.path.splitext(file)[0]] = ( |
Darryl Green | d98d8b5 | 2019-02-27 16:53:40 +0000 | [diff] [blame] | 205 | os.path.join(root, file) |
Darryl Green | d9ad9ec | 2019-02-25 11:35:05 +0000 | [diff] [blame] | 206 | ) |
Darryl Green | 3da1504 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 207 | |
Gilles Peskine | 2b3f134 | 2019-07-04 19:01:22 +0200 | [diff] [blame] | 208 | @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 Green | b7447e7 | 2019-04-05 17:06:17 +0100 | [diff] [blame] | 215 | def _get_abi_dumps_from_shared_libraries(self, version): |
Gilles Peskine | fceb4ce | 2019-02-25 20:36:52 +0100 | [diff] [blame] | 216 | """Generate the ABI dumps for the specified git revision. |
Darryl Green | b7447e7 | 2019-04-05 17:06:17 +0100 | [diff] [blame] | 217 | The shared libraries must have been built and the module paths |
| 218 | present in version.modules.""" |
Darryl Green | 02b6865 | 2019-03-05 16:25:38 +0000 | [diff] [blame] | 219 | for mbed_module, module_path in version.modules.items(): |
Darryl Green | 3da1504 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 220 | output_path = os.path.join( |
Darryl Green | cf43425 | 2019-04-04 14:39:33 +0100 | [diff] [blame] | 221 | self.report_dir, "{}-{}-{}.dump".format( |
| 222 | mbed_module, version.revision, version.version |
Darryl Green | d98d8b5 | 2019-02-27 16:53:40 +0000 | [diff] [blame] | 223 | ) |
Darryl Green | 3da1504 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 224 | ) |
| 225 | abi_dump_command = [ |
| 226 | "abi-dumper", |
Darryl Green | d9ad9ec | 2019-02-25 11:35:05 +0000 | [diff] [blame] | 227 | module_path, |
Darryl Green | 3da1504 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 228 | "-o", output_path, |
Gilles Peskine | 2b3f134 | 2019-07-04 19:01:22 +0200 | [diff] [blame] | 229 | "-lver", self._pretty_revision(version), |
Darryl Green | 3da1504 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 230 | ] |
Darryl Green | 4a483e4 | 2019-04-12 16:24:25 +0100 | [diff] [blame] | 231 | abi_dump_output = subprocess.check_output( |
Darryl Green | 3da1504 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 232 | abi_dump_command, |
Darryl Green | 3da1504 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 233 | stderr=subprocess.STDOUT |
| 234 | ) |
Darryl Green | f0f9f7f | 2019-03-08 11:30:04 +0000 | [diff] [blame] | 235 | self.log.debug(abi_dump_output.decode("utf-8")) |
Darryl Green | 02b6865 | 2019-03-05 16:25:38 +0000 | [diff] [blame] | 236 | version.abi_dumps[mbed_module] = output_path |
Darryl Green | 3da1504 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 237 | |
Darryl Green | 7bb9cb5 | 2019-03-05 16:30:39 +0000 | [diff] [blame] | 238 | def _cleanup_worktree(self, git_worktree_path): |
Gilles Peskine | fceb4ce | 2019-02-25 20:36:52 +0100 | [diff] [blame] | 239 | """Remove the specified git worktree.""" |
Darryl Green | 3da1504 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 240 | shutil.rmtree(git_worktree_path) |
Darryl Green | 4a483e4 | 2019-04-12 16:24:25 +0100 | [diff] [blame] | 241 | worktree_output = subprocess.check_output( |
Darryl Green | 3da1504 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 242 | [self.git_command, "worktree", "prune"], |
| 243 | cwd=self.repo_path, |
Darryl Green | 3da1504 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 244 | stderr=subprocess.STDOUT |
| 245 | ) |
Darryl Green | f0f9f7f | 2019-03-08 11:30:04 +0000 | [diff] [blame] | 246 | self.log.debug(worktree_output.decode("utf-8")) |
Darryl Green | 3da1504 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 247 | |
Darryl Green | 7bb9cb5 | 2019-03-05 16:30:39 +0000 | [diff] [blame] | 248 | def _get_abi_dump_for_ref(self, version): |
Gilles Peskine | fceb4ce | 2019-02-25 20:36:52 +0100 | [diff] [blame] | 249 | """Generate the ABI dumps for the specified git revision.""" |
Darryl Green | 7bb9cb5 | 2019-03-05 16:30:39 +0000 | [diff] [blame] | 250 | 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 Green | b7447e7 | 2019-04-05 17:06:17 +0100 | [diff] [blame] | 253 | self._get_abi_dumps_from_shared_libraries(version) |
Darryl Green | 7bb9cb5 | 2019-03-05 16:30:39 +0000 | [diff] [blame] | 254 | self._cleanup_worktree(git_worktree_path) |
Darryl Green | 3da1504 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 255 | |
Darryl Green | 7bb9cb5 | 2019-03-05 16:30:39 +0000 | [diff] [blame] | 256 | def _remove_children_with_tag(self, parent, tag): |
Darryl Green | 32e7a50 | 2019-02-21 13:09:26 +0000 | [diff] [blame] | 257 | children = parent.getchildren() |
| 258 | for child in children: |
| 259 | if child.tag == tag: |
| 260 | parent.remove(child) |
| 261 | else: |
Darryl Green | 7bb9cb5 | 2019-03-05 16:30:39 +0000 | [diff] [blame] | 262 | self._remove_children_with_tag(child, tag) |
Darryl Green | 32e7a50 | 2019-02-21 13:09:26 +0000 | [diff] [blame] | 263 | |
Darryl Green | 7bb9cb5 | 2019-03-05 16:30:39 +0000 | [diff] [blame] | 264 | def _remove_extra_detail_from_report(self, report_root): |
Darryl Green | 32e7a50 | 2019-02-21 13:09:26 +0000 | [diff] [blame] | 265 | for tag in ['test_info', 'test_results', 'problem_summary', |
Darryl Green | db95d7e | 2019-06-05 12:57:50 +0100 | [diff] [blame] | 266 | 'added_symbols', 'affected']: |
Darryl Green | 7bb9cb5 | 2019-03-05 16:30:39 +0000 | [diff] [blame] | 267 | self._remove_children_with_tag(report_root, tag) |
Darryl Green | 32e7a50 | 2019-02-21 13:09:26 +0000 | [diff] [blame] | 268 | |
| 269 | for report in report_root: |
| 270 | for problems in report.getchildren()[:]: |
| 271 | if not problems.getchildren(): |
| 272 | report.remove(problems) |
| 273 | |
Gilles Peskine | b14b730 | 2019-07-04 19:17:40 +0200 | [diff] [blame] | 274 | 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 Green | 3da1504 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 331 | def get_abi_compatibility_report(self): |
Gilles Peskine | fceb4ce | 2019-02-25 20:36:52 +0100 | [diff] [blame] | 332 | """Generate a report of the differences between the reference ABI |
Darryl Green | b7447e7 | 2019-04-05 17:06:17 +0100 | [diff] [blame] | 333 | and the new ABI. ABI dumps from self.old_version and self.new_version |
| 334 | must be available.""" |
Gilles Peskine | b14b730 | 2019-07-04 19:17:40 +0200 | [diff] [blame] | 335 | compatibility_report = ["Checking evolution from {} to {}".format( |
Gilles Peskine | 2b3f134 | 2019-07-04 19:01:22 +0200 | [diff] [blame] | 336 | self._pretty_revision(self.old_version), |
| 337 | self._pretty_revision(self.new_version) |
Gilles Peskine | b14b730 | 2019-07-04 19:17:40 +0200 | [diff] [blame] | 338 | )] |
Darryl Green | 3da1504 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 339 | compliance_return_code = 0 |
Darryl Green | 02b6865 | 2019-03-05 16:25:38 +0000 | [diff] [blame] | 340 | shared_modules = list(set(self.old_version.modules.keys()) & |
| 341 | set(self.new_version.modules.keys())) |
Darryl Green | d98d8b5 | 2019-02-27 16:53:40 +0000 | [diff] [blame] | 342 | for mbed_module in shared_modules: |
Gilles Peskine | b14b730 | 2019-07-04 19:17:40 +0200 | [diff] [blame] | 343 | if not self._is_library_compatible(mbed_module, |
| 344 | compatibility_report): |
| 345 | compliance_return_code = 1 |
Darryl Green | 3e9626e | 2019-05-29 11:29:08 +0100 | [diff] [blame] | 346 | 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 Green | ab3893b | 2019-02-25 17:01:55 +0000 | [diff] [blame] | 349 | if self.can_remove_report_dir: |
Darryl Green | 3da1504 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 350 | os.rmdir(self.report_dir) |
Gilles Peskine | b14b730 | 2019-07-04 19:17:40 +0200 | [diff] [blame] | 351 | self.log.info("\n".join(compatibility_report)) |
Darryl Green | 3da1504 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 352 | return compliance_return_code |
| 353 | |
| 354 | def check_for_abi_changes(self): |
Gilles Peskine | fceb4ce | 2019-02-25 20:36:52 +0100 | [diff] [blame] | 355 | """Generate a report of ABI differences |
| 356 | between self.old_rev and self.new_rev.""" |
Darryl Green | 3da1504 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 357 | self.check_repo_path() |
| 358 | self.check_abi_tools_are_installed() |
Darryl Green | 7bb9cb5 | 2019-03-05 16:30:39 +0000 | [diff] [blame] | 359 | self._get_abi_dump_for_ref(self.old_version) |
| 360 | self._get_abi_dump_for_ref(self.new_version) |
Darryl Green | 3da1504 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 361 | return self.get_abi_compatibility_report() |
| 362 | |
| 363 | |
| 364 | def run_main(): |
| 365 | try: |
| 366 | parser = argparse.ArgumentParser( |
| 367 | description=( |
Darryl Green | 31321ca | 2018-04-16 12:02:29 +0100 | [diff] [blame] | 368 | """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 Green | 32e7a50 | 2019-02-21 13:09:26 +0000 | [diff] [blame] | 372 | The results of the comparison are either formatted as HTML and |
Darryl Green | 765d20d | 2019-03-05 15:21:32 +0000 | [diff] [blame] | 373 | 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 Green | 3da1504 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 377 | ) |
| 378 | ) |
| 379 | parser.add_argument( |
Darryl Green | f0f9f7f | 2019-03-08 11:30:04 +0000 | [diff] [blame] | 380 | "-v", "--verbose", action="store_true", |
| 381 | help="set verbosity level", |
| 382 | ) |
| 383 | parser.add_argument( |
Darryl Green | 31321ca | 2018-04-16 12:02:29 +0100 | [diff] [blame] | 384 | "-r", "--report-dir", type=str, default="reports", |
Darryl Green | 3da1504 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 385 | help="directory where reports are stored, default is reports", |
| 386 | ) |
| 387 | parser.add_argument( |
Darryl Green | 31321ca | 2018-04-16 12:02:29 +0100 | [diff] [blame] | 388 | "-k", "--keep-all-reports", action="store_true", |
Darryl Green | 3da1504 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 389 | help="keep all reports, even if there are no compatibility issues", |
| 390 | ) |
| 391 | parser.add_argument( |
Darryl Green | 826e5af | 2019-03-01 09:54:44 +0000 | [diff] [blame] | 392 | "-o", "--old-rev", type=str, help="revision for old version.", |
| 393 | required=True, |
Darryl Green | 3da1504 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 394 | ) |
| 395 | parser.add_argument( |
Darryl Green | 826e5af | 2019-03-01 09:54:44 +0000 | [diff] [blame] | 396 | "-or", "--old-repo", type=str, help="repository for old version." |
Darryl Green | d9ad9ec | 2019-02-25 11:35:05 +0000 | [diff] [blame] | 397 | ) |
| 398 | parser.add_argument( |
Darryl Green | 826e5af | 2019-03-01 09:54:44 +0000 | [diff] [blame] | 399 | "-oc", "--old-crypto-rev", type=str, |
| 400 | help="revision for old crypto submodule." |
Darryl Green | 3da1504 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 401 | ) |
Darryl Green | d3cde6f | 2019-02-20 15:01:56 +0000 | [diff] [blame] | 402 | parser.add_argument( |
Darryl Green | 826e5af | 2019-03-01 09:54:44 +0000 | [diff] [blame] | 403 | "-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 Green | d9ad9ec | 2019-02-25 11:35:05 +0000 | [diff] [blame] | 420 | ) |
| 421 | parser.add_argument( |
Darryl Green | d3cde6f | 2019-02-20 15:01:56 +0000 | [diff] [blame] | 422 | "-s", "--skip-file", type=str, |
Gilles Peskine | cc02658 | 2019-07-04 19:00:31 +0200 | [diff] [blame] | 423 | help=("path to file containing symbols and types to skip " |
| 424 | "(typically \"-s identifiers\" after running " |
| 425 | "\"tests/scripts/list-identifiers.sh --internal\")") |
Darryl Green | d3cde6f | 2019-02-20 15:01:56 +0000 | [diff] [blame] | 426 | ) |
Darryl Green | 32e7a50 | 2019-02-21 13:09:26 +0000 | [diff] [blame] | 427 | 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 Green | 3da1504 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 431 | abi_args = parser.parse_args() |
Darryl Green | 03625fe | 2019-04-11 15:50:41 +0100 | [diff] [blame] | 432 | if os.path.isfile(abi_args.report_dir): |
| 433 | print("Error: {} is not a directory".format(abi_args.report_dir)) |
| 434 | parser.exit() |
Darryl Green | f1d272d | 2019-04-09 09:14:17 +0100 | [diff] [blame] | 435 | old_version = SimpleNamespace( |
| 436 | version="old", |
| 437 | repository=abi_args.old_repo, |
| 438 | revision=abi_args.old_rev, |
Gilles Peskine | 2b3f134 | 2019-07-04 19:01:22 +0200 | [diff] [blame] | 439 | commit=None, |
Darryl Green | f1d272d | 2019-04-09 09:14:17 +0100 | [diff] [blame] | 440 | crypto_repository=abi_args.old_crypto_repo, |
| 441 | crypto_revision=abi_args.old_crypto_rev, |
| 442 | abi_dumps={}, |
| 443 | modules={} |
Darryl Green | b7447e7 | 2019-04-05 17:06:17 +0100 | [diff] [blame] | 444 | ) |
Darryl Green | f1d272d | 2019-04-09 09:14:17 +0100 | [diff] [blame] | 445 | new_version = SimpleNamespace( |
| 446 | version="new", |
| 447 | repository=abi_args.new_repo, |
| 448 | revision=abi_args.new_rev, |
Gilles Peskine | 2b3f134 | 2019-07-04 19:01:22 +0200 | [diff] [blame] | 449 | commit=None, |
Darryl Green | f1d272d | 2019-04-09 09:14:17 +0100 | [diff] [blame] | 450 | crypto_repository=abi_args.new_crypto_repo, |
| 451 | crypto_revision=abi_args.new_crypto_rev, |
| 452 | abi_dumps={}, |
| 453 | modules={} |
Darryl Green | b7447e7 | 2019-04-05 17:06:17 +0100 | [diff] [blame] | 454 | ) |
Darryl Green | f1d272d | 2019-04-09 09:14:17 +0100 | [diff] [blame] | 455 | 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 Green | 3da1504 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 461 | ) |
Darryl Green | f1d272d | 2019-04-09 09:14:17 +0100 | [diff] [blame] | 462 | abi_check = AbiChecker(old_version, new_version, configuration) |
Darryl Green | 3da1504 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 463 | return_code = abi_check.check_for_abi_changes() |
| 464 | sys.exit(return_code) |
Darryl Green | 62a18e3 | 2019-04-18 14:59:51 +0100 | [diff] [blame] | 465 | except Exception: # pylint: disable=broad-except |
| 466 | # Print the backtrace and exit explicitly so as to exit with |
| 467 | # status 2, not 1. |
Darryl Green | c47ac26 | 2018-03-15 10:12:06 +0000 | [diff] [blame] | 468 | traceback.print_exc() |
Darryl Green | 3da1504 | 2018-03-01 14:53:49 +0000 | [diff] [blame] | 469 | sys.exit(2) |
| 470 | |
| 471 | |
| 472 | if __name__ == "__main__": |
| 473 | run_main() |