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