1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3# Copyright (c) 2023 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 16''' 17注意: 181. 扩展syscap头文件的枚举定义,须在开始位置标记最小值,且必须大于等于500。 19''' 20import argparse 21 22LICENCE = '''/* 23 * Copyright (c) 2023 Huawei Device Co., Ltd. 24 * Licensed under the Apache License, Version 2.0 (the "License"); 25 * you may not use this file except in compliance with the License. 26 * You may obtain a copy of the License at 27 * 28 * http://www.apache.org/licenses/LICENSE-2.0 29 * 30 * Unless required by applicable law or agreed to in writing, software 31 * distributed under the License is distributed on an "AS IS" BASIS, 32 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 33 * See the License for the specific language governing permissions and 34 * limitations under the License. 35 */ 36 37''' 38 39BEFORE_ENUM = '''#ifndef SYSCAP_DEFINE_H 40#define SYSCAP_DEFINE_H 41 42#include <stdint.h> 43 44#define SINGLE_SYSCAP_LEN (256 + 17) 45#ifdef __cplusplus 46#if __cplusplus 47extern "C" { 48#endif /* __cplusplus */ 49#endif /* __cplusplus */ 50 51typedef struct SystemCapabilityWithNum { 52 char str[SINGLE_SYSCAP_LEN]; 53 uint16_t num; 54} SyscapWithNum; 55 56/* 57 * New SyscapNum must be added last and 58 * don't delete anyone, just comment after it. 59 */ 60''' 61 62AFTER_STRUCT = ''' 63#ifdef __cplusplus 64#if __cplusplus 65} 66#endif /* __cplusplus */ 67#endif /* __cplusplus */ 68#endif // SYSCAP_DEFINE_H 69''' 70 71 72def gen_define_enum(enum:list): 73 header = 'typedef enum SystemCapabilityNum {\n' 74 tail = '} SyscapNum;\n\n' 75 trunk = ''.join(enum) 76 return header + trunk + tail 77 78 79def gen_define_array(stru:list): 80 header = 'const static SyscapWithNum g_arraySyscap[] = {\n' 81 tail = '};\n\n' 82 trunk = ''.join(stru) 83 return header + trunk + tail 84 85 86def read_syscap(path): 87 syscap_enum = [] 88 op_flag = False 89 with open(path, 'r') as fb: 90 f = fb.readlines() 91 for line in f: 92 if line.startswith('typedef enum '): 93 op_flag = True 94 continue 95 elif op_flag and line.startswith('}'): 96 break 97 if op_flag: 98 syscap_enum.append(line) 99 100 syscap_stru = [] 101 op_flag = False 102 for line in f: 103 if line.startswith('const static SyscapWithNum '): 104 op_flag = True 105 continue 106 elif op_flag and line.startswith('}'): 107 op_flag = False 108 break 109 if op_flag: 110 syscap_stru.append(line) 111 112 return syscap_enum, syscap_stru 113 114 115def merge_define(base, extern): 116 base_enmu, base_stru = read_syscap(base) 117 ext_enmu, ext_stru = read_syscap(extern) 118 119 if '500' in base_enmu[-1] and '500,' in ext_enmu[0]: 120 res_enmu = base_enmu[:-1] + ext_enmu 121 else: 122 res_enmu = base_enmu + ext_enmu 123 124 res_stru = base_stru + ext_stru 125 return res_enmu, res_stru 126 127 128def assemble_header_file(fenum, fstru): 129 enum, stru = merge_define(fenum, fstru) 130 enum_def = gen_define_enum(enum) 131 stru_def = gen_define_array(stru) 132 return LICENCE + BEFORE_ENUM + enum_def + stru_def + AFTER_STRUCT 133 134 135def parse_args(): 136 parser = argparse.ArgumentParser() 137 parser.add_argument('--base', 138 help='base syscap config header.') 139 parser.add_argument('--extern', 140 help='extern syscap config header.') 141 parser.add_argument('--output', 142 help='output app file') 143 arguments = parser.parse_args() 144 return arguments 145 146 147if __name__ == '__main__': 148 args = parse_args() 149 base_file = args.base 150 extern_file = args.extern 151 output_file = args.output 152 153 full = assemble_header_file(base_file, extern_file) 154 with open(output_file, 'w') as out: 155 out.writelines(full) 156 157