1import os,sys,shutil,time
2import argparse
3
4def find_files(top, extensions=('.c', '.cpp', '.h'), exclude=('ult', 'googletest', 'classtrace', '.git')):
5    res = []
6    for root, dirs, files in os.walk(top):
7        dirs[:] = [d for d in dirs if d not in exclude]
8        #hard code to exclude mos_ file and _utils
9        files = [os.path.join(root, file) for file in files if os.path.splitext(file)[1] in extensions and not file.startswith('mos_') and not file.startswith('mhw_') and not file.startswith('media_') and not file.endswith('_utils.h')]
10        res.extend(files)
11    return res
12
13def chk_ADD_TRACE(file_path, class_info):
14    with open(file_path, 'r', errors="ignore") as fh:
15        #print(file_path)
16        lines = fh.readlines()
17        for line in lines:
18            if line.find('MEDIA_CLASS_DEFINE_END') > -1:
19                class_name = line.split('MEDIA_CLASS_DEFINE_END')[1][1:-2]
20                if class_name not in class_info:
21                    class_info[class_name] = [file_path]
22                else:
23                    class_info[class_name].append(file_path)
24
25def genHeaderFile(top, offset_head_file):
26    with open(offset_head_file, 'r') as fh:
27        lines = fh.readlines()
28        #the head file is not the origin/empty file, already has the offset info and do not need re-generate.
29        if str(lines).count('#define OFFSET_') > 0:
30            return
31
32    files = find_files(top, ('.h'))
33    files.sort()
34
35    class_info = {}
36    for file_name in files:
37        chk_ADD_TRACE(file_name, class_info)
38
39    class_list = list(class_info.keys())
40    class_list.sort()
41
42    with open(offset_head_file, 'w') as fh:
43        fh.write('#ifndef __TRACE_OFFSET_H__\n')
44        fh.write('#define __TRACE_OFFSET_H__\n')
45        fh.write('enum {\n')
46        for idx, class_name in enumerate(class_list):
47            org_files = list(set(class_info[class_name]))
48            org_files.sort()
49            #for org_file in org_files:
50            #    fh.write("  //%s\n"%org_file)
51            fh.write("  OFFSET_%s,\n"%(class_name))
52        fh.write('};\n')
53        fh.write('#endif //__TRACE_OFFSET_H__\n')
54
55
56if __name__ == "__main__":
57    parser = argparse.ArgumentParser(description='Command line inputs for checking script',argument_default=argparse.SUPPRESS)
58    parser.add_argument('-w', '--workspace', help='workspace of driver, ', required=True)
59    parser.add_argument('-f', '--headfile', help='the abs path of media_trace_offset.h', required=True)
60    args = parser.parse_args()
61    time_start = time.time()
62    genHeaderFile(args.workspace, args.headfile)
63    print("generate offset spend %0.4f s"%(time.time() - time_start))
64