SConscript revision 10453
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 32import bisect 33import imp 34import marshal 35import os 36import re 37import sys 38import zlib 39 40from os.path import basename, dirname, exists, isdir, isfile, join as joinpath 41 42import SCons 43 44# This file defines how to build a particular configuration of gem5 45# based on variable settings in the 'env' build environment. 46 47Import('*') 48 49# Children need to see the environment 50Export('env') 51 52build_env = [(opt, env[opt]) for opt in export_vars] 53 54from m5.util import code_formatter, compareVersions 55 56######################################################################## 57# Code for adding source files of various types 58# 59# When specifying a source file of some type, a set of guards can be 60# specified for that file. When get() is used to find the files, if 61# get specifies a set of filters, only files that match those filters 62# will be accepted (unspecified filters on files are assumed to be 63# false). Current filters are: 64# main -- specifies the gem5 main() function 65# skip_lib -- do not put this file into the gem5 library 66# skip_no_python -- do not put this file into a no_python library 67# as it embeds compiled Python 68# <unittest> -- unit tests use filters based on the unit test name 69# 70# A parent can now be specified for a source file and default filter 71# values will be retrieved recursively from parents (children override 72# parents). 73# 74class SourceMeta(type): 75 '''Meta class for source files that keeps track of all files of a 76 particular type and has a get function for finding all functions 77 of a certain type that match a set of guards''' 78 def __init__(cls, name, bases, dict): 79 super(SourceMeta, cls).__init__(name, bases, dict) 80 cls.all = [] 81 82 def get(cls, **guards): 83 '''Find all files that match the specified guards. If a source 84 file does not specify a flag, the default is False''' 85 for src in cls.all: 86 for flag,value in guards.iteritems(): 87 # if the flag is found and has a different value, skip 88 # this file 89 if src.all_guards.get(flag, False) != value: 90 break 91 else: 92 yield src 93 94class SourceFile(object): 95 '''Base object that encapsulates the notion of a source file. 96 This includes, the source node, target node, various manipulations 97 of those. A source file also specifies a set of guards which 98 describing which builds the source file applies to. A parent can 99 also be specified to get default guards from''' 100 __metaclass__ = SourceMeta 101 def __init__(self, source, parent=None, **guards): 102 self.guards = guards 103 self.parent = parent 104 105 tnode = source 106 if not isinstance(source, SCons.Node.FS.File): 107 tnode = File(source) 108 109 self.tnode = tnode 110 self.snode = tnode.srcnode() 111 112 for base in type(self).__mro__: 113 if issubclass(base, SourceFile): 114 base.all.append(self) 115 116 @property 117 def filename(self): 118 return str(self.tnode) 119 120 @property 121 def dirname(self): 122 return dirname(self.filename) 123 124 @property 125 def basename(self): 126 return basename(self.filename) 127 128 @property 129 def extname(self): 130 index = self.basename.rfind('.') 131 if index <= 0: 132 # dot files aren't extensions 133 return self.basename, None 134 135 return self.basename[:index], self.basename[index+1:] 136 137 @property 138 def all_guards(self): 139 '''find all guards for this object getting default values 140 recursively from its parents''' 141 guards = {} 142 if self.parent: 143 guards.update(self.parent.guards) 144 guards.update(self.guards) 145 return guards 146 147 def __lt__(self, other): return self.filename < other.filename 148 def __le__(self, other): return self.filename <= other.filename 149 def __gt__(self, other): return self.filename > other.filename 150 def __ge__(self, other): return self.filename >= other.filename 151 def __eq__(self, other): return self.filename == other.filename 152 def __ne__(self, other): return self.filename != other.filename 153 154 @staticmethod 155 def done(): 156 def disabled(cls, name, *ignored): 157 raise RuntimeError("Additional SourceFile '%s'" % name,\ 158 "declared, but targets deps are already fixed.") 159 SourceFile.__init__ = disabled 160 161 162class Source(SourceFile): 163 '''Add a c/c++ source file to the build''' 164 def __init__(self, source, Werror=True, swig=False, **guards): 165 '''specify the source file, and any guards''' 166 super(Source, self).__init__(source, **guards) 167 168 self.Werror = Werror 169 self.swig = swig 170 171class 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# Generate all of the SimObject param C++ struct header files 631params_hh_files = [] 632for name,simobj in sorted(sim_objects.iteritems()): 633 py_source = PySource.modules[simobj.__module__] 634 extra_deps = [ py_source.tnode ] 635 636 hh_file = File('params/%s.hh' % name) 637 params_hh_files.append(hh_file) 638 env.Command(hh_file, Value(name), 639 MakeAction(createSimObjectParamStruct, Transform("SO PARAM"))) 640 env.Depends(hh_file, depends + extra_deps) 641 642# Generate any needed param SWIG wrapper files 643params_i_files = [] 644for name,param in params_to_swig.iteritems(): 645 i_file = File('python/m5/internal/%s.i' % (param.swig_module_name())) 646 params_i_files.append(i_file) 647 env.Command(i_file, Value(name), 648 MakeAction(createParamSwigWrapper, Transform("SW PARAM"))) 649 env.Depends(i_file, depends) 650 SwigSource('m5.internal', i_file) 651 652# Generate all enum header files 653for name,enum in sorted(all_enums.iteritems()): 654 py_source = PySource.modules[enum.__module__] 655 extra_deps = [ py_source.tnode ] 656 657 cc_file = File('enums/%s.cc' % name) 658 env.Command(cc_file, Value(name), 659 MakeAction(createEnumStrings, Transform("ENUM STR"))) 660 env.Depends(cc_file, depends + extra_deps) 661 Source(cc_file) 662 663 hh_file = File('enums/%s.hh' % name) 664 env.Command(hh_file, Value(name), 665 MakeAction(createEnumDecls, Transform("ENUMDECL"))) 666 env.Depends(hh_file, depends + extra_deps) 667 668 i_file = File('python/m5/internal/enum_%s.i' % name) 669 env.Command(i_file, Value(name), 670 MakeAction(createEnumSwigWrapper, Transform("ENUMSWIG"))) 671 env.Depends(i_file, depends + extra_deps) 672 SwigSource('m5.internal', i_file) 673 674# Generate SimObject SWIG wrapper files 675for name,simobj in sim_objects.iteritems(): 676 py_source = PySource.modules[simobj.__module__] 677 extra_deps = [ py_source.tnode ] 678 679 i_file = File('python/m5/internal/param_%s.i' % name) 680 env.Command(i_file, Value(name), 681 MakeAction(createSimObjectSwigWrapper, Transform("SO SWIG"))) 682 env.Depends(i_file, depends + extra_deps) 683 SwigSource('m5.internal', i_file) 684 685# Generate the main swig init file 686def makeEmbeddedSwigInit(target, source, env): 687 code = code_formatter() 688 module = source[0].get_contents() 689 code('''\ 690#include "sim/init.hh" 691 692extern "C" { 693 void init_${module}(); 694} 695 696EmbeddedSwig embed_swig_${module}(init_${module}); 697''') 698 code.write(str(target[0])) 699 700# Build all swig modules 701for swig in SwigSource.all: 702 env.Command([swig.cc_source.tnode, swig.py_source.tnode], swig.tnode, 703 MakeAction('$SWIG $SWIGFLAGS -outdir ${TARGETS[1].dir} ' 704 '-o ${TARGETS[0]} $SOURCES', Transform("SWIG"))) 705 cc_file = str(swig.tnode) 706 init_file = '%s/%s_init.cc' % (dirname(cc_file), basename(cc_file)) 707 env.Command(init_file, Value(swig.module), 708 MakeAction(makeEmbeddedSwigInit, Transform("EMBED SW"))) 709 Source(init_file, **swig.guards) 710 711# Build all protocol buffers if we have got protoc and protobuf available 712if env['HAVE_PROTOBUF']: 713 for proto in ProtoBuf.all: 714 # Use both the source and header as the target, and the .proto 715 # file as the source. When executing the protoc compiler, also 716 # specify the proto_path to avoid having the generated files 717 # include the path. 718 env.Command([proto.cc_file, proto.hh_file], proto.tnode, 719 MakeAction('$PROTOC --cpp_out ${TARGET.dir} ' 720 '--proto_path ${SOURCE.dir} $SOURCE', 721 Transform("PROTOC"))) 722 723 # Add the C++ source file 724 Source(proto.cc_file, **proto.guards) 725elif ProtoBuf.all: 726 print 'Got protobuf to build, but lacks support!' 727 Exit(1) 728 729# 730# Handle debug flags 731# 732def makeDebugFlagCC(target, source, env): 733 assert(len(target) == 1 and len(source) == 1) 734 735 val = eval(source[0].get_contents()) 736 name, compound, desc = val 737 compound = list(sorted(compound)) 738 739 code = code_formatter() 740 741 # file header 742 code(''' 743/* 744 * DO NOT EDIT THIS FILE! Automatically generated 745 */ 746 747#include "base/debug.hh" 748''') 749 750 for flag in compound: 751 code('#include "debug/$flag.hh"') 752 code() 753 code('namespace Debug {') 754 code() 755 756 if not compound: 757 code('SimpleFlag $name("$name", "$desc");') 758 else: 759 code('CompoundFlag $name("$name", "$desc",') 760 code.indent() 761 last = len(compound) - 1 762 for i,flag in enumerate(compound): 763 if i != last: 764 code('$flag,') 765 else: 766 code('$flag);') 767 code.dedent() 768 769 code() 770 code('} // namespace Debug') 771 772 code.write(str(target[0])) 773 774def makeDebugFlagHH(target, source, env): 775 assert(len(target) == 1 and len(source) == 1) 776 777 val = eval(source[0].get_contents()) 778 name, compound, desc = val 779 780 code = code_formatter() 781 782 # file header boilerplate 783 code('''\ 784/* 785 * DO NOT EDIT THIS FILE! 786 * 787 * Automatically generated by SCons 788 */ 789 790#ifndef __DEBUG_${name}_HH__ 791#define __DEBUG_${name}_HH__ 792 793namespace Debug { 794''') 795 796 if compound: 797 code('class CompoundFlag;') 798 code('class SimpleFlag;') 799 800 if compound: 801 code('extern CompoundFlag $name;') 802 for flag in compound: 803 code('extern SimpleFlag $flag;') 804 else: 805 code('extern SimpleFlag $name;') 806 807 code(''' 808} 809 810#endif // __DEBUG_${name}_HH__ 811''') 812 813 code.write(str(target[0])) 814 815for name,flag in sorted(debug_flags.iteritems()): 816 n, compound, desc = flag 817 assert n == name 818 819 env.Command('debug/%s.hh' % name, Value(flag), 820 MakeAction(makeDebugFlagHH, Transform("TRACING", 0))) 821 env.Command('debug/%s.cc' % name, Value(flag), 822 MakeAction(makeDebugFlagCC, Transform("TRACING", 0))) 823 Source('debug/%s.cc' % name) 824 825# Embed python files. All .py files that have been indicated by a 826# PySource() call in a SConscript need to be embedded into the M5 827# library. To do that, we compile the file to byte code, marshal the 828# byte code, compress it, and then generate a c++ file that 829# inserts the result into an array. 830def embedPyFile(target, source, env): 831 def c_str(string): 832 if string is None: 833 return "0" 834 return '"%s"' % string 835 836 '''Action function to compile a .py into a code object, marshal 837 it, compress it, and stick it into an asm file so the code appears 838 as just bytes with a label in the data section''' 839 840 src = file(str(source[0]), 'r').read() 841 842 pysource = PySource.tnodes[source[0]] 843 compiled = compile(src, pysource.abspath, 'exec') 844 marshalled = marshal.dumps(compiled) 845 compressed = zlib.compress(marshalled) 846 data = compressed 847 sym = pysource.symname 848 849 code = code_formatter() 850 code('''\ 851#include "sim/init.hh" 852 853namespace { 854 855const uint8_t data_${sym}[] = { 856''') 857 code.indent() 858 step = 16 859 for i in xrange(0, len(data), step): 860 x = array.array('B', data[i:i+step]) 861 code(''.join('%d,' % d for d in x)) 862 code.dedent() 863 864 code('''}; 865 866EmbeddedPython embedded_${sym}( 867 ${{c_str(pysource.arcname)}}, 868 ${{c_str(pysource.abspath)}}, 869 ${{c_str(pysource.modpath)}}, 870 data_${sym}, 871 ${{len(data)}}, 872 ${{len(marshalled)}}); 873 874} // anonymous namespace 875''') 876 code.write(str(target[0])) 877 878for source in PySource.all: 879 env.Command(source.cpp, source.tnode, 880 MakeAction(embedPyFile, Transform("EMBED PY"))) 881 Source(source.cpp, skip_no_python=True) 882 883######################################################################## 884# 885# Define binaries. Each different build type (debug, opt, etc.) gets 886# a slightly different build environment. 887# 888 889# List of constructed environments to pass back to SConstruct 890date_source = Source('base/date.cc', skip_lib=True) 891 892# Capture this directory for the closure makeEnv, otherwise when it is 893# called, it won't know what directory it should use. 894variant_dir = Dir('.').path 895def variant(*path): 896 return os.path.join(variant_dir, *path) 897def variantd(*path): 898 return variant(*path)+'/' 899 900# Function to create a new build environment as clone of current 901# environment 'env' with modified object suffix and optional stripped 902# binary. Additional keyword arguments are appended to corresponding 903# build environment vars. 904def makeEnv(env, label, objsfx, strip = False, **kwargs): 905 # SCons doesn't know to append a library suffix when there is a '.' in the 906 # name. Use '_' instead. 907 libname = variant('gem5_' + label) 908 exename = variant('gem5.' + label) 909 secondary_exename = variant('m5.' + label) 910 911 new_env = env.Clone(OBJSUFFIX=objsfx, SHOBJSUFFIX=objsfx + 's') 912 new_env.Label = label 913 new_env.Append(**kwargs) 914 915 swig_env = new_env.Clone() 916 917 # Both gcc and clang have issues with unused labels and values in 918 # the SWIG generated code 919 swig_env.Append(CCFLAGS=['-Wno-unused-label', '-Wno-unused-value']) 920 921 # Add additional warnings here that should not be applied to 922 # the SWIG generated code 923 new_env.Append(CXXFLAGS='-Wmissing-declarations') 924 925 if env['GCC']: 926 # Depending on the SWIG version, we also need to supress 927 # warnings about uninitialized variables and missing field 928 # initializers. 929 swig_env.Append(CCFLAGS=['-Wno-uninitialized', 930 '-Wno-missing-field-initializers', 931 '-Wno-unused-but-set-variable']) 932 933 # If gcc supports it, also warn for deletion of derived 934 # classes with non-virtual desctructors. For gcc >= 4.7 we 935 # also have to disable warnings about the SWIG code having 936 # potentially uninitialized variables. 937 if compareVersions(env['GCC_VERSION'], '4.7') >= 0: 938 new_env.Append(CXXFLAGS='-Wdelete-non-virtual-dtor') 939 swig_env.Append(CCFLAGS='-Wno-maybe-uninitialized') 940 if env['CLANG']: 941 # Always enable the warning for deletion of derived classes 942 # with non-virtual destructors 943 new_env.Append(CXXFLAGS=['-Wdelete-non-virtual-dtor']) 944 945 swig_env.Append(CCFLAGS=[ 946 # Some versions of SWIG can return uninitialized values 947 '-Wno-sometimes-uninitialized', 948 # Register storage is requested in a lot of places in 949 # SWIG-generated code. 950 '-Wno-deprecated-register', 951 ]) 952 953 werror_env = new_env.Clone() 954 werror_env.Append(CCFLAGS='-Werror') 955 956 def make_obj(source, static, extra_deps = None): 957 '''This function adds the specified source to the correct 958 build environment, and returns the corresponding SCons Object 959 nodes''' 960 961 if source.swig: 962 env = swig_env 963 elif source.Werror: 964 env = werror_env 965 else: 966 env = new_env 967 968 if static: 969 obj = env.StaticObject(source.tnode) 970 else: 971 obj = env.SharedObject(source.tnode) 972 973 if extra_deps: 974 env.Depends(obj, extra_deps) 975 976 return obj 977 978 lib_guards = {'main': False, 'skip_lib': False} 979 980 # Without Python, leave out all SWIG and Python content from the 981 # library builds. The option doesn't affect gem5 built as a program 982 if GetOption('without_python'): 983 lib_guards['skip_no_python'] = False 984 985 static_objs = [ make_obj(s, True) for s in Source.get(**lib_guards) ] 986 shared_objs = [ make_obj(s, False) for s in Source.get(**lib_guards) ] 987 988 static_date = make_obj(date_source, static=True, extra_deps=static_objs) 989 static_objs.append(static_date) 990 991 shared_date = make_obj(date_source, static=False, extra_deps=shared_objs) 992 shared_objs.append(shared_date) 993 994 # First make a library of everything but main() so other programs can 995 # link against m5. 996 static_lib = new_env.StaticLibrary(libname, static_objs) 997 shared_lib = new_env.SharedLibrary(libname, shared_objs) 998 999 # Now link a stub with main() and the static library. 1000 main_objs = [ make_obj(s, True) for s in Source.get(main=True) ] 1001 1002 for test in UnitTest.all: 1003 flags = { test.target : True } 1004 test_sources = Source.get(**flags) 1005 test_objs = [ make_obj(s, static=True) for s in test_sources ] 1006 if test.main: 1007 test_objs += main_objs 1008 path = variant('unittest/%s.%s' % (test.target, label)) 1009 new_env.Program(path, test_objs + static_objs) 1010 1011 progname = exename 1012 if strip: 1013 progname += '.unstripped' 1014 1015 targets = new_env.Program(progname, main_objs + static_objs) 1016 1017 if strip: 1018 if sys.platform == 'sunos5': 1019 cmd = 'cp $SOURCE $TARGET; strip $TARGET' 1020 else: 1021 cmd = 'strip $SOURCE -o $TARGET' 1022 targets = new_env.Command(exename, progname, 1023 MakeAction(cmd, Transform("STRIP"))) 1024 1025 new_env.Command(secondary_exename, exename, 1026 MakeAction('ln $SOURCE $TARGET', Transform("HARDLINK"))) 1027 1028 new_env.M5Binary = targets[0] 1029 return new_env 1030 1031# Start out with the compiler flags common to all compilers, 1032# i.e. they all use -g for opt and -g -pg for prof 1033ccflags = {'debug' : [], 'opt' : ['-g'], 'fast' : [], 'prof' : ['-g', '-pg'], 1034 'perf' : ['-g']} 1035 1036# Start out with the linker flags common to all linkers, i.e. -pg for 1037# prof, and -lprofiler for perf. The -lprofile flag is surrounded by 1038# no-as-needed and as-needed as the binutils linker is too clever and 1039# simply doesn't link to the library otherwise. 1040ldflags = {'debug' : [], 'opt' : [], 'fast' : [], 'prof' : ['-pg'], 1041 'perf' : ['-Wl,--no-as-needed', '-lprofiler', '-Wl,--as-needed']} 1042 1043# For Link Time Optimization, the optimisation flags used to compile 1044# individual files are decoupled from those used at link time 1045# (i.e. you can compile with -O3 and perform LTO with -O0), so we need 1046# to also update the linker flags based on the target. 1047if env['GCC']: 1048 if sys.platform == 'sunos5': 1049 ccflags['debug'] += ['-gstabs+'] 1050 else: 1051 ccflags['debug'] += ['-ggdb3'] 1052 ldflags['debug'] += ['-O0'] 1053 # opt, fast, prof and perf all share the same cc flags, also add 1054 # the optimization to the ldflags as LTO defers the optimization 1055 # to link time 1056 for target in ['opt', 'fast', 'prof', 'perf']: 1057 ccflags[target] += ['-O3'] 1058 ldflags[target] += ['-O3'] 1059 1060 ccflags['fast'] += env['LTO_CCFLAGS'] 1061 ldflags['fast'] += env['LTO_LDFLAGS'] 1062elif env['CLANG']: 1063 ccflags['debug'] += ['-g', '-O0'] 1064 # opt, fast, prof and perf all share the same cc flags 1065 for target in ['opt', 'fast', 'prof', 'perf']: 1066 ccflags[target] += ['-O3'] 1067else: 1068 print 'Unknown compiler, please fix compiler options' 1069 Exit(1) 1070 1071 1072# To speed things up, we only instantiate the build environments we 1073# need. We try to identify the needed environment for each target; if 1074# we can't, we fall back on instantiating all the environments just to 1075# be safe. 1076target_types = ['debug', 'opt', 'fast', 'prof', 'perf'] 1077obj2target = {'do': 'debug', 'o': 'opt', 'fo': 'fast', 'po': 'prof', 1078 'gpo' : 'perf'} 1079 1080def identifyTarget(t): 1081 ext = t.split('.')[-1] 1082 if ext in target_types: 1083 return ext 1084 if obj2target.has_key(ext): 1085 return obj2target[ext] 1086 match = re.search(r'/tests/([^/]+)/', t) 1087 if match and match.group(1) in target_types: 1088 return match.group(1) 1089 return 'all' 1090 1091needed_envs = [identifyTarget(target) for target in BUILD_TARGETS] 1092if 'all' in needed_envs: 1093 needed_envs += target_types 1094 1095gem5_root = Dir('.').up().up().abspath 1096def makeEnvirons(target, source, env): 1097 # cause any later Source() calls to be fatal, as a diagnostic. 1098 Source.done() 1099 1100 envList = [] 1101 1102 # Debug binary 1103 if 'debug' in needed_envs: 1104 envList.append( 1105 makeEnv(env, 'debug', '.do', 1106 CCFLAGS = Split(ccflags['debug']), 1107 CPPDEFINES = ['DEBUG', 'TRACING_ON=1'], 1108 LINKFLAGS = Split(ldflags['debug']))) 1109 1110 # Optimized binary 1111 if 'opt' in needed_envs: 1112 envList.append( 1113 makeEnv(env, 'opt', '.o', 1114 CCFLAGS = Split(ccflags['opt']), 1115 CPPDEFINES = ['TRACING_ON=1'], 1116 LINKFLAGS = Split(ldflags['opt']))) 1117 1118 # "Fast" binary 1119 if 'fast' in needed_envs: 1120 envList.append( 1121 makeEnv(env, 'fast', '.fo', strip = True, 1122 CCFLAGS = Split(ccflags['fast']), 1123 CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'], 1124 LINKFLAGS = Split(ldflags['fast']))) 1125 1126 # Profiled binary using gprof 1127 if 'prof' in needed_envs: 1128 envList.append( 1129 makeEnv(env, 'prof', '.po', 1130 CCFLAGS = Split(ccflags['prof']), 1131 CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'], 1132 LINKFLAGS = Split(ldflags['prof']))) 1133 1134 # Profiled binary using google-pprof 1135 if 'perf' in needed_envs: 1136 envList.append( 1137 makeEnv(env, 'perf', '.gpo', 1138 CCFLAGS = Split(ccflags['perf']), 1139 CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'], 1140 LINKFLAGS = Split(ldflags['perf']))) 1141 1142 # Set up the regression tests for each build. 1143 for e in envList: 1144 SConscript(os.path.join(gem5_root, 'tests', 'SConscript'), 1145 variant_dir = variantd('tests', e.Label), 1146 exports = { 'env' : e }, duplicate = False) 1147 1148# The MakeEnvirons Builder defers the full dependency collection until 1149# after processing the ISA definition (due to dynamically generated 1150# source files). Add this dependency to all targets so they will wait 1151# until the environments are completely set up. Otherwise, a second 1152# process (e.g. -j2 or higher) will try to compile the requested target, 1153# not know how, and fail. 1154env.Append(BUILDERS = {'MakeEnvirons' : 1155 Builder(action=MakeAction(makeEnvirons, 1156 Transform("ENVIRONS", 1)))}) 1157 1158isa_target = env['PHONY_BASE'] + '-deps' 1159environs = env['PHONY_BASE'] + '-environs' 1160env.Depends('#all-deps', isa_target) 1161env.Depends('#all-environs', environs) 1162env.ScanISA(isa_target, File('arch/%s/generated/inc.d' % env['TARGET_ISA'])) 1163envSetup = env.MakeEnvirons(environs, isa_target) 1164 1165# make sure no -deps targets occur before all ISAs are complete 1166env.Depends(isa_target, '#all-isas') 1167# likewise for -environs targets and all the -deps targets 1168env.Depends(environs, '#all-deps') 1169