SConscript revision 2686
110779SCurtis.Dunham@arm.com# -*- mode:python -*-
210779SCurtis.Dunham@arm.com
310779SCurtis.Dunham@arm.com# Copyright (c) 2004-2005 The Regents of The University of Michigan
410779SCurtis.Dunham@arm.com# All rights reserved.
510779SCurtis.Dunham@arm.com#
610779SCurtis.Dunham@arm.com# Redistribution and use in source and binary forms, with or without
710779SCurtis.Dunham@arm.com# modification, are permitted provided that the following conditions are
810779SCurtis.Dunham@arm.com# met: redistributions of source code must retain the above copyright
910779SCurtis.Dunham@arm.com# notice, this list of conditions and the following disclaimer;
1010779SCurtis.Dunham@arm.com# redistributions in binary form must reproduce the above copyright
1110779SCurtis.Dunham@arm.com# notice, this list of conditions and the following disclaimer in the
1210779SCurtis.Dunham@arm.com# documentation and/or other materials provided with the distribution;
1310779SCurtis.Dunham@arm.com# neither the name of the copyright holders nor the names of its
1410779SCurtis.Dunham@arm.com# contributors may be used to endorse or promote products derived from
1510779SCurtis.Dunham@arm.com# this software without specific prior written permission.
1610779SCurtis.Dunham@arm.com#
1710779SCurtis.Dunham@arm.com# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1810779SCurtis.Dunham@arm.com# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1910779SCurtis.Dunham@arm.com# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2010779SCurtis.Dunham@arm.com# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2110779SCurtis.Dunham@arm.com# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2210779SCurtis.Dunham@arm.com# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2310779SCurtis.Dunham@arm.com# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2410779SCurtis.Dunham@arm.com# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2510779SCurtis.Dunham@arm.com# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2610779SCurtis.Dunham@arm.com# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2710779SCurtis.Dunham@arm.com# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2810779SCurtis.Dunham@arm.com#
2910779SCurtis.Dunham@arm.com# Authors: Steve Reinhardt
3010779SCurtis.Dunham@arm.com#          Nathan Binkert
3110779SCurtis.Dunham@arm.com
3210779SCurtis.Dunham@arm.comimport os, os.path, re, sys
3310779SCurtis.Dunham@arm.comfrom zipfile import PyZipFile
3410779SCurtis.Dunham@arm.com
3510779SCurtis.Dunham@arm.com# handy function for path joins
3610779SCurtis.Dunham@arm.comdef join(*args):
3710779SCurtis.Dunham@arm.com    return os.path.normpath(os.path.join(*args))
3810779SCurtis.Dunham@arm.com
3910779SCurtis.Dunham@arm.comImport('env')
4010779SCurtis.Dunham@arm.com
4110779SCurtis.Dunham@arm.com# This SConscript is in charge of collecting .py files and generating a zip archive that is appended to the m5 binary.
4210779SCurtis.Dunham@arm.com
4310779SCurtis.Dunham@arm.com# Copy .py source files here (relative to src/python in the build
4410779SCurtis.Dunham@arm.com# directory).
4510779SCurtis.Dunham@arm.compyzip_root = 'zip'
4610779SCurtis.Dunham@arm.com
4711315SCurtis.Dunham@arm.com# List of files & directories to include in the zip file.  To include
4811315SCurtis.Dunham@arm.com# a package, list only the root directory of the package, not any
4911315SCurtis.Dunham@arm.com# internal .py files (else they will get the path stripped off when
5011315SCurtis.Dunham@arm.com# they are imported into the zip file).
5110779SCurtis.Dunham@arm.compyzip_files = []
5210779SCurtis.Dunham@arm.com
5311618SCurtis.Dunham@arm.com# List of additional files on which the zip archive depends, but which
5410779SCurtis.Dunham@arm.com# are not included in pyzip_files... i.e. individual .py files within
5510779SCurtis.Dunham@arm.com# a package.
5610779SCurtis.Dunham@arm.compyzip_dep_files = []
5710779SCurtis.Dunham@arm.com
5810779SCurtis.Dunham@arm.com# Add the specified package to the zip archive.  Adds the directory to
5910779SCurtis.Dunham@arm.com# pyzip_files and all included .py files to pyzip_dep_files.
6010779SCurtis.Dunham@arm.comdef addPkg(pkgdir):
6110779SCurtis.Dunham@arm.com    pyzip_files.append(join(pyzip_root, pkgdir))
6210779SCurtis.Dunham@arm.com    origdir = os.getcwd()
6310779SCurtis.Dunham@arm.com    srcdir = join(Dir('.').srcnode().abspath, pkgdir)
6410779SCurtis.Dunham@arm.com    os.chdir(srcdir)
6510779SCurtis.Dunham@arm.com    for path, dirs, files in os.walk('.'):
6610779SCurtis.Dunham@arm.com        for i,dir in enumerate(dirs):
6710779SCurtis.Dunham@arm.com            if dir == 'SCCS':
6810779SCurtis.Dunham@arm.com                del dirs[i]
6910779SCurtis.Dunham@arm.com                break
7010779SCurtis.Dunham@arm.com
7110779SCurtis.Dunham@arm.com        for f in files:
7210779SCurtis.Dunham@arm.com            if f.endswith('.py'):
7310779SCurtis.Dunham@arm.com                source = join(pkgdir, path, f)
7410779SCurtis.Dunham@arm.com                target = join(pyzip_root, source)
7510779SCurtis.Dunham@arm.com                pyzip_dep_files.append(target)
7610779SCurtis.Dunham@arm.com                env.CopyFile(target, source)
7710779SCurtis.Dunham@arm.com
7810779SCurtis.Dunham@arm.com    os.chdir(origdir)
7910779SCurtis.Dunham@arm.com
8010779SCurtis.Dunham@arm.com# Generate Python file that contains a dict specifying the current
8110779SCurtis.Dunham@arm.com# build_env flags.
8210779SCurtis.Dunham@arm.comdef MakeDefinesPyFile(target, source, env):
8310779SCurtis.Dunham@arm.com    f = file(str(target[0]), 'w')
8410779SCurtis.Dunham@arm.com    print >>f, "import __main__"
8510779SCurtis.Dunham@arm.com    print >>f, "__main__.m5_build_env = ",
8610779SCurtis.Dunham@arm.com    print >>f, source[0]
8710779SCurtis.Dunham@arm.com    f.close()
8810779SCurtis.Dunham@arm.com
8910779SCurtis.Dunham@arm.comoptionDict = dict([(opt, env[opt]) for opt in env.ExportOptions])
9010779SCurtis.Dunham@arm.comenv.Command('defines.py', Value(optionDict), MakeDefinesPyFile)
9110779SCurtis.Dunham@arm.com
9210779SCurtis.Dunham@arm.com# Now specify the packages & files for the zip archive.
9310779SCurtis.Dunham@arm.comaddPkg('m5')
9410779SCurtis.Dunham@arm.compyzip_files.append('defines.py')
9510779SCurtis.Dunham@arm.compyzip_files.append(join(env['ROOT'], 'util/pbs/jobfile.py'))
9610779SCurtis.Dunham@arm.com
9710779SCurtis.Dunham@arm.com# Action function to build the zip archive.  Uses the PyZipFile module
9810779SCurtis.Dunham@arm.com# included in the standard Python library.
9910779SCurtis.Dunham@arm.comdef buildPyZip(target, source, env):
10010779SCurtis.Dunham@arm.com    pzf = PyZipFile(str(target[0]), 'w')
10110779SCurtis.Dunham@arm.com    for s in source:
10210779SCurtis.Dunham@arm.com        pzf.writepy(str(s))
10310779SCurtis.Dunham@arm.com
10410779SCurtis.Dunham@arm.com# Add the zip file target to the environment.
10510779SCurtis.Dunham@arm.comenv.Command('m5py.zip', pyzip_files, buildPyZip)
10610779SCurtis.Dunham@arm.comenv.Depends('m5py.zip', pyzip_dep_files)
10710779SCurtis.Dunham@arm.com