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