SConscript revision 4486
12139SN/A# -*- mode:python -*-
22139SN/A
313610Sgiacomo.gabrielli@arm.com# Copyright (c) 2004-2005 The Regents of The University of Michigan
412109SRekai.GonzalezAlberquilla@arm.com# All rights reserved.
512109SRekai.GonzalezAlberquilla@arm.com#
612109SRekai.GonzalezAlberquilla@arm.com# Redistribution and use in source and binary forms, with or without
712109SRekai.GonzalezAlberquilla@arm.com# modification, are permitted provided that the following conditions are
812109SRekai.GonzalezAlberquilla@arm.com# met: redistributions of source code must retain the above copyright
912109SRekai.GonzalezAlberquilla@arm.com# notice, this list of conditions and the following disclaimer;
1012109SRekai.GonzalezAlberquilla@arm.com# redistributions in binary form must reproduce the above copyright
1112109SRekai.GonzalezAlberquilla@arm.com# notice, this list of conditions and the following disclaimer in the
1212109SRekai.GonzalezAlberquilla@arm.com# documentation and/or other materials provided with the distribution;
1312109SRekai.GonzalezAlberquilla@arm.com# neither the name of the copyright holders nor the names of its
1412109SRekai.GonzalezAlberquilla@arm.com# contributors may be used to endorse or promote products derived from
152139SN/A# this software without specific prior written permission.
162139SN/A#
172139SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
182139SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
192139SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
202139SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
212139SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
222139SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
232139SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
242139SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
252139SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
262139SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
272139SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
282139SN/A#
292139SN/A# Authors: Steve Reinhardt
302139SN/A
312139SN/Aimport os
322139SN/Aimport sys
332139SN/Aimport zipfile
342139SN/A
352139SN/Afrom os.path import basename
362139SN/Afrom os.path import join as joinpath
372139SN/A
382139SN/Aimport SCons
392139SN/A
402665Ssaidi@eecs.umich.edu# This file defines how to build a particular configuration of M5
412665Ssaidi@eecs.umich.edu# based on variable settings in the 'env' build environment.
422139SN/A
434202Sbinkertn@umich.eduImport('*')
448961Sgblack@eecs.umich.edu
4510196SCurtis.Dunham@arm.com# Children need to see the environment
462139SN/AExport('env')
4712246Sgabeblack@google.com
4812246Sgabeblack@google.com########################################################################
494202Sbinkertn@umich.edu# Code for adding source files
502152SN/A#
512152SN/Asources = []
522139SN/Adef Source(source):
532139SN/A    if isinstance(source, SCons.Node.FS.File):
542139SN/A        sources.append(source)
552139SN/A    else:
562139SN/A        sources.append(File(source))
572152SN/A
582152SN/A# Children should have access
592139SN/AExport('Source')
6012015Sgabeblack@google.com
6112015Sgabeblack@google.com########################################################################
629020Sgblack@eecs.umich.edu# Code for adding python objects
634781Snate@binkert.org#
647799Sgblack@eecs.umich.edupy_sources = []
654781Snate@binkert.orgpy_source_packages = {}
664781Snate@binkert.orgdef PySource(package, source):
673170Sstever@eecs.umich.edu    if not isinstance(source, SCons.Node.FS.File):
685664Sgblack@eecs.umich.edu        source = File(source)
698105Sgblack@eecs.umich.edu    py_source_packages[source] = package
706179Sksewell@umich.edu    py_sources.append(source)
714781Snate@binkert.org
7210553Salexandru.dutu@amd.comsim_objects = []
736329Sgblack@eecs.umich.edudef SimObject(source):
744781Snate@binkert.org    if not isinstance(source, SCons.Node.FS.File):
754781Snate@binkert.org        source = File(source)
764781Snate@binkert.org    PySource('m5.objects', source)
774781Snate@binkert.org    modname = basename(str(source))
784781Snate@binkert.org    sim_objects.append(modname)
7912015Sgabeblack@google.com
8012015Sgabeblack@google.comswig_sources = []
812152SN/Aswig_source_packages = {}
8211308Santhony.gutierrez@amd.comdef SwigSource(package, source):
8312016Sgabeblack@google.com    if not isinstance(source, SCons.Node.FS.File):
8412016Sgabeblack@google.com        source = File(source)
8511308Santhony.gutierrez@amd.com    swig_source_packages[source] = package
8611696Santhony.gutierrez@amd.com    swig_sources.append(source)
8711308Santhony.gutierrez@amd.com
8812016Sgabeblack@google.com# Children should have access
8912016Sgabeblack@google.comExport('PySource')
9011308Santhony.gutierrez@amd.comExport('SimObject')
912152SN/AExport('SwigSource')
922152SN/A
932152SN/A########################################################################
942152SN/A#
952152SN/A# Set some compiler variables
962152SN/A#
972152SN/A
982152SN/A# Include file paths are rooted in this directory.  SCons will
992152SN/A# automatically expand '.' to refer to both the source directory and
1002152SN/A# the corresponding build directory to pick up generated include
10112222Sgabeblack@google.com# files.
1022152SN/Aenv.Append(CPPPATH=Dir('.'))
10312222Sgabeblack@google.com
10412222Sgabeblack@google.com# Add a flag defining what THE_ISA should be for all compilation
10512222Sgabeblack@google.comenv.Append(CPPDEFINES=[('THE_ISA','%s_ISA' % env['TARGET_ISA'].upper())])
10612222Sgabeblack@google.com
1072152SN/A########################################################################
10812222Sgabeblack@google.com# Walk the tree and execute all SConscripts
10912222Sgabeblack@google.com#
11012222Sgabeblack@google.comscripts = []
11112222Sgabeblack@google.comsrcdir = env['SRCDIR']
1122152SN/Afor root, dirs, files in os.walk(srcdir, topdown=True):
1132152SN/A    if root == srcdir:
1142152SN/A        # we don't want to recurse back into this SConscript
1152152SN/A        continue
1162152SN/A    
1172152SN/A    if 'SConscript' in files:
11812222Sgabeblack@google.com        # strip off the srcdir part since scons will try to find the
11912222Sgabeblack@google.com        # script in the build directory
1206993Snate@binkert.org        base = root[len(srcdir) + 1:]
1216998Snate@binkert.org        SConscript(joinpath(base, 'SConscript'))
1226998Snate@binkert.org
1236998Snate@binkert.orgfor opt in env.ExportOptions:
12412222Sgabeblack@google.com    env.ConfigFile(opt)
12512222Sgabeblack@google.com
12612222Sgabeblack@google.com########################################################################
1276993Snate@binkert.org#
1286993Snate@binkert.org# Deal with python/swig, object code.  Collect .py files and
12910319SAndreas.Sandberg@ARM.com# generating a zip archive that is appended to the m5 binary.
1306993Snate@binkert.org#
1312152SN/A
13212222Sgabeblack@google.com# Generate Python file that contains a dict specifying the current
1332152SN/A# build_env flags.
13412222Sgabeblack@google.comdef MakeDefinesPyFile(target, source, env):
1355944Sgblack@eecs.umich.edu    f = file(str(target[0]), 'w')
13610196SCurtis.Dunham@arm.com    print >>f, "m5_build_env = ", source[0]
13712222Sgabeblack@google.com    f.close()
13812222Sgabeblack@google.com
13912222Sgabeblack@google.comoptionDict = dict([(opt, env[opt]) for opt in env.ExportOptions])
14012222Sgabeblack@google.comenv.Command('python/m5/defines.py', Value(optionDict), MakeDefinesPyFile)
14110196SCurtis.Dunham@arm.comPySource('m5', 'python/m5/defines.py')
14212222Sgabeblack@google.com
14312222Sgabeblack@google.comdef MakeInfoPyFile(target, source, env):
14412222Sgabeblack@google.com    f = file(str(target[0]), 'w')
14512222Sgabeblack@google.com    for src in source:
14610196SCurtis.Dunham@arm.com        data = ''.join(file(src.srcnode().abspath, 'r').xreadlines())
14712222Sgabeblack@google.com        print >>f, "%s = %s" % (src, repr(data))
14812222Sgabeblack@google.com    f.close()
14912222Sgabeblack@google.com
15012222Sgabeblack@google.comenv.Command('python/m5/info.py',
15112222Sgabeblack@google.com            [ '#/AUTHORS', '#/LICENSE', '#/README', '#/RELEASE_NOTES' ],
15212222Sgabeblack@google.com            MakeInfoPyFile)
15310196SCurtis.Dunham@arm.comPySource('m5', 'python/m5/info.py')
15412222Sgabeblack@google.com
15512222Sgabeblack@google.comdef MakeObjectsInitFile(target, source, env):
15612222Sgabeblack@google.com    f = file(str(target[0]), 'w')
15712222Sgabeblack@google.com    print >>f, 'from m5.SimObject import *'
15812222Sgabeblack@google.com    for src_path in source:
15912222Sgabeblack@google.com        src_file = basename(src_path.get_contents())
16012222Sgabeblack@google.com        assert(src_file.endswith('.py'))
16110196SCurtis.Dunham@arm.com        src_module = src_file[:-3]
16212222Sgabeblack@google.com        print >>f, 'from %s import *' % src_module
16312222Sgabeblack@google.com    f.close()
16412222Sgabeblack@google.com
16512222Sgabeblack@google.comenv.Command('python/m5/objects/__init__.py',
16612222Sgabeblack@google.com            [ Value(o) for o in sim_objects],
16712222Sgabeblack@google.com            MakeObjectsInitFile)
16810196SCurtis.Dunham@arm.comPySource('m5.objects', 'python/m5/objects/__init__.py')
16912222Sgabeblack@google.com
17012222Sgabeblack@google.comswig_modules = []
17112222Sgabeblack@google.comfor source in swig_sources:
17212222Sgabeblack@google.com    source.rfile() # Hack to cause the symlink to the .i file to be created
17312222Sgabeblack@google.com    package = swig_source_packages[source]
17412222Sgabeblack@google.com    filename = str(source)
17512222Sgabeblack@google.com    module = basename(filename)
17612222Sgabeblack@google.com
17712222Sgabeblack@google.com    assert(module.endswith('.i'))
17812222Sgabeblack@google.com    module = module[:-2]
17912222Sgabeblack@google.com    cc_file = 'swig/%s_wrap.cc' % module
18010196SCurtis.Dunham@arm.com    py_file = 'm5/internal/%s.py' % module
18112222Sgabeblack@google.com
18212222Sgabeblack@google.com    env.Command([cc_file, py_file], source,
18312222Sgabeblack@google.com                '$SWIG $SWIGFLAGS -outdir ${TARGETS[1].dir} '
18412222Sgabeblack@google.com                '-o ${TARGETS[0]} $SOURCES')
18512222Sgabeblack@google.com    env.Depends(py_file, source)
18612222Sgabeblack@google.com    env.Depends(cc_file, source)
18712222Sgabeblack@google.com                
18812222Sgabeblack@google.com    swig_modules.append(Value(module))
18912222Sgabeblack@google.com    Source(cc_file)
19012222Sgabeblack@google.com    PySource(package, py_file)
19112222Sgabeblack@google.com
19212222Sgabeblack@google.comdef MakeSwigInit(target, source, env):
19312222Sgabeblack@google.com    f = file(str(target[0]), 'w')
19412222Sgabeblack@google.com    print >>f, 'extern "C" {'
19512222Sgabeblack@google.com    for module in source:
19612222Sgabeblack@google.com        print >>f, '    void init_%s();' % module.get_contents()
19712222Sgabeblack@google.com    print >>f, '}'
19812222Sgabeblack@google.com    print >>f, 'void init_swig() {'
19912222Sgabeblack@google.com    for module in source:
20012222Sgabeblack@google.com        print >>f, '    init_%s();' % module.get_contents()
20112222Sgabeblack@google.com    print >>f, '}'
20212222Sgabeblack@google.com    f.close()
20312222Sgabeblack@google.comenv.Command('python/swig/init.cc', swig_modules, MakeSwigInit)
20412222Sgabeblack@google.com
20512222Sgabeblack@google.comdef CompilePyFile(target, source, env):
20612222Sgabeblack@google.com    import py_compile
20712222Sgabeblack@google.com    py_compile.compile(str(source[0]), str(target[0]))
20812222Sgabeblack@google.com
20912222Sgabeblack@google.compy_compiled = []
21012222Sgabeblack@google.compy_arcname = {}
21112222Sgabeblack@google.compy_zip_depends = []
21212222Sgabeblack@google.comfor source in py_sources:
21312222Sgabeblack@google.com    filename = str(source)
21412222Sgabeblack@google.com    package = py_source_packages[source]
21512222Sgabeblack@google.com    arc_path = package.split('.') + [ basename(filename) + 'c' ]
21612222Sgabeblack@google.com    zip_path = [ 'zip' ] + arc_path
21712222Sgabeblack@google.com    arcname = joinpath(*arc_path)
21812222Sgabeblack@google.com    zipname = joinpath(*zip_path)
21912222Sgabeblack@google.com    f = File(zipname)
22012222Sgabeblack@google.com
22112222Sgabeblack@google.com    env.Command(f, source, CompilePyFile)
22212222Sgabeblack@google.com    py_compiled.append(f)
22312222Sgabeblack@google.com    py_arcname[f] = arcname
22412222Sgabeblack@google.com
22510196SCurtis.Dunham@arm.com    # make the zipfile depend on the archive name so that the archive
2268335Snate@binkert.org    # is rebuilt if the name changes
2278335Snate@binkert.org    py_zip_depends.append(Value(arcname))
22812109SRekai.GonzalezAlberquilla@arm.com
22913610Sgiacomo.gabrielli@arm.com# Action function to build the zip archive.  Uses the PyZipFile module
2309920Syasuko.eckert@amd.com# included in the standard Python library.
2318335Snate@binkert.orgdef buildPyZip(target, source, env):
23213610Sgiacomo.gabrielli@arm.com    zf = zipfile.ZipFile(str(target[0]), 'w')
23313610Sgiacomo.gabrielli@arm.com    for s in source:
234        arcname = py_arcname[s]
235        zipname = str(s)
236        zf.write(zipname, arcname)
237    zf.close()
238
239# Add the zip file target to the environment.
240env.Command('m5py.zip', py_compiled, buildPyZip)
241env.Depends('m5py.zip', py_zip_depends)
242
243########################################################################
244#
245# Define binaries.  Each different build type (debug, opt, etc.) gets
246# a slightly different build environment.
247#
248
249# List of constructed environments to pass back to SConstruct
250envList = []
251
252# This function adds the specified sources to the given build
253# environment, and returns a list of all the corresponding SCons
254# Object nodes (including an extra one for date.cc).  We explicitly
255# add the Object nodes so we can set up special dependencies for
256# date.cc.
257def make_objs(sources, env):
258    objs = [env.Object(s) for s in sources]
259    # make date.cc depend on all other objects so it always gets
260    # recompiled whenever anything else does
261    date_obj = env.Object('base/date.cc')
262    env.Depends(date_obj, objs)
263    objs.append(date_obj)
264    return objs
265
266# Function to create a new build environment as clone of current
267# environment 'env' with modified object suffix and optional stripped
268# binary.  Additional keyword arguments are appended to corresponding
269# build environment vars.
270def makeEnv(label, objsfx, strip = False, **kwargs):
271    newEnv = env.Copy(OBJSUFFIX=objsfx)
272    newEnv.Label = label
273    newEnv.Append(**kwargs)
274    exe = 'm5.' + label  # final executable
275    bin = exe + '.bin'   # executable w/o appended Python zip archive
276    newEnv.Program(bin, make_objs(sources, newEnv))
277    if strip:
278        stripped_bin = bin + '.stripped'
279        if sys.platform == 'sunos5':
280            newEnv.Command(stripped_bin, bin, 'cp $SOURCE $TARGET; strip $TARGET')
281        else:
282            newEnv.Command(stripped_bin, bin, 'strip $SOURCE -o $TARGET')
283        bin = stripped_bin
284    targets = newEnv.Concat(exe, [bin, 'm5py.zip'])
285    newEnv.M5Binary = targets[0]
286    envList.append(newEnv)
287
288# Debug binary
289ccflags = {}
290if env['GCC']:
291    if sys.platform == 'sunos5':
292        ccflags['debug'] = '-gstabs+'
293    else:
294        ccflags['debug'] = '-ggdb3'
295    ccflags['opt'] = '-g -O3'
296    ccflags['fast'] = '-O3'
297    ccflags['prof'] = '-O3 -g -pg'
298elif env['SUNCC']:
299    ccflags['debug'] = '-g0'
300    ccflags['opt'] = '-g -O'
301    ccflags['fast'] = '-fast'
302    ccflags['prof'] = '-fast -g -pg'
303elif env['ICC']:
304    ccflags['debug'] = '-g -O0'
305    ccflags['opt'] = '-g -O'
306    ccflags['fast'] = '-fast'
307    ccflags['prof'] = '-fast -g -pg'
308else:
309    print 'Unknown compiler, please fix compiler options'
310    Exit(1)    
311
312makeEnv('debug', '.do',
313        CCFLAGS = Split(ccflags['debug']),
314        CPPDEFINES = ['DEBUG', 'TRACING_ON=1'])
315
316# Optimized binary
317makeEnv('opt', '.o',
318        CCFLAGS = Split(ccflags['opt']),
319        CPPDEFINES = ['TRACING_ON=1'])
320
321# "Fast" binary
322makeEnv('fast', '.fo', strip = True,
323        CCFLAGS = Split(ccflags['fast']),
324        CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'])
325
326# Profiled binary
327makeEnv('prof', '.po',
328        CCFLAGS = Split(ccflags['prof']),
329        CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
330        LINKFLAGS = '-pg')
331
332Return('envList')
333