OpenSTA/app/StaMain.cc

139 lines
3.3 KiB
C++
Raw Normal View History

2018-09-28 17:54:21 +02:00
// OpenSTA, Static Timing Analyzer
2020-03-07 03:50:37 +01:00
// Copyright (c) 2020, 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/>.
2020-04-05 20:35:51 +02:00
#include "app/StaMain.hh"
2018-09-28 17:54:21 +02:00
#include <tcl.h>
#include <stdlib.h>
2020-04-05 20:35:51 +02:00
#include "util/StringUtil.hh"
#include "util/Vector.hh"
#include "search/Sta.hh"
2018-09-28 17:54:21 +02:00
namespace sta {
2020-03-30 18:06:22 +02:00
static char *
unencode(const char *inits[]);
2019-08-08 23:13:02 +02:00
int
parseThreadsArg(int &argc,
char *argv[])
2018-09-28 17:54:21 +02:00
{
char *thread_arg = findCmdLineKey(argc, argv, "-threads");
if (thread_arg) {
2019-08-08 23:13:02 +02:00
if (stringEqual(thread_arg, "max"))
return processorCount();
else if (isDigits(thread_arg))
return atoi(thread_arg);
2018-09-28 17:54:21 +02:00
else
fprintf(stderr,"Warning: -threads must be max or a positive integer.\n");
}
2019-08-08 23:13:02 +02:00
return 1;
2018-09-28 17:54:21 +02:00
}
bool
findCmdLineFlag(int &argc,
char *argv[],
2018-09-28 17:54:21 +02:00
const char *flag)
{
2019-06-18 01:42:26 +02:00
for (int i = 1; i < argc; i++) {
char *arg = argv[i];
if (stringEq(arg, flag)) {
// remove flag from argv.
2019-06-18 01:42:26 +02:00
for (int j = i + 1; j < argc; j++, i++)
argv[i] = argv[j];
argc--;
2019-06-18 01:42:26 +02:00
return true;
}
2018-09-28 17:54:21 +02:00
}
2019-06-18 01:42:26 +02:00
return false;
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)
{
2019-06-18 01:42:26 +02:00
for (int i = 1; i < argc; i++) {
char *arg = argv[i];
if (stringEq(arg, key) && i + 1 < argc) {
2019-06-18 01:42:26 +02:00
char *value = argv[i + 1];
// remove key and value from argv.
2019-06-18 01:42:26 +02:00
for (int j = i + 2; j < argc; j++, i++)
argv[i] = argv[j];
argc -= 2;
2019-06-18 01:42:26 +02:00
return value;
}
2018-09-28 17:54:21 +02:00
}
2019-06-18 01:42:26 +02:00
return nullptr;
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[])
2020-03-30 18:06:22 +02:00
{
char *unencoded = unencode(inits);
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;
}
static char *
unencode(const char *inits[])
2018-09-28 17:54:21 +02:00
{
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';
2020-03-30 18:06:22 +02:00
return unencoded;
2018-09-28 17:54:21 +02:00
}
} // namespace