SConscript revision 5604
1955SN/A# -*- mode:python -*- 2955SN/A 31762SN/A# Copyright (c) 2004-2005 The Regents of The University of Michigan 4955SN/A# All rights reserved. 5955SN/A# 6955SN/A# Redistribution and use in source and binary forms, with or without 7955SN/A# modification, are permitted provided that the following conditions are 8955SN/A# met: redistributions of source code must retain the above copyright 9955SN/A# notice, this list of conditions and the following disclaimer; 10955SN/A# redistributions in binary form must reproduce the above copyright 11955SN/A# notice, this list of conditions and the following disclaimer in the 12955SN/A# documentation and/or other materials provided with the distribution; 13955SN/A# neither the name of the copyright holders nor the names of its 14955SN/A# contributors may be used to endorse or promote products derived from 15955SN/A# this software without specific prior written permission. 16955SN/A# 17955SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18955SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19955SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20955SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21955SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22955SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23955SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24955SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25955SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26955SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27955SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 282665Ssaidi@eecs.umich.edu# 294762Snate@binkert.org# Authors: Nathan Binkert 30955SN/A 315522Snate@binkert.orgimport array 326143Snate@binkert.orgimport imp 334762Snate@binkert.orgimport marshal 345522Snate@binkert.orgimport os 35955SN/Aimport re 365522Snate@binkert.orgimport sys 37955SN/Aimport zlib 385522Snate@binkert.org 394202Sbinkertn@umich.edufrom os.path import basename, exists, isdir, isfile, join as joinpath 405742Snate@binkert.org 41955SN/Aimport SCons 424381Sbinkertn@umich.edu 434381Sbinkertn@umich.edu# This file defines how to build a particular configuration of M5 448334Snate@binkert.org# based on variable settings in the 'env' build environment. 45955SN/A 46955SN/AImport('*') 474202Sbinkertn@umich.edu 48955SN/A# Children need to see the environment 494382Sbinkertn@umich.eduExport('env') 504382Sbinkertn@umich.edu 514382Sbinkertn@umich.edubuild_env = dict([(opt, env[opt]) for opt in env.ExportOptions]) 526654Snate@binkert.org 535517Snate@binkert.orgdef sort_list(_list): 548614Sgblack@eecs.umich.edu """return a sorted copy of '_list'""" 557674Snate@binkert.org if isinstance(_list, list): 566143Snate@binkert.org _list = _list[:] 576143Snate@binkert.org else: 586143Snate@binkert.org _list = list(_list) 598233Snate@binkert.org _list.sort() 608233Snate@binkert.org return _list 618233Snate@binkert.org 628233Snate@binkert.orgclass PySourceFile(object): 638233Snate@binkert.org invalid_sym_char = re.compile('[^A-z0-9_]') 648334Snate@binkert.org def __init__(self, package, tnode): 658334Snate@binkert.org snode = tnode.srcnode() 668233Snate@binkert.org filename = str(tnode) 678233Snate@binkert.org pyname = basename(filename) 688233Snate@binkert.org assert pyname.endswith('.py') 698233Snate@binkert.org name = pyname[:-3] 708233Snate@binkert.org if package: 718233Snate@binkert.org path = package.split('.') 726143Snate@binkert.org else: 738233Snate@binkert.org path = [] 748233Snate@binkert.org 758233Snate@binkert.org modpath = path[:] 766143Snate@binkert.org if name != '__init__': 776143Snate@binkert.org modpath += [name] 786143Snate@binkert.org modpath = '.'.join(modpath) 796143Snate@binkert.org 808233Snate@binkert.org arcpath = path + [ pyname ] 818233Snate@binkert.org arcname = joinpath(*arcpath) 828233Snate@binkert.org 836143Snate@binkert.org debugname = snode.abspath 848233Snate@binkert.org if not exists(debugname): 858233Snate@binkert.org debugname = tnode.abspath 868233Snate@binkert.org 878233Snate@binkert.org self.tnode = tnode 886143Snate@binkert.org self.snode = snode 896143Snate@binkert.org self.pyname = pyname 906143Snate@binkert.org self.package = package 914762Snate@binkert.org self.modpath = modpath 926143Snate@binkert.org self.arcname = arcname 938233Snate@binkert.org self.debugname = debugname 948233Snate@binkert.org self.compiled = File(filename + 'c') 958233Snate@binkert.org self.assembly = File(filename + '.s') 968233Snate@binkert.org self.symname = "PyEMB_" + self.invalid_sym_char.sub('_', modpath) 978233Snate@binkert.org 986143Snate@binkert.org 998233Snate@binkert.org######################################################################## 1008233Snate@binkert.org# Code for adding source files of various types 1018233Snate@binkert.org# 1028233Snate@binkert.orgcc_lib_sources = [] 1036143Snate@binkert.orgdef Source(source): 1046143Snate@binkert.org '''Add a source file to the libm5 build''' 1056143Snate@binkert.org if not isinstance(source, SCons.Node.FS.File): 1066143Snate@binkert.org source = File(source) 1076143Snate@binkert.org 1086143Snate@binkert.org cc_lib_sources.append(source) 1096143Snate@binkert.org 1106143Snate@binkert.orgcc_bin_sources = [] 1116143Snate@binkert.orgdef BinSource(source): 1127065Snate@binkert.org '''Add a source file to the m5 binary build''' 1136143Snate@binkert.org if not isinstance(source, SCons.Node.FS.File): 1148233Snate@binkert.org source = File(source) 1158233Snate@binkert.org 1168233Snate@binkert.org cc_bin_sources.append(source) 1178233Snate@binkert.org 1188233Snate@binkert.orgpy_sources = [] 1198233Snate@binkert.orgdef PySource(package, source): 1208233Snate@binkert.org '''Add a python source file to the named package''' 1218233Snate@binkert.org if not isinstance(source, SCons.Node.FS.File): 1228233Snate@binkert.org source = File(source) 1238233Snate@binkert.org 1248233Snate@binkert.org source = PySourceFile(package, source) 1258233Snate@binkert.org py_sources.append(source) 1268233Snate@binkert.org 1278233Snate@binkert.orgsim_objects_fixed = False 1288233Snate@binkert.orgsim_object_modfiles = set() 1298233Snate@binkert.orgdef SimObject(source): 1308233Snate@binkert.org '''Add a SimObject python file as a python source object and add 1318233Snate@binkert.org it to a list of sim object modules''' 1328233Snate@binkert.org 1338233Snate@binkert.org if sim_objects_fixed: 1348233Snate@binkert.org raise AttributeError, "Too late to call SimObject now." 1358233Snate@binkert.org 1368233Snate@binkert.org if not isinstance(source, SCons.Node.FS.File): 1378233Snate@binkert.org source = File(source) 1388233Snate@binkert.org 1398233Snate@binkert.org PySource('m5.objects', source) 1408233Snate@binkert.org modfile = basename(str(source)) 1418233Snate@binkert.org assert modfile.endswith('.py') 1428233Snate@binkert.org modname = modfile[:-3] 1438233Snate@binkert.org sim_object_modfiles.add(modname) 1448233Snate@binkert.org 1456143Snate@binkert.orgswig_sources = [] 1466143Snate@binkert.orgdef SwigSource(package, source): 1476143Snate@binkert.org '''Add a swig file to build''' 1486143Snate@binkert.org if not isinstance(source, SCons.Node.FS.File): 1496143Snate@binkert.org source = File(source) 1506143Snate@binkert.org val = source,package 1516143Snate@binkert.org swig_sources.append(val) 1526143Snate@binkert.org 1536143Snate@binkert.orgunit_tests = [] 1548945Ssteve.reinhardt@amd.comdef UnitTest(target, sources): 1558233Snate@binkert.org if not isinstance(sources, (list, tuple)): 1568233Snate@binkert.org sources = [ sources ] 1576143Snate@binkert.org 1588945Ssteve.reinhardt@amd.com srcs = [] 1596143Snate@binkert.org for source in sources: 1606143Snate@binkert.org if not isinstance(source, SCons.Node.FS.File): 1616143Snate@binkert.org source = File(source) 1626143Snate@binkert.org srcs.append(source) 1635522Snate@binkert.org 1646143Snate@binkert.org unit_tests.append((target, srcs)) 1656143Snate@binkert.org 1666143Snate@binkert.org# Children should have access 1676143Snate@binkert.orgExport('Source') 1688233Snate@binkert.orgExport('BinSource') 1698233Snate@binkert.orgExport('PySource') 1708233Snate@binkert.orgExport('SimObject') 1716143Snate@binkert.orgExport('SwigSource') 1726143Snate@binkert.orgExport('UnitTest') 1736143Snate@binkert.org 1746143Snate@binkert.org######################################################################## 1755522Snate@binkert.org# 1765522Snate@binkert.org# Trace Flags 1775522Snate@binkert.org# 1785522Snate@binkert.orgall_flags = {} 1795604Snate@binkert.orgtrace_flags = [] 1805604Snate@binkert.orgdef TraceFlag(name, desc=''): 1816143Snate@binkert.org if name in all_flags: 1826143Snate@binkert.org raise AttributeError, "Flag %s already specified" % name 1834762Snate@binkert.org flag = (name, (), desc) 1844762Snate@binkert.org trace_flags.append(flag) 1856143Snate@binkert.org all_flags[name] = () 1866727Ssteve.reinhardt@amd.com 1876727Ssteve.reinhardt@amd.comdef CompoundFlag(name, flags, desc=''): 1886727Ssteve.reinhardt@amd.com if name in all_flags: 1894762Snate@binkert.org raise AttributeError, "Flag %s already specified" % name 1906143Snate@binkert.org 1916143Snate@binkert.org compound = tuple(flags) 1926143Snate@binkert.org for flag in compound: 1936143Snate@binkert.org if flag not in all_flags: 1946727Ssteve.reinhardt@amd.com raise AttributeError, "Trace flag %s not found" % flag 1956143Snate@binkert.org if all_flags[flag]: 1967674Snate@binkert.org raise AttributeError, \ 1977674Snate@binkert.org "Compound flag can't point to another compound flag" 1985604Snate@binkert.org 1996143Snate@binkert.org flag = (name, compound, desc) 2006143Snate@binkert.org trace_flags.append(flag) 2016143Snate@binkert.org all_flags[name] = compound 2024762Snate@binkert.org 2036143Snate@binkert.orgExport('TraceFlag') 2044762Snate@binkert.orgExport('CompoundFlag') 2054762Snate@binkert.org 2064762Snate@binkert.org######################################################################## 2076143Snate@binkert.org# 2086143Snate@binkert.org# Set some compiler variables 2094762Snate@binkert.org# 2108233Snate@binkert.org 2118233Snate@binkert.org# Include file paths are rooted in this directory. SCons will 2128233Snate@binkert.org# automatically expand '.' to refer to both the source directory and 2138233Snate@binkert.org# the corresponding build directory to pick up generated include 2146143Snate@binkert.org# files. 2156143Snate@binkert.orgenv.Append(CPPPATH=Dir('.')) 2164762Snate@binkert.org 2176143Snate@binkert.org# Add a flag defining what THE_ISA should be for all compilation 2184762Snate@binkert.orgenv.Append(CPPDEFINES=[('THE_ISA','%s_ISA' % env['TARGET_ISA'].upper())]) 2196143Snate@binkert.org 2204762Snate@binkert.org######################################################################## 2216143Snate@binkert.org# 2228233Snate@binkert.org# Walk the tree and execute all SConscripts in subdirectories 2238233Snate@binkert.org# 2248233Snate@binkert.org 2256143Snate@binkert.orgfor base_dir in base_dir_list: 2266143Snate@binkert.org here = Dir('.').srcnode().abspath 2276143Snate@binkert.org for root, dirs, files in os.walk(base_dir, topdown=True): 2286143Snate@binkert.org if root == here: 2296143Snate@binkert.org # we don't want to recurse back into this SConscript 2306143Snate@binkert.org continue 2316143Snate@binkert.org 2326143Snate@binkert.org if 'SConscript' in files: 2338233Snate@binkert.org build_dir = joinpath(env['BUILDDIR'], root[len(base_dir) + 1:]) 2348233Snate@binkert.org SConscript(joinpath(root, 'SConscript'), build_dir=build_dir) 235955SN/A 2368235Snate@binkert.orgfor opt in env.ExportOptions: 2378235Snate@binkert.org env.ConfigFile(opt) 2386143Snate@binkert.org 2398235Snate@binkert.org######################################################################## 2408235Snate@binkert.org# 2418235Snate@binkert.org# Prevent any SimObjects from being added after this point, they 2428235Snate@binkert.org# should all have been added in the SConscripts above 2438235Snate@binkert.org# 2448235Snate@binkert.orgclass DictImporter(object): 2458235Snate@binkert.org '''This importer takes a dictionary of arbitrary module names that 2468235Snate@binkert.org map to arbitrary filenames.''' 2478235Snate@binkert.org def __init__(self, modules): 2488235Snate@binkert.org self.modules = modules 2498235Snate@binkert.org self.installed = set() 2508235Snate@binkert.org 2518235Snate@binkert.org def __del__(self): 2528235Snate@binkert.org self.unload() 2538235Snate@binkert.org 2548235Snate@binkert.org def unload(self): 2558235Snate@binkert.org import sys 2565584Snate@binkert.org for module in self.installed: 2574382Sbinkertn@umich.edu del sys.modules[module] 2584202Sbinkertn@umich.edu self.installed = set() 2594382Sbinkertn@umich.edu 2604382Sbinkertn@umich.edu def find_module(self, fullname, path): 2614382Sbinkertn@umich.edu if fullname == '__scons': 2625584Snate@binkert.org return self 2634382Sbinkertn@umich.edu 2644382Sbinkertn@umich.edu if fullname == 'm5.objects': 2654382Sbinkertn@umich.edu return self 2668232Snate@binkert.org 2675192Ssaidi@eecs.umich.edu if fullname.startswith('m5.internal'): 2688232Snate@binkert.org return None 2698232Snate@binkert.org 2708232Snate@binkert.org if fullname in self.modules and exists(self.modules[fullname]): 2715192Ssaidi@eecs.umich.edu return self 2728232Snate@binkert.org 2735192Ssaidi@eecs.umich.edu return None 2745799Snate@binkert.org 2758232Snate@binkert.org def load_module(self, fullname): 2765192Ssaidi@eecs.umich.edu mod = imp.new_module(fullname) 2775192Ssaidi@eecs.umich.edu sys.modules[fullname] = mod 2785192Ssaidi@eecs.umich.edu self.installed.add(fullname) 2798232Snate@binkert.org 2805192Ssaidi@eecs.umich.edu mod.__loader__ = self 2818232Snate@binkert.org if fullname == 'm5.objects': 2825192Ssaidi@eecs.umich.edu mod.__path__ = fullname.split('.') 2835192Ssaidi@eecs.umich.edu return mod 2845192Ssaidi@eecs.umich.edu 2855192Ssaidi@eecs.umich.edu if fullname == '__scons': 2864382Sbinkertn@umich.edu mod.__dict__['m5_build_env'] = build_env 2874382Sbinkertn@umich.edu return mod 2884382Sbinkertn@umich.edu 2892667Sstever@eecs.umich.edu srcfile = self.modules[fullname] 2902667Sstever@eecs.umich.edu if basename(srcfile) == '__init__.py': 2912667Sstever@eecs.umich.edu mod.__path__ = fullname.split('.') 2922667Sstever@eecs.umich.edu mod.__file__ = srcfile 2932667Sstever@eecs.umich.edu 2942667Sstever@eecs.umich.edu exec file(srcfile, 'r') in mod.__dict__ 2955742Snate@binkert.org 2965742Snate@binkert.org return mod 2975742Snate@binkert.org 2985793Snate@binkert.orgpy_modules = {} 2998334Snate@binkert.orgfor source in py_sources: 3005793Snate@binkert.org py_modules[source.modpath] = source.snode.abspath 3015793Snate@binkert.org 3025793Snate@binkert.org# install the python importer so we can grab stuff from the source 3034382Sbinkertn@umich.edu# tree itself. We can't have SimObjects added after this point or 3044762Snate@binkert.org# else we won't know about them for the rest of the stuff. 3055344Sstever@gmail.comsim_objects_fixed = True 3064382Sbinkertn@umich.eduimporter = DictImporter(py_modules) 3075341Sstever@gmail.comsys.meta_path[0:0] = [ importer ] 3085742Snate@binkert.org 3095742Snate@binkert.orgimport m5 3105742Snate@binkert.org 3115742Snate@binkert.org# import all sim objects so we can populate the all_objects list 3125742Snate@binkert.org# make sure that we're working with a list, then let's sort it 3134762Snate@binkert.orgsim_objects = list(sim_object_modfiles) 3145742Snate@binkert.orgsim_objects.sort() 3155742Snate@binkert.orgfor simobj in sim_objects: 3167722Sgblack@eecs.umich.edu exec('from m5.objects import %s' % simobj) 3175742Snate@binkert.org 3185742Snate@binkert.org# we need to unload all of the currently imported modules so that they 3195742Snate@binkert.org# will be re-imported the next time the sconscript is run 3205742Snate@binkert.orgimporter.unload() 3218242Sbradley.danofsky@amd.comsys.meta_path.remove(importer) 3228242Sbradley.danofsky@amd.com 3238242Sbradley.danofsky@amd.comsim_objects = m5.SimObject.allClasses 3248242Sbradley.danofsky@amd.comall_enums = m5.params.allEnums 3255341Sstever@gmail.com 3265742Snate@binkert.orgall_params = {} 3277722Sgblack@eecs.umich.edufor name,obj in sim_objects.iteritems(): 3284773Snate@binkert.org for param in obj._params.local.values(): 3296108Snate@binkert.org if not hasattr(param, 'swig_decl'): 3301858SN/A continue 3311085SN/A pname = param.ptype_str 3326658Snate@binkert.org if pname not in all_params: 3336658Snate@binkert.org all_params[pname] = param 3347673Snate@binkert.org 3356658Snate@binkert.org######################################################################## 3366658Snate@binkert.org# 3376658Snate@binkert.org# calculate extra dependencies 3386658Snate@binkert.org# 3396658Snate@binkert.orgmodule_depends = ["m5", "m5.SimObject", "m5.params"] 3406658Snate@binkert.orgdepends = [ File(py_modules[dep]) for dep in module_depends ] 3416658Snate@binkert.org 3427673Snate@binkert.org######################################################################## 3437673Snate@binkert.org# 3447673Snate@binkert.org# Commands for the basic automatically generated python files 3457673Snate@binkert.org# 3467673Snate@binkert.org 3477673Snate@binkert.org# Generate Python file containing a dict specifying the current 3487673Snate@binkert.org# build_env flags. 3496658Snate@binkert.orgdef makeDefinesPyFile(target, source, env): 3507673Snate@binkert.org f = file(str(target[0]), 'w') 3517673Snate@binkert.org print >>f, "m5_build_env = ", source[0] 3527673Snate@binkert.org f.close() 3537673Snate@binkert.org 3547673Snate@binkert.org# Generate python file containing info about the M5 source code 3557673Snate@binkert.orgdef makeInfoPyFile(target, source, env): 3567673Snate@binkert.org f = file(str(target[0]), 'w') 3577673Snate@binkert.org for src in source: 3587673Snate@binkert.org data = ''.join(file(src.srcnode().abspath, 'r').xreadlines()) 3597673Snate@binkert.org print >>f, "%s = %s" % (src, repr(data)) 3606658Snate@binkert.org f.close() 3617756SAli.Saidi@ARM.com 3627816Ssteve.reinhardt@amd.com# Generate the __init__.py file for m5.objects 3636658Snate@binkert.orgdef makeObjectsInitFile(target, source, env): 3644382Sbinkertn@umich.edu f = file(str(target[0]), 'w') 3654382Sbinkertn@umich.edu print >>f, 'from params import *' 3664762Snate@binkert.org print >>f, 'from m5.SimObject import *' 3674762Snate@binkert.org for module in source: 3684762Snate@binkert.org print >>f, 'from %s import *' % module.get_contents() 3696654Snate@binkert.org f.close() 3706654Snate@binkert.org 3715517Snate@binkert.org# Generate a file with all of the compile options in it 3725517Snate@binkert.orgenv.Command('python/m5/defines.py', Value(build_env), makeDefinesPyFile) 3735517Snate@binkert.orgPySource('m5', 'python/m5/defines.py') 3745517Snate@binkert.org 3755517Snate@binkert.org# Generate a file that wraps the basic top level files 3765517Snate@binkert.orgenv.Command('python/m5/info.py', 3775517Snate@binkert.org [ '#/AUTHORS', '#/LICENSE', '#/README', '#/RELEASE_NOTES' ], 3785517Snate@binkert.org makeInfoPyFile) 3795517Snate@binkert.orgPySource('m5', 'python/m5/info.py') 3805517Snate@binkert.org 3815517Snate@binkert.org# Generate an __init__.py file for the objects package 3825517Snate@binkert.orgenv.Command('python/m5/objects/__init__.py', 3835517Snate@binkert.org [ Value(o) for o in sort_list(sim_object_modfiles) ], 3845517Snate@binkert.org makeObjectsInitFile) 3855517Snate@binkert.orgPySource('m5.objects', 'python/m5/objects/__init__.py') 3865517Snate@binkert.org 3875517Snate@binkert.org######################################################################## 3886654Snate@binkert.org# 3895517Snate@binkert.org# Create all of the SimObject param headers and enum headers 3905517Snate@binkert.org# 3915517Snate@binkert.org 3925517Snate@binkert.orgdef createSimObjectParam(target, source, env): 3935517Snate@binkert.org assert len(target) == 1 and len(source) == 1 3945517Snate@binkert.org 3955517Snate@binkert.org hh_file = file(target[0].abspath, 'w') 3965517Snate@binkert.org name = str(source[0].get_contents()) 3976143Snate@binkert.org obj = sim_objects[name] 3986654Snate@binkert.org 3995517Snate@binkert.org print >>hh_file, obj.cxx_decl() 4005517Snate@binkert.org 4015517Snate@binkert.orgdef createSwigParam(target, source, env): 4025517Snate@binkert.org assert len(target) == 1 and len(source) == 1 4035517Snate@binkert.org 4045517Snate@binkert.org i_file = file(target[0].abspath, 'w') 4055517Snate@binkert.org name = str(source[0].get_contents()) 4065517Snate@binkert.org param = all_params[name] 4075517Snate@binkert.org 4085517Snate@binkert.org for line in param.swig_decl(): 4095517Snate@binkert.org print >>i_file, line 4105517Snate@binkert.org 4115517Snate@binkert.orgdef createEnumStrings(target, source, env): 4125517Snate@binkert.org assert len(target) == 1 and len(source) == 1 4136654Snate@binkert.org 4146654Snate@binkert.org cc_file = file(target[0].abspath, 'w') 4155517Snate@binkert.org name = str(source[0].get_contents()) 4165517Snate@binkert.org obj = all_enums[name] 4176143Snate@binkert.org 4186143Snate@binkert.org print >>cc_file, obj.cxx_def() 4196143Snate@binkert.org cc_file.close() 4206727Ssteve.reinhardt@amd.com 4215517Snate@binkert.orgdef createEnumParam(target, source, env): 4226727Ssteve.reinhardt@amd.com assert len(target) == 1 and len(source) == 1 4235517Snate@binkert.org 4245517Snate@binkert.org hh_file = file(target[0].abspath, 'w') 4255517Snate@binkert.org name = str(source[0].get_contents()) 4266654Snate@binkert.org obj = all_enums[name] 4276654Snate@binkert.org 4287673Snate@binkert.org print >>hh_file, obj.cxx_decl() 4296654Snate@binkert.org 4306654Snate@binkert.org# Generate all of the SimObject param struct header files 4316654Snate@binkert.orgparams_hh_files = [] 4326654Snate@binkert.orgfor name,simobj in sim_objects.iteritems(): 4335517Snate@binkert.org extra_deps = [ File(py_modules[simobj.__module__]) ] 4345517Snate@binkert.org 4355517Snate@binkert.org hh_file = File('params/%s.hh' % name) 4366143Snate@binkert.org params_hh_files.append(hh_file) 4375517Snate@binkert.org env.Command(hh_file, Value(name), createSimObjectParam) 4384762Snate@binkert.org env.Depends(hh_file, depends + extra_deps) 4395517Snate@binkert.org 4405517Snate@binkert.org# Generate any parameter header files needed 4416143Snate@binkert.orgparams_i_files = [] 4426143Snate@binkert.orgfor name,param in all_params.iteritems(): 4435517Snate@binkert.org if isinstance(param, m5.params.VectorParamDesc): 4445517Snate@binkert.org ext = 'vptype' 4455517Snate@binkert.org else: 4465517Snate@binkert.org ext = 'ptype' 4475517Snate@binkert.org 4485517Snate@binkert.org i_file = File('params/%s_%s.i' % (name, ext)) 4495517Snate@binkert.org params_i_files.append(i_file) 4505517Snate@binkert.org env.Command(i_file, Value(name), createSwigParam) 4515517Snate@binkert.org env.Depends(i_file, depends) 4528596Ssteve.reinhardt@amd.com 4538596Ssteve.reinhardt@amd.com# Generate all enum header files 4548596Ssteve.reinhardt@amd.comfor name,enum in all_enums.iteritems(): 4558596Ssteve.reinhardt@amd.com extra_deps = [ File(py_modules[enum.__module__]) ] 4568596Ssteve.reinhardt@amd.com 4578596Ssteve.reinhardt@amd.com cc_file = File('enums/%s.cc' % name) 4588596Ssteve.reinhardt@amd.com env.Command(cc_file, Value(name), createEnumStrings) 4596143Snate@binkert.org env.Depends(cc_file, depends + extra_deps) 4605517Snate@binkert.org Source(cc_file) 4616654Snate@binkert.org 4626654Snate@binkert.org hh_file = File('enums/%s.hh' % name) 4636654Snate@binkert.org env.Command(hh_file, Value(name), createEnumParam) 4646654Snate@binkert.org env.Depends(hh_file, depends + extra_deps) 4656654Snate@binkert.org 4666654Snate@binkert.org# Build the big monolithic swigged params module (wraps all SimObject 4675517Snate@binkert.org# param structs and enum structs) 4685517Snate@binkert.orgdef buildParams(target, source, env): 4695517Snate@binkert.org names = [ s.get_contents() for s in source ] 4708596Ssteve.reinhardt@amd.com objs = [ sim_objects[name] for name in names ] 4718596Ssteve.reinhardt@amd.com out = file(target[0].abspath, 'w') 4724762Snate@binkert.org 4734762Snate@binkert.org ordered_objs = [] 4744762Snate@binkert.org obj_seen = set() 4754762Snate@binkert.org def order_obj(obj): 4764762Snate@binkert.org name = str(obj) 4774762Snate@binkert.org if name in obj_seen: 4787675Snate@binkert.org return 4794762Snate@binkert.org 4804762Snate@binkert.org obj_seen.add(name) 4814762Snate@binkert.org if str(obj) != 'SimObject': 4824762Snate@binkert.org order_obj(obj.__bases__[0]) 4834382Sbinkertn@umich.edu 4844382Sbinkertn@umich.edu ordered_objs.append(obj) 4855517Snate@binkert.org 4866654Snate@binkert.org for obj in objs: 4875517Snate@binkert.org order_obj(obj) 4888126Sgblack@eecs.umich.edu 4896654Snate@binkert.org enums = set() 4907673Snate@binkert.org predecls = [] 4916654Snate@binkert.org pd_seen = set() 4926654Snate@binkert.org 4936654Snate@binkert.org def add_pds(*pds): 4946654Snate@binkert.org for pd in pds: 4956654Snate@binkert.org if pd not in pd_seen: 4966654Snate@binkert.org predecls.append(pd) 4976654Snate@binkert.org pd_seen.add(pd) 4986669Snate@binkert.org 4996669Snate@binkert.org for obj in ordered_objs: 5006669Snate@binkert.org params = obj._params.local.values() 5016669Snate@binkert.org for param in params: 5026669Snate@binkert.org ptype = param.ptype 5036669Snate@binkert.org if issubclass(ptype, m5.params.Enum): 5046654Snate@binkert.org if ptype not in enums: 5057673Snate@binkert.org enums.add(ptype) 5065517Snate@binkert.org pds = param.swig_predecls() 5078126Sgblack@eecs.umich.edu if isinstance(pds, (list, tuple)): 5085798Snate@binkert.org add_pds(*pds) 5097756SAli.Saidi@ARM.com else: 5107816Ssteve.reinhardt@amd.com add_pds(pds) 5115798Snate@binkert.org 5125798Snate@binkert.org print >>out, '%module params' 5135517Snate@binkert.org 5145517Snate@binkert.org print >>out, '%{' 5157673Snate@binkert.org for obj in ordered_objs: 5165517Snate@binkert.org print >>out, '#include "params/%s.hh"' % obj 5175517Snate@binkert.org print >>out, '%}' 5187673Snate@binkert.org 5197673Snate@binkert.org for pd in predecls: 5205517Snate@binkert.org print >>out, pd 5215798Snate@binkert.org 5225798Snate@binkert.org enums = list(enums) 5238333Snate@binkert.org enums.sort() 5247816Ssteve.reinhardt@amd.com for enum in enums: 5255798Snate@binkert.org print >>out, '%%include "enums/%s.hh"' % enum.__name__ 5265798Snate@binkert.org print >>out 5274762Snate@binkert.org 5284762Snate@binkert.org for obj in ordered_objs: 5294762Snate@binkert.org if obj.swig_objdecls: 5304762Snate@binkert.org for decl in obj.swig_objdecls: 5314762Snate@binkert.org print >>out, decl 5328596Ssteve.reinhardt@amd.com continue 5335517Snate@binkert.org 5345517Snate@binkert.org code = '' 5355517Snate@binkert.org base = obj.get_base() 5365517Snate@binkert.org 5375517Snate@binkert.org code += '// stop swig from creating/wrapping default ctor/dtor\n' 5387673Snate@binkert.org code += '%%nodefault %s;\n' % obj.cxx_class 5398596Ssteve.reinhardt@amd.com code += 'class %s ' % obj.cxx_class 5407673Snate@binkert.org if base: 5415517Snate@binkert.org code += ': public %s' % base 5428596Ssteve.reinhardt@amd.com code += ' {};\n' 5435517Snate@binkert.org 5445517Snate@binkert.org klass = obj.cxx_class; 5455517Snate@binkert.org if hasattr(obj, 'cxx_namespace'): 5468596Ssteve.reinhardt@amd.com new_code = 'namespace %s {\n' % obj.cxx_namespace 5475517Snate@binkert.org new_code += code 5487673Snate@binkert.org new_code += '}\n' 5497673Snate@binkert.org code = new_code 5507673Snate@binkert.org klass = '%s::%s' % (obj.cxx_namespace, klass) 5515517Snate@binkert.org 5525517Snate@binkert.org print >>out, code 5535517Snate@binkert.org 5545517Snate@binkert.org print >>out, '%%include "src/sim/sim_object_params.hh"' % obj 5555517Snate@binkert.org for obj in ordered_objs: 5565517Snate@binkert.org print >>out, '%%include "params/%s.hh"' % obj 5575517Snate@binkert.org 5587673Snate@binkert.orgparams_file = File('params/params.i') 5597673Snate@binkert.orgnames = sort_list(sim_objects.keys()) 5607673Snate@binkert.orgenv.Command(params_file, [ Value(v) for v in names ], buildParams) 5615517Snate@binkert.orgenv.Depends(params_file, params_hh_files + params_i_files + depends) 5628596Ssteve.reinhardt@amd.comSwigSource('m5.objects', params_file) 5635517Snate@binkert.org 5645517Snate@binkert.org# Build all swig modules 5655517Snate@binkert.orgswig_modules = [] 5665517Snate@binkert.orgcc_swig_sources = [] 5675517Snate@binkert.orgfor source,package in swig_sources: 5687673Snate@binkert.org filename = str(source) 5697673Snate@binkert.org assert filename.endswith('.i') 5707673Snate@binkert.org 5715517Snate@binkert.org base = '.'.join(filename.split('.')[:-1]) 5728596Ssteve.reinhardt@amd.com module = basename(base) 5737675Snate@binkert.org cc_file = base + '_wrap.cc' 5747675Snate@binkert.org py_file = base + '.py' 5757675Snate@binkert.org 5767675Snate@binkert.org env.Command([cc_file, py_file], source, 5777675Snate@binkert.org '$SWIG $SWIGFLAGS -outdir ${TARGETS[1].dir} ' 5787675Snate@binkert.org '-o ${TARGETS[0]} $SOURCES') 5798596Ssteve.reinhardt@amd.com env.Depends(py_file, source) 5807675Snate@binkert.org env.Depends(cc_file, source) 5817675Snate@binkert.org 5828596Ssteve.reinhardt@amd.com swig_modules.append(Value(module)) 5838596Ssteve.reinhardt@amd.com cc_swig_sources.append(File(cc_file)) 5848596Ssteve.reinhardt@amd.com PySource(package, py_file) 5858596Ssteve.reinhardt@amd.com 5868596Ssteve.reinhardt@amd.com# Generate the main swig init file 5878596Ssteve.reinhardt@amd.comdef makeSwigInit(target, source, env): 5888596Ssteve.reinhardt@amd.com f = file(str(target[0]), 'w') 5898596Ssteve.reinhardt@amd.com print >>f, 'extern "C" {' 5908596Ssteve.reinhardt@amd.com for module in source: 5914762Snate@binkert.org print >>f, ' void init_%s();' % module.get_contents() 5926143Snate@binkert.org print >>f, '}' 5936143Snate@binkert.org print >>f, 'void initSwig() {' 5946143Snate@binkert.org for module in source: 5954762Snate@binkert.org print >>f, ' init_%s();' % module.get_contents() 5964762Snate@binkert.org print >>f, '}' 5974762Snate@binkert.org f.close() 5987756SAli.Saidi@ARM.com 5998596Ssteve.reinhardt@amd.comenv.Command('python/swig/init.cc', swig_modules, makeSwigInit) 6004762Snate@binkert.orgSource('python/swig/init.cc') 6014762Snate@binkert.org 6028596Ssteve.reinhardt@amd.com# Generate traceflags.py 6035463Snate@binkert.orgdef traceFlagsPy(target, source, env): 6048596Ssteve.reinhardt@amd.com assert(len(target) == 1) 6058596Ssteve.reinhardt@amd.com 6065463Snate@binkert.org f = file(str(target[0]), 'w') 6077756SAli.Saidi@ARM.com 6088596Ssteve.reinhardt@amd.com allFlags = [] 6094762Snate@binkert.org for s in source: 6107677Snate@binkert.org val = eval(s.get_contents()) 6114762Snate@binkert.org allFlags.append(val) 6124762Snate@binkert.org 6136143Snate@binkert.org print >>f, 'baseFlags = [' 6146143Snate@binkert.org for flag, compound, desc in allFlags: 6156143Snate@binkert.org if not compound: 6164762Snate@binkert.org print >>f, " '%s'," % flag 6174762Snate@binkert.org print >>f, " ]" 6187756SAli.Saidi@ARM.com print >>f 6197816Ssteve.reinhardt@amd.com 6204762Snate@binkert.org print >>f, 'compoundFlags = [' 6214762Snate@binkert.org print >>f, " 'All'," 6224762Snate@binkert.org for flag, compound, desc in allFlags: 6234762Snate@binkert.org if compound: 6247756SAli.Saidi@ARM.com print >>f, " '%s'," % flag 6258596Ssteve.reinhardt@amd.com print >>f, " ]" 6264762Snate@binkert.org print >>f 6274762Snate@binkert.org 6287677Snate@binkert.org print >>f, "allFlags = frozenset(baseFlags + compoundFlags)" 6297756SAli.Saidi@ARM.com print >>f 6308596Ssteve.reinhardt@amd.com 6317675Snate@binkert.org print >>f, 'compoundFlagMap = {' 6327677Snate@binkert.org all = tuple([flag for flag,compound,desc in allFlags if not compound]) 6335517Snate@binkert.org print >>f, " 'All' : %s," % (all, ) 6348596Ssteve.reinhardt@amd.com for flag, compound, desc in allFlags: 6357675Snate@binkert.org if compound: 6368596Ssteve.reinhardt@amd.com print >>f, " '%s' : %s," % (flag, compound) 6378596Ssteve.reinhardt@amd.com print >>f, " }" 6388596Ssteve.reinhardt@amd.com print >>f 6398596Ssteve.reinhardt@amd.com 6408596Ssteve.reinhardt@amd.com print >>f, 'flagDescriptions = {' 6414762Snate@binkert.org print >>f, " 'All' : 'All flags'," 6427674Snate@binkert.org for flag, compound, desc in allFlags: 6437674Snate@binkert.org print >>f, " '%s' : '%s'," % (flag, desc) 6447674Snate@binkert.org print >>f, " }" 6457674Snate@binkert.org 6467674Snate@binkert.org f.close() 6477674Snate@binkert.org 6487674Snate@binkert.orgdef traceFlagsCC(target, source, env): 6497674Snate@binkert.org assert(len(target) == 1) 6507674Snate@binkert.org 6517674Snate@binkert.org f = file(str(target[0]), 'w') 6527674Snate@binkert.org 6537674Snate@binkert.org allFlags = [] 6547674Snate@binkert.org for s in source: 6557674Snate@binkert.org val = eval(s.get_contents()) 6567674Snate@binkert.org allFlags.append(val) 6574762Snate@binkert.org 6586143Snate@binkert.org # file header 6596143Snate@binkert.org print >>f, ''' 6607756SAli.Saidi@ARM.com/* 6617816Ssteve.reinhardt@amd.com * DO NOT EDIT THIS FILE! Automatically generated 6628235Snate@binkert.org */ 6638596Ssteve.reinhardt@amd.com 6647756SAli.Saidi@ARM.com#include "base/traceflags.hh" 6657816Ssteve.reinhardt@amd.com 6668235Snate@binkert.orgusing namespace Trace; 6674382Sbinkertn@umich.edu 6688232Snate@binkert.orgconst char *Trace::flagStrings[] = 6698232Snate@binkert.org{''' 6708232Snate@binkert.org 6718232Snate@binkert.org # The string array is used by SimpleEnumParam to map the strings 6728232Snate@binkert.org # provided by the user to enum values. 6736229Snate@binkert.org for flag, compound, desc in allFlags: 6748232Snate@binkert.org if not compound: 6758232Snate@binkert.org print >>f, ' "%s",' % flag 6768232Snate@binkert.org 6776229Snate@binkert.org print >>f, ' "All",' 6787673Snate@binkert.org for flag, compound, desc in allFlags: 6795517Snate@binkert.org if compound: 6805517Snate@binkert.org print >>f, ' "%s",' % flag 6817673Snate@binkert.org 6825517Snate@binkert.org print >>f, '};' 6835517Snate@binkert.org print >>f 6845517Snate@binkert.org print >>f, 'const int Trace::numFlagStrings = %d;' % (len(allFlags) + 1) 6855517Snate@binkert.org print >>f 6868232Snate@binkert.org 6877673Snate@binkert.org # 6887673Snate@binkert.org # Now define the individual compound flag arrays. There is an array 6898232Snate@binkert.org # for each compound flag listing the component base flags. 6908232Snate@binkert.org # 6918232Snate@binkert.org all = tuple([flag for flag,compound,desc in allFlags if not compound]) 6928232Snate@binkert.org print >>f, 'static const Flags AllMap[] = {' 6937673Snate@binkert.org for flag, compound, desc in allFlags: 6945517Snate@binkert.org if not compound: 6958232Snate@binkert.org print >>f, " %s," % flag 6968232Snate@binkert.org print >>f, '};' 6978232Snate@binkert.org print >>f 6988232Snate@binkert.org 6997673Snate@binkert.org for flag, compound, desc in allFlags: 7008232Snate@binkert.org if not compound: 7018232Snate@binkert.org continue 7028232Snate@binkert.org print >>f, 'static const Flags %sMap[] = {' % flag 7038232Snate@binkert.org for flag in compound: 7048232Snate@binkert.org print >>f, " %s," % flag 7058232Snate@binkert.org print >>f, " (Flags)-1" 7067673Snate@binkert.org print >>f, '};' 7075517Snate@binkert.org print >>f 7088232Snate@binkert.org 7098232Snate@binkert.org # 7105517Snate@binkert.org # Finally the compoundFlags[] array maps the compound flags 7117673Snate@binkert.org # to their individual arrays/ 7125517Snate@binkert.org # 7138232Snate@binkert.org print >>f, 'const Flags *Trace::compoundFlags[] =' 7148232Snate@binkert.org print >>f, '{' 7155517Snate@binkert.org print >>f, ' AllMap,' 7168232Snate@binkert.org for flag, compound, desc in allFlags: 7178232Snate@binkert.org if compound: 7188232Snate@binkert.org print >>f, ' %sMap,' % flag 7197673Snate@binkert.org # file trailer 7205517Snate@binkert.org print >>f, '};' 7215517Snate@binkert.org 7227673Snate@binkert.org f.close() 7235517Snate@binkert.org 7245517Snate@binkert.orgdef traceFlagsHH(target, source, env): 7255517Snate@binkert.org assert(len(target) == 1) 7268232Snate@binkert.org 7275517Snate@binkert.org f = file(str(target[0]), 'w') 7285517Snate@binkert.org 7298232Snate@binkert.org allFlags = [] 7308232Snate@binkert.org for s in source: 7315517Snate@binkert.org val = eval(s.get_contents()) 7328232Snate@binkert.org allFlags.append(val) 7338232Snate@binkert.org 7345517Snate@binkert.org # file header boilerplate 7358232Snate@binkert.org print >>f, ''' 7368232Snate@binkert.org/* 7378232Snate@binkert.org * DO NOT EDIT THIS FILE! 7385517Snate@binkert.org * 7398232Snate@binkert.org * Automatically generated from traceflags.py 7408232Snate@binkert.org */ 7418232Snate@binkert.org 7428232Snate@binkert.org#ifndef __BASE_TRACE_FLAGS_HH__ 7438232Snate@binkert.org#define __BASE_TRACE_FLAGS_HH__ 7448232Snate@binkert.org 7455517Snate@binkert.orgnamespace Trace { 7468232Snate@binkert.org 7478232Snate@binkert.orgenum Flags {''' 7485517Snate@binkert.org 7498232Snate@binkert.org # Generate the enum. Base flags come first, then compound flags. 7507673Snate@binkert.org idx = 0 7515517Snate@binkert.org for flag, compound, desc in allFlags: 7527673Snate@binkert.org if not compound: 7535517Snate@binkert.org print >>f, ' %s = %d,' % (flag, idx) 7548232Snate@binkert.org idx += 1 7558232Snate@binkert.org 7568232Snate@binkert.org numBaseFlags = idx 7575192Ssaidi@eecs.umich.edu print >>f, ' NumFlags = %d,' % idx 7588232Snate@binkert.org 7598232Snate@binkert.org # put a comment in here to separate base from compound flags 7608232Snate@binkert.org print >>f, ''' 7618232Snate@binkert.org// The remaining enum values are *not* valid indices for Trace::flags. 7628232Snate@binkert.org// They are "compound" flags, which correspond to sets of base 7635192Ssaidi@eecs.umich.edu// flags, and are used by changeFlag.''' 7647674Snate@binkert.org 7655522Snate@binkert.org print >>f, ' All = %d,' % idx 7665522Snate@binkert.org idx += 1 7677674Snate@binkert.org for flag, compound, desc in allFlags: 7687674Snate@binkert.org if compound: 7697674Snate@binkert.org print >>f, ' %s = %d,' % (flag, idx) 7707674Snate@binkert.org idx += 1 7717674Snate@binkert.org 7727674Snate@binkert.org numCompoundFlags = idx - numBaseFlags 7737674Snate@binkert.org print >>f, ' NumCompoundFlags = %d' % numCompoundFlags 7747674Snate@binkert.org 7755522Snate@binkert.org # trailer boilerplate 7765522Snate@binkert.org print >>f, '''\ 7775522Snate@binkert.org}; // enum Flags 7785517Snate@binkert.org 7795522Snate@binkert.org// Array of strings for SimpleEnumParam 7805517Snate@binkert.orgextern const char *flagStrings[]; 7816143Snate@binkert.orgextern const int numFlagStrings; 7826727Ssteve.reinhardt@amd.com 7835522Snate@binkert.org// Array of arraay pointers: for each compound flag, gives the list of 7845522Snate@binkert.org// base flags to set. Inidividual flag arrays are terminated by -1. 7855522Snate@binkert.orgextern const Flags *compoundFlags[]; 7867674Snate@binkert.org 7875517Snate@binkert.org/* namespace Trace */ } 7887673Snate@binkert.org 7897673Snate@binkert.org#endif // __BASE_TRACE_FLAGS_HH__ 7907674Snate@binkert.org''' 7917673Snate@binkert.org 7927674Snate@binkert.org f.close() 7937674Snate@binkert.org 7948946Sandreas.hansson@arm.comflags = [ Value(f) for f in trace_flags ] 7957674Snate@binkert.orgenv.Command('base/traceflags.py', flags, traceFlagsPy) 7967674Snate@binkert.orgPySource('m5', 'base/traceflags.py') 7977674Snate@binkert.org 7985522Snate@binkert.orgenv.Command('base/traceflags.hh', flags, traceFlagsHH) 7995522Snate@binkert.orgenv.Command('base/traceflags.cc', flags, traceFlagsCC) 8007674Snate@binkert.orgSource('base/traceflags.cc') 8017674Snate@binkert.org 8027674Snate@binkert.org# Generate program_info.cc 8037674Snate@binkert.orgdef programInfo(target, source, env): 8047673Snate@binkert.org def gen_file(target, rev, node, date): 8057674Snate@binkert.org pi_stats = file(target, 'w') 8067674Snate@binkert.org print >>pi_stats, 'const char *hgRev = "%s:%s";' % (rev, node) 8077674Snate@binkert.org print >>pi_stats, 'const char *hgDate = "%s";' % date 8087674Snate@binkert.org pi_stats.close() 8097674Snate@binkert.org 8107674Snate@binkert.org target = str(target[0]) 8117674Snate@binkert.org scons_dir = str(source[0].get_contents()) 8127674Snate@binkert.org try: 8137811Ssteve.reinhardt@amd.com import mercurial.demandimport, mercurial.hg, mercurial.ui 8147674Snate@binkert.org import mercurial.util, mercurial.node 8157673Snate@binkert.org if not exists(scons_dir) or not isdir(scons_dir) or \ 8165522Snate@binkert.org not exists(joinpath(scons_dir, ".hg")): 8176143Snate@binkert.org raise ValueError 8187756SAli.Saidi@ARM.com repo = mercurial.hg.repository(mercurial.ui.ui(), scons_dir) 8197816Ssteve.reinhardt@amd.com rev = mercurial.node.nullrev + repo.changelog.count() 8207674Snate@binkert.org changenode = repo.changelog.node(rev) 8214382Sbinkertn@umich.edu changes = repo.changelog.read(changenode) 8224382Sbinkertn@umich.edu date = mercurial.util.datestr(changes[2]) 8234382Sbinkertn@umich.edu 8244382Sbinkertn@umich.edu gen_file(target, rev, mercurial.node.hex(changenode), date) 8254382Sbinkertn@umich.edu 8264382Sbinkertn@umich.edu mercurial.demandimport.disable() 8274382Sbinkertn@umich.edu except ImportError: 8284382Sbinkertn@umich.edu gen_file(target, "Unknown", "Unknown", "Unknown") 8294382Sbinkertn@umich.edu 8304382Sbinkertn@umich.edu except: 8316143Snate@binkert.org print "in except" 832955SN/A gen_file(target, "Unknown", "Unknown", "Unknown") 8332655Sstever@eecs.umich.edu mercurial.demandimport.disable() 8342655Sstever@eecs.umich.edu 8352655Sstever@eecs.umich.eduenv.Command('base/program_info.cc', 8362655Sstever@eecs.umich.edu Value(str(SCons.Node.FS.default_fs.SConstruct_dir)), 8372655Sstever@eecs.umich.edu programInfo) 8385601Snate@binkert.org 8395601Snate@binkert.org# embed python files. All .py files that have been indicated by a 8408334Snate@binkert.org# PySource() call in a SConscript need to be embedded into the M5 8418334Snate@binkert.org# library. To do that, we compile the file to byte code, marshal the 8428334Snate@binkert.org# byte code, compress it, and then generate an assembly file that 8435522Snate@binkert.org# inserts the result into the data section with symbols indicating the 8445863Snate@binkert.org# beginning, and end (and with the size at the end) 8455601Snate@binkert.orgpy_sources_tnodes = {} 8465601Snate@binkert.orgfor pysource in py_sources: 8475601Snate@binkert.org py_sources_tnodes[pysource.tnode] = pysource 8485863Snate@binkert.org 8498945Ssteve.reinhardt@amd.comdef objectifyPyFile(target, source, env): 8505559Snate@binkert.org '''Action function to compile a .py into a code object, marshal 8515559Snate@binkert.org it, compress it, and stick it into an asm file so the code appears 8525559Snate@binkert.org as just bytes with a label in the data section''' 8535559Snate@binkert.org 8548656Sandreas.hansson@arm.com src = file(str(source[0]), 'r').read() 8558946Sandreas.hansson@arm.com dst = file(str(target[0]), 'w') 8568614Sgblack@eecs.umich.edu 8578737Skoansin.tan@gmail.com pysource = py_sources_tnodes[source[0]] 8588737Skoansin.tan@gmail.com compiled = compile(src, pysource.debugname, 'exec') 8598737Skoansin.tan@gmail.com marshalled = marshal.dumps(compiled) 8608945Ssteve.reinhardt@amd.com compressed = zlib.compress(marshalled) 8618945Ssteve.reinhardt@amd.com data = compressed 8628945Ssteve.reinhardt@amd.com 8638945Ssteve.reinhardt@amd.com # Some C/C++ compilers prepend an underscore to global symbol 8646143Snate@binkert.org # names, so if they're going to do that, we need to prepend that 8656143Snate@binkert.org # leading underscore to globals in the assembly file. 8666143Snate@binkert.org if env['LEADING_UNDERSCORE']: 8676143Snate@binkert.org sym = '_' + pysource.symname 8686143Snate@binkert.org else: 8696143Snate@binkert.org sym = pysource.symname 8706143Snate@binkert.org 8718945Ssteve.reinhardt@amd.com step = 16 8728945Ssteve.reinhardt@amd.com print >>dst, ".data" 8736143Snate@binkert.org print >>dst, ".globl %s_beg" % sym 8746143Snate@binkert.org print >>dst, ".globl %s_end" % sym 8756143Snate@binkert.org print >>dst, "%s_beg:" % sym 8766143Snate@binkert.org for i in xrange(0, len(data), step): 8776143Snate@binkert.org x = array.array('B', data[i:i+step]) 8786143Snate@binkert.org print >>dst, ".byte", ','.join([str(d) for d in x]) 8796143Snate@binkert.org print >>dst, "%s_end:" % sym 8806143Snate@binkert.org print >>dst, ".long %d" % len(marshalled) 8816143Snate@binkert.org 8826143Snate@binkert.orgfor source in py_sources: 8836143Snate@binkert.org env.Command(source.assembly, source.tnode, objectifyPyFile) 8846143Snate@binkert.org Source(source.assembly) 8856143Snate@binkert.org 8868594Snate@binkert.org# Generate init_python.cc which creates a bunch of EmbeddedPyModule 8878594Snate@binkert.org# structs that describe the embedded python code. One such struct 8888594Snate@binkert.org# contains information about the importer that python uses to get at 8898594Snate@binkert.org# the embedded files, and then there's a list of all of the rest that 8906143Snate@binkert.org# the importer uses to load the rest on demand. 8916143Snate@binkert.orgpy_sources_symbols = {} 8926143Snate@binkert.orgfor pysource in py_sources: 8936143Snate@binkert.org py_sources_symbols[pysource.symname] = pysource 8946143Snate@binkert.orgdef pythonInit(target, source, env): 8956240Snate@binkert.org dst = file(str(target[0]), 'w') 8965554Snate@binkert.org 8975522Snate@binkert.org def dump_mod(sym, endchar=','): 8985522Snate@binkert.org pysource = py_sources_symbols[sym] 8995797Snate@binkert.org print >>dst, ' { "%s",' % pysource.arcname 9005797Snate@binkert.org print >>dst, ' "%s",' % pysource.modpath 9015522Snate@binkert.org print >>dst, ' %s_beg, %s_end,' % (sym, sym) 9025601Snate@binkert.org print >>dst, ' %s_end - %s_beg,' % (sym, sym) 9038233Snate@binkert.org print >>dst, ' *(int *)%s_end }%s' % (sym, endchar) 9048233Snate@binkert.org 9058235Snate@binkert.org print >>dst, '#include "sim/init.hh"' 9068235Snate@binkert.org 9078235Snate@binkert.org for sym in source: 9088235Snate@binkert.org sym = sym.get_contents() 9098235Snate@binkert.org print >>dst, "extern const char %s_beg[], %s_end[];" % (sym, sym) 9108942Sgblack@eecs.umich.edu 9118235Snate@binkert.org print >>dst, "const EmbeddedPyModule embeddedPyImporter = " 9126143Snate@binkert.org dump_mod("PyEMB_importer", endchar=';'); 9132655Sstever@eecs.umich.edu print >>dst 9146143Snate@binkert.org 9156143Snate@binkert.org print >>dst, "const EmbeddedPyModule embeddedPyModules[] = {" 9168233Snate@binkert.org for i,sym in enumerate(source): 9176143Snate@binkert.org sym = sym.get_contents() 9186143Snate@binkert.org if sym == "PyEMB_importer": 9194007Ssaidi@eecs.umich.edu # Skip the importer since we've already exported it 9204596Sbinkertn@umich.edu continue 9214007Ssaidi@eecs.umich.edu dump_mod(sym) 9224596Sbinkertn@umich.edu print >>dst, " { 0, 0, 0, 0, 0, 0 }" 9237756SAli.Saidi@ARM.com print >>dst, "};" 9247816Ssteve.reinhardt@amd.com 9258334Snate@binkert.orgsymbols = [Value(s.symname) for s in py_sources] 9268334Snate@binkert.orgenv.Command('sim/init_python.cc', symbols, pythonInit) 9278334Snate@binkert.orgSource('sim/init_python.cc') 9288334Snate@binkert.org 9295601Snate@binkert.org######################################################################## 9305601Snate@binkert.org# 9312655Sstever@eecs.umich.edu# Define binaries. Each different build type (debug, opt, etc.) gets 932955SN/A# a slightly different build environment. 9333918Ssaidi@eecs.umich.edu# 9348946Sandreas.hansson@arm.com 9353918Ssaidi@eecs.umich.edu# List of constructed environments to pass back to SConstruct 9363918Ssaidi@eecs.umich.eduenvList = [] 9373918Ssaidi@eecs.umich.edu 9383918Ssaidi@eecs.umich.edu# This function adds the specified sources to the given build 9393918Ssaidi@eecs.umich.edu# environment, and returns a list of all the corresponding SCons 9403918Ssaidi@eecs.umich.edu# Object nodes (including an extra one for date.cc). We explicitly 9413918Ssaidi@eecs.umich.edu# add the Object nodes so we can set up special dependencies for 9423918Ssaidi@eecs.umich.edu# date.cc. 9433918Ssaidi@eecs.umich.edudef make_objs(sources, env, static): 9443918Ssaidi@eecs.umich.edu if static: 9453918Ssaidi@eecs.umich.edu XObject = env.StaticObject 9463918Ssaidi@eecs.umich.edu else: 9473940Ssaidi@eecs.umich.edu XObject = env.SharedObject 9483940Ssaidi@eecs.umich.edu 9493940Ssaidi@eecs.umich.edu objs = [ XObject(s) for s in sources ] 9503942Ssaidi@eecs.umich.edu 9513940Ssaidi@eecs.umich.edu # make date.cc depend on all other objects so it always gets 9528946Sandreas.hansson@arm.com # recompiled whenever anything else does 9538946Sandreas.hansson@arm.com date_obj = XObject('base/date.cc') 9548946Sandreas.hansson@arm.com 9558946Sandreas.hansson@arm.com # Make the generation of program_info.cc dependend on all 9568946Sandreas.hansson@arm.com # the other cc files and the compiling of program_info.cc 9573515Ssaidi@eecs.umich.edu # dependent on all the objects but program_info.o 9583918Ssaidi@eecs.umich.edu pinfo_obj = XObject('base/program_info.cc') 9594762Snate@binkert.org env.Depends('base/program_info.cc', sources) 9603515Ssaidi@eecs.umich.edu env.Depends(date_obj, objs) 9618881Smarc.orr@gmail.com env.Depends(pinfo_obj, objs) 9628881Smarc.orr@gmail.com objs.extend([date_obj, pinfo_obj]) 9638881Smarc.orr@gmail.com return objs 9648881Smarc.orr@gmail.com 9658881Smarc.orr@gmail.com# Function to create a new build environment as clone of current 9668881Smarc.orr@gmail.com# environment 'env' with modified object suffix and optional stripped 9678881Smarc.orr@gmail.com# binary. Additional keyword arguments are appended to corresponding 9688881Smarc.orr@gmail.com# build environment vars. 9698881Smarc.orr@gmail.comdef makeEnv(label, objsfx, strip = False, **kwargs): 9708881Smarc.orr@gmail.com # SCons doesn't know to append a library suffix when there is a '.' in the 9718881Smarc.orr@gmail.com # name. Use '_' instead. 9728881Smarc.orr@gmail.com libname = 'm5_' + label 9738881Smarc.orr@gmail.com exename = 'm5.' + label 9748881Smarc.orr@gmail.com 9758881Smarc.orr@gmail.com new_env = env.Copy(OBJSUFFIX=objsfx, SHOBJSUFFIX=objsfx + 's') 9768881Smarc.orr@gmail.com new_env.Label = label 9778881Smarc.orr@gmail.com new_env.Append(**kwargs) 9788881Smarc.orr@gmail.com 9798881Smarc.orr@gmail.com swig_env = new_env.Copy() 9808881Smarc.orr@gmail.com if env['GCC']: 9818881Smarc.orr@gmail.com swig_env.Append(CCFLAGS='-Wno-uninitialized') 9828881Smarc.orr@gmail.com swig_env.Append(CCFLAGS='-Wno-sign-compare') 9838881Smarc.orr@gmail.com swig_env.Append(CCFLAGS='-Wno-parentheses') 9848881Smarc.orr@gmail.com 9858881Smarc.orr@gmail.com static_objs = make_objs(cc_lib_sources, new_env, static=True) 9868881Smarc.orr@gmail.com shared_objs = make_objs(cc_lib_sources, new_env, static=False) 9878881Smarc.orr@gmail.com static_objs += [ swig_env.StaticObject(s) for s in cc_swig_sources ] 9888881Smarc.orr@gmail.com shared_objs += [ swig_env.SharedObject(s) for s in cc_swig_sources ] 989955SN/A 990955SN/A # First make a library of everything but main() so other programs can 9918881Smarc.orr@gmail.com # link against m5. 9928881Smarc.orr@gmail.com static_lib = new_env.StaticLibrary(libname, static_objs + static_objs) 9938881Smarc.orr@gmail.com shared_lib = new_env.SharedLibrary(libname, shared_objs + shared_objs) 9948881Smarc.orr@gmail.com 995955SN/A for target, sources in unit_tests: 996955SN/A objs = [ new_env.StaticObject(s) for s in sources ] 9978881Smarc.orr@gmail.com new_env.Program("unittest/%s.%s" % (target, label), objs + static_lib) 9988881Smarc.orr@gmail.com 9998881Smarc.orr@gmail.com # Now link a stub with main() and the static library. 10008881Smarc.orr@gmail.com objects = [new_env.Object(s) for s in cc_bin_sources] + static_lib 1001955SN/A if strip: 1002955SN/A unstripped_exe = exename + '.unstripped' 10038881Smarc.orr@gmail.com new_env.Program(unstripped_exe, objects) 10048881Smarc.orr@gmail.com if sys.platform == 'sunos5': 10058881Smarc.orr@gmail.com cmd = 'cp $SOURCE $TARGET; strip $TARGET' 10068881Smarc.orr@gmail.com else: 10078881Smarc.orr@gmail.com cmd = 'strip $SOURCE -o $TARGET' 10081869SN/A targets = new_env.Command(exename, unstripped_exe, cmd) 10091869SN/A else: 1010 targets = new_env.Program(exename, objects) 1011 1012 new_env.M5Binary = targets[0] 1013 envList.append(new_env) 1014 1015# Debug binary 1016ccflags = {} 1017if env['GCC']: 1018 if sys.platform == 'sunos5': 1019 ccflags['debug'] = '-gstabs+' 1020 else: 1021 ccflags['debug'] = '-ggdb3' 1022 ccflags['opt'] = '-g -O3' 1023 ccflags['fast'] = '-O3' 1024 ccflags['prof'] = '-O3 -g -pg' 1025elif env['SUNCC']: 1026 ccflags['debug'] = '-g0' 1027 ccflags['opt'] = '-g -O' 1028 ccflags['fast'] = '-fast' 1029 ccflags['prof'] = '-fast -g -pg' 1030elif env['ICC']: 1031 ccflags['debug'] = '-g -O0' 1032 ccflags['opt'] = '-g -O' 1033 ccflags['fast'] = '-fast' 1034 ccflags['prof'] = '-fast -g -pg' 1035else: 1036 print 'Unknown compiler, please fix compiler options' 1037 Exit(1) 1038 1039makeEnv('debug', '.do', 1040 CCFLAGS = Split(ccflags['debug']), 1041 CPPDEFINES = ['DEBUG', 'TRACING_ON=1']) 1042 1043# Optimized binary 1044makeEnv('opt', '.o', 1045 CCFLAGS = Split(ccflags['opt']), 1046 CPPDEFINES = ['TRACING_ON=1']) 1047 1048# "Fast" binary 1049makeEnv('fast', '.fo', strip = True, 1050 CCFLAGS = Split(ccflags['fast']), 1051 CPPDEFINES = ['NDEBUG', 'TRACING_ON=0']) 1052 1053# Profiled binary 1054makeEnv('prof', '.po', 1055 CCFLAGS = Split(ccflags['prof']), 1056 CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'], 1057 LINKFLAGS = '-pg') 1058 1059Return('envList') 1060