1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3# Copyright (c) 2022 Huawei Device Co., Ltd.
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16import os
17import stat
18import json
19import argparse
20
21license_string = """/*
22 * Copyright (c) 2022 Huawei Device Co., Ltd.
23 * Licensed under the Apache License, Version 2.0 (the "License");
24 * you may not use this file except in compliance with the License.
25 * You may obtain a copy of the License at
26 *
27 *     http://www.apache.org/licenses/LICENSE-2.0
28 *
29 * Unless required by applicable law or agreed to in writing, software
30 * distributed under the License is distributed on an "AS IS" BASIS,
31 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
32 * See the License for the specific language governing permissions and
33 * limitations under the License.
34 */
35
36"""
37header_string = """#include <string.h>
38
39namespace OHOS {
40namespace ACELite {
41"""
42
43tail_string = """
44} // namespace ACELite
45} // namespace OHOS"""
46
47variable_define_string = """
48enum ConfigStatus {
49    ENABLE = 0,
50    DISABLE,
51};
52
53typedef struct {
54    const char *name;
55    enum ConfigStatus type;
56} SysCapDef;
57
58"""
59
60function_define_string = """
61static int GetSysCapNum()
62{
63    return sizeof(g_syscap) / sizeof(SysCapDef);
64}
65
66static bool HasSystemCapability(const char *cap)
67{
68    int sysCapNum = GetSysCapNum();
69    for (int i = 0; i < sysCapNum; i++) {
70        if (strcmp(cap, g_syscap[i].name) == 0) {
71            if (g_syscap[i].type == ENABLE) {
72                return true;
73            } else {
74                return false;
75            }
76        }
77    }
78    return false;
79}"""
80
81def parse_args():
82    parser = argparse.ArgumentParser()
83    parser.add_argument('--syscap-file',
84                        help='syscap json file')
85    parser.add_argument('--output-file',
86                        help='output app file')
87    arguments = parser.parse_args()
88    return arguments
89
90def get_syscap_list():
91    path = parse_args().syscap_file
92    with open(path, 'rb') as syscap_file:
93        syscap_json = json.load(syscap_file)['syscap']
94        syscap_file.close()
95        syscap_list = syscap_json['os']
96        if ('private' in syscap_json):
97            for i in syscap_json['private']:
98                syscap_list.append(i)
99    return syscap_list
100
101def assemble_syscap_array():
102    string = "static const SysCapDef g_syscap[] = {\n"
103    for i in get_syscap_list():
104        string = ''.join([string, '    {', '"', i, '", ENABLE},\n'])
105    string = ''.join([string.strip(',\n'), '\n};\n'])
106    return string
107
108def assemble_cpp_file():
109    line = license_string + header_string \
110         + variable_define_string + assemble_syscap_array() \
111         + function_define_string \
112         + tail_string
113    outpath = parse_args().output_file
114    flags = os.O_WRONLY | os.O_CREAT
115    modes = stat.S_IWUSR | stat.S_IRUSR
116    with os.fdopen(os.open(outpath, flags, modes), 'w') as f:
117        f.writelines(line)
118        f.close()
119
120if __name__ == '__main__':
121    assemble_cpp_file()
122