| #!/usr/bin/env python3 |
| # Copyright 2022 The Chromium OS Authors. All rights reserved. |
| # Use of this source code is governed by a BSD-style license that can be |
| # found in the LICENSE file. |
| |
| """Compares generated D-Bus header files. |
| |
| This confirms that new dbus-binding generator outputs the same file |
| (except for the header guard parts) as original dbus-binding generator. |
| """ |
| |
| import argparse |
| import difflib |
| import pathlib |
| import re |
| import sys |
| |
| |
| def get_parser(): |
| """Returns an argument parser.""" |
| parser = argparse.ArgumentParser(description=__doc__) |
| parser.add_argument( |
| "--out", |
| required=True, |
| type=pathlib.Path, |
| help="file to output diff result", |
| ) |
| parser.add_argument( |
| "--cpp", |
| required=True, |
| type=pathlib.Path, |
| help="header file generated by generate-chromeos-dbus-bindings", |
| ) |
| parser.add_argument( |
| "--go", |
| required=True, |
| type=pathlib.Path, |
| help="header file generated by go-generate-chromeos-dbus-bindings", |
| ) |
| return parser |
| |
| |
| def main(argv): |
| parser = get_parser() |
| opts = parser.parse_args(argv) |
| |
| # New dbus-binding generator outputs to a different directory than |
| # original dbus-binding generator. So we need to erase the extra string |
| # caused by this difference. |
| dbus_tmp_headerguard_re = re.compile( |
| r"^(#(?:ifndef|define|endif //) [_A-Z0-9]*)_DBUS_TMP" |
| ) |
| headers_by_cpp = [ |
| dbus_tmp_headerguard_re.sub(r"\1", x) |
| for x in opts.cpp.read_text(encoding="utf-8").splitlines() |
| ] |
| |
| headers_by_go = opts.go.read_text(encoding="utf-8").splitlines() |
| |
| diff_result = list( |
| difflib.unified_diff( |
| headers_by_cpp, |
| headers_by_go, |
| fromfile=f"a/{opts.cpp.name}", |
| tofile=f"b/{opts.go.name}", |
| lineterm="", |
| ) |
| ) |
| # Even if there is no diff result, write to a file. Because an action rule |
| # in gn file requires at least one output file. |
| opts.out.write_text("\n".join(diff_result), encoding="utf-8") |
| if diff_result: |
| # Just for convenience, also output to stderr on error so that the |
| # build log includes the diff result. |
| print("\n".join(diff_result), file=sys.stderr) |
| return 1 if diff_result else 0 |
| |
| |
| if __name__ == "__main__": |
| sys.exit(main(sys.argv[1:])) |