blob: f9fb7f65d6edee7edbde53a92969b4b6223be731 [file] [log] [blame]
Darryl Green7c2dd582018-03-01 14:53:49 +00001#!/usr/bin/env python3
Darryl Green127c5af2018-03-12 15:44:31 +00002#
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#
Darryl Green7c2dd582018-03-01 14:53:49 +00009# 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.
12# The results of the comparison are formatted as HTML and stored at
13# a configurable location. Returns 0 on success, 1 on ABI/API non-compliance,
14# and 2 if there is an error while running the script.
15# Note: must be run from Mbed TLS root.
16
17import os
18import sys
19import traceback
20import shutil
21import subprocess
22import argparse
23import logging
24import tempfile
25
26
27class AbiChecker(object):
28
29 def __init__(self, report_dir, old_rev, new_rev, keep_all_reports):
30 self.repo_path = "."
31 self.log = None
32 self.setup_logger()
33 self.report_dir = os.path.abspath(report_dir)
34 self.keep_all_reports = keep_all_reports
35 self.should_keep_report_dir = os.path.isdir(self.report_dir)
36 self.old_rev = old_rev
37 self.new_rev = new_rev
38 self.mbedtls_modules = ["libmbedcrypto", "libmbedtls", "libmbedx509"]
39 self.old_dumps = {}
40 self.new_dumps = {}
41 self.git_command = "git"
42 self.make_command = "make"
43
44 def check_repo_path(self):
45 if not __file__ == os.path.join(".", "scripts", "abi_check.py"):
46 raise Exception("Must be run from Mbed TLS root")
47
48 def setup_logger(self):
49 self.log = logging.getLogger()
50 self.log.setLevel(logging.INFO)
51 self.log.addHandler(logging.StreamHandler())
52
53 def check_abi_tools_are_installed(self):
54 for command in ["abi-dumper", "abi-compliance-checker"]:
55 if not shutil.which(command):
56 raise Exception("{} not installed, aborting".format(command))
57
58 def get_clean_worktree_for_git_revision(self, git_rev):
59 self.log.info(
60 "Checking out git worktree for revision {}".format(git_rev)
61 )
62 git_worktree_path = tempfile.mkdtemp()
63 worktree_process = subprocess.Popen(
64 [self.git_command, "worktree", "add", git_worktree_path, git_rev],
65 cwd=self.repo_path,
66 stdout=subprocess.PIPE,
67 stderr=subprocess.STDOUT
68 )
69 worktree_output, _ = worktree_process.communicate()
70 self.log.info(worktree_output.decode("utf-8"))
71 if worktree_process.returncode != 0:
72 raise Exception("Checking out worktree failed, aborting")
73 return git_worktree_path
74
75 def build_shared_libraries(self, git_worktree_path):
76 my_environment = os.environ.copy()
77 my_environment["CFLAGS"] = "-g -Og"
78 my_environment["SHARED"] = "1"
79 make_process = subprocess.Popen(
80 self.make_command,
81 env=my_environment,
82 cwd=git_worktree_path,
83 stdout=subprocess.PIPE,
84 stderr=subprocess.STDOUT
85 )
86 make_output, _ = make_process.communicate()
87 self.log.info(make_output.decode("utf-8"))
88 if make_process.returncode != 0:
89 raise Exception("make failed, aborting")
90
91 def get_abi_dumps_from_shared_libraries(self, git_ref, git_worktree_path):
92 abi_dumps = {}
93 for mbed_module in self.mbedtls_modules:
94 output_path = os.path.join(
95 self.report_dir, "{}-{}.dump".format(mbed_module, git_ref)
96 )
97 abi_dump_command = [
98 "abi-dumper",
99 os.path.join(
100 git_worktree_path, "library", mbed_module + ".so"),
101 "-o", output_path,
102 "-lver", git_ref
103 ]
104 abi_dump_process = subprocess.Popen(
105 abi_dump_command,
106 stdout=subprocess.PIPE,
107 stderr=subprocess.STDOUT
108 )
109 abi_dump_output, _ = abi_dump_process.communicate()
110 self.log.info(abi_dump_output.decode("utf-8"))
111 if abi_dump_process.returncode != 0:
112 raise Exception("abi-dumper failed, aborting")
113 abi_dumps[mbed_module] = output_path
114 return abi_dumps
115
116 def cleanup_worktree(self, git_worktree_path):
117 shutil.rmtree(git_worktree_path)
118 worktree_process = subprocess.Popen(
119 [self.git_command, "worktree", "prune"],
120 cwd=self.repo_path,
121 stdout=subprocess.PIPE,
122 stderr=subprocess.STDOUT
123 )
124 worktree_output, _ = worktree_process.communicate()
125 self.log.info(worktree_output.decode("utf-8"))
126 if worktree_process.returncode != 0:
127 raise Exception("Worktree cleanup failed, aborting")
128
129 def get_abi_dump_for_ref(self, git_rev):
130 git_worktree_path = self.get_clean_worktree_for_git_revision(git_rev)
131 self.build_shared_libraries(git_worktree_path)
132 abi_dumps = self.get_abi_dumps_from_shared_libraries(
133 git_rev, git_worktree_path
134 )
135 self.cleanup_worktree(git_worktree_path)
136 return abi_dumps
137
138 def get_abi_compatibility_report(self):
139 compatibility_report = ""
140 compliance_return_code = 0
141 for mbed_module in self.mbedtls_modules:
142 output_path = os.path.join(
143 self.report_dir, "{}-{}-{}.html".format(
144 mbed_module, self.old_rev, self.new_rev
145 )
146 )
147 abi_compliance_command = [
148 "abi-compliance-checker",
149 "-l", mbed_module,
150 "-old", self.old_dumps[mbed_module],
151 "-new", self.new_dumps[mbed_module],
152 "-strict",
153 "-report-path", output_path
154 ]
155 abi_compliance_process = subprocess.Popen(
156 abi_compliance_command,
157 stdout=subprocess.PIPE,
158 stderr=subprocess.STDOUT
159 )
160 abi_compliance_output, _ = abi_compliance_process.communicate()
161 self.log.info(abi_compliance_output.decode("utf-8"))
162 if abi_compliance_process.returncode == 0:
163 compatibility_report += (
164 "No compatibility issues for {}\n".format(mbed_module)
165 )
166 if not self.keep_all_reports:
167 os.remove(output_path)
168 elif abi_compliance_process.returncode == 1:
169 compliance_return_code = 1
170 self.should_keep_report_dir = True
171 compatibility_report += (
172 "Compatibility issues found for {}, "
173 "for details see {}\n".format(mbed_module, output_path)
174 )
175 else:
176 raise Exception(
177 "abi-compliance-checker failed with a return code of {},"
178 " aborting".format(abi_compliance_process.returncode)
179 )
180 os.remove(self.old_dumps[mbed_module])
181 os.remove(self.new_dumps[mbed_module])
182 if not self.should_keep_report_dir and not self.keep_all_reports:
183 os.rmdir(self.report_dir)
184 self.log.info(compatibility_report)
185 return compliance_return_code
186
187 def check_for_abi_changes(self):
188 self.check_repo_path()
189 self.check_abi_tools_are_installed()
190 self.old_dumps = self.get_abi_dump_for_ref(self.old_rev)
191 self.new_dumps = self.get_abi_dump_for_ref(self.new_rev)
192 return self.get_abi_compatibility_report()
193
194
195def run_main():
196 try:
197 parser = argparse.ArgumentParser(
198 description=(
199 "This script is a small wrapper around the "
200 "abi-compliance-checker and abi-dumper tools, applying them "
201 "to compare the ABI and API of the library files from two "
202 "different Git revisions within an Mbed TLS repository."
203 " The results of the comparison are formatted as HTML and"
204 " stored at a configurable location. Returns 0 on success, "
205 "1 on ABI/API non-compliance, and 2 if there is an error "
206 "while running the script. # Note: must be run from "
207 "Mbed TLS root."
208 )
209 )
210 parser.add_argument(
211 "-r", "--report_dir", type=str, default="reports",
212 help="directory where reports are stored, default is reports",
213 )
214 parser.add_argument(
215 "-k", "--keep_all_reports", action="store_true",
216 help="keep all reports, even if there are no compatibility issues",
217 )
218 parser.add_argument(
219 "-o", "--old_rev", type=str, help="revision for old version",
220 required=True
221 )
222 parser.add_argument(
223 "-n", "--new_rev", type=str, help="revision for new version",
224 required=True
225 )
226 abi_args = parser.parse_args()
227 abi_check = AbiChecker(
228 abi_args.report_dir, abi_args.old_rev,
229 abi_args.new_rev, abi_args.keep_all_reports
230 )
231 return_code = abi_check.check_for_abi_changes()
232 sys.exit(return_code)
233 except Exception as error:
234 traceback.print_exc(error)
235 sys.exit(2)
236
237
238if __name__ == "__main__":
239 run_main()