3 # Copyright (c) 2004-2005 The Regents of The University of Michigan
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.
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.
29 # Authors: Nathan Binkert
40 from os.path import basename, dirname, exists, isdir, isfile, join as joinpath
44 # This file defines how to build a particular configuration of gem5
45 # based on variable settings in the 'env' build environment.
49 # Children need to see the environment
52 build_env = [(opt, env[opt]) for opt in export_vars]
54 from m5.util import code_formatter, compareVersions
56 ########################################################################
57 # Code for adding source files of various types
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 # <unittest> -- unit tests use filters based on the unit test name
68 # A parent can now be specified for a source file and default filter
69 # values will be retrieved recursively from parents (children override
72 class SourceMeta(type):
73 '''Meta class for source files that keeps track of all files of a
74 particular type and has a get function for finding all functions
75 of a certain type that match a set of guards'''
76 def __init__(cls, name, bases, dict):
77 super(SourceMeta, cls).__init__(name, bases, dict)
80 def get(cls, **guards):
81 '''Find all files that match the specified guards. If a source
82 file does not specify a flag, the default is False'''
84 for flag,value in guards.iteritems():
85 # if the flag is found and has a different value, skip
87 if src.all_guards.get(flag, False) != value:
92 class SourceFile(object):
93 '''Base object that encapsulates the notion of a source file.
94 This includes, the source node, target node, various manipulations
95 of those. A source file also specifies a set of guards which
96 describing which builds the source file applies to. A parent can
97 also be specified to get default guards from'''
98 __metaclass__ = SourceMeta
99 def __init__(self, source, parent=None, **guards):
104 if not isinstance(source, SCons.Node.FS.File):
108 self.snode = tnode.srcnode()
110 for base in type(self).__mro__:
111 if issubclass(base, SourceFile):
112 base.all.append(self)
116 return str(self.tnode)
120 return dirname(self.filename)
124 return basename(self.filename)
128 index = self.basename.rfind('.')
130 # dot files aren't extensions
131 return self.basename, None
133 return self.basename[:index], self.basename[index+1:]
136 def all_guards(self):
137 '''find all guards for this object getting default values
138 recursively from its parents'''
141 guards.update(self.parent.guards)
142 guards.update(self.guards)
145 def __lt__(self, other): return self.filename < other.filename
146 def __le__(self, other): return self.filename <= other.filename
147 def __gt__(self, other): return self.filename > other.filename
148 def __ge__(self, other): return self.filename >= other.filename
149 def __eq__(self, other): return self.filename == other.filename
150 def __ne__(self, other): return self.filename != other.filename
152 class Source(SourceFile):
153 '''Add a c/c++ source file to the build'''
154 def __init__(self, source, Werror=True, swig=False, **guards):
155 '''specify the source file, and any guards'''
156 super(Source, self).__init__(source, **guards)
161 class PySource(SourceFile):
162 '''Add a python source file to the named package'''
163 invalid_sym_char = re.compile('[^A-z0-9_]')
168 def __init__(self, package, source, **guards):
169 '''specify the python package, the source file, and any guards'''
170 super(PySource, self).__init__(source, **guards)
172 modname,ext = self.extname
176 path = package.split('.')
181 if modname != '__init__':
182 modpath += [ modname ]
183 modpath = '.'.join(modpath)
185 arcpath = path + [ self.basename ]
186 abspath = self.snode.abspath
187 if not exists(abspath):
188 abspath = self.tnode.abspath
190 self.package = package
191 self.modname = modname
192 self.modpath = modpath
193 self.arcname = joinpath(*arcpath)
194 self.abspath = abspath
195 self.compiled = File(self.filename + 'c')
196 self.cpp = File(self.filename + '.cc')
197 self.symname = PySource.invalid_sym_char.sub('_', modpath)
199 PySource.modules[modpath] = self
200 PySource.tnodes[self.tnode] = self
201 PySource.symnames[self.symname] = self
203 class SimObject(PySource):
204 '''Add a SimObject python file as a python source object and add
205 it to a list of sim object modules'''
210 def __init__(self, source, **guards):
211 '''Specify the source file and any guards (automatically in
212 the m5.objects package)'''
213 super(SimObject, self).__init__('m5.objects', source, **guards)
215 raise AttributeError, "Too late to call SimObject now."
217 bisect.insort_right(SimObject.modnames, self.modname)
219 class SwigSource(SourceFile):
220 '''Add a swig file to build'''
222 def __init__(self, package, source, **guards):
223 '''Specify the python package, the source file, and any guards'''
224 super(SwigSource, self).__init__(source, **guards)
226 modname,ext = self.extname
229 self.module = modname
230 cc_file = joinpath(self.dirname, modname + '_wrap.cc')
231 py_file = joinpath(self.dirname, modname + '.py')
233 self.cc_source = Source(cc_file, swig=True, parent=self)
234 self.py_source = PySource(package, py_file, parent=self)
236 class ProtoBuf(SourceFile):
237 '''Add a Protocol Buffer to build'''
239 def __init__(self, source, **guards):
240 '''Specify the source file, and any guards'''
241 super(ProtoBuf, self).__init__(source, **guards)
243 # Get the file name and the extension
244 modname,ext = self.extname
245 assert ext == 'proto'
247 # Currently, we stick to generating the C++ headers, so we
248 # only need to track the source and header.
249 self.cc_file = File(joinpath(self.dirname, modname + '.pb.cc'))
250 self.hh_file = File(joinpath(self.dirname, modname + '.pb.h'))
252 class UnitTest(object):
253 '''Create a UnitTest'''
256 def __init__(self, target, *sources, **kwargs):
257 '''Specify the target name and any sources. Sources that are
258 not SourceFiles are evalued with Source(). All files are
259 guarded with a guard of the same name as the UnitTest
264 if not isinstance(src, SourceFile):
265 src = Source(src, skip_lib=True)
266 src.guards[target] = True
271 self.main = kwargs.get('main', False)
272 UnitTest.all.append(self)
274 # Children should have access
282 ########################################################################
287 def DebugFlag(name, desc=None):
288 if name in debug_flags:
289 raise AttributeError, "Flag %s already specified" % name
290 debug_flags[name] = (name, (), desc)
292 def CompoundFlag(name, flags, desc=None):
293 if name in debug_flags:
294 raise AttributeError, "Flag %s already specified" % name
296 compound = tuple(flags)
297 debug_flags[name] = (name, compound, desc)
300 Export('CompoundFlag')
302 ########################################################################
304 # Set some compiler variables
307 # Include file paths are rooted in this directory. SCons will
308 # automatically expand '.' to refer to both the source directory and
309 # the corresponding build directory to pick up generated include
311 env.Append(CPPPATH=Dir('.'))
313 for extra_dir in extras_dir_list:
314 env.Append(CPPPATH=Dir(extra_dir))
316 # Workaround for bug in SCons version > 0.97d20071212
317 # Scons bug id: 2006 gem5 Bug id: 308
318 for root, dirs, files in os.walk(base_dir, topdown=True):
319 Dir(root[len(base_dir) + 1:])
321 ########################################################################
323 # Walk the tree and execute all SConscripts in subdirectories
326 here = Dir('.').srcnode().abspath
327 for root, dirs, files in os.walk(base_dir, topdown=True):
329 # we don't want to recurse back into this SConscript
332 if 'SConscript' in files:
333 build_dir = joinpath(env['BUILDDIR'], root[len(base_dir) + 1:])
334 SConscript(joinpath(root, 'SConscript'), variant_dir=build_dir)
336 for extra_dir in extras_dir_list:
337 prefix_len = len(dirname(extra_dir)) + 1
338 for root, dirs, files in os.walk(extra_dir, topdown=True):
339 # if build lives in the extras directory, don't walk down it
343 if 'SConscript' in files:
344 build_dir = joinpath(env['BUILDDIR'], root[prefix_len:])
345 SConscript(joinpath(root, 'SConscript'), variant_dir=build_dir)
347 for opt in export_vars:
350 def makeTheISA(source, target, env):
351 isas = [ src.get_contents() for src in source ]
352 target_isa = env['TARGET_ISA']
354 return isa.upper() + '_ISA'
357 return isa[0].upper() + isa[1:].lower() + 'ISA'
360 code = code_formatter()
362 #ifndef __CONFIG_THE_ISA_HH__
363 #define __CONFIG_THE_ISA_HH__
367 for i,isa in enumerate(isas):
368 code('#define $0 $1', define(isa), i + 1)
372 #define THE_ISA ${{define(target_isa)}}
373 #define TheISA ${{namespace(target_isa)}}
374 #define THE_ISA_STR "${{target_isa}}"
376 #endif // __CONFIG_THE_ISA_HH__''')
378 code.write(str(target[0]))
380 env.Command('config/the_isa.hh', map(Value, all_isa_list),
381 MakeAction(makeTheISA, Transform("CFG ISA", 0)))
383 ########################################################################
385 # Prevent any SimObjects from being added after this point, they
386 # should all have been added in the SConscripts above
388 SimObject.fixed = True
390 class DictImporter(object):
391 '''This importer takes a dictionary of arbitrary module names that
392 map to arbitrary filenames.'''
393 def __init__(self, modules):
394 self.modules = modules
395 self.installed = set()
402 for module in self.installed:
403 del sys.modules[module]
404 self.installed = set()
406 def find_module(self, fullname, path):
407 if fullname == 'm5.defines':
410 if fullname == 'm5.objects':
413 if fullname.startswith('m5.internal'):
416 source = self.modules.get(fullname, None)
417 if source is not None and fullname.startswith('m5.objects'):
422 def load_module(self, fullname):
423 mod = imp.new_module(fullname)
424 sys.modules[fullname] = mod
425 self.installed.add(fullname)
427 mod.__loader__ = self
428 if fullname == 'm5.objects':
429 mod.__path__ = fullname.split('.')
432 if fullname == 'm5.defines':
433 mod.__dict__['buildEnv'] = m5.util.SmartDict(build_env)
436 source = self.modules[fullname]
437 if source.modname == '__init__':
438 mod.__path__ = source.modpath
439 mod.__file__ = source.abspath
441 exec file(source.abspath, 'r') in mod.__dict__
447 from m5.util import code_formatter
452 # install the python importer so we can grab stuff from the source
453 # tree itself. We can't have SimObjects added after this point or
454 # else we won't know about them for the rest of the stuff.
455 importer = DictImporter(PySource.modules)
456 sys.meta_path[0:0] = [ importer ]
458 # import all sim objects so we can populate the all_objects list
459 # make sure that we're working with a list, then let's sort it
460 for modname in SimObject.modnames:
461 exec('from m5.objects import %s' % modname)
463 # we need to unload all of the currently imported modules so that they
464 # will be re-imported the next time the sconscript is run
466 sys.meta_path.remove(importer)
468 sim_objects = m5.SimObject.allClasses
469 all_enums = m5.params.allEnums
471 if m5.SimObject.noCxxHeader:
472 print >> sys.stderr, \
473 "warning: At least one SimObject lacks a header specification. " \
474 "This can cause unexpected results in the generated SWIG " \
477 # Find param types that need to be explicitly wrapped with swig.
478 # These will be recognized because the ParamDesc will have a
479 # swig_decl() method. Most param types are based on types that don't
480 # need this, either because they're based on native types (like Int)
481 # or because they're SimObjects (which get swigged independently).
482 # For now the only things handled here are VectorParam types.
484 for name,obj in sorted(sim_objects.iteritems()):
485 for param in obj._params.local.values():
486 # load the ptype attribute now because it depends on the
487 # current version of SimObject.allClasses, but when scons
488 # actually uses the value, all versions of
489 # SimObject.allClasses will have been loaded
492 if not hasattr(param, 'swig_decl'):
494 pname = param.ptype_str
495 if pname not in params_to_swig:
496 params_to_swig[pname] = param
498 ########################################################################
500 # calculate extra dependencies
502 module_depends = ["m5", "m5.SimObject", "m5.params"]
503 depends = [ PySource.modules[dep].snode for dep in module_depends ]
505 ########################################################################
507 # Commands for the basic automatically generated python files
510 # Generate Python file containing a dict specifying the current
512 def makeDefinesPyFile(target, source, env):
513 build_env = source[0].get_contents()
515 code = code_formatter()
520 buildEnv = m5.util.SmartDict($build_env)
522 compileDate = m5.internal.core.compileDate
524 for key,val in m5.internal.core.__dict__.iteritems():
525 if key.startswith('flag_'):
530 code.write(target[0].abspath)
532 defines_info = Value(build_env)
533 # Generate a file with all of the compile options in it
534 env.Command('python/m5/defines.py', defines_info,
535 MakeAction(makeDefinesPyFile, Transform("DEFINES", 0)))
536 PySource('m5', 'python/m5/defines.py')
538 # Generate python file containing info about the M5 source code
539 def makeInfoPyFile(target, source, env):
540 code = code_formatter()
542 data = ''.join(file(src.srcnode().abspath, 'r').xreadlines())
543 code('$src = ${{repr(data)}}')
544 code.write(str(target[0]))
546 # Generate a file that wraps the basic top level files
547 env.Command('python/m5/info.py',
548 [ '#/COPYING', '#/LICENSE', '#/README', ],
549 MakeAction(makeInfoPyFile, Transform("INFO")))
550 PySource('m5', 'python/m5/info.py')
552 ########################################################################
554 # Create all of the SimObject param headers and enum headers
557 def createSimObjectParamStruct(target, source, env):
558 assert len(target) == 1 and len(source) == 1
560 name = str(source[0].get_contents())
561 obj = sim_objects[name]
563 code = code_formatter()
564 obj.cxx_param_decl(code)
565 code.write(target[0].abspath)
567 def createParamSwigWrapper(target, source, env):
568 assert len(target) == 1 and len(source) == 1
570 name = str(source[0].get_contents())
571 param = params_to_swig[name]
573 code = code_formatter()
574 param.swig_decl(code)
575 code.write(target[0].abspath)
577 def createEnumStrings(target, source, env):
578 assert len(target) == 1 and len(source) == 1
580 name = str(source[0].get_contents())
581 obj = all_enums[name]
583 code = code_formatter()
585 code.write(target[0].abspath)
587 def createEnumDecls(target, source, env):
588 assert len(target) == 1 and len(source) == 1
590 name = str(source[0].get_contents())
591 obj = all_enums[name]
593 code = code_formatter()
595 code.write(target[0].abspath)
597 def createEnumSwigWrapper(target, source, env):
598 assert len(target) == 1 and len(source) == 1
600 name = str(source[0].get_contents())
601 obj = all_enums[name]
603 code = code_formatter()
605 code.write(target[0].abspath)
607 def createSimObjectSwigWrapper(target, source, env):
608 name = source[0].get_contents()
609 obj = sim_objects[name]
611 code = code_formatter()
613 code.write(target[0].abspath)
615 # Generate all of the SimObject param C++ struct header files
617 for name,simobj in sorted(sim_objects.iteritems()):
618 py_source = PySource.modules[simobj.__module__]
619 extra_deps = [ py_source.tnode ]
621 hh_file = File('params/%s.hh' % name)
622 params_hh_files.append(hh_file)
623 env.Command(hh_file, Value(name),
624 MakeAction(createSimObjectParamStruct, Transform("SO PARAM")))
625 env.Depends(hh_file, depends + extra_deps)
627 # Generate any needed param SWIG wrapper files
629 for name,param in params_to_swig.iteritems():
630 i_file = File('python/m5/internal/%s.i' % (param.swig_module_name()))
631 params_i_files.append(i_file)
632 env.Command(i_file, Value(name),
633 MakeAction(createParamSwigWrapper, Transform("SW PARAM")))
634 env.Depends(i_file, depends)
635 SwigSource('m5.internal', i_file)
637 # Generate all enum header files
638 for name,enum in sorted(all_enums.iteritems()):
639 py_source = PySource.modules[enum.__module__]
640 extra_deps = [ py_source.tnode ]
642 cc_file = File('enums/%s.cc' % name)
643 env.Command(cc_file, Value(name),
644 MakeAction(createEnumStrings, Transform("ENUM STR")))
645 env.Depends(cc_file, depends + extra_deps)
648 hh_file = File('enums/%s.hh' % name)
649 env.Command(hh_file, Value(name),
650 MakeAction(createEnumDecls, Transform("ENUMDECL")))
651 env.Depends(hh_file, depends + extra_deps)
653 i_file = File('python/m5/internal/enum_%s.i' % name)
654 env.Command(i_file, Value(name),
655 MakeAction(createEnumSwigWrapper, Transform("ENUMSWIG")))
656 env.Depends(i_file, depends + extra_deps)
657 SwigSource('m5.internal', i_file)
659 # Generate SimObject SWIG wrapper files
660 for name,simobj in sim_objects.iteritems():
661 py_source = PySource.modules[simobj.__module__]
662 extra_deps = [ py_source.tnode ]
664 i_file = File('python/m5/internal/param_%s.i' % name)
665 env.Command(i_file, Value(name),
666 MakeAction(createSimObjectSwigWrapper, Transform("SO SWIG")))
667 env.Depends(i_file, depends + extra_deps)
668 SwigSource('m5.internal', i_file)
670 # Generate the main swig init file
671 def makeEmbeddedSwigInit(target, source, env):
672 code = code_formatter()
673 module = source[0].get_contents()
675 #include "sim/init.hh"
678 void init_${module}();
681 EmbeddedSwig embed_swig_${module}(init_${module});
683 code.write(str(target[0]))
685 # Build all swig modules
686 for swig in SwigSource.all:
687 env.Command([swig.cc_source.tnode, swig.py_source.tnode], swig.tnode,
688 MakeAction('$SWIG $SWIGFLAGS -outdir ${TARGETS[1].dir} '
689 '-o ${TARGETS[0]} $SOURCES', Transform("SWIG")))
690 cc_file = str(swig.tnode)
691 init_file = '%s/%s_init.cc' % (dirname(cc_file), basename(cc_file))
692 env.Command(init_file, Value(swig.module),
693 MakeAction(makeEmbeddedSwigInit, Transform("EMBED SW")))
694 Source(init_file, **swig.guards)
696 # Build all protocol buffers if we have got protoc and protobuf available
697 if env['HAVE_PROTOBUF']:
698 for proto in ProtoBuf.all:
699 # Use both the source and header as the target, and the .proto
700 # file as the source. When executing the protoc compiler, also
701 # specify the proto_path to avoid having the generated files
703 env.Command([proto.cc_file, proto.hh_file], proto.tnode,
704 MakeAction('$PROTOC --cpp_out ${TARGET.dir} '
705 '--proto_path ${SOURCE.dir} $SOURCE',
706 Transform("PROTOC")))
708 # Add the C++ source file
709 Source(proto.cc_file, **proto.guards)
711 print 'Got protobuf to build, but lacks support!'
717 def makeDebugFlagCC(target, source, env):
718 assert(len(target) == 1 and len(source) == 1)
720 val = eval(source[0].get_contents())
721 name, compound, desc = val
722 compound = list(sorted(compound))
724 code = code_formatter()
729 * DO NOT EDIT THIS FILE! Automatically generated
732 #include "base/debug.hh"
735 for flag in compound:
736 code('#include "debug/$flag.hh"')
738 code('namespace Debug {')
742 code('SimpleFlag $name("$name", "$desc");')
744 code('CompoundFlag $name("$name", "$desc",')
746 last = len(compound) - 1
747 for i,flag in enumerate(compound):
755 code('} // namespace Debug')
757 code.write(str(target[0]))
759 def makeDebugFlagHH(target, source, env):
760 assert(len(target) == 1 and len(source) == 1)
762 val = eval(source[0].get_contents())
763 name, compound, desc = val
765 code = code_formatter()
767 # file header boilerplate
770 * DO NOT EDIT THIS FILE!
772 * Automatically generated by SCons
775 #ifndef __DEBUG_${name}_HH__
776 #define __DEBUG_${name}_HH__
782 code('class CompoundFlag;')
783 code('class SimpleFlag;')
786 code('extern CompoundFlag $name;')
787 for flag in compound:
788 code('extern SimpleFlag $flag;')
790 code('extern SimpleFlag $name;')
795 #endif // __DEBUG_${name}_HH__
798 code.write(str(target[0]))
800 for name,flag in sorted(debug_flags.iteritems()):
801 n, compound, desc = flag
804 env.Command('debug/%s.hh' % name, Value(flag),
805 MakeAction(makeDebugFlagHH, Transform("TRACING", 0)))
806 env.Command('debug/%s.cc' % name, Value(flag),
807 MakeAction(makeDebugFlagCC, Transform("TRACING", 0)))
808 Source('debug/%s.cc' % name)
810 # Embed python files. All .py files that have been indicated by a
811 # PySource() call in a SConscript need to be embedded into the M5
812 # library. To do that, we compile the file to byte code, marshal the
813 # byte code, compress it, and then generate a c++ file that
814 # inserts the result into an array.
815 def embedPyFile(target, source, env):
819 return '"%s"' % string
821 '''Action function to compile a .py into a code object, marshal
822 it, compress it, and stick it into an asm file so the code appears
823 as just bytes with a label in the data section'''
825 src = file(str(source[0]), 'r').read()
827 pysource = PySource.tnodes[source[0]]
828 compiled = compile(src, pysource.abspath, 'exec')
829 marshalled = marshal.dumps(compiled)
830 compressed = zlib.compress(marshalled)
832 sym = pysource.symname
834 code = code_formatter()
836 #include "sim/init.hh"
840 const uint8_t data_${sym}[] = {
844 for i in xrange(0, len(data), step):
845 x = array.array('B', data[i:i+step])
846 code(''.join('%d,' % d for d in x))
851 EmbeddedPython embedded_${sym}(
852 ${{c_str(pysource.arcname)}},
853 ${{c_str(pysource.abspath)}},
854 ${{c_str(pysource.modpath)}},
857 ${{len(marshalled)}});
859 } // anonymous namespace
861 code.write(str(target[0]))
863 for source in PySource.all:
864 env.Command(source.cpp, source.tnode,
865 MakeAction(embedPyFile, Transform("EMBED PY")))
868 ########################################################################
870 # Define binaries. Each different build type (debug, opt, etc.) gets
871 # a slightly different build environment.
874 # List of constructed environments to pass back to SConstruct
877 date_source = Source('base/date.cc', skip_lib=True)
879 # Function to create a new build environment as clone of current
880 # environment 'env' with modified object suffix and optional stripped
881 # binary. Additional keyword arguments are appended to corresponding
882 # build environment vars.
883 def makeEnv(label, objsfx, strip = False, **kwargs):
884 # SCons doesn't know to append a library suffix when there is a '.' in the
885 # name. Use '_' instead.
886 libname = 'gem5_' + label
887 exename = 'gem5.' + label
888 secondary_exename = 'm5.' + label
890 new_env = env.Clone(OBJSUFFIX=objsfx, SHOBJSUFFIX=objsfx + 's')
891 new_env.Label = label
892 new_env.Append(**kwargs)
894 swig_env = new_env.Clone()
895 swig_env.Append(CCFLAGS='-Werror')
897 swig_env.Append(CCFLAGS=['-Wno-uninitialized', '-Wno-sign-compare',
898 '-Wno-parentheses', '-Wno-unused-label',
899 '-Wno-unused-value'])
900 if compareVersions(env['GCC_VERSION'], '4.6') >= 0:
901 swig_env.Append(CCFLAGS='-Wno-unused-but-set-variable')
903 swig_env.Append(CCFLAGS=['-Wno-unused-label', '-Wno-unused-value'])
905 werror_env = new_env.Clone()
906 werror_env.Append(CCFLAGS='-Werror')
908 def make_obj(source, static, extra_deps = None):
909 '''This function adds the specified source to the correct
910 build environment, and returns the corresponding SCons Object
921 obj = env.StaticObject(source.tnode)
923 obj = env.SharedObject(source.tnode)
926 env.Depends(obj, extra_deps)
931 [ make_obj(s, True) for s in Source.get(main=False, skip_lib=False) ]
933 [ make_obj(s, False) for s in Source.get(main=False, skip_lib=False) ]
935 static_date = make_obj(date_source, static=True, extra_deps=static_objs)
936 static_objs.append(static_date)
938 shared_date = make_obj(date_source, static=False, extra_deps=shared_objs)
939 shared_objs.append(shared_date)
941 # First make a library of everything but main() so other programs can
943 static_lib = new_env.StaticLibrary(libname, static_objs)
944 shared_lib = new_env.SharedLibrary(libname, shared_objs)
946 # Now link a stub with main() and the static library.
947 main_objs = [ make_obj(s, True) for s in Source.get(main=True) ]
949 for test in UnitTest.all:
950 flags = { test.target : True }
951 test_sources = Source.get(**flags)
952 test_objs = [ make_obj(s, static=True) for s in test_sources ]
954 test_objs += main_objs
955 testname = "unittest/%s.%s" % (test.target, label)
956 new_env.Program(testname, test_objs + static_objs)
960 progname += '.unstripped'
962 targets = new_env.Program(progname, main_objs + static_objs)
965 if sys.platform == 'sunos5':
966 cmd = 'cp $SOURCE $TARGET; strip $TARGET'
968 cmd = 'strip $SOURCE -o $TARGET'
969 targets = new_env.Command(exename, progname,
970 MakeAction(cmd, Transform("STRIP")))
972 new_env.Command(secondary_exename, exename,
973 MakeAction('ln $SOURCE $TARGET', Transform("HARDLINK")))
975 new_env.M5Binary = targets[0]
976 envList.append(new_env)
978 # Start out with the compiler flags common to all compilers,
979 # i.e. they all use -g for opt and -g -pg for prof
980 ccflags = {'debug' : [], 'opt' : ['-g'], 'fast' : [], 'prof' : ['-g', '-pg'],
983 # Start out with the linker flags common to all linkers, i.e. -pg for
984 # prof, and -lprofiler for perf. The -lprofile flag is surrounded by
985 # no-as-needed and as-needed as the binutils linker is too clever and
986 # simply doesn't link to the library otherwise.
987 ldflags = {'debug' : [], 'opt' : [], 'fast' : [], 'prof' : ['-pg'],
988 'perf' : ['-Wl,--no-as-needed', '-lprofiler', '-Wl,--as-needed']}
990 # For Link Time Optimization, the optimisation flags used to compile
991 # individual files are decoupled from those used at link time
992 # (i.e. you can compile with -O3 and perform LTO with -O0), so we need
993 # to also update the linker flags based on the target.
995 if sys.platform == 'sunos5':
996 ccflags['debug'] += ['-gstabs+']
998 ccflags['debug'] += ['-ggdb3']
999 ldflags['debug'] += ['-O0']
1000 # opt, fast, prof and perf all share the same cc flags, also add
1001 # the optimization to the ldflags as LTO defers the optimization
1003 for target in ['opt', 'fast', 'prof', 'perf']:
1004 ccflags[target] += ['-O3']
1005 ldflags[target] += ['-O3']
1007 ccflags['fast'] += env['LTO_CCFLAGS']
1008 ldflags['fast'] += env['LTO_LDFLAGS']
1011 ccflags['debug'] += ['-g0']
1012 ccflags['opt'] += ['-O']
1013 for target in ['fast', 'prof', 'perf']:
1014 ccflags[target] += ['-fast']
1016 ccflags['debug'] += ['-g', '-O0']
1017 ccflags['opt'] += ['-O']
1018 for target in ['fast', 'prof', 'perf']:
1019 ccflags[target] += ['-fast']
1021 ccflags['debug'] += ['-g', '-O0']
1022 # opt, fast, prof and perf all share the same cc flags
1023 for target in ['opt', 'fast', 'prof', 'perf']:
1024 ccflags[target] += ['-O3']
1026 print 'Unknown compiler, please fix compiler options'
1030 # To speed things up, we only instantiate the build environments we
1031 # need. We try to identify the needed environment for each target; if
1032 # we can't, we fall back on instantiating all the environments just to
1034 target_types = ['debug', 'opt', 'fast', 'prof', 'perf']
1035 obj2target = {'do': 'debug', 'o': 'opt', 'fo': 'fast', 'po': 'prof',
1038 def identifyTarget(t):
1039 ext = t.split('.')[-1]
1040 if ext in target_types:
1042 if obj2target.has_key(ext):
1043 return obj2target[ext]
1044 match = re.search(r'/tests/([^/]+)/', t)
1045 if match and match.group(1) in target_types:
1046 return match.group(1)
1049 needed_envs = [identifyTarget(target) for target in BUILD_TARGETS]
1050 if 'all' in needed_envs:
1051 needed_envs += target_types
1054 if 'debug' in needed_envs:
1055 makeEnv('debug', '.do',
1056 CCFLAGS = Split(ccflags['debug']),
1057 CPPDEFINES = ['DEBUG', 'TRACING_ON=1'],
1058 LINKFLAGS = Split(ldflags['debug']))
1061 if 'opt' in needed_envs:
1062 makeEnv('opt', '.o',
1063 CCFLAGS = Split(ccflags['opt']),
1064 CPPDEFINES = ['TRACING_ON=1'],
1065 LINKFLAGS = Split(ldflags['opt']))
1068 if 'fast' in needed_envs:
1069 makeEnv('fast', '.fo', strip = True,
1070 CCFLAGS = Split(ccflags['fast']),
1071 CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
1072 LINKFLAGS = Split(ldflags['fast']))
1074 # Profiled binary using gprof
1075 if 'prof' in needed_envs:
1076 makeEnv('prof', '.po',
1077 CCFLAGS = Split(ccflags['prof']),
1078 CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
1079 LINKFLAGS = Split(ldflags['prof']))
1081 # Profiled binary using google-pprof
1082 if 'perf' in needed_envs:
1083 makeEnv('perf', '.gpo',
1084 CCFLAGS = Split(ccflags['perf']),
1085 CPPDEFINES = ['NDEBUG', 'TRACING_ON=0'],
1086 LINKFLAGS = Split(ldflags['perf']))