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