makeOpt.py
4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import os, sys
from maui import *
import os.path
import re
import cPickle as pickle
re_option = re.compile(r"\[\s*(.+)\s*]")
build_path = get_build_path()
xml_path = os.path.join(build_path, "xml")
macro_path = os.path.join(xml_path, "macro")
info_log_filename = os.path.join(build_path, "log", "info.log")
features_log_filename = os.path.join(build_path, "log", "MMI_features.log")
output_filename = os.path.join(xml_path, "option.x")
def process_info(opt, filename):
info_log_section = ''
for ln in file(filename):
ln = ln.strip()
if not ln:
continue
if re_option.match(ln):
info_log_section = re_option.match(ln).groups()[0].strip()
continue
if info_log_section == 'COMMON OPTION':
if '=' not in ln:
opt[ln] = True
else:
h, l = ln.split('=', 1)
try:
v = eval(l)
except:
v = l.strip(' ()')
if h in opt:
print 'Warning:', h, 'has been defined.'
opt[h] = v
def process_features(opt, filename):
features_log_section = ''
for ln in file(filename):
ln = ln.strip()
if ln[:3] == '[D]':
ln = ln[4:]
d = [i.strip() for i in ln.split(' ', 1) if i.strip()]
if len(d) == 1:
opt[d[0]] = True
else:
try:
v = eval(d[1])
except:
v = d[1].strip(' ()')
h = d[0]
if h in opt:
print 'Warning:', h, 'has been defined.'
opt[h] = v
def make(force = False):
# ------------------------------------------------------------------------------
# Variables
# ------------------------------------------------------------------------------
#
# This is most important data, we will store compile options into this dictionary.
#
opt = {}
# ------------------------------------------------------------------------------
# Check whether need to rebuild option.x
# ------------------------------------------------------------------------------
need_rebuild = False
macro_files = []
if os.path.exists(macro_path):
for f in os.listdir(macro_path):
if len(f) > 3:
macro_files.append(os.path.join(macro_path, f))
if not os.path.exists(info_log_filename):
error_exit('Cannot find info.log:', info_log_filename)
if not os.path.exists(features_log_filename):
error_exit('Cannot find MMI_features.log:', features_log_filename)
if not force and not check_need_rebuild(output_filename, [info_log_filename, features_log_filename] + macro_files):
print '[makeopt] The output file is latest'
return
print '[makeopt] Build options ...'
# ------------------------------------------------------------------------------
# Process
# ------------------------------------------------------------------------------
# Process info.log
process_info(opt, info_log_filename)
process_features(opt, features_log_filename)
for fn in macro_files:
process_features(opt, fn)
# Output as a cache
print '[makeopt] Collect options count:', len(opt)
try:
os.makedirs(xml_path)
except:
pass
try:
pickle.dump(opt, file(output_filename, "wb"), 2)
except:
error_exit('Save output file fail:', output_filename)
def getOpt():
# check whether need to rebuild option.x
make()
try:
opt = pickle.load(file(output_filename, "rb"))
except:
traceback.print_exc()
error_exit('Read opt.bin error:', output_filename)
return opt
if __name__ == "__main__":
make()