SConscript revision 10455
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# 292665Ssaidi@eecs.umich.edu# Authors: Nathan Binkert 30955SN/A 31955SN/Aimport array 32955SN/Aimport bisect 334202Sbinkertn@umich.eduimport imp 344202Sbinkertn@umich.eduimport marshal 35955SN/Aimport os 36955SN/Aimport re 37955SN/Aimport sys 38955SN/Aimport zlib 394202Sbinkertn@umich.edu 40955SN/Afrom os.path import basename, dirname, exists, isdir, isfile, join as joinpath 414202Sbinkertn@umich.edu 424202Sbinkertn@umich.eduimport SCons 434202Sbinkertn@umich.edu 444202Sbinkertn@umich.edu# This file defines how to build a particular configuration of gem5 454202Sbinkertn@umich.edu# based on variable settings in the 'env' build environment. 464202Sbinkertn@umich.edu 474202Sbinkertn@umich.eduImport('*') 484202Sbinkertn@umich.edu 494202Sbinkertn@umich.edu# Children need to see the environment 504202Sbinkertn@umich.eduExport('env') 51955SN/A 524202Sbinkertn@umich.edubuild_env = [(opt, env[opt]) for opt in export_vars] 534202Sbinkertn@umich.edu 54955SN/Afrom m5.util import code_formatter, compareVersions 552667Sstever@eecs.umich.edu 562667Sstever@eecs.umich.edu######################################################################## 572667Sstever@eecs.umich.edu# Code for adding source files of various types 582667Sstever@eecs.umich.edu# 592667Sstever@eecs.umich.edu# When specifying a source file of some type, a set of guards can be 602667Sstever@eecs.umich.edu# specified for that file. When get() is used to find the files, if 612037SN/A# get specifies a set of filters, only files that match those filters 622037SN/A# will be accepted (unspecified filters on files are assumed to be 632037SN/A# false). Current filters are: 644202Sbinkertn@umich.edu# main -- specifies the gem5 main() function 654202Sbinkertn@umich.edu# skip_lib -- do not put this file into the gem5 library 664202Sbinkertn@umich.edu# skip_no_python -- do not put this file into a no_python library 674202Sbinkertn@umich.edu# as it embeds compiled Python 684202Sbinkertn@umich.edu# <unittest> -- unit tests use filters based on the unit test name 694202Sbinkertn@umich.edu# 704202Sbinkertn@umich.edu# A parent can now be specified for a source file and default filter 714202Sbinkertn@umich.edu# values will be retrieved recursively from parents (children override 724202Sbinkertn@umich.edu# parents). 734202Sbinkertn@umich.edu# 744202Sbinkertn@umich.educlass SourceMeta(type): 754202Sbinkertn@umich.edu '''Meta class for source files that keeps track of all files of a 764202Sbinkertn@umich.edu particular type and has a get function for finding all functions 771858SN/A of a certain type that match a set of guards''' 781858SN/A def __init__(cls, name, bases, dict): 791858SN/A super(SourceMeta, cls).__init__(name, bases, dict) 801085SN/A cls.all = [] 81955SN/A 82955SN/A def get(cls, **guards): 83955SN/A '''Find all files that match the specified guards. If a source 84955SN/A file does not specify a flag, the default is False''' 851108SN/A for src in cls.all: 86955SN/A for flag,value in guards.iteritems(): 87955SN/A # if the flag is found and has a different value, skip 88955SN/A # this file 89955SN/A if src.all_guards.get(flag, False) != value: 90955SN/A break 91955SN/A else: 92955SN/A yield src 93955SN/A 94955SN/Aclass SourceFile(object): 95955SN/A '''Base object that encapsulates the notion of a source file. 96955SN/A This includes, the source node, target node, various manipulations 97955SN/A of those. A source file also specifies a set of guards which 98955SN/A describing which builds the source file applies to. A parent can 99955SN/A also be specified to get default guards from''' 100955SN/A __metaclass__ = SourceMeta 101955SN/A def __init__(self, source, parent=None, **guards): 1022655Sstever@eecs.umich.edu self.guards = guards 1032655Sstever@eecs.umich.edu self.parent = parent 1042655Sstever@eecs.umich.edu 1052655Sstever@eecs.umich.edu tnode = source 1062655Sstever@eecs.umich.edu if not isinstance(source, SCons.Node.FS.File): 1072655Sstever@eecs.umich.edu tnode = File(source) 1082655Sstever@eecs.umich.edu 1092655Sstever@eecs.umich.edu self.tnode = tnode 1102655Sstever@eecs.umich.edu self.snode = tnode.srcnode() 1112655Sstever@eecs.umich.edu 1122655Sstever@eecs.umich.edu for base in type(self).__mro__: 1132655Sstever@eecs.umich.edu if issubclass(base, SourceFile): 1142655Sstever@eecs.umich.edu base.all.append(self) 1152655Sstever@eecs.umich.edu 1162655Sstever@eecs.umich.edu @property 1172655Sstever@eecs.umich.edu def filename(self): 1184007Ssaidi@eecs.umich.edu return str(self.tnode) 1194007Ssaidi@eecs.umich.edu 1204007Ssaidi@eecs.umich.edu @property 1214007Ssaidi@eecs.umich.edu def dirname(self): 1222655Sstever@eecs.umich.edu return dirname(self.filename) 1232655Sstever@eecs.umich.edu 1242655Sstever@eecs.umich.edu @property 1252655Sstever@eecs.umich.edu def basename(self): 1262655Sstever@eecs.umich.edu return basename(self.filename) 127955SN/A 1283918Ssaidi@eecs.umich.edu @property 1293918Ssaidi@eecs.umich.edu def extname(self): 1303918Ssaidi@eecs.umich.edu index = self.basename.rfind('.') 1313918Ssaidi@eecs.umich.edu if index <= 0: 1323918Ssaidi@eecs.umich.edu # dot files aren't extensions 1333918Ssaidi@eecs.umich.edu return self.basename, None 1343918Ssaidi@eecs.umich.edu 1353918Ssaidi@eecs.umich.edu return self.basename[:index], self.basename[index+1:] 1363918Ssaidi@eecs.umich.edu 1373918Ssaidi@eecs.umich.edu @property 1383918Ssaidi@eecs.umich.edu def all_guards(self): 1393918Ssaidi@eecs.umich.edu '''find all guards for this object getting default values 1403918Ssaidi@eecs.umich.edu recursively from its parents''' 1413918Ssaidi@eecs.umich.edu guards = {} 1423940Ssaidi@eecs.umich.edu if self.parent: 1433940Ssaidi@eecs.umich.edu guards.update(self.parent.guards) 1443940Ssaidi@eecs.umich.edu guards.update(self.guards) 1453942Ssaidi@eecs.umich.edu return guards 1463940Ssaidi@eecs.umich.edu 1473515Ssaidi@eecs.umich.edu def __lt__(self, other): return self.filename < other.filename 1483918Ssaidi@eecs.umich.edu def __le__(self, other): return self.filename <= other.filename 1493918Ssaidi@eecs.umich.edu def __gt__(self, other): return self.filename > other.filename 1503515Ssaidi@eecs.umich.edu def __ge__(self, other): return self.filename >= other.filename 1512655Sstever@eecs.umich.edu def __eq__(self, other): return self.filename == other.filename 1523918Ssaidi@eecs.umich.edu def __ne__(self, other): return self.filename != other.filename 1533619Sbinkertn@umich.edu 154955SN/A @staticmethod 155955SN/A def done(): 1562655Sstever@eecs.umich.edu def disabled(cls, name, *ignored): 1573918Ssaidi@eecs.umich.edu raise RuntimeError("Additional SourceFile '%s'" % name,\ 1583619Sbinkertn@umich.edu "declared, but targets deps are already fixed.") 159955SN/A SourceFile.__init__ = disabled 160955SN/A 1612655Sstever@eecs.umich.edu 1623918Ssaidi@eecs.umich.educlass Source(SourceFile): 1633619Sbinkertn@umich.edu '''Add a c/c++ source file to the build''' 164955SN/A def __init__(self, source, Werror=True, swig=False, **guards): 165955SN/A '''specify the source file, and any guards''' 1662655Sstever@eecs.umich.edu super(Source, self).__init__(source, **guards) 1673918Ssaidi@eecs.umich.edu 1683683Sstever@eecs.umich.edu self.Werror = Werror 1692655Sstever@eecs.umich.edu self.swig = swig 1701869SN/A 1711869SN/Aclass PySource(SourceFile): 172 '''Add a python source file to the named package''' 173 invalid_sym_char = re.compile('[^A-z0-9_]') 174 modules = {} 175 tnodes = {} 176 symnames = {} 177 178 def __init__(self, package, source, **guards): 179 '''specify the python package, the source file, and any guards''' 180 super(PySource, self).__init__(source, **guards) 181 182 modname,ext = self.extname 183 assert ext == 'py' 184 185 if package: 186 path = package.split('.') 187 else: 188 path = [] 189 190 modpath = path[:] 191 if modname != '__init__': 192 modpath += [ modname ] 193 modpath = '.'.join(modpath) 194 195 arcpath = path + [ self.basename ] 196 abspath = self.snode.abspath 197 if not exists(abspath): 198 abspath = self.tnode.abspath 199 200 self.package = package 201 self.modname = modname 202 self.modpath = modpath 203 self.arcname = joinpath(*arcpath) 204 self.abspath = abspath 205 self.compiled = File(self.filename + 'c') 206 self.cpp = File(self.filename + '.cc') 207 self.symname = PySource.invalid_sym_char.sub('_', modpath) 208 209 PySource.modules[modpath] = self 210 PySource.tnodes[self.tnode] = self 211 PySource.symnames[self.symname] = self 212 213class SimObject(PySource): 214 '''Add a SimObject python file as a python source object and add 215 it to a list of sim object modules''' 216 217 fixed = False 218 modnames = [] 219 220 def __init__(self, source, **guards): 221 '''Specify the source file and any guards (automatically in 222 the m5.objects package)''' 223 super(SimObject, self).__init__('m5.objects', source, **guards) 224 if self.fixed: 225 raise AttributeError, "Too late to call SimObject now." 226 227 bisect.insort_right(SimObject.modnames, self.modname) 228 229class SwigSource(SourceFile): 230 '''Add a swig file to build''' 231 232 def __init__(self, package, source, **guards): 233 '''Specify the python package, the source file, and any guards''' 234 super(SwigSource, self).__init__(source, skip_no_python=True, **guards) 235 236 modname,ext = self.extname 237 assert ext == 'i' 238 239 self.module = modname 240 cc_file = joinpath(self.dirname, modname + '_wrap.cc') 241 py_file = joinpath(self.dirname, modname + '.py') 242 243 self.cc_source = Source(cc_file, swig=True, parent=self, **guards) 244 self.py_source = PySource(package, py_file, parent=self, **guards) 245 246class ProtoBuf(SourceFile): 247 '''Add a Protocol Buffer to build''' 248 249 def __init__(self, source, **guards): 250 '''Specify the source file, and any guards''' 251 super(ProtoBuf, self).__init__(source, **guards) 252 253 # Get the file name and the extension 254 modname,ext = self.extname 255 assert ext == 'proto' 256 257 # Currently, we stick to generating the C++ headers, so we 258 # only need to track the source and header. 259 self.cc_file = File(modname + '.pb.cc') 260 self.hh_file = File(modname + '.pb.h') 261 262class UnitTest(object): 263 '''Create a UnitTest''' 264 265 all = [] 266 def __init__(self, target, *sources, **kwargs): 267 '''Specify the target name and any sources. Sources that are 268 not SourceFiles are evalued with Source(). All files are 269 guarded with a guard of the same name as the UnitTest 270 target.''' 271 272 srcs = [] 273 for src in sources: 274 if not isinstance(src, SourceFile): 275 src = Source(src, skip_lib=True) 276 src.guards[target] = True 277 srcs.append(src) 278 279 self.sources = srcs 280 self.target = target 281 self.main = kwargs.get('main', False) 282 UnitTest.all.append(self) 283 284# Children should have access 285Export('Source') 286Export('PySource') 287Export('SimObject') 288Export('SwigSource') 289Export('ProtoBuf') 290Export('UnitTest') 291 292######################################################################## 293# 294# Debug Flags 295# 296debug_flags = {} 297def DebugFlag(name, desc=None): 298 if name in debug_flags: 299 raise AttributeError, "Flag %s already specified" % name 300 debug_flags[name] = (name, (), desc) 301 302def CompoundFlag(name, flags, desc=None): 303 if name in debug_flags: 304 raise AttributeError, "Flag %s already specified" % name 305 306 compound = tuple(flags) 307 debug_flags[name] = (name, compound, desc) 308 309Export('DebugFlag') 310Export('CompoundFlag') 311 312######################################################################## 313# 314# Set some compiler variables 315# 316 317# Include file paths are rooted in this directory. SCons will 318# automatically expand '.' to refer to both the source directory and 319# the corresponding build directory to pick up generated include 320# files. 321env.Append(CPPPATH=Dir('.')) 322 323for extra_dir in extras_dir_list: 324 env.Append(CPPPATH=Dir(extra_dir)) 325 326# Workaround for bug in SCons version > 0.97d20071212 327# Scons bug id: 2006 gem5 Bug id: 308 328for root, dirs, files in os.walk(base_dir, topdown=True): 329 Dir(root[len(base_dir) + 1:]) 330 331######################################################################## 332# 333# Walk the tree and execute all SConscripts in subdirectories 334# 335 336here = Dir('.').srcnode().abspath 337for root, dirs, files in os.walk(base_dir, topdown=True): 338 if root == here: 339 # we don't want to recurse back into this SConscript 340 continue 341 342 if 'SConscript' in files: 343 build_dir = joinpath(env['BUILDDIR'], root[len(base_dir) + 1:]) 344 SConscript(joinpath(root, 'SConscript'), variant_dir=build_dir) 345 346for extra_dir in extras_dir_list: 347 prefix_len = len(dirname(extra_dir)) + 1 348 349 # Also add the corresponding build directory to pick up generated 350 # include files. 351 env.Append(CPPPATH=Dir(joinpath(env['BUILDDIR'], extra_dir[prefix_len:]))) 352 353 for root, dirs, files in os.walk(extra_dir, topdown=True): 354 # if build lives in the extras directory, don't walk down it 355 if 'build' in dirs: 356 dirs.remove('build') 357 358 if 'SConscript' in files: 359 build_dir = joinpath(env['BUILDDIR'], root[prefix_len:]) 360 SConscript(joinpath(root, 'SConscript'), variant_dir=build_dir) 361 362for opt in export_vars: 363 env.ConfigFile(opt) 364 365def makeTheISA(source, target, env): 366 isas = [ src.get_contents() for src in source ] 367 target_isa = env['TARGET_ISA'] 368 def define(isa): 369 return isa.upper() + '_ISA' 370 371 def namespace(isa): 372 return isa[0].upper() + isa[1:].lower() + 'ISA' 373 374 375 code = code_formatter() 376 code('''\ 377#ifndef __CONFIG_THE_ISA_HH__ 378#define __CONFIG_THE_ISA_HH__ 379 380''') 381 382 for i,isa in enumerate(isas): 383 code('#define $0 $1', define(isa), i + 1) 384 385 code(''' 386 387#define THE_ISA ${{define(target_isa)}} 388#define TheISA ${{namespace(target_isa)}} 389#define THE_ISA_STR "${{target_isa}}" 390 391#endif // __CONFIG_THE_ISA_HH__''') 392 393 code.write(str(target[0])) 394 395env.Command('config/the_isa.hh', map(Value, all_isa_list), 396 MakeAction(makeTheISA, Transform("CFG ISA", 0))) 397 398######################################################################## 399# 400# Prevent any SimObjects from being added after this point, they 401# should all have been added in the SConscripts above 402# 403SimObject.fixed = True 404 405class DictImporter(object): 406 '''This importer takes a dictionary of arbitrary module names that 407 map to arbitrary filenames.''' 408 def __init__(self, modules): 409 self.modules = modules 410 self.installed = set() 411 412 def __del__(self): 413 self.unload() 414 415 def unload(self): 416 import sys 417 for module in self.installed: 418 del sys.modules[module] 419 self.installed = set() 420 421 def find_module(self, fullname, path): 422 if fullname == 'm5.defines': 423 return self 424 425 if fullname == 'm5.objects': 426 return self 427 428 if fullname.startswith('m5.internal'): 429 return None 430 431 source = self.modules.get(fullname, None) 432 if source is not None and fullname.startswith('m5.objects'): 433 return self 434 435 return None 436 437 def load_module(self, fullname): 438 mod = imp.new_module(fullname) 439 sys.modules[fullname] = mod 440 self.installed.add(fullname) 441 442 mod.__loader__ = self 443 if fullname == 'm5.objects': 444 mod.__path__ = fullname.split('.') 445 return mod 446 447 if fullname == 'm5.defines': 448 mod.__dict__['buildEnv'] = m5.util.SmartDict(build_env) 449 return mod 450 451 source = self.modules[fullname] 452 if source.modname == '__init__': 453 mod.__path__ = source.modpath 454 mod.__file__ = source.abspath 455 456 exec file(source.abspath, 'r') in mod.__dict__ 457 458 return mod 459 460import m5.SimObject 461import m5.params 462from m5.util import code_formatter 463 464m5.SimObject.clear() 465m5.params.clear() 466 467# install the python importer so we can grab stuff from the source 468# tree itself. We can't have SimObjects added after this point or 469# else we won't know about them for the rest of the stuff. 470importer = DictImporter(PySource.modules) 471sys.meta_path[0:0] = [ importer ] 472 473# import all sim objects so we can populate the all_objects list 474# make sure that we're working with a list, then let's sort it 475for modname in SimObject.modnames: 476 exec('from m5.objects import %s' % modname) 477 478# we need to unload all of the currently imported modules so that they 479# will be re-imported the next time the sconscript is run 480importer.unload() 481sys.meta_path.remove(importer) 482 483sim_objects = m5.SimObject.allClasses 484all_enums = m5.params.allEnums 485 486if m5.SimObject.noCxxHeader: 487 print >> sys.stderr, \ 488 "warning: At least one SimObject lacks a header specification. " \ 489 "This can cause unexpected results in the generated SWIG " \ 490 "wrappers." 491 492# Find param types that need to be explicitly wrapped with swig. 493# These will be recognized because the ParamDesc will have a 494# swig_decl() method. Most param types are based on types that don't 495# need this, either because they're based on native types (like Int) 496# or because they're SimObjects (which get swigged independently). 497# For now the only things handled here are VectorParam types. 498params_to_swig = {} 499for name,obj in sorted(sim_objects.iteritems()): 500 for param in obj._params.local.values(): 501 # load the ptype attribute now because it depends on the 502 # current version of SimObject.allClasses, but when scons 503 # actually uses the value, all versions of 504 # SimObject.allClasses will have been loaded 505 param.ptype 506 507 if not hasattr(param, 'swig_decl'): 508 continue 509 pname = param.ptype_str 510 if pname not in params_to_swig: 511 params_to_swig[pname] = param 512 513######################################################################## 514# 515# calculate extra dependencies 516# 517module_depends = ["m5", "m5.SimObject", "m5.params"] 518depends = [ PySource.modules[dep].snode for dep in module_depends ] 519 520######################################################################## 521# 522# Commands for the basic automatically generated python files 523# 524 525# Generate Python file containing a dict specifying the current 526# buildEnv flags. 527def makeDefinesPyFile(target, source, env): 528 build_env = source[0].get_contents() 529 530 code = code_formatter() 531 code(""" 532import m5.internal 533import m5.util 534 535buildEnv = m5.util.SmartDict($build_env) 536 537compileDate = m5.internal.core.compileDate 538_globals = globals() 539for key,val in m5.internal.core.__dict__.iteritems(): 540 if key.startswith('flag_'): 541 flag = key[5:] 542 _globals[flag] = val 543del _globals 544""") 545 code.write(target[0].abspath) 546 547defines_info = Value(build_env) 548# Generate a file with all of the compile options in it 549env.Command('python/m5/defines.py', defines_info, 550 MakeAction(makeDefinesPyFile, Transform("DEFINES", 0))) 551PySource('m5', 'python/m5/defines.py') 552 553# Generate python file containing info about the M5 source code 554def makeInfoPyFile(target, source, env): 555 code = code_formatter() 556 for src in source: 557 data = ''.join(file(src.srcnode().abspath, 'r').xreadlines()) 558 code('$src = ${{repr(data)}}') 559 code.write(str(target[0])) 560 561# Generate a file that wraps the basic top level files 562env.Command('python/m5/info.py', 563 [ '#/COPYING', '#/LICENSE', '#/README', ], 564 MakeAction(makeInfoPyFile, Transform("INFO"))) 565PySource('m5', 'python/m5/info.py') 566 567######################################################################## 568# 569# Create all of the SimObject param headers and enum headers 570# 571 572def createSimObjectParamStruct(target, source, env): 573 assert len(target) == 1 and len(source) == 1 574 575 name = str(source[0].get_contents()) 576 obj = sim_objects[name] 577 578 code = code_formatter() 579 obj.cxx_param_decl(code) 580 code.write(target[0].abspath) 581 582def createParamSwigWrapper(target, source, env): 583 assert len(target) == 1 and len(source) == 1 584 585 name = str(source[0].get_contents()) 586 param = params_to_swig[name] 587 588 code = code_formatter() 589 param.swig_decl(code) 590 code.write(target[0].abspath) 591 592def createEnumStrings(target, source, env): 593 assert len(target) == 1 and len(source) == 1 594 595 name = str(source[0].get_contents()) 596 obj = all_enums[name] 597 598 code = code_formatter() 599 obj.cxx_def(code) 600 code.write(target[0].abspath) 601 602def createEnumDecls(target, source, env): 603 assert len(target) == 1 and len(source) == 1 604 605 name = str(source[0].get_contents()) 606 obj = all_enums[name] 607 608 code = code_formatter() 609 obj.cxx_decl(code) 610 code.write(target[0].abspath) 611 612def createEnumSwigWrapper(target, source, env): 613 assert len(target) == 1 and len(source) == 1 614 615 name = str(source[0].get_contents()) 616 obj = all_enums[name] 617 618 code = code_formatter() 619 obj.swig_decl(code) 620 code.write(target[0].abspath) 621 622def createSimObjectSwigWrapper(target, source, env): 623 name = source[0].get_contents() 624 obj = sim_objects[name] 625 626 code = code_formatter() 627 obj.swig_decl(code) 628 code.write(target[0].abspath) 629 630# dummy target for generated code 631# we start out with all the Source files so they get copied to build/*/ also. 632SWIG = env.Dummy('swig', [s.tnode for s in Source.get()]) 633 634# Generate all of the SimObject param C++ struct header files 635params_hh_files = [] 636for name,simobj in sorted(sim_objects.iteritems()): 637 py_source = PySource.modules[simobj.__module__] 638 extra_deps = [ py_source.tnode ] 639 640 hh_file = File('params/%s.hh' % name) 641 params_hh_files.append(hh_file) 642 env.Command(hh_file, Value(name), 643 MakeAction(createSimObjectParamStruct, Transform("SO PARAM"))) 644 env.Depends(hh_file, depends + extra_deps) 645 env.Depends(SWIG, hh_file) 646 647# Generate any needed param SWIG wrapper files 648params_i_files = [] 649for name,param in params_to_swig.iteritems(): 650 i_file = File('python/m5/internal/%s.i' % (param.swig_module_name())) 651 params_i_files.append(i_file) 652 env.Command(i_file, Value(name), 653 MakeAction(createParamSwigWrapper, Transform("SW PARAM"))) 654 env.Depends(i_file, depends) 655 env.Depends(SWIG, i_file) 656 SwigSource('m5.internal', i_file) 657 658# Generate all enum header files 659for name,enum in sorted(all_enums.iteritems()): 660 py_source = PySource.modules[enum.__module__] 661 extra_deps = [ py_source.tnode ] 662 663 cc_file = File('enums/%s.cc' % name) 664 env.Command(cc_file, Value(name), 665 MakeAction(createEnumStrings, Transform("ENUM STR"))) 666 env.Depends(cc_file, depends + extra_deps) 667 env.Depends(SWIG, cc_file) 668 Source(cc_file) 669 670 hh_file = File('enums/%s.hh' % name) 671 env.Command(hh_file, Value(name), 672 MakeAction(createEnumDecls, Transform("ENUMDECL"))) 673 env.Depends(hh_file, depends + extra_deps) 674 env.Depends(SWIG, hh_file) 675 676 i_file = File('python/m5/internal/enum_%s.i' % name) 677 env.Command(i_file, Value(name), 678 MakeAction(createEnumSwigWrapper, Transform("ENUMSWIG"))) 679 env.Depends(i_file, depends + extra_deps) 680 env.Depends(SWIG, i_file) 681 SwigSource('m5.internal', i_file) 682 683# Generate SimObject SWIG wrapper files 684for name,simobj in sim_objects.iteritems(): 685 py_source = PySource.modules[simobj.__module__] 686 extra_deps = [ py_source.tnode ] 687 688 i_file = File('python/m5/internal/param_%s.i' % name) 689 env.Command(i_file, Value(name), 690 MakeAction(createSimObjectSwigWrapper, Transform("SO SWIG"))) 691 env.Depends(i_file, depends + extra_deps) 692 SwigSource('m5.internal', i_file) 693 694# Generate the main swig init file 695def makeEmbeddedSwigInit(target, source, env): 696 code = code_formatter() 697 module = source[0].get_contents() 698 code('''\ 699#include "sim/init.hh" 700 701extern "C" { 702 void init_${module}(); 703} 704 705EmbeddedSwig embed_swig_${module}(init_${module}); 706''') 707 code.write(str(target[0])) 708 709# Build all swig modules 710for swig in SwigSource.all: 711 env.Command([swig.cc_source.tnode, swig.py_source.tnode], swig.tnode, 712 MakeAction('$SWIG $SWIGFLAGS -outdir ${TARGETS[1].dir} ' 713 '-o ${TARGETS[0]} $SOURCES', Transform("SWIG"))) 714 cc_file = str(swig.tnode) 715 init_file = '%s/%s_init.cc' % (dirname(cc_file), basename(cc_file)) 716 env.Command(init_file, Value(swig.module), 717 MakeAction(makeEmbeddedSwigInit, Transform("EMBED SW"))) 718 env.Depends(SWIG, init_file) 719 Source(init_file, **swig.guards) 720 721# Build all protocol buffers if we have got protoc and protobuf available 722if env['HAVE_PROTOBUF']: 723 for proto in ProtoBuf.all: 724 # Use both the source and header as the target, and the .proto 725 # file as the source. When executing the protoc compiler, also 726 # specify the proto_path to avoid having the generated files 727 # include the path. 728 env.Command([proto.cc_file, proto.hh_file], proto.tnode, 729 MakeAction('$PROTOC --cpp_out ${TARGET.dir} ' 730 '--proto_path ${SOURCE.dir} $SOURCE', 731 Transform("PROTOC"))) 732 733 env.Depends(SWIG, [proto.cc_file, proto.hh_file]) 734 # Add the C++ source file 735 Source(proto.cc_file, **proto.guards) 736elif ProtoBuf.all: 737 print 'Got protobuf to build, but lacks support!' 738 Exit(1) 739 740# 741# Handle debug flags 742# 743def makeDebugFlagCC(target, source, env): 744 assert(len(target) == 1 and len(source) == 1) 745 746 code = code_formatter() 747 748 # delay definition of CompoundFlags until after all the definition 749 # of all constituent SimpleFlags 750 comp_code = code_formatter() 751 752 # file header 753 code(''' 754/* 755 * DO NOT EDIT THIS FILE! Automatically generated by SCons. 756 */ 757 758#include "base/debug.hh" 759 760namespace Debug { 761 762''') 763 764 for name, flag in sorted(source[0].read().iteritems()): 765 n, compound, desc = flag 766 assert n == name 767 768 if not compound: 769 code('SimpleFlag $name("$name", "$desc");') 770 else: 771 comp_code('CompoundFlag $name("$name", "$desc",') 772 comp_code.indent() 773 last = len(compound) - 1 774 for i,flag in enumerate(compound): 775 if i != last: 776 comp_code('$flag,') 777 else: 778 comp_code('$flag);') 779 comp_code.dedent() 780 781 code.append(comp_code) 782 code() 783 code('} // namespace Debug') 784 785 code.write(str(target[0])) 786 787def makeDebugFlagHH(target, source, env): 788 assert(len(target) == 1 and len(source) == 1) 789 790 val = eval(source[0].get_contents()) 791 name, compound, desc = val 792 793 code = code_formatter() 794 795 # file header boilerplate 796 code('''\ 797/* 798 * DO NOT EDIT THIS FILE! Automatically generated by SCons. 799 */ 800 801#ifndef __DEBUG_${name}_HH__ 802#define __DEBUG_${name}_HH__ 803 804namespace Debug { 805''') 806 807 if compound: 808 code('class CompoundFlag;') 809 code('class SimpleFlag;') 810 811 if compound: 812 code('extern CompoundFlag $name;') 813 for flag in compound: 814 code('extern SimpleFlag $flag;') 815 else: 816 code('extern SimpleFlag $name;') 817 818 code(''' 819} 820 821#endif // __DEBUG_${name}_HH__ 822''') 823 824 code.write(str(target[0])) 825 826for name,flag in sorted(debug_flags.iteritems()): 827 n, compound, desc = flag 828 assert n == name 829 830 hh_file = 'debug/%s.hh' % name 831 env.Command(hh_file, Value(flag), 832 MakeAction(makeDebugFlagHH, Transform("TRACING", 0))) 833 env.Depends(SWIG, hh_file) 834 835env.Command('debug/flags.cc', Value(debug_flags), 836 MakeAction(makeDebugFlagCC, Transform("TRACING", 0))) 837env.Depends(SWIG, 'debug/flags.cc') 838Source('debug/flags.cc') 839 840# Embed python files. All .py files that have been indicated by a 841# PySource() call in a SConscript need to be embedded into the M5 842# library. To do that, we compile the file to byte code, marshal the 843# byte code, compress it, and then generate a c++ file that 844# inserts the result into an array. 845def embedPyFile(target, source, env): 846 def c_str(string): 847 if string is None: 848 return "0" 849 return '"%s"' % string 850 851 '''Action function to compile a .py into a code object, marshal 852 it, compress it, and stick it into an asm file so the code appears 853 as just bytes with a label in the data section''' 854 855 src = file(str(source[0]), 'r').read() 856 857 pysource = PySource.tnodes[source[0]] 858 compiled = compile(src, pysource.abspath, 'exec') 859 marshalled = marshal.dumps(compiled) 860 compressed = zlib.compress(marshalled) 861 data = compressed 862 sym = pysource.symname 863 864 code = code_formatter() 865 code('''\ 866#include "sim/init.hh" 867 868namespace { 869 870const uint8_t data_${sym}[] = { 871''') 872 code.indent() 873 step = 16 874 for i in xrange(0, len(data), step): 875 x = array.array('B', data[i:i+step]) 876 code(''.join('%d,' % d for d in x)) 877 code.dedent() 878 879 code('''}; 880 881EmbeddedPython embedded_${sym}( 882 ${{c_str(pysource.arcname)}}, 883 ${{c_str(pysource.abspath)}}, 884 ${{c_str(pysource.modpath)}}, 885 data_${sym}, 886 ${{len(data)}}, 887 ${{len(marshalled)}}); 888 889} // anonymous namespace 890''') 891 code.write(str(target[0])) 892 893for source in PySource.all: 894 env.Command(source.cpp, source.tnode, 895 MakeAction(embedPyFile, Transform("EMBED PY"))) 896 env.Depends(SWIG, source.cpp) 897 Source(source.cpp, skip_no_python=True) 898 899######################################################################## 900# 901# Define binaries. Each different build type (debug, opt, etc.) gets 902# a slightly different build environment. 903# 904 905# List of constructed environments to pass back to SConstruct 906date_source = Source('base/date.cc', skip_lib=True) 907 908# Capture this directory for the closure makeEnv, otherwise when it is 909# called, it won't know what directory it should use. 910variant_dir = Dir('.').path 911def variant(*path): 912 return os.path.join(variant_dir, *path) 913def variantd(*path): 914 return variant(*path)+'/' 915 916# Function to create a new build environment as clone of current 917# environment 'env' with modified object suffix and optional stripped 918# binary. Additional keyword arguments are appended to corresponding 919# build environment vars. 920def makeEnv(env, label, objsfx, strip = False, **kwargs): 921 # SCons doesn't know to append a library suffix when there is a '.' in the 922 # name. Use '_' instead. 923 libname = variant('gem5_' + label) 924 exename = variant('gem5.' + label) 925 secondary_exename = variant('m5.' + label) 926 927 new_env = env.Clone(OBJSUFFIX=objsfx, SHOBJSUFFIX=objsfx + 's') 928 new_env.Label = label 929 new_env.Append(**kwargs) 930 931 swig_env = new_env.Clone() 932 933 # Both gcc and clang have issues with unused labels and values in 934 # the SWIG generated code 935 swig_env.Append(CCFLAGS=['-Wno-unused-label', '-Wno-unused-value']) 936 937 # Add additional warnings here that should not be applied to 938 # the SWIG generated code 939 new_env.Append(CXXFLAGS='-Wmissing-declarations') 940 941 if env['GCC']: 942 # Depending on the SWIG version, we also need to supress 943 # warnings about uninitialized variables and missing field 944 # initializers. 945 swig_env.Append(CCFLAGS=['-Wno-uninitialized', 946 '-Wno-missing-field-initializers', 947 '-Wno-unused-but-set-variable']) 948 949 # If gcc supports it, also warn for deletion of derived 950 # classes with non-virtual desctructors. For gcc >= 4.7 we 951 # also have to disable warnings about the SWIG code having 952 # potentially uninitialized variables. 953 if compareVersions(env['GCC_VERSION'], '4.7') >= 0: 954 new_env.Append(CXXFLAGS='-Wdelete-non-virtual-dtor') 955 swig_env.Append(CCFLAGS='-Wno-maybe-uninitialized') 956 if env['CLANG']: 957 # Always enable the warning for deletion of derived classes 958 # with non-virtual destructors 959 new_env.Append(CXXFLAGS=['-Wdelete-non-virtual-dtor']) 960 961 swig_env.Append(CCFLAGS=[ 962 # Some versions of SWIG can return uninitialized values 963 '-Wno-sometimes-uninitialized', 964 # Register storage is requested in a lot of places in 965 # SWIG-generated code. 966 '-Wno-deprecated-register', 967 ]) 968 969 werror_env = new_env.Clone() 970 werror_env.Append(CCFLAGS='-Werror') 971 972 def make_obj(source, static, extra_deps = None): 973 '''This function adds the specified source to the correct 974 build environment, and returns the corresponding SCons Object 975 nodes''' 976 977 if source.swig: 978 env = swig_env 979 elif source.Werror: 980 env = werror_env 981 else: 982 env = new_env 983 984 if static: 985 obj = env.StaticObject(source.tnode) 986 else: 987 obj = env.SharedObject(source.tnode) 988 989 if extra_deps: 990 env.Depends(obj, extra_deps) 991 992 return obj 993 994 lib_guards = {'main': False, 'skip_lib': False} 995 996 # Without Python, leave out all SWIG and Python content from the 997 # library builds. The option doesn't affect gem5 built as a program 998 if GetOption('without_python'): 999 lib_guards['skip_no_python'] = False 1000 1001 static_objs = [ make_obj(s, True) for s in Source.get(**lib_guards) ] 1002 shared_objs = [ make_obj(s, False) for s in Source.get(**lib_guards) ] 1003 1004 static_date = make_obj(date_source, static=True, extra_deps=static_objs) 1005 static_objs.append(static_date) 1006 1007 shared_date = make_obj(date_source, static=False, extra_deps=shared_objs) 1008 shared_objs.append(shared_date) 1009 1010 # First make a library of everything but main() so other programs can 1011 # link against m5. 1012 static_lib = new_env.StaticLibrary(libname, static_objs) 1013 shared_lib = new_env.SharedLibrary(libname, shared_objs) 1014 1015 # Now link a stub with main() and the static library. 1016 main_objs = [ make_obj(s, True) for s in Source.get(main=True) ] 1017 1018 for test in UnitTest.all: 1019 flags = { test.target : True } 1020 test_sources = Source.get(**flags) 1021 test_objs = [ make_obj(s, static=True) for s in test_sources ] 1022 if test.main: 1023 test_objs += main_objs 1024 path = variant('unittest/%s.%s' % (test.target, label)) 1025 new_env.Program(path, test_objs + static_objs) 1026 1027 progname = exename 1028 if strip: 1029 progname += '.unstripped' 1030 1031 targets = new_env.Program(progname, main_objs + static_objs) 1032 1033 if strip: 1034 if sys.platform == 'sunos5': 1035 cmd = 'cp $SOURCE $TARGET; strip $TARGET' 1036 else: 1037 cmd = 'strip $SOURCE -o $TARGET' 1038 targets = new_env.Command(exename, progname, 1039 MakeAction(cmd, Transform("STRIP"))) 1040 1041 new_env.Command(secondary_exename, exename, 1042 MakeAction('ln $SOURCE $TARGET', Transform("HARDLINK"))) 1043 1044 new_env.M5Binary = targets[0] 1045 return new_env 1046 1047# Start out with the compiler flags common to all compilers, 1048# i.e. they all use -g for opt and -g -pg for prof 1049ccflags = {'debug' : [], 'opt' : ['-g'], 'fast' : [], 'prof' : ['-g', '-pg'], 1050 'perf' : ['-g']} 1051 1052# Start out with the linker flags common to all linkers, i.e. -pg for 1053# prof, and -lprofiler for perf. The -lprofile flag is surrounded by 1054# no-as-needed and as-needed as the binutils linker is too clever and 1055# simply doesn't link to the library otherwise. 1056ldflags = {'debug' : [], 'opt' : [], 'fast' : [], 'prof' : ['-pg'], 1057 'perf' : ['-Wl,--no-as-needed', '-lprofiler', '-Wl,--as-needed']} 1058 1059# For Link Time Optimization, the optimisation flags used to compile 1060# individual files are decoupled from those used at link time 1061# (i.e. you can compile with -O3 and perform LTO with -O0), so we need 1062# to also update the linker flags based on the target. 1063if env['GCC']: 1064 if sys.platform == 'sunos5': 1065 ccflags['debug'] += ['-gstabs+'] 1066 else: 1067 ccflags['debug'] += ['-ggdb3'] 1068 ldflags['debug'] += ['-O0'] 1069 # opt, fast, prof and perf all share the same cc flags, also add 1070 # the optimization to the ldflags as LTO defers the optimization 1071 # to link time 1072 for target in ['opt', 'fast', 'prof', 'perf']: 1073 ccflags[target] += ['-O3'] 1074 ldflags[target] += ['-O3'] 1075 1076 ccflags['fast'] += env['LTO_CCFLAGS'] 1077 ldflags['fast'] += env['LTO_LDFLAGS'] 1078elif env['CLANG']: 1079 ccflags['debug'] += ['-g', '-O0'] 1080 # opt, fast, prof and perf all share the same cc flags 1081 for target in ['opt', 'fast', 'prof', 'perf']: 1082 ccflags[target] += ['-O3'] 1083else: 1084 print 'Unknown compiler, please fix compiler options' 1085 Exit(1) 1086 1087 1088# To speed things up, we only instantiate the build environments we 1089# need. We try to identify the needed environment for each target; if 1090# we can't, we fall back on instantiating all the environments just to 1091# be safe. 1092target_types = ['debug', 'opt', 'fast', 'prof', 'perf'] 1093obj2target = {'do': 'debug', 'o': 'opt', 'fo': 'fast', 'po': 'prof', 1094 'gpo' : 'perf'} 1095 1096def identifyTarget(t): 1097 ext = t.split('.')[-1] 1098 if ext in target_types: 1099 return ext 1100 if obj2target.has_key(ext): 1101 return obj2target[ext] 1102 match = re.search(r'/tests/([^/]+)/', t) 1103 if match and match.group(1) in target_types: 1104 return match.group(1) 1105 return 'all' 1106 1107needed_envs = [identifyTarget(target) for target in BUILD_TARGETS] 1108if 'all' in needed_envs: 1109 needed_envs += target_types 1110 1111gem5_root = Dir('.').up().up().abspath 1112def makeEnvirons(target, source, env): 1113 # cause any later Source() calls to be fatal, as a diagnostic. 1114 Source.done() 1115 1116 envList = [] 1117 1118 # Debug binary 1119 if 'debug' in needed_envs: 1120 envList.append( 1121 makeEnv(env, 'debug', '.do', 1122 CCFLAGS = Split(ccflags['debug']), 1123 CPPDEFINES = ['DEBUG', 'TRACING_ON=1'], 1124 LINKFLAGS = Split(ldflags['debug']))) 1125 1126 # Optimized binary 1127 if 'opt' in needed_envs: 1128 envList.append( 1129 makeEnv(env, 'opt', '.o', 1130 CCFLAGS = Split(ccflags['opt']), 1131 CPPDEFINES = ['TRACING_ON=1'], 1132 LINKFLAGS = Split(ldflags['opt']))) 1133 1134 # "Fast" binary 1135 if 'fast' in needed_envs: 1136 envList.append( 1137 makeEnv(env, 'fast', '.fo', strip = True, 1138 CCFLAGS = Split(ccflags['fast']), 1139 CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'], 1140 LINKFLAGS = Split(ldflags['fast']))) 1141 1142 # Profiled binary using gprof 1143 if 'prof' in needed_envs: 1144 envList.append( 1145 makeEnv(env, 'prof', '.po', 1146 CCFLAGS = Split(ccflags['prof']), 1147 CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'], 1148 LINKFLAGS = Split(ldflags['prof']))) 1149 1150 # Profiled binary using google-pprof 1151 if 'perf' in needed_envs: 1152 envList.append( 1153 makeEnv(env, 'perf', '.gpo', 1154 CCFLAGS = Split(ccflags['perf']), 1155 CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'], 1156 LINKFLAGS = Split(ldflags['perf']))) 1157 1158 # Set up the regression tests for each build. 1159 for e in envList: 1160 SConscript(os.path.join(gem5_root, 'tests', 'SConscript'), 1161 variant_dir = variantd('tests', e.Label), 1162 exports = { 'env' : e }, duplicate = False) 1163 1164# The MakeEnvirons Builder defers the full dependency collection until 1165# after processing the ISA definition (due to dynamically generated 1166# source files). Add this dependency to all targets so they will wait 1167# until the environments are completely set up. Otherwise, a second 1168# process (e.g. -j2 or higher) will try to compile the requested target, 1169# not know how, and fail. 1170env.Append(BUILDERS = {'MakeEnvirons' : 1171 Builder(action=MakeAction(makeEnvirons, 1172 Transform("ENVIRONS", 1)))}) 1173 1174isa_target = env['PHONY_BASE'] + '-deps' 1175environs = env['PHONY_BASE'] + '-environs' 1176env.Depends('#all-deps', isa_target) 1177env.Depends('#all-environs', environs) 1178env.ScanISA(isa_target, File('arch/%s/generated/inc.d' % env['TARGET_ISA'])) 1179envSetup = env.MakeEnvirons(environs, isa_target) 1180 1181# make sure no -deps targets occur before all ISAs are complete 1182env.Depends(isa_target, '#all-isas') 1183# likewise for -environs targets and all the -deps targets 1184env.Depends(environs, '#all-deps') 1185