OpenCores
URL https://opencores.org/ocsvn/c0or1k/c0or1k/trunk

Subversion Repositories c0or1k

[/] [c0or1k/] [trunk/] [tools/] [cml2header.py] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 drasko
#!/usr/bin/python
2
 
3
import os
4
import sys
5
import re
6
import StringIO
7
import getopt
8
 
9
class cml2header_translator:
10
        '''
11
        Translates a cml2 configuration file to a C header file
12
        that can be included in source code.
13
        '''
14
        def __init__(self):
15
                self.isnotset = re.compile("^# (.*) is not set")
16
 
17
        def linetrans(self, instream, outstream, trailer=None):
18
                '''
19
                Translates a stream line-by-line
20
                '''
21
                if not hasattr(instream, "readline"):
22
                        instream = open(instream, "r")
23
                if not hasattr(outstream, "readline"):
24
                        outstream = open(outstream, "w")
25
                while 1:
26
                        line = instream.readline()
27
                        if not line:
28
                                break
29
                        new = self.write_include(line)
30
                        if new:
31
                                outstream.write(new)
32
                instream.close()
33
                if trailer:
34
                        outstream.write(trailer)
35
                outstream.close()
36
 
37
        def write_include(self, line):
38
                '''
39
                Translate a line of CML2 config file statement into
40
                C preprocessor #define format.
41
                '''
42
                if line.find("PRIVATE") > -1 or line[:2] == "$$":
43
                        return ""
44
                match = self.isnotset.match(line)
45
                if match:
46
                        return "#undef  %s\n" % match.group(1)
47
                if line == "#\n":
48
                        return None
49
                elif line[0] == "#":
50
                        return "/* " + line[1:].strip() + " */\n"
51
                eq = line.find("=")
52
                if eq == -1:
53
                        return line
54
                else:
55
                        line = line.split('#')[0]
56
                        symbol = line[:eq]
57
                        value = line[eq+1 :].strip()
58
                if value == 'y':
59
                        return "#define %s 1\n" % symbol
60
                elif value == 'm':
61
                        return "#undef %s\n#define %s_MODULE 1\n" % (symbol, symbol)
62
                elif value == 'n':
63
                        return "#undef  %s\n" % symbol
64
                else:
65
                        return "#define %s %s\n" % (symbol, value)
66
 
67
        def cml_configfile_exists(self, path):
68
                if not os.path.exists(path):
69
                        print "CML2 configuration file not found."
70
                        print "Given path:", path, "does not exist."
71
                        return 0
72
                else:
73
                        return 1
74
 
75
        def translate(self, configfile_path = None, headerfile_path = None):
76
 
77
                if configfile_path == None:
78
                        configfile_path = os.getcwd() + '/' + "config.out"
79
 
80
                if headerfile_path == None:
81
                        headerfile_path = os.getcwd() + '/' + "config.h"
82
                '''
83
                Takes a configuration file generated by the cml2 configurator,
84
                and converts it to a C header file. This header file is then
85
                included by the kernel sources and the preprocessor definitions
86
                in it are used during the compilation of the kernel.
87
                '''
88
                if not self.cml_configfile_exists(configfile_path):
89
                        print "Failed to translate cml configuration file", configfile_path, 'to', headerfile_path
90
                        return -1
91
 
92
                self.linetrans(configfile_path, headerfile_path)
93
                return 0
94
 
95
        def usage(self):
96
                helpstr =       \
97
                '''\
98
    Description:
99
    cml2header.py takes a cml2 configuration file as input and generates
100
    a valid C header file to be included in C source code.
101
 
102
    Usage:
103
    cml2header.py -i inputfile -o outputfile [-h]
104
 
105
    Options:
106
        -i    The CML2 configuration file       (config.out by default)
107
        -o    The name of the header file       (config.h by default)
108
        -h    This help message
109
                '''
110
                print helpstr
111
 
112
def main():
113
        translator = cml2header_translator()
114
        try:
115
                opts, args = getopt.getopt(sys.argv[1:], "i:o:h:")
116
        except getopt.GetoptError:
117
                # print help information and exit:
118
                translator.usage()
119
                sys.exit(2)
120
 
121
        ofile = None
122
        ifile = None
123
 
124
        # Print help message if requested and quit
125
        if '-h' in opts:
126
                translator.usage()
127
                sys.exit()
128
 
129
        # Get the input and output filenames.
130
        for switch, val in opts:
131
                if switch == '-i':
132
                        ifile = val
133
                if switch == '-o':
134
                        ofile = val
135
 
136
        # Translate
137
        if translator.translate(ifile, ofile) < 0:
138
                print "Translation of", ifile, 'to', ofile, 'failed.'
139
 
140
# Run the main routine only if this file is explicitly executed.
141
# (i.e. not referred from another module)
142
if __name__ == "__main__":
143
    main()
144
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.