2 # Copyright 2004 Free Software Foundation, Inc.
4 # This file is part of GNU Radio
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)
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.
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.
22 """Misc utilities used at build time
25 import re, os, os.path
26 from build_utils_codes import *
29 # set srcdir to the directory that contains Makefile.am
31 srcdir = os.environ['srcdir']
39 def log_output_name (name):
40 (base, ext) = os.path.splitext (name)
41 ext = ext[1:] # drop the leading '.'
43 entry = name_dict.setdefault (ext, [])
46 def open_and_log_name (name, dir):
48 log_output_name (name)
51 def expand_template (d, template_filename, extra = ""):
52 '''Given a dictionary D and a TEMPLATE_FILENAME, expand template into output file
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)
63 def output_glue (dirname):
64 output_makefile_fragment ()
65 output_ifile_include (dirname)
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')
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', [])
82 f.write ('#include <%s>\n' % (file[0:-1] + 'h',))
85 f.write ('%%include <%s>\n' % (file,))
87 def output_subfrag (f, ext):
88 files = name_dict.setdefault (ext, [])
90 f.write ("GENERATED_%s =" % (ext.upper ()))
92 f.write (" \\\n\t%s" % (file,))
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)
101 raise ValueError, "Incorrectly formed template_name '%s'" % (template_name,)
104 def open_src (name, mode):
106 return open (os.path.join (srcdir, name), mode)
108 def do_substitution (d, in_file, out_file):
109 def repl (match_obj):
110 key = match_obj.group (1)
114 inp = in_file.read ()
115 out = re.sub (r"@([a-zA-Z0-9_]+)@", repl, inp)
120 copyright = '''/* -*- c++ -*- */
122 * Copyright 2003,2004 Free Software Foundation, Inc.
124 * This file is part of GNU Radio
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)
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.
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.
143 def is_complex (code3):
144 if i_code (code3) == 'c' or o_code (code3) == 'c':
150 def standard_dict (name, code3):
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)