1#!/usr/bin/env python3
2#
3# Copyright (C) 2016 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
19from os import listdir
20from os.path import isfile, join as path_join
21from subprocess import call
22import argparse
23
24def main():
25    """this python program tries to build all hardware interfaces from a directory"""
26
27    args = parseArgs()
28
29    path = args.path
30    is_open_gl = args.g
31
32    success, failure = genFiles(path, is_open_gl)
33
34    print("Success: ", ", ".join(success))
35    print("Failure: ", ", ".join(failure))
36
37    ratio = len(success) / (len(success) + len(failure))
38
39    print("%% success = %.2f" % (100 * ratio))
40
41def parseArgs():
42    parser = argparse.ArgumentParser()
43    parser.add_argument("path", help="location of headers to parse", type=str)
44    parser.add_argument("-g", help="enable opengl specific parsing", action="store_true")
45
46    return parser.parse_args()
47
48def genFiles(path, is_open_gl):
49    success = []
50    failure = []
51
52    for header in sorted(headers(path)):
53        fname = header[:-2]
54
55        command = ["c2hal",
56                   "-r", "android.hardware:hardware/interfaces",
57                   "-p", "android.hardware." + fname + "@1.0"]
58
59        if is_open_gl:
60            command += ["-g"]
61
62        command += [path_join(path, header)]
63
64        res = call(command)
65
66        if res == 0:
67            success += [header]
68        else:
69            failure += [header]
70
71    return success, failure
72
73def headers(path):
74    """all .h files in a directory"""
75    for item in listdir(path):
76        if not isfile(path_join(path, item)):
77            continue
78
79        if not item.endswith(".h"):
80            continue
81
82        yield item
83
84
85
86if __name__ == "__main__":
87    main()