OpenSTA/app/StaMain.cc

246 lines
5.9 KiB
C++
Raw Normal View History

2018-09-28 17:54:21 +02:00
// OpenSTA, Static Timing Analyzer
2019-01-01 21:26:11 +01:00
// Copyright (c) 2019, Parallax Software, Inc.
2018-09-28 17:54:21 +02:00
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <tcl.h>
#include <stdlib.h>
#include "Machine.hh"
#include "StringUtil.hh"
#include "Vector.hh"
#include "Sta.hh"
#include "StaMain.hh"
namespace sta {
typedef sta::Vector<SwigInitFunc> SwigInitFuncSeq;
// "Arguments" passed to staTclAppInit.
static int sta_argc;
static char **sta_argv;
2019-05-20 01:06:06 +02:00
static const char **sta_tcl_inits;
2018-09-28 17:54:21 +02:00
static SwigInitFunc sta_swig_init;
static const char *init_filename = "[file join $env(HOME) .sta]";
void
staMain(Sta *sta,
int argc,
char *argv[],
2019-05-20 01:06:06 +02:00
SwigInitFunc swig_init,
const char *tcl_inits[])
2018-09-28 17:54:21 +02:00
{
initSta();
Sta::setSta(sta);
sta->makeComponents();
int thread_count = 1;
bool threads_exists = false;
parseThreadsArg(argc, argv, thread_count, threads_exists);
if (threads_exists)
sta->setThreadCount(thread_count);
2019-05-20 01:06:06 +02:00
staSetupAppInit(argc, argv, swig_init, tcl_inits);
2018-09-28 17:54:21 +02:00
// Set argc to 1 so Tcl_Main doesn't source any files.
// Tcl_Main never returns.
Tcl_Main(1, argv, staTclAppInit);
}
void
parseThreadsArg(int argc,
char **argv,
int &thread_count,
bool &exists)
{
char *thread_arg = findCmdLineKey(argc, argv, "-threads");
if (thread_arg) {
if (stringEqual(thread_arg, "max")) {
thread_count = processorCount();
exists = true;
}
else if (isDigits(thread_arg)) {
thread_count = atoi(thread_arg);
exists = true;
}
else
fprintf(stderr,"Warning: -threads must be max or a positive integer.\n");
}
}
// Set globals to pass to staTclAppInit.
void
staSetupAppInit(int argc,
char *argv[],
2019-05-20 01:06:06 +02:00
SwigInitFunc swig_init,
const char *tcl_inits[])
2018-09-28 17:54:21 +02:00
{
sta_argc = argc;
sta_argv = argv;
2019-05-20 01:06:06 +02:00
sta_tcl_inits = tcl_inits;
2018-09-28 17:54:21 +02:00
sta_swig_init = swig_init;
}
// Tcl init executed inside Tcl_Main.
int
staTclAppInit(Tcl_Interp *interp)
{
int argc = sta_argc;
char **argv = sta_argv;
2019-01-04 01:14:15 +01:00
// source init.tcl
Tcl_Init(interp);
2018-09-28 17:54:21 +02:00
// Define swig commands.
sta_swig_init(interp);
Sta *sta = Sta::sta();
sta->setTclInterp(interp);
// Eval encoded sta TCL sources.
2019-05-20 01:06:06 +02:00
evalTclInit(interp, sta_tcl_inits);
2018-09-28 17:54:21 +02:00
if (!findCmdLineFlag(argc, argv, "-no_splash"))
Tcl_Eval(interp, "sta::show_splash");
// Import exported commands from sta namespace to global namespace.
Tcl_Eval(interp, "sta::define_sta_cmds");
Tcl_Eval(interp, "namespace import sta::*");
2018-09-28 17:54:21 +02:00
if (!findCmdLineFlag(argc, argv, "-no_init"))
2019-06-05 19:20:48 +02:00
sourceTclFile(init_filename, true, true, interp);
2018-09-28 17:54:21 +02:00
bool exit_after_cmd_file = findCmdLineFlag(argc, argv, "-exit");
if (argc > 2 ||
(argc > 1 && argv[1][0] == '-'))
2019-06-17 17:32:28 +02:00
showUsage(argv[0]);
else {
if (argc == 2) {
char *cmd_file = argv[1];
if (cmd_file) {
sourceTclFile(cmd_file, false, false, interp);
if (exit_after_cmd_file)
exit(EXIT_SUCCESS);
}
}
}
2018-09-28 17:54:21 +02:00
return TCL_OK;
}
bool
findCmdLineFlag(int &argc,
char *argv[],
2018-09-28 17:54:21 +02:00
const char *flag)
{
bool found = false;
int j = 1;
int argc1 = argc;
for (int i = 1; i < argc1; i++) {
char *arg = argv[i];
if (stringEq(arg, flag)) {
found = true;
// remove flag from argv.
argc--;
}
else
argv[j++] = argv[i];
2018-09-28 17:54:21 +02:00
}
return found;
2018-09-28 17:54:21 +02:00
}
char *
findCmdLineKey(int &argc,
char *argv[],
2018-09-28 17:54:21 +02:00
const char *key)
{
char *value = nullptr;
int j = 1;
int argc1 = argc;
for (int i = 1; i < argc1; i++) {
char *arg = argv[i];
if (stringEq(arg, key) && i + 1 < argc) {
value = argv[i + 1];
// remove key and value from argv.
i++;
argc -= 2;
}
else
argv[j++] = argv[i];
2018-09-28 17:54:21 +02:00
}
return value;
2018-09-28 17:54:21 +02:00
}
// Use overridden version of source to echo cmds and results.
2019-06-04 19:18:14 +02:00
void
2019-06-05 19:20:48 +02:00
sourceTclFile(const char *filename,
bool echo,
bool verbose,
Tcl_Interp *interp)
2018-09-28 17:54:21 +02:00
{
2019-01-17 00:37:31 +01:00
string cmd;
2019-06-05 19:20:48 +02:00
stringPrint(cmd, "source %s%s%s",
echo ? "-echo " : "",
verbose ? "-verbose " : "",
filename);
2019-01-17 00:37:31 +01:00
Tcl_Eval(interp, cmd.c_str());
2018-09-28 17:54:21 +02:00
}
void
evalTclInit(Tcl_Interp *interp,
const char *inits[])
{
size_t length = 0;
for (const char **e = inits; *e; e++) {
const char *init = *e;
length += strlen(init);
}
char *unencoded = new char[length / 3 + 1];
char *u = unencoded;
for (const char **e = inits; *e; e++) {
const char *init = *e;
size_t init_length = strlen(init);
for (const char *s = init; s < &init[init_length]; s += 3) {
char code[4] = {s[0], s[1], s[2], '\0'};
char ch = atoi(code);
*u++ = ch;
}
}
*u = '\0';
if (Tcl_Eval(interp, unencoded) != TCL_OK) {
// Get a backtrace for the error.
Tcl_Eval(interp, "$errorInfo");
const char *tcl_err = Tcl_GetStringResult(interp);
fprintf(stderr, "Error: TCL init script: %s.\n", tcl_err);
fprintf(stderr, " Try deleting app/TclInitVar.cc and rebuilding.\n");
exit(0);
}
delete [] unencoded;
}
void
2019-06-17 17:32:28 +02:00
showUsage(const char * prog)
2018-09-28 17:54:21 +02:00
{
printf("Usage: %s [-help] [-version] [-no_init] [-exit] cmd_file\n", prog);
2018-09-28 17:54:21 +02:00
printf(" -help show help and exit\n");
printf(" -version show version and exit\n");
printf(" -no_init do not read .sta init file\n");
printf(" -threads count|max use count threads\n");
2019-06-01 17:07:38 +02:00
printf(" -no_splash do not show the license splash at startup\n");
printf(" -exit exit after reading cmd_file\n");
printf(" cmd_file source cmd_file\n");
2018-09-28 17:54:21 +02:00
}
} // namespace