forked from The-OpenROAD-Project/OpenSTA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStaMain.cc
138 lines (126 loc) · 3.3 KB
/
StaMain.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// OpenSTA, Static Timing Analyzer
// Copyright (c) 2020, Parallax Software, Inc.
//
// 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 "StaMain.hh"
#include <tcl.h>
#include <stdlib.h>
#include "StringUtil.hh"
#include "Vector.hh"
#include "Sta.hh"
namespace sta {
static char *
unencode(const char *inits[]);
int
parseThreadsArg(int &argc,
char *argv[])
{
char *thread_arg = findCmdLineKey(argc, argv, "-threads");
if (thread_arg) {
if (stringEqual(thread_arg, "max"))
return processorCount();
else if (isDigits(thread_arg))
return atoi(thread_arg);
else
fprintf(stderr,"Warning: -threads must be max or a positive integer.\n");
}
return 1;
}
bool
findCmdLineFlag(int &argc,
char *argv[],
const char *flag)
{
for (int i = 1; i < argc; i++) {
char *arg = argv[i];
if (stringEq(arg, flag)) {
// remove flag from argv.
for (int j = i + 1; j < argc; j++, i++)
argv[i] = argv[j];
argc--;
return true;
}
}
return false;
}
char *
findCmdLineKey(int &argc,
char *argv[],
const char *key)
{
for (int i = 1; i < argc; i++) {
char *arg = argv[i];
if (stringEq(arg, key) && i + 1 < argc) {
char *value = argv[i + 1];
// remove key and value from argv.
for (int j = i + 2; j < argc; j++, i++)
argv[i] = argv[j];
argc -= 2;
return value;
}
}
return nullptr;
}
// Use overridden version of source to echo cmds and results.
void
sourceTclFile(const char *filename,
bool echo,
bool verbose,
Tcl_Interp *interp)
{
string cmd;
stringPrint(cmd, "source %s%s%s",
echo ? "-echo " : "",
verbose ? "-verbose " : "",
filename);
Tcl_Eval(interp, cmd.c_str());
}
void
evalTclInit(Tcl_Interp *interp,
const char *inits[])
{
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 TclInitVar.cc and rebuilding.\n");
exit(0);
}
delete [] unencoded;
}
static char *
unencode(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';
return unencoded;
}
} // namespace