SConscript revision 4379:69a44bfdcaf7
1# -*- mode:python -*-
2
3# Copyright (c) 2004-2005 The Regents of The University of Michigan
4# All rights reserved.
5#
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.
16#
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.
28#
29# Authors: Steve Reinhardt
30#          Nathan Binkert
31
32import os
33import zipfile
34
35# handy function for path joins
36def join(*args):
37    return os.path.normpath(os.path.join(*args))
38
39Import('*')
40
41# This SConscript is in charge of collecting .py files and generating
42# a zip archive that is appended to the m5 binary.
43
44# List of files & directories to include in the zip file.  To include
45# a package, list only the root directory of the package, not any
46# internal .py files (else they will get the path stripped off when
47# they are imported into the zip file).
48pyzip_files = []
49
50# List of additional files on which the zip archive depends, but which
51# are not included in pyzip_files... i.e. individual .py files within
52# a package.
53pyzip_dep_files = []
54
55# Add the specified package to the zip archive.  Adds the directory to
56# pyzip_files and all included .py files to pyzip_dep_files.
57def addPkg(pkgdir):
58    pyzip_files.append(pkgdir)
59    origdir = os.getcwd()
60    srcdir = join(Dir('.').srcnode().abspath, pkgdir)
61    os.chdir(srcdir)
62    for path, dirs, files in os.walk('.'):
63        for i,dir in enumerate(dirs):
64            if dir == 'SCCS':
65                del dirs[i]
66                break
67
68        for f in files:
69            if f.endswith('.py'):
70                pyzip_dep_files.append(join(pkgdir, path, f))
71
72    os.chdir(origdir)
73
74# Generate Python file that contains a dict specifying the current
75# build_env flags.
76def MakeDefinesPyFile(target, source, env):
77    f = file(str(target[0]), 'w')
78    print >>f, "m5_build_env = ", source[0]
79    f.close()
80
81optionDict = dict([(opt, env[opt]) for opt in env.ExportOptions])
82env.Command('m5/defines.py', Value(optionDict), MakeDefinesPyFile)
83
84def MakeInfoPyFile(target, source, env):
85    f = file(str(target[0]), 'w')
86    for src in source:
87        data = ''.join(file(src.srcnode().abspath, 'r').xreadlines())
88        print >>f, "%s = %s" % (src, repr(data))
89    f.close()
90
91env.Command('m5/info.py',
92            [ '#/AUTHORS', '#/LICENSE', '#/README', '#/RELEASE_NOTES' ],
93            MakeInfoPyFile)
94
95# Now specify the packages & files for the zip archive.
96addPkg('m5')
97pyzip_files.append('m5/defines.py')
98pyzip_files.append('m5/info.py')
99pyzip_files.append(join(env['ROOT'], 'util/pbs/jobfile.py'))
100pyzip_files.append(join(env['ROOT'], 'src/base/traceflags.py'))
101
102swig_modules = []
103def swig_it(module):
104    cc_file = 'swig/%s_wrap.cc' % module
105    py_file = 'm5/internal/%s.py' % module
106    source = File('swig/%s.i' % module)
107    source.rfile() # Hack to cause the symlink to the .i file to be created
108    env.Command([cc_file, py_file], source,
109                '$SWIG $SWIGFLAGS -outdir ${TARGETS[1].dir} '
110                '-o ${TARGETS[0]} $SOURCES')
111    swig_modules.append(module)
112    Source('swig/%s_wrap.cc' % module)
113
114Source('swig/init.cc')
115Source('swig/pyevent.cc')
116Source('swig/pyobject.cc')
117
118swig_it('core')
119swig_it('debug')
120swig_it('event')
121swig_it('random')
122swig_it('sim_object')
123swig_it('stats')
124swig_it('trace')
125
126# Automatically generate m5/internals/__init__.py
127def MakeInternalsInit(target, source, env):
128    f = file(str(target[0]), 'w')
129    for m in swig_modules:
130        print >>f, 'import %s' % m
131    f.close()
132
133swig_py_files = [ 'm5/internal/%s.py' % m for m in swig_modules ]
134env.Command('m5/internal/__init__.py', swig_py_files, MakeInternalsInit)
135pyzip_dep_files.append('m5/internal/__init__.py')
136
137def MakeSwigInit(target, source, env):
138    f = file(str(target[0]), 'w')
139    print >>f, 'extern "C" {'
140    for m in swig_modules:
141        print >>f, '    void init_%s();' % m
142    print >>f, '}'
143    print >>f, 'void init_swig() {'
144    for m in swig_modules:
145        print >>f, '    init_%s();' % m
146    print >>f, '}'
147    f.close()
148
149swig_cc_files = [ 'swig/%s_wrap.cc' % m for m in swig_modules ]
150env.Command('swig/init.cc', swig_cc_files, MakeSwigInit)
151
152# Action function to build the zip archive.  Uses the PyZipFile module
153# included in the standard Python library.
154def buildPyZip(target, source, env):
155    pzf = zipfile.PyZipFile(str(target[0]), 'w')
156    for s in source:
157        pzf.writepy(str(s))
158
159# Add the zip file target to the environment.
160env.Command('m5py.zip', pyzip_files, buildPyZip)
161env.Depends('m5py.zip', pyzip_dep_files)
162