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"""Fix prebuilt ELF check errors.
18
19This script fixes prebuilt ELF check errors by updating LOCAL_SHARED_LIBRARIES,
20adding LOCAL_MULTILIB, or adding LOCAL_CHECK_ELF_FILES.
21"""
22
23import argparse
24import io
25
26from elfcheck.rewriter import Rewriter
27
28
29def _parse_args():
30    parser = argparse.ArgumentParser()
31    parser.add_argument('android_mk', help='path to Android.mk')
32    parser.add_argument('--in-place', action='store_true',
33                        help='update the input file in place')
34    parser.add_argument('--var', action='append', default=[],
35                        metavar='KEY=VALUE', help='extra makefile variables')
36    return parser.parse_args()
37
38
39def _parse_arg_var(args_var):
40    variables = {}
41    for var in args_var:
42        if '=' in var:
43            key, value = var.split('=', 1)
44            key = key.strip()
45            value = value.strip()
46            variables[key] = value
47    return variables
48
49
50def main():
51    """Main function"""
52    args = _parse_args()
53    rewriter = Rewriter(args.android_mk, _parse_arg_var(args.var))
54    if args.in_place:
55        output_buffer = io.StringIO()
56        rewriter.rewrite(output_buffer)
57        with open(args.android_mk, 'w') as output_file:
58            output_file.write(output_buffer.getvalue())
59    else:
60        rewriter.rewrite()
61
62
63if __name__ == '__main__':
64    main()
65