Skip to content

Commit

Permalink
cli(tcl): add ability to pass argument to tcl script from cli
Browse files Browse the repository at this point in the history
  • Loading branch information
anonkey committed Oct 11, 2023
1 parent 51e9b08 commit b48a254
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ List of major changes and improvements between releases

Yosys 0.34 .. Yosys 0.35-dev
--------------------------
* New commands and options
- Added option "--" to pass arguments down to tcl when using -c option.

Yosys 0.33 .. Yosys 0.34
--------------------------
Expand Down
46 changes: 36 additions & 10 deletions kernel/driver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
# include <editline/readline.h>
#endif

#ifdef YOSYS_ENABLE_TCL
# include <vector>
#endif

#include <stdio.h>
#include <string.h>
#include <limits.h>
Expand All @@ -53,33 +57,39 @@
# include <unistd.h>
#else
char *optarg;
int optind = 1, optcur = 1;
int optind = 1, optcur = 1, optopt = 0;
int getopt(int argc, char **argv, const char *optstring)
{
if (optind >= argc || argv[optind][0] != '-')
return -1;

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++)
if (opt == optstring[i] && optstring[i + 1] == ':')
if (optopt == optstring[i] && optstring[i + 1] == ':')
takes_arg = true;

if (!takes_arg) {
if (argv[optind][++optcur] == 0)
optind++, optcur = 1;
return opt;
return optopt;
}

if (argv[optind][++optcur]) {
optarg = argv[optind++] + optcur;
optcur = 1;
return opt;
return optopt;
}

optarg = argv[++optind];
optind++, optcur = 1;
return opt;
return optopt;
}
#endif

Expand Down Expand Up @@ -221,6 +231,7 @@ int main(int argc, char **argv)
std::string depsfile = "";
std::string topmodule = "";
std::string perffile = "";
bool skip_args_left = false;
bool scriptfile_tcl = false;
bool print_banner = true;
bool print_stats = true;
Expand Down Expand Up @@ -456,6 +467,7 @@ int main(int argc, char **argv)
run_shell = false;
break;
case 'c':

scriptfile = optarg;
scriptfile_tcl = true;
run_shell = false;
Expand Down Expand Up @@ -514,6 +526,7 @@ int main(int argc, char **argv)
exit(1);
}
}
skip_args_left = optopt == '-';

if (log_errfile == NULL) {
log_files.push_back(stdout);
Expand Down Expand Up @@ -561,17 +574,30 @@ int main(int argc, char **argv)
run_pass(vdef_cmd);
}

while (optind < argc)
if (run_frontend(argv[optind++], frontend_command))
run_shell = false;
if (!(scriptfile_tcl && skip_args_left)) {
while (optind < argc)
if (run_frontend(argv[optind++], frontend_command))
run_shell = false;
}

if (!topmodule.empty())
run_pass("hierarchy -top " + topmodule);

if (!scriptfile.empty()) {
if (scriptfile_tcl) {
#ifdef YOSYS_ENABLE_TCL
if (Tcl_EvalFile(yosys_get_tcl_interp(), scriptfile.c_str()) != TCL_OK)
int tcl_argc = skip_args_left ? argc - optind : 0;
std::vector<Tcl_Obj*> script_args;
Tcl_Interp *interp = yosys_get_tcl_interp();
if (skip_args_left) {
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()));
#else
log_error("Can't exectue TCL script: this version of yosys is not built with TCL support enabled.\n");
Expand Down

0 comments on commit b48a254

Please sign in to comment.