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