Imported Upstream version 3.0
[debian/gnuradio] / gnuradio-core / src / python / build_utils.py
1 #
2 # Copyright 2004 Free Software Foundation, Inc.
3
4 # This file is part of GNU Radio
5
6 # GNU Radio is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2, or (at your option)
9 # any later version.
10
11 # GNU Radio is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with GNU Radio; see the file COPYING.  If not, write to
18 # the Free Software Foundation, Inc., 51 Franklin Street,
19 # Boston, MA 02110-1301, USA.
20
21
22 """Misc utilities used at build time
23 """
24
25 import re, os, os.path
26 from build_utils_codes import *
27
28
29 # set srcdir to the directory that contains Makefile.am
30 try:
31     srcdir = os.environ['srcdir']
32 except KeyError, e:
33     srcdir = "."
34 srcdir = srcdir + '/'
35
36
37 name_dict = {}
38
39 def log_output_name (name):
40     (base, ext) = os.path.splitext (name)
41     ext = ext[1:]                       # drop the leading '.'
42     
43     entry = name_dict.setdefault (ext, [])
44     entry.append (name)
45     
46 def open_and_log_name (name, dir):
47     f = open (name, dir)
48     log_output_name (name)
49     return f
50
51 def expand_template (d, template_filename, extra = ""):
52     '''Given a dictionary D and a TEMPLATE_FILENAME, expand template into output file
53     '''
54     output_extension = extract_extension (template_filename)
55     template = open_src (template_filename, 'r')
56     output_name = d['NAME'] + extra + '.' + output_extension
57     log_output_name (output_name)
58     output = open (output_name, 'w')
59     do_substitution (d, template, output)
60     template.close ()
61     output.close ()
62
63 def output_glue (dirname):
64     output_makefile_fragment ()
65     output_ifile_include (dirname)
66     
67 def output_makefile_fragment ():
68     f = open ('Makefile.gen', 'w')
69     f.write ('#\n# This file is machine generated.  All edits will be overwritten\n#\n')
70     output_subfrag (f, 'h')
71     output_subfrag (f, 'i')
72     output_subfrag (f, 'cc')
73     f.close ()
74
75 def output_ifile_include (dirname):
76     f = open ('%s_generated.i' % (dirname,), 'w')
77     f.write ('//\n// This file is machine generated.  All edits will be overwritten\n//\n')
78     files = name_dict.setdefault ('i', [])
79     files.sort ()
80     f.write ('%{\n')
81     for file in files:
82         f.write ('#include <%s>\n' % (file[0:-1] + 'h',))
83     f.write ('%}\n\n')
84     for file in files:
85         f.write ('%%include <%s>\n' % (file,))
86
87 def output_subfrag (f, ext):
88     files = name_dict.setdefault (ext, [])
89     files.sort ()
90     f.write ("GENERATED_%s =" % (ext.upper ()))
91     for file in files:
92         f.write (" \\\n\t%s" % (file,))
93     f.write ("\n\n")
94     
95
96 def extract_extension (template_name):
97     # template name is something like: GrFIRfilterXXX.h.t
98     # we return everything between the penultimate . and .t
99     mo = re.search (r'\.([a-z]+)\.t$', template_name)
100     if not mo:
101         raise ValueError, "Incorrectly formed template_name '%s'" % (template_name,)
102     return mo.group (1)
103
104 def open_src (name, mode):
105     global srcdir
106     return open (os.path.join (srcdir, name), mode)
107
108 def do_substitution (d, in_file, out_file):
109     def repl (match_obj):
110         key = match_obj.group (1)
111         # print key
112         return d[key]
113     
114     inp = in_file.read ()
115     out = re.sub (r"@([a-zA-Z0-9_]+)@", repl, inp)
116     out_file.write (out)
117
118     
119
120 copyright = '''/* -*- c++ -*- */
121 /*
122  * Copyright 2003,2004 Free Software Foundation, Inc.
123  * 
124  * This file is part of GNU Radio
125  * 
126  * GNU Radio is free software; you can redistribute it and/or modify
127  * it under the terms of the GNU General Public License as published by
128  * the Free Software Foundation; either version 2, or (at your option)
129  * any later version.
130  * 
131  * GNU Radio is distributed in the hope that it will be useful,
132  * but WITHOUT ANY WARRANTY; without even the implied warranty of
133  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
134  * GNU General Public License for more details.
135  * 
136  * You should have received a copy of the GNU General Public License
137  * along with GNU Radio; see the file COPYING.  If not, write to
138  * the Free Software Foundation, Inc., 51 Franklin Street,
139  * Boston, MA 02110-1301, USA.
140  */
141 '''
142
143 def is_complex (code3):
144     if i_code (code3) == 'c' or o_code (code3) == 'c':
145         return '1'
146     else:
147         return '0'
148
149
150 def standard_dict (name, code3):
151     d = {}
152     d['NAME'] = name
153     d['GUARD_NAME'] = 'INCLUDED_%s_H' % name.upper ()
154     d['BASE_NAME'] = re.sub ('^gr_', '', name)
155     d['SPTR_NAME'] = '%s_sptr' % name
156     d['WARNING'] = 'WARNING: this file is machine generated.  Edits will be over written'
157     d['COPYRIGHT'] = copyright
158     d['TYPE'] = i_type (code3)
159     d['I_TYPE'] = i_type (code3)
160     d['O_TYPE'] = o_type (code3)
161     d['TAP_TYPE'] = tap_type (code3)
162     d['IS_COMPLEX'] = is_complex (code3)
163     return d