SConscript revision 13630
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))
25813630Sciro.santilli@arm.com    if hpp_code is not None:
25913630Sciro.santilli@arm.com        cpp_code(symbol_len_declaration + ' = {};'.format(len(data)))
26013576Sciro.santilli@arm.com    cpp_code(symbol_declaration + ' = {')
26113576Sciro.santilli@arm.com    cpp_code.indent()
26213576Sciro.santilli@arm.com    step = 16
26313576Sciro.santilli@arm.com    for i in xrange(0, len(data), step):
26413576Sciro.santilli@arm.com        x = array.array('B', data[i:i+step])
26513576Sciro.santilli@arm.com        cpp_code(''.join('%d,' % d for d in x))
26613576Sciro.santilli@arm.com    cpp_code.dedent()
26713576Sciro.santilli@arm.com    cpp_code('};')
26813576Sciro.santilli@arm.com    if namespace is not None:
26913576Sciro.santilli@arm.com        cpp_code('}')
27013576Sciro.santilli@arm.com
27113576Sciro.santilli@arm.comdef Blob(blob_path, symbol):
27213576Sciro.santilli@arm.com    '''
27313576Sciro.santilli@arm.com    Embed an arbitrary blob into the gem5 executable,
27413576Sciro.santilli@arm.com    and make it accessible to C++ as a byte array.
27513576Sciro.santilli@arm.com    '''
27613576Sciro.santilli@arm.com    blob_path = os.path.abspath(blob_path)
27713576Sciro.santilli@arm.com    blob_out_dir = os.path.join(env['BUILDDIR'], 'blobs')
27813576Sciro.santilli@arm.com    path_noext = joinpath(blob_out_dir, symbol)
27913576Sciro.santilli@arm.com    cpp_path = path_noext + '.cc'
28013576Sciro.santilli@arm.com    hpp_path = path_noext + '.hh'
28113576Sciro.santilli@arm.com    def embedBlob(target, source, env):
28213576Sciro.santilli@arm.com        data = file(str(source[0]), 'r').read()
28313576Sciro.santilli@arm.com        cpp_code = code_formatter()
28413576Sciro.santilli@arm.com        hpp_code = code_formatter()
28513576Sciro.santilli@arm.com        blobToCpp(data, symbol, cpp_code, hpp_code, namespace='Blobs')
28613576Sciro.santilli@arm.com        cpp_path = str(target[0])
28713576Sciro.santilli@arm.com        hpp_path = str(target[1])
28813576Sciro.santilli@arm.com        cpp_dir = os.path.split(cpp_path)[0]
28913576Sciro.santilli@arm.com        if not os.path.exists(cpp_dir):
29013576Sciro.santilli@arm.com            os.makedirs(cpp_dir)
29113576Sciro.santilli@arm.com        cpp_code.write(cpp_path)
29213576Sciro.santilli@arm.com        hpp_code.write(hpp_path)
29313576Sciro.santilli@arm.com    env.Command([cpp_path, hpp_path], blob_path,
29413576Sciro.santilli@arm.com                MakeAction(embedBlob, Transform("EMBED BLOB")))
29513576Sciro.santilli@arm.com    Source(cpp_path)
29613576Sciro.santilli@arm.com
29713577Sciro.santilli@arm.comdef GdbXml(xml_id, symbol):
29813577Sciro.santilli@arm.com    Blob(joinpath(gdb_xml_dir, xml_id), symbol)
29913577Sciro.santilli@arm.com
3006143Snate@binkert.orgclass Source(SourceFile):
30112302Sgabeblack@google.com    ungrouped_tag = 'No link group'
30212302Sgabeblack@google.com    source_groups = set()
30312302Sgabeblack@google.com
30412302Sgabeblack@google.com    _current_group_tag = ungrouped_tag
30512302Sgabeblack@google.com
30612302Sgabeblack@google.com    @staticmethod
30712302Sgabeblack@google.com    def link_group_tag(group):
30812302Sgabeblack@google.com        return 'link group: %s' % group
30911983Sgabeblack@google.com
31011983Sgabeblack@google.com    @classmethod
31111983Sgabeblack@google.com    def set_group(cls, group):
31212302Sgabeblack@google.com        new_tag = Source.link_group_tag(group)
31312302Sgabeblack@google.com        Source._current_group_tag = new_tag
31412302Sgabeblack@google.com        Source.source_groups.add(group)
31512302Sgabeblack@google.com
31612302Sgabeblack@google.com    def _add_link_group_tag(self):
31712302Sgabeblack@google.com        self.tags.add(Source._current_group_tag)
31811983Sgabeblack@google.com
3196143Snate@binkert.org    '''Add a c/c++ source file to the build'''
32012305Sgabeblack@google.com    def __init__(self, source, tags=None, add_tags=None):
32112302Sgabeblack@google.com        '''specify the source file, and any tags'''
32212302Sgabeblack@google.com        super(Source, self).__init__(source, tags, add_tags)
32312302Sgabeblack@google.com        self._add_link_group_tag()
3246143Snate@binkert.org
3256143Snate@binkert.orgclass PySource(SourceFile):
3266143Snate@binkert.org    '''Add a python source file to the named package'''
3275522Snate@binkert.org    invalid_sym_char = re.compile('[^A-z0-9_]')
3286143Snate@binkert.org    modules = {}
3296143Snate@binkert.org    tnodes = {}
3306143Snate@binkert.org    symnames = {}
3319982Satgutier@umich.edu
33212302Sgabeblack@google.com    def __init__(self, package, source, tags=None, add_tags=None):
33312302Sgabeblack@google.com        '''specify the python package, the source file, and any tags'''
33412302Sgabeblack@google.com        super(PySource, self).__init__(source, tags, add_tags)
3356143Snate@binkert.org
3366143Snate@binkert.org        modname,ext = self.extname
3376143Snate@binkert.org        assert ext == 'py'
3386143Snate@binkert.org
3395522Snate@binkert.org        if package:
3405522Snate@binkert.org            path = package.split('.')
3415522Snate@binkert.org        else:
3425522Snate@binkert.org            path = []
3435604Snate@binkert.org
3445604Snate@binkert.org        modpath = path[:]
3456143Snate@binkert.org        if modname != '__init__':
3466143Snate@binkert.org            modpath += [ modname ]
3474762Snate@binkert.org        modpath = '.'.join(modpath)
3484762Snate@binkert.org
3496143Snate@binkert.org        arcpath = path + [ self.basename ]
3506727Ssteve.reinhardt@amd.com        abspath = self.snode.abspath
3516727Ssteve.reinhardt@amd.com        if not exists(abspath):
3526727Ssteve.reinhardt@amd.com            abspath = self.tnode.abspath
3534762Snate@binkert.org
3546143Snate@binkert.org        self.package = package
3556143Snate@binkert.org        self.modname = modname
3566143Snate@binkert.org        self.modpath = modpath
3576143Snate@binkert.org        self.arcname = joinpath(*arcpath)
3586727Ssteve.reinhardt@amd.com        self.abspath = abspath
3596143Snate@binkert.org        self.compiled = File(self.filename + 'c')
3607674Snate@binkert.org        self.cpp = File(self.filename + '.cc')
3617674Snate@binkert.org        self.symname = PySource.invalid_sym_char.sub('_', modpath)
3625604Snate@binkert.org
3636143Snate@binkert.org        PySource.modules[modpath] = self
3646143Snate@binkert.org        PySource.tnodes[self.tnode] = self
3656143Snate@binkert.org        PySource.symnames[self.symname] = self
3664762Snate@binkert.org
3676143Snate@binkert.orgclass SimObject(PySource):
3684762Snate@binkert.org    '''Add a SimObject python file as a python source object and add
3694762Snate@binkert.org    it to a list of sim object modules'''
3704762Snate@binkert.org
3716143Snate@binkert.org    fixed = False
3726143Snate@binkert.org    modnames = []
3734762Snate@binkert.org
37412302Sgabeblack@google.com    def __init__(self, source, tags=None, add_tags=None):
37512302Sgabeblack@google.com        '''Specify the source file and any tags (automatically in
3768233Snate@binkert.org        the m5.objects package)'''
37712302Sgabeblack@google.com        super(SimObject, self).__init__('m5.objects', source, tags, add_tags)
3786143Snate@binkert.org        if self.fixed:
3796143Snate@binkert.org            raise AttributeError, "Too late to call SimObject now."
3804762Snate@binkert.org
3816143Snate@binkert.org        bisect.insort_right(SimObject.modnames, self.modname)
3824762Snate@binkert.org
3839396Sandreas.hansson@arm.comclass ProtoBuf(SourceFile):
3849396Sandreas.hansson@arm.com    '''Add a Protocol Buffer to build'''
3859396Sandreas.hansson@arm.com
38612302Sgabeblack@google.com    def __init__(self, source, tags=None, add_tags=None):
38712302Sgabeblack@google.com        '''Specify the source file, and any tags'''
38812302Sgabeblack@google.com        super(ProtoBuf, self).__init__(source, tags, add_tags)
3899396Sandreas.hansson@arm.com
3909396Sandreas.hansson@arm.com        # Get the file name and the extension
3919396Sandreas.hansson@arm.com        modname,ext = self.extname
3929396Sandreas.hansson@arm.com        assert ext == 'proto'
3939396Sandreas.hansson@arm.com
3949396Sandreas.hansson@arm.com        # Currently, we stick to generating the C++ headers, so we
3959396Sandreas.hansson@arm.com        # only need to track the source and header.
3969930Sandreas.hansson@arm.com        self.cc_file = File(modname + '.pb.cc')
3979930Sandreas.hansson@arm.com        self.hh_file = File(modname + '.pb.h')
3989396Sandreas.hansson@arm.com
3996143Snate@binkert.org
40012797Sgabeblack@google.comexectuable_classes = []
40112797Sgabeblack@google.comclass ExecutableMeta(type):
40212797Sgabeblack@google.com    '''Meta class for Executables.'''
4038235Snate@binkert.org    all = []
40412797Sgabeblack@google.com
40512797Sgabeblack@google.com    def __init__(cls, name, bases, d):
40612797Sgabeblack@google.com        if not d.pop('abstract', False):
40712797Sgabeblack@google.com            ExecutableMeta.all.append(cls)
40812797Sgabeblack@google.com        super(ExecutableMeta, cls).__init__(name, bases, d)
40912797Sgabeblack@google.com
41012797Sgabeblack@google.com        cls.all = []
41112797Sgabeblack@google.com
41212797Sgabeblack@google.comclass Executable(object):
41312797Sgabeblack@google.com    '''Base class for creating an executable from sources.'''
41412797Sgabeblack@google.com    __metaclass__ = ExecutableMeta
41512797Sgabeblack@google.com
41612797Sgabeblack@google.com    abstract = True
41712797Sgabeblack@google.com
41812797Sgabeblack@google.com    def __init__(self, target, *srcs_and_filts):
41912757Sgabeblack@google.com        '''Specify the target name and any sources. Sources that are
42012757Sgabeblack@google.com        not SourceFiles are evalued with Source().'''
42112797Sgabeblack@google.com        super(Executable, self).__init__()
42212797Sgabeblack@google.com        self.all.append(self)
42312797Sgabeblack@google.com        self.target = target
42412757Sgabeblack@google.com
42512757Sgabeblack@google.com        isFilter = lambda arg: isinstance(arg, SourceFilter)
42612757Sgabeblack@google.com        self.filters = filter(isFilter, srcs_and_filts)
42712757Sgabeblack@google.com        sources = filter(lambda a: not isFilter(a), srcs_and_filts)
4288235Snate@binkert.org
42912302Sgabeblack@google.com        srcs = SourceList()
4308235Snate@binkert.org        for src in sources:
4318235Snate@binkert.org            if not isinstance(src, SourceFile):
43212757Sgabeblack@google.com                src = Source(src, tags=[])
4338235Snate@binkert.org            srcs.append(src)
4348235Snate@binkert.org
4358235Snate@binkert.org        self.sources = srcs
43612757Sgabeblack@google.com        self.dir = Dir('.')
43712313Sgabeblack@google.com
43812797Sgabeblack@google.com    def path(self, env):
43912797Sgabeblack@google.com        return self.dir.File(self.target + '.' + env['EXE_SUFFIX'])
44012797Sgabeblack@google.com
44112797Sgabeblack@google.com    def srcs_to_objs(self, env, sources):
44212797Sgabeblack@google.com        return list([ s.static(env) for s in sources ])
44312797Sgabeblack@google.com
44412797Sgabeblack@google.com    @classmethod
44512797Sgabeblack@google.com    def declare_all(cls, env):
44612797Sgabeblack@google.com        return list([ instance.declare(env) for instance in cls.all ])
44712797Sgabeblack@google.com
44812797Sgabeblack@google.com    def declare(self, env, objs=None):
44912797Sgabeblack@google.com        if objs is None:
45012797Sgabeblack@google.com            objs = self.srcs_to_objs(env, self.sources)
45112797Sgabeblack@google.com
45212797Sgabeblack@google.com        if env['STRIP_EXES']:
45312797Sgabeblack@google.com            stripped = self.path(env)
45412797Sgabeblack@google.com            unstripped = env.File(str(stripped) + '.unstripped')
45512797Sgabeblack@google.com            if sys.platform == 'sunos5':
45612797Sgabeblack@google.com                cmd = 'cp $SOURCE $TARGET; strip $TARGET'
45712797Sgabeblack@google.com            else:
45812797Sgabeblack@google.com                cmd = 'strip $SOURCE -o $TARGET'
45912797Sgabeblack@google.com            env.Program(unstripped, objs)
46012797Sgabeblack@google.com            return env.Command(stripped, unstripped,
46112797Sgabeblack@google.com                               MakeAction(cmd, Transform("STRIP")))
46212797Sgabeblack@google.com        else:
46312797Sgabeblack@google.com            return env.Program(self.path(env), objs)
46412797Sgabeblack@google.com
46512797Sgabeblack@google.comclass UnitTest(Executable):
46612797Sgabeblack@google.com    '''Create a UnitTest'''
46712797Sgabeblack@google.com    def __init__(self, target, *srcs_and_filts, **kwargs):
46812797Sgabeblack@google.com        super(UnitTest, self).__init__(target, *srcs_and_filts)
46912797Sgabeblack@google.com
47012797Sgabeblack@google.com        self.main = kwargs.get('main', False)
47112797Sgabeblack@google.com
47212797Sgabeblack@google.com    def declare(self, env):
47312797Sgabeblack@google.com        sources = list(self.sources)
47412797Sgabeblack@google.com        for f in self.filters:
47512797Sgabeblack@google.com            sources = Source.all.apply_filter(f)
47612797Sgabeblack@google.com        objs = self.srcs_to_objs(env, sources) + env['STATIC_OBJS']
47712797Sgabeblack@google.com        if self.main:
47812797Sgabeblack@google.com            objs += env['MAIN_OBJS']
47912797Sgabeblack@google.com        return super(UnitTest, self).declare(env, objs)
48012797Sgabeblack@google.com
48112797Sgabeblack@google.comclass GTest(Executable):
48212313Sgabeblack@google.com    '''Create a unit test based on the google test framework.'''
48312313Sgabeblack@google.com    all = []
48412797Sgabeblack@google.com    def __init__(self, *srcs_and_filts, **kwargs):
48512797Sgabeblack@google.com        super(GTest, self).__init__(*srcs_and_filts)
48612797Sgabeblack@google.com
48712371Sgabeblack@google.com        self.skip_lib = kwargs.pop('skip_lib', False)
4885584Snate@binkert.org
48912797Sgabeblack@google.com    @classmethod
49012797Sgabeblack@google.com    def declare_all(cls, env):
49112797Sgabeblack@google.com        env = env.Clone()
49212797Sgabeblack@google.com        env.Append(LIBS=env['GTEST_LIBS'])
49312797Sgabeblack@google.com        env.Append(CPPFLAGS=env['GTEST_CPPFLAGS'])
49412797Sgabeblack@google.com        env['GTEST_LIB_SOURCES'] = Source.all.with_tag('gtest lib')
49512797Sgabeblack@google.com        env['GTEST_OUT_DIR'] = \
49612797Sgabeblack@google.com            Dir(env['BUILDDIR']).Dir('unittests.' + env['EXE_SUFFIX'])
49712797Sgabeblack@google.com        return super(GTest, cls).declare_all(env)
49812797Sgabeblack@google.com
49912797Sgabeblack@google.com    def declare(self, env):
50012797Sgabeblack@google.com        sources = list(self.sources)
50112797Sgabeblack@google.com        if not self.skip_lib:
50212797Sgabeblack@google.com            sources += env['GTEST_LIB_SOURCES']
50312797Sgabeblack@google.com        for f in self.filters:
50412797Sgabeblack@google.com            sources += Source.all.apply_filter(f)
50512797Sgabeblack@google.com        objs = self.srcs_to_objs(env, sources)
50612797Sgabeblack@google.com
50712797Sgabeblack@google.com        binary = super(GTest, self).declare(env, objs)
50812797Sgabeblack@google.com
50912797Sgabeblack@google.com        out_dir = env['GTEST_OUT_DIR']
51012797Sgabeblack@google.com        xml_file = out_dir.Dir(str(self.dir)).File(self.target + '.xml')
51112797Sgabeblack@google.com        AlwaysBuild(env.Command(xml_file, binary,
51212797Sgabeblack@google.com            "${SOURCES[0]} --gtest_output=xml:${TARGETS[0]}"))
51312797Sgabeblack@google.com
51412797Sgabeblack@google.com        return binary
51512797Sgabeblack@google.com
51612797Sgabeblack@google.comclass Gem5(Executable):
51712797Sgabeblack@google.com    '''Create a gem5 executable.'''
51812797Sgabeblack@google.com
51912797Sgabeblack@google.com    def __init__(self, target):
52012797Sgabeblack@google.com        super(Gem5, self).__init__(target)
52112797Sgabeblack@google.com
52212797Sgabeblack@google.com    def declare(self, env):
52312797Sgabeblack@google.com        objs = env['MAIN_OBJS'] + env['STATIC_OBJS']
52412797Sgabeblack@google.com        return super(Gem5, self).declare(env, objs)
52512797Sgabeblack@google.com
52612797Sgabeblack@google.com
5274382Sbinkertn@umich.edu# Children should have access
52813576Sciro.santilli@arm.comExport('Blob')
52913577Sciro.santilli@arm.comExport('GdbXml')
5304202Sbinkertn@umich.eduExport('Source')
5314382Sbinkertn@umich.eduExport('PySource')
5324382Sbinkertn@umich.eduExport('SimObject')
5339396Sandreas.hansson@arm.comExport('ProtoBuf')
53412797Sgabeblack@google.comExport('Executable')
5355584Snate@binkert.orgExport('UnitTest')
53612313Sgabeblack@google.comExport('GTest')
5374382Sbinkertn@umich.edu
5384382Sbinkertn@umich.edu########################################################################
5394382Sbinkertn@umich.edu#
5408232Snate@binkert.org# Debug Flags
5415192Ssaidi@eecs.umich.edu#
5428232Snate@binkert.orgdebug_flags = {}
5438232Snate@binkert.orgdef DebugFlag(name, desc=None):
5448232Snate@binkert.org    if name in debug_flags:
5455192Ssaidi@eecs.umich.edu        raise AttributeError, "Flag %s already specified" % name
5468232Snate@binkert.org    debug_flags[name] = (name, (), desc)
5475192Ssaidi@eecs.umich.edu
5485799Snate@binkert.orgdef CompoundFlag(name, flags, desc=None):
5498232Snate@binkert.org    if name in debug_flags:
5505192Ssaidi@eecs.umich.edu        raise AttributeError, "Flag %s already specified" % name
5515192Ssaidi@eecs.umich.edu
5525192Ssaidi@eecs.umich.edu    compound = tuple(flags)
5538232Snate@binkert.org    debug_flags[name] = (name, compound, desc)
5545192Ssaidi@eecs.umich.edu
5558232Snate@binkert.orgExport('DebugFlag')
5565192Ssaidi@eecs.umich.eduExport('CompoundFlag')
5575192Ssaidi@eecs.umich.edu
5585192Ssaidi@eecs.umich.edu########################################################################
5595192Ssaidi@eecs.umich.edu#
5604382Sbinkertn@umich.edu# Set some compiler variables
5614382Sbinkertn@umich.edu#
5624382Sbinkertn@umich.edu
5632667Sstever@eecs.umich.edu# Include file paths are rooted in this directory.  SCons will
5642667Sstever@eecs.umich.edu# automatically expand '.' to refer to both the source directory and
5652667Sstever@eecs.umich.edu# the corresponding build directory to pick up generated include
5662667Sstever@eecs.umich.edu# files.
5672667Sstever@eecs.umich.eduenv.Append(CPPPATH=Dir('.'))
5682667Sstever@eecs.umich.edu
5695742Snate@binkert.orgfor extra_dir in extras_dir_list:
5705742Snate@binkert.org    env.Append(CPPPATH=Dir(extra_dir))
5715742Snate@binkert.org
5725793Snate@binkert.org# Workaround for bug in SCons version > 0.97d20071212
5738334Snate@binkert.org# Scons bug id: 2006 gem5 Bug id: 308
5745793Snate@binkert.orgfor root, dirs, files in os.walk(base_dir, topdown=True):
5755793Snate@binkert.org    Dir(root[len(base_dir) + 1:])
5765793Snate@binkert.org
5774382Sbinkertn@umich.edu########################################################################
5784762Snate@binkert.org#
5795344Sstever@gmail.com# Walk the tree and execute all SConscripts in subdirectories
5804382Sbinkertn@umich.edu#
5815341Sstever@gmail.com
5825742Snate@binkert.orghere = Dir('.').srcnode().abspath
5835742Snate@binkert.orgfor root, dirs, files in os.walk(base_dir, topdown=True):
5845742Snate@binkert.org    if root == here:
5855742Snate@binkert.org        # we don't want to recurse back into this SConscript
5865742Snate@binkert.org        continue
5874762Snate@binkert.org
5885742Snate@binkert.org    if 'SConscript' in files:
5895742Snate@binkert.org        build_dir = joinpath(env['BUILDDIR'], root[len(base_dir) + 1:])
59011984Sgabeblack@google.com        Source.set_group(build_dir)
5917722Sgblack@eecs.umich.edu        SConscript(joinpath(root, 'SConscript'), variant_dir=build_dir)
5925742Snate@binkert.org
5935742Snate@binkert.orgfor extra_dir in extras_dir_list:
5945742Snate@binkert.org    prefix_len = len(dirname(extra_dir)) + 1
5959930Sandreas.hansson@arm.com
5969930Sandreas.hansson@arm.com    # Also add the corresponding build directory to pick up generated
5979930Sandreas.hansson@arm.com    # include files.
5989930Sandreas.hansson@arm.com    env.Append(CPPPATH=Dir(joinpath(env['BUILDDIR'], extra_dir[prefix_len:])))
5999930Sandreas.hansson@arm.com
6005742Snate@binkert.org    for root, dirs, files in os.walk(extra_dir, topdown=True):
6018242Sbradley.danofsky@amd.com        # if build lives in the extras directory, don't walk down it
6028242Sbradley.danofsky@amd.com        if 'build' in dirs:
6038242Sbradley.danofsky@amd.com            dirs.remove('build')
6048242Sbradley.danofsky@amd.com
6055341Sstever@gmail.com        if 'SConscript' in files:
6065742Snate@binkert.org            build_dir = joinpath(env['BUILDDIR'], root[prefix_len:])
6077722Sgblack@eecs.umich.edu            SConscript(joinpath(root, 'SConscript'), variant_dir=build_dir)
6084773Snate@binkert.org
6096108Snate@binkert.orgfor opt in export_vars:
6101858SN/A    env.ConfigFile(opt)
6111085SN/A
6126658Snate@binkert.orgdef makeTheISA(source, target, env):
6136658Snate@binkert.org    isas = [ src.get_contents() for src in source ]
6147673Snate@binkert.org    target_isa = env['TARGET_ISA']
6156658Snate@binkert.org    def define(isa):
6166658Snate@binkert.org        return isa.upper() + '_ISA'
61711308Santhony.gutierrez@amd.com
6186658Snate@binkert.org    def namespace(isa):
61911308Santhony.gutierrez@amd.com        return isa[0].upper() + isa[1:].lower() + 'ISA'
6206658Snate@binkert.org
6216658Snate@binkert.org
6227673Snate@binkert.org    code = code_formatter()
6237673Snate@binkert.org    code('''\
6247673Snate@binkert.org#ifndef __CONFIG_THE_ISA_HH__
6257673Snate@binkert.org#define __CONFIG_THE_ISA_HH__
6267673Snate@binkert.org
6277673Snate@binkert.org''')
6287673Snate@binkert.org
62910467Sandreas.hansson@arm.com    # create defines for the preprocessing and compile-time determination
6306658Snate@binkert.org    for i,isa in enumerate(isas):
6317673Snate@binkert.org        code('#define $0 $1', define(isa), i + 1)
63210467Sandreas.hansson@arm.com    code()
63310467Sandreas.hansson@arm.com
63410467Sandreas.hansson@arm.com    # create an enum for any run-time determination of the ISA, we
63510467Sandreas.hansson@arm.com    # reuse the same name as the namespaces
63610467Sandreas.hansson@arm.com    code('enum class Arch {')
63710467Sandreas.hansson@arm.com    for i,isa in enumerate(isas):
63810467Sandreas.hansson@arm.com        if i + 1 == len(isas):
63910467Sandreas.hansson@arm.com            code('  $0 = $1', namespace(isa), define(isa))
64010467Sandreas.hansson@arm.com        else:
64110467Sandreas.hansson@arm.com            code('  $0 = $1,', namespace(isa), define(isa))
64210467Sandreas.hansson@arm.com    code('};')
6437673Snate@binkert.org
6447673Snate@binkert.org    code('''
6457673Snate@binkert.org
6467673Snate@binkert.org#define THE_ISA ${{define(target_isa)}}
6477673Snate@binkert.org#define TheISA ${{namespace(target_isa)}}
6489048SAli.Saidi@ARM.com#define THE_ISA_STR "${{target_isa}}"
6497673Snate@binkert.org
6507673Snate@binkert.org#endif // __CONFIG_THE_ISA_HH__''')
6517673Snate@binkert.org
6527673Snate@binkert.org    code.write(str(target[0]))
6536658Snate@binkert.org
6547756SAli.Saidi@ARM.comenv.Command('config/the_isa.hh', map(Value, all_isa_list),
6557816Ssteve.reinhardt@amd.com            MakeAction(makeTheISA, Transform("CFG ISA", 0)))
6566658Snate@binkert.org
65711308Santhony.gutierrez@amd.comdef makeTheGPUISA(source, target, env):
65811308Santhony.gutierrez@amd.com    isas = [ src.get_contents() for src in source ]
65911308Santhony.gutierrez@amd.com    target_gpu_isa = env['TARGET_GPU_ISA']
66011308Santhony.gutierrez@amd.com    def define(isa):
66111308Santhony.gutierrez@amd.com        return isa.upper() + '_ISA'
66211308Santhony.gutierrez@amd.com
66311308Santhony.gutierrez@amd.com    def namespace(isa):
66411308Santhony.gutierrez@amd.com        return isa[0].upper() + isa[1:].lower() + 'ISA'
66511308Santhony.gutierrez@amd.com
66611308Santhony.gutierrez@amd.com
66711308Santhony.gutierrez@amd.com    code = code_formatter()
66811308Santhony.gutierrez@amd.com    code('''\
66911308Santhony.gutierrez@amd.com#ifndef __CONFIG_THE_GPU_ISA_HH__
67011308Santhony.gutierrez@amd.com#define __CONFIG_THE_GPU_ISA_HH__
67111308Santhony.gutierrez@amd.com
67211308Santhony.gutierrez@amd.com''')
67311308Santhony.gutierrez@amd.com
67411308Santhony.gutierrez@amd.com    # create defines for the preprocessing and compile-time determination
67511308Santhony.gutierrez@amd.com    for i,isa in enumerate(isas):
67611308Santhony.gutierrez@amd.com        code('#define $0 $1', define(isa), i + 1)
67711308Santhony.gutierrez@amd.com    code()
67811308Santhony.gutierrez@amd.com
67911308Santhony.gutierrez@amd.com    # create an enum for any run-time determination of the ISA, we
68011308Santhony.gutierrez@amd.com    # reuse the same name as the namespaces
68111308Santhony.gutierrez@amd.com    code('enum class GPUArch {')
68211308Santhony.gutierrez@amd.com    for i,isa in enumerate(isas):
68311308Santhony.gutierrez@amd.com        if i + 1 == len(isas):
68411308Santhony.gutierrez@amd.com            code('  $0 = $1', namespace(isa), define(isa))
68511308Santhony.gutierrez@amd.com        else:
68611308Santhony.gutierrez@amd.com            code('  $0 = $1,', namespace(isa), define(isa))
68711308Santhony.gutierrez@amd.com    code('};')
68811308Santhony.gutierrez@amd.com
68911308Santhony.gutierrez@amd.com    code('''
69011308Santhony.gutierrez@amd.com
69111308Santhony.gutierrez@amd.com#define THE_GPU_ISA ${{define(target_gpu_isa)}}
69211308Santhony.gutierrez@amd.com#define TheGpuISA ${{namespace(target_gpu_isa)}}
69311308Santhony.gutierrez@amd.com#define THE_GPU_ISA_STR "${{target_gpu_isa}}"
69411308Santhony.gutierrez@amd.com
69511308Santhony.gutierrez@amd.com#endif // __CONFIG_THE_GPU_ISA_HH__''')
69611308Santhony.gutierrez@amd.com
69711308Santhony.gutierrez@amd.com    code.write(str(target[0]))
69811308Santhony.gutierrez@amd.com
69911308Santhony.gutierrez@amd.comenv.Command('config/the_gpu_isa.hh', map(Value, all_gpu_isa_list),
70011308Santhony.gutierrez@amd.com            MakeAction(makeTheGPUISA, Transform("CFG ISA", 0)))
70111308Santhony.gutierrez@amd.com
7024382Sbinkertn@umich.edu########################################################################
7034382Sbinkertn@umich.edu#
7044762Snate@binkert.org# Prevent any SimObjects from being added after this point, they
7054762Snate@binkert.org# should all have been added in the SConscripts above
7064762Snate@binkert.org#
7076654Snate@binkert.orgSimObject.fixed = True
7086654Snate@binkert.org
7095517Snate@binkert.orgclass DictImporter(object):
7105517Snate@binkert.org    '''This importer takes a dictionary of arbitrary module names that
7115517Snate@binkert.org    map to arbitrary filenames.'''
7125517Snate@binkert.org    def __init__(self, modules):
7135517Snate@binkert.org        self.modules = modules
7145517Snate@binkert.org        self.installed = set()
7155517Snate@binkert.org
7165517Snate@binkert.org    def __del__(self):
7175517Snate@binkert.org        self.unload()
7185517Snate@binkert.org
7195517Snate@binkert.org    def unload(self):
7205517Snate@binkert.org        import sys
7215517Snate@binkert.org        for module in self.installed:
7225517Snate@binkert.org            del sys.modules[module]
7235517Snate@binkert.org        self.installed = set()
7245517Snate@binkert.org
7255517Snate@binkert.org    def find_module(self, fullname, path):
7266654Snate@binkert.org        if fullname == 'm5.defines':
7275517Snate@binkert.org            return self
7285517Snate@binkert.org
7295517Snate@binkert.org        if fullname == 'm5.objects':
7305517Snate@binkert.org            return self
7315517Snate@binkert.org
73211802Sandreas.sandberg@arm.com        if fullname.startswith('_m5'):
7335517Snate@binkert.org            return None
7345517Snate@binkert.org
7356143Snate@binkert.org        source = self.modules.get(fullname, None)
7366654Snate@binkert.org        if source is not None and fullname.startswith('m5.objects'):
7375517Snate@binkert.org            return self
7385517Snate@binkert.org
7395517Snate@binkert.org        return None
7405517Snate@binkert.org
7415517Snate@binkert.org    def load_module(self, fullname):
7425517Snate@binkert.org        mod = imp.new_module(fullname)
7435517Snate@binkert.org        sys.modules[fullname] = mod
7445517Snate@binkert.org        self.installed.add(fullname)
7455517Snate@binkert.org
7465517Snate@binkert.org        mod.__loader__ = self
7475517Snate@binkert.org        if fullname == 'm5.objects':
7485517Snate@binkert.org            mod.__path__ = fullname.split('.')
7495517Snate@binkert.org            return mod
7505517Snate@binkert.org
7516654Snate@binkert.org        if fullname == 'm5.defines':
7526654Snate@binkert.org            mod.__dict__['buildEnv'] = m5.util.SmartDict(build_env)
7535517Snate@binkert.org            return mod
7545517Snate@binkert.org
7556143Snate@binkert.org        source = self.modules[fullname]
7566143Snate@binkert.org        if source.modname == '__init__':
7576143Snate@binkert.org            mod.__path__ = source.modpath
7586727Ssteve.reinhardt@amd.com        mod.__file__ = source.abspath
7595517Snate@binkert.org
7606727Ssteve.reinhardt@amd.com        exec file(source.abspath, 'r') in mod.__dict__
7615517Snate@binkert.org
7625517Snate@binkert.org        return mod
7635517Snate@binkert.org
7646654Snate@binkert.orgimport m5.SimObject
7656654Snate@binkert.orgimport m5.params
7667673Snate@binkert.orgfrom m5.util import code_formatter
7676654Snate@binkert.org
7686654Snate@binkert.orgm5.SimObject.clear()
7696654Snate@binkert.orgm5.params.clear()
7706654Snate@binkert.org
7715517Snate@binkert.org# install the python importer so we can grab stuff from the source
7725517Snate@binkert.org# tree itself.  We can't have SimObjects added after this point or
7735517Snate@binkert.org# else we won't know about them for the rest of the stuff.
7746143Snate@binkert.orgimporter = DictImporter(PySource.modules)
7755517Snate@binkert.orgsys.meta_path[0:0] = [ importer ]
7764762Snate@binkert.org
7775517Snate@binkert.org# import all sim objects so we can populate the all_objects list
7785517Snate@binkert.org# make sure that we're working with a list, then let's sort it
7796143Snate@binkert.orgfor modname in SimObject.modnames:
7806143Snate@binkert.org    exec('from m5.objects import %s' % modname)
7815517Snate@binkert.org
7825517Snate@binkert.org# we need to unload all of the currently imported modules so that they
7835517Snate@binkert.org# will be re-imported the next time the sconscript is run
7845517Snate@binkert.orgimporter.unload()
7855517Snate@binkert.orgsys.meta_path.remove(importer)
7865517Snate@binkert.org
7875517Snate@binkert.orgsim_objects = m5.SimObject.allClasses
7885517Snate@binkert.orgall_enums = m5.params.allEnums
7895517Snate@binkert.org
7906143Snate@binkert.orgfor name,obj in sorted(sim_objects.iteritems()):
7915517Snate@binkert.org    for param in obj._params.local.values():
7926654Snate@binkert.org        # load the ptype attribute now because it depends on the
7936654Snate@binkert.org        # current version of SimObject.allClasses, but when scons
7946654Snate@binkert.org        # actually uses the value, all versions of
7956654Snate@binkert.org        # SimObject.allClasses will have been loaded
7966654Snate@binkert.org        param.ptype
7976654Snate@binkert.org
7984762Snate@binkert.org########################################################################
7994762Snate@binkert.org#
8004762Snate@binkert.org# calculate extra dependencies
8014762Snate@binkert.org#
8024762Snate@binkert.orgmodule_depends = ["m5", "m5.SimObject", "m5.params"]
8037675Snate@binkert.orgdepends = [ PySource.modules[dep].snode for dep in module_depends ]
80410584Sandreas.hansson@arm.comdepends.sort(key = lambda x: x.name)
8054762Snate@binkert.org
8064762Snate@binkert.org########################################################################
8074762Snate@binkert.org#
8084762Snate@binkert.org# Commands for the basic automatically generated python files
8094382Sbinkertn@umich.edu#
8104382Sbinkertn@umich.edu
8115517Snate@binkert.org# Generate Python file containing a dict specifying the current
8126654Snate@binkert.org# buildEnv flags.
8135517Snate@binkert.orgdef makeDefinesPyFile(target, source, env):
8148126Sgblack@eecs.umich.edu    build_env = source[0].get_contents()
8156654Snate@binkert.org
8167673Snate@binkert.org    code = code_formatter()
8176654Snate@binkert.org    code("""
81811802Sandreas.sandberg@arm.comimport _m5.core
8196654Snate@binkert.orgimport m5.util
8206654Snate@binkert.org
8216654Snate@binkert.orgbuildEnv = m5.util.SmartDict($build_env)
8226654Snate@binkert.org
82311802Sandreas.sandberg@arm.comcompileDate = _m5.core.compileDate
8246669Snate@binkert.org_globals = globals()
82511802Sandreas.sandberg@arm.comfor key,val in _m5.core.__dict__.iteritems():
8266669Snate@binkert.org    if key.startswith('flag_'):
8276669Snate@binkert.org        flag = key[5:]
8286669Snate@binkert.org        _globals[flag] = val
8296669Snate@binkert.orgdel _globals
8306654Snate@binkert.org""")
8317673Snate@binkert.org    code.write(target[0].abspath)
8325517Snate@binkert.org
8338126Sgblack@eecs.umich.edudefines_info = Value(build_env)
8345798Snate@binkert.org# Generate a file with all of the compile options in it
8357756SAli.Saidi@ARM.comenv.Command('python/m5/defines.py', defines_info,
8367816Ssteve.reinhardt@amd.com            MakeAction(makeDefinesPyFile, Transform("DEFINES", 0)))
8375798Snate@binkert.orgPySource('m5', 'python/m5/defines.py')
8385798Snate@binkert.org
8395517Snate@binkert.org# Generate python file containing info about the M5 source code
8405517Snate@binkert.orgdef makeInfoPyFile(target, source, env):
8417673Snate@binkert.org    code = code_formatter()
8425517Snate@binkert.org    for src in source:
8435517Snate@binkert.org        data = ''.join(file(src.srcnode().abspath, 'r').xreadlines())
8447673Snate@binkert.org        code('$src = ${{repr(data)}}')
8457673Snate@binkert.org    code.write(str(target[0]))
8465517Snate@binkert.org
8475798Snate@binkert.org# Generate a file that wraps the basic top level files
8485798Snate@binkert.orgenv.Command('python/m5/info.py',
8498333Snate@binkert.org            [ '#/COPYING', '#/LICENSE', '#/README', ],
8507816Ssteve.reinhardt@amd.com            MakeAction(makeInfoPyFile, Transform("INFO")))
8515798Snate@binkert.orgPySource('m5', 'python/m5/info.py')
8525798Snate@binkert.org
8534762Snate@binkert.org########################################################################
8544762Snate@binkert.org#
8554762Snate@binkert.org# Create all of the SimObject param headers and enum headers
8564762Snate@binkert.org#
8574762Snate@binkert.org
8588596Ssteve.reinhardt@amd.comdef createSimObjectParamStruct(target, source, env):
8595517Snate@binkert.org    assert len(target) == 1 and len(source) == 1
8605517Snate@binkert.org
86111997Sgabeblack@google.com    name = source[0].get_text_contents()
8625517Snate@binkert.org    obj = sim_objects[name]
8635517Snate@binkert.org
8647673Snate@binkert.org    code = code_formatter()
8658596Ssteve.reinhardt@amd.com    obj.cxx_param_decl(code)
8667673Snate@binkert.org    code.write(target[0].abspath)
8675517Snate@binkert.org
86810458Sandreas.hansson@arm.comdef createSimObjectCxxConfig(is_header):
86910458Sandreas.hansson@arm.com    def body(target, source, env):
87010458Sandreas.hansson@arm.com        assert len(target) == 1 and len(source) == 1
87110458Sandreas.hansson@arm.com
87210458Sandreas.hansson@arm.com        name = str(source[0].get_contents())
87310458Sandreas.hansson@arm.com        obj = sim_objects[name]
87410458Sandreas.hansson@arm.com
87510458Sandreas.hansson@arm.com        code = code_formatter()
87610458Sandreas.hansson@arm.com        obj.cxx_config_param_file(code, is_header)
87710458Sandreas.hansson@arm.com        code.write(target[0].abspath)
87810458Sandreas.hansson@arm.com    return body
87910458Sandreas.hansson@arm.com
8805517Snate@binkert.orgdef createEnumStrings(target, source, env):
88111996Sgabeblack@google.com    assert len(target) == 1 and len(source) == 2
8825517Snate@binkert.org
88311997Sgabeblack@google.com    name = source[0].get_text_contents()
88411996Sgabeblack@google.com    use_python = source[1].read()
8855517Snate@binkert.org    obj = all_enums[name]
8865517Snate@binkert.org
8877673Snate@binkert.org    code = code_formatter()
8887673Snate@binkert.org    obj.cxx_def(code)
88911996Sgabeblack@google.com    if use_python:
89011988Sandreas.sandberg@arm.com        obj.pybind_def(code)
8917673Snate@binkert.org    code.write(target[0].abspath)
8925517Snate@binkert.org
8938596Ssteve.reinhardt@amd.comdef createEnumDecls(target, source, env):
8945517Snate@binkert.org    assert len(target) == 1 and len(source) == 1
8955517Snate@binkert.org
89611997Sgabeblack@google.com    name = source[0].get_text_contents()
8975517Snate@binkert.org    obj = all_enums[name]
8985517Snate@binkert.org
8997673Snate@binkert.org    code = code_formatter()
9007673Snate@binkert.org    obj.cxx_decl(code)
9017673Snate@binkert.org    code.write(target[0].abspath)
9025517Snate@binkert.org
90311988Sandreas.sandberg@arm.comdef createSimObjectPyBindWrapper(target, source, env):
90411997Sgabeblack@google.com    name = source[0].get_text_contents()
9058596Ssteve.reinhardt@amd.com    obj = sim_objects[name]
9068596Ssteve.reinhardt@amd.com
9078596Ssteve.reinhardt@amd.com    code = code_formatter()
90811988Sandreas.sandberg@arm.com    obj.pybind_decl(code)
9098596Ssteve.reinhardt@amd.com    code.write(target[0].abspath)
9108596Ssteve.reinhardt@amd.com
9118596Ssteve.reinhardt@amd.com# Generate all of the SimObject param C++ struct header files
9124762Snate@binkert.orgparams_hh_files = []
9136143Snate@binkert.orgfor name,simobj in sorted(sim_objects.iteritems()):
9146143Snate@binkert.org    py_source = PySource.modules[simobj.__module__]
9156143Snate@binkert.org    extra_deps = [ py_source.tnode ]
9164762Snate@binkert.org
9174762Snate@binkert.org    hh_file = File('params/%s.hh' % name)
9184762Snate@binkert.org    params_hh_files.append(hh_file)
9197756SAli.Saidi@ARM.com    env.Command(hh_file, Value(name),
9208596Ssteve.reinhardt@amd.com                MakeAction(createSimObjectParamStruct, Transform("SO PARAM")))
9214762Snate@binkert.org    env.Depends(hh_file, depends + extra_deps)
9224762Snate@binkert.org
92310458Sandreas.hansson@arm.com# C++ parameter description files
92410458Sandreas.hansson@arm.comif GetOption('with_cxx_config'):
92510458Sandreas.hansson@arm.com    for name,simobj in sorted(sim_objects.iteritems()):
92610458Sandreas.hansson@arm.com        py_source = PySource.modules[simobj.__module__]
92710458Sandreas.hansson@arm.com        extra_deps = [ py_source.tnode ]
92810458Sandreas.hansson@arm.com
92910458Sandreas.hansson@arm.com        cxx_config_hh_file = File('cxx_config/%s.hh' % name)
93010458Sandreas.hansson@arm.com        cxx_config_cc_file = File('cxx_config/%s.cc' % name)
93110458Sandreas.hansson@arm.com        env.Command(cxx_config_hh_file, Value(name),
93210458Sandreas.hansson@arm.com                    MakeAction(createSimObjectCxxConfig(True),
93310458Sandreas.hansson@arm.com                    Transform("CXXCPRHH")))
93410458Sandreas.hansson@arm.com        env.Command(cxx_config_cc_file, Value(name),
93510458Sandreas.hansson@arm.com                    MakeAction(createSimObjectCxxConfig(False),
93610458Sandreas.hansson@arm.com                    Transform("CXXCPRCC")))
93710458Sandreas.hansson@arm.com        env.Depends(cxx_config_hh_file, depends + extra_deps +
93810458Sandreas.hansson@arm.com                    [File('params/%s.hh' % name), File('sim/cxx_config.hh')])
93910458Sandreas.hansson@arm.com        env.Depends(cxx_config_cc_file, depends + extra_deps +
94010458Sandreas.hansson@arm.com                    [cxx_config_hh_file])
94110458Sandreas.hansson@arm.com        Source(cxx_config_cc_file)
94210458Sandreas.hansson@arm.com
94310458Sandreas.hansson@arm.com    cxx_config_init_cc_file = File('cxx_config/init.cc')
94410458Sandreas.hansson@arm.com
94510458Sandreas.hansson@arm.com    def createCxxConfigInitCC(target, source, env):
94610458Sandreas.hansson@arm.com        assert len(target) == 1 and len(source) == 1
94710458Sandreas.hansson@arm.com
94810458Sandreas.hansson@arm.com        code = code_formatter()
94910458Sandreas.hansson@arm.com
95010458Sandreas.hansson@arm.com        for name,simobj in sorted(sim_objects.iteritems()):
95110458Sandreas.hansson@arm.com            if not hasattr(simobj, 'abstract') or not simobj.abstract:
95210458Sandreas.hansson@arm.com                code('#include "cxx_config/${name}.hh"')
95310458Sandreas.hansson@arm.com        code()
95410458Sandreas.hansson@arm.com        code('void cxxConfigInit()')
95510458Sandreas.hansson@arm.com        code('{')
95610458Sandreas.hansson@arm.com        code.indent()
95710458Sandreas.hansson@arm.com        for name,simobj in sorted(sim_objects.iteritems()):
95810458Sandreas.hansson@arm.com            not_abstract = not hasattr(simobj, 'abstract') or \
95910458Sandreas.hansson@arm.com                not simobj.abstract
96010458Sandreas.hansson@arm.com            if not_abstract and 'type' in simobj.__dict__:
96110458Sandreas.hansson@arm.com                code('cxx_config_directory["${name}"] = '
96210458Sandreas.hansson@arm.com                     '${name}CxxConfigParams::makeDirectoryEntry();')
96310458Sandreas.hansson@arm.com        code.dedent()
96410458Sandreas.hansson@arm.com        code('}')
96510458Sandreas.hansson@arm.com        code.write(target[0].abspath)
96610458Sandreas.hansson@arm.com
96710458Sandreas.hansson@arm.com    py_source = PySource.modules[simobj.__module__]
96810458Sandreas.hansson@arm.com    extra_deps = [ py_source.tnode ]
96910458Sandreas.hansson@arm.com    env.Command(cxx_config_init_cc_file, Value(name),
97010458Sandreas.hansson@arm.com        MakeAction(createCxxConfigInitCC, Transform("CXXCINIT")))
97110458Sandreas.hansson@arm.com    cxx_param_hh_files = ["cxx_config/%s.hh" % simobj
97210584Sandreas.hansson@arm.com        for name,simobj in sorted(sim_objects.iteritems())
97310458Sandreas.hansson@arm.com        if not hasattr(simobj, 'abstract') or not simobj.abstract]
97410458Sandreas.hansson@arm.com    Depends(cxx_config_init_cc_file, cxx_param_hh_files +
97510458Sandreas.hansson@arm.com            [File('sim/cxx_config.hh')])
97610458Sandreas.hansson@arm.com    Source(cxx_config_init_cc_file)
97710458Sandreas.hansson@arm.com
9784762Snate@binkert.org# Generate all enum header files
9796143Snate@binkert.orgfor name,enum in sorted(all_enums.iteritems()):
9806143Snate@binkert.org    py_source = PySource.modules[enum.__module__]
9816143Snate@binkert.org    extra_deps = [ py_source.tnode ]
9824762Snate@binkert.org
9834762Snate@binkert.org    cc_file = File('enums/%s.cc' % name)
98411996Sgabeblack@google.com    env.Command(cc_file, [Value(name), Value(env['USE_PYTHON'])],
9857816Ssteve.reinhardt@amd.com                MakeAction(createEnumStrings, Transform("ENUM STR")))
9864762Snate@binkert.org    env.Depends(cc_file, depends + extra_deps)
9874762Snate@binkert.org    Source(cc_file)
9884762Snate@binkert.org
9894762Snate@binkert.org    hh_file = File('enums/%s.hh' % name)
9907756SAli.Saidi@ARM.com    env.Command(hh_file, Value(name),
9918596Ssteve.reinhardt@amd.com                MakeAction(createEnumDecls, Transform("ENUMDECL")))
9924762Snate@binkert.org    env.Depends(hh_file, depends + extra_deps)
9934762Snate@binkert.org
99411988Sandreas.sandberg@arm.com# Generate SimObject Python bindings wrapper files
99511988Sandreas.sandberg@arm.comif env['USE_PYTHON']:
99611988Sandreas.sandberg@arm.com    for name,simobj in sorted(sim_objects.iteritems()):
99711988Sandreas.sandberg@arm.com        py_source = PySource.modules[simobj.__module__]
99811988Sandreas.sandberg@arm.com        extra_deps = [ py_source.tnode ]
99911988Sandreas.sandberg@arm.com        cc_file = File('python/_m5/param_%s.cc' % name)
100011988Sandreas.sandberg@arm.com        env.Command(cc_file, Value(name),
100111988Sandreas.sandberg@arm.com                    MakeAction(createSimObjectPyBindWrapper,
100211988Sandreas.sandberg@arm.com                               Transform("SO PyBind")))
100311988Sandreas.sandberg@arm.com        env.Depends(cc_file, depends + extra_deps)
100411988Sandreas.sandberg@arm.com        Source(cc_file)
10054382Sbinkertn@umich.edu
10069396Sandreas.hansson@arm.com# Build all protocol buffers if we have got protoc and protobuf available
10079396Sandreas.hansson@arm.comif env['HAVE_PROTOBUF']:
10089396Sandreas.hansson@arm.com    for proto in ProtoBuf.all:
10099396Sandreas.hansson@arm.com        # Use both the source and header as the target, and the .proto
10109396Sandreas.hansson@arm.com        # file as the source. When executing the protoc compiler, also
10119396Sandreas.hansson@arm.com        # specify the proto_path to avoid having the generated files
10129396Sandreas.hansson@arm.com        # include the path.
10139396Sandreas.hansson@arm.com        env.Command([proto.cc_file, proto.hh_file], proto.tnode,
10149396Sandreas.hansson@arm.com                    MakeAction('$PROTOC --cpp_out ${TARGET.dir} '
10159396Sandreas.hansson@arm.com                               '--proto_path ${SOURCE.dir} $SOURCE',
10169396Sandreas.hansson@arm.com                               Transform("PROTOC")))
10179396Sandreas.hansson@arm.com
10189396Sandreas.hansson@arm.com        # Add the C++ source file
101912302Sgabeblack@google.com        Source(proto.cc_file, tags=proto.tags)
10209396Sandreas.hansson@arm.comelif ProtoBuf.all:
102112563Sgabeblack@google.com    print('Got protobuf to build, but lacks support!')
10229396Sandreas.hansson@arm.com    Exit(1)
10239396Sandreas.hansson@arm.com
10248232Snate@binkert.org#
10258232Snate@binkert.org# Handle debug flags
10268232Snate@binkert.org#
10278232Snate@binkert.orgdef makeDebugFlagCC(target, source, env):
10288232Snate@binkert.org    assert(len(target) == 1 and len(source) == 1)
10296229Snate@binkert.org
103010455SCurtis.Dunham@arm.com    code = code_formatter()
10316229Snate@binkert.org
103210455SCurtis.Dunham@arm.com    # delay definition of CompoundFlags until after all the definition
103310455SCurtis.Dunham@arm.com    # of all constituent SimpleFlags
103410455SCurtis.Dunham@arm.com    comp_code = code_formatter()
10355517Snate@binkert.org
10365517Snate@binkert.org    # file header
10377673Snate@binkert.org    code('''
10385517Snate@binkert.org/*
103910455SCurtis.Dunham@arm.com * DO NOT EDIT THIS FILE! Automatically generated by SCons.
10405517Snate@binkert.org */
10415517Snate@binkert.org
10428232Snate@binkert.org#include "base/debug.hh"
104310455SCurtis.Dunham@arm.com
104410455SCurtis.Dunham@arm.comnamespace Debug {
104510455SCurtis.Dunham@arm.com
10467673Snate@binkert.org''')
10477673Snate@binkert.org
104810455SCurtis.Dunham@arm.com    for name, flag in sorted(source[0].read().iteritems()):
104910455SCurtis.Dunham@arm.com        n, compound, desc = flag
105010455SCurtis.Dunham@arm.com        assert n == name
10515517Snate@binkert.org
105210455SCurtis.Dunham@arm.com        if not compound:
105310455SCurtis.Dunham@arm.com            code('SimpleFlag $name("$name", "$desc");')
105410455SCurtis.Dunham@arm.com        else:
105510455SCurtis.Dunham@arm.com            comp_code('CompoundFlag $name("$name", "$desc",')
105610455SCurtis.Dunham@arm.com            comp_code.indent()
105710455SCurtis.Dunham@arm.com            last = len(compound) - 1
105810455SCurtis.Dunham@arm.com            for i,flag in enumerate(compound):
105910455SCurtis.Dunham@arm.com                if i != last:
106010685Sandreas.hansson@arm.com                    comp_code('&$flag,')
106110455SCurtis.Dunham@arm.com                else:
106210685Sandreas.hansson@arm.com                    comp_code('&$flag);')
106310455SCurtis.Dunham@arm.com            comp_code.dedent()
10645517Snate@binkert.org
106510455SCurtis.Dunham@arm.com    code.append(comp_code)
10668232Snate@binkert.org    code()
10678232Snate@binkert.org    code('} // namespace Debug')
10685517Snate@binkert.org
10697673Snate@binkert.org    code.write(str(target[0]))
10705517Snate@binkert.org
10718232Snate@binkert.orgdef makeDebugFlagHH(target, source, env):
10728232Snate@binkert.org    assert(len(target) == 1 and len(source) == 1)
10735517Snate@binkert.org
10748232Snate@binkert.org    val = eval(source[0].get_contents())
10758232Snate@binkert.org    name, compound, desc = val
10768232Snate@binkert.org
10777673Snate@binkert.org    code = code_formatter()
10785517Snate@binkert.org
10795517Snate@binkert.org    # file header boilerplate
10807673Snate@binkert.org    code('''\
10815517Snate@binkert.org/*
108210455SCurtis.Dunham@arm.com * DO NOT EDIT THIS FILE! Automatically generated by SCons.
10835517Snate@binkert.org */
10845517Snate@binkert.org
10858232Snate@binkert.org#ifndef __DEBUG_${name}_HH__
10868232Snate@binkert.org#define __DEBUG_${name}_HH__
10875517Snate@binkert.org
10888232Snate@binkert.orgnamespace Debug {
10898232Snate@binkert.org''')
10905517Snate@binkert.org
10918232Snate@binkert.org    if compound:
10928232Snate@binkert.org        code('class CompoundFlag;')
10938232Snate@binkert.org    code('class SimpleFlag;')
10945517Snate@binkert.org
10958232Snate@binkert.org    if compound:
10968232Snate@binkert.org        code('extern CompoundFlag $name;')
10978232Snate@binkert.org        for flag in compound:
10988232Snate@binkert.org            code('extern SimpleFlag $flag;')
10998232Snate@binkert.org    else:
11008232Snate@binkert.org        code('extern SimpleFlag $name;')
11015517Snate@binkert.org
11028232Snate@binkert.org    code('''
11038232Snate@binkert.org}
11045517Snate@binkert.org
11058232Snate@binkert.org#endif // __DEBUG_${name}_HH__
11067673Snate@binkert.org''')
11075517Snate@binkert.org
11087673Snate@binkert.org    code.write(str(target[0]))
11095517Snate@binkert.org
11108232Snate@binkert.orgfor name,flag in sorted(debug_flags.iteritems()):
11118232Snate@binkert.org    n, compound, desc = flag
11128232Snate@binkert.org    assert n == name
11135192Ssaidi@eecs.umich.edu
111410454SCurtis.Dunham@arm.com    hh_file = 'debug/%s.hh' % name
111510454SCurtis.Dunham@arm.com    env.Command(hh_file, Value(flag),
11168232Snate@binkert.org                MakeAction(makeDebugFlagHH, Transform("TRACING", 0)))
111710455SCurtis.Dunham@arm.com
111810455SCurtis.Dunham@arm.comenv.Command('debug/flags.cc', Value(debug_flags),
111910455SCurtis.Dunham@arm.com            MakeAction(makeDebugFlagCC, Transform("TRACING", 0)))
112010455SCurtis.Dunham@arm.comSource('debug/flags.cc')
11215192Ssaidi@eecs.umich.edu
112211077SCurtis.Dunham@arm.com# version tags
112311330SCurtis.Dunham@arm.comtags = \
112411077SCurtis.Dunham@arm.comenv.Command('sim/tags.cc', None,
112511077SCurtis.Dunham@arm.com            MakeAction('util/cpt_upgrader.py --get-cc-file > $TARGET',
112611077SCurtis.Dunham@arm.com                       Transform("VER TAGS")))
112711330SCurtis.Dunham@arm.comenv.AlwaysBuild(tags)
112811077SCurtis.Dunham@arm.com
11297674Snate@binkert.org# Embed python files.  All .py files that have been indicated by a
11305522Snate@binkert.org# PySource() call in a SConscript need to be embedded into the M5
11315522Snate@binkert.org# library.  To do that, we compile the file to byte code, marshal the
11327674Snate@binkert.org# byte code, compress it, and then generate a c++ file that
11337674Snate@binkert.org# inserts the result into an array.
11347674Snate@binkert.orgdef embedPyFile(target, source, env):
11357674Snate@binkert.org    def c_str(string):
11367674Snate@binkert.org        if string is None:
11377674Snate@binkert.org            return "0"
11387674Snate@binkert.org        return '"%s"' % string
11397674Snate@binkert.org
11405522Snate@binkert.org    '''Action function to compile a .py into a code object, marshal
11415522Snate@binkert.org    it, compress it, and stick it into an asm file so the code appears
11425522Snate@binkert.org    as just bytes with a label in the data section'''
11435517Snate@binkert.org
11445522Snate@binkert.org    src = file(str(source[0]), 'r').read()
11455517Snate@binkert.org
11466143Snate@binkert.org    pysource = PySource.tnodes[source[0]]
11476727Ssteve.reinhardt@amd.com    compiled = compile(src, pysource.abspath, 'exec')
11485522Snate@binkert.org    marshalled = marshal.dumps(compiled)
11495522Snate@binkert.org    compressed = zlib.compress(marshalled)
11505522Snate@binkert.org    data = compressed
11517674Snate@binkert.org    sym = pysource.symname
11525517Snate@binkert.org
11537673Snate@binkert.org    code = code_formatter()
11547673Snate@binkert.org    code('''\
11557674Snate@binkert.org#include "sim/init.hh"
11567673Snate@binkert.org
11577674Snate@binkert.orgnamespace {
11587674Snate@binkert.org
11597674Snate@binkert.org''')
116013576Sciro.santilli@arm.com    blobToCpp(data, 'data_' + sym, code)
116113576Sciro.santilli@arm.com    code('''\
116211308Santhony.gutierrez@amd.com
11637673Snate@binkert.org
11647674Snate@binkert.orgEmbeddedPython embedded_${sym}(
11657674Snate@binkert.org    ${{c_str(pysource.arcname)}},
11667674Snate@binkert.org    ${{c_str(pysource.abspath)}},
11677674Snate@binkert.org    ${{c_str(pysource.modpath)}},
11687674Snate@binkert.org    data_${sym},
11697674Snate@binkert.org    ${{len(data)}},
11707674Snate@binkert.org    ${{len(marshalled)}});
11717674Snate@binkert.org
11727811Ssteve.reinhardt@amd.com} // anonymous namespace
11737674Snate@binkert.org''')
11747673Snate@binkert.org    code.write(str(target[0]))
11755522Snate@binkert.org
11766143Snate@binkert.orgfor source in PySource.all:
117710453SAndrew.Bardsley@arm.com    env.Command(source.cpp, source.tnode,
11787816Ssteve.reinhardt@amd.com                MakeAction(embedPyFile, Transform("EMBED PY")))
117912302Sgabeblack@google.com    Source(source.cpp, tags=source.tags, add_tags='python')
11804382Sbinkertn@umich.edu
11814382Sbinkertn@umich.edu########################################################################
11824382Sbinkertn@umich.edu#
11834382Sbinkertn@umich.edu# Define binaries.  Each different build type (debug, opt, etc.) gets
11844382Sbinkertn@umich.edu# a slightly different build environment.
11854382Sbinkertn@umich.edu#
11864382Sbinkertn@umich.edu
11874382Sbinkertn@umich.edu# List of constructed environments to pass back to SConstruct
118812302Sgabeblack@google.comdate_source = Source('base/date.cc', tags=[])
11894382Sbinkertn@umich.edu
119012797Sgabeblack@google.comgem5_binary = Gem5('gem5')
119112797Sgabeblack@google.com
11922655Sstever@eecs.umich.edu# Function to create a new build environment as clone of current
11932655Sstever@eecs.umich.edu# environment 'env' with modified object suffix and optional stripped
11942655Sstever@eecs.umich.edu# binary.  Additional keyword arguments are appended to corresponding
11952655Sstever@eecs.umich.edu# build environment vars.
119612063Sgabeblack@google.comdef makeEnv(env, label, objsfx, strip=False, disable_partial=False, **kwargs):
11975601Snate@binkert.org    # SCons doesn't know to append a library suffix when there is a '.' in the
11985601Snate@binkert.org    # name.  Use '_' instead.
119912222Sgabeblack@google.com    libname = 'gem5_' + label
120012222Sgabeblack@google.com    secondary_exename = 'm5.' + label
12015522Snate@binkert.org
12025863Snate@binkert.org    new_env = env.Clone(OBJSUFFIX=objsfx, SHOBJSUFFIX=objsfx + 's')
12035601Snate@binkert.org    new_env.Label = label
12045601Snate@binkert.org    new_env.Append(**kwargs)
12055601Snate@binkert.org
120612302Sgabeblack@google.com    lib_sources = Source.all.with_tag('gem5 lib')
120710453SAndrew.Bardsley@arm.com
120811988Sandreas.sandberg@arm.com    # Without Python, leave out all Python content from the library
120911988Sandreas.sandberg@arm.com    # builds.  The option doesn't affect gem5 built as a program
121010453SAndrew.Bardsley@arm.com    if GetOption('without_python'):
121112302Sgabeblack@google.com        lib_sources = lib_sources.without_tag('python')
121210453SAndrew.Bardsley@arm.com
121311983Sgabeblack@google.com    static_objs = []
121411983Sgabeblack@google.com    shared_objs = []
121512302Sgabeblack@google.com
121612302Sgabeblack@google.com    for s in lib_sources.with_tag(Source.ungrouped_tag):
121712362Sgabeblack@google.com        static_objs.append(s.static(new_env))
121812362Sgabeblack@google.com        shared_objs.append(s.shared(new_env))
121911983Sgabeblack@google.com
122012302Sgabeblack@google.com    for group in Source.source_groups:
122112302Sgabeblack@google.com        srcs = lib_sources.with_tag(Source.link_group_tag(group))
122211983Sgabeblack@google.com        if not srcs:
122311983Sgabeblack@google.com            continue
122411983Sgabeblack@google.com
122512362Sgabeblack@google.com        group_static = [ s.static(new_env) for s in srcs ]
122612362Sgabeblack@google.com        group_shared = [ s.shared(new_env) for s in srcs ]
122712310Sgabeblack@google.com
122812063Sgabeblack@google.com        # If partial linking is disabled, add these sources to the build
122912063Sgabeblack@google.com        # directly, and short circuit this loop.
123012063Sgabeblack@google.com        if disable_partial:
123112310Sgabeblack@google.com            static_objs.extend(group_static)
123212310Sgabeblack@google.com            shared_objs.extend(group_shared)
123312063Sgabeblack@google.com            continue
123412063Sgabeblack@google.com
123511983Sgabeblack@google.com        # Set up the static partially linked objects.
123611983Sgabeblack@google.com        file_name = new_env.subst("${OBJPREFIX}lib${OBJSUFFIX}.partial")
123711983Sgabeblack@google.com        target = File(joinpath(group, file_name))
123812310Sgabeblack@google.com        partial = env.PartialStatic(target=target, source=group_static)
123912310Sgabeblack@google.com        static_objs.extend(partial)
124011983Sgabeblack@google.com
124111983Sgabeblack@google.com        # Set up the shared partially linked objects.
124211983Sgabeblack@google.com        file_name = new_env.subst("${SHOBJPREFIX}lib${SHOBJSUFFIX}.partial")
124311983Sgabeblack@google.com        target = File(joinpath(group, file_name))
124412310Sgabeblack@google.com        partial = env.PartialShared(target=target, source=group_shared)
124512310Sgabeblack@google.com        shared_objs.extend(partial)
12466143Snate@binkert.org
124712362Sgabeblack@google.com    static_date = date_source.static(new_env)
124812306Sgabeblack@google.com    new_env.Depends(static_date, static_objs)
124912310Sgabeblack@google.com    static_objs.extend(static_date)
125010453SAndrew.Bardsley@arm.com
125112362Sgabeblack@google.com    shared_date = date_source.shared(new_env)
125212306Sgabeblack@google.com    new_env.Depends(shared_date, shared_objs)
125312310Sgabeblack@google.com    shared_objs.extend(shared_date)
12545554Snate@binkert.org
125512797Sgabeblack@google.com    main_objs = [ s.static(new_env) for s in Source.all.with_tag('main') ]
125612797Sgabeblack@google.com
12575522Snate@binkert.org    # First make a library of everything but main() so other programs can
12585522Snate@binkert.org    # link against m5.
12595797Snate@binkert.org    static_lib = new_env.StaticLibrary(libname, static_objs)
12605797Snate@binkert.org    shared_lib = new_env.SharedLibrary(libname, shared_objs)
12615522Snate@binkert.org
126212797Sgabeblack@google.com    # Keep track of the object files generated so far so Executables can
126312797Sgabeblack@google.com    # include them.
126412797Sgabeblack@google.com    new_env['STATIC_OBJS'] = static_objs
126512797Sgabeblack@google.com    new_env['SHARED_OBJS'] = shared_objs
126612797Sgabeblack@google.com    new_env['MAIN_OBJS'] = main_objs
12678233Snate@binkert.org
126812797Sgabeblack@google.com    new_env['STATIC_LIB'] = static_lib
126912797Sgabeblack@google.com    new_env['SHARED_LIB'] = shared_lib
12708235Snate@binkert.org
127112797Sgabeblack@google.com    # Record some settings for building Executables.
127212797Sgabeblack@google.com    new_env['EXE_SUFFIX'] = label
127312797Sgabeblack@google.com    new_env['STRIP_EXES'] = strip
127412370Sgabeblack@google.com
127512797Sgabeblack@google.com    for cls in ExecutableMeta.all:
127612797Sgabeblack@google.com        cls.declare_all(new_env)
127712313Sgabeblack@google.com
127812797Sgabeblack@google.com    new_env.M5Binary = File(gem5_binary.path(new_env))
12796143Snate@binkert.org
128012797Sgabeblack@google.com    new_env.Command(secondary_exename, new_env.M5Binary,
12818334Snate@binkert.org            MakeAction('ln $SOURCE $TARGET', Transform("HARDLINK")))
12828334Snate@binkert.org
128311993Sgabeblack@google.com    # Set up regression tests.
128411993Sgabeblack@google.com    SConscript(os.path.join(env.root.abspath, 'tests', 'SConscript'),
128512223Sgabeblack@google.com               variant_dir=Dir('tests').Dir(new_env.Label),
128611993Sgabeblack@google.com               exports={ 'env' : new_env }, duplicate=False)
12872655Sstever@eecs.umich.edu
12889225Sandreas.hansson@arm.com# Start out with the compiler flags common to all compilers,
12899225Sandreas.hansson@arm.com# i.e. they all use -g for opt and -g -pg for prof
12909226Sandreas.hansson@arm.comccflags = {'debug' : [], 'opt' : ['-g'], 'fast' : [], 'prof' : ['-g', '-pg'],
12919226Sandreas.hansson@arm.com           'perf' : ['-g']}
12929225Sandreas.hansson@arm.com
12939226Sandreas.hansson@arm.com# Start out with the linker flags common to all linkers, i.e. -pg for
12949226Sandreas.hansson@arm.com# prof, and -lprofiler for perf. The -lprofile flag is surrounded by
12959226Sandreas.hansson@arm.com# no-as-needed and as-needed as the binutils linker is too clever and
12969226Sandreas.hansson@arm.com# simply doesn't link to the library otherwise.
12979226Sandreas.hansson@arm.comldflags = {'debug' : [], 'opt' : [], 'fast' : [], 'prof' : ['-pg'],
12989226Sandreas.hansson@arm.com           'perf' : ['-Wl,--no-as-needed', '-lprofiler', '-Wl,--as-needed']}
12999225Sandreas.hansson@arm.com
13009227Sandreas.hansson@arm.com# For Link Time Optimization, the optimisation flags used to compile
13019227Sandreas.hansson@arm.com# individual files are decoupled from those used at link time
13029227Sandreas.hansson@arm.com# (i.e. you can compile with -O3 and perform LTO with -O0), so we need
13039227Sandreas.hansson@arm.com# to also update the linker flags based on the target.
13048946Sandreas.hansson@arm.comif env['GCC']:
13053918Ssaidi@eecs.umich.edu    if sys.platform == 'sunos5':
13069225Sandreas.hansson@arm.com        ccflags['debug'] += ['-gstabs+']
13073918Ssaidi@eecs.umich.edu    else:
13089225Sandreas.hansson@arm.com        ccflags['debug'] += ['-ggdb3']
13099225Sandreas.hansson@arm.com    ldflags['debug'] += ['-O0']
13109227Sandreas.hansson@arm.com    # opt, fast, prof and perf all share the same cc flags, also add
13119227Sandreas.hansson@arm.com    # the optimization to the ldflags as LTO defers the optimization
13129227Sandreas.hansson@arm.com    # to link time
13139226Sandreas.hansson@arm.com    for target in ['opt', 'fast', 'prof', 'perf']:
13149225Sandreas.hansson@arm.com        ccflags[target] += ['-O3']
13159227Sandreas.hansson@arm.com        ldflags[target] += ['-O3']
13169227Sandreas.hansson@arm.com
13179227Sandreas.hansson@arm.com    ccflags['fast'] += env['LTO_CCFLAGS']
13189227Sandreas.hansson@arm.com    ldflags['fast'] += env['LTO_LDFLAGS']
13198946Sandreas.hansson@arm.comelif env['CLANG']:
13209225Sandreas.hansson@arm.com    ccflags['debug'] += ['-g', '-O0']
13219226Sandreas.hansson@arm.com    # opt, fast, prof and perf all share the same cc flags
13229226Sandreas.hansson@arm.com    for target in ['opt', 'fast', 'prof', 'perf']:
13239226Sandreas.hansson@arm.com        ccflags[target] += ['-O3']
13243515Ssaidi@eecs.umich.eduelse:
132512563Sgabeblack@google.com    print('Unknown compiler, please fix compiler options')
13264762Snate@binkert.org    Exit(1)
13273515Ssaidi@eecs.umich.edu
13288881Smarc.orr@gmail.com
13298881Smarc.orr@gmail.com# To speed things up, we only instantiate the build environments we
13308881Smarc.orr@gmail.com# need.  We try to identify the needed environment for each target; if
13318881Smarc.orr@gmail.com# we can't, we fall back on instantiating all the environments just to
13328881Smarc.orr@gmail.com# be safe.
13339226Sandreas.hansson@arm.comtarget_types = ['debug', 'opt', 'fast', 'prof', 'perf']
13349226Sandreas.hansson@arm.comobj2target = {'do': 'debug', 'o': 'opt', 'fo': 'fast', 'po': 'prof',
13359226Sandreas.hansson@arm.com              'gpo' : 'perf'}
13368881Smarc.orr@gmail.com
13378881Smarc.orr@gmail.comdef identifyTarget(t):
13388881Smarc.orr@gmail.com    ext = t.split('.')[-1]
13398881Smarc.orr@gmail.com    if ext in target_types:
13408881Smarc.orr@gmail.com        return ext
13418881Smarc.orr@gmail.com    if obj2target.has_key(ext):
13428881Smarc.orr@gmail.com        return obj2target[ext]
13438881Smarc.orr@gmail.com    match = re.search(r'/tests/([^/]+)/', t)
13448881Smarc.orr@gmail.com    if match and match.group(1) in target_types:
13458881Smarc.orr@gmail.com        return match.group(1)
13468881Smarc.orr@gmail.com    return 'all'
13478881Smarc.orr@gmail.com
13488881Smarc.orr@gmail.comneeded_envs = [identifyTarget(target) for target in BUILD_TARGETS]
13498881Smarc.orr@gmail.comif 'all' in needed_envs:
13508881Smarc.orr@gmail.com    needed_envs += target_types
13518881Smarc.orr@gmail.com
135213508Snikos.nikoleris@arm.comdisable_partial = False
135313508Snikos.nikoleris@arm.comif env['PLATFORM'] == 'darwin':
135413508Snikos.nikoleris@arm.com    # Up until Apple LLVM version 10.0.0 (clang-1000.11.45.5), partial
135513508Snikos.nikoleris@arm.com    # linked objects do not expose symbols that are marked with the
135613508Snikos.nikoleris@arm.com    # hidden visibility and consequently building gem5 on Mac OS
135713508Snikos.nikoleris@arm.com    # fails. As a workaround, we disable partial linking, however, we
135813508Snikos.nikoleris@arm.com    # may want to revisit in the future.
135913508Snikos.nikoleris@arm.com    disable_partial = True
136013508Snikos.nikoleris@arm.com
136112222Sgabeblack@google.com# Debug binary
136212222Sgabeblack@google.comif 'debug' in needed_envs:
136312222Sgabeblack@google.com    makeEnv(env, 'debug', '.do',
136412222Sgabeblack@google.com            CCFLAGS = Split(ccflags['debug']),
136512222Sgabeblack@google.com            CPPDEFINES = ['DEBUG', 'TRACING_ON=1'],
136613508Snikos.nikoleris@arm.com            LINKFLAGS = Split(ldflags['debug']),
136713508Snikos.nikoleris@arm.com            disable_partial=disable_partial)
1368955SN/A
136912222Sgabeblack@google.com# Optimized binary
137012222Sgabeblack@google.comif 'opt' in needed_envs:
137112222Sgabeblack@google.com    makeEnv(env, 'opt', '.o',
137212222Sgabeblack@google.com            CCFLAGS = Split(ccflags['opt']),
137312222Sgabeblack@google.com            CPPDEFINES = ['TRACING_ON=1'],
137413508Snikos.nikoleris@arm.com            LINKFLAGS = Split(ldflags['opt']),
137513508Snikos.nikoleris@arm.com            disable_partial=disable_partial)
1376955SN/A
137712222Sgabeblack@google.com# "Fast" binary
137812222Sgabeblack@google.comif 'fast' in needed_envs:
137913508Snikos.nikoleris@arm.com    disable_partial = disable_partial and \
138012222Sgabeblack@google.com            env.get('BROKEN_INCREMENTAL_LTO', False) and \
138112222Sgabeblack@google.com            GetOption('force_lto')
138212222Sgabeblack@google.com    makeEnv(env, 'fast', '.fo', strip = True,
138312222Sgabeblack@google.com            CCFLAGS = Split(ccflags['fast']),
138412222Sgabeblack@google.com            CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
138512222Sgabeblack@google.com            LINKFLAGS = Split(ldflags['fast']),
138612222Sgabeblack@google.com            disable_partial=disable_partial)
13871869SN/A
138812222Sgabeblack@google.com# Profiled binary using gprof
138912222Sgabeblack@google.comif 'prof' in needed_envs:
139012222Sgabeblack@google.com    makeEnv(env, 'prof', '.po',
139112222Sgabeblack@google.com            CCFLAGS = Split(ccflags['prof']),
139212222Sgabeblack@google.com            CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
139313508Snikos.nikoleris@arm.com            LINKFLAGS = Split(ldflags['prof']),
139413508Snikos.nikoleris@arm.com            disable_partial=disable_partial)
13959226Sandreas.hansson@arm.com
139612222Sgabeblack@google.com# Profiled binary using google-pprof
139712222Sgabeblack@google.comif 'perf' in needed_envs:
139812222Sgabeblack@google.com    makeEnv(env, 'perf', '.gpo',
139912222Sgabeblack@google.com            CCFLAGS = Split(ccflags['perf']),
140012222Sgabeblack@google.com            CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
140113508Snikos.nikoleris@arm.com            LINKFLAGS = Split(ldflags['perf']),
140213508Snikos.nikoleris@arm.com            disable_partial=disable_partial)
1403