Merge branch 'master' into verilog-ams
[sverilog.git] / load_module.cc
blob5dc55cce1ccbfe5aafbb6990aa671c599c3ca22c
1 /*
2 * Copyright (c) 2001 Stephen Williams (steve@icarus.com)
4 * This source code is free software; you can redistribute it
5 * and/or modify it in source code form under the terms of the GNU
6 * General Public License as published by the Free Software
7 * Foundation; either version 2 of the License, or (at your option)
8 * any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 #ifdef HAVE_CVS_IDENT
20 #ident "$Id: load_module.cc,v 1.13 2005/03/22 15:53:48 steve Exp $"
21 #endif
23 # include "config.h"
24 # include "util.h"
25 # include "parse_api.h"
26 # include "compiler.h"
27 # include <iostream>
28 # include <map>
29 # include <cstdlib>
30 # include <cstring>
31 # include <string>
32 # include <sys/types.h>
33 # include <dirent.h>
34 # include <ctype.h>
35 # include <assert.h>
38 * The module library items are maps of key names to file name within
39 * the directory.
41 struct module_library {
42 char*dir;
43 bool key_case_sensitive;
44 map<string,const char*>name_map;
45 struct module_library*next;
48 static struct module_library*library_list = 0;
49 static struct module_library*library_last = 0;
51 const char dir_character = '/';
52 extern FILE *depend_file;
55 * Use the type name as a key, and search the module library for a
56 * file name that has that key.
58 bool load_module(const char*type)
60 char path[4096];
61 char*ltype = strdup(type);
63 for (char*tmp = ltype ; *tmp ; tmp += 1)
64 *tmp = tolower(*tmp);
66 for (struct module_library*lcur = library_list
67 ; lcur != 0 ; lcur = lcur->next) {
69 const char*key = lcur->key_case_sensitive? type : ltype;
70 map<string,const char*>::const_iterator cur;
71 cur = lcur->name_map.find(key);
72 if (cur == lcur->name_map.end())
73 continue;
75 sprintf(path, "%s%c%s", lcur->dir, dir_character, (*cur).second);
77 if(depend_file) {
78 fprintf(depend_file, "%s\n", path);
81 if (ivlpp_string) {
82 char*cmdline = (char*)malloc(strlen(ivlpp_string) +
83 strlen(path) + 2);
84 strcpy(cmdline, ivlpp_string);
85 strcat(cmdline, " ");
86 strcat(cmdline, path);
87 FILE*file = popen(cmdline, "r");
89 if (verbose_flag)
90 cerr << "Executing: " << cmdline << endl;
92 pform_parse(path, file);
93 pclose(file);
94 free(cmdline);
96 } else {
97 if (verbose_flag)
98 cerr << "Loading library file "
99 << path << "." << endl;
101 FILE*file = fopen(path, "r");
102 assert(file);
103 pform_parse(path, file);
104 fclose(file);
107 return true;
111 return false;
115 * This function takes the name of a library directory that the caller
116 * passed, and builds a name index for it.
118 int build_library_index(const char*path, bool key_case_sensitive)
120 DIR*dir = opendir(path);
121 if (dir == 0)
122 return -1;
124 if (verbose_flag) {
125 cerr << "Indexing library: " << path << endl;
128 struct module_library*mlp = new struct module_library;
129 mlp->dir = strdup(path);
130 mlp->key_case_sensitive = key_case_sensitive;
132 /* Scan the director for files. check each file name to see if
133 it has one of the configured suffixes. If it does, then use
134 the root of the name as the key and index the file name. */
135 while (struct dirent*de = readdir(dir)) {
136 unsigned namsiz = strlen(de->d_name);
137 char*key = 0;
139 for (list<const char*>::iterator suf = library_suff.begin()
140 ; suf != library_suff.end()
141 ; suf ++ ) {
142 const char*sufptr = *suf;
143 unsigned sufsiz = strlen(sufptr);
145 if (sufsiz >= namsiz)
146 continue;
148 /* If the directory is case insensitive, then so
149 is the suffix. */
150 if (key_case_sensitive) {
151 if (strcmp(de->d_name + (namsiz-sufsiz),
152 sufptr) != 0)
153 continue;
154 } else {
155 if (strcasecmp(de->d_name + (namsiz-sufsiz),
156 sufptr) != 0)
157 continue;
160 key = new char[namsiz-sufsiz+1];
161 strncpy(key, de->d_name, namsiz-sufsiz);
162 key[namsiz-sufsiz] = 0;
164 break;
167 if (key == 0)
168 continue;
170 /* If the key is not to be case sensitive, then change
171 it to lowercase. */
172 if (! key_case_sensitive)
173 for (char*tmp = key ; *tmp ; tmp += 1)
174 *tmp = tolower(*tmp);
176 mlp->name_map[key] = strdup(de->d_name);
177 delete[]key;
180 closedir(dir);
182 if (library_last) {
183 assert(library_list);
184 library_last->next = mlp;
185 mlp->next = 0;
186 library_last = mlp;
187 } else {
188 library_list = mlp;
189 library_last = mlp;
190 mlp->next = 0;
193 return 0;
197 * $Log: load_module.cc,v $
198 * Revision 1.13 2005/03/22 15:53:48 steve
199 * popen must be matched by pclose.
201 * Revision 1.12 2003/06/05 04:31:09 steve
202 * INclude missing assert.h in load_module.
204 * Revision 1.11 2002/08/12 01:34:59 steve
205 * conditional ident string using autoconfig.
207 * Revision 1.10 2002/08/03 22:30:00 steve
208 * Fix suffix parsing of library index.
210 * Revision 1.9 2002/07/10 04:34:18 steve
211 * Library dir case insensitivity includes the suffix.
213 * Revision 1.8 2002/06/06 18:57:18 steve
214 * Use standard name for iostream.
216 * Revision 1.7 2002/05/28 20:40:37 steve
217 * ivl indexes the search path for libraries, and
218 * supports case insensitive module-to-file lookup.
220 * Revision 1.6 2002/05/28 00:50:39 steve
221 * Add the ivl -C flag for bulk configuration
222 * from the driver, and use that to run library
223 * modules through the preprocessor.
225 * Revision 1.5 2002/04/04 05:26:13 steve
226 * Add dependency generation.
228 * Revision 1.4 2001/11/20 23:36:34 steve
229 * Close library files after parsing.
231 * Revision 1.3 2001/11/16 05:07:19 steve
232 * Add support for +libext+ in command files.
234 * Revision 1.2 2001/10/22 02:05:21 steve
235 * Handle activating tasks in another root.
237 * Revision 1.1 2001/10/20 23:02:40 steve
238 * Add automatic module libraries.