piotr | 437f546 | 2014-02-04 17:57:25 +0100 | [diff] [blame] | 1 | # |
| 2 | # Copyright 2004,2009,2012 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 3, 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 | # set do_makefile to either true or false dependeing on the environment |
| 37 | try: |
| 38 | if os.environ['do_makefile'] == '0': |
| 39 | do_makefile = False |
| 40 | else: |
| 41 | do_makefile = True |
| 42 | except KeyError, e: |
| 43 | do_makefile = False |
| 44 | |
| 45 | # set do_sources to either true or false dependeing on the environment |
| 46 | try: |
| 47 | if os.environ['do_sources'] == '0': |
| 48 | do_sources = False |
| 49 | else: |
| 50 | do_sources = True |
| 51 | except KeyError, e: |
| 52 | do_sources = True |
| 53 | |
| 54 | name_dict = {} |
| 55 | |
| 56 | def log_output_name (name): |
| 57 | (base, ext) = os.path.splitext (name) |
| 58 | ext = ext[1:] # drop the leading '.' |
| 59 | |
| 60 | entry = name_dict.setdefault (ext, []) |
| 61 | entry.append (name) |
| 62 | |
| 63 | def open_and_log_name (name, dir): |
| 64 | global do_sources |
| 65 | if do_sources: |
| 66 | f = open (name, dir) |
| 67 | else: |
| 68 | f = None |
| 69 | log_output_name (name) |
| 70 | return f |
| 71 | |
| 72 | def expand_template (d, template_filename, extra = ""): |
| 73 | '''Given a dictionary D and a TEMPLATE_FILENAME, expand template into output file |
| 74 | ''' |
| 75 | global do_sources |
| 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) |
| 80 | if do_sources: |
| 81 | output = open (output_name, 'w') |
| 82 | do_substitution (d, template, output) |
| 83 | output.close () |
| 84 | template.close () |
| 85 | |
| 86 | def output_glue (dirname): |
| 87 | output_makefile_fragment () |
| 88 | output_ifile_include (dirname) |
| 89 | |
| 90 | def output_makefile_fragment (): |
| 91 | global do_makefile |
| 92 | if not do_makefile: |
| 93 | return |
| 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 (os.path.join (os.environ.get('gendir', os.environ.get('srcdir', '.')), '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') |
| 101 | f.close () |
| 102 | |
| 103 | def output_ifile_include (dirname): |
| 104 | global do_sources |
| 105 | if do_sources: |
| 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', []) |
| 109 | files.sort () |
| 110 | f.write ('%{\n') |
| 111 | for file in files: |
| 112 | f.write ('#include <%s>\n' % (file[0:-1] + 'h',)) |
| 113 | f.write ('%}\n\n') |
| 114 | for file in files: |
| 115 | f.write ('%%include <%s>\n' % (file,)) |
| 116 | |
| 117 | def output_subfrag (f, ext): |
| 118 | files = name_dict.setdefault (ext, []) |
| 119 | files.sort () |
| 120 | f.write ("GENERATED_%s =" % (ext.upper ())) |
| 121 | for file in files: |
| 122 | f.write (" \\\n\t%s" % (file,)) |
| 123 | f.write ("\n\n") |
| 124 | |
| 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) |
| 129 | if not mo: |
| 130 | raise ValueError, "Incorrectly formed template_name '%s'" % (template_name,) |
| 131 | return mo.group (1) |
| 132 | |
| 133 | def open_src (name, mode): |
| 134 | global srcdir |
| 135 | return open (os.path.join (srcdir, name), mode) |
| 136 | |
| 137 | def do_substitution (d, in_file, out_file): |
| 138 | def repl (match_obj): |
| 139 | key = match_obj.group (1) |
| 140 | # print key |
| 141 | return d[key] |
| 142 | |
| 143 | inp = in_file.read () |
| 144 | out = re.sub (r"@([a-zA-Z0-9_]+)@", repl, inp) |
| 145 | out_file.write (out) |
| 146 | |
| 147 | |
| 148 | |
| 149 | copyright = '''/* -*- c++ -*- */ |
| 150 | /* |
| 151 | * Copyright 2003,2004 Free Software Foundation, Inc. |
| 152 | * |
| 153 | * This file is part of GNU Radio |
| 154 | * |
| 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) |
| 158 | * any later version. |
| 159 | * |
| 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. |
| 164 | * |
| 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. |
| 169 | */ |
| 170 | ''' |
| 171 | |
| 172 | def is_complex (code3): |
| 173 | if i_code (code3) == 'c' or o_code (code3) == 'c': |
| 174 | return '1' |
| 175 | else: |
| 176 | return '0' |
| 177 | |
| 178 | |
| 179 | def standard_dict (name, code3, package='gr'): |
| 180 | d = {} |
| 181 | d['NAME'] = name |
| 182 | d['NAME_IMPL'] = name+'_impl' |
| 183 | d['GUARD_NAME'] = 'INCLUDED_%s_%s_H' % (package.upper(), name.upper()) |
| 184 | d['GUARD_NAME_IMPL'] = 'INCLUDED_%s_%s_IMPL_H' % (package.upper(), name.upper()) |
| 185 | d['BASE_NAME'] = re.sub ('^' + package + '_', '', name) |
| 186 | d['SPTR_NAME'] = '%s_sptr' % name |
| 187 | d['WARNING'] = 'WARNING: this file is machine generated. Edits will be overwritten' |
| 188 | d['COPYRIGHT'] = copyright |
| 189 | d['TYPE'] = i_type (code3) |
| 190 | d['I_TYPE'] = i_type (code3) |
| 191 | d['O_TYPE'] = o_type (code3) |
| 192 | d['TAP_TYPE'] = tap_type (code3) |
| 193 | d['IS_COMPLEX'] = is_complex (code3) |
| 194 | return d |
| 195 | |
| 196 | |
| 197 | def standard_dict2 (name, code3, package): |
| 198 | d = {} |
| 199 | d['NAME'] = name |
| 200 | d['BASE_NAME'] = name |
| 201 | d['GUARD_NAME'] = 'INCLUDED_%s_%s_H' % (package.upper(), name.upper()) |
| 202 | d['WARNING'] = 'WARNING: this file is machine generated. Edits will be overwritten' |
| 203 | d['COPYRIGHT'] = copyright |
| 204 | d['TYPE'] = i_type (code3) |
| 205 | d['I_TYPE'] = i_type (code3) |
| 206 | d['O_TYPE'] = o_type (code3) |
| 207 | d['TAP_TYPE'] = tap_type (code3) |
| 208 | d['IS_COMPLEX'] = is_complex (code3) |
| 209 | return d |
| 210 | |
| 211 | def standard_impl_dict2 (name, code3, package): |
| 212 | d = {} |
| 213 | d['NAME'] = name |
| 214 | d['IMPL_NAME'] = name |
| 215 | d['BASE_NAME'] = name.rstrip("impl").rstrip("_") |
| 216 | d['GUARD_NAME'] = 'INCLUDED_%s_%s_H' % (package.upper(), name.upper()) |
| 217 | d['WARNING'] = 'WARNING: this file is machine generated. Edits will be overwritten' |
| 218 | d['COPYRIGHT'] = copyright |
| 219 | d['FIR_TYPE'] = "fir_filter_" + code3 |
| 220 | d['CFIR_TYPE'] = "fir_filter_" + code3[0:2] + 'c' |
| 221 | d['TYPE'] = i_type (code3) |
| 222 | d['I_TYPE'] = i_type (code3) |
| 223 | d['O_TYPE'] = o_type (code3) |
| 224 | d['TAP_TYPE'] = tap_type (code3) |
| 225 | d['IS_COMPLEX'] = is_complex (code3) |
| 226 | return d |