3 # Copyright (c) 2004-2005 The Regents of The University of Michigan
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are
8 # met: redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer;
10 # redistributions in binary form must reproduce the above copyright
11 # notice, this list of conditions and the following disclaimer in the
12 # documentation and/or other materials provided with the distribution;
13 # neither the name of the copyright holders nor the names of its
14 # contributors may be used to endorse or promote products derived from
15 # this software without specific prior written permission.
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 import os, os.path, re, sys
35 def WriteEmbeddedPyFile(target, source, path, name, ext, filename):
36 if isinstance(source, str):
37 source = file(source, 'r')
39 if isinstance(target, str):
40 target = file(target, 'w')
42 print >>target, "AddModule(%s, %s, %s, %s, '''\\" % \
43 (`path`, `name`, `ext`, `filename`)
47 # escape existing backslashes
48 line = line.replace('\\', '\\\\')
49 # escape existing triple quotes
50 line = line.replace("'''", r"\'\'\'")
54 print >>target, "''')"
57 def WriteCFile(target, source, name):
58 if isinstance(source, str):
59 source = file(source, 'r')
61 if isinstance(target, str):
62 target = file(target, 'w')
64 print >>target, 'const char %s_string[] = {' % name
67 from array import array
71 foo.fromfile(source, 10000)
72 l = [ str(i) for i in foo.tolist() ]
74 for i in xrange(0,9999,20):
75 print >>target, ','.join(l[i:i+20]) + ','
77 l = [ str(i) for i in foo.tolist() ]
79 for i in xrange(0,len(l),20):
80 print >>target, ','.join(l[i:i+20]) + ','
81 print >>target, ','.join(l[i:]) + ','
84 print >>target, 'const int %s_length = %d;' % (name, count)
88 dir,file = os.path.split(path)
92 dir,base = os.path.split(dir)
96 def MakeEmbeddedPyFile(target, source, env):
97 target = file(str(target[0]), 'w')
102 path,pyfile = splitpath(src)
105 if not node.has_key(dir):
109 name,ext = pyfile.split('.')
110 if name == '__init__':
111 node['.hasinit'] = True
112 node[pyfile] = (src,name,ext,src)
117 for name,entry in tree.items():
118 if not isinstance(entry, dict): continue
119 if entry.has_key('.hasinit'): continue
123 for key,val in entry.iteritems():
124 if tree.has_key(key):
126 "dir already has %s can't add it again" % key
130 def populate(node, path = []):
134 if name == '.hasinit':
138 if isinstance(entry, dict):
139 if not entry.has_key('.hasinit'):
140 raise NameError, 'package directory missing __init__.py'
141 populate(entry, path + [ name ])
143 pyfile,name,ext,filename = entry
144 files.append((pyfile, path, name, ext, filename))
147 for pyfile, path, name, ext, filename in files:
148 WriteEmbeddedPyFile(target, pyfile, path, name, ext, filename)
150 def MakeDefinesPyFile(target, source, env):
151 f = file(str(target[0]), 'w')
152 print >>f, "import __main__"
153 print >>f, "__main__.m5_build_env = ",
158 def MakePythonCFile(target, source, env):
160 target = file(str(target[0]), 'w')
163 #include "base/embedfile.hh"
169 fname = os.path.basename(src)
170 name = 'embedded_file%d' % CFileCounter
172 WriteCFile(target, src, name)
174 EmbedMap %(name)s("%(fname)s",
175 %(name)s_string, %(name)s_length);
183 # base list of .py files to embed
184 embedded_py_files = [ '../util/pbs/jobfile.py' ]
185 # add all .py files in python/m5
186 objpath = os.path.join(env['SRCDIR'], 'python', 'm5')
187 for root, dirs, files in os.walk(objpath, topdown=True):
188 for i,dir in enumerate(dirs):
193 assert(root.startswith(objpath))
195 if f.endswith('.py'):
196 embedded_py_files.append(os.path.join(root, f))
198 embedfile_hh = os.path.join(env['SRCDIR'], 'base/embedfile.hh')
200 optionDict = dict([(opt, env[opt]) for opt in env.ExportOptions])
201 env.Command('defines.py', Value(optionDict), MakeDefinesPyFile)
203 env.Command('embedded_py.py', embedded_py_files, MakeEmbeddedPyFile)
204 env.Depends('embedded_py.cc', embedfile_hh)
205 env.Command('embedded_py.cc',
206 ['string_importer.py', 'defines.py', 'embedded_py.py'],