OpenSTA/app/StaMain.cc

152 lines
3.6 KiB
C++
Raw Normal View History

2018-09-28 17:54:21 +02:00
// OpenSTA, Static Timing Analyzer
// Copyright (c) 2024, 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
2018-09-28 17:54:21 +02:00
// 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/>.
2018-09-28 17:54:21 +02:00
2020-04-05 23:53:44 +02:00
#include "StaMain.hh"
2020-04-05 20:35:51 +02:00
2018-09-28 17:54:21 +02:00
#include <tcl.h>
#include <cstdlib>
2020-11-30 16:05:50 +01:00
#include <sys/stat.h>
2020-04-05 20:35:51 +02:00
2021-02-14 17:44:35 +01:00
#include "Machine.hh"
2020-04-05 23:53:44 +02:00
#include "StringUtil.hh"
#include "Vector.hh"
#include "Sta.hh"
2018-09-28 17:54:21 +02:00
namespace sta {
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--;
argv[argc] = nullptr;
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;
argv[argc] = nullptr;
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.
2020-11-13 20:46:19 +01:00
int
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);
2020-11-13 20:46:19 +01:00
int code = Tcl_Eval(interp, cmd.c_str());
const char *result = Tcl_GetStringResult(interp);
if (result[0] != '\0')
printf("%s\n", result);
return code;
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);
2020-04-05 23:53:44 +02:00
fprintf(stderr, " Try deleting TclInitVar.cc and rebuilding.\n");
2020-03-30 18:06:22 +02:00
exit(0);
}
delete [] unencoded;
}
2021-03-11 02:47:18 +01:00
char *
2020-03-30 18:06:22 +02:00
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
}
// Hack until c++17 filesystem is better supported.
bool
is_regular_file(const char *filename)
{
struct stat sb;
return stat(filename, &sb) == 0 && S_ISREG(sb.st_mode);
}
2018-09-28 17:54:21 +02:00
} // namespace