SConscript revision 4486
16167SN/A# -*- mode:python -*-
26167SN/A
39204Sandreas.hansson@arm.com# Copyright (c) 2004-2005 The Regents of The University of Michigan
49204Sandreas.hansson@arm.com# All rights reserved.
59204Sandreas.hansson@arm.com#
68343SN/A# Redistribution and use in source and binary forms, with or without
79204Sandreas.hansson@arm.com# modification, are permitted provided that the following conditions are
89204Sandreas.hansson@arm.com# met: redistributions of source code must retain the above copyright
99204Sandreas.hansson@arm.com# notice, this list of conditions and the following disclaimer;
109204Sandreas.hansson@arm.com# redistributions in binary form must reproduce the above copyright
119204Sandreas.hansson@arm.com# notice, this list of conditions and the following disclaimer in the
129150SAli.Saidi@ARM.com# documentation and/or other materials provided with the distribution;
139150SAli.Saidi@ARM.com# neither the name of the copyright holders nor the names of its
149150SAli.Saidi@ARM.com# contributors may be used to endorse or promote products derived from
159150SAli.Saidi@ARM.com# this software without specific prior written permission.
169150SAli.Saidi@ARM.com#
179150SAli.Saidi@ARM.com# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
189150SAli.Saidi@ARM.com# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
199055Ssaidi@eecs.umich.edu# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
209055Ssaidi@eecs.umich.edu# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
219150SAli.Saidi@ARM.com# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
229150SAli.Saidi@ARM.com# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
239150SAli.Saidi@ARM.com# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
249055Ssaidi@eecs.umich.edu# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
259055Ssaidi@eecs.umich.edu# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
269204Sandreas.hansson@arm.com# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
279204Sandreas.hansson@arm.com# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
289204Sandreas.hansson@arm.com#
299204Sandreas.hansson@arm.com# Authors: Steve Reinhardt
309204Sandreas.hansson@arm.com
319204Sandreas.hansson@arm.comimport os
329204Sandreas.hansson@arm.comimport sys
339204Sandreas.hansson@arm.comimport zipfile
349204Sandreas.hansson@arm.com
359204Sandreas.hansson@arm.comfrom os.path import basename
369113SBrad.Beckmann@amd.comfrom os.path import join as joinpath
379113SBrad.Beckmann@amd.com
389113SBrad.Beckmann@amd.comimport SCons
399113SBrad.Beckmann@amd.com
409113SBrad.Beckmann@amd.com# This file defines how to build a particular configuration of M5
419113SBrad.Beckmann@amd.com# based on variable settings in the 'env' build environment.
428343SN/A
439204Sandreas.hansson@arm.comImport('*')
448343SN/A
457935SN/A# Children need to see the environment
469150SAli.Saidi@ARM.comExport('env')
479150SAli.Saidi@ARM.com
489150SAli.Saidi@ARM.com########################################################################
497935SN/A# Code for adding source files
508343SN/A#
519150SAli.Saidi@ARM.comsources = []
529150SAli.Saidi@ARM.comdef Source(source):
537935SN/A    if isinstance(source, SCons.Node.FS.File):
549150SAli.Saidi@ARM.com        sources.append(source)
559150SAli.Saidi@ARM.com    else:
567935SN/A        sources.append(File(source))
577935SN/A
589150SAli.Saidi@ARM.com# Children should have access
599150SAli.Saidi@ARM.comExport('Source')
608343SN/A
617935SN/A########################################################################
629204Sandreas.hansson@arm.com# Code for adding python objects
638343SN/A#
648343SN/Apy_sources = []
656167SN/Apy_source_packages = {}
666167SN/Adef PySource(package, source):
67    if not isinstance(source, SCons.Node.FS.File):
68        source = File(source)
69    py_source_packages[source] = package
70    py_sources.append(source)
71
72sim_objects = []
73def SimObject(source):
74    if not isinstance(source, SCons.Node.FS.File):
75        source = File(source)
76    PySource('m5.objects', source)
77    modname = basename(str(source))
78    sim_objects.append(modname)
79
80swig_sources = []
81swig_source_packages = {}
82def SwigSource(package, source):
83    if not isinstance(source, SCons.Node.FS.File):
84        source = File(source)
85    swig_source_packages[source] = package
86    swig_sources.append(source)
87
88# Children should have access
89Export('PySource')
90Export('SimObject')
91Export('SwigSource')
92
93########################################################################
94#
95# Set some compiler variables
96#
97
98# Include file paths are rooted in this directory.  SCons will
99# automatically expand '.' to refer to both the source directory and
100# the corresponding build directory to pick up generated include
101# files.
102env.Append(CPPPATH=Dir('.'))
103
104# Add a flag defining what THE_ISA should be for all compilation
105env.Append(CPPDEFINES=[('THE_ISA','%s_ISA' % env['TARGET_ISA'].upper())])
106
107########################################################################
108# Walk the tree and execute all SConscripts
109#
110scripts = []
111srcdir = env['SRCDIR']
112for root, dirs, files in os.walk(srcdir, topdown=True):
113    if root == srcdir:
114        # we don't want to recurse back into this SConscript
115        continue
116    
117    if 'SConscript' in files:
118        # strip off the srcdir part since scons will try to find the
119        # script in the build directory
120        base = root[len(srcdir) + 1:]
121        SConscript(joinpath(base, 'SConscript'))
122
123for opt in env.ExportOptions:
124    env.ConfigFile(opt)
125
126########################################################################
127#
128# Deal with python/swig, object code.  Collect .py files and
129# generating a zip archive that is appended to the m5 binary.
130#
131
132# Generate Python file that contains a dict specifying the current
133# build_env flags.
134def MakeDefinesPyFile(target, source, env):
135    f = file(str(target[0]), 'w')
136    print >>f, "m5_build_env = ", source[0]
137    f.close()
138
139optionDict = dict([(opt, env[opt]) for opt in env.ExportOptions])
140env.Command('python/m5/defines.py', Value(optionDict), MakeDefinesPyFile)
141PySource('m5', 'python/m5/defines.py')
142
143def MakeInfoPyFile(target, source, env):
144    f = file(str(target[0]), 'w')
145    for src in source:
146        data = ''.join(file(src.srcnode().abspath, 'r').xreadlines())
147        print >>f, "%s = %s" % (src, repr(data))
148    f.close()
149
150env.Command('python/m5/info.py',
151            [ '#/AUTHORS', '#/LICENSE', '#/README', '#/RELEASE_NOTES' ],
152            MakeInfoPyFile)
153PySource('m5', 'python/m5/info.py')
154
155def MakeObjectsInitFile(target, source, env):
156    f = file(str(target[0]), 'w')
157    print >>f, 'from m5.SimObject import *'
158    for src_path in source:
159        src_file = basename(src_path.get_contents())
160        assert(src_file.endswith('.py'))
161        src_module = src_file[:-3]
162        print >>f, 'from %s import *' % src_module
163    f.close()
164
165env.Command('python/m5/objects/__init__.py',
166            [ Value(o) for o in sim_objects],
167            MakeObjectsInitFile)
168PySource('m5.objects', 'python/m5/objects/__init__.py')
169
170swig_modules = []
171for source in swig_sources:
172    source.rfile() # Hack to cause the symlink to the .i file to be created
173    package = swig_source_packages[source]
174    filename = str(source)
175    module = basename(filename)
176
177    assert(module.endswith('.i'))
178    module = module[:-2]
179    cc_file = 'swig/%s_wrap.cc' % module
180    py_file = 'm5/internal/%s.py' % module
181
182    env.Command([cc_file, py_file], source,
183                '$SWIG $SWIGFLAGS -outdir ${TARGETS[1].dir} '
184                '-o ${TARGETS[0]} $SOURCES')
185    env.Depends(py_file, source)
186    env.Depends(cc_file, source)
187                
188    swig_modules.append(Value(module))
189    Source(cc_file)
190    PySource(package, py_file)
191
192def MakeSwigInit(target, source, env):
193    f = file(str(target[0]), 'w')
194    print >>f, 'extern "C" {'
195    for module in source:
196        print >>f, '    void init_%s();' % module.get_contents()
197    print >>f, '}'
198    print >>f, 'void init_swig() {'
199    for module in source:
200        print >>f, '    init_%s();' % module.get_contents()
201    print >>f, '}'
202    f.close()
203env.Command('python/swig/init.cc', swig_modules, MakeSwigInit)
204
205def CompilePyFile(target, source, env):
206    import py_compile
207    py_compile.compile(str(source[0]), str(target[0]))
208
209py_compiled = []
210py_arcname = {}
211py_zip_depends = []
212for source in py_sources:
213    filename = str(source)
214    package = py_source_packages[source]
215    arc_path = package.split('.') + [ basename(filename) + 'c' ]
216    zip_path = [ 'zip' ] + arc_path
217    arcname = joinpath(*arc_path)
218    zipname = joinpath(*zip_path)
219    f = File(zipname)
220
221    env.Command(f, source, CompilePyFile)
222    py_compiled.append(f)
223    py_arcname[f] = arcname
224
225    # make the zipfile depend on the archive name so that the archive
226    # is rebuilt if the name changes
227    py_zip_depends.append(Value(arcname))
228
229# Action function to build the zip archive.  Uses the PyZipFile module
230# included in the standard Python library.
231def buildPyZip(target, source, env):
232    zf = zipfile.ZipFile(str(target[0]), 'w')
233    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