Bobby R. Bruce has submitted this change. ( https://gem5-review.googlesource.com/c/public/gem5/+/46880 )

Change subject: python,scons: Only generate pybind if using python
......................................................................

python,scons: Only generate pybind if using python

This reimplements the previously reverted change: Always generate default
create() methods.

The pybind code should only be generated when python is enabled. This
change passes whether python is enabled into the SimObject code creation
method. Then, the params code is optionally included.

Note: Due to some problems in GCC's linker (or something else...) we
need to have a single file with all of the generated code for the
SimObject.

Change-Id: I0f93b3d787d47f26db2de6c4447730f7df87a0dc
Issue-on: https://gem5.atlassian.net/browse/GEM5-1003
Signed-off-by: Jason Lowe-Power <ja...@lowepower.com>
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/46880
Maintainer: Bobby R. Bruce <bbr...@ucdavis.edu>
Tested-by: kokoro <noreply+kok...@google.com>
Reviewed-by: Bobby R. Bruce <bbr...@ucdavis.edu>
---
M src/SConscript
M src/python/m5/SimObject.py
2 files changed, 97 insertions(+), 76 deletions(-)

Approvals:
  Bobby R. Bruce: Looks good to me, approved; Looks good to me, approved
  kokoro: Regressions pass



diff --git a/src/SConscript b/src/SConscript
index 5fe0ab2..1ff6a07 100644
--- a/src/SConscript
+++ b/src/SConscript
@@ -962,12 +962,16 @@
     obj.cxx_decl(code)
     code.write(target[0].abspath)

-def createSimObjectPyBindWrapper(target, source, env):
+def createSimObjectWrappers(target, source, env):
     name = source[0].get_text_contents()
     obj = sim_objects[name]

     code = code_formatter()
-    obj.pybind_decl(code)
+    # We want to generate a single .cc file which contains most of the
+ # SimObject autogenerated code to reduce the number of files to compile and
+    # link. We need to pass in whether python is enabled so that the pybind
+    # wrappers are only generated when python is enabled
+    obj.params_create_decl(code, env['USE_PYTHON'])
     code.write(target[0].abspath)

 # Generate all of the SimObject param C++ struct header files
@@ -1061,17 +1065,16 @@
                 MakeAction(createEnumDecls, Transform("ENUMDECL")))
     env.Depends(hh_file, depends + extra_deps)

-# Generate SimObject Python bindings wrapper files
-if env['USE_PYTHON']:
-    for name,simobj in sorted(sim_objects.items()):
-        py_source = PySource.modules[simobj.__module__]
-        extra_deps = [ py_source.tnode ]
-        cc_file = File('python/_m5/param_%s.cc' % name)
-        env.Command(cc_file, Value(name),
-                    MakeAction(createSimObjectPyBindWrapper,
-                               Transform("SO PyBind")))
-        env.Depends(cc_file, depends + extra_deps)
-        Source(cc_file)
+# Generate SimObject Python bindings and create method wrapper files
+for name,simobj in sorted(sim_objects.items()):
+    py_source = PySource.modules[simobj.__module__]
+    extra_deps = [ py_source.tnode ]
+    cc_file = File('python/_m5/param_%s.cc' % name)
+    env.Command(cc_file, Value(name),
+                MakeAction(createSimObjectWrappers,
+                            Transform("SO PyB/C")))
+    env.Depends(cc_file, depends + extra_deps)
+    Source(cc_file)

 #
 # Handle debug flags
diff --git a/src/python/m5/SimObject.py b/src/python/m5/SimObject.py
index 71b39d0..e2f5d58 100644
--- a/src/python/m5/SimObject.py
+++ b/src/python/m5/SimObject.py
@@ -700,17 +700,21 @@
     def pybind_predecls(cls, code):
         code('#include "${{cls.cxx_header}}"')

-    def pybind_decl(cls, code):
+    def params_create_decl(cls, code, python_enabled):
         py_class_name = cls.pybind_class

         # The 'local' attribute restricts us to the params declared in
         # the object itself, not including inherited params (which
         # will also be inherited from the base class's param struct
         # here). Sort the params based on their key
- params = list(map(lambda k_v: k_v[1], sorted(cls._params.local.items())))
+        params = list(map(lambda k_v: k_v[1],
+                          sorted(cls._params.local.items())))
         ports = cls._ports.local

-        code('''#include "pybind11/pybind11.h"
+        # only include pybind if python is enabled in the build
+        if python_enabled:
+
+            code('''#include "pybind11/pybind11.h"
 #include "pybind11/stl.h"

 #include <type_traits>
@@ -724,79 +728,93 @@
 #include "${{cls.cxx_header}}"

 ''')
+        else:
+            code('''
+#include <type_traits>

-        for param in params:
-            param.pybind_predecls(code)
+#include "base/compiler.hh"
+#include "params/$cls.hh"

-        code('''namespace py = pybind11;
+#include "${{cls.cxx_header}}"
+
+''')
+        # only include the python params code if python is enabled.
+        if python_enabled:
+            for param in params:
+                param.pybind_predecls(code)
+
+            code('''namespace py = pybind11;

 static void
 module_init(py::module_ &m_internal)
 {
     py::module_ m = m_internal.def_submodule("param_${cls}");
 ''')
-        code.indent()
-        if cls._base:
-            code('py::class_<${cls}Params, ${{cls._base.type}}Params, ' \
-                 'std::unique_ptr<${{cls}}Params, py::nodelete>>(' \
-                 'm, "${cls}Params")')
-        else:
-            code('py::class_<${cls}Params, ' \
-                 'std::unique_ptr<${cls}Params, py::nodelete>>(' \
-                 'm, "${cls}Params")')
+            code.indent()
+            if cls._base:
+ code('py::class_<${cls}Params, ${{cls._base.type}}Params, ' \
+                    'std::unique_ptr<${{cls}}Params, py::nodelete>>(' \
+                    'm, "${cls}Params")')
+            else:
+                code('py::class_<${cls}Params, ' \
+                    'std::unique_ptr<${cls}Params, py::nodelete>>(' \
+                    'm, "${cls}Params")')

-        code.indent()
-        if not hasattr(cls, 'abstract') or not cls.abstract:
-            code('.def(py::init<>())')
-            code('.def("create", &${cls}Params::create)')
+            code.indent()
+            if not hasattr(cls, 'abstract') or not cls.abstract:
+                code('.def(py::init<>())')
+                code('.def("create", &${cls}Params::create)')

-        param_exports = cls.cxx_param_exports + [
-            PyBindProperty(k)
-            for k, v in sorted(cls._params.local.items())
-        ] + [
-            PyBindProperty("port_%s_connection_count" % port.name)
-            for port in ports.values()
-        ]
-        for exp in param_exports:
-            exp.export(code, "%sParams" % cls)
+            param_exports = cls.cxx_param_exports + [
+                PyBindProperty(k)
+                for k, v in sorted(cls._params.local.items())
+            ] + [
+                PyBindProperty("port_%s_connection_count" % port.name)
+                for port in ports.values()
+            ]
+            for exp in param_exports:
+                exp.export(code, "%sParams" % cls)

-        code(';')
-        code()
-        code.dedent()
+            code(';')
+            code()
+            code.dedent()

-        bases = []
-        if 'cxx_base' in cls._value_dict:
-            # If the c++ base class implied by python inheritance was
-            # overridden, use that value.
-            if cls.cxx_base:
-                bases.append(cls.cxx_base)
-        elif cls._base:
-            # If not and if there was a SimObject base, use its c++ class
-            # as this class' base.
-            bases.append(cls._base.cxx_class)
-        # Add in any extra bases that were requested.
-        bases.extend(cls.cxx_extra_bases)
+            bases = []
+            if 'cxx_base' in cls._value_dict:
+                # If the c++ base class implied by python inheritance was
+                # overridden, use that value.
+                if cls.cxx_base:
+                    bases.append(cls.cxx_base)
+            elif cls._base:
+ # If not and if there was a SimObject base, use its c++ class
+                # as this class' base.
+                bases.append(cls._base.cxx_class)
+            # Add in any extra bases that were requested.
+            bases.extend(cls.cxx_extra_bases)

-        if bases:
-            base_str = ", ".join(bases)
-            code('py::class_<${{cls.cxx_class}}, ${base_str}, ' \
-                 'std::unique_ptr<${{cls.cxx_class}}, py::nodelete>>(' \
-                 'm, "${py_class_name}")')
-        else:
-            code('py::class_<${{cls.cxx_class}}, ' \
-                 'std::unique_ptr<${{cls.cxx_class}}, py::nodelete>>(' \
-                 'm, "${py_class_name}")')
-        code.indent()
-        for exp in cls.cxx_exports:
-            exp.export(code, cls.cxx_class)
-        code(';')
-        code.dedent()
-        code()
-        code.dedent()
-        code('}')
-        code()
- code('static EmbeddedPyBind embed_obj("${0}", module_init, "${1}");',
-             cls, cls._base.type if cls._base else "")
+            if bases:
+                base_str = ", ".join(bases)
+                code('py::class_<${{cls.cxx_class}}, ${base_str}, ' \
+                    'std::unique_ptr<${{cls.cxx_class}}, py::nodelete>>(' \
+                    'm, "${py_class_name}")')
+            else:
+                code('py::class_<${{cls.cxx_class}}, ' \
+                    'std::unique_ptr<${{cls.cxx_class}}, py::nodelete>>(' \
+                    'm, "${py_class_name}")')
+            code.indent()
+            for exp in cls.cxx_exports:
+                exp.export(code, cls.cxx_class)
+            code(';')
+            code.dedent()
+            code()
+            code.dedent()
+            code('}')
+            code()
+            code('static EmbeddedPyBind '
+                 'embed_obj("${0}", module_init, "${1}");',
+                cls, cls._base.type if cls._base else "")
+
+        # include the create() methods whether or not python is enabled.
         if not hasattr(cls, 'abstract') or not cls.abstract:
             if 'type' in cls.__dict__:
                 code()

--
To view, visit https://gem5-review.googlesource.com/c/public/gem5/+/46880
To unsubscribe, or for help writing mail filters, visit https://gem5-review.googlesource.com/settings

Gerrit-Project: public/gem5
Gerrit-Branch: minor-release-staging-v21-0-1
Gerrit-Change-Id: I0f93b3d787d47f26db2de6c4447730f7df87a0dc
Gerrit-Change-Number: 46880
Gerrit-PatchSet: 3
Gerrit-Owner: Bobby R. Bruce <bbr...@ucdavis.edu>
Gerrit-Reviewer: Andreas Sandberg <andreas.sandb...@arm.com>
Gerrit-Reviewer: Bobby R. Bruce <bbr...@ucdavis.edu>
Gerrit-Reviewer: Gabe Black <gabe.bl...@gmail.com>
Gerrit-Reviewer: Jason Lowe-Power <ja...@lowepower.com>
Gerrit-Reviewer: kokoro <noreply+kok...@google.com>
Gerrit-MessageType: merged
_______________________________________________
gem5-dev mailing list -- gem5-dev@gem5.org
To unsubscribe send an email to gem5-dev-le...@gem5.org
%(web_page_url)slistinfo%(cgiext)s/%(_internal_name)s

Reply via email to