SConscript revision 13576
1955SN/A# -*- mode:python -*-
2955SN/A
313576Sciro.santilli@arm.com# Copyright (c) 2018 ARM Limited
413576Sciro.santilli@arm.com#
513576Sciro.santilli@arm.com# The license below extends only to copyright in the software and shall
613576Sciro.santilli@arm.com# not be construed as granting a license to any other intellectual
713576Sciro.santilli@arm.com# property including but not limited to intellectual property relating
813576Sciro.santilli@arm.com# to a hardware implementation of the functionality of the software
913576Sciro.santilli@arm.com# licensed hereunder.  You may use the software subject to the license
1013576Sciro.santilli@arm.com# terms below provided that you ensure that this notice is replicated
1113576Sciro.santilli@arm.com# unmodified and in its entirety in all distributions of the software,
1213576Sciro.santilli@arm.com# modified or unmodified, in source code or in binary form.
1313576Sciro.santilli@arm.com#
141762SN/A# Copyright (c) 2004-2005 The Regents of The University of Michigan
15955SN/A# All rights reserved.
16955SN/A#
17955SN/A# Redistribution and use in source and binary forms, with or without
18955SN/A# modification, are permitted provided that the following conditions are
19955SN/A# met: redistributions of source code must retain the above copyright
20955SN/A# notice, this list of conditions and the following disclaimer;
21955SN/A# redistributions in binary form must reproduce the above copyright
22955SN/A# notice, this list of conditions and the following disclaimer in the
23955SN/A# documentation and/or other materials provided with the distribution;
24955SN/A# neither the name of the copyright holders nor the names of its
25955SN/A# contributors may be used to endorse or promote products derived from
26955SN/A# this software without specific prior written permission.
27955SN/A#
28955SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29955SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30955SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31955SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32955SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33955SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34955SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35955SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36955SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37955SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38955SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
392665Ssaidi@eecs.umich.edu#
404762Snate@binkert.org# Authors: Nathan Binkert
41955SN/A
4212563Sgabeblack@google.comfrom __future__ import print_function
4312563Sgabeblack@google.com
445522Snate@binkert.orgimport array
456143Snate@binkert.orgimport bisect
4612371Sgabeblack@google.comimport functools
474762Snate@binkert.orgimport imp
485522Snate@binkert.orgimport marshal
49955SN/Aimport os
505522Snate@binkert.orgimport re
5111974Sgabeblack@google.comimport subprocess
52955SN/Aimport sys
535522Snate@binkert.orgimport zlib
544202Sbinkertn@umich.edu
555742Snate@binkert.orgfrom os.path import basename, dirname, exists, isdir, isfile, join as joinpath
56955SN/A
574381Sbinkertn@umich.eduimport SCons
584381Sbinkertn@umich.edu
5912246Sgabeblack@google.comfrom gem5_scons import Transform
6012246Sgabeblack@google.com
618334Snate@binkert.org# This file defines how to build a particular configuration of gem5
62955SN/A# based on variable settings in the 'env' build environment.
63955SN/A
644202Sbinkertn@umich.eduImport('*')
65955SN/A
664382Sbinkertn@umich.edu# Children need to see the environment
674382Sbinkertn@umich.eduExport('env')
684382Sbinkertn@umich.edu
696654Snate@binkert.orgbuild_env = [(opt, env[opt]) for opt in export_vars]
705517Snate@binkert.org
718614Sgblack@eecs.umich.edufrom m5.util import code_formatter, compareVersions
727674Snate@binkert.org
736143Snate@binkert.org########################################################################
746143Snate@binkert.org# Code for adding source files of various types
756143Snate@binkert.org#
7612302Sgabeblack@google.com# When specifying a source file of some type, a set of tags can be
7712302Sgabeblack@google.com# specified for that file.
7812302Sgabeblack@google.com
7912371Sgabeblack@google.comclass SourceFilter(object):
8012371Sgabeblack@google.com    def __init__(self, predicate):
8112371Sgabeblack@google.com        self.predicate = predicate
8212371Sgabeblack@google.com
8312371Sgabeblack@google.com    def __or__(self, other):
8412371Sgabeblack@google.com        return SourceFilter(lambda tags: self.predicate(tags) or
8512371Sgabeblack@google.com                                         other.predicate(tags))
8612371Sgabeblack@google.com
8712371Sgabeblack@google.com    def __and__(self, other):
8812371Sgabeblack@google.com        return SourceFilter(lambda tags: self.predicate(tags) and
8912371Sgabeblack@google.com                                         other.predicate(tags))
9012371Sgabeblack@google.com
9112371Sgabeblack@google.comdef with_tags_that(predicate):
9212371Sgabeblack@google.com    '''Return a list of sources with tags that satisfy a predicate.'''
9312371Sgabeblack@google.com    return SourceFilter(predicate)
9412371Sgabeblack@google.com
9512371Sgabeblack@google.comdef with_any_tags(*tags):
9612371Sgabeblack@google.com    '''Return a list of sources with any of the supplied tags.'''
9712371Sgabeblack@google.com    return SourceFilter(lambda stags: len(set(tags) & stags) > 0)
9812371Sgabeblack@google.com
9912371Sgabeblack@google.comdef with_all_tags(*tags):
10012371Sgabeblack@google.com    '''Return a list of sources with all of the supplied tags.'''
10112371Sgabeblack@google.com    return SourceFilter(lambda stags: set(tags) <= stags)
10212371Sgabeblack@google.com
10312371Sgabeblack@google.comdef with_tag(tag):
10412371Sgabeblack@google.com    '''Return a list of sources with the supplied tag.'''
10512371Sgabeblack@google.com    return SourceFilter(lambda stags: tag in stags)
10612371Sgabeblack@google.com
10712371Sgabeblack@google.comdef without_tags(*tags):
10812371Sgabeblack@google.com    '''Return a list of sources without any of the supplied tags.'''
10912371Sgabeblack@google.com    return SourceFilter(lambda stags: len(set(tags) & stags) == 0)
11012371Sgabeblack@google.com
11112371Sgabeblack@google.comdef without_tag(tag):
11212371Sgabeblack@google.com    '''Return a list of sources with the supplied tag.'''
11312371Sgabeblack@google.com    return SourceFilter(lambda stags: tag not in stags)
11412371Sgabeblack@google.com
11512371Sgabeblack@google.comsource_filter_factories = {
11612371Sgabeblack@google.com    'with_tags_that': with_tags_that,
11712371Sgabeblack@google.com    'with_any_tags': with_any_tags,
11812371Sgabeblack@google.com    'with_all_tags': with_all_tags,
11912371Sgabeblack@google.com    'with_tag': with_tag,
12012371Sgabeblack@google.com    'without_tags': without_tags,
12112371Sgabeblack@google.com    'without_tag': without_tag,
12212371Sgabeblack@google.com}
12312371Sgabeblack@google.com
12412371Sgabeblack@google.comExport(source_filter_factories)
12512371Sgabeblack@google.com
12612302Sgabeblack@google.comclass SourceList(list):
12712371Sgabeblack@google.com    def apply_filter(self, f):
12812302Sgabeblack@google.com        def match(source):
12912371Sgabeblack@google.com            return f.predicate(source.tags)
13012302Sgabeblack@google.com        return SourceList(filter(match, self))
13112302Sgabeblack@google.com
13212371Sgabeblack@google.com    def __getattr__(self, name):
13312371Sgabeblack@google.com        func = source_filter_factories.get(name, None)
13412371Sgabeblack@google.com        if not func:
13512371Sgabeblack@google.com            raise AttributeError
13612302Sgabeblack@google.com
13712371Sgabeblack@google.com        @functools.wraps(func)
13812371Sgabeblack@google.com        def wrapper(*args, **kwargs):
13912371Sgabeblack@google.com            return self.apply_filter(func(*args, **kwargs))
14012371Sgabeblack@google.com        return wrapper
14111983Sgabeblack@google.com
1426143Snate@binkert.orgclass SourceMeta(type):
1438233Snate@binkert.org    '''Meta class for source files that keeps track of all files of a
14412302Sgabeblack@google.com    particular type.'''
1456143Snate@binkert.org    def __init__(cls, name, bases, dict):
1466143Snate@binkert.org        super(SourceMeta, cls).__init__(name, bases, dict)
14712302Sgabeblack@google.com        cls.all = SourceList()
1484762Snate@binkert.org
1496143Snate@binkert.orgclass SourceFile(object):
1508233Snate@binkert.org    '''Base object that encapsulates the notion of a source file.
1518233Snate@binkert.org    This includes, the source node, target node, various manipulations
15212302Sgabeblack@google.com    of those.  A source file also specifies a set of tags which
15312302Sgabeblack@google.com    describing arbitrary properties of the source file.'''
1546143Snate@binkert.org    __metaclass__ = SourceMeta
15512362Sgabeblack@google.com
15612362Sgabeblack@google.com    static_objs = {}
15712362Sgabeblack@google.com    shared_objs = {}
15812362Sgabeblack@google.com
15912302Sgabeblack@google.com    def __init__(self, source, tags=None, add_tags=None):
16012302Sgabeblack@google.com        if tags is None:
16112302Sgabeblack@google.com            tags='gem5 lib'
16212302Sgabeblack@google.com        if isinstance(tags, basestring):
16312302Sgabeblack@google.com            tags = set([tags])
16412363Sgabeblack@google.com        if not isinstance(tags, set):
16512363Sgabeblack@google.com            tags = set(tags)
16612363Sgabeblack@google.com        self.tags = tags
16712363Sgabeblack@google.com
16812302Sgabeblack@google.com        if add_tags:
16912363Sgabeblack@google.com            if isinstance(add_tags, basestring):
17012363Sgabeblack@google.com                add_tags = set([add_tags])
17112363Sgabeblack@google.com            if not isinstance(add_tags, set):
17212363Sgabeblack@google.com                add_tags = set(add_tags)
17312363Sgabeblack@google.com            self.tags |= add_tags
1748233Snate@binkert.org
1756143Snate@binkert.org        tnode = source
1766143Snate@binkert.org        if not isinstance(source, SCons.Node.FS.File):
1776143Snate@binkert.org            tnode = File(source)
1786143Snate@binkert.org
1796143Snate@binkert.org        self.tnode = tnode
1806143Snate@binkert.org        self.snode = tnode.srcnode()
1816143Snate@binkert.org
1826143Snate@binkert.org        for base in type(self).__mro__:
1836143Snate@binkert.org            if issubclass(base, SourceFile):
1847065Snate@binkert.org                base.all.append(self)
1856143Snate@binkert.org
18612362Sgabeblack@google.com    def static(self, env):
18712362Sgabeblack@google.com        key = (self.tnode, env['OBJSUFFIX'])
18812362Sgabeblack@google.com        if not key in self.static_objs:
18912362Sgabeblack@google.com            self.static_objs[key] = env.StaticObject(self.tnode)
19012362Sgabeblack@google.com        return self.static_objs[key]
19112362Sgabeblack@google.com
19212362Sgabeblack@google.com    def shared(self, env):
19312362Sgabeblack@google.com        key = (self.tnode, env['OBJSUFFIX'])
19412362Sgabeblack@google.com        if not key in self.shared_objs:
19512362Sgabeblack@google.com            self.shared_objs[key] = env.SharedObject(self.tnode)
19612362Sgabeblack@google.com        return self.shared_objs[key]
19712362Sgabeblack@google.com
1988233Snate@binkert.org    @property
1998233Snate@binkert.org    def filename(self):
2008233Snate@binkert.org        return str(self.tnode)
2018233Snate@binkert.org
2028233Snate@binkert.org    @property
2038233Snate@binkert.org    def dirname(self):
2048233Snate@binkert.org        return dirname(self.filename)
2058233Snate@binkert.org
2068233Snate@binkert.org    @property
2078233Snate@binkert.org    def basename(self):
2088233Snate@binkert.org        return basename(self.filename)
2098233Snate@binkert.org
2108233Snate@binkert.org    @property
2118233Snate@binkert.org    def extname(self):
2128233Snate@binkert.org        index = self.basename.rfind('.')
2138233Snate@binkert.org        if index <= 0:
2148233Snate@binkert.org            # dot files aren't extensions
2158233Snate@binkert.org            return self.basename, None
2168233Snate@binkert.org
2178233Snate@binkert.org        return self.basename[:index], self.basename[index+1:]
2188233Snate@binkert.org
2196143Snate@binkert.org    def __lt__(self, other): return self.filename < other.filename
2206143Snate@binkert.org    def __le__(self, other): return self.filename <= other.filename
2216143Snate@binkert.org    def __gt__(self, other): return self.filename > other.filename
2226143Snate@binkert.org    def __ge__(self, other): return self.filename >= other.filename
2236143Snate@binkert.org    def __eq__(self, other): return self.filename == other.filename
2246143Snate@binkert.org    def __ne__(self, other): return self.filename != other.filename
2259982Satgutier@umich.edu
22613576Sciro.santilli@arm.comdef blobToCpp(data, symbol, cpp_code, hpp_code=None, namespace=None):
22713576Sciro.santilli@arm.com    '''
22813576Sciro.santilli@arm.com    Convert bytes data into C++ .cpp and .hh uint8_t byte array
22913576Sciro.santilli@arm.com    code containing that binary data.
23013576Sciro.santilli@arm.com
23113576Sciro.santilli@arm.com    :param data: binary data to be converted to C++
23213576Sciro.santilli@arm.com    :param symbol: name of the symbol
23313576Sciro.santilli@arm.com    :param cpp_code: append the generated cpp_code to this object
23413576Sciro.santilli@arm.com    :param hpp_code: append the generated hpp_code to this object
23513576Sciro.santilli@arm.com                     If None, ignore it. Otherwise, also include it
23613576Sciro.santilli@arm.com                     in the .cpp file.
23713576Sciro.santilli@arm.com    :param namespace: namespace to put the symbol into. If None,
23813576Sciro.santilli@arm.com                      don't put the symbols into any namespace.
23913576Sciro.santilli@arm.com    '''
24013576Sciro.santilli@arm.com    symbol_len_declaration = 'const std::size_t {}_len'.format(symbol)
24113576Sciro.santilli@arm.com    symbol_declaration = 'const std::uint8_t {}[]'.format(symbol)
24213576Sciro.santilli@arm.com    if hpp_code is not None:
24313576Sciro.santilli@arm.com        cpp_code('''\
24413576Sciro.santilli@arm.com#include "blobs/{}.hh"
24513576Sciro.santilli@arm.com'''.format(symbol))
24613576Sciro.santilli@arm.com        hpp_code('''\
24713576Sciro.santilli@arm.com#include <cstddef>
24813576Sciro.santilli@arm.com#include <cstdint>
24913576Sciro.santilli@arm.com''')
25013576Sciro.santilli@arm.com        if namespace is not None:
25113576Sciro.santilli@arm.com            hpp_code('namespace {} {{'.format(namespace))
25213576Sciro.santilli@arm.com        hpp_code('extern ' + symbol_len_declaration + ';')
25313576Sciro.santilli@arm.com        hpp_code('extern ' + symbol_declaration + ';')
25413576Sciro.santilli@arm.com        if namespace is not None:
25513576Sciro.santilli@arm.com            hpp_code('}')
25613576Sciro.santilli@arm.com    if namespace is not None:
25713576Sciro.santilli@arm.com        cpp_code('namespace {} {{'.format(namespace))
25813576Sciro.santilli@arm.com    cpp_code(symbol_len_declaration + ' = {};'.format(len(data)))
25913576Sciro.santilli@arm.com    cpp_code(symbol_declaration + ' = {')
26013576Sciro.santilli@arm.com    cpp_code.indent()
26113576Sciro.santilli@arm.com    step = 16
26213576Sciro.santilli@arm.com    for i in xrange(0, len(data), step):
26313576Sciro.santilli@arm.com        x = array.array('B', data[i:i+step])
26413576Sciro.santilli@arm.com        cpp_code(''.join('%d,' % d for d in x))
26513576Sciro.santilli@arm.com    cpp_code.dedent()
26613576Sciro.santilli@arm.com    cpp_code('};')
26713576Sciro.santilli@arm.com    if namespace is not None:
26813576Sciro.santilli@arm.com        cpp_code('}')
26913576Sciro.santilli@arm.com
27013576Sciro.santilli@arm.comdef Blob(blob_path, symbol):
27113576Sciro.santilli@arm.com    '''
27213576Sciro.santilli@arm.com    Embed an arbitrary blob into the gem5 executable,
27313576Sciro.santilli@arm.com    and make it accessible to C++ as a byte array.
27413576Sciro.santilli@arm.com    '''
27513576Sciro.santilli@arm.com    blob_path = os.path.abspath(blob_path)
27613576Sciro.santilli@arm.com    blob_out_dir = os.path.join(env['BUILDDIR'], 'blobs')
27713576Sciro.santilli@arm.com    path_noext = joinpath(blob_out_dir, symbol)
27813576Sciro.santilli@arm.com    cpp_path = path_noext + '.cc'
27913576Sciro.santilli@arm.com    hpp_path = path_noext + '.hh'
28013576Sciro.santilli@arm.com    def embedBlob(target, source, env):
28113576Sciro.santilli@arm.com        data = file(str(source[0]), 'r').read()
28213576Sciro.santilli@arm.com        cpp_code = code_formatter()
28313576Sciro.santilli@arm.com        hpp_code = code_formatter()
28413576Sciro.santilli@arm.com        blobToCpp(data, symbol, cpp_code, hpp_code, namespace='Blobs')
28513576Sciro.santilli@arm.com        cpp_path = str(target[0])
28613576Sciro.santilli@arm.com        hpp_path = str(target[1])
28713576Sciro.santilli@arm.com        cpp_dir = os.path.split(cpp_path)[0]
28813576Sciro.santilli@arm.com        if not os.path.exists(cpp_dir):
28913576Sciro.santilli@arm.com            os.makedirs(cpp_dir)
29013576Sciro.santilli@arm.com        cpp_code.write(cpp_path)
29113576Sciro.santilli@arm.com        hpp_code.write(hpp_path)
29213576Sciro.santilli@arm.com    env.Command([cpp_path, hpp_path], blob_path,
29313576Sciro.santilli@arm.com                MakeAction(embedBlob, Transform("EMBED BLOB")))
29413576Sciro.santilli@arm.com    Source(cpp_path)
29513576Sciro.santilli@arm.com
2966143Snate@binkert.orgclass Source(SourceFile):
29712302Sgabeblack@google.com    ungrouped_tag = 'No link group'
29812302Sgabeblack@google.com    source_groups = set()
29912302Sgabeblack@google.com
30012302Sgabeblack@google.com    _current_group_tag = ungrouped_tag
30112302Sgabeblack@google.com
30212302Sgabeblack@google.com    @staticmethod
30312302Sgabeblack@google.com    def link_group_tag(group):
30412302Sgabeblack@google.com        return 'link group: %s' % group
30511983Sgabeblack@google.com
30611983Sgabeblack@google.com    @classmethod
30711983Sgabeblack@google.com    def set_group(cls, group):
30812302Sgabeblack@google.com        new_tag = Source.link_group_tag(group)
30912302Sgabeblack@google.com        Source._current_group_tag = new_tag
31012302Sgabeblack@google.com        Source.source_groups.add(group)
31112302Sgabeblack@google.com
31212302Sgabeblack@google.com    def _add_link_group_tag(self):
31312302Sgabeblack@google.com        self.tags.add(Source._current_group_tag)
31411983Sgabeblack@google.com
3156143Snate@binkert.org    '''Add a c/c++ source file to the build'''
31612305Sgabeblack@google.com    def __init__(self, source, tags=None, add_tags=None):
31712302Sgabeblack@google.com        '''specify the source file, and any tags'''
31812302Sgabeblack@google.com        super(Source, self).__init__(source, tags, add_tags)
31912302Sgabeblack@google.com        self._add_link_group_tag()
3206143Snate@binkert.org
3216143Snate@binkert.orgclass PySource(SourceFile):
3226143Snate@binkert.org    '''Add a python source file to the named package'''
3235522Snate@binkert.org    invalid_sym_char = re.compile('[^A-z0-9_]')
3246143Snate@binkert.org    modules = {}
3256143Snate@binkert.org    tnodes = {}
3266143Snate@binkert.org    symnames = {}
3279982Satgutier@umich.edu
32812302Sgabeblack@google.com    def __init__(self, package, source, tags=None, add_tags=None):
32912302Sgabeblack@google.com        '''specify the python package, the source file, and any tags'''
33012302Sgabeblack@google.com        super(PySource, self).__init__(source, tags, add_tags)
3316143Snate@binkert.org
3326143Snate@binkert.org        modname,ext = self.extname
3336143Snate@binkert.org        assert ext == 'py'
3346143Snate@binkert.org
3355522Snate@binkert.org        if package:
3365522Snate@binkert.org            path = package.split('.')
3375522Snate@binkert.org        else:
3385522Snate@binkert.org            path = []
3395604Snate@binkert.org
3405604Snate@binkert.org        modpath = path[:]
3416143Snate@binkert.org        if modname != '__init__':
3426143Snate@binkert.org            modpath += [ modname ]
3434762Snate@binkert.org        modpath = '.'.join(modpath)
3444762Snate@binkert.org
3456143Snate@binkert.org        arcpath = path + [ self.basename ]
3466727Ssteve.reinhardt@amd.com        abspath = self.snode.abspath
3476727Ssteve.reinhardt@amd.com        if not exists(abspath):
3486727Ssteve.reinhardt@amd.com            abspath = self.tnode.abspath
3494762Snate@binkert.org
3506143Snate@binkert.org        self.package = package
3516143Snate@binkert.org        self.modname = modname
3526143Snate@binkert.org        self.modpath = modpath
3536143Snate@binkert.org        self.arcname = joinpath(*arcpath)
3546727Ssteve.reinhardt@amd.com        self.abspath = abspath
3556143Snate@binkert.org        self.compiled = File(self.filename + 'c')
3567674Snate@binkert.org        self.cpp = File(self.filename + '.cc')
3577674Snate@binkert.org        self.symname = PySource.invalid_sym_char.sub('_', modpath)
3585604Snate@binkert.org
3596143Snate@binkert.org        PySource.modules[modpath] = self
3606143Snate@binkert.org        PySource.tnodes[self.tnode] = self
3616143Snate@binkert.org        PySource.symnames[self.symname] = self
3624762Snate@binkert.org
3636143Snate@binkert.orgclass SimObject(PySource):
3644762Snate@binkert.org    '''Add a SimObject python file as a python source object and add
3654762Snate@binkert.org    it to a list of sim object modules'''
3664762Snate@binkert.org
3676143Snate@binkert.org    fixed = False
3686143Snate@binkert.org    modnames = []
3694762Snate@binkert.org
37012302Sgabeblack@google.com    def __init__(self, source, tags=None, add_tags=None):
37112302Sgabeblack@google.com        '''Specify the source file and any tags (automatically in
3728233Snate@binkert.org        the m5.objects package)'''
37312302Sgabeblack@google.com        super(SimObject, self).__init__('m5.objects', source, tags, add_tags)
3746143Snate@binkert.org        if self.fixed:
3756143Snate@binkert.org            raise AttributeError, "Too late to call SimObject now."
3764762Snate@binkert.org
3776143Snate@binkert.org        bisect.insort_right(SimObject.modnames, self.modname)
3784762Snate@binkert.org
3799396Sandreas.hansson@arm.comclass ProtoBuf(SourceFile):
3809396Sandreas.hansson@arm.com    '''Add a Protocol Buffer to build'''
3819396Sandreas.hansson@arm.com
38212302Sgabeblack@google.com    def __init__(self, source, tags=None, add_tags=None):
38312302Sgabeblack@google.com        '''Specify the source file, and any tags'''
38412302Sgabeblack@google.com        super(ProtoBuf, self).__init__(source, tags, add_tags)
3859396Sandreas.hansson@arm.com
3869396Sandreas.hansson@arm.com        # Get the file name and the extension
3879396Sandreas.hansson@arm.com        modname,ext = self.extname
3889396Sandreas.hansson@arm.com        assert ext == 'proto'
3899396Sandreas.hansson@arm.com
3909396Sandreas.hansson@arm.com        # Currently, we stick to generating the C++ headers, so we
3919396Sandreas.hansson@arm.com        # only need to track the source and header.
3929930Sandreas.hansson@arm.com        self.cc_file = File(modname + '.pb.cc')
3939930Sandreas.hansson@arm.com        self.hh_file = File(modname + '.pb.h')
3949396Sandreas.hansson@arm.com
3956143Snate@binkert.org
39612797Sgabeblack@google.comexectuable_classes = []
39712797Sgabeblack@google.comclass ExecutableMeta(type):
39812797Sgabeblack@google.com    '''Meta class for Executables.'''
3998235Snate@binkert.org    all = []
40012797Sgabeblack@google.com
40112797Sgabeblack@google.com    def __init__(cls, name, bases, d):
40212797Sgabeblack@google.com        if not d.pop('abstract', False):
40312797Sgabeblack@google.com            ExecutableMeta.all.append(cls)
40412797Sgabeblack@google.com        super(ExecutableMeta, cls).__init__(name, bases, d)
40512797Sgabeblack@google.com
40612797Sgabeblack@google.com        cls.all = []
40712797Sgabeblack@google.com
40812797Sgabeblack@google.comclass Executable(object):
40912797Sgabeblack@google.com    '''Base class for creating an executable from sources.'''
41012797Sgabeblack@google.com    __metaclass__ = ExecutableMeta
41112797Sgabeblack@google.com
41212797Sgabeblack@google.com    abstract = True
41312797Sgabeblack@google.com
41412797Sgabeblack@google.com    def __init__(self, target, *srcs_and_filts):
41512757Sgabeblack@google.com        '''Specify the target name and any sources. Sources that are
41612757Sgabeblack@google.com        not SourceFiles are evalued with Source().'''
41712797Sgabeblack@google.com        super(Executable, self).__init__()
41812797Sgabeblack@google.com        self.all.append(self)
41912797Sgabeblack@google.com        self.target = target
42012757Sgabeblack@google.com
42112757Sgabeblack@google.com        isFilter = lambda arg: isinstance(arg, SourceFilter)
42212757Sgabeblack@google.com        self.filters = filter(isFilter, srcs_and_filts)
42312757Sgabeblack@google.com        sources = filter(lambda a: not isFilter(a), srcs_and_filts)
4248235Snate@binkert.org
42512302Sgabeblack@google.com        srcs = SourceList()
4268235Snate@binkert.org        for src in sources:
4278235Snate@binkert.org            if not isinstance(src, SourceFile):
42812757Sgabeblack@google.com                src = Source(src, tags=[])
4298235Snate@binkert.org            srcs.append(src)
4308235Snate@binkert.org
4318235Snate@binkert.org        self.sources = srcs
43212757Sgabeblack@google.com        self.dir = Dir('.')
43312313Sgabeblack@google.com
43412797Sgabeblack@google.com    def path(self, env):
43512797Sgabeblack@google.com        return self.dir.File(self.target + '.' + env['EXE_SUFFIX'])
43612797Sgabeblack@google.com
43712797Sgabeblack@google.com    def srcs_to_objs(self, env, sources):
43812797Sgabeblack@google.com        return list([ s.static(env) for s in sources ])
43912797Sgabeblack@google.com
44012797Sgabeblack@google.com    @classmethod
44112797Sgabeblack@google.com    def declare_all(cls, env):
44212797Sgabeblack@google.com        return list([ instance.declare(env) for instance in cls.all ])
44312797Sgabeblack@google.com
44412797Sgabeblack@google.com    def declare(self, env, objs=None):
44512797Sgabeblack@google.com        if objs is None:
44612797Sgabeblack@google.com            objs = self.srcs_to_objs(env, self.sources)
44712797Sgabeblack@google.com
44812797Sgabeblack@google.com        if env['STRIP_EXES']:
44912797Sgabeblack@google.com            stripped = self.path(env)
45012797Sgabeblack@google.com            unstripped = env.File(str(stripped) + '.unstripped')
45112797Sgabeblack@google.com            if sys.platform == 'sunos5':
45212797Sgabeblack@google.com                cmd = 'cp $SOURCE $TARGET; strip $TARGET'
45312797Sgabeblack@google.com            else:
45412797Sgabeblack@google.com                cmd = 'strip $SOURCE -o $TARGET'
45512797Sgabeblack@google.com            env.Program(unstripped, objs)
45612797Sgabeblack@google.com            return env.Command(stripped, unstripped,
45712797Sgabeblack@google.com                               MakeAction(cmd, Transform("STRIP")))
45812797Sgabeblack@google.com        else:
45912797Sgabeblack@google.com            return env.Program(self.path(env), objs)
46012797Sgabeblack@google.com
46112797Sgabeblack@google.comclass UnitTest(Executable):
46212797Sgabeblack@google.com    '''Create a UnitTest'''
46312797Sgabeblack@google.com    def __init__(self, target, *srcs_and_filts, **kwargs):
46412797Sgabeblack@google.com        super(UnitTest, self).__init__(target, *srcs_and_filts)
46512797Sgabeblack@google.com
46612797Sgabeblack@google.com        self.main = kwargs.get('main', False)
46712797Sgabeblack@google.com
46812797Sgabeblack@google.com    def declare(self, env):
46912797Sgabeblack@google.com        sources = list(self.sources)
47012797Sgabeblack@google.com        for f in self.filters:
47112797Sgabeblack@google.com            sources = Source.all.apply_filter(f)
47212797Sgabeblack@google.com        objs = self.srcs_to_objs(env, sources) + env['STATIC_OBJS']
47312797Sgabeblack@google.com        if self.main:
47412797Sgabeblack@google.com            objs += env['MAIN_OBJS']
47512797Sgabeblack@google.com        return super(UnitTest, self).declare(env, objs)
47612797Sgabeblack@google.com
47712797Sgabeblack@google.comclass GTest(Executable):
47812313Sgabeblack@google.com    '''Create a unit test based on the google test framework.'''
47912313Sgabeblack@google.com    all = []
48012797Sgabeblack@google.com    def __init__(self, *srcs_and_filts, **kwargs):
48112797Sgabeblack@google.com        super(GTest, self).__init__(*srcs_and_filts)
48212797Sgabeblack@google.com
48312371Sgabeblack@google.com        self.skip_lib = kwargs.pop('skip_lib', False)
4845584Snate@binkert.org
48512797Sgabeblack@google.com    @classmethod
48612797Sgabeblack@google.com    def declare_all(cls, env):
48712797Sgabeblack@google.com        env = env.Clone()
48812797Sgabeblack@google.com        env.Append(LIBS=env['GTEST_LIBS'])
48912797Sgabeblack@google.com        env.Append(CPPFLAGS=env['GTEST_CPPFLAGS'])
49012797Sgabeblack@google.com        env['GTEST_LIB_SOURCES'] = Source.all.with_tag('gtest lib')
49112797Sgabeblack@google.com        env['GTEST_OUT_DIR'] = \
49212797Sgabeblack@google.com            Dir(env['BUILDDIR']).Dir('unittests.' + env['EXE_SUFFIX'])
49312797Sgabeblack@google.com        return super(GTest, cls).declare_all(env)
49412797Sgabeblack@google.com
49512797Sgabeblack@google.com    def declare(self, env):
49612797Sgabeblack@google.com        sources = list(self.sources)
49712797Sgabeblack@google.com        if not self.skip_lib:
49812797Sgabeblack@google.com            sources += env['GTEST_LIB_SOURCES']
49912797Sgabeblack@google.com        for f in self.filters:
50012797Sgabeblack@google.com            sources += Source.all.apply_filter(f)
50112797Sgabeblack@google.com        objs = self.srcs_to_objs(env, sources)
50212797Sgabeblack@google.com
50312797Sgabeblack@google.com        binary = super(GTest, self).declare(env, objs)
50412797Sgabeblack@google.com
50512797Sgabeblack@google.com        out_dir = env['GTEST_OUT_DIR']
50612797Sgabeblack@google.com        xml_file = out_dir.Dir(str(self.dir)).File(self.target + '.xml')
50712797Sgabeblack@google.com        AlwaysBuild(env.Command(xml_file, binary,
50812797Sgabeblack@google.com            "${SOURCES[0]} --gtest_output=xml:${TARGETS[0]}"))
50912797Sgabeblack@google.com
51012797Sgabeblack@google.com        return binary
51112797Sgabeblack@google.com
51212797Sgabeblack@google.comclass Gem5(Executable):
51312797Sgabeblack@google.com    '''Create a gem5 executable.'''
51412797Sgabeblack@google.com
51512797Sgabeblack@google.com    def __init__(self, target):
51612797Sgabeblack@google.com        super(Gem5, self).__init__(target)
51712797Sgabeblack@google.com
51812797Sgabeblack@google.com    def declare(self, env):
51912797Sgabeblack@google.com        objs = env['MAIN_OBJS'] + env['STATIC_OBJS']
52012797Sgabeblack@google.com        return super(Gem5, self).declare(env, objs)
52112797Sgabeblack@google.com
52212797Sgabeblack@google.com
5234382Sbinkertn@umich.edu# Children should have access
52413576Sciro.santilli@arm.comExport('Blob')
5254202Sbinkertn@umich.eduExport('Source')
5264382Sbinkertn@umich.eduExport('PySource')
5274382Sbinkertn@umich.eduExport('SimObject')
5289396Sandreas.hansson@arm.comExport('ProtoBuf')
52912797Sgabeblack@google.comExport('Executable')
5305584Snate@binkert.orgExport('UnitTest')
53112313Sgabeblack@google.comExport('GTest')
5324382Sbinkertn@umich.edu
5334382Sbinkertn@umich.edu########################################################################
5344382Sbinkertn@umich.edu#
5358232Snate@binkert.org# Debug Flags
5365192Ssaidi@eecs.umich.edu#
5378232Snate@binkert.orgdebug_flags = {}
5388232Snate@binkert.orgdef DebugFlag(name, desc=None):
5398232Snate@binkert.org    if name in debug_flags:
5405192Ssaidi@eecs.umich.edu        raise AttributeError, "Flag %s already specified" % name
5418232Snate@binkert.org    debug_flags[name] = (name, (), desc)
5425192Ssaidi@eecs.umich.edu
5435799Snate@binkert.orgdef CompoundFlag(name, flags, desc=None):
5448232Snate@binkert.org    if name in debug_flags:
5455192Ssaidi@eecs.umich.edu        raise AttributeError, "Flag %s already specified" % name
5465192Ssaidi@eecs.umich.edu
5475192Ssaidi@eecs.umich.edu    compound = tuple(flags)
5488232Snate@binkert.org    debug_flags[name] = (name, compound, desc)
5495192Ssaidi@eecs.umich.edu
5508232Snate@binkert.orgExport('DebugFlag')
5515192Ssaidi@eecs.umich.eduExport('CompoundFlag')
5525192Ssaidi@eecs.umich.edu
5535192Ssaidi@eecs.umich.edu########################################################################
5545192Ssaidi@eecs.umich.edu#
5554382Sbinkertn@umich.edu# Set some compiler variables
5564382Sbinkertn@umich.edu#
5574382Sbinkertn@umich.edu
5582667Sstever@eecs.umich.edu# Include file paths are rooted in this directory.  SCons will
5592667Sstever@eecs.umich.edu# automatically expand '.' to refer to both the source directory and
5602667Sstever@eecs.umich.edu# the corresponding build directory to pick up generated include
5612667Sstever@eecs.umich.edu# files.
5622667Sstever@eecs.umich.eduenv.Append(CPPPATH=Dir('.'))
5632667Sstever@eecs.umich.edu
5645742Snate@binkert.orgfor extra_dir in extras_dir_list:
5655742Snate@binkert.org    env.Append(CPPPATH=Dir(extra_dir))
5665742Snate@binkert.org
5675793Snate@binkert.org# Workaround for bug in SCons version > 0.97d20071212
5688334Snate@binkert.org# Scons bug id: 2006 gem5 Bug id: 308
5695793Snate@binkert.orgfor root, dirs, files in os.walk(base_dir, topdown=True):
5705793Snate@binkert.org    Dir(root[len(base_dir) + 1:])
5715793Snate@binkert.org
5724382Sbinkertn@umich.edu########################################################################
5734762Snate@binkert.org#
5745344Sstever@gmail.com# Walk the tree and execute all SConscripts in subdirectories
5754382Sbinkertn@umich.edu#
5765341Sstever@gmail.com
5775742Snate@binkert.orghere = Dir('.').srcnode().abspath
5785742Snate@binkert.orgfor root, dirs, files in os.walk(base_dir, topdown=True):
5795742Snate@binkert.org    if root == here:
5805742Snate@binkert.org        # we don't want to recurse back into this SConscript
5815742Snate@binkert.org        continue
5824762Snate@binkert.org
5835742Snate@binkert.org    if 'SConscript' in files:
5845742Snate@binkert.org        build_dir = joinpath(env['BUILDDIR'], root[len(base_dir) + 1:])
58511984Sgabeblack@google.com        Source.set_group(build_dir)
5867722Sgblack@eecs.umich.edu        SConscript(joinpath(root, 'SConscript'), variant_dir=build_dir)
5875742Snate@binkert.org
5885742Snate@binkert.orgfor extra_dir in extras_dir_list:
5895742Snate@binkert.org    prefix_len = len(dirname(extra_dir)) + 1
5909930Sandreas.hansson@arm.com
5919930Sandreas.hansson@arm.com    # Also add the corresponding build directory to pick up generated
5929930Sandreas.hansson@arm.com    # include files.
5939930Sandreas.hansson@arm.com    env.Append(CPPPATH=Dir(joinpath(env['BUILDDIR'], extra_dir[prefix_len:])))
5949930Sandreas.hansson@arm.com
5955742Snate@binkert.org    for root, dirs, files in os.walk(extra_dir, topdown=True):
5968242Sbradley.danofsky@amd.com        # if build lives in the extras directory, don't walk down it
5978242Sbradley.danofsky@amd.com        if 'build' in dirs:
5988242Sbradley.danofsky@amd.com            dirs.remove('build')
5998242Sbradley.danofsky@amd.com
6005341Sstever@gmail.com        if 'SConscript' in files:
6015742Snate@binkert.org            build_dir = joinpath(env['BUILDDIR'], root[prefix_len:])
6027722Sgblack@eecs.umich.edu            SConscript(joinpath(root, 'SConscript'), variant_dir=build_dir)
6034773Snate@binkert.org
6046108Snate@binkert.orgfor opt in export_vars:
6051858SN/A    env.ConfigFile(opt)
6061085SN/A
6076658Snate@binkert.orgdef makeTheISA(source, target, env):
6086658Snate@binkert.org    isas = [ src.get_contents() for src in source ]
6097673Snate@binkert.org    target_isa = env['TARGET_ISA']
6106658Snate@binkert.org    def define(isa):
6116658Snate@binkert.org        return isa.upper() + '_ISA'
61211308Santhony.gutierrez@amd.com
6136658Snate@binkert.org    def namespace(isa):
61411308Santhony.gutierrez@amd.com        return isa[0].upper() + isa[1:].lower() + 'ISA'
6156658Snate@binkert.org
6166658Snate@binkert.org
6177673Snate@binkert.org    code = code_formatter()
6187673Snate@binkert.org    code('''\
6197673Snate@binkert.org#ifndef __CONFIG_THE_ISA_HH__
6207673Snate@binkert.org#define __CONFIG_THE_ISA_HH__
6217673Snate@binkert.org
6227673Snate@binkert.org''')
6237673Snate@binkert.org
62410467Sandreas.hansson@arm.com    # create defines for the preprocessing and compile-time determination
6256658Snate@binkert.org    for i,isa in enumerate(isas):
6267673Snate@binkert.org        code('#define $0 $1', define(isa), i + 1)
62710467Sandreas.hansson@arm.com    code()
62810467Sandreas.hansson@arm.com
62910467Sandreas.hansson@arm.com    # create an enum for any run-time determination of the ISA, we
63010467Sandreas.hansson@arm.com    # reuse the same name as the namespaces
63110467Sandreas.hansson@arm.com    code('enum class Arch {')
63210467Sandreas.hansson@arm.com    for i,isa in enumerate(isas):
63310467Sandreas.hansson@arm.com        if i + 1 == len(isas):
63410467Sandreas.hansson@arm.com            code('  $0 = $1', namespace(isa), define(isa))
63510467Sandreas.hansson@arm.com        else:
63610467Sandreas.hansson@arm.com            code('  $0 = $1,', namespace(isa), define(isa))
63710467Sandreas.hansson@arm.com    code('};')
6387673Snate@binkert.org
6397673Snate@binkert.org    code('''
6407673Snate@binkert.org
6417673Snate@binkert.org#define THE_ISA ${{define(target_isa)}}
6427673Snate@binkert.org#define TheISA ${{namespace(target_isa)}}
6439048SAli.Saidi@ARM.com#define THE_ISA_STR "${{target_isa}}"
6447673Snate@binkert.org
6457673Snate@binkert.org#endif // __CONFIG_THE_ISA_HH__''')
6467673Snate@binkert.org
6477673Snate@binkert.org    code.write(str(target[0]))
6486658Snate@binkert.org
6497756SAli.Saidi@ARM.comenv.Command('config/the_isa.hh', map(Value, all_isa_list),
6507816Ssteve.reinhardt@amd.com            MakeAction(makeTheISA, Transform("CFG ISA", 0)))
6516658Snate@binkert.org
65211308Santhony.gutierrez@amd.comdef makeTheGPUISA(source, target, env):
65311308Santhony.gutierrez@amd.com    isas = [ src.get_contents() for src in source ]
65411308Santhony.gutierrez@amd.com    target_gpu_isa = env['TARGET_GPU_ISA']
65511308Santhony.gutierrez@amd.com    def define(isa):
65611308Santhony.gutierrez@amd.com        return isa.upper() + '_ISA'
65711308Santhony.gutierrez@amd.com
65811308Santhony.gutierrez@amd.com    def namespace(isa):
65911308Santhony.gutierrez@amd.com        return isa[0].upper() + isa[1:].lower() + 'ISA'
66011308Santhony.gutierrez@amd.com
66111308Santhony.gutierrez@amd.com
66211308Santhony.gutierrez@amd.com    code = code_formatter()
66311308Santhony.gutierrez@amd.com    code('''\
66411308Santhony.gutierrez@amd.com#ifndef __CONFIG_THE_GPU_ISA_HH__
66511308Santhony.gutierrez@amd.com#define __CONFIG_THE_GPU_ISA_HH__
66611308Santhony.gutierrez@amd.com
66711308Santhony.gutierrez@amd.com''')
66811308Santhony.gutierrez@amd.com
66911308Santhony.gutierrez@amd.com    # create defines for the preprocessing and compile-time determination
67011308Santhony.gutierrez@amd.com    for i,isa in enumerate(isas):
67111308Santhony.gutierrez@amd.com        code('#define $0 $1', define(isa), i + 1)
67211308Santhony.gutierrez@amd.com    code()
67311308Santhony.gutierrez@amd.com
67411308Santhony.gutierrez@amd.com    # create an enum for any run-time determination of the ISA, we
67511308Santhony.gutierrez@amd.com    # reuse the same name as the namespaces
67611308Santhony.gutierrez@amd.com    code('enum class GPUArch {')
67711308Santhony.gutierrez@amd.com    for i,isa in enumerate(isas):
67811308Santhony.gutierrez@amd.com        if i + 1 == len(isas):
67911308Santhony.gutierrez@amd.com            code('  $0 = $1', namespace(isa), define(isa))
68011308Santhony.gutierrez@amd.com        else:
68111308Santhony.gutierrez@amd.com            code('  $0 = $1,', namespace(isa), define(isa))
68211308Santhony.gutierrez@amd.com    code('};')
68311308Santhony.gutierrez@amd.com
68411308Santhony.gutierrez@amd.com    code('''
68511308Santhony.gutierrez@amd.com
68611308Santhony.gutierrez@amd.com#define THE_GPU_ISA ${{define(target_gpu_isa)}}
68711308Santhony.gutierrez@amd.com#define TheGpuISA ${{namespace(target_gpu_isa)}}
68811308Santhony.gutierrez@amd.com#define THE_GPU_ISA_STR "${{target_gpu_isa}}"
68911308Santhony.gutierrez@amd.com
69011308Santhony.gutierrez@amd.com#endif // __CONFIG_THE_GPU_ISA_HH__''')
69111308Santhony.gutierrez@amd.com
69211308Santhony.gutierrez@amd.com    code.write(str(target[0]))
69311308Santhony.gutierrez@amd.com
69411308Santhony.gutierrez@amd.comenv.Command('config/the_gpu_isa.hh', map(Value, all_gpu_isa_list),
69511308Santhony.gutierrez@amd.com            MakeAction(makeTheGPUISA, Transform("CFG ISA", 0)))
69611308Santhony.gutierrez@amd.com
6974382Sbinkertn@umich.edu########################################################################
6984382Sbinkertn@umich.edu#
6994762Snate@binkert.org# Prevent any SimObjects from being added after this point, they
7004762Snate@binkert.org# should all have been added in the SConscripts above
7014762Snate@binkert.org#
7026654Snate@binkert.orgSimObject.fixed = True
7036654Snate@binkert.org
7045517Snate@binkert.orgclass DictImporter(object):
7055517Snate@binkert.org    '''This importer takes a dictionary of arbitrary module names that
7065517Snate@binkert.org    map to arbitrary filenames.'''
7075517Snate@binkert.org    def __init__(self, modules):
7085517Snate@binkert.org        self.modules = modules
7095517Snate@binkert.org        self.installed = set()
7105517Snate@binkert.org
7115517Snate@binkert.org    def __del__(self):
7125517Snate@binkert.org        self.unload()
7135517Snate@binkert.org
7145517Snate@binkert.org    def unload(self):
7155517Snate@binkert.org        import sys
7165517Snate@binkert.org        for module in self.installed:
7175517Snate@binkert.org            del sys.modules[module]
7185517Snate@binkert.org        self.installed = set()
7195517Snate@binkert.org
7205517Snate@binkert.org    def find_module(self, fullname, path):
7216654Snate@binkert.org        if fullname == 'm5.defines':
7225517Snate@binkert.org            return self
7235517Snate@binkert.org
7245517Snate@binkert.org        if fullname == 'm5.objects':
7255517Snate@binkert.org            return self
7265517Snate@binkert.org
72711802Sandreas.sandberg@arm.com        if fullname.startswith('_m5'):
7285517Snate@binkert.org            return None
7295517Snate@binkert.org
7306143Snate@binkert.org        source = self.modules.get(fullname, None)
7316654Snate@binkert.org        if source is not None and fullname.startswith('m5.objects'):
7325517Snate@binkert.org            return self
7335517Snate@binkert.org
7345517Snate@binkert.org        return None
7355517Snate@binkert.org
7365517Snate@binkert.org    def load_module(self, fullname):
7375517Snate@binkert.org        mod = imp.new_module(fullname)
7385517Snate@binkert.org        sys.modules[fullname] = mod
7395517Snate@binkert.org        self.installed.add(fullname)
7405517Snate@binkert.org
7415517Snate@binkert.org        mod.__loader__ = self
7425517Snate@binkert.org        if fullname == 'm5.objects':
7435517Snate@binkert.org            mod.__path__ = fullname.split('.')
7445517Snate@binkert.org            return mod
7455517Snate@binkert.org
7466654Snate@binkert.org        if fullname == 'm5.defines':
7476654Snate@binkert.org            mod.__dict__['buildEnv'] = m5.util.SmartDict(build_env)
7485517Snate@binkert.org            return mod
7495517Snate@binkert.org
7506143Snate@binkert.org        source = self.modules[fullname]
7516143Snate@binkert.org        if source.modname == '__init__':
7526143Snate@binkert.org            mod.__path__ = source.modpath
7536727Ssteve.reinhardt@amd.com        mod.__file__ = source.abspath
7545517Snate@binkert.org
7556727Ssteve.reinhardt@amd.com        exec file(source.abspath, 'r') in mod.__dict__
7565517Snate@binkert.org
7575517Snate@binkert.org        return mod
7585517Snate@binkert.org
7596654Snate@binkert.orgimport m5.SimObject
7606654Snate@binkert.orgimport m5.params
7617673Snate@binkert.orgfrom m5.util import code_formatter
7626654Snate@binkert.org
7636654Snate@binkert.orgm5.SimObject.clear()
7646654Snate@binkert.orgm5.params.clear()
7656654Snate@binkert.org
7665517Snate@binkert.org# install the python importer so we can grab stuff from the source
7675517Snate@binkert.org# tree itself.  We can't have SimObjects added after this point or
7685517Snate@binkert.org# else we won't know about them for the rest of the stuff.
7696143Snate@binkert.orgimporter = DictImporter(PySource.modules)
7705517Snate@binkert.orgsys.meta_path[0:0] = [ importer ]
7714762Snate@binkert.org
7725517Snate@binkert.org# import all sim objects so we can populate the all_objects list
7735517Snate@binkert.org# make sure that we're working with a list, then let's sort it
7746143Snate@binkert.orgfor modname in SimObject.modnames:
7756143Snate@binkert.org    exec('from m5.objects import %s' % modname)
7765517Snate@binkert.org
7775517Snate@binkert.org# we need to unload all of the currently imported modules so that they
7785517Snate@binkert.org# will be re-imported the next time the sconscript is run
7795517Snate@binkert.orgimporter.unload()
7805517Snate@binkert.orgsys.meta_path.remove(importer)
7815517Snate@binkert.org
7825517Snate@binkert.orgsim_objects = m5.SimObject.allClasses
7835517Snate@binkert.orgall_enums = m5.params.allEnums
7845517Snate@binkert.org
7856143Snate@binkert.orgfor name,obj in sorted(sim_objects.iteritems()):
7865517Snate@binkert.org    for param in obj._params.local.values():
7876654Snate@binkert.org        # load the ptype attribute now because it depends on the
7886654Snate@binkert.org        # current version of SimObject.allClasses, but when scons
7896654Snate@binkert.org        # actually uses the value, all versions of
7906654Snate@binkert.org        # SimObject.allClasses will have been loaded
7916654Snate@binkert.org        param.ptype
7926654Snate@binkert.org
7934762Snate@binkert.org########################################################################
7944762Snate@binkert.org#
7954762Snate@binkert.org# calculate extra dependencies
7964762Snate@binkert.org#
7974762Snate@binkert.orgmodule_depends = ["m5", "m5.SimObject", "m5.params"]
7987675Snate@binkert.orgdepends = [ PySource.modules[dep].snode for dep in module_depends ]
79910584Sandreas.hansson@arm.comdepends.sort(key = lambda x: x.name)
8004762Snate@binkert.org
8014762Snate@binkert.org########################################################################
8024762Snate@binkert.org#
8034762Snate@binkert.org# Commands for the basic automatically generated python files
8044382Sbinkertn@umich.edu#
8054382Sbinkertn@umich.edu
8065517Snate@binkert.org# Generate Python file containing a dict specifying the current
8076654Snate@binkert.org# buildEnv flags.
8085517Snate@binkert.orgdef makeDefinesPyFile(target, source, env):
8098126Sgblack@eecs.umich.edu    build_env = source[0].get_contents()
8106654Snate@binkert.org
8117673Snate@binkert.org    code = code_formatter()
8126654Snate@binkert.org    code("""
81311802Sandreas.sandberg@arm.comimport _m5.core
8146654Snate@binkert.orgimport m5.util
8156654Snate@binkert.org
8166654Snate@binkert.orgbuildEnv = m5.util.SmartDict($build_env)
8176654Snate@binkert.org
81811802Sandreas.sandberg@arm.comcompileDate = _m5.core.compileDate
8196669Snate@binkert.org_globals = globals()
82011802Sandreas.sandberg@arm.comfor key,val in _m5.core.__dict__.iteritems():
8216669Snate@binkert.org    if key.startswith('flag_'):
8226669Snate@binkert.org        flag = key[5:]
8236669Snate@binkert.org        _globals[flag] = val
8246669Snate@binkert.orgdel _globals
8256654Snate@binkert.org""")
8267673Snate@binkert.org    code.write(target[0].abspath)
8275517Snate@binkert.org
8288126Sgblack@eecs.umich.edudefines_info = Value(build_env)
8295798Snate@binkert.org# Generate a file with all of the compile options in it
8307756SAli.Saidi@ARM.comenv.Command('python/m5/defines.py', defines_info,
8317816Ssteve.reinhardt@amd.com            MakeAction(makeDefinesPyFile, Transform("DEFINES", 0)))
8325798Snate@binkert.orgPySource('m5', 'python/m5/defines.py')
8335798Snate@binkert.org
8345517Snate@binkert.org# Generate python file containing info about the M5 source code
8355517Snate@binkert.orgdef makeInfoPyFile(target, source, env):
8367673Snate@binkert.org    code = code_formatter()
8375517Snate@binkert.org    for src in source:
8385517Snate@binkert.org        data = ''.join(file(src.srcnode().abspath, 'r').xreadlines())
8397673Snate@binkert.org        code('$src = ${{repr(data)}}')
8407673Snate@binkert.org    code.write(str(target[0]))
8415517Snate@binkert.org
8425798Snate@binkert.org# Generate a file that wraps the basic top level files
8435798Snate@binkert.orgenv.Command('python/m5/info.py',
8448333Snate@binkert.org            [ '#/COPYING', '#/LICENSE', '#/README', ],
8457816Ssteve.reinhardt@amd.com            MakeAction(makeInfoPyFile, Transform("INFO")))
8465798Snate@binkert.orgPySource('m5', 'python/m5/info.py')
8475798Snate@binkert.org
8484762Snate@binkert.org########################################################################
8494762Snate@binkert.org#
8504762Snate@binkert.org# Create all of the SimObject param headers and enum headers
8514762Snate@binkert.org#
8524762Snate@binkert.org
8538596Ssteve.reinhardt@amd.comdef createSimObjectParamStruct(target, source, env):
8545517Snate@binkert.org    assert len(target) == 1 and len(source) == 1
8555517Snate@binkert.org
85611997Sgabeblack@google.com    name = source[0].get_text_contents()
8575517Snate@binkert.org    obj = sim_objects[name]
8585517Snate@binkert.org
8597673Snate@binkert.org    code = code_formatter()
8608596Ssteve.reinhardt@amd.com    obj.cxx_param_decl(code)
8617673Snate@binkert.org    code.write(target[0].abspath)
8625517Snate@binkert.org
86310458Sandreas.hansson@arm.comdef createSimObjectCxxConfig(is_header):
86410458Sandreas.hansson@arm.com    def body(target, source, env):
86510458Sandreas.hansson@arm.com        assert len(target) == 1 and len(source) == 1
86610458Sandreas.hansson@arm.com
86710458Sandreas.hansson@arm.com        name = str(source[0].get_contents())
86810458Sandreas.hansson@arm.com        obj = sim_objects[name]
86910458Sandreas.hansson@arm.com
87010458Sandreas.hansson@arm.com        code = code_formatter()
87110458Sandreas.hansson@arm.com        obj.cxx_config_param_file(code, is_header)
87210458Sandreas.hansson@arm.com        code.write(target[0].abspath)
87310458Sandreas.hansson@arm.com    return body
87410458Sandreas.hansson@arm.com
8755517Snate@binkert.orgdef createEnumStrings(target, source, env):
87611996Sgabeblack@google.com    assert len(target) == 1 and len(source) == 2
8775517Snate@binkert.org
87811997Sgabeblack@google.com    name = source[0].get_text_contents()
87911996Sgabeblack@google.com    use_python = source[1].read()
8805517Snate@binkert.org    obj = all_enums[name]
8815517Snate@binkert.org
8827673Snate@binkert.org    code = code_formatter()
8837673Snate@binkert.org    obj.cxx_def(code)
88411996Sgabeblack@google.com    if use_python:
88511988Sandreas.sandberg@arm.com        obj.pybind_def(code)
8867673Snate@binkert.org    code.write(target[0].abspath)
8875517Snate@binkert.org
8888596Ssteve.reinhardt@amd.comdef createEnumDecls(target, source, env):
8895517Snate@binkert.org    assert len(target) == 1 and len(source) == 1
8905517Snate@binkert.org
89111997Sgabeblack@google.com    name = source[0].get_text_contents()
8925517Snate@binkert.org    obj = all_enums[name]
8935517Snate@binkert.org
8947673Snate@binkert.org    code = code_formatter()
8957673Snate@binkert.org    obj.cxx_decl(code)
8967673Snate@binkert.org    code.write(target[0].abspath)
8975517Snate@binkert.org
89811988Sandreas.sandberg@arm.comdef createSimObjectPyBindWrapper(target, source, env):
89911997Sgabeblack@google.com    name = source[0].get_text_contents()
9008596Ssteve.reinhardt@amd.com    obj = sim_objects[name]
9018596Ssteve.reinhardt@amd.com
9028596Ssteve.reinhardt@amd.com    code = code_formatter()
90311988Sandreas.sandberg@arm.com    obj.pybind_decl(code)
9048596Ssteve.reinhardt@amd.com    code.write(target[0].abspath)
9058596Ssteve.reinhardt@amd.com
9068596Ssteve.reinhardt@amd.com# Generate all of the SimObject param C++ struct header files
9074762Snate@binkert.orgparams_hh_files = []
9086143Snate@binkert.orgfor name,simobj in sorted(sim_objects.iteritems()):
9096143Snate@binkert.org    py_source = PySource.modules[simobj.__module__]
9106143Snate@binkert.org    extra_deps = [ py_source.tnode ]
9114762Snate@binkert.org
9124762Snate@binkert.org    hh_file = File('params/%s.hh' % name)
9134762Snate@binkert.org    params_hh_files.append(hh_file)
9147756SAli.Saidi@ARM.com    env.Command(hh_file, Value(name),
9158596Ssteve.reinhardt@amd.com                MakeAction(createSimObjectParamStruct, Transform("SO PARAM")))
9164762Snate@binkert.org    env.Depends(hh_file, depends + extra_deps)
9174762Snate@binkert.org
91810458Sandreas.hansson@arm.com# C++ parameter description files
91910458Sandreas.hansson@arm.comif GetOption('with_cxx_config'):
92010458Sandreas.hansson@arm.com    for name,simobj in sorted(sim_objects.iteritems()):
92110458Sandreas.hansson@arm.com        py_source = PySource.modules[simobj.__module__]
92210458Sandreas.hansson@arm.com        extra_deps = [ py_source.tnode ]
92310458Sandreas.hansson@arm.com
92410458Sandreas.hansson@arm.com        cxx_config_hh_file = File('cxx_config/%s.hh' % name)
92510458Sandreas.hansson@arm.com        cxx_config_cc_file = File('cxx_config/%s.cc' % name)
92610458Sandreas.hansson@arm.com        env.Command(cxx_config_hh_file, Value(name),
92710458Sandreas.hansson@arm.com                    MakeAction(createSimObjectCxxConfig(True),
92810458Sandreas.hansson@arm.com                    Transform("CXXCPRHH")))
92910458Sandreas.hansson@arm.com        env.Command(cxx_config_cc_file, Value(name),
93010458Sandreas.hansson@arm.com                    MakeAction(createSimObjectCxxConfig(False),
93110458Sandreas.hansson@arm.com                    Transform("CXXCPRCC")))
93210458Sandreas.hansson@arm.com        env.Depends(cxx_config_hh_file, depends + extra_deps +
93310458Sandreas.hansson@arm.com                    [File('params/%s.hh' % name), File('sim/cxx_config.hh')])
93410458Sandreas.hansson@arm.com        env.Depends(cxx_config_cc_file, depends + extra_deps +
93510458Sandreas.hansson@arm.com                    [cxx_config_hh_file])
93610458Sandreas.hansson@arm.com        Source(cxx_config_cc_file)
93710458Sandreas.hansson@arm.com
93810458Sandreas.hansson@arm.com    cxx_config_init_cc_file = File('cxx_config/init.cc')
93910458Sandreas.hansson@arm.com
94010458Sandreas.hansson@arm.com    def createCxxConfigInitCC(target, source, env):
94110458Sandreas.hansson@arm.com        assert len(target) == 1 and len(source) == 1
94210458Sandreas.hansson@arm.com
94310458Sandreas.hansson@arm.com        code = code_formatter()
94410458Sandreas.hansson@arm.com
94510458Sandreas.hansson@arm.com        for name,simobj in sorted(sim_objects.iteritems()):
94610458Sandreas.hansson@arm.com            if not hasattr(simobj, 'abstract') or not simobj.abstract:
94710458Sandreas.hansson@arm.com                code('#include "cxx_config/${name}.hh"')
94810458Sandreas.hansson@arm.com        code()
94910458Sandreas.hansson@arm.com        code('void cxxConfigInit()')
95010458Sandreas.hansson@arm.com        code('{')
95110458Sandreas.hansson@arm.com        code.indent()
95210458Sandreas.hansson@arm.com        for name,simobj in sorted(sim_objects.iteritems()):
95310458Sandreas.hansson@arm.com            not_abstract = not hasattr(simobj, 'abstract') or \
95410458Sandreas.hansson@arm.com                not simobj.abstract
95510458Sandreas.hansson@arm.com            if not_abstract and 'type' in simobj.__dict__:
95610458Sandreas.hansson@arm.com                code('cxx_config_directory["${name}"] = '
95710458Sandreas.hansson@arm.com                     '${name}CxxConfigParams::makeDirectoryEntry();')
95810458Sandreas.hansson@arm.com        code.dedent()
95910458Sandreas.hansson@arm.com        code('}')
96010458Sandreas.hansson@arm.com        code.write(target[0].abspath)
96110458Sandreas.hansson@arm.com
96210458Sandreas.hansson@arm.com    py_source = PySource.modules[simobj.__module__]
96310458Sandreas.hansson@arm.com    extra_deps = [ py_source.tnode ]
96410458Sandreas.hansson@arm.com    env.Command(cxx_config_init_cc_file, Value(name),
96510458Sandreas.hansson@arm.com        MakeAction(createCxxConfigInitCC, Transform("CXXCINIT")))
96610458Sandreas.hansson@arm.com    cxx_param_hh_files = ["cxx_config/%s.hh" % simobj
96710584Sandreas.hansson@arm.com        for name,simobj in sorted(sim_objects.iteritems())
96810458Sandreas.hansson@arm.com        if not hasattr(simobj, 'abstract') or not simobj.abstract]
96910458Sandreas.hansson@arm.com    Depends(cxx_config_init_cc_file, cxx_param_hh_files +
97010458Sandreas.hansson@arm.com            [File('sim/cxx_config.hh')])
97110458Sandreas.hansson@arm.com    Source(cxx_config_init_cc_file)
97210458Sandreas.hansson@arm.com
9734762Snate@binkert.org# Generate all enum header files
9746143Snate@binkert.orgfor name,enum in sorted(all_enums.iteritems()):
9756143Snate@binkert.org    py_source = PySource.modules[enum.__module__]
9766143Snate@binkert.org    extra_deps = [ py_source.tnode ]
9774762Snate@binkert.org
9784762Snate@binkert.org    cc_file = File('enums/%s.cc' % name)
97911996Sgabeblack@google.com    env.Command(cc_file, [Value(name), Value(env['USE_PYTHON'])],
9807816Ssteve.reinhardt@amd.com                MakeAction(createEnumStrings, Transform("ENUM STR")))
9814762Snate@binkert.org    env.Depends(cc_file, depends + extra_deps)
9824762Snate@binkert.org    Source(cc_file)
9834762Snate@binkert.org
9844762Snate@binkert.org    hh_file = File('enums/%s.hh' % name)
9857756SAli.Saidi@ARM.com    env.Command(hh_file, Value(name),
9868596Ssteve.reinhardt@amd.com                MakeAction(createEnumDecls, Transform("ENUMDECL")))
9874762Snate@binkert.org    env.Depends(hh_file, depends + extra_deps)
9884762Snate@binkert.org
98911988Sandreas.sandberg@arm.com# Generate SimObject Python bindings wrapper files
99011988Sandreas.sandberg@arm.comif env['USE_PYTHON']:
99111988Sandreas.sandberg@arm.com    for name,simobj in sorted(sim_objects.iteritems()):
99211988Sandreas.sandberg@arm.com        py_source = PySource.modules[simobj.__module__]
99311988Sandreas.sandberg@arm.com        extra_deps = [ py_source.tnode ]
99411988Sandreas.sandberg@arm.com        cc_file = File('python/_m5/param_%s.cc' % name)
99511988Sandreas.sandberg@arm.com        env.Command(cc_file, Value(name),
99611988Sandreas.sandberg@arm.com                    MakeAction(createSimObjectPyBindWrapper,
99711988Sandreas.sandberg@arm.com                               Transform("SO PyBind")))
99811988Sandreas.sandberg@arm.com        env.Depends(cc_file, depends + extra_deps)
99911988Sandreas.sandberg@arm.com        Source(cc_file)
10004382Sbinkertn@umich.edu
10019396Sandreas.hansson@arm.com# Build all protocol buffers if we have got protoc and protobuf available
10029396Sandreas.hansson@arm.comif env['HAVE_PROTOBUF']:
10039396Sandreas.hansson@arm.com    for proto in ProtoBuf.all:
10049396Sandreas.hansson@arm.com        # Use both the source and header as the target, and the .proto
10059396Sandreas.hansson@arm.com        # file as the source. When executing the protoc compiler, also
10069396Sandreas.hansson@arm.com        # specify the proto_path to avoid having the generated files
10079396Sandreas.hansson@arm.com        # include the path.
10089396Sandreas.hansson@arm.com        env.Command([proto.cc_file, proto.hh_file], proto.tnode,
10099396Sandreas.hansson@arm.com                    MakeAction('$PROTOC --cpp_out ${TARGET.dir} '
10109396Sandreas.hansson@arm.com                               '--proto_path ${SOURCE.dir} $SOURCE',
10119396Sandreas.hansson@arm.com                               Transform("PROTOC")))
10129396Sandreas.hansson@arm.com
10139396Sandreas.hansson@arm.com        # Add the C++ source file
101412302Sgabeblack@google.com        Source(proto.cc_file, tags=proto.tags)
10159396Sandreas.hansson@arm.comelif ProtoBuf.all:
101612563Sgabeblack@google.com    print('Got protobuf to build, but lacks support!')
10179396Sandreas.hansson@arm.com    Exit(1)
10189396Sandreas.hansson@arm.com
10198232Snate@binkert.org#
10208232Snate@binkert.org# Handle debug flags
10218232Snate@binkert.org#
10228232Snate@binkert.orgdef makeDebugFlagCC(target, source, env):
10238232Snate@binkert.org    assert(len(target) == 1 and len(source) == 1)
10246229Snate@binkert.org
102510455SCurtis.Dunham@arm.com    code = code_formatter()
10266229Snate@binkert.org
102710455SCurtis.Dunham@arm.com    # delay definition of CompoundFlags until after all the definition
102810455SCurtis.Dunham@arm.com    # of all constituent SimpleFlags
102910455SCurtis.Dunham@arm.com    comp_code = code_formatter()
10305517Snate@binkert.org
10315517Snate@binkert.org    # file header
10327673Snate@binkert.org    code('''
10335517Snate@binkert.org/*
103410455SCurtis.Dunham@arm.com * DO NOT EDIT THIS FILE! Automatically generated by SCons.
10355517Snate@binkert.org */
10365517Snate@binkert.org
10378232Snate@binkert.org#include "base/debug.hh"
103810455SCurtis.Dunham@arm.com
103910455SCurtis.Dunham@arm.comnamespace Debug {
104010455SCurtis.Dunham@arm.com
10417673Snate@binkert.org''')
10427673Snate@binkert.org
104310455SCurtis.Dunham@arm.com    for name, flag in sorted(source[0].read().iteritems()):
104410455SCurtis.Dunham@arm.com        n, compound, desc = flag
104510455SCurtis.Dunham@arm.com        assert n == name
10465517Snate@binkert.org
104710455SCurtis.Dunham@arm.com        if not compound:
104810455SCurtis.Dunham@arm.com            code('SimpleFlag $name("$name", "$desc");')
104910455SCurtis.Dunham@arm.com        else:
105010455SCurtis.Dunham@arm.com            comp_code('CompoundFlag $name("$name", "$desc",')
105110455SCurtis.Dunham@arm.com            comp_code.indent()
105210455SCurtis.Dunham@arm.com            last = len(compound) - 1
105310455SCurtis.Dunham@arm.com            for i,flag in enumerate(compound):
105410455SCurtis.Dunham@arm.com                if i != last:
105510685Sandreas.hansson@arm.com                    comp_code('&$flag,')
105610455SCurtis.Dunham@arm.com                else:
105710685Sandreas.hansson@arm.com                    comp_code('&$flag);')
105810455SCurtis.Dunham@arm.com            comp_code.dedent()
10595517Snate@binkert.org
106010455SCurtis.Dunham@arm.com    code.append(comp_code)
10618232Snate@binkert.org    code()
10628232Snate@binkert.org    code('} // namespace Debug')
10635517Snate@binkert.org
10647673Snate@binkert.org    code.write(str(target[0]))
10655517Snate@binkert.org
10668232Snate@binkert.orgdef makeDebugFlagHH(target, source, env):
10678232Snate@binkert.org    assert(len(target) == 1 and len(source) == 1)
10685517Snate@binkert.org
10698232Snate@binkert.org    val = eval(source[0].get_contents())
10708232Snate@binkert.org    name, compound, desc = val
10718232Snate@binkert.org
10727673Snate@binkert.org    code = code_formatter()
10735517Snate@binkert.org
10745517Snate@binkert.org    # file header boilerplate
10757673Snate@binkert.org    code('''\
10765517Snate@binkert.org/*
107710455SCurtis.Dunham@arm.com * DO NOT EDIT THIS FILE! Automatically generated by SCons.
10785517Snate@binkert.org */
10795517Snate@binkert.org
10808232Snate@binkert.org#ifndef __DEBUG_${name}_HH__
10818232Snate@binkert.org#define __DEBUG_${name}_HH__
10825517Snate@binkert.org
10838232Snate@binkert.orgnamespace Debug {
10848232Snate@binkert.org''')
10855517Snate@binkert.org
10868232Snate@binkert.org    if compound:
10878232Snate@binkert.org        code('class CompoundFlag;')
10888232Snate@binkert.org    code('class SimpleFlag;')
10895517Snate@binkert.org
10908232Snate@binkert.org    if compound:
10918232Snate@binkert.org        code('extern CompoundFlag $name;')
10928232Snate@binkert.org        for flag in compound:
10938232Snate@binkert.org            code('extern SimpleFlag $flag;')
10948232Snate@binkert.org    else:
10958232Snate@binkert.org        code('extern SimpleFlag $name;')
10965517Snate@binkert.org
10978232Snate@binkert.org    code('''
10988232Snate@binkert.org}
10995517Snate@binkert.org
11008232Snate@binkert.org#endif // __DEBUG_${name}_HH__
11017673Snate@binkert.org''')
11025517Snate@binkert.org
11037673Snate@binkert.org    code.write(str(target[0]))
11045517Snate@binkert.org
11058232Snate@binkert.orgfor name,flag in sorted(debug_flags.iteritems()):
11068232Snate@binkert.org    n, compound, desc = flag
11078232Snate@binkert.org    assert n == name
11085192Ssaidi@eecs.umich.edu
110910454SCurtis.Dunham@arm.com    hh_file = 'debug/%s.hh' % name
111010454SCurtis.Dunham@arm.com    env.Command(hh_file, Value(flag),
11118232Snate@binkert.org                MakeAction(makeDebugFlagHH, Transform("TRACING", 0)))
111210455SCurtis.Dunham@arm.com
111310455SCurtis.Dunham@arm.comenv.Command('debug/flags.cc', Value(debug_flags),
111410455SCurtis.Dunham@arm.com            MakeAction(makeDebugFlagCC, Transform("TRACING", 0)))
111510455SCurtis.Dunham@arm.comSource('debug/flags.cc')
11165192Ssaidi@eecs.umich.edu
111711077SCurtis.Dunham@arm.com# version tags
111811330SCurtis.Dunham@arm.comtags = \
111911077SCurtis.Dunham@arm.comenv.Command('sim/tags.cc', None,
112011077SCurtis.Dunham@arm.com            MakeAction('util/cpt_upgrader.py --get-cc-file > $TARGET',
112111077SCurtis.Dunham@arm.com                       Transform("VER TAGS")))
112211330SCurtis.Dunham@arm.comenv.AlwaysBuild(tags)
112311077SCurtis.Dunham@arm.com
11247674Snate@binkert.org# Embed python files.  All .py files that have been indicated by a
11255522Snate@binkert.org# PySource() call in a SConscript need to be embedded into the M5
11265522Snate@binkert.org# library.  To do that, we compile the file to byte code, marshal the
11277674Snate@binkert.org# byte code, compress it, and then generate a c++ file that
11287674Snate@binkert.org# inserts the result into an array.
11297674Snate@binkert.orgdef embedPyFile(target, source, env):
11307674Snate@binkert.org    def c_str(string):
11317674Snate@binkert.org        if string is None:
11327674Snate@binkert.org            return "0"
11337674Snate@binkert.org        return '"%s"' % string
11347674Snate@binkert.org
11355522Snate@binkert.org    '''Action function to compile a .py into a code object, marshal
11365522Snate@binkert.org    it, compress it, and stick it into an asm file so the code appears
11375522Snate@binkert.org    as just bytes with a label in the data section'''
11385517Snate@binkert.org
11395522Snate@binkert.org    src = file(str(source[0]), 'r').read()
11405517Snate@binkert.org
11416143Snate@binkert.org    pysource = PySource.tnodes[source[0]]
11426727Ssteve.reinhardt@amd.com    compiled = compile(src, pysource.abspath, 'exec')
11435522Snate@binkert.org    marshalled = marshal.dumps(compiled)
11445522Snate@binkert.org    compressed = zlib.compress(marshalled)
11455522Snate@binkert.org    data = compressed
11467674Snate@binkert.org    sym = pysource.symname
11475517Snate@binkert.org
11487673Snate@binkert.org    code = code_formatter()
11497673Snate@binkert.org    code('''\
11507674Snate@binkert.org#include "sim/init.hh"
11517673Snate@binkert.org
11527674Snate@binkert.orgnamespace {
11537674Snate@binkert.org
11547674Snate@binkert.org''')
115513576Sciro.santilli@arm.com    blobToCpp(data, 'data_' + sym, code)
115613576Sciro.santilli@arm.com    code('''\
115711308Santhony.gutierrez@amd.com
11587673Snate@binkert.org
11597674Snate@binkert.orgEmbeddedPython embedded_${sym}(
11607674Snate@binkert.org    ${{c_str(pysource.arcname)}},
11617674Snate@binkert.org    ${{c_str(pysource.abspath)}},
11627674Snate@binkert.org    ${{c_str(pysource.modpath)}},
11637674Snate@binkert.org    data_${sym},
11647674Snate@binkert.org    ${{len(data)}},
11657674Snate@binkert.org    ${{len(marshalled)}});
11667674Snate@binkert.org
11677811Ssteve.reinhardt@amd.com} // anonymous namespace
11687674Snate@binkert.org''')
11697673Snate@binkert.org    code.write(str(target[0]))
11705522Snate@binkert.org
11716143Snate@binkert.orgfor source in PySource.all:
117210453SAndrew.Bardsley@arm.com    env.Command(source.cpp, source.tnode,
11737816Ssteve.reinhardt@amd.com                MakeAction(embedPyFile, Transform("EMBED PY")))
117412302Sgabeblack@google.com    Source(source.cpp, tags=source.tags, add_tags='python')
11754382Sbinkertn@umich.edu
11764382Sbinkertn@umich.edu########################################################################
11774382Sbinkertn@umich.edu#
11784382Sbinkertn@umich.edu# Define binaries.  Each different build type (debug, opt, etc.) gets
11794382Sbinkertn@umich.edu# a slightly different build environment.
11804382Sbinkertn@umich.edu#
11814382Sbinkertn@umich.edu
11824382Sbinkertn@umich.edu# List of constructed environments to pass back to SConstruct
118312302Sgabeblack@google.comdate_source = Source('base/date.cc', tags=[])
11844382Sbinkertn@umich.edu
118512797Sgabeblack@google.comgem5_binary = Gem5('gem5')
118612797Sgabeblack@google.com
11872655Sstever@eecs.umich.edu# Function to create a new build environment as clone of current
11882655Sstever@eecs.umich.edu# environment 'env' with modified object suffix and optional stripped
11892655Sstever@eecs.umich.edu# binary.  Additional keyword arguments are appended to corresponding
11902655Sstever@eecs.umich.edu# build environment vars.
119112063Sgabeblack@google.comdef makeEnv(env, label, objsfx, strip=False, disable_partial=False, **kwargs):
11925601Snate@binkert.org    # SCons doesn't know to append a library suffix when there is a '.' in the
11935601Snate@binkert.org    # name.  Use '_' instead.
119412222Sgabeblack@google.com    libname = 'gem5_' + label
119512222Sgabeblack@google.com    secondary_exename = 'm5.' + label
11965522Snate@binkert.org
11975863Snate@binkert.org    new_env = env.Clone(OBJSUFFIX=objsfx, SHOBJSUFFIX=objsfx + 's')
11985601Snate@binkert.org    new_env.Label = label
11995601Snate@binkert.org    new_env.Append(**kwargs)
12005601Snate@binkert.org
120112302Sgabeblack@google.com    lib_sources = Source.all.with_tag('gem5 lib')
120210453SAndrew.Bardsley@arm.com
120311988Sandreas.sandberg@arm.com    # Without Python, leave out all Python content from the library
120411988Sandreas.sandberg@arm.com    # builds.  The option doesn't affect gem5 built as a program
120510453SAndrew.Bardsley@arm.com    if GetOption('without_python'):
120612302Sgabeblack@google.com        lib_sources = lib_sources.without_tag('python')
120710453SAndrew.Bardsley@arm.com
120811983Sgabeblack@google.com    static_objs = []
120911983Sgabeblack@google.com    shared_objs = []
121012302Sgabeblack@google.com
121112302Sgabeblack@google.com    for s in lib_sources.with_tag(Source.ungrouped_tag):
121212362Sgabeblack@google.com        static_objs.append(s.static(new_env))
121312362Sgabeblack@google.com        shared_objs.append(s.shared(new_env))
121411983Sgabeblack@google.com
121512302Sgabeblack@google.com    for group in Source.source_groups:
121612302Sgabeblack@google.com        srcs = lib_sources.with_tag(Source.link_group_tag(group))
121711983Sgabeblack@google.com        if not srcs:
121811983Sgabeblack@google.com            continue
121911983Sgabeblack@google.com
122012362Sgabeblack@google.com        group_static = [ s.static(new_env) for s in srcs ]
122112362Sgabeblack@google.com        group_shared = [ s.shared(new_env) for s in srcs ]
122212310Sgabeblack@google.com
122312063Sgabeblack@google.com        # If partial linking is disabled, add these sources to the build
122412063Sgabeblack@google.com        # directly, and short circuit this loop.
122512063Sgabeblack@google.com        if disable_partial:
122612310Sgabeblack@google.com            static_objs.extend(group_static)
122712310Sgabeblack@google.com            shared_objs.extend(group_shared)
122812063Sgabeblack@google.com            continue
122912063Sgabeblack@google.com
123011983Sgabeblack@google.com        # Set up the static partially linked objects.
123111983Sgabeblack@google.com        file_name = new_env.subst("${OBJPREFIX}lib${OBJSUFFIX}.partial")
123211983Sgabeblack@google.com        target = File(joinpath(group, file_name))
123312310Sgabeblack@google.com        partial = env.PartialStatic(target=target, source=group_static)
123412310Sgabeblack@google.com        static_objs.extend(partial)
123511983Sgabeblack@google.com
123611983Sgabeblack@google.com        # Set up the shared partially linked objects.
123711983Sgabeblack@google.com        file_name = new_env.subst("${SHOBJPREFIX}lib${SHOBJSUFFIX}.partial")
123811983Sgabeblack@google.com        target = File(joinpath(group, file_name))
123912310Sgabeblack@google.com        partial = env.PartialShared(target=target, source=group_shared)
124012310Sgabeblack@google.com        shared_objs.extend(partial)
12416143Snate@binkert.org
124212362Sgabeblack@google.com    static_date = date_source.static(new_env)
124312306Sgabeblack@google.com    new_env.Depends(static_date, static_objs)
124412310Sgabeblack@google.com    static_objs.extend(static_date)
124510453SAndrew.Bardsley@arm.com
124612362Sgabeblack@google.com    shared_date = date_source.shared(new_env)
124712306Sgabeblack@google.com    new_env.Depends(shared_date, shared_objs)
124812310Sgabeblack@google.com    shared_objs.extend(shared_date)
12495554Snate@binkert.org
125012797Sgabeblack@google.com    main_objs = [ s.static(new_env) for s in Source.all.with_tag('main') ]
125112797Sgabeblack@google.com
12525522Snate@binkert.org    # First make a library of everything but main() so other programs can
12535522Snate@binkert.org    # link against m5.
12545797Snate@binkert.org    static_lib = new_env.StaticLibrary(libname, static_objs)
12555797Snate@binkert.org    shared_lib = new_env.SharedLibrary(libname, shared_objs)
12565522Snate@binkert.org
125712797Sgabeblack@google.com    # Keep track of the object files generated so far so Executables can
125812797Sgabeblack@google.com    # include them.
125912797Sgabeblack@google.com    new_env['STATIC_OBJS'] = static_objs
126012797Sgabeblack@google.com    new_env['SHARED_OBJS'] = shared_objs
126112797Sgabeblack@google.com    new_env['MAIN_OBJS'] = main_objs
12628233Snate@binkert.org
126312797Sgabeblack@google.com    new_env['STATIC_LIB'] = static_lib
126412797Sgabeblack@google.com    new_env['SHARED_LIB'] = shared_lib
12658235Snate@binkert.org
126612797Sgabeblack@google.com    # Record some settings for building Executables.
126712797Sgabeblack@google.com    new_env['EXE_SUFFIX'] = label
126812797Sgabeblack@google.com    new_env['STRIP_EXES'] = strip
126912370Sgabeblack@google.com
127012797Sgabeblack@google.com    for cls in ExecutableMeta.all:
127112797Sgabeblack@google.com        cls.declare_all(new_env)
127212313Sgabeblack@google.com
127312797Sgabeblack@google.com    new_env.M5Binary = File(gem5_binary.path(new_env))
12746143Snate@binkert.org
127512797Sgabeblack@google.com    new_env.Command(secondary_exename, new_env.M5Binary,
12768334Snate@binkert.org            MakeAction('ln $SOURCE $TARGET', Transform("HARDLINK")))
12778334Snate@binkert.org
127811993Sgabeblack@google.com    # Set up regression tests.
127911993Sgabeblack@google.com    SConscript(os.path.join(env.root.abspath, 'tests', 'SConscript'),
128012223Sgabeblack@google.com               variant_dir=Dir('tests').Dir(new_env.Label),
128111993Sgabeblack@google.com               exports={ 'env' : new_env }, duplicate=False)
12822655Sstever@eecs.umich.edu
12839225Sandreas.hansson@arm.com# Start out with the compiler flags common to all compilers,
12849225Sandreas.hansson@arm.com# i.e. they all use -g for opt and -g -pg for prof
12859226Sandreas.hansson@arm.comccflags = {'debug' : [], 'opt' : ['-g'], 'fast' : [], 'prof' : ['-g', '-pg'],
12869226Sandreas.hansson@arm.com           'perf' : ['-g']}
12879225Sandreas.hansson@arm.com
12889226Sandreas.hansson@arm.com# Start out with the linker flags common to all linkers, i.e. -pg for
12899226Sandreas.hansson@arm.com# prof, and -lprofiler for perf. The -lprofile flag is surrounded by
12909226Sandreas.hansson@arm.com# no-as-needed and as-needed as the binutils linker is too clever and
12919226Sandreas.hansson@arm.com# simply doesn't link to the library otherwise.
12929226Sandreas.hansson@arm.comldflags = {'debug' : [], 'opt' : [], 'fast' : [], 'prof' : ['-pg'],
12939226Sandreas.hansson@arm.com           'perf' : ['-Wl,--no-as-needed', '-lprofiler', '-Wl,--as-needed']}
12949225Sandreas.hansson@arm.com
12959227Sandreas.hansson@arm.com# For Link Time Optimization, the optimisation flags used to compile
12969227Sandreas.hansson@arm.com# individual files are decoupled from those used at link time
12979227Sandreas.hansson@arm.com# (i.e. you can compile with -O3 and perform LTO with -O0), so we need
12989227Sandreas.hansson@arm.com# to also update the linker flags based on the target.
12998946Sandreas.hansson@arm.comif env['GCC']:
13003918Ssaidi@eecs.umich.edu    if sys.platform == 'sunos5':
13019225Sandreas.hansson@arm.com        ccflags['debug'] += ['-gstabs+']
13023918Ssaidi@eecs.umich.edu    else:
13039225Sandreas.hansson@arm.com        ccflags['debug'] += ['-ggdb3']
13049225Sandreas.hansson@arm.com    ldflags['debug'] += ['-O0']
13059227Sandreas.hansson@arm.com    # opt, fast, prof and perf all share the same cc flags, also add
13069227Sandreas.hansson@arm.com    # the optimization to the ldflags as LTO defers the optimization
13079227Sandreas.hansson@arm.com    # to link time
13089226Sandreas.hansson@arm.com    for target in ['opt', 'fast', 'prof', 'perf']:
13099225Sandreas.hansson@arm.com        ccflags[target] += ['-O3']
13109227Sandreas.hansson@arm.com        ldflags[target] += ['-O3']
13119227Sandreas.hansson@arm.com
13129227Sandreas.hansson@arm.com    ccflags['fast'] += env['LTO_CCFLAGS']
13139227Sandreas.hansson@arm.com    ldflags['fast'] += env['LTO_LDFLAGS']
13148946Sandreas.hansson@arm.comelif env['CLANG']:
13159225Sandreas.hansson@arm.com    ccflags['debug'] += ['-g', '-O0']
13169226Sandreas.hansson@arm.com    # opt, fast, prof and perf all share the same cc flags
13179226Sandreas.hansson@arm.com    for target in ['opt', 'fast', 'prof', 'perf']:
13189226Sandreas.hansson@arm.com        ccflags[target] += ['-O3']
13193515Ssaidi@eecs.umich.eduelse:
132012563Sgabeblack@google.com    print('Unknown compiler, please fix compiler options')
13214762Snate@binkert.org    Exit(1)
13223515Ssaidi@eecs.umich.edu
13238881Smarc.orr@gmail.com
13248881Smarc.orr@gmail.com# To speed things up, we only instantiate the build environments we
13258881Smarc.orr@gmail.com# need.  We try to identify the needed environment for each target; if
13268881Smarc.orr@gmail.com# we can't, we fall back on instantiating all the environments just to
13278881Smarc.orr@gmail.com# be safe.
13289226Sandreas.hansson@arm.comtarget_types = ['debug', 'opt', 'fast', 'prof', 'perf']
13299226Sandreas.hansson@arm.comobj2target = {'do': 'debug', 'o': 'opt', 'fo': 'fast', 'po': 'prof',
13309226Sandreas.hansson@arm.com              'gpo' : 'perf'}
13318881Smarc.orr@gmail.com
13328881Smarc.orr@gmail.comdef identifyTarget(t):
13338881Smarc.orr@gmail.com    ext = t.split('.')[-1]
13348881Smarc.orr@gmail.com    if ext in target_types:
13358881Smarc.orr@gmail.com        return ext
13368881Smarc.orr@gmail.com    if obj2target.has_key(ext):
13378881Smarc.orr@gmail.com        return obj2target[ext]
13388881Smarc.orr@gmail.com    match = re.search(r'/tests/([^/]+)/', t)
13398881Smarc.orr@gmail.com    if match and match.group(1) in target_types:
13408881Smarc.orr@gmail.com        return match.group(1)
13418881Smarc.orr@gmail.com    return 'all'
13428881Smarc.orr@gmail.com
13438881Smarc.orr@gmail.comneeded_envs = [identifyTarget(target) for target in BUILD_TARGETS]
13448881Smarc.orr@gmail.comif 'all' in needed_envs:
13458881Smarc.orr@gmail.com    needed_envs += target_types
13468881Smarc.orr@gmail.com
134713508Snikos.nikoleris@arm.comdisable_partial = False
134813508Snikos.nikoleris@arm.comif env['PLATFORM'] == 'darwin':
134913508Snikos.nikoleris@arm.com    # Up until Apple LLVM version 10.0.0 (clang-1000.11.45.5), partial
135013508Snikos.nikoleris@arm.com    # linked objects do not expose symbols that are marked with the
135113508Snikos.nikoleris@arm.com    # hidden visibility and consequently building gem5 on Mac OS
135213508Snikos.nikoleris@arm.com    # fails. As a workaround, we disable partial linking, however, we
135313508Snikos.nikoleris@arm.com    # may want to revisit in the future.
135413508Snikos.nikoleris@arm.com    disable_partial = True
135513508Snikos.nikoleris@arm.com
135612222Sgabeblack@google.com# Debug binary
135712222Sgabeblack@google.comif 'debug' in needed_envs:
135812222Sgabeblack@google.com    makeEnv(env, 'debug', '.do',
135912222Sgabeblack@google.com            CCFLAGS = Split(ccflags['debug']),
136012222Sgabeblack@google.com            CPPDEFINES = ['DEBUG', 'TRACING_ON=1'],
136113508Snikos.nikoleris@arm.com            LINKFLAGS = Split(ldflags['debug']),
136213508Snikos.nikoleris@arm.com            disable_partial=disable_partial)
1363955SN/A
136412222Sgabeblack@google.com# Optimized binary
136512222Sgabeblack@google.comif 'opt' in needed_envs:
136612222Sgabeblack@google.com    makeEnv(env, 'opt', '.o',
136712222Sgabeblack@google.com            CCFLAGS = Split(ccflags['opt']),
136812222Sgabeblack@google.com            CPPDEFINES = ['TRACING_ON=1'],
136913508Snikos.nikoleris@arm.com            LINKFLAGS = Split(ldflags['opt']),
137013508Snikos.nikoleris@arm.com            disable_partial=disable_partial)
1371955SN/A
137212222Sgabeblack@google.com# "Fast" binary
137312222Sgabeblack@google.comif 'fast' in needed_envs:
137413508Snikos.nikoleris@arm.com    disable_partial = disable_partial and \
137512222Sgabeblack@google.com            env.get('BROKEN_INCREMENTAL_LTO', False) and \
137612222Sgabeblack@google.com            GetOption('force_lto')
137712222Sgabeblack@google.com    makeEnv(env, 'fast', '.fo', strip = True,
137812222Sgabeblack@google.com            CCFLAGS = Split(ccflags['fast']),
137912222Sgabeblack@google.com            CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
138012222Sgabeblack@google.com            LINKFLAGS = Split(ldflags['fast']),
138112222Sgabeblack@google.com            disable_partial=disable_partial)
13821869SN/A
138312222Sgabeblack@google.com# Profiled binary using gprof
138412222Sgabeblack@google.comif 'prof' in needed_envs:
138512222Sgabeblack@google.com    makeEnv(env, 'prof', '.po',
138612222Sgabeblack@google.com            CCFLAGS = Split(ccflags['prof']),
138712222Sgabeblack@google.com            CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
138813508Snikos.nikoleris@arm.com            LINKFLAGS = Split(ldflags['prof']),
138913508Snikos.nikoleris@arm.com            disable_partial=disable_partial)
13909226Sandreas.hansson@arm.com
139112222Sgabeblack@google.com# Profiled binary using google-pprof
139212222Sgabeblack@google.comif 'perf' in needed_envs:
139312222Sgabeblack@google.com    makeEnv(env, 'perf', '.gpo',
139412222Sgabeblack@google.com            CCFLAGS = Split(ccflags['perf']),
139512222Sgabeblack@google.com            CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
139613508Snikos.nikoleris@arm.com            LINKFLAGS = Split(ldflags['perf']),
139713508Snikos.nikoleris@arm.com            disable_partial=disable_partial)
1398