python,scons: Add a gem5py_m5 program which supports the m5 module.

Like gem5py which uses the same main source file, this program will run
arbitrary python scripts. Unlike the other program, it will include
support for the m5 module. That will make it capable of generating
SimObject param, enum, etc c++ files.

Change-Id: I15fd7545f6b1ea6559cbe27cef30c778867ebe70
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/49421
Reviewed-by: Jason Lowe-Power <power.jg@gmail.com>
Maintainer: Jason Lowe-Power <power.jg@gmail.com>
Tested-by: kokoro <noreply+kokoro@google.com>
diff --git a/src/SConscript b/src/SConscript
index b19c3c5..d566010 100644
--- a/src/SConscript
+++ b/src/SConscript
@@ -81,8 +81,11 @@
 # as gem5. This is in an unorthodox location to avoid building it for every
 # variant.
 gem5py_env = gem5py_env.Clone()
-gem5py = gem5py_env.Program('gem5py', 'python/gem5py.cc')[0]
+gem5py = gem5py_env.File('gem5py')
+gem5py_m5 = gem5py_env.File('gem5py_m5')
 gem5py_env['GEM5PY'] = gem5py
+gem5py_env['GEM5PY_M5'] = gem5py_m5
+gem5py_env['OBJSUFFIX'] = '.pyo'
 # Inject build_tools into PYTHONPATH for when we run gem5py.
 pythonpath = gem5py_env['ENV'].get('PYTHONPATH', '').split(':')
 pythonpath.append(build_tools.abspath)
@@ -134,7 +137,7 @@
                        '"${PYSOURCE_ABSPATH}"',
                        Transform("EMBED PY", max_sources=1)),
             **overrides)
-        Source(cpp, tags=self.tags, add_tags='python')
+        Source(cpp, tags=self.tags, add_tags=['python', 'm5_module'])
 
 class SimObject(PySource):
     '''Add a SimObject python file as a python source object and add
@@ -640,6 +643,15 @@
             INFOPY_PY=build_tools.File('infopy.py'))
 PySource('m5', 'python/m5/info.py')
 
+gem5py_m5_env = gem5py_env.Clone()
+gem5py_env.Append(CPPPATH=env['CPPPATH'])
+gem5py_env.Append(LIBS='z')
+gem5py_env.Program(gem5py, 'python/gem5py.cc')[0]
+m5_module_source = \
+        Source.all.with_all_tags(env, 'm5_module', 'gem5 lib')
+m5_module_static = list(map(lambda s: s.static(gem5py_env), m5_module_source))
+gem5py_env.Program(gem5py_m5, [ 'python/gem5py.cc' ] + m5_module_static)
+
 ########################################################################
 #
 # Create all of the SimObject param headers and enum headers
diff --git a/src/python/SConscript b/src/python/SConscript
index c9cd2d4..1939100 100644
--- a/src/python/SConscript
+++ b/src/python/SConscript
@@ -253,10 +253,10 @@
 PySource('m5.ext.pystats', 'm5/ext/pystats/jsonloader.py')
 PySource('m5.stats', 'm5/stats/gem5stats.py')
 
-Source('embedded.cc', add_tags='python')
-Source('importer.cc', add_tags='python')
+Source('embedded.cc', add_tags=['python', 'm5_module'])
+Source('importer.cc', add_tags=['python', 'm5_module'])
 cc, hh = env.Blob('m5ImporterCode', 'importer.py')
-Source(cc, add_tags='python')
+Source(cc, add_tags=['python', 'm5_module'])
 
 Source('pybind11/core.cc', add_tags='python')
 Source('pybind11/debug.cc', add_tags='python')