SConscript revision 2665
11046SN/A# -*- mode:python -*-
21046SN/A
31762SN/A# Copyright (c) 2004-2005 The Regents of The University of Michigan
41046SN/A# All rights reserved.
51046SN/A#
61046SN/A# Redistribution and use in source and binary forms, with or without
71046SN/A# modification, are permitted provided that the following conditions are
81046SN/A# met: redistributions of source code must retain the above copyright
91046SN/A# notice, this list of conditions and the following disclaimer;
101046SN/A# redistributions in binary form must reproduce the above copyright
111046SN/A# notice, this list of conditions and the following disclaimer in the
121046SN/A# documentation and/or other materials provided with the distribution;
131046SN/A# neither the name of the copyright holders nor the names of its
141046SN/A# contributors may be used to endorse or promote products derived from
151046SN/A# this software without specific prior written permission.
161046SN/A#
171046SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
181046SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
191046SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
201046SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
211046SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
221046SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
231046SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
241046SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
251046SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
261046SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
271046SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
282665Ssaidi@eecs.umich.edu#
292665Ssaidi@eecs.umich.edu# Authors: Steve Reinhardt
302665Ssaidi@eecs.umich.edu#          Nathan Binkert
311046SN/A
324202Sbinkertn@umich.eduimport os, os.path, re, sys
331530SN/Afrom zipfile import PyZipFile
345522Snate@binkert.org
354382Sbinkertn@umich.edu# handy function for path joins
364382Sbinkertn@umich.edudef join(*args):
375471Snate@binkert.org    return os.path.normpath(os.path.join(*args))
385801Snate@binkert.org
395801Snate@binkert.orgImport('env')
404382Sbinkertn@umich.edu
414382Sbinkertn@umich.edu# This SConscript is in charge of collecting .py files and generating a zip archive that is appended to the m5 binary.
425470Snate@binkert.org
434382Sbinkertn@umich.edu# Copy .py source files here (relative to src/python in the build
444382Sbinkertn@umich.edu# directory).
454762Snate@binkert.orgpyzip_root = 'zip'
464382Sbinkertn@umich.edu
475799Snate@binkert.org# List of files & directories to include in the zip file.  To include
487674Snate@binkert.org# a package, list only the root directory of the package, not any
498295Snate@binkert.org# internal .py files (else they will get the path stripped off when
505467Snate@binkert.org# they are imported into the zip file).
515467Snate@binkert.orgpyzip_files = []
526502Snate@binkert.org
536654Snate@binkert.org# List of additional files on which the zip archive depends, but which
548999Suri.wiener@arm.com# are not included in pyzip_files... i.e. individual .py files within
556501Snate@binkert.org# a package.
565467Snate@binkert.orgpyzip_dep_files = []
575467Snate@binkert.org
586500Snate@binkert.org# Add the specified package to the zip archive.  Adds the directory to
596654Snate@binkert.org# pyzip_files and all included .py files to pyzip_dep_files.
607503Snate@binkert.orgdef addPkg(pkgdir):
617816Ssteve.reinhardt@amd.com    pyzip_files.append(join(pyzip_root, pkgdir))
6211988Sandreas.sandberg@arm.com    origdir = os.getcwd()
632667Sstever@eecs.umich.edu    srcdir = join(Dir('.').srcnode().abspath, pkgdir)
644382Sbinkertn@umich.edu    os.chdir(srcdir)
657677Snate@binkert.org    for path, dirs, files in os.walk('.'):
6611988Sandreas.sandberg@arm.com        for i,dir in enumerate(dirs):
6711988Sandreas.sandberg@arm.com            if dir == 'SCCS':
6811988Sandreas.sandberg@arm.com                del dirs[i]
6911988Sandreas.sandberg@arm.com                break
7011988Sandreas.sandberg@arm.com
7111988Sandreas.sandberg@arm.com        for f in files:
72            if f.endswith('.py'):
73                source = join(pkgdir, path, f)
74                target = join(pyzip_root, source)
75                pyzip_dep_files.append(target)
76                env.CopyFile(target, source)
77
78    os.chdir(origdir)
79
80# Generate Python file that contains a dict specifying the current
81# build_env flags.
82def MakeDefinesPyFile(target, source, env):
83    f = file(str(target[0]), 'w')
84    print >>f, "import __main__"
85    print >>f, "__main__.m5_build_env = ",
86    print >>f, source[0]
87    f.close()
88
89optionDict = dict([(opt, env[opt]) for opt in env.ExportOptions])
90env.Command('defines.py', Value(optionDict), MakeDefinesPyFile)
91
92# Now specify the packages & files for the zip archive.
93addPkg('m5')
94pyzip_files.append('defines.py')
95pyzip_files.append(join(env['ROOT'], 'util/pbs/jobfile.py'))
96
97# Action function to build the zip archive.  Uses the PyZipFile module
98# included in the standard Python library.
99def buildPyZip(target, source, env):
100    pzf = PyZipFile(str(target[0]), 'w')
101    for s in source:
102        pzf.writepy(str(s))
103
104# Add the zip file target to the environment.
105env.Command('m5py.zip', pyzip_files, buildPyZip)
106env.Depends('m5py.zip', pyzip_dep_files)
107