SConscript revision 4486
1955SN/A# -*- mode:python -*-
2955SN/A
31762SN/A# Copyright (c) 2004-2005 The Regents of The University of Michigan
4955SN/A# All rights reserved.
5955SN/A#
6955SN/A# Redistribution and use in source and binary forms, with or without
7955SN/A# modification, are permitted provided that the following conditions are
8955SN/A# met: redistributions of source code must retain the above copyright
9955SN/A# notice, this list of conditions and the following disclaimer;
10955SN/A# redistributions in binary form must reproduce the above copyright
11955SN/A# notice, this list of conditions and the following disclaimer in the
12955SN/A# documentation and/or other materials provided with the distribution;
13955SN/A# neither the name of the copyright holders nor the names of its
14955SN/A# contributors may be used to endorse or promote products derived from
15955SN/A# this software without specific prior written permission.
16955SN/A#
17955SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18955SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19955SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20955SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21955SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22955SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23955SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24955SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25955SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26955SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27955SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
282665Ssaidi@eecs.umich.edu#
294762Snate@binkert.org# Authors: Steve Reinhardt
30955SN/A
315522Snate@binkert.orgimport os
326143Snate@binkert.orgimport sys
334762Snate@binkert.orgimport zipfile
345522Snate@binkert.org
35955SN/Afrom os.path import basename
365522Snate@binkert.orgfrom os.path import join as joinpath
37955SN/A
385522Snate@binkert.orgimport SCons
394202Sbinkertn@umich.edu
405742Snate@binkert.org# This file defines how to build a particular configuration of M5
41955SN/A# based on variable settings in the 'env' build environment.
424381Sbinkertn@umich.edu
434381Sbinkertn@umich.eduImport('*')
448334Snate@binkert.org
45955SN/A# Children need to see the environment
46955SN/AExport('env')
474202Sbinkertn@umich.edu
48955SN/A########################################################################
494382Sbinkertn@umich.edu# Code for adding source files
504382Sbinkertn@umich.edu#
514382Sbinkertn@umich.edusources = []
526654Snate@binkert.orgdef Source(source):
535517Snate@binkert.org    if isinstance(source, SCons.Node.FS.File):
548614Sgblack@eecs.umich.edu        sources.append(source)
557674Snate@binkert.org    else:
566143Snate@binkert.org        sources.append(File(source))
576143Snate@binkert.org
586143Snate@binkert.org# Children should have access
598233Snate@binkert.orgExport('Source')
608233Snate@binkert.org
618233Snate@binkert.org########################################################################
628233Snate@binkert.org# Code for adding python objects
638233Snate@binkert.org#
648334Snate@binkert.orgpy_sources = []
658334Snate@binkert.orgpy_source_packages = {}
668233Snate@binkert.orgdef PySource(package, source):
678233Snate@binkert.org    if not isinstance(source, SCons.Node.FS.File):
688233Snate@binkert.org        source = File(source)
698233Snate@binkert.org    py_source_packages[source] = package
708233Snate@binkert.org    py_sources.append(source)
718233Snate@binkert.org
726143Snate@binkert.orgsim_objects = []
738233Snate@binkert.orgdef SimObject(source):
748233Snate@binkert.org    if not isinstance(source, SCons.Node.FS.File):
758233Snate@binkert.org        source = File(source)
766143Snate@binkert.org    PySource('m5.objects', source)
776143Snate@binkert.org    modname = basename(str(source))
786143Snate@binkert.org    sim_objects.append(modname)
796143Snate@binkert.org
808233Snate@binkert.orgswig_sources = []
818233Snate@binkert.orgswig_source_packages = {}
828233Snate@binkert.orgdef SwigSource(package, source):
836143Snate@binkert.org    if not isinstance(source, SCons.Node.FS.File):
848233Snate@binkert.org        source = File(source)
858233Snate@binkert.org    swig_source_packages[source] = package
868233Snate@binkert.org    swig_sources.append(source)
878233Snate@binkert.org
886143Snate@binkert.org# Children should have access
896143Snate@binkert.orgExport('PySource')
906143Snate@binkert.orgExport('SimObject')
914762Snate@binkert.orgExport('SwigSource')
926143Snate@binkert.org
938233Snate@binkert.org########################################################################
948233Snate@binkert.org#
958233Snate@binkert.org# Set some compiler variables
968233Snate@binkert.org#
978233Snate@binkert.org
986143Snate@binkert.org# Include file paths are rooted in this directory.  SCons will
998233Snate@binkert.org# automatically expand '.' to refer to both the source directory and
1008233Snate@binkert.org# the corresponding build directory to pick up generated include
1018233Snate@binkert.org# files.
1028233Snate@binkert.orgenv.Append(CPPPATH=Dir('.'))
1036143Snate@binkert.org
1046143Snate@binkert.org# Add a flag defining what THE_ISA should be for all compilation
1056143Snate@binkert.orgenv.Append(CPPDEFINES=[('THE_ISA','%s_ISA' % env['TARGET_ISA'].upper())])
1066143Snate@binkert.org
1076143Snate@binkert.org########################################################################
1086143Snate@binkert.org# Walk the tree and execute all SConscripts
1096143Snate@binkert.org#
1106143Snate@binkert.orgscripts = []
1116143Snate@binkert.orgsrcdir = env['SRCDIR']
1127065Snate@binkert.orgfor root, dirs, files in os.walk(srcdir, topdown=True):
1136143Snate@binkert.org    if root == srcdir:
1148233Snate@binkert.org        # we don't want to recurse back into this SConscript
1158233Snate@binkert.org        continue
1168233Snate@binkert.org    
1178233Snate@binkert.org    if 'SConscript' in files:
1188233Snate@binkert.org        # strip off the srcdir part since scons will try to find the
1198233Snate@binkert.org        # script in the build directory
1208233Snate@binkert.org        base = root[len(srcdir) + 1:]
1218233Snate@binkert.org        SConscript(joinpath(base, 'SConscript'))
1228233Snate@binkert.org
1238233Snate@binkert.orgfor opt in env.ExportOptions:
1248233Snate@binkert.org    env.ConfigFile(opt)
1258233Snate@binkert.org
1268233Snate@binkert.org########################################################################
1278233Snate@binkert.org#
1288233Snate@binkert.org# Deal with python/swig, object code.  Collect .py files and
1298233Snate@binkert.org# generating a zip archive that is appended to the m5 binary.
1308233Snate@binkert.org#
1318233Snate@binkert.org
1328233Snate@binkert.org# Generate Python file that contains a dict specifying the current
1338233Snate@binkert.org# build_env flags.
1348233Snate@binkert.orgdef MakeDefinesPyFile(target, source, env):
1358233Snate@binkert.org    f = file(str(target[0]), 'w')
1368233Snate@binkert.org    print >>f, "m5_build_env = ", source[0]
1378233Snate@binkert.org    f.close()
1388233Snate@binkert.org
1398233Snate@binkert.orgoptionDict = dict([(opt, env[opt]) for opt in env.ExportOptions])
1408233Snate@binkert.orgenv.Command('python/m5/defines.py', Value(optionDict), MakeDefinesPyFile)
1418233Snate@binkert.orgPySource('m5', 'python/m5/defines.py')
1428233Snate@binkert.org
1438233Snate@binkert.orgdef MakeInfoPyFile(target, source, env):
1448233Snate@binkert.org    f = file(str(target[0]), 'w')
1456143Snate@binkert.org    for src in source:
1466143Snate@binkert.org        data = ''.join(file(src.srcnode().abspath, 'r').xreadlines())
1476143Snate@binkert.org        print >>f, "%s = %s" % (src, repr(data))
1486143Snate@binkert.org    f.close()
1496143Snate@binkert.org
1506143Snate@binkert.orgenv.Command('python/m5/info.py',
1516143Snate@binkert.org            [ '#/AUTHORS', '#/LICENSE', '#/README', '#/RELEASE_NOTES' ],
1526143Snate@binkert.org            MakeInfoPyFile)
1536143Snate@binkert.orgPySource('m5', 'python/m5/info.py')
1548945Ssteve.reinhardt@amd.com
1558233Snate@binkert.orgdef MakeObjectsInitFile(target, source, env):
1568233Snate@binkert.org    f = file(str(target[0]), 'w')
1576143Snate@binkert.org    print >>f, 'from m5.SimObject import *'
1588945Ssteve.reinhardt@amd.com    for src_path in source:
1596143Snate@binkert.org        src_file = basename(src_path.get_contents())
1606143Snate@binkert.org        assert(src_file.endswith('.py'))
1616143Snate@binkert.org        src_module = src_file[:-3]
1626143Snate@binkert.org        print >>f, 'from %s import *' % src_module
1635522Snate@binkert.org    f.close()
1646143Snate@binkert.org
1656143Snate@binkert.orgenv.Command('python/m5/objects/__init__.py',
1666143Snate@binkert.org            [ Value(o) for o in sim_objects],
1676143Snate@binkert.org            MakeObjectsInitFile)
1688233Snate@binkert.orgPySource('m5.objects', 'python/m5/objects/__init__.py')
1698233Snate@binkert.org
1708233Snate@binkert.orgswig_modules = []
1716143Snate@binkert.orgfor source in swig_sources:
1726143Snate@binkert.org    source.rfile() # Hack to cause the symlink to the .i file to be created
1736143Snate@binkert.org    package = swig_source_packages[source]
1746143Snate@binkert.org    filename = str(source)
1755522Snate@binkert.org    module = basename(filename)
1765522Snate@binkert.org
1775522Snate@binkert.org    assert(module.endswith('.i'))
1785522Snate@binkert.org    module = module[:-2]
1795604Snate@binkert.org    cc_file = 'swig/%s_wrap.cc' % module
1805604Snate@binkert.org    py_file = 'm5/internal/%s.py' % module
1816143Snate@binkert.org
1826143Snate@binkert.org    env.Command([cc_file, py_file], source,
1834762Snate@binkert.org                '$SWIG $SWIGFLAGS -outdir ${TARGETS[1].dir} '
1844762Snate@binkert.org                '-o ${TARGETS[0]} $SOURCES')
1856143Snate@binkert.org    env.Depends(py_file, source)
1866727Ssteve.reinhardt@amd.com    env.Depends(cc_file, source)
1876727Ssteve.reinhardt@amd.com                
1886727Ssteve.reinhardt@amd.com    swig_modules.append(Value(module))
1894762Snate@binkert.org    Source(cc_file)
1906143Snate@binkert.org    PySource(package, py_file)
1916143Snate@binkert.org
1926143Snate@binkert.orgdef MakeSwigInit(target, source, env):
1936143Snate@binkert.org    f = file(str(target[0]), 'w')
1946727Ssteve.reinhardt@amd.com    print >>f, 'extern "C" {'
1956143Snate@binkert.org    for module in source:
1967674Snate@binkert.org        print >>f, '    void init_%s();' % module.get_contents()
1977674Snate@binkert.org    print >>f, '}'
1985604Snate@binkert.org    print >>f, 'void init_swig() {'
1996143Snate@binkert.org    for module in source:
2006143Snate@binkert.org        print >>f, '    init_%s();' % module.get_contents()
2016143Snate@binkert.org    print >>f, '}'
2024762Snate@binkert.org    f.close()
2036143Snate@binkert.orgenv.Command('python/swig/init.cc', swig_modules, MakeSwigInit)
2044762Snate@binkert.org
2054762Snate@binkert.orgdef CompilePyFile(target, source, env):
2064762Snate@binkert.org    import py_compile
2076143Snate@binkert.org    py_compile.compile(str(source[0]), str(target[0]))
2086143Snate@binkert.org
2094762Snate@binkert.orgpy_compiled = []
2108233Snate@binkert.orgpy_arcname = {}
2118233Snate@binkert.orgpy_zip_depends = []
2128233Snate@binkert.orgfor source in py_sources:
2138233Snate@binkert.org    filename = str(source)
2146143Snate@binkert.org    package = py_source_packages[source]
2156143Snate@binkert.org    arc_path = package.split('.') + [ basename(filename) + 'c' ]
2164762Snate@binkert.org    zip_path = [ 'zip' ] + arc_path
2176143Snate@binkert.org    arcname = joinpath(*arc_path)
2184762Snate@binkert.org    zipname = joinpath(*zip_path)
2196143Snate@binkert.org    f = File(zipname)
2204762Snate@binkert.org
2216143Snate@binkert.org    env.Command(f, source, CompilePyFile)
2228233Snate@binkert.org    py_compiled.append(f)
2238233Snate@binkert.org    py_arcname[f] = arcname
2248233Snate@binkert.org
2256143Snate@binkert.org    # make the zipfile depend on the archive name so that the archive
2266143Snate@binkert.org    # is rebuilt if the name changes
2276143Snate@binkert.org    py_zip_depends.append(Value(arcname))
2286143Snate@binkert.org
2296143Snate@binkert.org# Action function to build the zip archive.  Uses the PyZipFile module
2306143Snate@binkert.org# included in the standard Python library.
2316143Snate@binkert.orgdef buildPyZip(target, source, env):
2326143Snate@binkert.org    zf = zipfile.ZipFile(str(target[0]), 'w')
2338233Snate@binkert.org    for s in source:
2348233Snate@binkert.org        arcname = py_arcname[s]
235955SN/A        zipname = str(s)
2369396Sandreas.hansson@arm.com        zf.write(zipname, arcname)
2379396Sandreas.hansson@arm.com    zf.close()
2389396Sandreas.hansson@arm.com
2399396Sandreas.hansson@arm.com# Add the zip file target to the environment.
2409396Sandreas.hansson@arm.comenv.Command('m5py.zip', py_compiled, buildPyZip)
2419396Sandreas.hansson@arm.comenv.Depends('m5py.zip', py_zip_depends)
2429396Sandreas.hansson@arm.com
2439396Sandreas.hansson@arm.com########################################################################
2449396Sandreas.hansson@arm.com#
2459396Sandreas.hansson@arm.com# Define binaries.  Each different build type (debug, opt, etc.) gets
2469396Sandreas.hansson@arm.com# a slightly different build environment.
2479396Sandreas.hansson@arm.com#
2489396Sandreas.hansson@arm.com
2499396Sandreas.hansson@arm.com# List of constructed environments to pass back to SConstruct
2509396Sandreas.hansson@arm.comenvList = []
2519396Sandreas.hansson@arm.com
2528235Snate@binkert.org# This function adds the specified sources to the given build
2538235Snate@binkert.org# environment, and returns a list of all the corresponding SCons
2546143Snate@binkert.org# Object nodes (including an extra one for date.cc).  We explicitly
2558235Snate@binkert.org# add the Object nodes so we can set up special dependencies for
2569003SAli.Saidi@ARM.com# date.cc.
2578235Snate@binkert.orgdef make_objs(sources, env):
2588235Snate@binkert.org    objs = [env.Object(s) for s in sources]
2598235Snate@binkert.org    # make date.cc depend on all other objects so it always gets
2608235Snate@binkert.org    # recompiled whenever anything else does
2618235Snate@binkert.org    date_obj = env.Object('base/date.cc')
2628235Snate@binkert.org    env.Depends(date_obj, objs)
2638235Snate@binkert.org    objs.append(date_obj)
2648235Snate@binkert.org    return objs
2658235Snate@binkert.org
2668235Snate@binkert.org# Function to create a new build environment as clone of current
2678235Snate@binkert.org# environment 'env' with modified object suffix and optional stripped
2688235Snate@binkert.org# binary.  Additional keyword arguments are appended to corresponding
2698235Snate@binkert.org# build environment vars.
2708235Snate@binkert.orgdef makeEnv(label, objsfx, strip = False, **kwargs):
2719003SAli.Saidi@ARM.com    newEnv = env.Copy(OBJSUFFIX=objsfx)
2728235Snate@binkert.org    newEnv.Label = label
2735584Snate@binkert.org    newEnv.Append(**kwargs)
2744382Sbinkertn@umich.edu    exe = 'm5.' + label  # final executable
2754202Sbinkertn@umich.edu    bin = exe + '.bin'   # executable w/o appended Python zip archive
2764382Sbinkertn@umich.edu    newEnv.Program(bin, make_objs(sources, newEnv))
2774382Sbinkertn@umich.edu    if strip:
2784382Sbinkertn@umich.edu        stripped_bin = bin + '.stripped'
2799396Sandreas.hansson@arm.com        if sys.platform == 'sunos5':
2805584Snate@binkert.org            newEnv.Command(stripped_bin, bin, 'cp $SOURCE $TARGET; strip $TARGET')
2814382Sbinkertn@umich.edu        else:
2824382Sbinkertn@umich.edu            newEnv.Command(stripped_bin, bin, 'strip $SOURCE -o $TARGET')
2834382Sbinkertn@umich.edu        bin = stripped_bin
2848232Snate@binkert.org    targets = newEnv.Concat(exe, [bin, 'm5py.zip'])
2855192Ssaidi@eecs.umich.edu    newEnv.M5Binary = targets[0]
2868232Snate@binkert.org    envList.append(newEnv)
2878232Snate@binkert.org
2888232Snate@binkert.org# Debug binary
2895192Ssaidi@eecs.umich.educcflags = {}
2908232Snate@binkert.orgif env['GCC']:
2915192Ssaidi@eecs.umich.edu    if sys.platform == 'sunos5':
2925799Snate@binkert.org        ccflags['debug'] = '-gstabs+'
2938232Snate@binkert.org    else:
2945192Ssaidi@eecs.umich.edu        ccflags['debug'] = '-ggdb3'
2955192Ssaidi@eecs.umich.edu    ccflags['opt'] = '-g -O3'
2965192Ssaidi@eecs.umich.edu    ccflags['fast'] = '-O3'
2978232Snate@binkert.org    ccflags['prof'] = '-O3 -g -pg'
2985192Ssaidi@eecs.umich.eduelif env['SUNCC']:
2998232Snate@binkert.org    ccflags['debug'] = '-g0'
3005192Ssaidi@eecs.umich.edu    ccflags['opt'] = '-g -O'
3015192Ssaidi@eecs.umich.edu    ccflags['fast'] = '-fast'
3025192Ssaidi@eecs.umich.edu    ccflags['prof'] = '-fast -g -pg'
3035192Ssaidi@eecs.umich.eduelif env['ICC']:
3044382Sbinkertn@umich.edu    ccflags['debug'] = '-g -O0'
3054382Sbinkertn@umich.edu    ccflags['opt'] = '-g -O'
3064382Sbinkertn@umich.edu    ccflags['fast'] = '-fast'
3072667Sstever@eecs.umich.edu    ccflags['prof'] = '-fast -g -pg'
3082667Sstever@eecs.umich.eduelse:
3092667Sstever@eecs.umich.edu    print 'Unknown compiler, please fix compiler options'
3102667Sstever@eecs.umich.edu    Exit(1)    
3112667Sstever@eecs.umich.edu
3122667Sstever@eecs.umich.edumakeEnv('debug', '.do',
3135742Snate@binkert.org        CCFLAGS = Split(ccflags['debug']),
3145742Snate@binkert.org        CPPDEFINES = ['DEBUG', 'TRACING_ON=1'])
3155742Snate@binkert.org
3165793Snate@binkert.org# Optimized binary
3178334Snate@binkert.orgmakeEnv('opt', '.o',
3185793Snate@binkert.org        CCFLAGS = Split(ccflags['opt']),
3195793Snate@binkert.org        CPPDEFINES = ['TRACING_ON=1'])
3205793Snate@binkert.org
3214382Sbinkertn@umich.edu# "Fast" binary
3224762Snate@binkert.orgmakeEnv('fast', '.fo', strip = True,
3235344Sstever@gmail.com        CCFLAGS = Split(ccflags['fast']),
3244382Sbinkertn@umich.edu        CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'])
3255341Sstever@gmail.com
3265742Snate@binkert.org# Profiled binary
3275742Snate@binkert.orgmakeEnv('prof', '.po',
3285742Snate@binkert.org        CCFLAGS = Split(ccflags['prof']),
3295742Snate@binkert.org        CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
3305742Snate@binkert.org        LINKFLAGS = '-pg')
3314762Snate@binkert.org
3325742Snate@binkert.orgReturn('envList')
3335742Snate@binkert.org