yu.dong | c33b307 | 2024-08-21 23:14:49 -0700 | [diff] [blame] | 1 | import os, sys
|
| 2 | import fnmatch
|
| 3 | import argparse
|
| 4 |
|
| 5 |
|
| 6 | def get_folder_structure(target_path):
|
| 7 | target_path = os.path.abspath(target_path)
|
| 8 | res = []
|
| 9 | for root, dir, files in os.walk(target_path):
|
| 10 | if '_source' in root:
|
| 11 | continue
|
| 12 | current_folder = os.path.abspath(root)[len(target_path) + 1:]
|
| 13 | if not current_folder:
|
| 14 | continue
|
| 15 | for item in fnmatch.filter(files, "*.h"):
|
| 16 | res.append(os.path.join(current_folder, item))
|
| 17 |
|
| 18 | return res
|
| 19 |
|
| 20 |
|
| 21 | def _extract_structure_name(line):
|
| 22 | for i, char in enumerate(line):
|
| 23 | if char.isalpha():
|
| 24 | s_pos = i
|
| 25 | break
|
| 26 | else:
|
| 27 | print('Can not locate first alpha in [{}]'.format(line))
|
| 28 | sys.exit(1)
|
| 29 |
|
| 30 | e_pos = line.index(';')
|
| 31 | return line[s_pos: e_pos]
|
| 32 |
|
| 33 |
|
| 34 | def convert(content):
|
| 35 | res = content[:]
|
| 36 | struct_start_index = [i for i, line in enumerate(content) if 'typedef struct {' in line]
|
| 37 | struct_end_index = [i for i, line in enumerate(content) if line.startswith('} ') and '_enum;' not in line]
|
| 38 | struct_index = [(i, j) for i, j in zip(struct_start_index, struct_end_index)]
|
| 39 | cur_struct_no = -1
|
| 40 |
|
| 41 | for i, org_line in enumerate(content):
|
| 42 | if 'typedef struct {' in org_line:
|
| 43 | cur_struct_no += 1
|
| 44 | if ':' in org_line and ';' in org_line:
|
| 45 | skip_from_pos = org_line.index(':')
|
| 46 | skip_to_pos = org_line.index(';')
|
| 47 | if skip_from_pos < skip_to_pos:
|
| 48 | res[i] = org_line[: skip_from_pos] + org_line[skip_to_pos:]
|
| 49 | if 'FLEXIBLE_ARRAY_SIZE' in org_line:
|
| 50 | struct_name = _extract_structure_name(content[struct_index[cur_struct_no][1]]).upper()
|
| 51 | # struct_name = content[struct_index[cur_struct_no][1]][2:-2].upper()
|
| 52 | substruct_name = org_line[org_line.index('N := ') + len('N := '): -1].lower()
|
| 53 | substruct_name = '_'.join([_ for _ in substruct_name.split(' ') if _ != ''])
|
| 54 |
|
| 55 | print('struct_name: ' + struct_name)
|
| 56 | print('substruct_name: ' + substruct_name)
|
| 57 |
|
| 58 | macro_str = '_'.join(['MAX', struct_name, substruct_name]).upper()
|
| 59 | res[i] = org_line[:org_line.index('FLEXIBLE_ARRAY_SIZE')] + \
|
| 60 | macro_str + \
|
| 61 | org_line[org_line.index('FLEXIBLE_ARRAY_SIZE') + len('FLEXIBLE_ARRAY_SIZE'):]
|
| 62 | print(org_line[:org_line.index('FLEXIBLE_ARRAY_SIZE')])
|
| 63 | print(macro_str)
|
| 64 | print(org_line[org_line.index('FLEXIBLE_ARRAY_SIZE') + len('FLEXIBLE_ARRAY_SIZE'):])
|
| 65 |
|
| 66 | return res
|
| 67 |
|
| 68 |
|
| 69 | def convert_all_headers(input_top_folder, out_top_folder):
|
| 70 | input_top_folder = os.path.abspath(input_top_folder)
|
| 71 | out_top_folder = os.path.abspath(out_top_folder)
|
| 72 | fp_list = get_folder_structure(input_top_folder)
|
| 73 |
|
| 74 | for fp in fp_list:
|
| 75 | write_to_include_header_file('/'.join(fp.split(os.sep)), out_top_folder)
|
| 76 | with open(os.path.join(input_top_folder, fp), 'r') as fs:
|
| 77 | content = fs.readlines()
|
| 78 | conv_content = convert(content)
|
| 79 |
|
| 80 | output_dir = os.path.dirname(os.path.join(out_top_folder, fp))
|
| 81 | if not os.path.isdir(output_dir):
|
| 82 | os.makedirs(output_dir)
|
| 83 | with open(os.path.join(out_top_folder, fp), 'w') as fs:
|
| 84 | fs.writelines(conv_content)
|
| 85 |
|
| 86 |
|
| 87 | def write_to_include_header_file(icd_path, output_fp):
|
| 88 | print('#include "{}"\n'.format(icd_path))
|
| 89 | with open(os.path.join(output_fp, 'icd_headers_ut.h'), "a") as outFile:
|
| 90 | outFile.write('#include "{}"\n'.format(icd_path))
|
| 91 |
|
| 92 |
|
| 93 | if __name__ == '__main__':
|
| 94 | parser = argparse.ArgumentParser()
|
| 95 | parser.add_argument('-p', action='store', dest='targetFolder', required=True,
|
| 96 | help='Target path (the header file under the folder will be converted)')
|
| 97 | parser.add_argument('-o', action='store', dest='outputFolder',
|
| 98 | help='Converted results will be stored under this path')
|
| 99 | parser.add_argument('-proj', action='store', dest='projectName', required=True)
|
| 100 | parser.add_argument('-flavor', action='store', dest='flavor', required=True)
|
| 101 | args = parser.parse_args()
|
| 102 |
|
| 103 | if not os.path.isdir(args.targetFolder):
|
| 104 | print('[ERROR] Target folder is not exist [{}] (ignored)'.format(args.targetFolder))
|
| 105 | sys.exit(1)
|
| 106 | # raise ValueError('Target folder is not exist [{}]'.format(targetFolder))
|
| 107 | else:
|
| 108 | input_top_folder = args.targetFolder
|
| 109 |
|
| 110 | if not args.outputFolder or os.path.isdir(args.outputFolder):
|
| 111 | PROJECT = args.projectName # os.environ['project_name']
|
| 112 | FLAVOR = args.flavor # os.environ['flavor']
|
| 113 | output_top_folder = os.path.join('build', PROJECT, FLAVOR, 'MoDIS', '_BUILD_XGEN', 'dhl', 'database_modis',
|
| 114 | 'converted_icd_headers')
|
| 115 | if not os.path.exists(output_top_folder):
|
| 116 | os.makedirs(output_top_folder)
|
| 117 | else:
|
| 118 | output_top_folder = args.outputFolder
|
| 119 |
|
| 120 | convert_all_headers(input_top_folder, output_top_folder)
|