• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# coding=utf-8
3
4'''
5* Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
6* Licensed under the Apache License, Version 2.0 (the "License");
7* you may not use this file except in compliance with the License.
8* You may obtain a copy of the License at
9*
10*     http://www.apache.org/licenses/LICENSE-2.0
11*
12* Unless required by applicable law or agreed to in writing, software
13* distributed under the License is distributed on an "AS IS" BASIS,
14* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15* See the License for the specific language governing permissions and
16* limitations under the License.
17*
18* Description: extract strings from .c file, then, generate .cfg file.
19'''
20
21import os
22import time
23import string
24import re
25import shutil
26import hashlib
27import binascii
28import sys
29import io
30import subprocess
31
32g_params = {}
33g_file_id_dic = {}
34g_mod_id_dic = {}
35
36#convert sys argvs from str to dictionary
37def mk_param_dic():
38    key='default_key'
39    key_val=''
40    i=0
41    while i< len(sys.argv):
42        if sys.argv[i]=='_PYTHON_ARG_':
43            if key!='default_key':
44                g_params[key]=key_val
45            else:
46                pass
47
48            key = sys.argv[i + 1]
49            key_val = ''
50            i = i + 1
51        else:
52            key_val = '%s%s '%(key_val, sys.argv[i])
53        i = i + 1
54
55    #last key process
56    if key!='default_key':
57        g_params[key]=key_val
58    #add interl param
59    g_params['I_FILE_DIR'] = os.path.join(g_params['PRIM_XML_TEMP_ROOT_DIR'].strip(), g_params['PRIM_XML_KEY_WORD'])
60
61#convert file_id_file(lm_file_id.cfg) to dictionary
62def create_file_id_dic(path = None):
63    file_name = g_params['PRIM_XML_FILE_ID_FILE'].strip(' ') if path is None else path
64    with open(file_name, 'r') as f1:
65        for line in f1:
66            list_temp=re.split('[ \t\n\r\f\v]+', line)
67            if len(list_temp) >= 2 and list_temp[0][-2:]=='.c' and list_temp[1].isdigit():
68                g_file_id_dic[list_temp[0]]=list_temp[1]
69    print('g_file_id_dic', g_file_id_dic)
70    return g_file_id_dic
71
72#convert mod_id_file(lm_file_id.cfg) to dictionary
73def create_mod_id_dic():
74    file_name = g_params['PRIM_XML_MOD_ID_FILE'].strip(' ')
75    with open(file_name, 'r') as f1:
76        for line in f1:
77            list_temp=re.split('[ \t\n\r\f\v]+', line)
78            if len(list_temp) >= 2 and list_temp[0][-2:]=='.c' and list_temp[1].isdigit():
79                g_mod_id_dic[list_temp[0]]=list_temp[1]
80        print('g_mod_id_dic', g_mod_id_dic)
81
82#Generates a list of files(.c) from all subdirectories
83def creat_src_file_list():
84    full_file_name_list = []
85    sub_dir_list = g_params['PRIM_XML_SRC_SUB_DIR'].split(' ')
86    print('sub_dir_list')
87    print(sub_dir_list)
88    for sub_dir in sub_dir_list:
89        sub_dir=sub_dir.strip()
90        if sub_dir:
91            full_dir = g_params['PRIM_XML_SRC_ROOT_DIR']
92            full_dir = full_dir.strip()
93            full_dir = os.path.join(full_dir, sub_dir)
94            print('full_dir=', full_dir)
95            file_name_list = os.listdir(full_dir)
96            print('file_name_list=', file_name_list)
97            for file_name in file_name_list:
98                full_file_name = os.path.join(full_dir, file_name)
99                if os.path.isfile(full_file_name):
100                    print('isfile=', full_file_name)
101                    if full_file_name.endswith('.c'):
102                        print('cfile=', full_file_name)
103                        full_file_name_list.append(full_file_name)
104    print('full_file_name_list=', full_file_name_list)
105    return full_file_name_list
106
107#pre-compile .c to .i
108def conver_c_2_i(full_file_name_list):
109    dst_full_file_name_list = []
110    print('conver_c_2_i=', full_file_name_list)
111    temp_dir = g_params['I_FILE_DIR'].strip()
112    if os.path.isdir(temp_dir):
113        shutil.rmtree(temp_dir)
114    os.makedirs(temp_dir)
115    cc=g_params['CC'].strip()
116    cflag = g_params['CFLAGS'].strip()
117    for src_full_file_name in full_file_name_list:
118        src_file_name = src_full_file_name.split('/')[-1]
119
120#        if src_file_name in g_file_id_dic and src_file_name in g_mod_id_dic:
121        if src_file_name in g_file_id_dic:
122            print('E2I:', src_file_name)
123            dst_file_name = src_file_name
124            dst_file_name=dst_file_name.replace('.c', '.i')
125            dst_full_file_name = os.path.join(temp_dir, dst_file_name)
126
127            cmd_line = [cc, '-E', src_full_file_name, cflag, '-D__FILE_NAME__=%s'%src_file_name,
128                        '-D__FILE_IDX__=%s'%g_file_id_dic[src_file_name], '-P', '>', dst_full_file_name]
129            print('cmd_line=', cmd_line)
130            subprocess.run(cmd_line)
131            dst_full_file_name_list.append(dst_full_file_name)
132    return dst_full_file_name_list
133
134def para_exist_i_file(file_name):
135    key_word = g_params['PRIM_XML_KEY_WORD'].strip()
136    parse_file = os.path.join(os.path.dirname(file_name), '%s.cfg'%key_word)
137    print('file_name parse_file=', file_name, parse_file)
138    with open(file_name, 'r') as f1, open(parse_file, 'w+') as f2:
139        for line in f1:
140            match=re.search('PRIM_XML_HI_DIAG_LOG_MSG_', line)
141            if match:
142                match_id=re.search('_PRIM_ID_:', line)
143                match_sz=re.search(',_PRIM_SZ_:', line)
144                match_line=re.search(',_PRIM_LINE_:', line)
145                match_file=re.search(',_PRIM_FILE_:', line)
146                match_file=re.search(',_PRIM_FILE_:', line)
147#parse file :get print info
148def cp_effect_content_on_file(cfg_fp, file_name):
149    with open(file_name, 'r') as src_fp:
150        print('file_name-----', file_name)
151        #time.sleep(5)
152        for line in src_fp:
153            match=re.search('_PRIM_PRI_', line)
154            if match:
155                print('line-----', line)
156                cfg_fp.write(line.encode("UTF-8"))
157
158def cp_effect_content(dst_full_file_name_list, cfg_file = None, scons_flag = False):
159    print('cfg file:', cfg_file)
160    base_folder = g_params['PRIM_XML_TEMP_BASE_ROOT_DIR'].strip() if cfg_file is None else os.path.dirname(cfg_file)
161    key_word = g_params['PRIM_XML_KEY_WORD'].strip() if cfg_file is None else None
162    #cfg dir not exist-->mk dir
163    print('base_folder', base_folder)
164    #if scons_flag == False and not os.path.isdir(base_folder):
165    if not os.path.isdir(base_folder):
166        print('base_folder2', base_folder)
167        os.makedirs(base_folder)
168
169    cfg_file_name = os.path.join(base_folder, '%s.cfg'%key_word) if cfg_file is None else cfg_file
170    #cfg file is exist-->recreate it
171    if os.path.isfile(cfg_file_name):
172        os.remove(cfg_file_name)
173
174    with open(cfg_file_name, 'ab+') as cfg_fp:
175    #cfg_fp=io.open(cfg_file_name, 'wb+')
176        for src_file_name in dst_full_file_name_list:
177            if os.path.isfile(src_file_name):
178                cp_effect_content_on_file(cfg_fp, src_file_name)
179
180    #cfg_fp.close()
181
182#main func
183def mk_prim_main():
184    print('mk_prim_main in')
185    #creat sys params dictionary
186    mk_param_dic()
187    #print('g_params', g_params)
188    print('set_print_target')
189    print(g_params['LOG_FILE_NAME'].strip())
190
191    log_file_name = g_params['LOG_FILE_NAME'].strip()
192    log_file_dir = os.path.dirname(log_file_name)
193    if not os.path.isdir(log_file_dir):
194        os.makedirs(log_file_dir)
195    with open(log_file_name, 'w+') as log_file_fp:
196        last_print_target = sys.stdout
197        sys.stdout = log_file_fp
198
199        print('create_file_id_dic')
200        create_file_id_dic()
201        #print('create_mod_id_dic')
202        #create_mod_id_dic()
203        full_file_name_list = creat_src_file_list()
204        print('full_file*****')
205        print(full_file_name_list)
206        dst_full_file_name_list = conver_c_2_i(full_file_name_list)
207        print(dst_full_file_name_list)
208        cp_effect_content(dst_full_file_name_list)
209
210        sys.stdout = last_print_target
211    exit(0)
212
213if __name__ == '__main__':
214    mk_prim_main()
215