SConscript revision 13630
1955SN/A# -*- mode:python -*-
2955SN/A
31762SN/A# Copyright (c) 2018 ARM Limited
4955SN/A#
5955SN/A# The license below extends only to copyright in the software and shall
6955SN/A# not be construed as granting a license to any other intellectual
7955SN/A# property including but not limited to intellectual property relating
8955SN/A# to a hardware implementation of the functionality of the software
9955SN/A# licensed hereunder.  You may use the software subject to the license
10955SN/A# terms below provided that you ensure that this notice is replicated
11955SN/A# unmodified and in its entirety in all distributions of the software,
12955SN/A# modified or unmodified, in source code or in binary form.
13955SN/A#
14955SN/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#
282665Ssaidi@eecs.umich.edu# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
294762Snate@binkert.org# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30955SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
315522Snate@binkert.org# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
326143Snate@binkert.org# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
334762Snate@binkert.org# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
345522Snate@binkert.org# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35955SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
365522Snate@binkert.org# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37955SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
385522Snate@binkert.org# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
394202Sbinkertn@umich.edu#
405742Snate@binkert.org# Authors: Nathan Binkert
41955SN/A
424381Sbinkertn@umich.edufrom __future__ import print_function
434381Sbinkertn@umich.edu
448334Snate@binkert.orgimport array
45955SN/Aimport bisect
46955SN/Aimport functools
474202Sbinkertn@umich.eduimport imp
48955SN/Aimport marshal
494382Sbinkertn@umich.eduimport os
504382Sbinkertn@umich.eduimport re
514382Sbinkertn@umich.eduimport subprocess
526654Snate@binkert.orgimport sys
535517Snate@binkert.orgimport zlib
548614Sgblack@eecs.umich.edu
557674Snate@binkert.orgfrom os.path import basename, dirname, exists, isdir, isfile, join as joinpath
566143Snate@binkert.org
576143Snate@binkert.orgimport SCons
586143Snate@binkert.org
598233Snate@binkert.orgfrom gem5_scons import Transform
608233Snate@binkert.org
618233Snate@binkert.org# This file defines how to build a particular configuration of gem5
628233Snate@binkert.org# based on variable settings in the 'env' build environment.
638233Snate@binkert.org
648334Snate@binkert.orgImport('*')
658334Snate@binkert.org
6610453SAndrew.Bardsley@arm.com# Children need to see the environment
6710453SAndrew.Bardsley@arm.comExport('env')
688233Snate@binkert.org
698233Snate@binkert.orgbuild_env = [(opt, env[opt]) for opt in export_vars]
708233Snate@binkert.org
718233Snate@binkert.orgfrom m5.util import code_formatter, compareVersions
728233Snate@binkert.org
738233Snate@binkert.org########################################################################
746143Snate@binkert.org# Code for adding source files of various types
758233Snate@binkert.org#
768233Snate@binkert.org# When specifying a source file of some type, a set of tags can be
778233Snate@binkert.org# specified for that file.
786143Snate@binkert.org
796143Snate@binkert.orgclass SourceFilter(object):
806143Snate@binkert.org    def __init__(self, predicate):
8111308Santhony.gutierrez@amd.com        self.predicate = predicate
828233Snate@binkert.org
838233Snate@binkert.org    def __or__(self, other):
848233Snate@binkert.org        return SourceFilter(lambda tags: self.predicate(tags) or
856143Snate@binkert.org                                         other.predicate(tags))
868233Snate@binkert.org
878233Snate@binkert.org    def __and__(self, other):
888233Snate@binkert.org        return SourceFilter(lambda tags: self.predicate(tags) and
898233Snate@binkert.org                                         other.predicate(tags))
906143Snate@binkert.org
916143Snate@binkert.orgdef with_tags_that(predicate):
926143Snate@binkert.org    '''Return a list of sources with tags that satisfy a predicate.'''
934762Snate@binkert.org    return SourceFilter(predicate)
946143Snate@binkert.org
958233Snate@binkert.orgdef with_any_tags(*tags):
968233Snate@binkert.org    '''Return a list of sources with any of the supplied tags.'''
978233Snate@binkert.org    return SourceFilter(lambda stags: len(set(tags) & stags) > 0)
988233Snate@binkert.org
998233Snate@binkert.orgdef with_all_tags(*tags):
1006143Snate@binkert.org    '''Return a list of sources with all of the supplied tags.'''
1018233Snate@binkert.org    return SourceFilter(lambda stags: set(tags) <= stags)
1028233Snate@binkert.org
1038233Snate@binkert.orgdef with_tag(tag):
1048233Snate@binkert.org    '''Return a list of sources with the supplied tag.'''
1056143Snate@binkert.org    return SourceFilter(lambda stags: tag in stags)
1066143Snate@binkert.org
1076143Snate@binkert.orgdef without_tags(*tags):
1086143Snate@binkert.org    '''Return a list of sources without any of the supplied tags.'''
1096143Snate@binkert.org    return SourceFilter(lambda stags: len(set(tags) & stags) == 0)
1106143Snate@binkert.org
1116143Snate@binkert.orgdef without_tag(tag):
1126143Snate@binkert.org    '''Return a list of sources with the supplied tag.'''
1136143Snate@binkert.org    return SourceFilter(lambda stags: tag not in stags)
1147065Snate@binkert.org
1156143Snate@binkert.orgsource_filter_factories = {
1168233Snate@binkert.org    'with_tags_that': with_tags_that,
1178233Snate@binkert.org    'with_any_tags': with_any_tags,
1188233Snate@binkert.org    'with_all_tags': with_all_tags,
1198233Snate@binkert.org    'with_tag': with_tag,
1208233Snate@binkert.org    'without_tags': without_tags,
1218233Snate@binkert.org    'without_tag': without_tag,
1228233Snate@binkert.org}
1238233Snate@binkert.org
1248233Snate@binkert.orgExport(source_filter_factories)
1258233Snate@binkert.org
1268233Snate@binkert.orgclass SourceList(list):
1278233Snate@binkert.org    def apply_filter(self, f):
1288233Snate@binkert.org        def match(source):
1298233Snate@binkert.org            return f.predicate(source.tags)
1308233Snate@binkert.org        return SourceList(filter(match, self))
1318233Snate@binkert.org
1328233Snate@binkert.org    def __getattr__(self, name):
1338233Snate@binkert.org        func = source_filter_factories.get(name, None)
1348233Snate@binkert.org        if not func:
1358233Snate@binkert.org            raise AttributeError
1368233Snate@binkert.org
1378233Snate@binkert.org        @functools.wraps(func)
1388233Snate@binkert.org        def wrapper(*args, **kwargs):
1398233Snate@binkert.org            return self.apply_filter(func(*args, **kwargs))
1408233Snate@binkert.org        return wrapper
1418233Snate@binkert.org
1428233Snate@binkert.orgclass SourceMeta(type):
1438233Snate@binkert.org    '''Meta class for source files that keeps track of all files of a
1448233Snate@binkert.org    particular type.'''
1458233Snate@binkert.org    def __init__(cls, name, bases, dict):
1468233Snate@binkert.org        super(SourceMeta, cls).__init__(name, bases, dict)
1476143Snate@binkert.org        cls.all = SourceList()
1486143Snate@binkert.org
1496143Snate@binkert.orgclass SourceFile(object):
1506143Snate@binkert.org    '''Base object that encapsulates the notion of a source file.
1516143Snate@binkert.org    This includes, the source node, target node, various manipulations
1526143Snate@binkert.org    of those.  A source file also specifies a set of tags which
1539982Satgutier@umich.edu    describing arbitrary properties of the source file.'''
15410196SCurtis.Dunham@arm.com    __metaclass__ = SourceMeta
15510196SCurtis.Dunham@arm.com
15610196SCurtis.Dunham@arm.com    static_objs = {}
15710196SCurtis.Dunham@arm.com    shared_objs = {}
15810196SCurtis.Dunham@arm.com
15910196SCurtis.Dunham@arm.com    def __init__(self, source, tags=None, add_tags=None):
16010196SCurtis.Dunham@arm.com        if tags is None:
16110196SCurtis.Dunham@arm.com            tags='gem5 lib'
1626143Snate@binkert.org        if isinstance(tags, basestring):
1636143Snate@binkert.org            tags = set([tags])
1648945Ssteve.reinhardt@amd.com        if not isinstance(tags, set):
1658233Snate@binkert.org            tags = set(tags)
1668233Snate@binkert.org        self.tags = tags
1676143Snate@binkert.org
1688945Ssteve.reinhardt@amd.com        if add_tags:
1696143Snate@binkert.org            if isinstance(add_tags, basestring):
1706143Snate@binkert.org                add_tags = set([add_tags])
1716143Snate@binkert.org            if not isinstance(add_tags, set):
1726143Snate@binkert.org                add_tags = set(add_tags)
1735522Snate@binkert.org            self.tags |= add_tags
1746143Snate@binkert.org
1756143Snate@binkert.org        tnode = source
1766143Snate@binkert.org        if not isinstance(source, SCons.Node.FS.File):
1779982Satgutier@umich.edu            tnode = File(source)
1788233Snate@binkert.org
1798233Snate@binkert.org        self.tnode = tnode
1808233Snate@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):
1846143Snate@binkert.org                base.all.append(self)
1855522Snate@binkert.org
1865522Snate@binkert.org    def static(self, env):
1875522Snate@binkert.org        key = (self.tnode, env['OBJSUFFIX'])
1885522Snate@binkert.org        if not key in self.static_objs:
1895604Snate@binkert.org            self.static_objs[key] = env.StaticObject(self.tnode)
1905604Snate@binkert.org        return self.static_objs[key]
1916143Snate@binkert.org
1926143Snate@binkert.org    def shared(self, env):
1934762Snate@binkert.org        key = (self.tnode, env['OBJSUFFIX'])
1944762Snate@binkert.org        if not key in self.shared_objs:
1956143Snate@binkert.org            self.shared_objs[key] = env.SharedObject(self.tnode)
1966727Ssteve.reinhardt@amd.com        return self.shared_objs[key]
1976727Ssteve.reinhardt@amd.com
1986727Ssteve.reinhardt@amd.com    @property
1994762Snate@binkert.org    def filename(self):
2006143Snate@binkert.org        return str(self.tnode)
2016143Snate@binkert.org
2026143Snate@binkert.org    @property
2036143Snate@binkert.org    def dirname(self):
2046727Ssteve.reinhardt@amd.com        return dirname(self.filename)
2056143Snate@binkert.org
2067674Snate@binkert.org    @property
2077674Snate@binkert.org    def basename(self):
2085604Snate@binkert.org        return basename(self.filename)
2096143Snate@binkert.org
2106143Snate@binkert.org    @property
2116143Snate@binkert.org    def extname(self):
2124762Snate@binkert.org        index = self.basename.rfind('.')
2136143Snate@binkert.org        if index <= 0:
2144762Snate@binkert.org            # dot files aren't extensions
2154762Snate@binkert.org            return self.basename, None
2164762Snate@binkert.org
2176143Snate@binkert.org        return self.basename[:index], self.basename[index+1:]
2186143Snate@binkert.org
2194762Snate@binkert.org    def __lt__(self, other): return self.filename < other.filename
2208233Snate@binkert.org    def __le__(self, other): return self.filename <= other.filename
2218233Snate@binkert.org    def __gt__(self, other): return self.filename > other.filename
2228233Snate@binkert.org    def __ge__(self, other): return self.filename >= other.filename
2238233Snate@binkert.org    def __eq__(self, other): return self.filename == other.filename
2246143Snate@binkert.org    def __ne__(self, other): return self.filename != other.filename
2256143Snate@binkert.org
2264762Snate@binkert.orgdef blobToCpp(data, symbol, cpp_code, hpp_code=None, namespace=None):
2276143Snate@binkert.org    '''
2284762Snate@binkert.org    Convert bytes data into C++ .cpp and .hh uint8_t byte array
2296143Snate@binkert.org    code containing that binary data.
2304762Snate@binkert.org
2316143Snate@binkert.org    :param data: binary data to be converted to C++
2328233Snate@binkert.org    :param symbol: name of the symbol
2338233Snate@binkert.org    :param cpp_code: append the generated cpp_code to this object
23410453SAndrew.Bardsley@arm.com    :param hpp_code: append the generated hpp_code to this object
2356143Snate@binkert.org                     If None, ignore it. Otherwise, also include it
2366143Snate@binkert.org                     in the .cpp file.
2376143Snate@binkert.org    :param namespace: namespace to put the symbol into. If None,
2386143Snate@binkert.org                      don't put the symbols into any namespace.
2396143Snate@binkert.org    '''
2406143Snate@binkert.org    symbol_len_declaration = 'const std::size_t {}_len'.format(symbol)
2416143Snate@binkert.org    symbol_declaration = 'const std::uint8_t {}[]'.format(symbol)
2426143Snate@binkert.org    if hpp_code is not None:
24310453SAndrew.Bardsley@arm.com        cpp_code('''\
24410453SAndrew.Bardsley@arm.com#include "blobs/{}.hh"
245955SN/A'''.format(symbol))
2469396Sandreas.hansson@arm.com        hpp_code('''\
2479396Sandreas.hansson@arm.com#include <cstddef>
2489396Sandreas.hansson@arm.com#include <cstdint>
2499396Sandreas.hansson@arm.com''')
2509396Sandreas.hansson@arm.com        if namespace is not None:
2519396Sandreas.hansson@arm.com            hpp_code('namespace {} {{'.format(namespace))
2529396Sandreas.hansson@arm.com        hpp_code('extern ' + symbol_len_declaration + ';')
2539396Sandreas.hansson@arm.com        hpp_code('extern ' + symbol_declaration + ';')
2549396Sandreas.hansson@arm.com        if namespace is not None:
2559396Sandreas.hansson@arm.com            hpp_code('}')
2569396Sandreas.hansson@arm.com    if namespace is not None:
2579396Sandreas.hansson@arm.com        cpp_code('namespace {} {{'.format(namespace))
2589396Sandreas.hansson@arm.com    if hpp_code is not None:
2599930Sandreas.hansson@arm.com        cpp_code(symbol_len_declaration + ' = {};'.format(len(data)))
2609930Sandreas.hansson@arm.com    cpp_code(symbol_declaration + ' = {')
2619396Sandreas.hansson@arm.com    cpp_code.indent()
2628235Snate@binkert.org    step = 16
2638235Snate@binkert.org    for i in xrange(0, len(data), step):
2646143Snate@binkert.org        x = array.array('B', data[i:i+step])
2658235Snate@binkert.org        cpp_code(''.join('%d,' % d for d in x))
2669003SAli.Saidi@ARM.com    cpp_code.dedent()
2678235Snate@binkert.org    cpp_code('};')
2688235Snate@binkert.org    if namespace is not None:
2698235Snate@binkert.org        cpp_code('}')
2708235Snate@binkert.org
2718235Snate@binkert.orgdef Blob(blob_path, symbol):
2728235Snate@binkert.org    '''
2738235Snate@binkert.org    Embed an arbitrary blob into the gem5 executable,
2748235Snate@binkert.org    and make it accessible to C++ as a byte array.
2758235Snate@binkert.org    '''
2768235Snate@binkert.org    blob_path = os.path.abspath(blob_path)
2778235Snate@binkert.org    blob_out_dir = os.path.join(env['BUILDDIR'], 'blobs')
2788235Snate@binkert.org    path_noext = joinpath(blob_out_dir, symbol)
2798235Snate@binkert.org    cpp_path = path_noext + '.cc'
2808235Snate@binkert.org    hpp_path = path_noext + '.hh'
2819003SAli.Saidi@ARM.com    def embedBlob(target, source, env):
2828235Snate@binkert.org        data = file(str(source[0]), 'r').read()
2835584Snate@binkert.org        cpp_code = code_formatter()
2844382Sbinkertn@umich.edu        hpp_code = code_formatter()
2854202Sbinkertn@umich.edu        blobToCpp(data, symbol, cpp_code, hpp_code, namespace='Blobs')
2864382Sbinkertn@umich.edu        cpp_path = str(target[0])
2874382Sbinkertn@umich.edu        hpp_path = str(target[1])
2884382Sbinkertn@umich.edu        cpp_dir = os.path.split(cpp_path)[0]
2899396Sandreas.hansson@arm.com        if not os.path.exists(cpp_dir):
2905584Snate@binkert.org            os.makedirs(cpp_dir)
2914382Sbinkertn@umich.edu        cpp_code.write(cpp_path)
2924382Sbinkertn@umich.edu        hpp_code.write(hpp_path)
2934382Sbinkertn@umich.edu    env.Command([cpp_path, hpp_path], blob_path,
2948232Snate@binkert.org                MakeAction(embedBlob, Transform("EMBED BLOB")))
2955192Ssaidi@eecs.umich.edu    Source(cpp_path)
2968232Snate@binkert.org
2978232Snate@binkert.orgdef GdbXml(xml_id, symbol):
2988232Snate@binkert.org    Blob(joinpath(gdb_xml_dir, xml_id), symbol)
2995192Ssaidi@eecs.umich.edu
3008232Snate@binkert.orgclass Source(SourceFile):
3015192Ssaidi@eecs.umich.edu    ungrouped_tag = 'No link group'
3025799Snate@binkert.org    source_groups = set()
3038232Snate@binkert.org
3045192Ssaidi@eecs.umich.edu    _current_group_tag = ungrouped_tag
3055192Ssaidi@eecs.umich.edu
3065192Ssaidi@eecs.umich.edu    @staticmethod
3078232Snate@binkert.org    def link_group_tag(group):
3085192Ssaidi@eecs.umich.edu        return 'link group: %s' % group
3098232Snate@binkert.org
3105192Ssaidi@eecs.umich.edu    @classmethod
3115192Ssaidi@eecs.umich.edu    def set_group(cls, group):
3125192Ssaidi@eecs.umich.edu        new_tag = Source.link_group_tag(group)
3135192Ssaidi@eecs.umich.edu        Source._current_group_tag = new_tag
3144382Sbinkertn@umich.edu        Source.source_groups.add(group)
3154382Sbinkertn@umich.edu
3164382Sbinkertn@umich.edu    def _add_link_group_tag(self):
3172667Sstever@eecs.umich.edu        self.tags.add(Source._current_group_tag)
3182667Sstever@eecs.umich.edu
3192667Sstever@eecs.umich.edu    '''Add a c/c++ source file to the build'''
3202667Sstever@eecs.umich.edu    def __init__(self, source, tags=None, add_tags=None):
3212667Sstever@eecs.umich.edu        '''specify the source file, and any tags'''
3222667Sstever@eecs.umich.edu        super(Source, self).__init__(source, tags, add_tags)
3235742Snate@binkert.org        self._add_link_group_tag()
3245742Snate@binkert.org
3255742Snate@binkert.orgclass PySource(SourceFile):
3265793Snate@binkert.org    '''Add a python source file to the named package'''
3278334Snate@binkert.org    invalid_sym_char = re.compile('[^A-z0-9_]')
3285793Snate@binkert.org    modules = {}
3295793Snate@binkert.org    tnodes = {}
3305793Snate@binkert.org    symnames = {}
3314382Sbinkertn@umich.edu
3324762Snate@binkert.org    def __init__(self, package, source, tags=None, add_tags=None):
3335344Sstever@gmail.com        '''specify the python package, the source file, and any tags'''
3344382Sbinkertn@umich.edu        super(PySource, self).__init__(source, tags, add_tags)
3355341Sstever@gmail.com
3365742Snate@binkert.org        modname,ext = self.extname
3375742Snate@binkert.org        assert ext == 'py'
3385742Snate@binkert.org
3395742Snate@binkert.org        if package:
3405742Snate@binkert.org            path = package.split('.')
3414762Snate@binkert.org        else:
3425742Snate@binkert.org            path = []
3435742Snate@binkert.org
3447722Sgblack@eecs.umich.edu        modpath = path[:]
3455742Snate@binkert.org        if modname != '__init__':
3465742Snate@binkert.org            modpath += [ modname ]
3475742Snate@binkert.org        modpath = '.'.join(modpath)
3489930Sandreas.hansson@arm.com
3499930Sandreas.hansson@arm.com        arcpath = path + [ self.basename ]
3509930Sandreas.hansson@arm.com        abspath = self.snode.abspath
3519930Sandreas.hansson@arm.com        if not exists(abspath):
3529930Sandreas.hansson@arm.com            abspath = self.tnode.abspath
3535742Snate@binkert.org
3548242Sbradley.danofsky@amd.com        self.package = package
3558242Sbradley.danofsky@amd.com        self.modname = modname
3568242Sbradley.danofsky@amd.com        self.modpath = modpath
3578242Sbradley.danofsky@amd.com        self.arcname = joinpath(*arcpath)
3585341Sstever@gmail.com        self.abspath = abspath
3595742Snate@binkert.org        self.compiled = File(self.filename + 'c')
3607722Sgblack@eecs.umich.edu        self.cpp = File(self.filename + '.cc')
3614773Snate@binkert.org        self.symname = PySource.invalid_sym_char.sub('_', modpath)
3626108Snate@binkert.org
3631858SN/A        PySource.modules[modpath] = self
3641085SN/A        PySource.tnodes[self.tnode] = self
3656658Snate@binkert.org        PySource.symnames[self.symname] = self
3666658Snate@binkert.org
3677673Snate@binkert.orgclass SimObject(PySource):
3686658Snate@binkert.org    '''Add a SimObject python file as a python source object and add
3696658Snate@binkert.org    it to a list of sim object modules'''
37011308Santhony.gutierrez@amd.com
3716658Snate@binkert.org    fixed = False
37211308Santhony.gutierrez@amd.com    modnames = []
3736658Snate@binkert.org
3746658Snate@binkert.org    def __init__(self, source, tags=None, add_tags=None):
3757673Snate@binkert.org        '''Specify the source file and any tags (automatically in
3767673Snate@binkert.org        the m5.objects package)'''
3777673Snate@binkert.org        super(SimObject, self).__init__('m5.objects', source, tags, add_tags)
3787673Snate@binkert.org        if self.fixed:
3797673Snate@binkert.org            raise AttributeError, "Too late to call SimObject now."
3807673Snate@binkert.org
3817673Snate@binkert.org        bisect.insort_right(SimObject.modnames, self.modname)
38210467Sandreas.hansson@arm.com
3836658Snate@binkert.orgclass ProtoBuf(SourceFile):
3847673Snate@binkert.org    '''Add a Protocol Buffer to build'''
38510467Sandreas.hansson@arm.com
38610467Sandreas.hansson@arm.com    def __init__(self, source, tags=None, add_tags=None):
38710467Sandreas.hansson@arm.com        '''Specify the source file, and any tags'''
38810467Sandreas.hansson@arm.com        super(ProtoBuf, self).__init__(source, tags, add_tags)
38910467Sandreas.hansson@arm.com
39010467Sandreas.hansson@arm.com        # Get the file name and the extension
39110467Sandreas.hansson@arm.com        modname,ext = self.extname
39210467Sandreas.hansson@arm.com        assert ext == 'proto'
39310467Sandreas.hansson@arm.com
39410467Sandreas.hansson@arm.com        # Currently, we stick to generating the C++ headers, so we
39510467Sandreas.hansson@arm.com        # only need to track the source and header.
3967673Snate@binkert.org        self.cc_file = File(modname + '.pb.cc')
3977673Snate@binkert.org        self.hh_file = File(modname + '.pb.h')
3987673Snate@binkert.org
3997673Snate@binkert.org
4007673Snate@binkert.orgexectuable_classes = []
4019048SAli.Saidi@ARM.comclass ExecutableMeta(type):
4027673Snate@binkert.org    '''Meta class for Executables.'''
4037673Snate@binkert.org    all = []
4047673Snate@binkert.org
4057673Snate@binkert.org    def __init__(cls, name, bases, d):
4066658Snate@binkert.org        if not d.pop('abstract', False):
4077756SAli.Saidi@ARM.com            ExecutableMeta.all.append(cls)
4087816Ssteve.reinhardt@amd.com        super(ExecutableMeta, cls).__init__(name, bases, d)
4096658Snate@binkert.org
41011308Santhony.gutierrez@amd.com        cls.all = []
41111308Santhony.gutierrez@amd.com
41211308Santhony.gutierrez@amd.comclass Executable(object):
41311308Santhony.gutierrez@amd.com    '''Base class for creating an executable from sources.'''
41411308Santhony.gutierrez@amd.com    __metaclass__ = ExecutableMeta
41511308Santhony.gutierrez@amd.com
41611308Santhony.gutierrez@amd.com    abstract = True
41711308Santhony.gutierrez@amd.com
41811308Santhony.gutierrez@amd.com    def __init__(self, target, *srcs_and_filts):
41911308Santhony.gutierrez@amd.com        '''Specify the target name and any sources. Sources that are
42011308Santhony.gutierrez@amd.com        not SourceFiles are evalued with Source().'''
42111308Santhony.gutierrez@amd.com        super(Executable, self).__init__()
42211308Santhony.gutierrez@amd.com        self.all.append(self)
42311308Santhony.gutierrez@amd.com        self.target = target
42411308Santhony.gutierrez@amd.com
42511308Santhony.gutierrez@amd.com        isFilter = lambda arg: isinstance(arg, SourceFilter)
42611308Santhony.gutierrez@amd.com        self.filters = filter(isFilter, srcs_and_filts)
42711308Santhony.gutierrez@amd.com        sources = filter(lambda a: not isFilter(a), srcs_and_filts)
42811308Santhony.gutierrez@amd.com
42911308Santhony.gutierrez@amd.com        srcs = SourceList()
43011308Santhony.gutierrez@amd.com        for src in sources:
43111308Santhony.gutierrez@amd.com            if not isinstance(src, SourceFile):
43211308Santhony.gutierrez@amd.com                src = Source(src, tags=[])
43311308Santhony.gutierrez@amd.com            srcs.append(src)
43411308Santhony.gutierrez@amd.com
43511308Santhony.gutierrez@amd.com        self.sources = srcs
43611308Santhony.gutierrez@amd.com        self.dir = Dir('.')
43711308Santhony.gutierrez@amd.com
43811308Santhony.gutierrez@amd.com    def path(self, env):
43911308Santhony.gutierrez@amd.com        return self.dir.File(self.target + '.' + env['EXE_SUFFIX'])
44011308Santhony.gutierrez@amd.com
44111308Santhony.gutierrez@amd.com    def srcs_to_objs(self, env, sources):
44211308Santhony.gutierrez@amd.com        return list([ s.static(env) for s in sources ])
44311308Santhony.gutierrez@amd.com
44411308Santhony.gutierrez@amd.com    @classmethod
44511308Santhony.gutierrez@amd.com    def declare_all(cls, env):
44611308Santhony.gutierrez@amd.com        return list([ instance.declare(env) for instance in cls.all ])
44711308Santhony.gutierrez@amd.com
44811308Santhony.gutierrez@amd.com    def declare(self, env, objs=None):
44911308Santhony.gutierrez@amd.com        if objs is None:
45011308Santhony.gutierrez@amd.com            objs = self.srcs_to_objs(env, self.sources)
45111308Santhony.gutierrez@amd.com
45211308Santhony.gutierrez@amd.com        if env['STRIP_EXES']:
45311308Santhony.gutierrez@amd.com            stripped = self.path(env)
45411308Santhony.gutierrez@amd.com            unstripped = env.File(str(stripped) + '.unstripped')
4554382Sbinkertn@umich.edu            if sys.platform == 'sunos5':
4564382Sbinkertn@umich.edu                cmd = 'cp $SOURCE $TARGET; strip $TARGET'
4574762Snate@binkert.org            else:
4584762Snate@binkert.org                cmd = 'strip $SOURCE -o $TARGET'
4594762Snate@binkert.org            env.Program(unstripped, objs)
4606654Snate@binkert.org            return env.Command(stripped, unstripped,
4616654Snate@binkert.org                               MakeAction(cmd, Transform("STRIP")))
4625517Snate@binkert.org        else:
4635517Snate@binkert.org            return env.Program(self.path(env), objs)
4645517Snate@binkert.org
4655517Snate@binkert.orgclass UnitTest(Executable):
4665517Snate@binkert.org    '''Create a UnitTest'''
4675517Snate@binkert.org    def __init__(self, target, *srcs_and_filts, **kwargs):
4685517Snate@binkert.org        super(UnitTest, self).__init__(target, *srcs_and_filts)
4695517Snate@binkert.org
4705517Snate@binkert.org        self.main = kwargs.get('main', False)
4715517Snate@binkert.org
4725517Snate@binkert.org    def declare(self, env):
4735517Snate@binkert.org        sources = list(self.sources)
4745517Snate@binkert.org        for f in self.filters:
4755517Snate@binkert.org            sources = Source.all.apply_filter(f)
4765517Snate@binkert.org        objs = self.srcs_to_objs(env, sources) + env['STATIC_OBJS']
4775517Snate@binkert.org        if self.main:
4785517Snate@binkert.org            objs += env['MAIN_OBJS']
4796654Snate@binkert.org        return super(UnitTest, self).declare(env, objs)
4805517Snate@binkert.org
4815517Snate@binkert.orgclass GTest(Executable):
4825517Snate@binkert.org    '''Create a unit test based on the google test framework.'''
4835517Snate@binkert.org    all = []
4845517Snate@binkert.org    def __init__(self, *srcs_and_filts, **kwargs):
4855517Snate@binkert.org        super(GTest, self).__init__(*srcs_and_filts)
4865517Snate@binkert.org
4875517Snate@binkert.org        self.skip_lib = kwargs.pop('skip_lib', False)
4886143Snate@binkert.org
4896654Snate@binkert.org    @classmethod
4905517Snate@binkert.org    def declare_all(cls, env):
4915517Snate@binkert.org        env = env.Clone()
4925517Snate@binkert.org        env.Append(LIBS=env['GTEST_LIBS'])
4935517Snate@binkert.org        env.Append(CPPFLAGS=env['GTEST_CPPFLAGS'])
4945517Snate@binkert.org        env['GTEST_LIB_SOURCES'] = Source.all.with_tag('gtest lib')
4955517Snate@binkert.org        env['GTEST_OUT_DIR'] = \
4965517Snate@binkert.org            Dir(env['BUILDDIR']).Dir('unittests.' + env['EXE_SUFFIX'])
4975517Snate@binkert.org        return super(GTest, cls).declare_all(env)
4985517Snate@binkert.org
4995517Snate@binkert.org    def declare(self, env):
5005517Snate@binkert.org        sources = list(self.sources)
5015517Snate@binkert.org        if not self.skip_lib:
5025517Snate@binkert.org            sources += env['GTEST_LIB_SOURCES']
5035517Snate@binkert.org        for f in self.filters:
5046654Snate@binkert.org            sources += Source.all.apply_filter(f)
5056654Snate@binkert.org        objs = self.srcs_to_objs(env, sources)
5065517Snate@binkert.org
5075517Snate@binkert.org        binary = super(GTest, self).declare(env, objs)
5086143Snate@binkert.org
5096143Snate@binkert.org        out_dir = env['GTEST_OUT_DIR']
5106143Snate@binkert.org        xml_file = out_dir.Dir(str(self.dir)).File(self.target + '.xml')
5116727Ssteve.reinhardt@amd.com        AlwaysBuild(env.Command(xml_file, binary,
5125517Snate@binkert.org            "${SOURCES[0]} --gtest_output=xml:${TARGETS[0]}"))
5136727Ssteve.reinhardt@amd.com
5145517Snate@binkert.org        return binary
5155517Snate@binkert.org
5165517Snate@binkert.orgclass Gem5(Executable):
5176654Snate@binkert.org    '''Create a gem5 executable.'''
5186654Snate@binkert.org
5197673Snate@binkert.org    def __init__(self, target):
5206654Snate@binkert.org        super(Gem5, self).__init__(target)
5216654Snate@binkert.org
5226654Snate@binkert.org    def declare(self, env):
5236654Snate@binkert.org        objs = env['MAIN_OBJS'] + env['STATIC_OBJS']
5245517Snate@binkert.org        return super(Gem5, self).declare(env, objs)
5255517Snate@binkert.org
5265517Snate@binkert.org
5276143Snate@binkert.org# Children should have access
5285517Snate@binkert.orgExport('Blob')
5294762Snate@binkert.orgExport('GdbXml')
5305517Snate@binkert.orgExport('Source')
5315517Snate@binkert.orgExport('PySource')
5326143Snate@binkert.orgExport('SimObject')
5336143Snate@binkert.orgExport('ProtoBuf')
5345517Snate@binkert.orgExport('Executable')
5355517Snate@binkert.orgExport('UnitTest')
5365517Snate@binkert.orgExport('GTest')
5375517Snate@binkert.org
5385517Snate@binkert.org########################################################################
5395517Snate@binkert.org#
5405517Snate@binkert.org# Debug Flags
5415517Snate@binkert.org#
5425517Snate@binkert.orgdebug_flags = {}
5439338SAndreas.Sandberg@arm.comdef DebugFlag(name, desc=None):
5449338SAndreas.Sandberg@arm.com    if name in debug_flags:
5459338SAndreas.Sandberg@arm.com        raise AttributeError, "Flag %s already specified" % name
5469338SAndreas.Sandberg@arm.com    debug_flags[name] = (name, (), desc)
5479338SAndreas.Sandberg@arm.com
5489338SAndreas.Sandberg@arm.comdef CompoundFlag(name, flags, desc=None):
5498596Ssteve.reinhardt@amd.com    if name in debug_flags:
5508596Ssteve.reinhardt@amd.com        raise AttributeError, "Flag %s already specified" % name
5518596Ssteve.reinhardt@amd.com
5528596Ssteve.reinhardt@amd.com    compound = tuple(flags)
5538596Ssteve.reinhardt@amd.com    debug_flags[name] = (name, compound, desc)
5548596Ssteve.reinhardt@amd.com
5558596Ssteve.reinhardt@amd.comExport('DebugFlag')
5566143Snate@binkert.orgExport('CompoundFlag')
5575517Snate@binkert.org
5586654Snate@binkert.org########################################################################
5596654Snate@binkert.org#
5606654Snate@binkert.org# Set some compiler variables
5616654Snate@binkert.org#
5626654Snate@binkert.org
5636654Snate@binkert.org# Include file paths are rooted in this directory.  SCons will
5645517Snate@binkert.org# automatically expand '.' to refer to both the source directory and
5655517Snate@binkert.org# the corresponding build directory to pick up generated include
5665517Snate@binkert.org# files.
5678596Ssteve.reinhardt@amd.comenv.Append(CPPPATH=Dir('.'))
5688596Ssteve.reinhardt@amd.com
5694762Snate@binkert.orgfor extra_dir in extras_dir_list:
5704762Snate@binkert.org    env.Append(CPPPATH=Dir(extra_dir))
5714762Snate@binkert.org
5724762Snate@binkert.org# Workaround for bug in SCons version > 0.97d20071212
5734762Snate@binkert.org# Scons bug id: 2006 gem5 Bug id: 308
5744762Snate@binkert.orgfor root, dirs, files in os.walk(base_dir, topdown=True):
5757675Snate@binkert.org    Dir(root[len(base_dir) + 1:])
57610584Sandreas.hansson@arm.com
5774762Snate@binkert.org########################################################################
5784762Snate@binkert.org#
5794762Snate@binkert.org# Walk the tree and execute all SConscripts in subdirectories
5804762Snate@binkert.org#
5814382Sbinkertn@umich.edu
5824382Sbinkertn@umich.eduhere = Dir('.').srcnode().abspath
5835517Snate@binkert.orgfor root, dirs, files in os.walk(base_dir, topdown=True):
5846654Snate@binkert.org    if root == here:
5855517Snate@binkert.org        # we don't want to recurse back into this SConscript
5868126Sgblack@eecs.umich.edu        continue
5876654Snate@binkert.org
5887673Snate@binkert.org    if 'SConscript' in files:
5896654Snate@binkert.org        build_dir = joinpath(env['BUILDDIR'], root[len(base_dir) + 1:])
5906654Snate@binkert.org        Source.set_group(build_dir)
5916654Snate@binkert.org        SConscript(joinpath(root, 'SConscript'), variant_dir=build_dir)
5926654Snate@binkert.org
5936654Snate@binkert.orgfor extra_dir in extras_dir_list:
5946654Snate@binkert.org    prefix_len = len(dirname(extra_dir)) + 1
5956654Snate@binkert.org
5966669Snate@binkert.org    # Also add the corresponding build directory to pick up generated
5976669Snate@binkert.org    # include files.
5986669Snate@binkert.org    env.Append(CPPPATH=Dir(joinpath(env['BUILDDIR'], extra_dir[prefix_len:])))
5996669Snate@binkert.org
6006669Snate@binkert.org    for root, dirs, files in os.walk(extra_dir, topdown=True):
6016669Snate@binkert.org        # if build lives in the extras directory, don't walk down it
6026654Snate@binkert.org        if 'build' in dirs:
6037673Snate@binkert.org            dirs.remove('build')
6045517Snate@binkert.org
6058126Sgblack@eecs.umich.edu        if 'SConscript' in files:
6065798Snate@binkert.org            build_dir = joinpath(env['BUILDDIR'], root[prefix_len:])
6077756SAli.Saidi@ARM.com            SConscript(joinpath(root, 'SConscript'), variant_dir=build_dir)
6087816Ssteve.reinhardt@amd.com
6095798Snate@binkert.orgfor opt in export_vars:
6105798Snate@binkert.org    env.ConfigFile(opt)
6115517Snate@binkert.org
6125517Snate@binkert.orgdef makeTheISA(source, target, env):
6137673Snate@binkert.org    isas = [ src.get_contents() for src in source ]
6145517Snate@binkert.org    target_isa = env['TARGET_ISA']
6155517Snate@binkert.org    def define(isa):
6167673Snate@binkert.org        return isa.upper() + '_ISA'
6177673Snate@binkert.org
6185517Snate@binkert.org    def namespace(isa):
6195798Snate@binkert.org        return isa[0].upper() + isa[1:].lower() + 'ISA'
6205798Snate@binkert.org
6218333Snate@binkert.org
6227816Ssteve.reinhardt@amd.com    code = code_formatter()
6235798Snate@binkert.org    code('''\
6245798Snate@binkert.org#ifndef __CONFIG_THE_ISA_HH__
6254762Snate@binkert.org#define __CONFIG_THE_ISA_HH__
6264762Snate@binkert.org
6274762Snate@binkert.org''')
6284762Snate@binkert.org
6294762Snate@binkert.org    # create defines for the preprocessing and compile-time determination
6308596Ssteve.reinhardt@amd.com    for i,isa in enumerate(isas):
6315517Snate@binkert.org        code('#define $0 $1', define(isa), i + 1)
6325517Snate@binkert.org    code()
6335517Snate@binkert.org
6345517Snate@binkert.org    # create an enum for any run-time determination of the ISA, we
6355517Snate@binkert.org    # reuse the same name as the namespaces
6367673Snate@binkert.org    code('enum class Arch {')
6378596Ssteve.reinhardt@amd.com    for i,isa in enumerate(isas):
6387673Snate@binkert.org        if i + 1 == len(isas):
6395517Snate@binkert.org            code('  $0 = $1', namespace(isa), define(isa))
64010458Sandreas.hansson@arm.com        else:
64110458Sandreas.hansson@arm.com            code('  $0 = $1,', namespace(isa), define(isa))
64210458Sandreas.hansson@arm.com    code('};')
64310458Sandreas.hansson@arm.com
64410458Sandreas.hansson@arm.com    code('''
64510458Sandreas.hansson@arm.com
64610458Sandreas.hansson@arm.com#define THE_ISA ${{define(target_isa)}}
64710458Sandreas.hansson@arm.com#define TheISA ${{namespace(target_isa)}}
64810458Sandreas.hansson@arm.com#define THE_ISA_STR "${{target_isa}}"
64910458Sandreas.hansson@arm.com
65010458Sandreas.hansson@arm.com#endif // __CONFIG_THE_ISA_HH__''')
65110458Sandreas.hansson@arm.com
6528596Ssteve.reinhardt@amd.com    code.write(str(target[0]))
6535517Snate@binkert.org
6545517Snate@binkert.orgenv.Command('config/the_isa.hh', map(Value, all_isa_list),
6555517Snate@binkert.org            MakeAction(makeTheISA, Transform("CFG ISA", 0)))
6568596Ssteve.reinhardt@amd.com
6575517Snate@binkert.orgdef makeTheGPUISA(source, target, env):
6587673Snate@binkert.org    isas = [ src.get_contents() for src in source ]
6597673Snate@binkert.org    target_gpu_isa = env['TARGET_GPU_ISA']
6607673Snate@binkert.org    def define(isa):
6615517Snate@binkert.org        return isa.upper() + '_ISA'
6625517Snate@binkert.org
6635517Snate@binkert.org    def namespace(isa):
6645517Snate@binkert.org        return isa[0].upper() + isa[1:].lower() + 'ISA'
6655517Snate@binkert.org
6665517Snate@binkert.org
6675517Snate@binkert.org    code = code_formatter()
6687673Snate@binkert.org    code('''\
6697673Snate@binkert.org#ifndef __CONFIG_THE_GPU_ISA_HH__
6707673Snate@binkert.org#define __CONFIG_THE_GPU_ISA_HH__
6715517Snate@binkert.org
6728596Ssteve.reinhardt@amd.com''')
6735517Snate@binkert.org
6745517Snate@binkert.org    # create defines for the preprocessing and compile-time determination
6755517Snate@binkert.org    for i,isa in enumerate(isas):
6765517Snate@binkert.org        code('#define $0 $1', define(isa), i + 1)
6775517Snate@binkert.org    code()
6787673Snate@binkert.org
6797673Snate@binkert.org    # create an enum for any run-time determination of the ISA, we
6807673Snate@binkert.org    # reuse the same name as the namespaces
6815517Snate@binkert.org    code('enum class GPUArch {')
6828596Ssteve.reinhardt@amd.com    for i,isa in enumerate(isas):
6837675Snate@binkert.org        if i + 1 == len(isas):
6847675Snate@binkert.org            code('  $0 = $1', namespace(isa), define(isa))
6857675Snate@binkert.org        else:
6867675Snate@binkert.org            code('  $0 = $1,', namespace(isa), define(isa))
6877675Snate@binkert.org    code('};')
6887675Snate@binkert.org
6898596Ssteve.reinhardt@amd.com    code('''
6907675Snate@binkert.org
6917675Snate@binkert.org#define THE_GPU_ISA ${{define(target_gpu_isa)}}
6928596Ssteve.reinhardt@amd.com#define TheGpuISA ${{namespace(target_gpu_isa)}}
6938596Ssteve.reinhardt@amd.com#define THE_GPU_ISA_STR "${{target_gpu_isa}}"
6948596Ssteve.reinhardt@amd.com
6958596Ssteve.reinhardt@amd.com#endif // __CONFIG_THE_GPU_ISA_HH__''')
6968596Ssteve.reinhardt@amd.com
6978596Ssteve.reinhardt@amd.com    code.write(str(target[0]))
6988596Ssteve.reinhardt@amd.com
6998596Ssteve.reinhardt@amd.comenv.Command('config/the_gpu_isa.hh', map(Value, all_gpu_isa_list),
70010454SCurtis.Dunham@arm.com            MakeAction(makeTheGPUISA, Transform("CFG ISA", 0)))
70110454SCurtis.Dunham@arm.com
70210454SCurtis.Dunham@arm.com########################################################################
70310454SCurtis.Dunham@arm.com#
7048596Ssteve.reinhardt@amd.com# Prevent any SimObjects from being added after this point, they
7054762Snate@binkert.org# should all have been added in the SConscripts above
7066143Snate@binkert.org#
7076143Snate@binkert.orgSimObject.fixed = True
7086143Snate@binkert.org
7094762Snate@binkert.orgclass DictImporter(object):
7104762Snate@binkert.org    '''This importer takes a dictionary of arbitrary module names that
7114762Snate@binkert.org    map to arbitrary filenames.'''
7127756SAli.Saidi@ARM.com    def __init__(self, modules):
7138596Ssteve.reinhardt@amd.com        self.modules = modules
7144762Snate@binkert.org        self.installed = set()
71510454SCurtis.Dunham@arm.com
7164762Snate@binkert.org    def __del__(self):
71710458Sandreas.hansson@arm.com        self.unload()
71810458Sandreas.hansson@arm.com
71910458Sandreas.hansson@arm.com    def unload(self):
72010458Sandreas.hansson@arm.com        import sys
72110458Sandreas.hansson@arm.com        for module in self.installed:
72210458Sandreas.hansson@arm.com            del sys.modules[module]
72310458Sandreas.hansson@arm.com        self.installed = set()
72410458Sandreas.hansson@arm.com
72510458Sandreas.hansson@arm.com    def find_module(self, fullname, path):
72610458Sandreas.hansson@arm.com        if fullname == 'm5.defines':
72710458Sandreas.hansson@arm.com            return self
72810458Sandreas.hansson@arm.com
72910458Sandreas.hansson@arm.com        if fullname == 'm5.objects':
73010458Sandreas.hansson@arm.com            return self
73110458Sandreas.hansson@arm.com
73210458Sandreas.hansson@arm.com        if fullname.startswith('_m5'):
73310458Sandreas.hansson@arm.com            return None
73410458Sandreas.hansson@arm.com
73510458Sandreas.hansson@arm.com        source = self.modules.get(fullname, None)
73610458Sandreas.hansson@arm.com        if source is not None and fullname.startswith('m5.objects'):
73710458Sandreas.hansson@arm.com            return self
73810458Sandreas.hansson@arm.com
73910458Sandreas.hansson@arm.com        return None
74010458Sandreas.hansson@arm.com
74110458Sandreas.hansson@arm.com    def load_module(self, fullname):
74210458Sandreas.hansson@arm.com        mod = imp.new_module(fullname)
74310458Sandreas.hansson@arm.com        sys.modules[fullname] = mod
74410458Sandreas.hansson@arm.com        self.installed.add(fullname)
74510458Sandreas.hansson@arm.com
74610458Sandreas.hansson@arm.com        mod.__loader__ = self
74710458Sandreas.hansson@arm.com        if fullname == 'm5.objects':
74810458Sandreas.hansson@arm.com            mod.__path__ = fullname.split('.')
74910458Sandreas.hansson@arm.com            return mod
75010458Sandreas.hansson@arm.com
75110458Sandreas.hansson@arm.com        if fullname == 'm5.defines':
75210458Sandreas.hansson@arm.com            mod.__dict__['buildEnv'] = m5.util.SmartDict(build_env)
75310458Sandreas.hansson@arm.com            return mod
75410458Sandreas.hansson@arm.com
75510458Sandreas.hansson@arm.com        source = self.modules[fullname]
75610458Sandreas.hansson@arm.com        if source.modname == '__init__':
75710458Sandreas.hansson@arm.com            mod.__path__ = source.modpath
75810458Sandreas.hansson@arm.com        mod.__file__ = source.abspath
75910458Sandreas.hansson@arm.com
76010458Sandreas.hansson@arm.com        exec file(source.abspath, 'r') in mod.__dict__
76110458Sandreas.hansson@arm.com
76210458Sandreas.hansson@arm.com        return mod
76310458Sandreas.hansson@arm.com
76410458Sandreas.hansson@arm.comimport m5.SimObject
76510458Sandreas.hansson@arm.comimport m5.params
76610584Sandreas.hansson@arm.comfrom m5.util import code_formatter
76710458Sandreas.hansson@arm.com
76810458Sandreas.hansson@arm.comm5.SimObject.clear()
76910458Sandreas.hansson@arm.comm5.params.clear()
77010458Sandreas.hansson@arm.com
77110458Sandreas.hansson@arm.com# install the python importer so we can grab stuff from the source
7728596Ssteve.reinhardt@amd.com# tree itself.  We can't have SimObjects added after this point or
7735463Snate@binkert.org# else we won't know about them for the rest of the stuff.
77410584Sandreas.hansson@arm.comimporter = DictImporter(PySource.modules)
7758596Ssteve.reinhardt@amd.comsys.meta_path[0:0] = [ importer ]
7765463Snate@binkert.org
7777756SAli.Saidi@ARM.com# import all sim objects so we can populate the all_objects list
7788596Ssteve.reinhardt@amd.com# make sure that we're working with a list, then let's sort it
7794762Snate@binkert.orgfor modname in SimObject.modnames:
78010454SCurtis.Dunham@arm.com    exec('from m5.objects import %s' % modname)
7817677Snate@binkert.org
7824762Snate@binkert.org# we need to unload all of the currently imported modules so that they
7834762Snate@binkert.org# will be re-imported the next time the sconscript is run
7846143Snate@binkert.orgimporter.unload()
7856143Snate@binkert.orgsys.meta_path.remove(importer)
7866143Snate@binkert.org
7874762Snate@binkert.orgsim_objects = m5.SimObject.allClasses
7884762Snate@binkert.orgall_enums = m5.params.allEnums
7897756SAli.Saidi@ARM.com
7907816Ssteve.reinhardt@amd.comfor name,obj in sorted(sim_objects.iteritems()):
7914762Snate@binkert.org    for param in obj._params.local.values():
79210454SCurtis.Dunham@arm.com        # load the ptype attribute now because it depends on the
7934762Snate@binkert.org        # current version of SimObject.allClasses, but when scons
7944762Snate@binkert.org        # actually uses the value, all versions of
7954762Snate@binkert.org        # SimObject.allClasses will have been loaded
7967756SAli.Saidi@ARM.com        param.ptype
7978596Ssteve.reinhardt@amd.com
7984762Snate@binkert.org########################################################################
79910454SCurtis.Dunham@arm.com#
8004762Snate@binkert.org# calculate extra dependencies
8017677Snate@binkert.org#
8027756SAli.Saidi@ARM.commodule_depends = ["m5", "m5.SimObject", "m5.params"]
8038596Ssteve.reinhardt@amd.comdepends = [ PySource.modules[dep].snode for dep in module_depends ]
8047675Snate@binkert.orgdepends.sort(key = lambda x: x.name)
80510454SCurtis.Dunham@arm.com
8067677Snate@binkert.org########################################################################
8075517Snate@binkert.org#
8088596Ssteve.reinhardt@amd.com# Commands for the basic automatically generated python files
80910584Sandreas.hansson@arm.com#
8109248SAndreas.Sandberg@arm.com
8119248SAndreas.Sandberg@arm.com# Generate Python file containing a dict specifying the current
8128596Ssteve.reinhardt@amd.com# buildEnv flags.
8138596Ssteve.reinhardt@amd.comdef makeDefinesPyFile(target, source, env):
8148596Ssteve.reinhardt@amd.com    build_env = source[0].get_contents()
8159248SAndreas.Sandberg@arm.com
8168596Ssteve.reinhardt@amd.com    code = code_formatter()
8174762Snate@binkert.org    code("""
8187674Snate@binkert.orgimport _m5.core
8197674Snate@binkert.orgimport m5.util
8207674Snate@binkert.org
8217674Snate@binkert.orgbuildEnv = m5.util.SmartDict($build_env)
8227674Snate@binkert.org
8237674Snate@binkert.orgcompileDate = _m5.core.compileDate
8247674Snate@binkert.org_globals = globals()
8257674Snate@binkert.orgfor key,val in _m5.core.__dict__.iteritems():
8267674Snate@binkert.org    if key.startswith('flag_'):
8277674Snate@binkert.org        flag = key[5:]
8287674Snate@binkert.org        _globals[flag] = val
8297674Snate@binkert.orgdel _globals
8307674Snate@binkert.org""")
8317674Snate@binkert.org    code.write(target[0].abspath)
83211308Santhony.gutierrez@amd.com
8334762Snate@binkert.orgdefines_info = Value(build_env)
8346143Snate@binkert.org# Generate a file with all of the compile options in it
8356143Snate@binkert.orgenv.Command('python/m5/defines.py', defines_info,
8367756SAli.Saidi@ARM.com            MakeAction(makeDefinesPyFile, Transform("DEFINES", 0)))
8377816Ssteve.reinhardt@amd.comPySource('m5', 'python/m5/defines.py')
8388235Snate@binkert.org
8398596Ssteve.reinhardt@amd.com# Generate python file containing info about the M5 source code
8407756SAli.Saidi@ARM.comdef makeInfoPyFile(target, source, env):
8417816Ssteve.reinhardt@amd.com    code = code_formatter()
84210454SCurtis.Dunham@arm.com    for src in source:
8438235Snate@binkert.org        data = ''.join(file(src.srcnode().abspath, 'r').xreadlines())
8444382Sbinkertn@umich.edu        code('$src = ${{repr(data)}}')
8459396Sandreas.hansson@arm.com    code.write(str(target[0]))
8469396Sandreas.hansson@arm.com
8479396Sandreas.hansson@arm.com# Generate a file that wraps the basic top level files
8489396Sandreas.hansson@arm.comenv.Command('python/m5/info.py',
8499396Sandreas.hansson@arm.com            [ '#/COPYING', '#/LICENSE', '#/README', ],
8509396Sandreas.hansson@arm.com            MakeAction(makeInfoPyFile, Transform("INFO")))
8519396Sandreas.hansson@arm.comPySource('m5', 'python/m5/info.py')
8529396Sandreas.hansson@arm.com
8539396Sandreas.hansson@arm.com########################################################################
8549396Sandreas.hansson@arm.com#
8559396Sandreas.hansson@arm.com# Create all of the SimObject param headers and enum headers
8569396Sandreas.hansson@arm.com#
85710454SCurtis.Dunham@arm.com
8589396Sandreas.hansson@arm.comdef createSimObjectParamStruct(target, source, env):
8599396Sandreas.hansson@arm.com    assert len(target) == 1 and len(source) == 1
8609396Sandreas.hansson@arm.com
8619396Sandreas.hansson@arm.com    name = source[0].get_text_contents()
8629396Sandreas.hansson@arm.com    obj = sim_objects[name]
8639396Sandreas.hansson@arm.com
8648232Snate@binkert.org    code = code_formatter()
8658232Snate@binkert.org    obj.cxx_param_decl(code)
8668232Snate@binkert.org    code.write(target[0].abspath)
8678232Snate@binkert.org
8688232Snate@binkert.orgdef createSimObjectCxxConfig(is_header):
8696229Snate@binkert.org    def body(target, source, env):
87010455SCurtis.Dunham@arm.com        assert len(target) == 1 and len(source) == 1
8716229Snate@binkert.org
87210455SCurtis.Dunham@arm.com        name = str(source[0].get_contents())
87310455SCurtis.Dunham@arm.com        obj = sim_objects[name]
87410455SCurtis.Dunham@arm.com
8755517Snate@binkert.org        code = code_formatter()
8765517Snate@binkert.org        obj.cxx_config_param_file(code, is_header)
8777673Snate@binkert.org        code.write(target[0].abspath)
8785517Snate@binkert.org    return body
87910455SCurtis.Dunham@arm.com
8805517Snate@binkert.orgdef createEnumStrings(target, source, env):
8815517Snate@binkert.org    assert len(target) == 1 and len(source) == 2
8828232Snate@binkert.org
88310455SCurtis.Dunham@arm.com    name = source[0].get_text_contents()
88410455SCurtis.Dunham@arm.com    use_python = source[1].read()
88510455SCurtis.Dunham@arm.com    obj = all_enums[name]
8867673Snate@binkert.org
8877673Snate@binkert.org    code = code_formatter()
88810455SCurtis.Dunham@arm.com    obj.cxx_def(code)
88910455SCurtis.Dunham@arm.com    if use_python:
89010455SCurtis.Dunham@arm.com        obj.pybind_def(code)
8915517Snate@binkert.org    code.write(target[0].abspath)
89210455SCurtis.Dunham@arm.com
89310455SCurtis.Dunham@arm.comdef createEnumDecls(target, source, env):
89410455SCurtis.Dunham@arm.com    assert len(target) == 1 and len(source) == 1
89510455SCurtis.Dunham@arm.com
89610455SCurtis.Dunham@arm.com    name = source[0].get_text_contents()
89710455SCurtis.Dunham@arm.com    obj = all_enums[name]
89810455SCurtis.Dunham@arm.com
89910455SCurtis.Dunham@arm.com    code = code_formatter()
90010685Sandreas.hansson@arm.com    obj.cxx_decl(code)
90110455SCurtis.Dunham@arm.com    code.write(target[0].abspath)
90210685Sandreas.hansson@arm.com
90310455SCurtis.Dunham@arm.comdef createSimObjectPyBindWrapper(target, source, env):
9045517Snate@binkert.org    name = source[0].get_text_contents()
90510455SCurtis.Dunham@arm.com    obj = sim_objects[name]
9068232Snate@binkert.org
9078232Snate@binkert.org    code = code_formatter()
9085517Snate@binkert.org    obj.pybind_decl(code)
9097673Snate@binkert.org    code.write(target[0].abspath)
9105517Snate@binkert.org
9118232Snate@binkert.org# Generate all of the SimObject param C++ struct header files
9128232Snate@binkert.orgparams_hh_files = []
9135517Snate@binkert.orgfor name,simobj in sorted(sim_objects.iteritems()):
9148232Snate@binkert.org    py_source = PySource.modules[simobj.__module__]
9158232Snate@binkert.org    extra_deps = [ py_source.tnode ]
9168232Snate@binkert.org
9177673Snate@binkert.org    hh_file = File('params/%s.hh' % name)
9185517Snate@binkert.org    params_hh_files.append(hh_file)
9195517Snate@binkert.org    env.Command(hh_file, Value(name),
9207673Snate@binkert.org                MakeAction(createSimObjectParamStruct, Transform("SO PARAM")))
9215517Snate@binkert.org    env.Depends(hh_file, depends + extra_deps)
92210455SCurtis.Dunham@arm.com
9235517Snate@binkert.org# C++ parameter description files
9245517Snate@binkert.orgif GetOption('with_cxx_config'):
9258232Snate@binkert.org    for name,simobj in sorted(sim_objects.iteritems()):
9268232Snate@binkert.org        py_source = PySource.modules[simobj.__module__]
9275517Snate@binkert.org        extra_deps = [ py_source.tnode ]
9288232Snate@binkert.org
9298232Snate@binkert.org        cxx_config_hh_file = File('cxx_config/%s.hh' % name)
9305517Snate@binkert.org        cxx_config_cc_file = File('cxx_config/%s.cc' % name)
9318232Snate@binkert.org        env.Command(cxx_config_hh_file, Value(name),
9328232Snate@binkert.org                    MakeAction(createSimObjectCxxConfig(True),
9338232Snate@binkert.org                    Transform("CXXCPRHH")))
9345517Snate@binkert.org        env.Command(cxx_config_cc_file, Value(name),
9358232Snate@binkert.org                    MakeAction(createSimObjectCxxConfig(False),
9368232Snate@binkert.org                    Transform("CXXCPRCC")))
9378232Snate@binkert.org        env.Depends(cxx_config_hh_file, depends + extra_deps +
9388232Snate@binkert.org                    [File('params/%s.hh' % name), File('sim/cxx_config.hh')])
9398232Snate@binkert.org        env.Depends(cxx_config_cc_file, depends + extra_deps +
9408232Snate@binkert.org                    [cxx_config_hh_file])
9415517Snate@binkert.org        Source(cxx_config_cc_file)
9428232Snate@binkert.org
9438232Snate@binkert.org    cxx_config_init_cc_file = File('cxx_config/init.cc')
9445517Snate@binkert.org
9458232Snate@binkert.org    def createCxxConfigInitCC(target, source, env):
9467673Snate@binkert.org        assert len(target) == 1 and len(source) == 1
9475517Snate@binkert.org
9487673Snate@binkert.org        code = code_formatter()
9495517Snate@binkert.org
9508232Snate@binkert.org        for name,simobj in sorted(sim_objects.iteritems()):
9518232Snate@binkert.org            if not hasattr(simobj, 'abstract') or not simobj.abstract:
9528232Snate@binkert.org                code('#include "cxx_config/${name}.hh"')
9535192Ssaidi@eecs.umich.edu        code()
95410454SCurtis.Dunham@arm.com        code('void cxxConfigInit()')
95510454SCurtis.Dunham@arm.com        code('{')
9568232Snate@binkert.org        code.indent()
95710455SCurtis.Dunham@arm.com        for name,simobj in sorted(sim_objects.iteritems()):
95810455SCurtis.Dunham@arm.com            not_abstract = not hasattr(simobj, 'abstract') or \
95910455SCurtis.Dunham@arm.com                not simobj.abstract
96010455SCurtis.Dunham@arm.com            if not_abstract and 'type' in simobj.__dict__:
96110455SCurtis.Dunham@arm.com                code('cxx_config_directory["${name}"] = '
96210455SCurtis.Dunham@arm.com                     '${name}CxxConfigParams::makeDirectoryEntry();')
9635192Ssaidi@eecs.umich.edu        code.dedent()
96411077SCurtis.Dunham@arm.com        code('}')
96511077SCurtis.Dunham@arm.com        code.write(target[0].abspath)
96611077SCurtis.Dunham@arm.com
96711077SCurtis.Dunham@arm.com    py_source = PySource.modules[simobj.__module__]
96811077SCurtis.Dunham@arm.com    extra_deps = [ py_source.tnode ]
9697674Snate@binkert.org    env.Command(cxx_config_init_cc_file, Value(name),
9705522Snate@binkert.org        MakeAction(createCxxConfigInitCC, Transform("CXXCINIT")))
9715522Snate@binkert.org    cxx_param_hh_files = ["cxx_config/%s.hh" % simobj
9727674Snate@binkert.org        for name,simobj in sorted(sim_objects.iteritems())
9737674Snate@binkert.org        if not hasattr(simobj, 'abstract') or not simobj.abstract]
9747674Snate@binkert.org    Depends(cxx_config_init_cc_file, cxx_param_hh_files +
9757674Snate@binkert.org            [File('sim/cxx_config.hh')])
9767674Snate@binkert.org    Source(cxx_config_init_cc_file)
9777674Snate@binkert.org
9787674Snate@binkert.org# Generate all enum header files
9797674Snate@binkert.orgfor name,enum in sorted(all_enums.iteritems()):
9805522Snate@binkert.org    py_source = PySource.modules[enum.__module__]
9815522Snate@binkert.org    extra_deps = [ py_source.tnode ]
9825522Snate@binkert.org
9835517Snate@binkert.org    cc_file = File('enums/%s.cc' % name)
9845522Snate@binkert.org    env.Command(cc_file, [Value(name), Value(env['USE_PYTHON'])],
9855517Snate@binkert.org                MakeAction(createEnumStrings, Transform("ENUM STR")))
9866143Snate@binkert.org    env.Depends(cc_file, depends + extra_deps)
9876727Ssteve.reinhardt@amd.com    Source(cc_file)
9885522Snate@binkert.org
9895522Snate@binkert.org    hh_file = File('enums/%s.hh' % name)
9905522Snate@binkert.org    env.Command(hh_file, Value(name),
9917674Snate@binkert.org                MakeAction(createEnumDecls, Transform("ENUMDECL")))
9925517Snate@binkert.org    env.Depends(hh_file, depends + extra_deps)
9937673Snate@binkert.org
9947673Snate@binkert.org# Generate SimObject Python bindings wrapper files
9957674Snate@binkert.orgif env['USE_PYTHON']:
9967673Snate@binkert.org    for name,simobj in sorted(sim_objects.iteritems()):
9977674Snate@binkert.org        py_source = PySource.modules[simobj.__module__]
9987674Snate@binkert.org        extra_deps = [ py_source.tnode ]
9998946Sandreas.hansson@arm.com        cc_file = File('python/_m5/param_%s.cc' % name)
10007674Snate@binkert.org        env.Command(cc_file, Value(name),
10017674Snate@binkert.org                    MakeAction(createSimObjectPyBindWrapper,
10027674Snate@binkert.org                               Transform("SO PyBind")))
10035522Snate@binkert.org        env.Depends(cc_file, depends + extra_deps)
10045522Snate@binkert.org        Source(cc_file)
10057674Snate@binkert.org
10067674Snate@binkert.org# Build all protocol buffers if we have got protoc and protobuf available
100711308Santhony.gutierrez@amd.comif env['HAVE_PROTOBUF']:
10087674Snate@binkert.org    for proto in ProtoBuf.all:
10097673Snate@binkert.org        # Use both the source and header as the target, and the .proto
10107674Snate@binkert.org        # file as the source. When executing the protoc compiler, also
10117674Snate@binkert.org        # specify the proto_path to avoid having the generated files
10127674Snate@binkert.org        # include the path.
10137674Snate@binkert.org        env.Command([proto.cc_file, proto.hh_file], proto.tnode,
10147674Snate@binkert.org                    MakeAction('$PROTOC --cpp_out ${TARGET.dir} '
10157674Snate@binkert.org                               '--proto_path ${SOURCE.dir} $SOURCE',
10167674Snate@binkert.org                               Transform("PROTOC")))
10177674Snate@binkert.org
10187811Ssteve.reinhardt@amd.com        # Add the C++ source file
10197674Snate@binkert.org        Source(proto.cc_file, tags=proto.tags)
10207673Snate@binkert.orgelif ProtoBuf.all:
10215522Snate@binkert.org    print('Got protobuf to build, but lacks support!')
10226143Snate@binkert.org    Exit(1)
102310453SAndrew.Bardsley@arm.com
10247816Ssteve.reinhardt@amd.com#
102510454SCurtis.Dunham@arm.com# Handle debug flags
102610453SAndrew.Bardsley@arm.com#
10274382Sbinkertn@umich.edudef makeDebugFlagCC(target, source, env):
10284382Sbinkertn@umich.edu    assert(len(target) == 1 and len(source) == 1)
10294382Sbinkertn@umich.edu
10304382Sbinkertn@umich.edu    code = code_formatter()
10314382Sbinkertn@umich.edu
10324382Sbinkertn@umich.edu    # delay definition of CompoundFlags until after all the definition
10334382Sbinkertn@umich.edu    # of all constituent SimpleFlags
10344382Sbinkertn@umich.edu    comp_code = code_formatter()
103510196SCurtis.Dunham@arm.com
10364382Sbinkertn@umich.edu    # file header
103710196SCurtis.Dunham@arm.com    code('''
103810196SCurtis.Dunham@arm.com/*
103910196SCurtis.Dunham@arm.com * DO NOT EDIT THIS FILE! Automatically generated by SCons.
104010196SCurtis.Dunham@arm.com */
104110196SCurtis.Dunham@arm.com
104210196SCurtis.Dunham@arm.com#include "base/debug.hh"
104310196SCurtis.Dunham@arm.com
1044955SN/Anamespace Debug {
10452655Sstever@eecs.umich.edu
10462655Sstever@eecs.umich.edu''')
10472655Sstever@eecs.umich.edu
10482655Sstever@eecs.umich.edu    for name, flag in sorted(source[0].read().iteritems()):
104910196SCurtis.Dunham@arm.com        n, compound, desc = flag
10505601Snate@binkert.org        assert n == name
10515601Snate@binkert.org
105210196SCurtis.Dunham@arm.com        if not compound:
105310196SCurtis.Dunham@arm.com            code('SimpleFlag $name("$name", "$desc");')
105410196SCurtis.Dunham@arm.com        else:
10555522Snate@binkert.org            comp_code('CompoundFlag $name("$name", "$desc",')
10565863Snate@binkert.org            comp_code.indent()
10575601Snate@binkert.org            last = len(compound) - 1
10585601Snate@binkert.org            for i,flag in enumerate(compound):
10595601Snate@binkert.org                if i != last:
10605863Snate@binkert.org                    comp_code('&$flag,')
10619556Sandreas.hansson@arm.com                else:
10629556Sandreas.hansson@arm.com                    comp_code('&$flag);')
10639556Sandreas.hansson@arm.com            comp_code.dedent()
10649556Sandreas.hansson@arm.com
10659556Sandreas.hansson@arm.com    code.append(comp_code)
10665559Snate@binkert.org    code()
10679556Sandreas.hansson@arm.com    code('} // namespace Debug')
10689618Ssteve.reinhardt@amd.com
10699618Ssteve.reinhardt@amd.com    code.write(str(target[0]))
10709618Ssteve.reinhardt@amd.com
107110238Sandreas.hansson@arm.comdef makeDebugFlagHH(target, source, env):
107210878Sandreas.hansson@arm.com    assert(len(target) == 1 and len(source) == 1)
107311294Sandreas.hansson@arm.com
107411294Sandreas.hansson@arm.com    val = eval(source[0].get_contents())
107510457Sandreas.hansson@arm.com    name, compound, desc = val
107610457Sandreas.hansson@arm.com
107710457Sandreas.hansson@arm.com    code = code_formatter()
107810457Sandreas.hansson@arm.com
107910457Sandreas.hansson@arm.com    # file header boilerplate
108010457Sandreas.hansson@arm.com    code('''\
108110457Sandreas.hansson@arm.com/*
108210457Sandreas.hansson@arm.com * DO NOT EDIT THIS FILE! Automatically generated by SCons.
108310457Sandreas.hansson@arm.com */
10848737Skoansin.tan@gmail.com
108511294Sandreas.hansson@arm.com#ifndef __DEBUG_${name}_HH__
108611294Sandreas.hansson@arm.com#define __DEBUG_${name}_HH__
108711294Sandreas.hansson@arm.com
108810278SAndreas.Sandberg@ARM.comnamespace Debug {
108910457Sandreas.hansson@arm.com''')
109010457Sandreas.hansson@arm.com
109110457Sandreas.hansson@arm.com    if compound:
109210457Sandreas.hansson@arm.com        code('class CompoundFlag;')
109310457Sandreas.hansson@arm.com    code('class SimpleFlag;')
109410457Sandreas.hansson@arm.com
10958945Ssteve.reinhardt@amd.com    if compound:
109610686SAndreas.Sandberg@ARM.com        code('extern CompoundFlag $name;')
109710686SAndreas.Sandberg@ARM.com        for flag in compound:
109810686SAndreas.Sandberg@ARM.com            code('extern SimpleFlag $flag;')
109910686SAndreas.Sandberg@ARM.com    else:
110010686SAndreas.Sandberg@ARM.com        code('extern SimpleFlag $name;')
110110686SAndreas.Sandberg@ARM.com
11028945Ssteve.reinhardt@amd.com    code('''
11036143Snate@binkert.org}
11046143Snate@binkert.org
11056143Snate@binkert.org#endif // __DEBUG_${name}_HH__
11066143Snate@binkert.org''')
11076143Snate@binkert.org
11086143Snate@binkert.org    code.write(str(target[0]))
11096143Snate@binkert.org
11108945Ssteve.reinhardt@amd.comfor name,flag in sorted(debug_flags.iteritems()):
11118945Ssteve.reinhardt@amd.com    n, compound, desc = flag
11126143Snate@binkert.org    assert n == name
11136143Snate@binkert.org
11146143Snate@binkert.org    hh_file = 'debug/%s.hh' % name
11156143Snate@binkert.org    env.Command(hh_file, Value(flag),
11166143Snate@binkert.org                MakeAction(makeDebugFlagHH, Transform("TRACING", 0)))
11176143Snate@binkert.org
11186143Snate@binkert.orgenv.Command('debug/flags.cc', Value(debug_flags),
11196143Snate@binkert.org            MakeAction(makeDebugFlagCC, Transform("TRACING", 0)))
11206143Snate@binkert.orgSource('debug/flags.cc')
11216143Snate@binkert.org
11226143Snate@binkert.org# version tags
11236143Snate@binkert.orgtags = \
11246143Snate@binkert.orgenv.Command('sim/tags.cc', None,
112510453SAndrew.Bardsley@arm.com            MakeAction('util/cpt_upgrader.py --get-cc-file > $TARGET',
112610453SAndrew.Bardsley@arm.com                       Transform("VER TAGS")))
112710453SAndrew.Bardsley@arm.comenv.AlwaysBuild(tags)
112810453SAndrew.Bardsley@arm.com
112910453SAndrew.Bardsley@arm.com# Embed python files.  All .py files that have been indicated by a
113010453SAndrew.Bardsley@arm.com# PySource() call in a SConscript need to be embedded into the M5
113110453SAndrew.Bardsley@arm.com# library.  To do that, we compile the file to byte code, marshal the
113210453SAndrew.Bardsley@arm.com# byte code, compress it, and then generate a c++ file that
113310453SAndrew.Bardsley@arm.com# inserts the result into an array.
11346143Snate@binkert.orgdef embedPyFile(target, source, env):
11356143Snate@binkert.org    def c_str(string):
11366143Snate@binkert.org        if string is None:
113710453SAndrew.Bardsley@arm.com            return "0"
11386143Snate@binkert.org        return '"%s"' % string
11396240Snate@binkert.org
11405554Snate@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'''
11435797Snate@binkert.org
11445797Snate@binkert.org    src = file(str(source[0]), 'r').read()
11455522Snate@binkert.org
11465601Snate@binkert.org    pysource = PySource.tnodes[source[0]]
11478233Snate@binkert.org    compiled = compile(src, pysource.abspath, 'exec')
11488233Snate@binkert.org    marshalled = marshal.dumps(compiled)
11498235Snate@binkert.org    compressed = zlib.compress(marshalled)
11508235Snate@binkert.org    data = compressed
11518235Snate@binkert.org    sym = pysource.symname
11528235Snate@binkert.org
11539003SAli.Saidi@ARM.com    code = code_formatter()
11549003SAli.Saidi@ARM.com    code('''\
115510196SCurtis.Dunham@arm.com#include "sim/init.hh"
115610196SCurtis.Dunham@arm.com
11578235Snate@binkert.orgnamespace {
11586143Snate@binkert.org
11592655Sstever@eecs.umich.edu''')
11606143Snate@binkert.org    blobToCpp(data, 'data_' + sym, code)
11616143Snate@binkert.org    code('''\
11628233Snate@binkert.org
11636143Snate@binkert.org
11646143Snate@binkert.orgEmbeddedPython embedded_${sym}(
11654007Ssaidi@eecs.umich.edu    ${{c_str(pysource.arcname)}},
11664596Sbinkertn@umich.edu    ${{c_str(pysource.abspath)}},
11674007Ssaidi@eecs.umich.edu    ${{c_str(pysource.modpath)}},
11684596Sbinkertn@umich.edu    data_${sym},
11697756SAli.Saidi@ARM.com    ${{len(data)}},
11707816Ssteve.reinhardt@amd.com    ${{len(marshalled)}});
11718334Snate@binkert.org
11728334Snate@binkert.org} // anonymous namespace
11738334Snate@binkert.org''')
11748334Snate@binkert.org    code.write(str(target[0]))
11755601Snate@binkert.org
117610196SCurtis.Dunham@arm.comfor source in PySource.all:
11772655Sstever@eecs.umich.edu    env.Command(source.cpp, source.tnode,
11789225Sandreas.hansson@arm.com                MakeAction(embedPyFile, Transform("EMBED PY")))
11799225Sandreas.hansson@arm.com    Source(source.cpp, tags=source.tags, add_tags='python')
11809226Sandreas.hansson@arm.com
11819226Sandreas.hansson@arm.com########################################################################
11829225Sandreas.hansson@arm.com#
11839226Sandreas.hansson@arm.com# Define binaries.  Each different build type (debug, opt, etc.) gets
11849226Sandreas.hansson@arm.com# a slightly different build environment.
11859226Sandreas.hansson@arm.com#
11869226Sandreas.hansson@arm.com
11879226Sandreas.hansson@arm.com# List of constructed environments to pass back to SConstruct
11889226Sandreas.hansson@arm.comdate_source = Source('base/date.cc', tags=[])
11899225Sandreas.hansson@arm.com
11909227Sandreas.hansson@arm.comgem5_binary = Gem5('gem5')
11919227Sandreas.hansson@arm.com
11929227Sandreas.hansson@arm.com# Function to create a new build environment as clone of current
11939227Sandreas.hansson@arm.com# environment 'env' with modified object suffix and optional stripped
11948946Sandreas.hansson@arm.com# binary.  Additional keyword arguments are appended to corresponding
11953918Ssaidi@eecs.umich.edu# build environment vars.
11969225Sandreas.hansson@arm.comdef makeEnv(env, label, objsfx, strip=False, disable_partial=False, **kwargs):
11973918Ssaidi@eecs.umich.edu    # SCons doesn't know to append a library suffix when there is a '.' in the
11989225Sandreas.hansson@arm.com    # name.  Use '_' instead.
11999225Sandreas.hansson@arm.com    libname = 'gem5_' + label
12009227Sandreas.hansson@arm.com    secondary_exename = 'm5.' + label
12019227Sandreas.hansson@arm.com
12029227Sandreas.hansson@arm.com    new_env = env.Clone(OBJSUFFIX=objsfx, SHOBJSUFFIX=objsfx + 's')
12039226Sandreas.hansson@arm.com    new_env.Label = label
12049225Sandreas.hansson@arm.com    new_env.Append(**kwargs)
12059227Sandreas.hansson@arm.com
12069227Sandreas.hansson@arm.com    lib_sources = Source.all.with_tag('gem5 lib')
12079227Sandreas.hansson@arm.com
12089227Sandreas.hansson@arm.com    # Without Python, leave out all Python content from the library
12098946Sandreas.hansson@arm.com    # builds.  The option doesn't affect gem5 built as a program
12109225Sandreas.hansson@arm.com    if GetOption('without_python'):
12119226Sandreas.hansson@arm.com        lib_sources = lib_sources.without_tag('python')
12129226Sandreas.hansson@arm.com
12139226Sandreas.hansson@arm.com    static_objs = []
12143515Ssaidi@eecs.umich.edu    shared_objs = []
12153918Ssaidi@eecs.umich.edu
12164762Snate@binkert.org    for s in lib_sources.with_tag(Source.ungrouped_tag):
12173515Ssaidi@eecs.umich.edu        static_objs.append(s.static(new_env))
12188881Smarc.orr@gmail.com        shared_objs.append(s.shared(new_env))
12198881Smarc.orr@gmail.com
12208881Smarc.orr@gmail.com    for group in Source.source_groups:
12218881Smarc.orr@gmail.com        srcs = lib_sources.with_tag(Source.link_group_tag(group))
12228881Smarc.orr@gmail.com        if not srcs:
12239226Sandreas.hansson@arm.com            continue
12249226Sandreas.hansson@arm.com
12259226Sandreas.hansson@arm.com        group_static = [ s.static(new_env) for s in srcs ]
12268881Smarc.orr@gmail.com        group_shared = [ s.shared(new_env) for s in srcs ]
12278881Smarc.orr@gmail.com
12288881Smarc.orr@gmail.com        # If partial linking is disabled, add these sources to the build
12298881Smarc.orr@gmail.com        # directly, and short circuit this loop.
12308881Smarc.orr@gmail.com        if disable_partial:
12318881Smarc.orr@gmail.com            static_objs.extend(group_static)
12328881Smarc.orr@gmail.com            shared_objs.extend(group_shared)
12338881Smarc.orr@gmail.com            continue
12348881Smarc.orr@gmail.com
12358881Smarc.orr@gmail.com        # Set up the static partially linked objects.
12368881Smarc.orr@gmail.com        file_name = new_env.subst("${OBJPREFIX}lib${OBJSUFFIX}.partial")
12378881Smarc.orr@gmail.com        target = File(joinpath(group, file_name))
12388881Smarc.orr@gmail.com        partial = env.PartialStatic(target=target, source=group_static)
12398881Smarc.orr@gmail.com        static_objs.extend(partial)
12408881Smarc.orr@gmail.com
12418881Smarc.orr@gmail.com        # Set up the shared partially linked objects.
124210196SCurtis.Dunham@arm.com        file_name = new_env.subst("${SHOBJPREFIX}lib${SHOBJSUFFIX}.partial")
124310196SCurtis.Dunham@arm.com        target = File(joinpath(group, file_name))
124410196SCurtis.Dunham@arm.com        partial = env.PartialShared(target=target, source=group_shared)
124510196SCurtis.Dunham@arm.com        shared_objs.extend(partial)
1246955SN/A
124710196SCurtis.Dunham@arm.com    static_date = date_source.static(new_env)
1248955SN/A    new_env.Depends(static_date, static_objs)
124910196SCurtis.Dunham@arm.com    static_objs.extend(static_date)
125010196SCurtis.Dunham@arm.com
125110196SCurtis.Dunham@arm.com    shared_date = date_source.shared(new_env)
125210196SCurtis.Dunham@arm.com    new_env.Depends(shared_date, shared_objs)
125310196SCurtis.Dunham@arm.com    shared_objs.extend(shared_date)
125410196SCurtis.Dunham@arm.com
125510196SCurtis.Dunham@arm.com    main_objs = [ s.static(new_env) for s in Source.all.with_tag('main') ]
1256955SN/A
125710196SCurtis.Dunham@arm.com    # First make a library of everything but main() so other programs can
125810196SCurtis.Dunham@arm.com    # link against m5.
125910196SCurtis.Dunham@arm.com    static_lib = new_env.StaticLibrary(libname, static_objs)
126010196SCurtis.Dunham@arm.com    shared_lib = new_env.SharedLibrary(libname, shared_objs)
126110196SCurtis.Dunham@arm.com
126210196SCurtis.Dunham@arm.com    # Keep track of the object files generated so far so Executables can
126310196SCurtis.Dunham@arm.com    # include them.
12641869SN/A    new_env['STATIC_OBJS'] = static_objs
126510196SCurtis.Dunham@arm.com    new_env['SHARED_OBJS'] = shared_objs
126610196SCurtis.Dunham@arm.com    new_env['MAIN_OBJS'] = main_objs
126710196SCurtis.Dunham@arm.com
126810196SCurtis.Dunham@arm.com    new_env['STATIC_LIB'] = static_lib
126910196SCurtis.Dunham@arm.com    new_env['SHARED_LIB'] = shared_lib
127010196SCurtis.Dunham@arm.com
127110196SCurtis.Dunham@arm.com    # Record some settings for building Executables.
12729226Sandreas.hansson@arm.com    new_env['EXE_SUFFIX'] = label
127310196SCurtis.Dunham@arm.com    new_env['STRIP_EXES'] = strip
127410196SCurtis.Dunham@arm.com
127510196SCurtis.Dunham@arm.com    for cls in ExecutableMeta.all:
127610196SCurtis.Dunham@arm.com        cls.declare_all(new_env)
127710196SCurtis.Dunham@arm.com
127810196SCurtis.Dunham@arm.com    new_env.M5Binary = File(gem5_binary.path(new_env))
127910196SCurtis.Dunham@arm.com
128010196SCurtis.Dunham@arm.com    new_env.Command(secondary_exename, new_env.M5Binary,
128110196SCurtis.Dunham@arm.com            MakeAction('ln $SOURCE $TARGET', Transform("HARDLINK")))
128210196SCurtis.Dunham@arm.com
128310196SCurtis.Dunham@arm.com    # Set up regression tests.
128410196SCurtis.Dunham@arm.com    SConscript(os.path.join(env.root.abspath, 'tests', 'SConscript'),
128510196SCurtis.Dunham@arm.com               variant_dir=Dir('tests').Dir(new_env.Label),
128610196SCurtis.Dunham@arm.com               exports={ 'env' : new_env }, duplicate=False)
128710196SCurtis.Dunham@arm.com
128810196SCurtis.Dunham@arm.com# Start out with the compiler flags common to all compilers,
128910196SCurtis.Dunham@arm.com# i.e. they all use -g for opt and -g -pg for prof
129010196SCurtis.Dunham@arm.comccflags = {'debug' : [], 'opt' : ['-g'], 'fast' : [], 'prof' : ['-g', '-pg'],
129110196SCurtis.Dunham@arm.com           'perf' : ['-g']}
129210196SCurtis.Dunham@arm.com
129310196SCurtis.Dunham@arm.com# Start out with the linker flags common to all linkers, i.e. -pg for
129410196SCurtis.Dunham@arm.com# prof, and -lprofiler for perf. The -lprofile flag is surrounded by
129510196SCurtis.Dunham@arm.com# no-as-needed and as-needed as the binutils linker is too clever and
129610196SCurtis.Dunham@arm.com# simply doesn't link to the library otherwise.
129710196SCurtis.Dunham@arm.comldflags = {'debug' : [], 'opt' : [], 'fast' : [], 'prof' : ['-pg'],
129810196SCurtis.Dunham@arm.com           'perf' : ['-Wl,--no-as-needed', '-lprofiler', '-Wl,--as-needed']}
129910196SCurtis.Dunham@arm.com
130010196SCurtis.Dunham@arm.com# For Link Time Optimization, the optimisation flags used to compile
130110196SCurtis.Dunham@arm.com# individual files are decoupled from those used at link time
130210196SCurtis.Dunham@arm.com# (i.e. you can compile with -O3 and perform LTO with -O0), so we need
130310196SCurtis.Dunham@arm.com# to also update the linker flags based on the target.
130410196SCurtis.Dunham@arm.comif env['GCC']:
130510196SCurtis.Dunham@arm.com    if sys.platform == 'sunos5':
130610196SCurtis.Dunham@arm.com        ccflags['debug'] += ['-gstabs+']
130710196SCurtis.Dunham@arm.com    else:
130810196SCurtis.Dunham@arm.com        ccflags['debug'] += ['-ggdb3']
130910196SCurtis.Dunham@arm.com    ldflags['debug'] += ['-O0']
131010196SCurtis.Dunham@arm.com    # opt, fast, prof and perf all share the same cc flags, also add
131110196SCurtis.Dunham@arm.com    # the optimization to the ldflags as LTO defers the optimization
131210196SCurtis.Dunham@arm.com    # to link time
131310196SCurtis.Dunham@arm.com    for target in ['opt', 'fast', 'prof', 'perf']:
131410196SCurtis.Dunham@arm.com        ccflags[target] += ['-O3']
131510196SCurtis.Dunham@arm.com        ldflags[target] += ['-O3']
1316
1317    ccflags['fast'] += env['LTO_CCFLAGS']
1318    ldflags['fast'] += env['LTO_LDFLAGS']
1319elif env['CLANG']:
1320    ccflags['debug'] += ['-g', '-O0']
1321    # opt, fast, prof and perf all share the same cc flags
1322    for target in ['opt', 'fast', 'prof', 'perf']:
1323        ccflags[target] += ['-O3']
1324else:
1325    print('Unknown compiler, please fix compiler options')
1326    Exit(1)
1327
1328
1329# To speed things up, we only instantiate the build environments we
1330# need.  We try to identify the needed environment for each target; if
1331# we can't, we fall back on instantiating all the environments just to
1332# be safe.
1333target_types = ['debug', 'opt', 'fast', 'prof', 'perf']
1334obj2target = {'do': 'debug', 'o': 'opt', 'fo': 'fast', 'po': 'prof',
1335              'gpo' : 'perf'}
1336
1337def identifyTarget(t):
1338    ext = t.split('.')[-1]
1339    if ext in target_types:
1340        return ext
1341    if obj2target.has_key(ext):
1342        return obj2target[ext]
1343    match = re.search(r'/tests/([^/]+)/', t)
1344    if match and match.group(1) in target_types:
1345        return match.group(1)
1346    return 'all'
1347
1348needed_envs = [identifyTarget(target) for target in BUILD_TARGETS]
1349if 'all' in needed_envs:
1350    needed_envs += target_types
1351
1352disable_partial = False
1353if env['PLATFORM'] == 'darwin':
1354    # Up until Apple LLVM version 10.0.0 (clang-1000.11.45.5), partial
1355    # linked objects do not expose symbols that are marked with the
1356    # hidden visibility and consequently building gem5 on Mac OS
1357    # fails. As a workaround, we disable partial linking, however, we
1358    # may want to revisit in the future.
1359    disable_partial = True
1360
1361# Debug binary
1362if 'debug' in needed_envs:
1363    makeEnv(env, 'debug', '.do',
1364            CCFLAGS = Split(ccflags['debug']),
1365            CPPDEFINES = ['DEBUG', 'TRACING_ON=1'],
1366            LINKFLAGS = Split(ldflags['debug']),
1367            disable_partial=disable_partial)
1368
1369# Optimized binary
1370if 'opt' in needed_envs:
1371    makeEnv(env, 'opt', '.o',
1372            CCFLAGS = Split(ccflags['opt']),
1373            CPPDEFINES = ['TRACING_ON=1'],
1374            LINKFLAGS = Split(ldflags['opt']),
1375            disable_partial=disable_partial)
1376
1377# "Fast" binary
1378if 'fast' in needed_envs:
1379    disable_partial = disable_partial and \
1380            env.get('BROKEN_INCREMENTAL_LTO', False) and \
1381            GetOption('force_lto')
1382    makeEnv(env, 'fast', '.fo', strip = True,
1383            CCFLAGS = Split(ccflags['fast']),
1384            CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
1385            LINKFLAGS = Split(ldflags['fast']),
1386            disable_partial=disable_partial)
1387
1388# Profiled binary using gprof
1389if 'prof' in needed_envs:
1390    makeEnv(env, 'prof', '.po',
1391            CCFLAGS = Split(ccflags['prof']),
1392            CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
1393            LINKFLAGS = Split(ldflags['prof']),
1394            disable_partial=disable_partial)
1395
1396# Profiled binary using google-pprof
1397if 'perf' in needed_envs:
1398    makeEnv(env, 'perf', '.gpo',
1399            CCFLAGS = Split(ccflags['perf']),
1400            CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
1401            LINKFLAGS = Split(ldflags['perf']),
1402            disable_partial=disable_partial)
1403