SConscript (7673:b28bd1fa9a35) SConscript (7674:8e3734851770)
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

--- 37 unchanged lines hidden (view full) ---

46
47Import('*')
48
49# Children need to see the environment
50Export('env')
51
52build_env = [(opt, env[opt]) for opt in export_vars]
53
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

--- 37 unchanged lines hidden (view full) ---

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
55
54########################################################################
55# Code for adding source files of various types
56#
57class SourceMeta(type):
58 def __init__(cls, name, bases, dict):
59 super(SourceMeta, cls).__init__(name, bases, dict)
60 cls.all = []
61

--- 75 unchanged lines hidden (view full) ---

137 abspath = self.tnode.abspath
138
139 self.package = package
140 self.modname = modname
141 self.modpath = modpath
142 self.arcname = joinpath(*arcpath)
143 self.abspath = abspath
144 self.compiled = File(self.filename + 'c')
56########################################################################
57# Code for adding source files of various types
58#
59class SourceMeta(type):
60 def __init__(cls, name, bases, dict):
61 super(SourceMeta, cls).__init__(name, bases, dict)
62 cls.all = []
63

--- 75 unchanged lines hidden (view full) ---

139 abspath = self.tnode.abspath
140
141 self.package = package
142 self.modname = modname
143 self.modpath = modpath
144 self.arcname = joinpath(*arcpath)
145 self.abspath = abspath
146 self.compiled = File(self.filename + 'c')
145 self.assembly = File(self.filename + '.s')
146 self.symname = "PyEMB_" + PySource.invalid_sym_char.sub('_', modpath)
147 self.cpp = File(self.filename + '.cc')
148 self.symname = PySource.invalid_sym_char.sub('_', modpath)
147
148 PySource.modules[modpath] = self
149 PySource.tnodes[self.tnode] = self
150 PySource.symnames[self.symname] = self
151
152class SimObject(PySource):
153 '''Add a SimObject python file as a python source object and add
154 it to a list of sim object modules'''

--- 286 unchanged lines hidden (view full) ---

441 code.write(str(target[0]))
442
443# Generate a file that wraps the basic top level files
444env.Command('python/m5/info.py',
445 [ '#/AUTHORS', '#/LICENSE', '#/README', '#/RELEASE_NOTES' ],
446 makeInfoPyFile)
447PySource('m5', 'python/m5/info.py')
448
149
150 PySource.modules[modpath] = self
151 PySource.tnodes[self.tnode] = self
152 PySource.symnames[self.symname] = self
153
154class SimObject(PySource):
155 '''Add a SimObject python file as a python source object and add
156 it to a list of sim object modules'''

--- 286 unchanged lines hidden (view full) ---

443 code.write(str(target[0]))
444
445# Generate a file that wraps the basic top level files
446env.Command('python/m5/info.py',
447 [ '#/AUTHORS', '#/LICENSE', '#/README', '#/RELEASE_NOTES' ],
448 makeInfoPyFile)
449PySource('m5', 'python/m5/info.py')
450
449# Generate the __init__.py file for m5.objects
450def makeObjectsInitFile(target, source, env):
451 code = code_formatter()
452 code('''\
453from params import *
454from m5.SimObject import *
455''')
456
457 for module in source:
458 code('from $0 import *', module.get_contents())
459 code.write(str(target[0]))
460
461# Generate an __init__.py file for the objects package
462env.Command('python/m5/objects/__init__.py',
463 map(Value, SimObject.modnames),
464 makeObjectsInitFile)
465PySource('m5.objects', 'python/m5/objects/__init__.py')
466
467########################################################################
468#
469# Create all of the SimObject param headers and enum headers
470#
471
472def createSimObjectParam(target, source, env):
473 assert len(target) == 1 and len(source) == 1
474

--- 152 unchanged lines hidden (view full) ---

627 code.write(target[0].abspath)
628
629params_file = File('params/params.i')
630names = sorted(sim_objects.keys())
631env.Command(params_file, map(Value, names), buildParams)
632env.Depends(params_file, params_hh_files + params_i_files + depends)
633SwigSource('m5.objects', params_file)
634
451########################################################################
452#
453# Create all of the SimObject param headers and enum headers
454#
455
456def createSimObjectParam(target, source, env):
457 assert len(target) == 1 and len(source) == 1
458

--- 152 unchanged lines hidden (view full) ---

611 code.write(target[0].abspath)
612
613params_file = File('params/params.i')
614names = sorted(sim_objects.keys())
615env.Command(params_file, map(Value, names), buildParams)
616env.Depends(params_file, params_hh_files + params_i_files + depends)
617SwigSource('m5.objects', params_file)
618
619# Generate the main swig init file
620def makeEmbeddedSwigInit(target, source, env):
621 code = code_formatter()
622 module = source[0].get_contents()
623 code('''\
624#include "sim/init.hh"
625
626extern "C" {
627 void init_${module}();
628}
629
630EmbeddedSwig embed_swig_${module}(init_${module});
631''')
632 code.write(str(target[0]))
633
635# Build all swig modules
636for swig in SwigSource.all:
637 env.Command([swig.cc_source.tnode, swig.py_source.tnode], swig.tnode,
638 '$SWIG $SWIGFLAGS -outdir ${TARGETS[1].dir} '
639 '-o ${TARGETS[0]} $SOURCES')
634# Build all swig modules
635for swig in SwigSource.all:
636 env.Command([swig.cc_source.tnode, swig.py_source.tnode], swig.tnode,
637 '$SWIG $SWIGFLAGS -outdir ${TARGETS[1].dir} '
638 '-o ${TARGETS[0]} $SOURCES')
639 init_file = 'python/swig/init_%s.cc' % swig.module
640 env.Command(init_file, Value(swig.module), makeEmbeddedSwigInit)
641 Source(init_file)
640 env.Depends(swig.py_source.tnode, swig.tnode)
641 env.Depends(swig.cc_source.tnode, swig.tnode)
642
642 env.Depends(swig.py_source.tnode, swig.tnode)
643 env.Depends(swig.cc_source.tnode, swig.tnode)
644
643# Generate the main swig init file
644def makeSwigInit(target, source, env):
645 code = code_formatter()
646
647 code('extern "C" {')
648 code.indent()
649 for module in source:
650 code('void init_$0();', module.get_contents())
651 code.dedent()
652 code('}')
653
654 code('void initSwig() {')
655 code.indent()
656 for module in source:
657 code('init_$0();', module.get_contents())
658 code.dedent()
659 code('}')
660
661 code.write(str(target[0]))
662
663env.Command('python/swig/init.cc',
664 map(Value, sorted(s.module for s in SwigSource.all)),
665 makeSwigInit)
666Source('python/swig/init.cc')
667
668def getFlags(source_flags):
669 flagsMap = {}
670 flagsList = []
671 for s in source_flags:
672 val = eval(s.get_contents())
673 name, compound, desc = val
674 flagsList.append(val)
675 flagsMap[name] = bool(compound)

--- 211 unchanged lines hidden (view full) ---

887flags = map(Value, trace_flags.values())
888env.Command('base/traceflags.py', flags, traceFlagsPy)
889PySource('m5', 'base/traceflags.py')
890
891env.Command('base/traceflags.hh', flags, traceFlagsHH)
892env.Command('base/traceflags.cc', flags, traceFlagsCC)
893Source('base/traceflags.cc')
894
645def getFlags(source_flags):
646 flagsMap = {}
647 flagsList = []
648 for s in source_flags:
649 val = eval(s.get_contents())
650 name, compound, desc = val
651 flagsList.append(val)
652 flagsMap[name] = bool(compound)

--- 211 unchanged lines hidden (view full) ---

864flags = map(Value, trace_flags.values())
865env.Command('base/traceflags.py', flags, traceFlagsPy)
866PySource('m5', 'base/traceflags.py')
867
868env.Command('base/traceflags.hh', flags, traceFlagsHH)
869env.Command('base/traceflags.cc', flags, traceFlagsCC)
870Source('base/traceflags.cc')
871
895# embed python files. All .py files that have been indicated by a
872# Embed python files. All .py files that have been indicated by a
896# PySource() call in a SConscript need to be embedded into the M5
897# library. To do that, we compile the file to byte code, marshal the
873# PySource() call in a SConscript need to be embedded into the M5
874# library. To do that, we compile the file to byte code, marshal the
898# byte code, compress it, and then generate an assembly file that
899# inserts the result into the data section with symbols indicating the
900# beginning, and end (and with the size at the end)
901def objectifyPyFile(target, source, env):
875# byte code, compress it, and then generate a c++ file that
876# inserts the result into an array.
877def embedPyFile(target, source, env):
878 def c_str(string):
879 if string is None:
880 return "0"
881 return '"%s"' % string
882
902 '''Action function to compile a .py into a code object, marshal
903 it, compress it, and stick it into an asm file so the code appears
904 as just bytes with a label in the data section'''
905
906 src = file(str(source[0]), 'r').read()
907
908 pysource = PySource.tnodes[source[0]]
909 compiled = compile(src, pysource.abspath, 'exec')
910 marshalled = marshal.dumps(compiled)
911 compressed = zlib.compress(marshalled)
912 data = compressed
883 '''Action function to compile a .py into a code object, marshal
884 it, compress it, and stick it into an asm file so the code appears
885 as just bytes with a label in the data section'''
886
887 src = file(str(source[0]), 'r').read()
888
889 pysource = PySource.tnodes[source[0]]
890 compiled = compile(src, pysource.abspath, 'exec')
891 marshalled = marshal.dumps(compiled)
892 compressed = zlib.compress(marshalled)
893 data = compressed
894 sym = pysource.symname
913
895
914 # Some C/C++ compilers prepend an underscore to global symbol
915 # names, so if they're going to do that, we need to prepend that
916 # leading underscore to globals in the assembly file.
917 if env['LEADING_UNDERSCORE']:
918 sym = '_' + pysource.symname
919 else:
920 sym = pysource.symname
921
922 step = 16
923 code = code_formatter()
924 code('''\
896 code = code_formatter()
897 code('''\
925.data
926.globl ${sym}_beg
927.globl ${sym}_end
928${sym}_beg:''')
898#include "sim/init.hh"
929
899
900namespace {
901
902const char data_${sym}[] = {
903''')
904 code.indent()
905 step = 16
930 for i in xrange(0, len(data), step):
931 x = array.array('B', data[i:i+step])
906 for i in xrange(0, len(data), step):
907 x = array.array('B', data[i:i+step])
932 bytes = ','.join([str(d) for d in x])
933 code('.byte $bytes')
934 code('${sym}_end:')
935 code('.long $0', len(marshalled))
908 code(''.join('%d,' % d for d in x))
909 code.dedent()
910
911 code('''};
936
912
937 code.write(str(target[0]))
913EmbeddedPython embedded_${sym}(
914 ${{c_str(pysource.arcname)}},
915 ${{c_str(pysource.abspath)}},
916 ${{c_str(pysource.modpath)}},
917 data_${sym},
918 ${{len(data)}},
919 ${{len(marshalled)}});
938
920
939for source in PySource.all:
940 env.Command(source.assembly, source.tnode, objectifyPyFile)
941 Source(source.assembly)
942
943# Generate init_python.cc which creates a bunch of EmbeddedPyModule
944# structs that describe the embedded python code. One such struct
945# contains information about the importer that python uses to get at
946# the embedded files, and then there's a list of all of the rest that
947# the importer uses to load the rest on demand.
948def pythonInit(target, source, env):
949 code = code_formatter()
950
951 def dump_mod(sym, endchar=','):
952 def c_str(string):
953 if string is None:
954 return "0"
955 return '"%s"' % string
956
957 pysource = PySource.symnames[sym]
958 arcname = c_str(pysource.arcname)
959 abspath = c_str(pysource.abspath)
960 modpath = c_str(pysource.modpath)
961 code.indent()
962 code('''\
963{ $arcname,
964 $abspath,
965 $modpath,
966 ${sym}_beg, ${sym}_end,
967 ${sym}_end - ${sym}_beg,
968 *(int *)${sym}_end }$endchar
921/* namespace */ }
969''')
922''')
970 code.dedent()
971
972 code('#include "sim/init.hh"')
973 for sym in source:
974 sym = sym.get_contents()
975 code('extern const char ${sym}_beg[], ${sym}_end[];')
976
977 code('const EmbeddedPyModule embeddedPyImporter = ')
978 dump_mod("PyEMB_importer", endchar=';')
979 code()
980
981 code('const EmbeddedPyModule embeddedPyModules[] = {')
982 for i,sym in enumerate(source):
983 sym = sym.get_contents()
984 if sym == "PyEMB_importer":
985 # Skip the importer since we've already exported it
986 continue
987 dump_mod(sym)
988 code(' { 0, 0, 0, 0, 0, 0, 0 }')
989 code('};')
990
991 code.write(str(target[0]))
992
923 code.write(str(target[0]))
924
993env.Command('sim/init_python.cc',
994 map(Value, (s.symname for s in PySource.all)),
995 pythonInit)
996Source('sim/init_python.cc')
925for source in PySource.all:
926 env.Command(source.cpp, source.tnode, embedPyFile)
927 Source(source.cpp)
997
998########################################################################
999#
1000# Define binaries. Each different build type (debug, opt, etc.) gets
1001# a slightly different build environment.
1002#
1003
1004# List of constructed environments to pass back to SConstruct

--- 131 unchanged lines hidden ---
928
929########################################################################
930#
931# Define binaries. Each different build type (debug, opt, etc.) gets
932# a slightly different build environment.
933#
934
935# List of constructed environments to pass back to SConstruct

--- 131 unchanged lines hidden ---