31a32,33
> #include <Python.h> // must be before system headers... see Python docs
>
43,44d44
< #include "base/copyright.hh"
< #include "base/embedfile.hh"
54d53
< #include "python/pyconfig.hh"
114,157d112
< /// Show brief help message.
< void
< showBriefHelp(ostream &out)
< {
< char *prog = basename(myProgName);
<
< ccprintf(out, "Usage:\n");
< ccprintf(out,
< "%s [-d <dir>] [-E <var>[=<val>]] [-I <dir>] [-P <python>]\n"
< " [--<var>=<val>] <config file>\n"
< "\n"
< " -d set the output directory to <dir>\n"
< " -E set the environment variable <var> to <val> (or 'True')\n"
< " -I add the directory <dir> to python's path\n"
< " -P execute <python> directly in the configuration\n"
< " --var=val set the python variable <var> to '<val>'\n"
< " <configfile> config file name (ends in .py)\n\n",
< prog);
<
< ccprintf(out, "%s -X\n -X extract embedded files\n\n", prog);
< ccprintf(out, "%s -h\n -h print short help\n\n", prog);
< }
<
< /// Print welcome message.
< void
< sayHello(ostream &out)
< {
< extern const char *compileDate; // from date.cc
<
< ccprintf(out, "M5 Simulator System\n");
< // display copyright
< ccprintf(out, "%s\n", briefCopyright);
< ccprintf(out, "M5 compiled on %d\n", compileDate);
<
< char *host = getenv("HOSTNAME");
< if (!host)
< host = getenv("HOST");
<
< if (host)
< ccprintf(out, "M5 executing on %s\n", host);
<
< ccprintf(out, "M5 simulation started %s\n", Time::start);
< }
<
194,199c149
< char *
< getOptionString(int &index, int argc, char **argv)
< {
< char *option = argv[index] + 2;
< if (*option != '\0')
< return option;
---
> #include "config/python_build_env.hh"
201,207d150
< // We didn't find an argument, it must be in the next variable.
< if (++index >= argc)
< panic("option string for option '%s' not found", argv[index - 1]);
<
< return argv[index];
< }
<
221,223c164,168
< bool configfile_found = false;
< PythonConfig pyconfig;
< string outdir;
---
> // Python embedded interpreter invocation
> Py_SetProgramName(argv[0]);
> const char *fileName = Py_GetProgramFullPath();
> Py_Initialize();
> PySys_SetArgv(argc, argv);
225,228c170
< if (argc < 2) {
< showBriefHelp(cerr);
< exit(1);
< }
---
> // loadSwigModules();
230c172,176
< sayHello(cerr);
---
> // Set Python module path to include current file to find embedded
> // zip archive
> PyRun_SimpleString("import sys");
> string pathCmd = csprintf("sys.path[1:1] = ['%s']", fileName);
> PyRun_SimpleString(pathCmd.c_str());
232,237c178,181
< // Parse command-line options.
< // Since most of the complex options are handled through the
< // config database, we don't mess with getopts, and just parse
< // manually.
< for (int i = 1; i < argc; ++i) {
< char *arg_str = argv[i];
---
> // Pass compile timestamp string to Python
> extern const char *compileDate; // from date.cc
> string setCompileDate = csprintf("compileDate = '%s'", compileDate);
> PyRun_SimpleString(setCompileDate.c_str());
239,246c183,188
< // if arg starts with '--', parse as a special python option
< // of the format --<python var>=<string value>, if the arg
< // starts with '-', it should be a simulator option with a
< // format similar to getopt. In any other case, treat the
< // option as a configuration file name and load it.
< if (arg_str[0] == '-' && arg_str[1] == '-') {
< string str = &arg_str[2];
< string var, val;
---
> // PyRun_InteractiveLoop(stdin, "stdin");
> // m5/__init__.py currently contains main argv parsing loop etc.,
> // and will write out config.ini file before returning.
> PyImport_ImportModule("defines");
> PyImport_ImportModule("m5");
> Py_Finalize();
248,250c190
< if (!split_first(str, var, val, '='))
< panic("Could not parse configuration argument '%s'\n"
< "Expecting --<variable>=<value>\n", arg_str);
---
> configStream = simout.find("config.out");
252,331d191
< pyconfig.setVariable(var, val);
< } else if (arg_str[0] == '-') {
< char *option;
< string var, val;
<
< // switch on second char
< switch (arg_str[1]) {
< case 'd':
< outdir = getOptionString(i, argc, argv);
< break;
<
< case 'h':
< showBriefHelp(cerr);
< exit(1);
<
< case 'E':
< option = getOptionString(i, argc, argv);
< if (!split_first(option, var, val, '='))
< val = "True";
<
< if (setenv(var.c_str(), val.c_str(), true) == -1)
< panic("setenv: %s\n", strerror(errno));
< break;
<
< case 'I':
< option = getOptionString(i, argc, argv);
< pyconfig.addPath(option);
< break;
<
< case 'P':
< option = getOptionString(i, argc, argv);
< pyconfig.writeLine(option);
< break;
<
< case 'X': {
< list<EmbedFile> lst;
< EmbedMap::all(lst);
< list<EmbedFile>::iterator i = lst.begin();
< list<EmbedFile>::iterator end = lst.end();
<
< while (i != end) {
< cprintf("Embedded File: %s\n", i->name);
< cout.write(i->data, i->length);
< ++i;
< }
<
< return 0;
< }
<
< default:
< showBriefHelp(cerr);
< panic("invalid argument '%s'\n", arg_str);
< }
< } else {
< string file(arg_str);
< string base, ext;
<
< if (!split_last(file, base, ext, '.') || ext != "py")
< panic("Config file '%s' must end in '.py'\n", file);
<
< pyconfig.load(file);
< configfile_found = true;
< }
< }
<
< if (outdir.empty()) {
< char *env = getenv("OUTPUT_DIR");
< outdir = env ? env : ".";
< }
<
< simout.setDirectory(outdir);
<
< char *env = getenv("CONFIG_OUTPUT");
< if (!env)
< env = "config.out";
< configStream = simout.find(env);
<
< if (!configfile_found)
< panic("no configuration file specified!");
<
334,335c194
< if (!pyconfig.output(inifile))
< panic("Error processing python code");
---
> inifile.load("config.ini");
349,353d207
< // Print hello message to stats file if it's actually a file. If
< // it's not (i.e. it's cout or cerr) then we already did it above.
< if (simout.isFile(*outputStream))
< sayHello(*outputStream);
<