cli(tcl): add ability to pass argument to tcl script from cli

This commit is contained in:
anonkey 2023-10-02 17:18:33 +02:00
parent 672375ed02
commit ea91f189a3
No known key found for this signature in database
GPG Key ID: B01F7EA592D857E8
2 changed files with 41 additions and 15 deletions

View File

@ -4,6 +4,10 @@ List of major changes and improvements between releases
Yosys 0.34 .. Yosys 0.35-dev Yosys 0.34 .. Yosys 0.35-dev
-------------------------- --------------------------
* New commands and options
- Added option "--" to pass arguments down to tcl when using -c option.
- Added ability on MacOS and Windows to pass options after arguments on cli.
Yosys 0.33 .. Yosys 0.34 Yosys 0.33 .. Yosys 0.34
-------------------------- --------------------------

View File

@ -49,39 +49,47 @@
# include <sys/user.h> # include <sys/user.h>
#endif #endif
#if !defined(_WIN32) || defined(__MINGW32__)
# include <unistd.h>
#else
char *optarg; char *optarg;
int optind = 1, optcur = 1; int optind = 1, optcur = 1, optopt = 0;
int getopt(int argc, char **argv, const char *optstring) int getopt(int argc, char **argv, const char *optstring)
{ {
if (optind >= argc || argv[optind][0] != '-') if (optind >= argc)
return -1; return -1;
if (argv[optind][0] != '-') {
optopt = 1;
optarg = argv[optind++];
return optopt;
}
bool takes_arg = false; bool takes_arg = false;
int opt = argv[optind][optcur]; optopt = argv[optind][optcur];
if (optopt == '-') {
++optind;
return -1;
}
for (int i = 0; optstring[i]; i++) for (int i = 0; optstring[i]; i++)
if (opt == optstring[i] && optstring[i + 1] == ':') if (optopt == optstring[i] && optstring[i + 1] == ':')
takes_arg = true; takes_arg = true;
if (!takes_arg) { if (!takes_arg) {
if (argv[optind][++optcur] == 0) if (argv[optind][++optcur] == 0)
optind++, optcur = 1; optind++, optcur = 1;
return opt; return optopt;
} }
if (argv[optind][++optcur]) { if (argv[optind][++optcur]) {
optarg = argv[optind++] + optcur; optarg = argv[optind++] + optcur;
optcur = 1; optcur = 1;
return opt; return optopt;
} }
optarg = argv[++optind]; optarg = argv[++optind];
optind++, optcur = 1; optind++, optcur = 1;
return opt; return optopt;
} }
#endif
USING_YOSYS_NAMESPACE USING_YOSYS_NAMESPACE
@ -215,6 +223,7 @@ int main(int argc, char **argv)
std::string backend_command = "auto"; std::string backend_command = "auto";
std::vector<std::string> vlog_defines; std::vector<std::string> vlog_defines;
std::vector<std::string> passes_commands; std::vector<std::string> passes_commands;
std::vector<std::string> frontend_files;
std::vector<std::string> plugin_filenames; std::vector<std::string> plugin_filenames;
std::string output_filename = ""; std::string output_filename = "";
std::string scriptfile = ""; std::string scriptfile = "";
@ -509,6 +518,9 @@ int main(int argc, char **argv)
case 'C': case 'C':
run_tcl_shell = true; run_tcl_shell = true;
break; break;
case '\001':
frontend_files.push_back(optarg);
break;
default: default:
fprintf(stderr, "Run '%s -h' for help.\n", argv[0]); fprintf(stderr, "Run '%s -h' for help.\n", argv[0]);
exit(1); exit(1);
@ -561,17 +573,27 @@ int main(int argc, char **argv)
run_pass(vdef_cmd); run_pass(vdef_cmd);
} }
while (optind < argc) for (auto it = frontend_files.begin(); it != frontend_files.end(); ++it) {
if (run_frontend(argv[optind++], frontend_command)) if (run_frontend((*it).c_str(), frontend_command))
run_shell = false; run_shell = false;
}
if (!topmodule.empty()) if (!topmodule.empty())
run_pass("hierarchy -top " + topmodule); run_pass("hierarchy -top " + topmodule);
if (!scriptfile.empty()) { if (!scriptfile.empty()) {
if (scriptfile_tcl) { if (scriptfile_tcl) {
#ifdef YOSYS_ENABLE_TCL #ifdef YOSYS_ENABLE_TCL
if (Tcl_EvalFile(yosys_get_tcl_interp(), scriptfile.c_str()) != TCL_OK) int tcl_argc = argc - optind;
std::vector<Tcl_Obj*> script_args;
Tcl_Interp *interp = yosys_get_tcl_interp();
for (int i = optind; i < argc; ++i)
script_args.push_back(Tcl_NewStringObj(argv[i], strlen(argv[i])));
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("argv0", 5), NULL, Tcl_NewStringObj(scriptfile.c_str(), scriptfile.length()), 0);
if (Tcl_EvalFile(interp, scriptfile.c_str()) != TCL_OK)
log_error("TCL interpreter returned an error: %s\n", Tcl_GetStringResult(yosys_get_tcl_interp())); log_error("TCL interpreter returned an error: %s\n", Tcl_GetStringResult(yosys_get_tcl_interp()));
#else #else
log_error("Can't exectue TCL script: this version of yosys is not built with TCL support enabled.\n"); log_error("Can't exectue TCL script: this version of yosys is not built with TCL support enabled.\n");