Matthias Maennich | cec41ee | 2019-03-29 08:40:18 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # |
| 3 | # Copyright (C) 2019 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | # |
| 17 | |
| 18 | import subprocess |
| 19 | import logging |
| 20 | |
| 21 | log = logging.getLogger(__name__) |
| 22 | |
| 23 | class AbiTool(object): |
| 24 | """ Base class for different kinds of abi analysis tools""" |
| 25 | def dump_kernel_abi(self, linux_tree, dump_path): |
| 26 | raise NotImplementedError() |
| 27 | |
| 28 | def diff_abi(self, old_dump, new_dump, diff_report): |
| 29 | raise NotImplementedError() |
| 30 | |
| 31 | def name(self): |
| 32 | raise NotImplementedError() |
| 33 | |
| 34 | class Libabigail(AbiTool): |
| 35 | """" Concrete AbiTool implementation for libabigail """ |
| 36 | def dump_kernel_abi(self, linux_tree, dump_path): |
| 37 | dump_abi_cmd = ['abidw', |
| 38 | '--linux-tree', |
| 39 | linux_tree, |
| 40 | '--out-file', |
| 41 | dump_path] |
| 42 | subprocess.check_call(dump_abi_cmd) |
| 43 | |
| 44 | def diff_abi(self, old_dump, new_dump, diff_report): |
| 45 | log.info('libabigail diffing: {} and {} at {}'.format(old_dump, |
| 46 | new_dump, |
| 47 | diff_report)) |
| 48 | diff_abi_cmd = ['abidiff', |
| 49 | '--impacted-interfaces', |
| 50 | '--leaf-changes-only', |
| 51 | '--dump-diff-tree', |
| 52 | old_dump, |
| 53 | new_dump] |
| 54 | |
| 55 | with open(diff_report, 'w') as out: |
| 56 | try: |
| 57 | subprocess.check_call(diff_abi_cmd, stdout=out, stderr=out) |
| 58 | except subprocess.CalledProcessError as e: |
| 59 | if e.returncode in (1, 2): # abigail error, user error |
| 60 | raise |
| 61 | return True # actual abi change |
| 62 | |
| 63 | return False # no abi change |
| 64 | |
| 65 | def get_abi_tool(abi_tool): |
| 66 | if abi_tool == 'libabigail': |
| 67 | log.info('using libabigail for abi analysis') |
| 68 | return Libabigail() |
| 69 | |
| 70 | raise ValueError("not a valid abi_tool: %s" % abi_tool) |
| 71 | |