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