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