SConscript revision 2655:da93a2088efa
13534Sgblack@eecs.umich.edu# -*- mode:python -*-
23534Sgblack@eecs.umich.edu
33534Sgblack@eecs.umich.edu# Copyright (c) 2004-2005 The Regents of The University of Michigan
43534Sgblack@eecs.umich.edu# All rights reserved.
53534Sgblack@eecs.umich.edu#
63534Sgblack@eecs.umich.edu# Redistribution and use in source and binary forms, with or without
73534Sgblack@eecs.umich.edu# modification, are permitted provided that the following conditions are
83534Sgblack@eecs.umich.edu# met: redistributions of source code must retain the above copyright
93534Sgblack@eecs.umich.edu# notice, this list of conditions and the following disclaimer;
103534Sgblack@eecs.umich.edu# redistributions in binary form must reproduce the above copyright
113534Sgblack@eecs.umich.edu# notice, this list of conditions and the following disclaimer in the
123534Sgblack@eecs.umich.edu# documentation and/or other materials provided with the distribution;
133534Sgblack@eecs.umich.edu# neither the name of the copyright holders nor the names of its
143534Sgblack@eecs.umich.edu# contributors may be used to endorse or promote products derived from
153534Sgblack@eecs.umich.edu# this software without specific prior written permission.
163534Sgblack@eecs.umich.edu#
173534Sgblack@eecs.umich.edu# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
183534Sgblack@eecs.umich.edu# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
193534Sgblack@eecs.umich.edu# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
203534Sgblack@eecs.umich.edu# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
213534Sgblack@eecs.umich.edu# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
223534Sgblack@eecs.umich.edu# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
233534Sgblack@eecs.umich.edu# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
243534Sgblack@eecs.umich.edu# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
253534Sgblack@eecs.umich.edu# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
263534Sgblack@eecs.umich.edu# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
273534Sgblack@eecs.umich.edu# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
283534Sgblack@eecs.umich.edu
293534Sgblack@eecs.umich.eduimport os, os.path, re, sys
303534Sgblack@eecs.umich.edufrom zipfile import PyZipFile
313534Sgblack@eecs.umich.edu
324202Sbinkertn@umich.edu# handy function for path joins
333534Sgblack@eecs.umich.edudef join(*args):
348739Sgblack@eecs.umich.edu    return os.path.normpath(os.path.join(*args))
355480Snate@binkert.org
364486Sbinkertn@umich.eduImport('env')
374486Sbinkertn@umich.edu
385480Snate@binkert.org# This SConscript is in charge of collecting .py files and generating a zip archive that is appended to the m5 binary.
394202Sbinkertn@umich.edu
404202Sbinkertn@umich.edu# Copy .py source files here (relative to src/python in the build
414202Sbinkertn@umich.edu# directory).
424202Sbinkertn@umich.edupyzip_root = 'zip'
435192Ssaidi@eecs.umich.edu
448335Snate@binkert.org# List of files & directories to include in the zip file.  To include
458335Snate@binkert.org# 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(join(pyzip_root, 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                source = join(pkgdir, path, f)
71                target = join(pyzip_root, source)
72                pyzip_dep_files.append(target)
73                env.CopyFile(target, source)
74
75    os.chdir(origdir)
76
77# Generate Python file that contains a dict specifying the current
78# build_env flags.
79def MakeDefinesPyFile(target, source, env):
80    f = file(str(target[0]), 'w')
81    print >>f, "import __main__"
82    print >>f, "__main__.m5_build_env = ",
83    print >>f, source[0]
84    f.close()
85
86optionDict = dict([(opt, env[opt]) for opt in env.ExportOptions])
87env.Command('defines.py', Value(optionDict), MakeDefinesPyFile)
88
89# Now specify the packages & files for the zip archive.
90addPkg('m5')
91pyzip_files.append('defines.py')
92pyzip_files.append(join(env['ROOT'], 'util/pbs/jobfile.py'))
93
94# Action function to build the zip archive.  Uses the PyZipFile module
95# included in the standard Python library.
96def buildPyZip(target, source, env):
97    pzf = PyZipFile(str(target[0]), 'w')
98    for s in source:
99        pzf.writepy(str(s))
100
101# Add the zip file target to the environment.
102env.Command('m5py.zip', pyzip_files, buildPyZip)
103env.Depends('m5py.zip', pyzip_dep_files)
104