SConscript revision 1530
11046SN/A# -*- mode:python -*-
21046SN/A
31762SN/A# Copyright (c) 2005 The Regents of The University of Michigan
41046SN/A# All rights reserved.
51046SN/A#
61046SN/A# Redistribution and use in source and binary forms, with or without
71046SN/A# modification, are permitted provided that the following conditions are
81046SN/A# met: redistributions of source code must retain the above copyright
91046SN/A# notice, this list of conditions and the following disclaimer;
101046SN/A# redistributions in binary form must reproduce the above copyright
111046SN/A# notice, this list of conditions and the following disclaimer in the
121046SN/A# documentation and/or other materials provided with the distribution;
131046SN/A# neither the name of the copyright holders nor the names of its
141046SN/A# contributors may be used to endorse or promote products derived from
151046SN/A# this software without specific prior written permission.
161046SN/A#
171046SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
181046SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
191046SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
201046SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
211046SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
221046SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
231046SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
241046SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
251046SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
261046SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
271046SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
282665Ssaidi@eecs.umich.edu
292665Ssaidi@eecs.umich.eduimport os, os.path, re, sys
302665Ssaidi@eecs.umich.edu
311046SN/AImport('env')
324202Sbinkertn@umich.edu
331530SN/A# tell python where to find m5 python code
345522Snate@binkert.orgsys.path.append(os.path.join(env['SRCDIR'], 'python'))
354382Sbinkertn@umich.edu
364382Sbinkertn@umich.eduimport m5scons
375471Snate@binkert.org
385801Snate@binkert.orgdef WriteEmbeddedPyFile(target, source, path, name, ext, filename):
395801Snate@binkert.org    if isinstance(source, str):
404382Sbinkertn@umich.edu        source = file(source, 'r')
414382Sbinkertn@umich.edu
425470Snate@binkert.org    if isinstance(target, str):
434382Sbinkertn@umich.edu        target = file(target, 'w')
444382Sbinkertn@umich.edu
454762Snate@binkert.org    print >>target, "AddModule(%s, %s, %s, %s, '''\\" % \
464382Sbinkertn@umich.edu          (`path`, `name`, `ext`, `filename`)
475799Snate@binkert.org
487674Snate@binkert.org    for line in source:
498295Snate@binkert.org        line = line
505467Snate@binkert.org        # escape existing backslashes
515467Snate@binkert.org        line = line.replace('\\', '\\\\')
526502Snate@binkert.org        # escape existing triple quotes
536654Snate@binkert.org        line = line.replace("'''", r"\'\'\'")
548999Suri.wiener@arm.com
556501Snate@binkert.org        print >>target, line,
565467Snate@binkert.org
575467Snate@binkert.org    print >>target, "''')"
586500Snate@binkert.org    print >>target
596654Snate@binkert.org
607503Snate@binkert.orgdef WriteCFile(target, source, name):
617816Ssteve.reinhardt@amd.com    if isinstance(source, str):
6211988Sandreas.sandberg@arm.com        source = file(source, 'r')
632667Sstever@eecs.umich.edu
644382Sbinkertn@umich.edu    if isinstance(target, str):
657677Snate@binkert.org        target = file(target, 'w')
6611988Sandreas.sandberg@arm.com
6712302Sgabeblack@google.com    print >>target, 'const char %s_string[] = {' % name
6812302Sgabeblack@google.com
6912302Sgabeblack@google.com    count = 0
7012302Sgabeblack@google.com    from array import array
7112302Sgabeblack@google.com    try:
72        while True:
73            foo = array('B')
74            foo.fromfile(source, 10000)
75            l = [ str(i) for i in foo.tolist() ]
76            count += len(l)
77            for i in xrange(0,9999,20):
78                print >>target, ','.join(l[i:i+20]) + ','
79    except EOFError:
80        l = [ str(i) for i in foo.tolist() ]
81        count += len(l)
82        for i in xrange(0,len(l),20):
83            print >>target, ','.join(l[i:i+20]) + ','
84        print >>target, ','.join(l[i:]) + ','
85
86    print >>target, '};'
87    print >>target, 'const int %s_length = %d;' % (name, count)
88    print >>target
89
90def splitpath(path):
91    dir,file = os.path.split(path)
92    path = []
93    assert(file)
94    while dir:
95        dir,base = os.path.split(dir)
96        path.insert(0, base)
97    return path, file
98
99def MakeEmbeddedPyFile(target, source, env):
100    target = file(str(target[0]), 'w')
101   
102    tree = {}
103    for src in source:
104        src = str(src)
105        path,pyfile = splitpath(src)
106        node = tree
107        for dir in path:
108            if not node.has_key(dir):
109                node[dir] = { }
110            node = node[dir]
111
112        name,ext = pyfile.split('.')
113        if name == '__init__':
114            node['.hasinit'] = True
115        node[pyfile] = (src,name,ext,src)
116
117    done = False
118    while not done:
119        done = True
120        for name,entry in tree.items():
121            if not isinstance(entry, dict): continue
122            if entry.has_key('.hasinit'): continue
123
124            done = False
125            del tree[name]
126            for key,val in entry.iteritems():
127                if tree.has_key(key):
128                    raise NameError, \
129                          "dir already has %s can't add it again" % key
130                tree[key] = val
131
132    files = []
133    def populate(node, path = []):
134        names = node.keys()
135        names.sort()
136        for name in names:
137            if name == '.hasinit':
138                continue
139            
140            entry = node[name]
141            if isinstance(entry, dict):
142                if not entry.has_key('.hasinit'):
143                    raise NameError, 'package directory missing __init__.py'
144                populate(entry, path + [ name ])
145            else:
146                pyfile,name,ext,filename = entry
147                files.append((pyfile, path, name, ext, filename))
148    populate(tree)
149
150    for pyfile, path, name, ext, filename in files:
151        WriteEmbeddedPyFile(target, pyfile, path, name, ext, filename)
152
153def MakeDefinesPyFile(target, source, env):
154    f = file(str(target[0]), 'w')
155    print >>f, "import __main__"
156    print >>f, "__main__.m5_build_env = ",
157    print >>f, m5scons.flatten_defines(env['CPPDEFINES'])
158    f.close()
159
160CFileCounter = 0
161def MakePythonCFile(target, source, env):
162    global CFileCounter
163    target = file(str(target[0]), 'w')
164
165    print >>target, '''\
166#include "base/embedfile.hh"
167
168namespace {
169'''
170    for src in source:
171        src = str(src)
172        fname = os.path.basename(src)
173        name = 'embedded_file%d' % CFileCounter
174        CFileCounter += 1
175        WriteCFile(target, src, name)
176        print >>target, '''\
177EmbedMap %(name)s("%(fname)s",
178    %(name)s_string, %(name)s_length);
179
180''' % locals()
181    print >>target, '''\
182
183/* namespace */ }
184'''
185
186embedded_py_files = [ 'mpy_importer.py', '../util/pbs/jobfile.py' ]
187objpath = os.path.join(env['SRCDIR'], 'python/m5')
188for root, dirs, files in os.walk(objpath, topdown=True):
189    for i,dir in enumerate(dirs):
190        if dir == 'SCCS':
191            del dirs[i]
192            break
193
194    assert(root.startswith(objpath))
195    for f in files:
196        if f.endswith('.mpy') or f.endswith('.py'):
197            embedded_py_files.append(os.path.join(root, f))
198
199embedfile_hh = os.path.join(env['SRCDIR'], 'base/embedfile.hh')
200env.Command('defines.py', None, MakeDefinesPyFile)
201env.Command('embedded_py.py', embedded_py_files, MakeEmbeddedPyFile)
202env.Depends('embedded_py.cc', embedfile_hh)
203env.Command('embedded_py.cc',
204            ['string_importer.py', 'defines.py', 'embedded_py.py'],
205            MakePythonCFile)
206