Merge pull request #4672 from YosysHQ/emil/fix-tcl-args-cxxopts

driver: fix special args passing to tcl and python
This commit is contained in:
Emil J 2024-10-21 15:41:24 +02:00 committed by GitHub
commit 7db4c65970
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 8 additions and 7 deletions

View File

@ -222,7 +222,7 @@ int main(int argc, char **argv)
#endif // YOSYS_ENABLE_TCL #endif // YOSYS_ENABLE_TCL
#ifdef WITH_PYTHON #ifdef WITH_PYTHON
("y,py-scriptfile", "execute the Python <script>", ("y,py-scriptfile", "execute the Python <script>",
cxxopts::value<std::vector<std::string>>(), "<script>") cxxopts::value<std::string>(), "<script>")
#endif // WITH_PYTHON #endif // WITH_PYTHON
("p,commands", "execute <commands> (to chain commands, separate them with semicolon + whitespace: 'cmd1; cmd2')", ("p,commands", "execute <commands> (to chain commands, separate them with semicolon + whitespace: 'cmd1; cmd2')",
cxxopts::value<std::vector<std::string>>(), "<commands>") cxxopts::value<std::vector<std::string>>(), "<commands>")
@ -530,11 +530,11 @@ int main(int argc, char **argv)
if (!scriptfile.empty()) { if (!scriptfile.empty()) {
if (scriptfile_tcl) { if (scriptfile_tcl) {
#ifdef YOSYS_ENABLE_TCL #ifdef YOSYS_ENABLE_TCL
int tcl_argc = argc - optind; int tcl_argc = special_args.size();
std::vector<Tcl_Obj*> script_args; std::vector<Tcl_Obj*> script_args;
Tcl_Interp *interp = yosys_get_tcl_interp(); Tcl_Interp *interp = yosys_get_tcl_interp();
for (int i = optind; i < argc; ++i) for (auto arg : special_args)
script_args.push_back(Tcl_NewStringObj(argv[i], strlen(argv[i]))); script_args.push_back(Tcl_NewStringObj(arg.c_str(), arg.length()));
Tcl_ObjSetVar2(interp, Tcl_NewStringObj("argc", 4), NULL, Tcl_NewIntObj(tcl_argc), 0); Tcl_ObjSetVar2(interp, Tcl_NewStringObj("argc", 4), NULL, Tcl_NewIntObj(tcl_argc), 0);
Tcl_ObjSetVar2(interp, Tcl_NewStringObj("argv", 4), NULL, Tcl_NewListObj(tcl_argc, script_args.data()), 0); Tcl_ObjSetVar2(interp, Tcl_NewStringObj("argv", 4), NULL, Tcl_NewListObj(tcl_argc, script_args.data()), 0);
@ -548,10 +548,11 @@ int main(int argc, char **argv)
} else if (scriptfile_python) { } else if (scriptfile_python) {
#ifdef WITH_PYTHON #ifdef WITH_PYTHON
PyObject *sys = PyImport_ImportModule("sys"); PyObject *sys = PyImport_ImportModule("sys");
PyObject *new_argv = PyList_New(argc - optind + 1); int py_argc = special_args.size() + 1;
PyObject *new_argv = PyList_New(py_argc);
PyList_SetItem(new_argv, 0, PyUnicode_FromString(scriptfile.c_str())); PyList_SetItem(new_argv, 0, PyUnicode_FromString(scriptfile.c_str()));
for (int i = optind; i < argc; ++i) for (int i = 1; i < py_argc; ++i)
PyList_SetItem(new_argv, i - optind + 1, PyUnicode_FromString(argv[i])); PyList_SetItem(new_argv, i, PyUnicode_FromString(special_args[i - 1].c_str()));
PyObject *old_argv = PyObject_GetAttrString(sys, "argv"); PyObject *old_argv = PyObject_GetAttrString(sys, "argv");
PyObject_SetAttrString(sys, "argv", new_argv); PyObject_SetAttrString(sys, "argv", new_argv);