2 # Copyright 2004,2009 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 3, 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']
36 # set do_makefile to either true or false dependeing on the environment
38 if os.environ['do_makefile'] == '0':
45 # set do_sources to either true or false dependeing on the environment
47 if os.environ['do_sources'] == '0':
56 def log_output_name (name):
57 (base, ext) = os.path.splitext (name)
58 ext = ext[1:] # drop the leading '.'
60 entry = name_dict.setdefault (ext, [])
63 def open_and_log_name (name, dir):
69 log_output_name (name)
72 def expand_template (d, template_filename, extra = ""):
73 '''Given a dictionary D and a TEMPLATE_FILENAME, expand template into output file
76 output_extension = extract_extension (template_filename)
77 template = open_src (template_filename, 'r')
78 output_name = d['NAME'] + extra + '.' + output_extension
79 log_output_name (output_name)
81 output = open (output_name, 'w')
82 do_substitution (d, template, output)
86 def output_glue (dirname):
87 output_makefile_fragment ()
88 output_ifile_include (dirname)
90 def output_makefile_fragment ():
94 # overwrite the source, which must be writable; this should have been
95 # checked for beforehand in the top-level Makefile.gen.gen .
96 f = open_src ('Makefile.gen', 'w')
97 f.write ('#\n# This file is machine generated. All edits will be overwritten\n#\n')
98 output_subfrag (f, 'h')
99 output_subfrag (f, 'i')
100 output_subfrag (f, 'cc')
103 def output_ifile_include (dirname):
106 f = open ('%s_generated.i' % (dirname,), 'w')
107 f.write ('//\n// This file is machine generated. All edits will be overwritten\n//\n')
108 files = name_dict.setdefault ('i', [])
112 f.write ('#include <%s>\n' % (file[0:-1] + 'h',))
115 f.write ('%%include <%s>\n' % (file,))
117 def output_subfrag (f, ext):
118 files = name_dict.setdefault (ext, [])
120 f.write ("GENERATED_%s =" % (ext.upper ()))
122 f.write (" \\\n\t%s" % (file,))
125 def extract_extension (template_name):
126 # template name is something like: GrFIRfilterXXX.h.t
127 # we return everything between the penultimate . and .t
128 mo = re.search (r'\.([a-z]+)\.t$', template_name)
130 raise ValueError, "Incorrectly formed template_name '%s'" % (template_name,)
133 def open_src (name, mode):
135 return open (os.path.join (srcdir, name), mode)
137 def do_substitution (d, in_file, out_file):
138 def repl (match_obj):
139 key = match_obj.group (1)
143 inp = in_file.read ()
144 out = re.sub (r"@([a-zA-Z0-9_]+)@", repl, inp)
149 copyright = '''/* -*- c++ -*- */
151 * Copyright 2003,2004 Free Software Foundation, Inc.
153 * This file is part of GNU Radio
155 * GNU Radio is free software; you can redistribute it and/or modify
156 * it under the terms of the GNU General Public License as published by
157 * the Free Software Foundation; either version 3, or (at your option)
160 * GNU Radio is distributed in the hope that it will be useful,
161 * but WITHOUT ANY WARRANTY; without even the implied warranty of
162 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
163 * GNU General Public License for more details.
165 * You should have received a copy of the GNU General Public License
166 * along with GNU Radio; see the file COPYING. If not, write to
167 * the Free Software Foundation, Inc., 51 Franklin Street,
168 * Boston, MA 02110-1301, USA.
172 def is_complex (code3):
173 if i_code (code3) == 'c' or o_code (code3) == 'c':
179 def standard_dict (name, code3):
182 d['GUARD_NAME'] = 'INCLUDED_%s_H' % name.upper ()
183 d['BASE_NAME'] = re.sub ('^gr_', '', name)
184 d['SPTR_NAME'] = '%s_sptr' % name
185 d['WARNING'] = 'WARNING: this file is machine generated. Edits will be over written'
186 d['COPYRIGHT'] = copyright
187 d['TYPE'] = i_type (code3)
188 d['I_TYPE'] = i_type (code3)
189 d['O_TYPE'] = o_type (code3)
190 d['TAP_TYPE'] = tap_type (code3)
191 d['IS_COMPLEX'] = is_complex (code3)