1# -*- mode:python -*- 2 3# Copyright (c) 2004-2005 The Regents of The University of Michigan 4# All rights reserved. 5# 6# Redistribution and use in source and binary forms, with or without 7# modification, are permitted provided that the following conditions are 8# met: redistributions of source code must retain the above copyright 9# notice, this list of conditions and the following disclaimer; 10# redistributions in binary form must reproduce the above copyright 11# notice, this list of conditions and the following disclaimer in the 12# documentation and/or other materials provided with the distribution; 13# neither the name of the copyright holders nor the names of its 14# contributors may be used to endorse or promote products derived from 15# this software without specific prior written permission. 16# 17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28# 29# Authors: Nathan Binkert 30
| 1# -*- mode:python -*- 2 3# Copyright (c) 2004-2005 The Regents of The University of Michigan 4# All rights reserved. 5# 6# Redistribution and use in source and binary forms, with or without 7# modification, are permitted provided that the following conditions are 8# met: redistributions of source code must retain the above copyright 9# notice, this list of conditions and the following disclaimer; 10# redistributions in binary form must reproduce the above copyright 11# notice, this list of conditions and the following disclaimer in the 12# documentation and/or other materials provided with the distribution; 13# neither the name of the copyright holders nor the names of its 14# contributors may be used to endorse or promote products derived from 15# this software without specific prior written permission. 16# 17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28# 29# Authors: Nathan Binkert 30
|
| 31import array
|
31import imp
| 32import imp
|
| 33import marshal
|
32import os
| 34import os
|
33import py_compile
| 35import re
|
34import sys
| 36import sys
|
35import zipfile
| 37import zlib
|
36 37from os.path import basename, exists, isdir, isfile, join as joinpath 38 39import SCons 40 41# This file defines how to build a particular configuration of M5 42# based on variable settings in the 'env' build environment. 43 44Import('*') 45 46# Children need to see the environment 47Export('env') 48 49build_env = dict([(opt, env[opt]) for opt in env.ExportOptions]) 50 51def sort_list(_list): 52 """return a sorted copy of '_list'""" 53 if isinstance(_list, list): 54 _list = _list[:] 55 else: 56 _list = list(_list) 57 _list.sort() 58 return _list 59 60class PySourceFile(object):
| 38 39from os.path import basename, exists, isdir, isfile, join as joinpath 40 41import SCons 42 43# This file defines how to build a particular configuration of M5 44# based on variable settings in the 'env' build environment. 45 46Import('*') 47 48# Children need to see the environment 49Export('env') 50 51build_env = dict([(opt, env[opt]) for opt in env.ExportOptions]) 52 53def sort_list(_list): 54 """return a sorted copy of '_list'""" 55 if isinstance(_list, list): 56 _list = _list[:] 57 else: 58 _list = list(_list) 59 _list.sort() 60 return _list 61 62class PySourceFile(object):
|
| 63 invalid_sym_char = re.compile('[^A-z0-9_]')
|
61 def __init__(self, package, source): 62 filename = str(source) 63 pyname = basename(filename) 64 assert pyname.endswith('.py') 65 name = pyname[:-3]
| 64 def __init__(self, package, source): 65 filename = str(source) 66 pyname = basename(filename) 67 assert pyname.endswith('.py') 68 name = pyname[:-3]
|
66 path = package.split('.')
| 69 if package: 70 path = package.split('.') 71 else: 72 path = []
|
67 modpath = path 68 if name != '__init__': 69 modpath += [name] 70 modpath = '.'.join(modpath) 71
| 73 modpath = path 74 if name != '__init__': 75 modpath += [name] 76 modpath = '.'.join(modpath) 77
|
72 arcpath = package.split('.') + [ pyname + 'c' ]
| 78 arcpath = path + [ pyname ]
|
73 arcname = joinpath(*arcpath) 74
| 79 arcname = joinpath(*arcpath) 80
|
75 self.source = source
| 81 self.tnode = source 82 self.snode = source.srcnode()
|
76 self.pyname = pyname
| 83 self.pyname = pyname
|
77 self.srcpath = source.srcnode().abspath
| |
78 self.package = package 79 self.modpath = modpath 80 self.arcname = arcname 81 self.filename = filename 82 self.compiled = File(filename + 'c')
| 84 self.package = package 85 self.modpath = modpath 86 self.arcname = arcname 87 self.filename = filename 88 self.compiled = File(filename + 'c')
|
| 89 self.assembly = File(filename + '.s') 90 self.symname = "PyEMB_" + self.invalid_sym_char.sub('_', modpath) 91
|
83 84######################################################################## 85# Code for adding source files of various types 86#
| 92 93######################################################################## 94# Code for adding source files of various types 95#
|
87cc_sources = []
| 96cc_lib_sources = []
|
88def Source(source):
| 97def Source(source):
|
89 '''Add a C/C++ source file to the build'''
| 98 '''Add a source file to the libm5 build'''
|
90 if not isinstance(source, SCons.Node.FS.File): 91 source = File(source) 92
| 99 if not isinstance(source, SCons.Node.FS.File): 100 source = File(source) 101
|
93 cc_sources.append(source)
| 102 cc_lib_sources.append(source)
|
94
| 103
|
| 104cc_bin_sources = [] 105def BinSource(source): 106 '''Add a source file to the m5 binary build''' 107 if not isinstance(source, SCons.Node.FS.File): 108 source = File(source) 109 110 cc_bin_sources.append(source) 111
|
95py_sources = [] 96def PySource(package, source): 97 '''Add a python source file to the named package''' 98 if not isinstance(source, SCons.Node.FS.File): 99 source = File(source) 100 101 source = PySourceFile(package, source) 102 py_sources.append(source) 103 104sim_objects_fixed = False 105sim_object_modfiles = set() 106def SimObject(source): 107 '''Add a SimObject python file as a python source object and add 108 it to a list of sim object modules''' 109 110 if sim_objects_fixed: 111 raise AttributeError, "Too late to call SimObject now." 112 113 if not isinstance(source, SCons.Node.FS.File): 114 source = File(source) 115 116 PySource('m5.objects', source) 117 modfile = basename(str(source)) 118 assert modfile.endswith('.py') 119 modname = modfile[:-3] 120 sim_object_modfiles.add(modname) 121 122swig_sources = [] 123def SwigSource(package, source): 124 '''Add a swig file to build''' 125 if not isinstance(source, SCons.Node.FS.File): 126 source = File(source) 127 val = source,package 128 swig_sources.append(val) 129 130# Children should have access 131Export('Source')
| 112py_sources = [] 113def PySource(package, source): 114 '''Add a python source file to the named package''' 115 if not isinstance(source, SCons.Node.FS.File): 116 source = File(source) 117 118 source = PySourceFile(package, source) 119 py_sources.append(source) 120 121sim_objects_fixed = False 122sim_object_modfiles = set() 123def SimObject(source): 124 '''Add a SimObject python file as a python source object and add 125 it to a list of sim object modules''' 126 127 if sim_objects_fixed: 128 raise AttributeError, "Too late to call SimObject now." 129 130 if not isinstance(source, SCons.Node.FS.File): 131 source = File(source) 132 133 PySource('m5.objects', source) 134 modfile = basename(str(source)) 135 assert modfile.endswith('.py') 136 modname = modfile[:-3] 137 sim_object_modfiles.add(modname) 138 139swig_sources = [] 140def SwigSource(package, source): 141 '''Add a swig file to build''' 142 if not isinstance(source, SCons.Node.FS.File): 143 source = File(source) 144 val = source,package 145 swig_sources.append(val) 146 147# Children should have access 148Export('Source')
|
| 149Export('BinSource')
|
132Export('PySource') 133Export('SimObject') 134Export('SwigSource') 135 136######################################################################## 137# 138# Trace Flags 139# 140all_flags = {} 141trace_flags = [] 142def TraceFlag(name, desc=''): 143 if name in all_flags: 144 raise AttributeError, "Flag %s already specified" % name 145 flag = (name, (), desc) 146 trace_flags.append(flag) 147 all_flags[name] = () 148 149def CompoundFlag(name, flags, desc=''): 150 if name in all_flags: 151 raise AttributeError, "Flag %s already specified" % name 152 153 compound = tuple(flags) 154 for flag in compound: 155 if flag not in all_flags: 156 raise AttributeError, "Trace flag %s not found" % flag 157 if all_flags[flag]: 158 raise AttributeError, \ 159 "Compound flag can't point to another compound flag" 160 161 flag = (name, compound, desc) 162 trace_flags.append(flag) 163 all_flags[name] = compound 164 165Export('TraceFlag') 166Export('CompoundFlag') 167 168######################################################################## 169# 170# Set some compiler variables 171# 172 173# Include file paths are rooted in this directory. SCons will 174# automatically expand '.' to refer to both the source directory and 175# the corresponding build directory to pick up generated include 176# files. 177env.Append(CPPPATH=Dir('.')) 178 179# Add a flag defining what THE_ISA should be for all compilation 180env.Append(CPPDEFINES=[('THE_ISA','%s_ISA' % env['TARGET_ISA'].upper())]) 181 182######################################################################## 183# 184# Walk the tree and execute all SConscripts in subdirectories 185# 186 187for base_dir in base_dir_list: 188 here = Dir('.').srcnode().abspath 189 for root, dirs, files in os.walk(base_dir, topdown=True): 190 if root == here: 191 # we don't want to recurse back into this SConscript 192 continue 193 194 if 'SConscript' in files: 195 build_dir = joinpath(env['BUILDDIR'], root[len(base_dir) + 1:]) 196 SConscript(joinpath(root, 'SConscript'), build_dir=build_dir) 197 198for opt in env.ExportOptions: 199 env.ConfigFile(opt) 200 201######################################################################## 202# 203# Prevent any SimObjects from being added after this point, they 204# should all have been added in the SConscripts above 205# 206class DictImporter(object): 207 '''This importer takes a dictionary of arbitrary module names that 208 map to arbitrary filenames.''' 209 def __init__(self, modules): 210 self.modules = modules 211 self.installed = set() 212 213 def __del__(self): 214 self.unload() 215 216 def unload(self): 217 import sys 218 for module in self.installed: 219 del sys.modules[module] 220 self.installed = set() 221 222 def find_module(self, fullname, path): 223 if fullname == '__scons': 224 return self 225 226 if fullname == 'm5.objects': 227 return self 228 229 if fullname.startswith('m5.internal'): 230 return None 231 232 if fullname in self.modules and exists(self.modules[fullname]): 233 return self 234 235 return None 236 237 def load_module(self, fullname): 238 mod = imp.new_module(fullname) 239 sys.modules[fullname] = mod 240 self.installed.add(fullname) 241 242 mod.__loader__ = self 243 if fullname == 'm5.objects': 244 mod.__path__ = fullname.split('.') 245 return mod 246 247 if fullname == '__scons': 248 mod.__dict__['m5_build_env'] = build_env 249 return mod 250 251 srcfile = self.modules[fullname] 252 if basename(srcfile) == '__init__.py': 253 mod.__path__ = fullname.split('.') 254 mod.__file__ = srcfile 255 256 exec file(srcfile, 'r') in mod.__dict__ 257 258 return mod 259 260py_modules = {} 261for source in py_sources:
| 150Export('PySource') 151Export('SimObject') 152Export('SwigSource') 153 154######################################################################## 155# 156# Trace Flags 157# 158all_flags = {} 159trace_flags = [] 160def TraceFlag(name, desc=''): 161 if name in all_flags: 162 raise AttributeError, "Flag %s already specified" % name 163 flag = (name, (), desc) 164 trace_flags.append(flag) 165 all_flags[name] = () 166 167def CompoundFlag(name, flags, desc=''): 168 if name in all_flags: 169 raise AttributeError, "Flag %s already specified" % name 170 171 compound = tuple(flags) 172 for flag in compound: 173 if flag not in all_flags: 174 raise AttributeError, "Trace flag %s not found" % flag 175 if all_flags[flag]: 176 raise AttributeError, \ 177 "Compound flag can't point to another compound flag" 178 179 flag = (name, compound, desc) 180 trace_flags.append(flag) 181 all_flags[name] = compound 182 183Export('TraceFlag') 184Export('CompoundFlag') 185 186######################################################################## 187# 188# Set some compiler variables 189# 190 191# Include file paths are rooted in this directory. SCons will 192# automatically expand '.' to refer to both the source directory and 193# the corresponding build directory to pick up generated include 194# files. 195env.Append(CPPPATH=Dir('.')) 196 197# Add a flag defining what THE_ISA should be for all compilation 198env.Append(CPPDEFINES=[('THE_ISA','%s_ISA' % env['TARGET_ISA'].upper())]) 199 200######################################################################## 201# 202# Walk the tree and execute all SConscripts in subdirectories 203# 204 205for base_dir in base_dir_list: 206 here = Dir('.').srcnode().abspath 207 for root, dirs, files in os.walk(base_dir, topdown=True): 208 if root == here: 209 # we don't want to recurse back into this SConscript 210 continue 211 212 if 'SConscript' in files: 213 build_dir = joinpath(env['BUILDDIR'], root[len(base_dir) + 1:]) 214 SConscript(joinpath(root, 'SConscript'), build_dir=build_dir) 215 216for opt in env.ExportOptions: 217 env.ConfigFile(opt) 218 219######################################################################## 220# 221# Prevent any SimObjects from being added after this point, they 222# should all have been added in the SConscripts above 223# 224class DictImporter(object): 225 '''This importer takes a dictionary of arbitrary module names that 226 map to arbitrary filenames.''' 227 def __init__(self, modules): 228 self.modules = modules 229 self.installed = set() 230 231 def __del__(self): 232 self.unload() 233 234 def unload(self): 235 import sys 236 for module in self.installed: 237 del sys.modules[module] 238 self.installed = set() 239 240 def find_module(self, fullname, path): 241 if fullname == '__scons': 242 return self 243 244 if fullname == 'm5.objects': 245 return self 246 247 if fullname.startswith('m5.internal'): 248 return None 249 250 if fullname in self.modules and exists(self.modules[fullname]): 251 return self 252 253 return None 254 255 def load_module(self, fullname): 256 mod = imp.new_module(fullname) 257 sys.modules[fullname] = mod 258 self.installed.add(fullname) 259 260 mod.__loader__ = self 261 if fullname == 'm5.objects': 262 mod.__path__ = fullname.split('.') 263 return mod 264 265 if fullname == '__scons': 266 mod.__dict__['m5_build_env'] = build_env 267 return mod 268 269 srcfile = self.modules[fullname] 270 if basename(srcfile) == '__init__.py': 271 mod.__path__ = fullname.split('.') 272 mod.__file__ = srcfile 273 274 exec file(srcfile, 'r') in mod.__dict__ 275 276 return mod 277 278py_modules = {} 279for source in py_sources:
|
262 py_modules[source.modpath] = source.srcpath
| 280 py_modules[source.modpath] = source.snode.abspath
|
263 264# install the python importer so we can grab stuff from the source 265# tree itself. We can't have SimObjects added after this point or 266# else we won't know about them for the rest of the stuff. 267sim_objects_fixed = True 268importer = DictImporter(py_modules) 269sys.meta_path[0:0] = [ importer ] 270 271import m5 272 273# import all sim objects so we can populate the all_objects list 274# make sure that we're working with a list, then let's sort it 275sim_objects = list(sim_object_modfiles) 276sim_objects.sort() 277for simobj in sim_objects: 278 exec('from m5.objects import %s' % simobj) 279 280# we need to unload all of the currently imported modules so that they 281# will be re-imported the next time the sconscript is run 282importer.unload() 283sys.meta_path.remove(importer) 284 285sim_objects = m5.SimObject.allClasses 286all_enums = m5.params.allEnums 287 288all_params = {} 289for name,obj in sim_objects.iteritems(): 290 for param in obj._params.local.values(): 291 if not hasattr(param, 'swig_decl'): 292 continue 293 pname = param.ptype_str 294 if pname not in all_params: 295 all_params[pname] = param 296 297######################################################################## 298# 299# calculate extra dependencies 300# 301module_depends = ["m5", "m5.SimObject", "m5.params"] 302depends = [ File(py_modules[dep]) for dep in module_depends ] 303 304######################################################################## 305# 306# Commands for the basic automatically generated python files 307# 308 309# Generate Python file containing a dict specifying the current 310# build_env flags. 311def makeDefinesPyFile(target, source, env): 312 f = file(str(target[0]), 'w') 313 print >>f, "m5_build_env = ", source[0] 314 f.close() 315 316# Generate python file containing info about the M5 source code 317def makeInfoPyFile(target, source, env): 318 f = file(str(target[0]), 'w') 319 for src in source: 320 data = ''.join(file(src.srcnode().abspath, 'r').xreadlines()) 321 print >>f, "%s = %s" % (src, repr(data)) 322 f.close() 323 324# Generate the __init__.py file for m5.objects 325def makeObjectsInitFile(target, source, env): 326 f = file(str(target[0]), 'w') 327 print >>f, 'from params import *' 328 print >>f, 'from m5.SimObject import *' 329 for module in source: 330 print >>f, 'from %s import *' % module.get_contents() 331 f.close() 332 333# Generate a file with all of the compile options in it 334env.Command('python/m5/defines.py', Value(build_env), makeDefinesPyFile) 335PySource('m5', 'python/m5/defines.py') 336 337# Generate a file that wraps the basic top level files 338env.Command('python/m5/info.py', 339 [ '#/AUTHORS', '#/LICENSE', '#/README', '#/RELEASE_NOTES' ], 340 makeInfoPyFile) 341PySource('m5', 'python/m5/info.py') 342 343# Generate an __init__.py file for the objects package 344env.Command('python/m5/objects/__init__.py', 345 [ Value(o) for o in sort_list(sim_object_modfiles) ], 346 makeObjectsInitFile) 347PySource('m5.objects', 'python/m5/objects/__init__.py') 348 349######################################################################## 350# 351# Create all of the SimObject param headers and enum headers 352# 353 354def createSimObjectParam(target, source, env): 355 assert len(target) == 1 and len(source) == 1 356 357 hh_file = file(target[0].abspath, 'w') 358 name = str(source[0].get_contents()) 359 obj = sim_objects[name] 360 361 print >>hh_file, obj.cxx_decl() 362 363def createSwigParam(target, source, env): 364 assert len(target) == 1 and len(source) == 1 365 366 i_file = file(target[0].abspath, 'w') 367 name = str(source[0].get_contents()) 368 param = all_params[name] 369 370 for line in param.swig_decl(): 371 print >>i_file, line 372 373def createEnumStrings(target, source, env): 374 assert len(target) == 1 and len(source) == 1 375 376 cc_file = file(target[0].abspath, 'w') 377 name = str(source[0].get_contents()) 378 obj = all_enums[name] 379 380 print >>cc_file, obj.cxx_def() 381 cc_file.close() 382 383def createEnumParam(target, source, env): 384 assert len(target) == 1 and len(source) == 1 385 386 hh_file = file(target[0].abspath, 'w') 387 name = str(source[0].get_contents()) 388 obj = all_enums[name] 389 390 print >>hh_file, obj.cxx_decl() 391 392# Generate all of the SimObject param struct header files 393params_hh_files = [] 394for name,simobj in sim_objects.iteritems(): 395 extra_deps = [ File(py_modules[simobj.__module__]) ] 396 397 hh_file = File('params/%s.hh' % name) 398 params_hh_files.append(hh_file) 399 env.Command(hh_file, Value(name), createSimObjectParam) 400 env.Depends(hh_file, depends + extra_deps) 401 402# Generate any parameter header files needed 403params_i_files = [] 404for name,param in all_params.iteritems(): 405 if isinstance(param, m5.params.VectorParamDesc): 406 ext = 'vptype' 407 else: 408 ext = 'ptype' 409 410 i_file = File('params/%s_%s.i' % (name, ext)) 411 params_i_files.append(i_file) 412 env.Command(i_file, Value(name), createSwigParam) 413 env.Depends(i_file, depends) 414 415# Generate all enum header files 416for name,enum in all_enums.iteritems(): 417 extra_deps = [ File(py_modules[enum.__module__]) ] 418 419 cc_file = File('enums/%s.cc' % name) 420 env.Command(cc_file, Value(name), createEnumStrings) 421 env.Depends(cc_file, depends + extra_deps) 422 Source(cc_file) 423 424 hh_file = File('enums/%s.hh' % name) 425 env.Command(hh_file, Value(name), createEnumParam) 426 env.Depends(hh_file, depends + extra_deps) 427 428# Build the big monolithic swigged params module (wraps all SimObject 429# param structs and enum structs) 430def buildParams(target, source, env): 431 names = [ s.get_contents() for s in source ] 432 objs = [ sim_objects[name] for name in names ] 433 out = file(target[0].abspath, 'w') 434 435 ordered_objs = [] 436 obj_seen = set() 437 def order_obj(obj): 438 name = str(obj) 439 if name in obj_seen: 440 return 441 442 obj_seen.add(name) 443 if str(obj) != 'SimObject': 444 order_obj(obj.__bases__[0]) 445 446 ordered_objs.append(obj) 447 448 for obj in objs: 449 order_obj(obj) 450 451 enums = set() 452 predecls = [] 453 pd_seen = set() 454 455 def add_pds(*pds): 456 for pd in pds: 457 if pd not in pd_seen: 458 predecls.append(pd) 459 pd_seen.add(pd) 460 461 for obj in ordered_objs: 462 params = obj._params.local.values() 463 for param in params: 464 ptype = param.ptype 465 if issubclass(ptype, m5.params.Enum): 466 if ptype not in enums: 467 enums.add(ptype) 468 pds = param.swig_predecls() 469 if isinstance(pds, (list, tuple)): 470 add_pds(*pds) 471 else: 472 add_pds(pds) 473 474 print >>out, '%module params' 475 476 print >>out, '%{' 477 for obj in ordered_objs: 478 print >>out, '#include "params/%s.hh"' % obj 479 print >>out, '%}' 480 481 for pd in predecls: 482 print >>out, pd 483 484 enums = list(enums) 485 enums.sort() 486 for enum in enums: 487 print >>out, '%%include "enums/%s.hh"' % enum.__name__ 488 print >>out 489 490 for obj in ordered_objs: 491 if obj.swig_objdecls: 492 for decl in obj.swig_objdecls: 493 print >>out, decl 494 continue 495 496 code = '' 497 base = obj.get_base() 498 499 code += '// stop swig from creating/wrapping default ctor/dtor\n' 500 code += '%%nodefault %s;\n' % obj.cxx_class 501 code += 'class %s ' % obj.cxx_class 502 if base: 503 code += ': public %s' % base 504 code += ' {};\n' 505 506 klass = obj.cxx_class; 507 if hasattr(obj, 'cxx_namespace'): 508 new_code = 'namespace %s {\n' % obj.cxx_namespace 509 new_code += code 510 new_code += '}\n' 511 code = new_code 512 klass = '%s::%s' % (obj.cxx_namespace, klass) 513 514 print >>out, code 515 516 print >>out, '%%include "src/sim/sim_object_params.hh"' % obj 517 for obj in ordered_objs: 518 print >>out, '%%include "params/%s.hh"' % obj 519 520params_file = File('params/params.i') 521names = sort_list(sim_objects.keys()) 522env.Command(params_file, [ Value(v) for v in names ], buildParams) 523env.Depends(params_file, params_hh_files + params_i_files + depends) 524SwigSource('m5.objects', params_file) 525 526# Build all swig modules 527swig_modules = [] 528for source,package in swig_sources: 529 filename = str(source) 530 assert filename.endswith('.i') 531 532 base = '.'.join(filename.split('.')[:-1]) 533 module = basename(base) 534 cc_file = base + '_wrap.cc' 535 py_file = base + '.py' 536 537 env.Command([cc_file, py_file], source, 538 '$SWIG $SWIGFLAGS -outdir ${TARGETS[1].dir} ' 539 '-o ${TARGETS[0]} $SOURCES') 540 env.Depends(py_file, source) 541 env.Depends(cc_file, source) 542 543 swig_modules.append(Value(module)) 544 Source(cc_file) 545 PySource(package, py_file) 546 547# Generate the main swig init file 548def makeSwigInit(target, source, env): 549 f = file(str(target[0]), 'w') 550 print >>f, 'extern "C" {' 551 for module in source: 552 print >>f, ' void init_%s();' % module.get_contents() 553 print >>f, '}'
| 281 282# install the python importer so we can grab stuff from the source 283# tree itself. We can't have SimObjects added after this point or 284# else we won't know about them for the rest of the stuff. 285sim_objects_fixed = True 286importer = DictImporter(py_modules) 287sys.meta_path[0:0] = [ importer ] 288 289import m5 290 291# import all sim objects so we can populate the all_objects list 292# make sure that we're working with a list, then let's sort it 293sim_objects = list(sim_object_modfiles) 294sim_objects.sort() 295for simobj in sim_objects: 296 exec('from m5.objects import %s' % simobj) 297 298# we need to unload all of the currently imported modules so that they 299# will be re-imported the next time the sconscript is run 300importer.unload() 301sys.meta_path.remove(importer) 302 303sim_objects = m5.SimObject.allClasses 304all_enums = m5.params.allEnums 305 306all_params = {} 307for name,obj in sim_objects.iteritems(): 308 for param in obj._params.local.values(): 309 if not hasattr(param, 'swig_decl'): 310 continue 311 pname = param.ptype_str 312 if pname not in all_params: 313 all_params[pname] = param 314 315######################################################################## 316# 317# calculate extra dependencies 318# 319module_depends = ["m5", "m5.SimObject", "m5.params"] 320depends = [ File(py_modules[dep]) for dep in module_depends ] 321 322######################################################################## 323# 324# Commands for the basic automatically generated python files 325# 326 327# Generate Python file containing a dict specifying the current 328# build_env flags. 329def makeDefinesPyFile(target, source, env): 330 f = file(str(target[0]), 'w') 331 print >>f, "m5_build_env = ", source[0] 332 f.close() 333 334# Generate python file containing info about the M5 source code 335def makeInfoPyFile(target, source, env): 336 f = file(str(target[0]), 'w') 337 for src in source: 338 data = ''.join(file(src.srcnode().abspath, 'r').xreadlines()) 339 print >>f, "%s = %s" % (src, repr(data)) 340 f.close() 341 342# Generate the __init__.py file for m5.objects 343def makeObjectsInitFile(target, source, env): 344 f = file(str(target[0]), 'w') 345 print >>f, 'from params import *' 346 print >>f, 'from m5.SimObject import *' 347 for module in source: 348 print >>f, 'from %s import *' % module.get_contents() 349 f.close() 350 351# Generate a file with all of the compile options in it 352env.Command('python/m5/defines.py', Value(build_env), makeDefinesPyFile) 353PySource('m5', 'python/m5/defines.py') 354 355# Generate a file that wraps the basic top level files 356env.Command('python/m5/info.py', 357 [ '#/AUTHORS', '#/LICENSE', '#/README', '#/RELEASE_NOTES' ], 358 makeInfoPyFile) 359PySource('m5', 'python/m5/info.py') 360 361# Generate an __init__.py file for the objects package 362env.Command('python/m5/objects/__init__.py', 363 [ Value(o) for o in sort_list(sim_object_modfiles) ], 364 makeObjectsInitFile) 365PySource('m5.objects', 'python/m5/objects/__init__.py') 366 367######################################################################## 368# 369# Create all of the SimObject param headers and enum headers 370# 371 372def createSimObjectParam(target, source, env): 373 assert len(target) == 1 and len(source) == 1 374 375 hh_file = file(target[0].abspath, 'w') 376 name = str(source[0].get_contents()) 377 obj = sim_objects[name] 378 379 print >>hh_file, obj.cxx_decl() 380 381def createSwigParam(target, source, env): 382 assert len(target) == 1 and len(source) == 1 383 384 i_file = file(target[0].abspath, 'w') 385 name = str(source[0].get_contents()) 386 param = all_params[name] 387 388 for line in param.swig_decl(): 389 print >>i_file, line 390 391def createEnumStrings(target, source, env): 392 assert len(target) == 1 and len(source) == 1 393 394 cc_file = file(target[0].abspath, 'w') 395 name = str(source[0].get_contents()) 396 obj = all_enums[name] 397 398 print >>cc_file, obj.cxx_def() 399 cc_file.close() 400 401def createEnumParam(target, source, env): 402 assert len(target) == 1 and len(source) == 1 403 404 hh_file = file(target[0].abspath, 'w') 405 name = str(source[0].get_contents()) 406 obj = all_enums[name] 407 408 print >>hh_file, obj.cxx_decl() 409 410# Generate all of the SimObject param struct header files 411params_hh_files = [] 412for name,simobj in sim_objects.iteritems(): 413 extra_deps = [ File(py_modules[simobj.__module__]) ] 414 415 hh_file = File('params/%s.hh' % name) 416 params_hh_files.append(hh_file) 417 env.Command(hh_file, Value(name), createSimObjectParam) 418 env.Depends(hh_file, depends + extra_deps) 419 420# Generate any parameter header files needed 421params_i_files = [] 422for name,param in all_params.iteritems(): 423 if isinstance(param, m5.params.VectorParamDesc): 424 ext = 'vptype' 425 else: 426 ext = 'ptype' 427 428 i_file = File('params/%s_%s.i' % (name, ext)) 429 params_i_files.append(i_file) 430 env.Command(i_file, Value(name), createSwigParam) 431 env.Depends(i_file, depends) 432 433# Generate all enum header files 434for name,enum in all_enums.iteritems(): 435 extra_deps = [ File(py_modules[enum.__module__]) ] 436 437 cc_file = File('enums/%s.cc' % name) 438 env.Command(cc_file, Value(name), createEnumStrings) 439 env.Depends(cc_file, depends + extra_deps) 440 Source(cc_file) 441 442 hh_file = File('enums/%s.hh' % name) 443 env.Command(hh_file, Value(name), createEnumParam) 444 env.Depends(hh_file, depends + extra_deps) 445 446# Build the big monolithic swigged params module (wraps all SimObject 447# param structs and enum structs) 448def buildParams(target, source, env): 449 names = [ s.get_contents() for s in source ] 450 objs = [ sim_objects[name] for name in names ] 451 out = file(target[0].abspath, 'w') 452 453 ordered_objs = [] 454 obj_seen = set() 455 def order_obj(obj): 456 name = str(obj) 457 if name in obj_seen: 458 return 459 460 obj_seen.add(name) 461 if str(obj) != 'SimObject': 462 order_obj(obj.__bases__[0]) 463 464 ordered_objs.append(obj) 465 466 for obj in objs: 467 order_obj(obj) 468 469 enums = set() 470 predecls = [] 471 pd_seen = set() 472 473 def add_pds(*pds): 474 for pd in pds: 475 if pd not in pd_seen: 476 predecls.append(pd) 477 pd_seen.add(pd) 478 479 for obj in ordered_objs: 480 params = obj._params.local.values() 481 for param in params: 482 ptype = param.ptype 483 if issubclass(ptype, m5.params.Enum): 484 if ptype not in enums: 485 enums.add(ptype) 486 pds = param.swig_predecls() 487 if isinstance(pds, (list, tuple)): 488 add_pds(*pds) 489 else: 490 add_pds(pds) 491 492 print >>out, '%module params' 493 494 print >>out, '%{' 495 for obj in ordered_objs: 496 print >>out, '#include "params/%s.hh"' % obj 497 print >>out, '%}' 498 499 for pd in predecls: 500 print >>out, pd 501 502 enums = list(enums) 503 enums.sort() 504 for enum in enums: 505 print >>out, '%%include "enums/%s.hh"' % enum.__name__ 506 print >>out 507 508 for obj in ordered_objs: 509 if obj.swig_objdecls: 510 for decl in obj.swig_objdecls: 511 print >>out, decl 512 continue 513 514 code = '' 515 base = obj.get_base() 516 517 code += '// stop swig from creating/wrapping default ctor/dtor\n' 518 code += '%%nodefault %s;\n' % obj.cxx_class 519 code += 'class %s ' % obj.cxx_class 520 if base: 521 code += ': public %s' % base 522 code += ' {};\n' 523 524 klass = obj.cxx_class; 525 if hasattr(obj, 'cxx_namespace'): 526 new_code = 'namespace %s {\n' % obj.cxx_namespace 527 new_code += code 528 new_code += '}\n' 529 code = new_code 530 klass = '%s::%s' % (obj.cxx_namespace, klass) 531 532 print >>out, code 533 534 print >>out, '%%include "src/sim/sim_object_params.hh"' % obj 535 for obj in ordered_objs: 536 print >>out, '%%include "params/%s.hh"' % obj 537 538params_file = File('params/params.i') 539names = sort_list(sim_objects.keys()) 540env.Command(params_file, [ Value(v) for v in names ], buildParams) 541env.Depends(params_file, params_hh_files + params_i_files + depends) 542SwigSource('m5.objects', params_file) 543 544# Build all swig modules 545swig_modules = [] 546for source,package in swig_sources: 547 filename = str(source) 548 assert filename.endswith('.i') 549 550 base = '.'.join(filename.split('.')[:-1]) 551 module = basename(base) 552 cc_file = base + '_wrap.cc' 553 py_file = base + '.py' 554 555 env.Command([cc_file, py_file], source, 556 '$SWIG $SWIGFLAGS -outdir ${TARGETS[1].dir} ' 557 '-o ${TARGETS[0]} $SOURCES') 558 env.Depends(py_file, source) 559 env.Depends(cc_file, source) 560 561 swig_modules.append(Value(module)) 562 Source(cc_file) 563 PySource(package, py_file) 564 565# Generate the main swig init file 566def makeSwigInit(target, source, env): 567 f = file(str(target[0]), 'w') 568 print >>f, 'extern "C" {' 569 for module in source: 570 print >>f, ' void init_%s();' % module.get_contents() 571 print >>f, '}'
|
554 print >>f, 'void init_swig() {'
| 572 print >>f, 'void initSwig() {'
|
555 for module in source: 556 print >>f, ' init_%s();' % module.get_contents() 557 print >>f, '}' 558 f.close() 559
| 573 for module in source: 574 print >>f, ' init_%s();' % module.get_contents() 575 print >>f, '}' 576 f.close() 577
|
560env.Command('swig/init.cc', swig_modules, makeSwigInit) 561Source('swig/init.cc')
| 578env.Command('python/swig/init.cc', swig_modules, makeSwigInit) 579Source('python/swig/init.cc')
|
562 563# Generate traceflags.py 564def traceFlagsPy(target, source, env): 565 assert(len(target) == 1) 566 567 f = file(str(target[0]), 'w') 568 569 allFlags = [] 570 for s in source: 571 val = eval(s.get_contents()) 572 allFlags.append(val) 573 574 print >>f, 'baseFlags = [' 575 for flag, compound, desc in allFlags: 576 if not compound: 577 print >>f, " '%s'," % flag 578 print >>f, " ]" 579 print >>f 580 581 print >>f, 'compoundFlags = [' 582 print >>f, " 'All'," 583 for flag, compound, desc in allFlags: 584 if compound: 585 print >>f, " '%s'," % flag 586 print >>f, " ]" 587 print >>f 588 589 print >>f, "allFlags = frozenset(baseFlags + compoundFlags)" 590 print >>f 591 592 print >>f, 'compoundFlagMap = {' 593 all = tuple([flag for flag,compound,desc in allFlags if not compound]) 594 print >>f, " 'All' : %s," % (all, ) 595 for flag, compound, desc in allFlags: 596 if compound: 597 print >>f, " '%s' : %s," % (flag, compound) 598 print >>f, " }" 599 print >>f 600 601 print >>f, 'flagDescriptions = {' 602 print >>f, " 'All' : 'All flags'," 603 for flag, compound, desc in allFlags: 604 print >>f, " '%s' : '%s'," % (flag, desc) 605 print >>f, " }" 606 607 f.close() 608 609def traceFlagsCC(target, source, env): 610 assert(len(target) == 1) 611 612 f = file(str(target[0]), 'w') 613 614 allFlags = [] 615 for s in source: 616 val = eval(s.get_contents()) 617 allFlags.append(val) 618 619 # file header 620 print >>f, ''' 621/* 622 * DO NOT EDIT THIS FILE! Automatically generated 623 */ 624 625#include "base/traceflags.hh" 626 627using namespace Trace; 628 629const char *Trace::flagStrings[] = 630{''' 631 632 # The string array is used by SimpleEnumParam to map the strings 633 # provided by the user to enum values. 634 for flag, compound, desc in allFlags: 635 if not compound: 636 print >>f, ' "%s",' % flag 637 638 print >>f, ' "All",' 639 for flag, compound, desc in allFlags: 640 if compound: 641 print >>f, ' "%s",' % flag 642 643 print >>f, '};' 644 print >>f 645 print >>f, 'const int Trace::numFlagStrings = %d;' % (len(allFlags) + 1) 646 print >>f 647 648 # 649 # Now define the individual compound flag arrays. There is an array 650 # for each compound flag listing the component base flags. 651 # 652 all = tuple([flag for flag,compound,desc in allFlags if not compound]) 653 print >>f, 'static const Flags AllMap[] = {' 654 for flag, compound, desc in allFlags: 655 if not compound: 656 print >>f, " %s," % flag 657 print >>f, '};' 658 print >>f 659 660 for flag, compound, desc in allFlags: 661 if not compound: 662 continue 663 print >>f, 'static const Flags %sMap[] = {' % flag 664 for flag in compound: 665 print >>f, " %s," % flag 666 print >>f, " (Flags)-1" 667 print >>f, '};' 668 print >>f 669 670 # 671 # Finally the compoundFlags[] array maps the compound flags 672 # to their individual arrays/ 673 # 674 print >>f, 'const Flags *Trace::compoundFlags[] =' 675 print >>f, '{' 676 print >>f, ' AllMap,' 677 for flag, compound, desc in allFlags: 678 if compound: 679 print >>f, ' %sMap,' % flag 680 # file trailer 681 print >>f, '};' 682 683 f.close() 684 685def traceFlagsHH(target, source, env): 686 assert(len(target) == 1) 687 688 f = file(str(target[0]), 'w') 689 690 allFlags = [] 691 for s in source: 692 val = eval(s.get_contents()) 693 allFlags.append(val) 694 695 # file header boilerplate 696 print >>f, ''' 697/* 698 * DO NOT EDIT THIS FILE! 699 * 700 * Automatically generated from traceflags.py 701 */ 702 703#ifndef __BASE_TRACE_FLAGS_HH__ 704#define __BASE_TRACE_FLAGS_HH__ 705 706namespace Trace { 707 708enum Flags {''' 709 710 # Generate the enum. Base flags come first, then compound flags. 711 idx = 0 712 for flag, compound, desc in allFlags: 713 if not compound: 714 print >>f, ' %s = %d,' % (flag, idx) 715 idx += 1 716 717 numBaseFlags = idx 718 print >>f, ' NumFlags = %d,' % idx 719 720 # put a comment in here to separate base from compound flags 721 print >>f, ''' 722// The remaining enum values are *not* valid indices for Trace::flags. 723// They are "compound" flags, which correspond to sets of base 724// flags, and are used by changeFlag.''' 725 726 print >>f, ' All = %d,' % idx 727 idx += 1 728 for flag, compound, desc in allFlags: 729 if compound: 730 print >>f, ' %s = %d,' % (flag, idx) 731 idx += 1 732 733 numCompoundFlags = idx - numBaseFlags 734 print >>f, ' NumCompoundFlags = %d' % numCompoundFlags 735 736 # trailer boilerplate 737 print >>f, '''\ 738}; // enum Flags 739 740// Array of strings for SimpleEnumParam 741extern const char *flagStrings[]; 742extern const int numFlagStrings; 743 744// Array of arraay pointers: for each compound flag, gives the list of 745// base flags to set. Inidividual flag arrays are terminated by -1. 746extern const Flags *compoundFlags[]; 747 748/* namespace Trace */ } 749 750#endif // __BASE_TRACE_FLAGS_HH__ 751''' 752 753 f.close() 754 755flags = [ Value(f) for f in trace_flags ] 756env.Command('base/traceflags.py', flags, traceFlagsPy) 757PySource('m5', 'base/traceflags.py') 758 759env.Command('base/traceflags.hh', flags, traceFlagsHH) 760env.Command('base/traceflags.cc', flags, traceFlagsCC) 761Source('base/traceflags.cc') 762 763# Generate program_info.cc 764def programInfo(target, source, env): 765 def gen_file(target, rev, node, date): 766 pi_stats = file(target, 'w') 767 print >>pi_stats, 'const char *hgRev = "%s:%s";' % (rev, node) 768 print >>pi_stats, 'const char *hgDate = "%s";' % date 769 pi_stats.close() 770 771 target = str(target[0]) 772 scons_dir = str(source[0].get_contents()) 773 try: 774 import mercurial.demandimport, mercurial.hg, mercurial.ui 775 import mercurial.util, mercurial.node 776 if not exists(scons_dir) or not isdir(scons_dir) or \ 777 not exists(joinpath(scons_dir, ".hg")): 778 raise ValueError 779 repo = mercurial.hg.repository(mercurial.ui.ui(), scons_dir) 780 rev = mercurial.node.nullrev + repo.changelog.count() 781 changenode = repo.changelog.node(rev) 782 changes = repo.changelog.read(changenode) 783 date = mercurial.util.datestr(changes[2]) 784 785 gen_file(target, rev, mercurial.node.hex(changenode), date) 786 787 mercurial.demandimport.disable() 788 except ImportError: 789 gen_file(target, "Unknown", "Unknown", "Unknown") 790 791 except: 792 print "in except" 793 gen_file(target, "Unknown", "Unknown", "Unknown") 794 mercurial.demandimport.disable() 795 796env.Command('base/program_info.cc', 797 Value(str(SCons.Node.FS.default_fs.SConstruct_dir)), 798 programInfo) 799
| 580 581# Generate traceflags.py 582def traceFlagsPy(target, source, env): 583 assert(len(target) == 1) 584 585 f = file(str(target[0]), 'w') 586 587 allFlags = [] 588 for s in source: 589 val = eval(s.get_contents()) 590 allFlags.append(val) 591 592 print >>f, 'baseFlags = [' 593 for flag, compound, desc in allFlags: 594 if not compound: 595 print >>f, " '%s'," % flag 596 print >>f, " ]" 597 print >>f 598 599 print >>f, 'compoundFlags = [' 600 print >>f, " 'All'," 601 for flag, compound, desc in allFlags: 602 if compound: 603 print >>f, " '%s'," % flag 604 print >>f, " ]" 605 print >>f 606 607 print >>f, "allFlags = frozenset(baseFlags + compoundFlags)" 608 print >>f 609 610 print >>f, 'compoundFlagMap = {' 611 all = tuple([flag for flag,compound,desc in allFlags if not compound]) 612 print >>f, " 'All' : %s," % (all, ) 613 for flag, compound, desc in allFlags: 614 if compound: 615 print >>f, " '%s' : %s," % (flag, compound) 616 print >>f, " }" 617 print >>f 618 619 print >>f, 'flagDescriptions = {' 620 print >>f, " 'All' : 'All flags'," 621 for flag, compound, desc in allFlags: 622 print >>f, " '%s' : '%s'," % (flag, desc) 623 print >>f, " }" 624 625 f.close() 626 627def traceFlagsCC(target, source, env): 628 assert(len(target) == 1) 629 630 f = file(str(target[0]), 'w') 631 632 allFlags = [] 633 for s in source: 634 val = eval(s.get_contents()) 635 allFlags.append(val) 636 637 # file header 638 print >>f, ''' 639/* 640 * DO NOT EDIT THIS FILE! Automatically generated 641 */ 642 643#include "base/traceflags.hh" 644 645using namespace Trace; 646 647const char *Trace::flagStrings[] = 648{''' 649 650 # The string array is used by SimpleEnumParam to map the strings 651 # provided by the user to enum values. 652 for flag, compound, desc in allFlags: 653 if not compound: 654 print >>f, ' "%s",' % flag 655 656 print >>f, ' "All",' 657 for flag, compound, desc in allFlags: 658 if compound: 659 print >>f, ' "%s",' % flag 660 661 print >>f, '};' 662 print >>f 663 print >>f, 'const int Trace::numFlagStrings = %d;' % (len(allFlags) + 1) 664 print >>f 665 666 # 667 # Now define the individual compound flag arrays. There is an array 668 # for each compound flag listing the component base flags. 669 # 670 all = tuple([flag for flag,compound,desc in allFlags if not compound]) 671 print >>f, 'static const Flags AllMap[] = {' 672 for flag, compound, desc in allFlags: 673 if not compound: 674 print >>f, " %s," % flag 675 print >>f, '};' 676 print >>f 677 678 for flag, compound, desc in allFlags: 679 if not compound: 680 continue 681 print >>f, 'static const Flags %sMap[] = {' % flag 682 for flag in compound: 683 print >>f, " %s," % flag 684 print >>f, " (Flags)-1" 685 print >>f, '};' 686 print >>f 687 688 # 689 # Finally the compoundFlags[] array maps the compound flags 690 # to their individual arrays/ 691 # 692 print >>f, 'const Flags *Trace::compoundFlags[] =' 693 print >>f, '{' 694 print >>f, ' AllMap,' 695 for flag, compound, desc in allFlags: 696 if compound: 697 print >>f, ' %sMap,' % flag 698 # file trailer 699 print >>f, '};' 700 701 f.close() 702 703def traceFlagsHH(target, source, env): 704 assert(len(target) == 1) 705 706 f = file(str(target[0]), 'w') 707 708 allFlags = [] 709 for s in source: 710 val = eval(s.get_contents()) 711 allFlags.append(val) 712 713 # file header boilerplate 714 print >>f, ''' 715/* 716 * DO NOT EDIT THIS FILE! 717 * 718 * Automatically generated from traceflags.py 719 */ 720 721#ifndef __BASE_TRACE_FLAGS_HH__ 722#define __BASE_TRACE_FLAGS_HH__ 723 724namespace Trace { 725 726enum Flags {''' 727 728 # Generate the enum. Base flags come first, then compound flags. 729 idx = 0 730 for flag, compound, desc in allFlags: 731 if not compound: 732 print >>f, ' %s = %d,' % (flag, idx) 733 idx += 1 734 735 numBaseFlags = idx 736 print >>f, ' NumFlags = %d,' % idx 737 738 # put a comment in here to separate base from compound flags 739 print >>f, ''' 740// The remaining enum values are *not* valid indices for Trace::flags. 741// They are "compound" flags, which correspond to sets of base 742// flags, and are used by changeFlag.''' 743 744 print >>f, ' All = %d,' % idx 745 idx += 1 746 for flag, compound, desc in allFlags: 747 if compound: 748 print >>f, ' %s = %d,' % (flag, idx) 749 idx += 1 750 751 numCompoundFlags = idx - numBaseFlags 752 print >>f, ' NumCompoundFlags = %d' % numCompoundFlags 753 754 # trailer boilerplate 755 print >>f, '''\ 756}; // enum Flags 757 758// Array of strings for SimpleEnumParam 759extern const char *flagStrings[]; 760extern const int numFlagStrings; 761 762// Array of arraay pointers: for each compound flag, gives the list of 763// base flags to set. Inidividual flag arrays are terminated by -1. 764extern const Flags *compoundFlags[]; 765 766/* namespace Trace */ } 767 768#endif // __BASE_TRACE_FLAGS_HH__ 769''' 770 771 f.close() 772 773flags = [ Value(f) for f in trace_flags ] 774env.Command('base/traceflags.py', flags, traceFlagsPy) 775PySource('m5', 'base/traceflags.py') 776 777env.Command('base/traceflags.hh', flags, traceFlagsHH) 778env.Command('base/traceflags.cc', flags, traceFlagsCC) 779Source('base/traceflags.cc') 780 781# Generate program_info.cc 782def programInfo(target, source, env): 783 def gen_file(target, rev, node, date): 784 pi_stats = file(target, 'w') 785 print >>pi_stats, 'const char *hgRev = "%s:%s";' % (rev, node) 786 print >>pi_stats, 'const char *hgDate = "%s";' % date 787 pi_stats.close() 788 789 target = str(target[0]) 790 scons_dir = str(source[0].get_contents()) 791 try: 792 import mercurial.demandimport, mercurial.hg, mercurial.ui 793 import mercurial.util, mercurial.node 794 if not exists(scons_dir) or not isdir(scons_dir) or \ 795 not exists(joinpath(scons_dir, ".hg")): 796 raise ValueError 797 repo = mercurial.hg.repository(mercurial.ui.ui(), scons_dir) 798 rev = mercurial.node.nullrev + repo.changelog.count() 799 changenode = repo.changelog.node(rev) 800 changes = repo.changelog.read(changenode) 801 date = mercurial.util.datestr(changes[2]) 802 803 gen_file(target, rev, mercurial.node.hex(changenode), date) 804 805 mercurial.demandimport.disable() 806 except ImportError: 807 gen_file(target, "Unknown", "Unknown", "Unknown") 808 809 except: 810 print "in except" 811 gen_file(target, "Unknown", "Unknown", "Unknown") 812 mercurial.demandimport.disable() 813 814env.Command('base/program_info.cc', 815 Value(str(SCons.Node.FS.default_fs.SConstruct_dir)), 816 programInfo) 817
|
800# Build the zip file 801def compilePyFile(target, source, env): 802 '''Action function to compile a .py into a .pyc''' 803 py_compile.compile(str(source[0]), str(target[0]))
| 818# embed python files. All .py files that have been indicated by a 819# PySource() call in a SConscript need to be embedded into the M5 820# library. To do that, we compile the file to byte code, marshal the 821# byte code, compress it, and then generate an assembly file that 822# inserts the result into the data section with symbols indicating the 823# beginning, and end (and with the size at the end) 824py_sources_tnodes = {} 825for pysource in py_sources: 826 py_sources_tnodes[pysource.tnode] = pysource
|
804
| 827
|
805def buildPyZip(target, source, env): 806 '''Action function to build the zip archive. Uses the 807 PyZipFile module included in the standard Python library.'''
| 828def objectifyPyFile(target, source, env): 829 '''Action function to compile a .py into a code object, marshal 830 it, compress it, and stick it into an asm file so the code appears 831 as just bytes with a label in the data section'''
|
808
| 832
|
809 py_compiled = {} 810 for s in py_sources: 811 compname = str(s.compiled) 812 assert compname not in py_compiled 813 py_compiled[compname] = s
| 833 src = file(str(source[0]), 'r').read() 834 dst = file(str(target[0]), 'w')
|
814
| 835
|
815 zf = zipfile.ZipFile(str(target[0]), 'w') 816 for s in source: 817 zipname = str(s) 818 arcname = py_compiled[zipname].arcname 819 zf.write(zipname, arcname) 820 zf.close()
| 836 pysource = py_sources_tnodes[source[0]] 837 compiled = compile(src, pysource.snode.path, 'exec') 838 marshalled = marshal.dumps(compiled) 839 compressed = zlib.compress(marshalled) 840 data = compressed
|
821
| 841
|
822py_compiled = [] 823py_zip_depends = []
| 842 # Some C/C++ compilers prepend an underscore to global symbol 843 # names, so if they're going to do that, we need to prepend that 844 # leading underscore to globals in the assembly file. 845 if env['LEADING_UNDERSCORE']: 846 sym = '_' + pysource.symname 847 else: 848 sym = pysource.symname 849 850 step = 16 851 print >>dst, ".data" 852 print >>dst, ".globl %s_beg" % sym 853 print >>dst, ".globl %s_end" % sym 854 print >>dst, "%s_beg:" % sym 855 for i in xrange(0, len(data), step): 856 x = array.array('B', data[i:i+step]) 857 print >>dst, ".byte", ','.join([str(d) for d in x]) 858 print >>dst, "%s_end:" % sym 859 print >>dst, ".long %d" % len(marshalled) 860
|
824for source in py_sources:
| 861for source in py_sources:
|
825 env.Command(source.compiled, source.source, compilePyFile) 826 py_compiled.append(source.compiled)
| 862 env.Command(source.assembly, source.tnode, objectifyPyFile) 863 Source(source.assembly)
|
827
| 864
|
828 # make the zipfile depend on the archive name so that the archive 829 # is rebuilt if the name changes 830 py_zip_depends.append(Value(source.arcname))
| 865# Generate init_python.cc which creates a bunch of EmbeddedPyModule 866# structs that describe the embedded python code. One such struct 867# contains information about the importer that python uses to get at 868# the embedded files, and then there's a list of all of the rest that 869# the importer uses to load the rest on demand. 870py_sources_symbols = {} 871for pysource in py_sources: 872 py_sources_symbols[pysource.symname] = pysource 873def pythonInit(target, source, env): 874 dst = file(str(target[0]), 'w')
|
831
| 875
|
832# Add the zip file target to the environment. 833m5zip = File('m5py.zip') 834env.Command(m5zip, py_compiled, buildPyZip) 835env.Depends(m5zip, py_zip_depends)
| 876 def dump_mod(sym, endchar=','): 877 pysource = py_sources_symbols[sym] 878 print >>dst, ' { "%s",' % pysource.arcname 879 print >>dst, ' "%s",' % pysource.modpath 880 print >>dst, ' %s_beg, %s_end,' % (sym, sym) 881 print >>dst, ' %s_end - %s_beg,' % (sym, sym) 882 print >>dst, ' *(int *)%s_end }%s' % (sym, endchar) 883 884 print >>dst, '#include "sim/init.hh"'
|
836
| 885
|
| 886 for sym in source: 887 sym = sym.get_contents() 888 print >>dst, "extern const char %s_beg[], %s_end[];" % (sym, sym) 889 890 print >>dst, "const EmbeddedPyModule embeddedPyImporter = " 891 dump_mod("PyEMB_importer", endchar=';'); 892 print >>dst 893 894 print >>dst, "const EmbeddedPyModule embeddedPyModules[] = {" 895 for i,sym in enumerate(source): 896 sym = sym.get_contents() 897 if sym == "PyEMB_importer": 898 # Skip the importer since we've already exported it 899 continue 900 dump_mod(sym) 901 print >>dst, " { 0, 0, 0, 0, 0, 0 }" 902 print >>dst, "};" 903 904symbols = [Value(s.symname) for s in py_sources] 905env.Command('sim/init_python.cc', symbols, pythonInit) 906Source('sim/init_python.cc') 907
|
837######################################################################## 838# 839# Define binaries. Each different build type (debug, opt, etc.) gets 840# a slightly different build environment. 841# 842 843# List of constructed environments to pass back to SConstruct 844envList = [] 845 846# This function adds the specified sources to the given build 847# environment, and returns a list of all the corresponding SCons 848# Object nodes (including an extra one for date.cc). We explicitly 849# add the Object nodes so we can set up special dependencies for 850# date.cc. 851def make_objs(sources, env): 852 objs = [env.Object(s) for s in sources] 853 854 # make date.cc depend on all other objects so it always gets 855 # recompiled whenever anything else does 856 date_obj = env.Object('base/date.cc') 857 858 # Make the generation of program_info.cc dependend on all 859 # the other cc files and the compiling of program_info.cc 860 # dependent on all the objects but program_info.o 861 pinfo_obj = env.Object('base/program_info.cc') 862 env.Depends('base/program_info.cc', sources) 863 env.Depends(date_obj, objs) 864 env.Depends(pinfo_obj, objs) 865 objs.extend([date_obj,pinfo_obj]) 866 return objs 867 868# Function to create a new build environment as clone of current 869# environment 'env' with modified object suffix and optional stripped 870# binary. Additional keyword arguments are appended to corresponding 871# build environment vars. 872def makeEnv(label, objsfx, strip = False, **kwargs): 873 newEnv = env.Copy(OBJSUFFIX=objsfx) 874 newEnv.Label = label 875 newEnv.Append(**kwargs)
| 908######################################################################## 909# 910# Define binaries. Each different build type (debug, opt, etc.) gets 911# a slightly different build environment. 912# 913 914# List of constructed environments to pass back to SConstruct 915envList = [] 916 917# This function adds the specified sources to the given build 918# environment, and returns a list of all the corresponding SCons 919# Object nodes (including an extra one for date.cc). We explicitly 920# add the Object nodes so we can set up special dependencies for 921# date.cc. 922def make_objs(sources, env): 923 objs = [env.Object(s) for s in sources] 924 925 # make date.cc depend on all other objects so it always gets 926 # recompiled whenever anything else does 927 date_obj = env.Object('base/date.cc') 928 929 # Make the generation of program_info.cc dependend on all 930 # the other cc files and the compiling of program_info.cc 931 # dependent on all the objects but program_info.o 932 pinfo_obj = env.Object('base/program_info.cc') 933 env.Depends('base/program_info.cc', sources) 934 env.Depends(date_obj, objs) 935 env.Depends(pinfo_obj, objs) 936 objs.extend([date_obj,pinfo_obj]) 937 return objs 938 939# Function to create a new build environment as clone of current 940# environment 'env' with modified object suffix and optional stripped 941# binary. Additional keyword arguments are appended to corresponding 942# build environment vars. 943def makeEnv(label, objsfx, strip = False, **kwargs): 944 newEnv = env.Copy(OBJSUFFIX=objsfx) 945 newEnv.Label = label 946 newEnv.Append(**kwargs)
|
| 947 948 # First make a library of everything but main() so other programs can 949 # link against m5. 950 # 951 # SCons doesn't know to append a library suffix when there is a '.' in the 952 # name. Use '_' instead. 953 m5lib = newEnv.Library('m5_' + label, make_objs(cc_lib_sources, newEnv)) 954 955 # Now link a stub with main() and the library.
|
876 exe = 'm5.' + label # final executable
| 956 exe = 'm5.' + label # final executable
|
877 bin = exe + '.bin' # executable w/o appended Python zip archive 878 newEnv.Program(bin, make_objs(cc_sources, newEnv))
| 957 objects = [newEnv.Object(s) for s in cc_bin_sources] + m5lib
|
879 if strip:
| 958 if strip:
|
880 stripped_bin = bin + '.stripped'
| 959 unstripped_exe = exe + '.unstripped' 960 newEnv.Program(unstripped_exe, objects)
|
881 if sys.platform == 'sunos5': 882 cmd = 'cp $SOURCE $TARGET; strip $TARGET' 883 else: 884 cmd = 'strip $SOURCE -o $TARGET'
| 961 if sys.platform == 'sunos5': 962 cmd = 'cp $SOURCE $TARGET; strip $TARGET' 963 else: 964 cmd = 'strip $SOURCE -o $TARGET'
|
885 newEnv.Command(stripped_bin, bin, cmd) 886 bin = stripped_bin 887 targets = newEnv.Concat(exe, [bin, 'm5py.zip'])
| 965 targets = newEnv.Command(exe, unstripped_exe, cmd) 966 else: 967 targets = newEnv.Program(exe, objects) 968
|
888 newEnv.M5Binary = targets[0] 889 envList.append(newEnv) 890 891# Debug binary 892ccflags = {} 893if env['GCC']: 894 if sys.platform == 'sunos5': 895 ccflags['debug'] = '-gstabs+' 896 else: 897 ccflags['debug'] = '-ggdb3' 898 ccflags['opt'] = '-g -O3' 899 ccflags['fast'] = '-O3' 900 ccflags['prof'] = '-O3 -g -pg' 901elif env['SUNCC']: 902 ccflags['debug'] = '-g0' 903 ccflags['opt'] = '-g -O' 904 ccflags['fast'] = '-fast' 905 ccflags['prof'] = '-fast -g -pg' 906elif env['ICC']: 907 ccflags['debug'] = '-g -O0' 908 ccflags['opt'] = '-g -O' 909 ccflags['fast'] = '-fast' 910 ccflags['prof'] = '-fast -g -pg' 911else: 912 print 'Unknown compiler, please fix compiler options' 913 Exit(1) 914 915makeEnv('debug', '.do', 916 CCFLAGS = Split(ccflags['debug']), 917 CPPDEFINES = ['DEBUG', 'TRACING_ON=1']) 918 919# Optimized binary 920makeEnv('opt', '.o', 921 CCFLAGS = Split(ccflags['opt']), 922 CPPDEFINES = ['TRACING_ON=1']) 923 924# "Fast" binary 925makeEnv('fast', '.fo', strip = True, 926 CCFLAGS = Split(ccflags['fast']), 927 CPPDEFINES = ['NDEBUG', 'TRACING_ON=0']) 928 929# Profiled binary 930makeEnv('prof', '.po', 931 CCFLAGS = Split(ccflags['prof']), 932 CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'], 933 LINKFLAGS = '-pg') 934 935Return('envList')
| 969 newEnv.M5Binary = targets[0] 970 envList.append(newEnv) 971 972# Debug binary 973ccflags = {} 974if env['GCC']: 975 if sys.platform == 'sunos5': 976 ccflags['debug'] = '-gstabs+' 977 else: 978 ccflags['debug'] = '-ggdb3' 979 ccflags['opt'] = '-g -O3' 980 ccflags['fast'] = '-O3' 981 ccflags['prof'] = '-O3 -g -pg' 982elif env['SUNCC']: 983 ccflags['debug'] = '-g0' 984 ccflags['opt'] = '-g -O' 985 ccflags['fast'] = '-fast' 986 ccflags['prof'] = '-fast -g -pg' 987elif env['ICC']: 988 ccflags['debug'] = '-g -O0' 989 ccflags['opt'] = '-g -O' 990 ccflags['fast'] = '-fast' 991 ccflags['prof'] = '-fast -g -pg' 992else: 993 print 'Unknown compiler, please fix compiler options' 994 Exit(1) 995 996makeEnv('debug', '.do', 997 CCFLAGS = Split(ccflags['debug']), 998 CPPDEFINES = ['DEBUG', 'TRACING_ON=1']) 999 1000# Optimized binary 1001makeEnv('opt', '.o', 1002 CCFLAGS = Split(ccflags['opt']), 1003 CPPDEFINES = ['TRACING_ON=1']) 1004 1005# "Fast" binary 1006makeEnv('fast', '.fo', strip = True, 1007 CCFLAGS = Split(ccflags['fast']), 1008 CPPDEFINES = ['NDEBUG', 'TRACING_ON=0']) 1009 1010# Profiled binary 1011makeEnv('prof', '.po', 1012 CCFLAGS = Split(ccflags['prof']), 1013 CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'], 1014 LINKFLAGS = '-pg') 1015 1016Return('envList')
|