3 __date__ =
"$Sep 2, 2010 10:09:19 AM$"
10 from optparse
import OptionParser;
11 from subs
import subs;
13 KEYWORD =
'@precisions';
14 REGEX =
'^.*'+KEYWORD+
'\s+((\w+,?)+)\s+(\w+)\s+->\s*((\s\w+)+).*$';
18 return p.replace(path.realpath(
'.')+
'/',
'');
25 required_precisions = [];
30 file = path.realpath(file);
32 self.
file = list(path.split(file));
33 self.
date = path.getmtime(file);
34 if path.samefile(path.join(self.
file[0],self.
file[1]),sys.argv[0]):
35 raise ValueError(
'Let\'s just forget codegen.py');
38 self.
types = match[0].split(
',');
47 self.precstmp.append(prec);
50 raise ValueError(path.join(self.
file[0],self.
file[1])+
' : Invalid conversion string');
51 self.files_in.append(rel);
67 if self.
debug:
print precision,
':',
68 if new_file <> self.
file[1]:
74 makeprefix =
'--prefix '+prefix;
75 conversion = path.join(prefix, new_file);
79 print file_out+
':',file_in;
80 print "\t$(PYTHON)",path.realpath(sys.argv[0]),makeprefix,
'-p',precision,
"--file",file_in;
81 self.names.append(new_file);
82 self.files_out.append(file_out);
85 date = path.getmtime(conversion);
86 diff = self.
date - date;
87 self.dates.append(diff);
89 if diff > 0:
print 'Old',
90 else:
print 'Current',
92 if diff > 0: load =
True;
94 if self.
debug:
print 'Missing';
95 self.dates.append(
None);
98 if self.
debug:
print '<No Change>',
':';
99 else:
print >> sys.stderr, new_file,
'had no change for', precision;
100 self.names.append(
None);
101 self.dates.append(
None);
105 for i
in range(len(self.
names)):
106 name = self.
names[i];
108 if data
is None or name
is None:
continue;
110 fd = open(path.join(self.
file[0],name),
'w');
112 fd = open(path.join(self.
prefix,name),
'w');
120 name = self.
names[i];
121 date = self.
dates[i];
122 if name
is not None and (date
is None or date > 0):
124 else: self.converted.append(
None);
128 work = subs[sub_type];
129 prec_to = work[0].index(precision);
130 prec_from = work[0].index(self.
precision);
133 for i
in range(1,len(work)):
135 search = work[i][prec_from];
136 replace = work[i][prec_to];
137 if not search:
continue;
138 replace.replace(
'\*',
'*');
139 data = re.sub(search, replace, data);
141 print 'Bad replacement pair ',i,
'in',sub_type;
147 data = self.
substitute(
'all', data, precision);
149 for sub_type
in self.
types:
150 if sub_type ==
'all':
continue;
152 data = self.
substitute(sub_type, data, precision);
154 raise ValueError(
'I encountered an unrecoverable error while working in subtype:',sub_type+
'.');
155 data = re.sub(
'@precisions '+
','.join(self.
types)+
'.*',
'@generated '+precision, data);
158 def grep(string,list):
159 expr = re.compile(string)
160 return filter(expr.search,list)
162 parser = OptionParser();
163 parser.add_option(
"-d",
"--debug", help=
'Print debugging messages.', action=
'store_true', dest=
'debug', default=
False);
164 parser.add_option(
"-P",
"--prefix", help=
'The output directory if different from the input directory.', action=
'store', dest=
'prefix', default=
None);
165 parser.add_option(
"-i",
"--in-files", help=
'Print the filenames of files for precision generation.', action=
'store_true', dest=
'in_print', default=
False);
166 parser.add_option(
"-o",
"--out-files", help=
'Print the filenames for the precision generated files.', action=
'store_true', dest=
'out_print', default=
False);
167 parser.add_option(
"-c",
"--clean", help=
'Remove the files that are the product of generation.', action=
'store_true', dest=
'out_clean', default=
False);
168 parser.add_option(
"-t",
"--threads", help=
'Enter the number of threads to use for conversion.', action=
'store', dest=
'threads', default=1);
169 parser.add_option(
"-f",
"--file", help=
'Specify a file(s) on which to operate.', action=
'store', dest=
'fileslst', type=
'string', default=
"");
170 parser.add_option(
"-p",
"--prec", help=
'Specify a precision(s) on which to operate.', action=
'store', dest=
'precslst', type=
'string', default=
"");
171 parser.add_option(
"-m",
"--make", help=
'Spew a GNU Make friendly file to standard out.', action=
'store_true', dest=
'make', default=
False);
172 parser.add_option(
"-T",
"--test", help=
'Don\'t actually do any work.', action=
'store_true', dest=
'test', default=
False);
173 (options, args) = parser.parse_args();
175 rex = re.compile(REGEX);
179 fd = open(path.realpath(file),
'r');
180 lines = fd.readlines();
184 if m
is None:
continue;
185 work.append((file, m.groups(),
''.join(lines)));
188 options.files = options.fileslst.split();
189 if len(options.files):
190 for file
in options.files:
194 for root, dirs, files
in os.walk(startDir,
True,
None):
196 if file.startswith(
'.'):
continue;
197 if not file.endswith(
'.c')
and not file.endswith(
'.h')
and not file.endswith(
'.f'):
203 Conversion.debug = options.debug;
204 Conversion.make = options.make;
205 Conversion.prefix = options.prefix;
207 if options.out_print
or options.out_clean
or options.in_print
or options.make
or options.test:
208 Conversion.test =
True;
211 options.precs = options.precslst.split();
212 if len(options.precs):
213 Conversion.required_precisions = options.precs;
215 print '## Automatically generated Makefile';
216 print 'PYTHON ?= python';
221 print '\trm -f $(gen)';
222 print 'generate: $(gen)';
223 print '.PHONY: cleangen generate';
231 print >> sys.stderr, str(e);
235 print 'gen = ',
' '+
' '.join(c.files_out);
237 print '\trm -f $(gen)';
238 print 'generate: $(gen)';
239 print '.PHONY: cleangen generate';
240 if options.in_print:
print ' '.join(c.files_in);
241 if options.out_print:
print ' '.join(c.files_out);
242 if options.out_clean:
243 for file
in c.files_out:
244 if not path.exists(file):
continue;