SConscript revision 5463
1# -*- mode:python -*-
2
3# Copyright (c) 2004-2005 The Regents of The University of Michigan
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions are
8# met: redistributions of source code must retain the above copyright
9# notice, this list of conditions and the following disclaimer;
10# redistributions in binary form must reproduce the above copyright
11# notice, this list of conditions and the following disclaimer in the
12# documentation and/or other materials provided with the distribution;
13# neither the name of the copyright holders nor the names of its
14# contributors may be used to endorse or promote products derived from
15# this software without specific prior written permission.
16#
17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28#
29# Authors: Nathan Binkert
30
31import imp
32import os
33import sys
34
35from os.path import basename, exists, isdir, isfile, join as joinpath
36
37import SCons
38
39# This file defines how to build a particular configuration of M5
40# based on variable settings in the 'env' build environment.
41
42Import('*')
43
44# Children need to see the environment
45Export('env')
46
47def sort_list(_list):
48    """return a sorted copy of '_list'"""
49    if isinstance(_list, list):
50        _list = _list[:]
51    else:
52        _list = list(_list)
53    _list.sort()
54    return _list
55
56class PySourceFile(object):
57    def __init__(self, package, source):
58        filename = str(source)
59        pyname = basename(filename)
60        assert pyname.endswith('.py')
61        name = pyname[:-3]
62        path = package.split('.')
63        modpath = path
64        if name != '__init__':
65            modpath += [name]
66        modpath = '.'.join(modpath)
67
68        arcpath = package.split('.') + [ pyname + 'c' ]
69        arcname = joinpath(*arcpath)
70
71        self.source = source
72        self.pyname = pyname
73        self.srcpath = source.srcnode().abspath
74        self.package = package
75        self.modpath = modpath
76        self.arcname = arcname
77        self.filename = filename
78        self.compiled = File(filename + 'c')
79
80########################################################################
81# Code for adding source files of various types
82#
83cc_sources = []
84def Source(source):
85    '''Add a C/C++ source file to the build'''
86    if not isinstance(source, SCons.Node.FS.File):
87        source = File(source)
88
89    cc_sources.append(source)
90
91py_sources = []
92def PySource(package, source):
93    '''Add a python source file to the named package'''
94    if not isinstance(source, SCons.Node.FS.File):
95        source = File(source)
96
97    source = PySourceFile(package, source)
98    py_sources.append(source)
99
100sim_objects_fixed = False
101sim_object_modfiles = set()
102def SimObject(source):
103    '''Add a SimObject python file as a python source object and add
104    it to a list of sim object modules'''
105
106    if sim_objects_fixed:
107        raise AttributeError, "Too late to call SimObject now."
108
109    if not isinstance(source, SCons.Node.FS.File):
110        source = File(source)
111
112    PySource('m5.objects', source)
113    modfile = basename(str(source))
114    assert modfile.endswith('.py')
115    modname = modfile[:-3]
116    sim_object_modfiles.add(modname)
117
118swig_sources = []
119def SwigSource(package, source):
120    '''Add a swig file to build'''
121    if not isinstance(source, SCons.Node.FS.File):
122        source = File(source)
123    val = source,package
124    swig_sources.append(val)
125
126# Children should have access
127Export('Source')
128Export('PySource')
129Export('SimObject')
130Export('SwigSource')
131
132########################################################################
133#
134# Trace Flags
135#
136all_flags = {}
137trace_flags = []
138def TraceFlag(name, desc=''):
139    if name in all_flags:
140        raise AttributeError, "Flag %s already specified" % name
141    flag = (name, (), desc)
142    trace_flags.append(flag)
143    all_flags[name] = ()
144
145def CompoundFlag(name, flags, desc=''):
146    if name in all_flags:
147        raise AttributeError, "Flag %s already specified" % name
148
149    compound = tuple(flags)
150    for flag in compound:
151        if flag not in all_flags:
152            raise AttributeError, "Trace flag %s not found" % flag
153        if all_flags[flag]:
154            raise AttributeError, \
155                "Compound flag can't point to another compound flag"
156
157    flag = (name, compound, desc)
158    trace_flags.append(flag)
159    all_flags[name] = compound
160
161Export('TraceFlag')
162Export('CompoundFlag')
163
164########################################################################
165#
166# Set some compiler variables
167#
168
169# Include file paths are rooted in this directory.  SCons will
170# automatically expand '.' to refer to both the source directory and
171# the corresponding build directory to pick up generated include
172# files.
173env.Append(CPPPATH=Dir('.'))
174
175# Add a flag defining what THE_ISA should be for all compilation
176env.Append(CPPDEFINES=[('THE_ISA','%s_ISA' % env['TARGET_ISA'].upper())])
177
178########################################################################
179#
180# Walk the tree and execute all SConscripts in subdirectories
181#
182
183for base_dir in base_dir_list:
184    here = Dir('.').srcnode().abspath
185    for root, dirs, files in os.walk(base_dir, topdown=True):
186        if root == here:
187            # we don't want to recurse back into this SConscript
188            continue
189
190        if 'SConscript' in files:
191            build_dir = joinpath(env['BUILDDIR'], root[len(base_dir) + 1:])
192            SConscript(joinpath(root, 'SConscript'), build_dir=build_dir)
193
194for opt in env.ExportOptions:
195    env.ConfigFile(opt)
196
197########################################################################
198#
199# Prevent any SimObjects from being added after this point, they
200# should all have been added in the SConscripts above
201#
202sim_objects_fixed = True
203
204########################################################################
205#
206# Manually turn python/generate.py into a python module and import it
207#
208generate_file = File('python/generate.py')
209generate_module = imp.new_module('generate')
210sys.modules['generate'] = generate_module
211exec file(generate_file.srcnode().abspath, 'r') in generate_module.__dict__
212
213########################################################################
214#
215# build a generate
216#
217from generate import Generate
218optionDict = dict([(opt, env[opt]) for opt in env.ExportOptions])
219generate = Generate(py_sources, sim_object_modfiles, optionDict)
220m5 = generate.m5
221
222########################################################################
223#
224# calculate extra dependencies
225#
226module_depends = ["m5", "m5.SimObject", "m5.params"]
227module_depends = [ File(generate.py_modules[dep]) for dep in module_depends ]
228file_depends = [ generate_file ]
229depends = module_depends + file_depends
230
231########################################################################
232#
233# Commands for the basic automatically generated python files
234#
235
236# Generate a file with all of the compile options in it
237env.Command('python/m5/defines.py', Value(optionDict),
238            generate.makeDefinesPyFile)
239PySource('m5', 'python/m5/defines.py')
240
241# Generate a file that wraps the basic top level files
242env.Command('python/m5/info.py',
243            [ '#/AUTHORS', '#/LICENSE', '#/README', '#/RELEASE_NOTES' ],
244            generate.makeInfoPyFile)
245PySource('m5', 'python/m5/info.py')
246
247# Generate an __init__.py file for the objects package
248env.Command('python/m5/objects/__init__.py',
249            [ Value(o) for o in sort_list(sim_object_modfiles) ],
250            generate.makeObjectsInitFile)
251PySource('m5.objects', 'python/m5/objects/__init__.py')
252
253########################################################################
254#
255# Create all of the SimObject param headers and enum headers
256#
257
258# Generate all of the SimObject param struct header files
259params_hh_files = []
260for name,simobj in generate.sim_objects.iteritems():
261    extra_deps = [ File(generate.py_modules[simobj.__module__]) ]
262
263    hh_file = File('params/%s.hh' % name)
264    params_hh_files.append(hh_file)
265    env.Command(hh_file, Value(name), generate.createSimObjectParam)
266    env.Depends(hh_file, depends + extra_deps)
267
268# Generate any parameter header files needed
269params_i_files = []
270for name,param in generate.params.iteritems():
271    if isinstance(param, m5.params.VectorParamDesc):
272        ext = 'vptype'
273    else:
274        ext = 'ptype'
275
276    i_file = File('params/%s_%s.i' % (name, ext))
277    params_i_files.append(i_file)
278    env.Command(i_file, Value(name), generate.createSwigParam)
279    env.Depends(i_file, depends)
280
281# Generate all enum header files
282for name,enum in generate.enums.iteritems():
283    extra_deps = [ File(generate.py_modules[enum.__module__]) ]
284
285    cc_file = File('enums/%s.cc' % name)
286    env.Command(cc_file, Value(name), generate.createEnumStrings)
287    env.Depends(cc_file, depends + extra_deps)
288    Source(cc_file)
289
290    hh_file = File('enums/%s.hh' % name)
291    env.Command(hh_file, Value(name), generate.createEnumParam)
292    env.Depends(hh_file, depends + extra_deps)
293
294# Build the big monolithic swigged params module (wraps all SimObject
295# param structs and enum structs)
296params_file = File('params/params.i')
297names = sort_list(generate.sim_objects.keys())
298env.Command(params_file, [ Value(v) for v in names ],
299            generate.buildParams)
300env.Depends(params_file, params_hh_files + params_i_files + depends)
301SwigSource('m5.objects', params_file)
302
303# Build all swig modules
304swig_modules = []
305for source,package in swig_sources:
306    filename = str(source)
307    assert filename.endswith('.i')
308
309    base = '.'.join(filename.split('.')[:-1])
310    module = basename(base)
311    cc_file = base + '_wrap.cc'
312    py_file = base + '.py'
313
314    env.Command([cc_file, py_file], source,
315                '$SWIG $SWIGFLAGS -outdir ${TARGETS[1].dir} '
316                '-o ${TARGETS[0]} $SOURCES')
317    env.Depends(py_file, source)
318    env.Depends(cc_file, source)
319
320    swig_modules.append(Value(module))
321    Source(cc_file)
322    PySource(package, py_file)
323
324# Generate the main swig init file
325env.Command('swig/init.cc', swig_modules, generate.makeSwigInit)
326Source('swig/init.cc')
327
328# Generate traceflags.py
329flags = [ Value(f) for f in trace_flags ]
330env.Command('base/traceflags.py', flags, generate.traceFlagsPy)
331PySource('m5', 'base/traceflags.py')
332
333env.Command('base/traceflags.hh', flags, generate.traceFlagsHH)
334env.Command('base/traceflags.cc', flags, generate.traceFlagsCC)
335Source('base/traceflags.cc')
336
337# Generate program_info.cc
338env.Command('base/program_info.cc',
339            Value(str(SCons.Node.FS.default_fs.SConstruct_dir)),
340            generate.programInfo)
341
342# Build the zip file
343py_compiled = []
344py_zip_depends = []
345for source in py_sources:
346    env.Command(source.compiled, source.source, generate.compilePyFile)
347    py_compiled.append(source.compiled)
348
349    # make the zipfile depend on the archive name so that the archive
350    # is rebuilt if the name changes
351    py_zip_depends.append(Value(source.arcname))
352
353# Add the zip file target to the environment.
354m5zip = File('m5py.zip')
355env.Command(m5zip, py_compiled, generate.buildPyZip)
356env.Depends(m5zip, py_zip_depends)
357
358########################################################################
359#
360# Define binaries.  Each different build type (debug, opt, etc.) gets
361# a slightly different build environment.
362#
363
364# List of constructed environments to pass back to SConstruct
365envList = []
366
367# This function adds the specified sources to the given build
368# environment, and returns a list of all the corresponding SCons
369# Object nodes (including an extra one for date.cc).  We explicitly
370# add the Object nodes so we can set up special dependencies for
371# date.cc.
372def make_objs(sources, env):
373    objs = [env.Object(s) for s in sources]
374  
375    # make date.cc depend on all other objects so it always gets
376    # recompiled whenever anything else does
377    date_obj = env.Object('base/date.cc')
378
379    # Make the generation of program_info.cc dependend on all 
380    # the other cc files and the compiling of program_info.cc 
381    # dependent on all the objects but program_info.o 
382    pinfo_obj = env.Object('base/program_info.cc')
383    env.Depends('base/program_info.cc', sources)
384    env.Depends(date_obj, objs)
385    env.Depends(pinfo_obj, objs)
386    objs.extend([date_obj,pinfo_obj])
387    return objs
388
389# Function to create a new build environment as clone of current
390# environment 'env' with modified object suffix and optional stripped
391# binary.  Additional keyword arguments are appended to corresponding
392# build environment vars.
393def makeEnv(label, objsfx, strip = False, **kwargs):
394    newEnv = env.Copy(OBJSUFFIX=objsfx)
395    newEnv.Label = label
396    newEnv.Append(**kwargs)
397    exe = 'm5.' + label  # final executable
398    bin = exe + '.bin'   # executable w/o appended Python zip archive
399    newEnv.Program(bin, make_objs(cc_sources, newEnv))
400    if strip:
401        stripped_bin = bin + '.stripped'
402        if sys.platform == 'sunos5':
403            cmd = 'cp $SOURCE $TARGET; strip $TARGET'
404        else:
405            cmd = 'strip $SOURCE -o $TARGET'
406        newEnv.Command(stripped_bin, bin, cmd)
407        bin = stripped_bin
408    targets = newEnv.Concat(exe, [bin, 'm5py.zip'])
409    newEnv.M5Binary = targets[0]
410    envList.append(newEnv)
411
412# Debug binary
413ccflags = {}
414if env['GCC']:
415    if sys.platform == 'sunos5':
416        ccflags['debug'] = '-gstabs+'
417    else:
418        ccflags['debug'] = '-ggdb3'
419    ccflags['opt'] = '-g -O3'
420    ccflags['fast'] = '-O3'
421    ccflags['prof'] = '-O3 -g -pg'
422elif env['SUNCC']:
423    ccflags['debug'] = '-g0'
424    ccflags['opt'] = '-g -O'
425    ccflags['fast'] = '-fast'
426    ccflags['prof'] = '-fast -g -pg'
427elif env['ICC']:
428    ccflags['debug'] = '-g -O0'
429    ccflags['opt'] = '-g -O'
430    ccflags['fast'] = '-fast'
431    ccflags['prof'] = '-fast -g -pg'
432else:
433    print 'Unknown compiler, please fix compiler options'
434    Exit(1)
435
436makeEnv('debug', '.do',
437        CCFLAGS = Split(ccflags['debug']),
438        CPPDEFINES = ['DEBUG', 'TRACING_ON=1'])
439
440# Optimized binary
441makeEnv('opt', '.o',
442        CCFLAGS = Split(ccflags['opt']),
443        CPPDEFINES = ['TRACING_ON=1'])
444
445# "Fast" binary
446makeEnv('fast', '.fo', strip = True,
447        CCFLAGS = Split(ccflags['fast']),
448        CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'])
449
450# Profiled binary
451makeEnv('prof', '.po',
452        CCFLAGS = Split(ccflags['prof']),
453        CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
454        LINKFLAGS = '-pg')
455
456Return('envList')
457