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