gem5  [DEVELOP-FOR-23.0]
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
main.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008 The Hewlett-Packard Development Company
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met: redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer;
9  * redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution;
12  * neither the name of the copyright holders nor the names of its
13  * contributors may be used to endorse or promote products derived from
14  * this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <Python.h>
30 
31 #include <iostream>
32 
33 #include "pybind11/embed.h"
34 #include "pybind11/pybind11.h"
35 
36 #include "python/embedded.hh"
37 #include "sim/init_signals.hh"
38 
39 using namespace gem5;
40 
41 namespace py = pybind11;
42 
43 // main() is now pretty stripped down and just sets up python and then
44 // calls EmbeddedPython::initAll which loads the various embedded python
45 // modules into the python environment and then starts things running by
46 // running python's m5.main().
47 int
48 main(int argc, char **argv)
49 {
50  // Initialize gem5 special signal handling.
51  initSignals();
52 
53 #if PY_VERSION_HEX < 0x03080000
54  // Convert argv[0] to a wchar_t string, using python's locale and cleanup
55  // functions.
56  std::unique_ptr<wchar_t[], decltype(&PyMem_RawFree)> program(
57  Py_DecodeLocale(argv[0], nullptr),
58  &PyMem_RawFree);
59 
60  // This can help python find libraries at run time relative to this binary.
61  // It's probably not necessary, but is mostly harmless and might be useful.
62  Py_SetProgramName(program.get());
63 #else
64  // Preinitialize Python for Python 3.8+
65  // This ensures that the locale configuration takes effect
66  PyStatus status;
67 
68  PyConfig config;
69  PyConfig_InitPythonConfig(&config);
70 
71  /* Set the program name. Implicitly preinitialize Python. */
72  status = PyConfig_SetBytesString(&config, &config.program_name,
73  argv[0]);
74  if (PyStatus_Exception(status)) {
75  PyConfig_Clear(&config);
76  Py_ExitStatusException(status);
77  return 1;
78  }
79 #endif
80 
81  py::scoped_interpreter guard(true, argc, argv);
82 
83  auto importer = py::module_::import("importer");
84  importer.attr("install")();
85 
86  try {
87  py::module_::import("m5").attr("main")();
88  } catch (py::error_already_set &e) {
89  if (e.matches(PyExc_SystemExit))
90  return e.value().attr("code").cast<int>();
91 
92  std::cerr << e.what();
93  return 1;
94  }
95 
96  return 0;
97 }
gem5::ArmISA::e
Bitfield< 9 > e
Definition: misc_types.hh:65
gem5::initSignals
void initSignals()
Definition: init_signals.cc:187
main
int main(int argc, char **argv)
Definition: main.cc:48
embedded.hh
init_signals.hh
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: gpu_translation_state.hh:37
gem5::ArmISA::status
Bitfield< 5, 0 > status
Definition: misc_types.hh:480

Generated on Sun Jul 30 2023 01:56:59 for gem5 by doxygen 1.8.17