sim: Collapse gem5Main into main.

gem5Main is short and simple and only calls into python's m5.main()
function. It's also only called from one place and only makes sense to
call from one place, so lets just put it there inline and keep
everything together.

Change-Id: I17e303e3a29d5473f0f31cd51d7fb367f6c81f9e
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/49417
Reviewed-by: Gabe Black <gabe.black@gmail.com>
Maintainer: Gabe Black <gabe.black@gmail.com>
Tested-by: kokoro <noreply+kokoro@google.com>
This commit is contained in:
Gabe Black
2021-08-17 04:16:34 -07:00
parent ff802f4e27
commit 2af584096f
3 changed files with 31 additions and 41 deletions

View File

@@ -191,41 +191,4 @@ PYBIND11_EMBEDDED_MODULE(_m5, _m5)
EmbeddedPyBind::initAll(_m5);
}
/*
* Start up the M5 simulator. This mostly vectors into the python
* main function.
*/
int
gem5Main(int argc, char **argv)
{
// Embedded python doesn't set up sys.argv, so we'll do that ourselves.
py::list py_argv;
auto sys = py::module::import("sys");
if (py::hasattr(sys, "argv")) {
// sys.argv already exists, so grab that.
py_argv = sys.attr("argv");
} else {
// sys.argv doesn't exist, so create it.
sys.add_object("argv", py_argv);
}
// Clear out argv just in case it has something in it.
py_argv.attr("clear")();
// Fill it with our argvs.
for (int i = 0; i < argc; i++)
py_argv.append(argv[i]);
try {
py::module_::import("m5").attr("main")();
} catch (py::error_already_set &e) {
if (e.matches(PyExc_SystemExit))
return e.value().attr("code").cast<int>();
std::cerr << e.what();
return 1;
}
return 0;
}
} // namespace gem5

View File

@@ -98,8 +98,6 @@ class EmbeddedPyBind
static std::map<std::string, EmbeddedPyBind *> &getMap();
};
int gem5Main(int argc, char **argv);
} // namespace gem5
#endif // __SIM_INIT_HH__

View File

@@ -28,6 +28,8 @@
#include <Python.h>
#include <iostream>
#include "pybind11/embed.h"
#include "pybind11/pybind11.h"
@@ -41,7 +43,7 @@ namespace py = pybind11;
// main() is now pretty stripped down and just sets up python and then
// calls EmbeddedPython::initAll which loads the various embedded python
// modules into the python environment and then starts things running by
// calling gem5Main.
// running python's m5.main().
int
main(int argc, char **argv)
{
@@ -63,5 +65,32 @@ main(int argc, char **argv)
auto importer = py::module_::import("importer");
importer.attr("install")();
return gem5Main(argc, argv);
// Embedded python doesn't set up sys.argv, so we'll do that ourselves.
py::list py_argv;
auto sys = py::module::import("sys");
if (py::hasattr(sys, "argv")) {
// sys.argv already exists, so grab that.
py_argv = sys.attr("argv");
} else {
// sys.argv doesn't exist, so create it.
sys.add_object("argv", py_argv);
}
// Clear out argv just in case it has something in it.
py_argv.attr("clear")();
// Fill it with our argvs.
for (int i = 0; i < argc; i++)
py_argv.append(argv[i]);
try {
py::module_::import("m5").attr("main")();
} catch (py::error_already_set &e) {
if (e.matches(PyExc_SystemExit))
return e.value().attr("code").cast<int>();
std::cerr << e.what();
return 1;
}
return 0;
}