SConscript revision 4202
111986Sandreas.sandberg@arm.com# -*- mode:python -*-
211986Sandreas.sandberg@arm.com
311986Sandreas.sandberg@arm.com# Copyright (c) 2004-2005 The Regents of The University of Michigan
411986Sandreas.sandberg@arm.com# All rights reserved.
511986Sandreas.sandberg@arm.com#
611986Sandreas.sandberg@arm.com# Redistribution and use in source and binary forms, with or without
711986Sandreas.sandberg@arm.com# modification, are permitted provided that the following conditions are
811986Sandreas.sandberg@arm.com# met: redistributions of source code must retain the above copyright
911986Sandreas.sandberg@arm.com# notice, this list of conditions and the following disclaimer;
1011986Sandreas.sandberg@arm.com# redistributions in binary form must reproduce the above copyright
1111986Sandreas.sandberg@arm.com# notice, this list of conditions and the following disclaimer in the
1211986Sandreas.sandberg@arm.com# documentation and/or other materials provided with the distribution;
1311986Sandreas.sandberg@arm.com# neither the name of the copyright holders nor the names of its
1411986Sandreas.sandberg@arm.com# contributors may be used to endorse or promote products derived from
1511986Sandreas.sandberg@arm.com# this software without specific prior written permission.
1611986Sandreas.sandberg@arm.com#
1711986Sandreas.sandberg@arm.com# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1811986Sandreas.sandberg@arm.com# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1911986Sandreas.sandberg@arm.com# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2011986Sandreas.sandberg@arm.com# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2111986Sandreas.sandberg@arm.com# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2211986Sandreas.sandberg@arm.com# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2311986Sandreas.sandberg@arm.com# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2411986Sandreas.sandberg@arm.com# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2511986Sandreas.sandberg@arm.com# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2611986Sandreas.sandberg@arm.com# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2711986Sandreas.sandberg@arm.com# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2811986Sandreas.sandberg@arm.com#
2911986Sandreas.sandberg@arm.com# Authors: Steve Reinhardt
3011986Sandreas.sandberg@arm.com#          Nathan Binkert
3111986Sandreas.sandberg@arm.com
3211986Sandreas.sandberg@arm.comimport os
3311986Sandreas.sandberg@arm.comimport zipfile
3411986Sandreas.sandberg@arm.com
3511986Sandreas.sandberg@arm.com# handy function for path joins
3611986Sandreas.sandberg@arm.comdef join(*args):
3711986Sandreas.sandberg@arm.com    return os.path.normpath(os.path.join(*args))
3811986Sandreas.sandberg@arm.com
3911986Sandreas.sandberg@arm.comImport('*')
4011986Sandreas.sandberg@arm.com
4111986Sandreas.sandberg@arm.com# This SConscript is in charge of collecting .py files and generating
4211986Sandreas.sandberg@arm.com# a zip archive that is appended to the m5 binary.
4311986Sandreas.sandberg@arm.com
4412037Sandreas.sandberg@arm.com# List of files & directories to include in the zip file.  To include
4512037Sandreas.sandberg@arm.com# a package, list only the root directory of the package, not any
4612037Sandreas.sandberg@arm.com# internal .py files (else they will get the path stripped off when
4712037Sandreas.sandberg@arm.com# they are imported into the zip file).
4812037Sandreas.sandberg@arm.compyzip_files = []
4912037Sandreas.sandberg@arm.com
5012037Sandreas.sandberg@arm.com# List of additional files on which the zip archive depends, but which
5112037Sandreas.sandberg@arm.com# are not included in pyzip_files... i.e. individual .py files within
5212037Sandreas.sandberg@arm.com# a package.
5312037Sandreas.sandberg@arm.compyzip_dep_files = []
5412037Sandreas.sandberg@arm.com
5512037Sandreas.sandberg@arm.com# Add the specified package to the zip archive.  Adds the directory to
5612037Sandreas.sandberg@arm.com# pyzip_files and all included .py files to pyzip_dep_files.
5712037Sandreas.sandberg@arm.comdef addPkg(pkgdir):
5812037Sandreas.sandberg@arm.com    pyzip_files.append(pkgdir)
5912037Sandreas.sandberg@arm.com    origdir = os.getcwd()
6012037Sandreas.sandberg@arm.com    srcdir = join(Dir('.').srcnode().abspath, pkgdir)
6112037Sandreas.sandberg@arm.com    os.chdir(srcdir)
6212037Sandreas.sandberg@arm.com    for path, dirs, files in os.walk('.'):
6312037Sandreas.sandberg@arm.com        for i,dir in enumerate(dirs):
6411986Sandreas.sandberg@arm.com            if dir == 'SCCS':
6511986Sandreas.sandberg@arm.com                del dirs[i]
6611986Sandreas.sandberg@arm.com                break
6711986Sandreas.sandberg@arm.com
6811986Sandreas.sandberg@arm.com        for f in files:
6911986Sandreas.sandberg@arm.com            if f.endswith('.py'):
7011986Sandreas.sandberg@arm.com                pyzip_dep_files.append(join(pkgdir, path, f))
7111986Sandreas.sandberg@arm.com
7211986Sandreas.sandberg@arm.com    os.chdir(origdir)
7311986Sandreas.sandberg@arm.com
7411986Sandreas.sandberg@arm.com# Generate Python file that contains a dict specifying the current
7511986Sandreas.sandberg@arm.com# build_env flags.
7611986Sandreas.sandberg@arm.comdef MakeDefinesPyFile(target, source, env):
7711986Sandreas.sandberg@arm.com    f = file(str(target[0]), 'w')
7811986Sandreas.sandberg@arm.com    print >>f, "m5_build_env = ", source[0]
7911986Sandreas.sandberg@arm.com    f.close()
8011986Sandreas.sandberg@arm.com
8111986Sandreas.sandberg@arm.comoptionDict = dict([(opt, env[opt]) for opt in env.ExportOptions])
8211986Sandreas.sandberg@arm.comenv.Command('m5/defines.py', Value(optionDict), MakeDefinesPyFile)
8311986Sandreas.sandberg@arm.com
8411986Sandreas.sandberg@arm.comdef MakeInfoPyFile(target, source, env):
8511986Sandreas.sandberg@arm.com    f = file(str(target[0]), 'w')
8612037Sandreas.sandberg@arm.com    for src in source:
8712037Sandreas.sandberg@arm.com        data = ''.join(file(src.srcnode().abspath, 'r').xreadlines())
8812037Sandreas.sandberg@arm.com        print >>f, "%s = %s" % (src, repr(data))
8912037Sandreas.sandberg@arm.com    f.close()
9012037Sandreas.sandberg@arm.com
9112037Sandreas.sandberg@arm.comenv.Command('m5/info.py',
9212037Sandreas.sandberg@arm.com            [ '#/AUTHORS', '#/LICENSE', '#/README', '#/RELEASE_NOTES' ],
9312037Sandreas.sandberg@arm.com            MakeInfoPyFile)
9412037Sandreas.sandberg@arm.com
9512037Sandreas.sandberg@arm.com# Now specify the packages & files for the zip archive.
9612037Sandreas.sandberg@arm.comaddPkg('m5')
9712037Sandreas.sandberg@arm.compyzip_files.append('m5/defines.py')
9812037Sandreas.sandberg@arm.compyzip_files.append('m5/info.py')
9912037Sandreas.sandberg@arm.compyzip_files.append(join(env['ROOT'], 'util/pbs/jobfile.py'))
10012037Sandreas.sandberg@arm.compyzip_files.append(join(env['ROOT'], 'src/base/traceflags.py'))
10112037Sandreas.sandberg@arm.com
10212037Sandreas.sandberg@arm.comswig_modules = []
10312037Sandreas.sandberg@arm.comdef swig_it(module):
10411986Sandreas.sandberg@arm.com    env.Command(['swig/%s_wrap.cc' % module, 'm5/internal/%s.py' % module],
105                'swig/%s.i' % module,
106                '$SWIG $SWIGFLAGS -outdir ${TARGETS[1].dir} '
107                '-o ${TARGETS[0]} $SOURCES')
108    swig_modules.append(module)
109    Source('swig/%s_wrap.cc' % module)
110
111Source('swig/init.cc')
112Source('swig/pyevent.cc')
113Source('swig/pyobject.cc')
114
115swig_it('core')
116swig_it('debug')
117swig_it('event')
118swig_it('random')
119swig_it('sim_object')
120swig_it('stats')
121swig_it('trace')
122
123# Automatically generate m5/internals/__init__.py
124def MakeInternalsInit(target, source, env):
125    f = file(str(target[0]), 'w')
126    for m in swig_modules:
127        print >>f, 'import %s' % m
128    f.close()
129
130swig_py_files = [ 'm5/internal/%s.py' % m for m in swig_modules ]
131env.Command('m5/internal/__init__.py', swig_py_files, MakeInternalsInit)
132pyzip_dep_files.append('m5/internal/__init__.py')
133
134def MakeSwigInit(target, source, env):
135    f = file(str(target[0]), 'w')
136    print >>f, 'extern "C" {'
137    for m in swig_modules:
138        print >>f, '    void init_%s();' % m
139    print >>f, '}'
140    print >>f, 'void init_swig() {'
141    for m in swig_modules:
142        print >>f, '    init_%s();' % m
143    print >>f, '}'
144    f.close()
145
146swig_cc_files = [ 'swig/%s_wrap.cc' % m for m in swig_modules ]
147env.Command('swig/init.cc', swig_cc_files, MakeSwigInit)
148
149# Action function to build the zip archive.  Uses the PyZipFile module
150# included in the standard Python library.
151def buildPyZip(target, source, env):
152    pzf = zipfile.PyZipFile(str(target[0]), 'w')
153    for s in source:
154        pzf.writepy(str(s))
155
156# Add the zip file target to the environment.
157env.Command('m5py.zip', pyzip_files, buildPyZip)
158env.Depends('m5py.zip', pyzip_dep_files)
159