Debian Codegraph @

external/gsl/flatten_gsl.py

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # djmw 20080323, 20120511, 20120515, 20120809
4 #
5 # Makes the directory structure of gsl flat by renaming all files and references by #include
6 # E.g. the file 'd/a.c' will be renamed as: gsl_d__a.c
7 # djmw 20100223 New makefile layout
8 #
9
10 import os, sys, glob, re, time, shutil
11
12 version = '1.10'
13 date = time.strftime('%Y%m%d')
14
15 gslconfigh = 'gsl__config.h'
16 fromdir = '/home/david/praat/gsl/gsl-' + version + '/'
17 todir = '/home/david/praat/gsl/GSL-' + version + '/'
18 log = open (todir + 'flatten_gsl' + version + '.log', 'w')
19 gslconfigh = 'gsl__config.h'
20
21 print >> log, date
22
23 dirs = ['blas', 'block', 'bspline', 'cblas', 'cdf', 'combination', 'complex', 'const',
24 'deriv', 'dht', 'diff', 'doc', 'eigen', 'err', 'fft', 'fit', 'gsl', 'histogram', 'ieee-utils',
25 'integration', 'linalg', 'matrix', 'min', 'monte', 'multifit', 'multimin',
26 'multiroots', 'ntuple', 'ode-initval', 'permutation', 'poly', 'qrng', 'randist', 'rng', 'roots',
27 'siman', 'sort', 'specfunc', 'statistics', 'sum', 'sys', 'vector', 'wavelet']
28
29 # left out: utils test
30 itype = 0
31 imake = 1
32 idist = 2
33 iname = 3
34 icount = 4
35 ihrep = 5
36 # per file: [type inmakefile indist renamed_as ntimesincluded headerreplacement]
37 # type = c h
38
39 def write_to_file (todir, basename, multilinetext):
40 f = open (todir + basename, 'w')
41 f.write (multilinetext)
42 f.close()
43
44 def dict_from_list (dict, dir, list, type = 'c', inmake = 'y', indist = 'y'):
45 for item in list:
46 newname = 'gsl_' + dir + '__' + item
47 if dir == 'gsl':
48 newname = item
49 dict[dir + '/' + item] = [type, inmake, indist, newname, 0 , '']
50
51 def get_all_files (dirs):
52 files = {'./config.h': ['h', 'n', 'y', gslconfigh, 0, ''], \
53 './templates_on.h':['h', 'n', 'y', 'templates_on.h', 0, ''], \
54 './templates_off.h':['h', 'n', 'y', 'templates_off.h', 0, '']}
55 for dir in dirs:
56 os.chdir (fromdir + dir)
57 dict_from_list (files, dir, glob.glob('*.c'), 'c', 'y')
58 dict_from_list (files, dir, glob.glob('*.h'), 'h', 'n')
59 return files
60
61 def quoted_include_get_newname (dict, key):
62 if dict.has_key(key):
63 dict[key][imake] = 'n'
64 if dict[key][ihrep]:
65 newname = dict[key][ihrep]
66 dict['gsl/' + newname][icount] += 1
67 print >> log, 'Corrected an include header error ' + key + ' ' + newname
68 else:
69 newname = dict[key][iname]
70 dict[key][icount] += 1
71 return '"' + newname + '"'
72 return None
73
74 # #include .....
75 # rewrite <gsl/file.h> to "file.h"
76 # <file.h> -> <file.h>
77 # "file.(c|h)" -> "gsl_dir__file.(c|h)"
78 def process_files (dict):
79 include = re.compile (r'^(\s*\#\s*include\s+)([^\s]+)') # 2 groups
80 delim = re.compile (r'(<|")\s*([^\s]+)\s*(>|")')
81 for key, val in dict.items():
82 (dir, base) = os.path.split (key)
83 if dir == '.': # config.h will be/was done by hand, don't overwrite
84 continue
85 file_in_name = fromdir + key
86 file_in = open (file_in_name, 'r')
87 file_out_name = val[iname]
88 output_lines = ''
89 for line in file_in:
90 minclude = include.search (line)
91 if minclude:
92 headerfile = minclude.group(2)
93 mheader = delim.search (headerfile)
94 header = mheader.group(2)
95 if mheader.group(1) == '<':
96 if header[0:4] == 'gsl/':
97 newname = '"' + header[4:] +'"'
98 dict[header][icount] += 1
99 elif header == 'config.h':
100 newname = '"' + gslconfigh + '"'
101 dict['./'+header][icount] += 1
102 else:
103 newname = '<' + header + '>'
104 else:
105 newkey = dir + '/' + header
106 newname = quoted_include_get_newname (dict, newkey)
107 if not newname:
108 newkey = './' + header
109 newname = quoted_include_get_newname (dict, newkey)
110 if not newname:
111 newname = header
112 print >> log, 'File does not exist: '+ header + ' From: ' + key
113 output_lines += minclude.group(1) + newname + '\n'
114 else:
115 output_lines += line
116 if val[idist] == 'y':
117 write_to_file (todir, file_out_name, output_lines)
118 else:
119 dict[key][imake] = 'n'
120 return dict
121
122 def gen_make_objects (dict, suffix, endofline):
123 make_objects = ''
124 (i , imax) = (0, 3)
125 keysd = dict.keys()
126 keysd.sort()
127 for key in keysd:
128 val = dict[key]
129 if val[imake] == 'n':
130 continue
131 i += 1
132 post = ' '
133 if i%imax == 0: post = endofline
134 (root, ext) = os.path.splitext (val[iname])
135 make_objects += root + suffix + post
136 return make_objects
137
138 def gen_sconscript(dict):
139 make_objects = gen_make_objects (dict, '.c', ' \n ')
140 sconscript = '''# SConscript for library gsl. This file was generated by the program flatten_gsl.py
141 # djmw ''' + date + '''
142
143 sources = Split(""" ''' + make_objects + '''""")
144
145 Import ('env')
146 env.Library ('GSL', sources)
147
148 # End of SConscript
149 '''
150 write_to_file (todir, 'SConscript', sconscript)
151
152 def gen_makefile (dict):
153 make_objects = gen_make_objects (dict, '.o', ' \\\n ')
154 makefile = """# Makefile for library gsl. This file was generated by the program flatten_gsl.py
155 # djmw """ + date + """
156
157 include ../makefile.defs
158
159 CPPFLAGS = -I ../sys -I ../dwsys
160
161 OBJECTS = """ + make_objects + """
162
163 .PHONY: all clean
164
165 all: libgsl.a
166
167 clean:
168 $(RM) $(OBJECTS)
169 $(RM) libgsl.a
170
171 $(OBJECTS): *.h ../sys/*.h ../dwsys/*.h
172
173 libgsl.a: $(OBJECTS)
174 touch libgsl.a
175 rm libgsl.a
176 ar cq libgsl.a $(OBJECTS)
177 $(RANLIB) libgsl.a
178
179 # end of Makefile
180 """
181 write_to_file (todir, 'Makefile', makefile)
182
183 def copy_file (fromdir, todir, file):
184 text = open(fromdir + file).read()
185 write_to_file (todir, file, text)
186
187 def preprocessing (current_gsl_version):
188 if not os.path.exists (todir + gslconfigh):
189 if not os.path.exists (fromdir + 'config.h'):
190 sys.exit(todir + gslconfigh + ' and ' + fromdir + "config.h don't exist. Please run configure first!!")
191 else:
192 text = open(fromdir + 'config.h').read()
193 write_to_file (todir, gslconfigh, text)
194 print >> log, '----------- gsl__config.h created ------------------'
195 copy_file (fromdir,todir, 'templates_on.h')
196 copy_file (fromdir,todir, 'templates_off.h')
197 print >> log, """
198 Post/Preprocesing
199 The layout of config.h/gsl__config.h varies from one version of gsl to the other
200 Do it by hand in """ + todir + """gsl__config.h:
201 Replace the #define "haves" with
202
203 #define HAVE_IEEEFP_H 0
204
205 #if defined(linux)
206 #define HAVE_DECL_EXPM1 1
207 #else
208 #define HAVE_DECL_EXPM1 0
209 #endif
210 #if defined(linux) || defined (_WIN32)
211 #define HAVE_DECL_FINITE 1
212 #else
213 #define HAVE_DECL_FINITE 0
214 #endif
215 #if defined(linux)
216 #define HAVE_DECL_HYPOT 1
217 #else
218 #define HAVE_DECL_HYPOT 0
219 #endif
220 #if defined(linux)
221 #define HAVE_DECL_ISFINITE 0
222 #else
223 #define HAVE_DECL_ISFINITE 0
224 #endif
225 #if defined(linux)
226 #define HAVE_DECL_LOG1P 1
227 #else
228 #define HAVE_DECL_LOG1P 0
229 #endif
230 #if defined(linux)
231 #define HAVE_STRDUP 1
232 #else
233 #define HAVE_STRDUP 0
234 #endif
235 #if defined(linux)
236 #define HAVE_STRTOL 1
237 #else
238 #define HAVE_STRTOL 0
239 #endif
240 #if defined(linux)
241 #define HAVE_STRTOUL 1
242 #else
243 #define HAVE_STRTOUL 0
244 #endif
245 #if defined(linux)
246 #define HAVE_DLFCN_H 1
247 #else
248 #define HAVE_DLFCN_H 0
249 #endif
250 #if defined(linux)
251 #define HAVE_DECL_FEENABLEEXCEPT 1
252 #else
253 #define HAVE_DECL_FEENABLEEXCEPT 0
254 #endif
255 # 3.Inlines:
256 #undef HAVE_INLINE
257 #ifdef sgi
258 #define inline
259 #endif
260 #if defined(linux) || defined (macintosh) || defined (_WIN32)
261 #define HAVE_DECL_ISINF 1
262 #else
263 #define HAVE_DECL_ISINF 0
264 #endif
265 #if defined(linux) || defined (macintosh) || defined (_WIN32)
266 #define HAVE_DECL_ISNAN 1
267 #else
268 #define HAVE_DECL_ISNAN 0
269 #endif
270 # 4.
271 #if defined(linux)
272 #define HAVE_GNUX86_IEEE_INTERFACE 1
273 #endif
274 #undef HAVE_GNUSPARC_IEEE_INTERFACE
275 #undef HAVE_GNUM68K_IEEE_INTERFACE
276 #undef HAVE_GNUPPC_IEEE_INTERFACE
277 #undef HAVE_SUNOS4_IEEE_INTERFACE
278 #undef HAVE_SOLARIS_IEEE_INTERFACE
279 #undef HAVE_HPUX11_IEEE_INTERFACE
280 #undef HAVE_HPUX_IEEE_INTERFACE
281 #undef HAVE_TRU64_IEEE_INTERFACE
282 #undef HAVE_IRIX_IEEE_INTERFACE
283 #undef HAVE_AIX_IEEE_INTERFACE
284 #undef HAVE_FREEBSD_IEEE_INTERFACE
285 #undef HAVE_OS2EMX_IEEE_INTERFACE
286 #undef HAVE_NETBSD_IEEE_INTERFACE
287 #undef HAVE_OPENBSD_IEEE_INTERFACE
288 #undef HAVE_DARWIN_IEEE_INTERFACE
289 #undef HAVE_DARWIN86_IEEE_INTERFACE
290
291 #define GSL_DISABLE_DEPRECATED 1
292
293 #define USE_BLAS 0
294
295 By hand: Corrected in the fromdir/specfun/coupling.c:
296 #if ! defined (GSL_DISABLE_DEPRECATED)
297 gsl_sf_coupling_6j_INCORRECT_e ....
298 #endif
299 #if ! defined (GSL_DISABLE_DEPRECATED)
300 gsl_sf_coupling_6j_INCORRECT ....
301 #endif
302
303 """
304
305 def post_processing (current_gsl_version):
306 def correct_missing_prototypes (file, linenumber, prototype):
307 f = open (file, 'r')
308 lines = f.readlines ()
309 lines.insert (linenumber, prototype + '\n')
310 f.close ()
311 f = open (file, 'w')
312 f.writelines (lines)
313 f.close ()
314 print >> log, 'Inserted prototype in file '+ file
315 print >> log, ' ' + prototype
316 if current_gsl_version == '1.10':
317 here = todir + '/'
318 correct_missing_prototypes (here + 'gsl_matrix_complex_double.h', 235, \
319 'int gsl_matrix_complex_isnonneg (const gsl_matrix_complex * m);')
320 correct_missing_prototypes (here + 'gsl_matrix_complex_float.h', 235, \
321 'int gsl_matrix_complex_float_isnonneg (const gsl_matrix_complex_float * m);')
322 correct_missing_prototypes (here + 'gsl_matrix_complex_long_double.h', 235, \
323 'int gsl_matrix_complex_long_double_isnonneg (const gsl_matrix_complex_long_double * m);')
324 correct_missing_prototypes (here + 'gsl_vector_complex_double.h', 181, \
325 'int gsl_vector_complex_isnonneg (const gsl_vector_complex * v);')
326 correct_missing_prototypes (here + 'gsl_vector_complex_float.h', 181, \
327 'int gsl_vector_complex_float_isnonneg (const gsl_vector_complex_float * v);')
328 correct_missing_prototypes (here + 'gsl_vector_complex_long_double.h', 181, \
329 'int gsl_vector_complex_long_double_isnonneg (const gsl_vector_complex_long_double * v);')
330
331 def remove_double_header_files (dict):
332 num = 0
333 for key,val in dict.items():
334 (dir, base) = os.path.split (key)
335 if dir == 'gsl': continue
336 newkey = 'gsl/' + base
337 if dict.has_key (newkey):
338 f = open (fromdir + key, 'r')
339 lines1 = f.read()
340 f.close ()
341 f = open (fromdir + newkey, 'r')
342 lines2 = f.read()
343 f.close ()
344 if lines1 == lines2:
345 num += 1
346 dict[key][idist] = 'n'
347 dict[key][ihrep] = base
348 print >> log, 'Also in gsl/: ' + key
349 return num
350
351 def exclude_test_files (dict):
352 starts_with_test = re.compile (r'^test')
353 for key in dict.keys ():
354 (dir, base) = os.path.split (key)
355 if starts_with_test.search (base):
356 dict[key][idist] = 'n'
357 dict['siman/siman_tsp.c'][idist] = 'n'
358
359 def print_file_selection (dict, type = 'c', inmake = 'n', indist = 'y'):
360 selection = {}
361 for key, val in dict.items():
362 if val[itype] == type and val[imake] == inmake and val[idist] == indist:
363 selection[key] = val[icount]
364 keyss = selection.keys()
365 keyss.sort()
366 for key in keyss:
367 print >> log, '%3d %s' % (selection[key], key)
368 return len (keyss)
369
370 preprocessing (version)
371 files = get_all_files (dirs)
372 exclude_test_files (files)
373 print >> log, 'The following header files already exist in the gsl/ directory:'
374 ll = remove_double_header_files (files)
375 print >> log, 'Number of files = %d' % ll
376 print >> log, '\n\n process files'
377 process_files (files)
378 print >> log, '\n\n corect some header files:'
379 post_processing (version)
380 gen_sconscript (files)
381 gen_makefile (files)
382 print >> log, '\n\n header-files'
383 ll = print_file_selection (files, type = 'h')
384 print >> log, 'Number of files = %d' % ll
385 print >> log, '\n\n c-files'
386 ll = print_file_selection (files, type = 'c', inmake = 'y', indist = 'y')
387 print >> log, 'Number of files = %d' % ll
388 print >> log, '\n\n c-files included in other files'
389 ll = print_file_selection (files, type = 'c', inmake = 'n', indist = 'y')
390 print >> log, 'Number of files = %d' % ll
391 print >> log, '\n\n unused header-files'
392 ll = print_file_selection (files, type = 'h', indist = 'n')
393 print >> log, 'Number of files = %d' % ll
394 print >> log, '\n\n unused c-files'
395 ll = print_file_selection (files, type = 'c', indist = 'n')
396 print >> log, 'Number of files = %d' % ll
397
398
399 #print str(files)