forked from steveicarus/iverilog
/
load_module.cc
236 lines (201 loc) · 6.07 KB
/
load_module.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
* Copyright (c) 2001 Stephen Williams (steve@icarus.com)
*
* This source code is free software; you can redistribute it
* and/or modify it in source code form under the terms of the GNU
* General Public License as published by the Free Software
* Foundation; either version 2 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, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#ifdef HAVE_CVS_IDENT
#ident "$Id: load_module.cc,v 1.12 2003/06/05 04:31:09 steve Exp $"
#endif
# include "config.h"
# include "util.h"
# include "parse_api.h"
# include "compiler.h"
# include <iostream>
# include <map>
# include <string>
# include <sys/types.h>
# include <dirent.h>
# include <ctype.h>
# include <assert.h>
/*
* The module library items are maps of key names to file name within
* the directory.
*/
struct module_library {
char*dir;
bool key_case_sensitive;
map<string,const char*>name_map;
struct module_library*next;
};
static struct module_library*library_list = 0;
static struct module_library*library_last = 0;
const char dir_character = '/';
extern FILE *depend_file;
/*
* Use the type name as a key, and search the module library for a
* file name that has that key.
*/
bool load_module(const char*type)
{
char path[4096];
char*ltype = strdup(type);
for (char*tmp = ltype ; *tmp ; tmp += 1)
*tmp = tolower(*tmp);
for (struct module_library*lcur = library_list
; lcur != 0 ; lcur = lcur->next) {
const char*key = lcur->key_case_sensitive? type : ltype;
map<string,const char*>::const_iterator cur;
cur = lcur->name_map.find(key);
if (cur == lcur->name_map.end())
continue;
sprintf(path, "%s%c%s", lcur->dir, dir_character, (*cur).second);
if(depend_file) {
fprintf(depend_file, "%s\n", path);
}
if (ivlpp_string) {
char*cmdline = (char*)malloc(strlen(ivlpp_string) +
strlen(path) + 2);
strcpy(cmdline, ivlpp_string);
strcat(cmdline, " ");
strcat(cmdline, path);
FILE*file = popen(cmdline, "r");
if (verbose_flag)
cerr << "Executing: " << cmdline << endl;
pform_parse(path, file);
fclose(file);
free(cmdline);
} else {
if (verbose_flag)
cerr << "Loading library file "
<< path << "." << endl;
FILE*file = fopen(path, "r");
assert(file);
pform_parse(path, file);
fclose(file);
}
return true;
}
return false;
}
/*
* This function takes the name of a library directory that the caller
* passed, and builds a name index for it.
*/
int build_library_index(const char*path, bool key_case_sensitive)
{
DIR*dir = opendir(path);
if (dir == 0)
return -1;
if (verbose_flag) {
cerr << "Indexing library: " << path << endl;
}
struct module_library*mlp = new struct module_library;
mlp->dir = strdup(path);
mlp->key_case_sensitive = key_case_sensitive;
/* Scan the director for files. check each file name to see if
it has one of the configured suffixes. If it does, then use
the root of the name as the key and index the file name. */
while (struct dirent*de = readdir(dir)) {
unsigned namsiz = strlen(de->d_name);
char*key = 0;
for (list<const char*>::iterator suf = library_suff.begin()
; suf != library_suff.end()
; suf ++ ) {
const char*sufptr = *suf;
unsigned sufsiz = strlen(sufptr);
if (sufsiz >= namsiz)
continue;
/* If the directory is case insensitive, then so
is the suffix. */
if (key_case_sensitive) {
if (strcmp(de->d_name + (namsiz-sufsiz),
sufptr) != 0)
continue;
} else {
if (strcasecmp(de->d_name + (namsiz-sufsiz),
sufptr) != 0)
continue;
}
key = new char[namsiz-sufsiz+1];
strncpy(key, de->d_name, namsiz-sufsiz);
key[namsiz-sufsiz] = 0;
break;
}
if (key == 0)
continue;
/* If the key is not to be case sensitive, then change
it to lowercase. */
if (! key_case_sensitive)
for (char*tmp = key ; *tmp ; tmp += 1)
*tmp = tolower(*tmp);
mlp->name_map[key] = strdup(de->d_name);
delete[]key;
}
closedir(dir);
if (library_last) {
assert(library_list);
library_last->next = mlp;
mlp->next = 0;
library_last = mlp;
} else {
library_list = mlp;
library_last = mlp;
mlp->next = 0;
}
return 0;
}
/*
* $Log: load_module.cc,v $
* Revision 1.12 2003/06/05 04:31:09 steve
* INclude missing assert.h in load_module.
*
* Revision 1.11 2002/08/12 01:34:59 steve
* conditional ident string using autoconfig.
*
* Revision 1.10 2002/08/03 22:30:00 steve
* Fix suffix parsing of library index.
*
* Revision 1.9 2002/07/10 04:34:18 steve
* Library dir case insensitivity includes the suffix.
*
* Revision 1.8 2002/06/06 18:57:18 steve
* Use standard name for iostream.
*
* Revision 1.7 2002/05/28 20:40:37 steve
* ivl indexes the search path for libraries, and
* supports case insensitive module-to-file lookup.
*
* Revision 1.6 2002/05/28 00:50:39 steve
* Add the ivl -C flag for bulk configuration
* from the driver, and use that to run library
* modules through the preprocessor.
*
* Revision 1.5 2002/04/04 05:26:13 steve
* Add dependency generation.
*
* Revision 1.4 2001/11/20 23:36:34 steve
* Close library files after parsing.
*
* Revision 1.3 2001/11/16 05:07:19 steve
* Add support for +libext+ in command files.
*
* Revision 1.2 2001/10/22 02:05:21 steve
* Handle activating tasks in another root.
*
* Revision 1.1 2001/10/20 23:02:40 steve
* Add automatic module libraries.
*
*/