/[ascend]/trunk/base/generic/compiler/packages.c
ViewVC logotype

Contents of /trunk/base/generic/compiler/packages.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1512 - (show annotations) (download) (as text)
Wed Jun 27 13:45:17 2007 UTC (15 years, 11 months ago) by jpye
File MIME type: text/x-csrc
File size: 7881 byte(s)
Fixed handling of IDA not present. Commented out some debug stuff.
1 /* ASCEND modelling environment
2 Copyright (C) 1990, 1993, 1994 Thomas Guthrie Epperly, Kirk Abbott.
3 Copyright (C) 2006 Carnegie Mellon University
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
9
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.
14
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,
18 Boston, MA 02111-1307, USA.
19 *//**
20 @file
21 Code to support dynamic and static loading of user packages.
22
23 'Packages' are bits of code that add functionality to ASCEND, like 'plugins'
24 or 'addins' on other projects. In ASCEND we have a few that are pretty much
25 essential -- the 'built in' packges. They are only packages in the sense
26 that a 'register' function must be called to connect them correctly with
27 the rest of the system.
28
29 Next there are the 'static' packages. These ones are linked into
30 The default state is to have packages. As such it takes an explicit
31 definition of NO_PACKAGES, if packages are not to be handled.
32 An explicit definition of STATIC_PACKAGES or DYNAMIC_PACKAGES is also
33 required.
34 *//*
35 by Kirk Abbott
36 Created: July 4, 1994
37 Last in CVS: 1.14 ballan 1998/03/06 15:47:14
38 */
39
40 #if !defined(DYNAMIC_PACKAGES) && !defined(STATIC_PACKAGES) && !defined(NO_PACKAGES)
41 # error "Package linking option not set!"
42 #endif
43
44 #include <math.h>
45 #include <ctype.h> /* was compiler/actype.h */
46
47 #include <utilities/ascConfig.h>
48 #include <utilities/config.h> /* NEW */
49
50 #ifndef ASC_DEFAULTPATH
51 # error "Where is ASC_DEFAULTPATH???"
52 #endif
53
54 #include <general/ospath.h>
55
56
57 #include <utilities/ascMalloc.h>
58 #include <utilities/ascEnvVar.h>
59 #include <compiler/importhandler.h>
60 #include <utilities/ascPanic.h>
61 #include <general/list.h>
62 #include "symtab.h"
63
64
65 #include "functype.h"
66 #include "expr_types.h"
67 #include "extcall.h"
68 #include "mathinst.h"
69 #include "instance_enum.h"
70 #include "instquery.h"
71 #include "atomvalue.h"
72 #include "find.h"
73 #include "rel_blackbox.h"
74 #include "vlist.h"
75 #include "relation.h"
76 #include "safe.h"
77 #include "relation_util.h"
78 #include "extfunc.h"
79 #include <packages/sensitivity.h>
80 #include <packages/ascFreeAllVars.h>
81 #include <packages/defaultall.h>
82 #include "module.h"
83 #include "packages.h"
84
85 /*
86 Initialise the slv data structures used when calling external fns
87 */
88 void Init_BBoxInterp(struct BBoxInterp *interp)
89 {
90 if (interp){
91 interp->status = calc_all_ok;
92 interp->user_data = NULL;
93 interp->task = bb_none;
94 }
95 }
96
97 /*---------------------------------------------
98 BUILT-IN PACKAGES...
99 */
100
101 /**
102 Load builtin packages, unless NO_PACKAGES.
103
104 @return 0 if success, 1 if failure.
105 */
106 static
107 int Builtins_Init(void){
108 int result = 0;
109
110 #ifdef NO_PACKAGES
111 ERROR_REPORTER_HERE(ASC_USER_WARNING,"Builtins_Init: DISABLED at compile-time");
112 #else
113 /* ERROR_REPORTER_DEBUG("Loading function asc_free_all_variables\n"); */
114 result = CreateUserFunctionMethod("asc_free_all_variables"
115 ,Asc_FreeAllVars
116 ,1 /* num of args */
117 ,"Unset 'fixed' flag of all items of type 'solver_var'" /* help */
118 ,NULL /* user_data */
119 ,NULL /* destroy fn */
120 );
121
122 /* ERROR_REPORTER_DEBUG("Registering EXTERNAL asc_default_self"); */
123 result = CreateUserFunctionMethod("defaultself_visit_childatoms"
124 ,defaultself_visit_childatoms
125 ,1 /* num of args */
126 ,"Set local child atoms to their ATOMs' DEFAULT values; recurse into arrays." /* help */
127 ,NULL /* user_data */
128 ,NULL /* destroy fn */
129 );
130
131 /* ERROR_REPORTER_DEBUG("Registering EXTERNAL asc_default_all"); */
132 result = CreateUserFunctionMethod("defaultself_visit_submodels"
133 ,defaultself_visit_submodels
134 ,1 /* num of args */
135 ,"Call 'default_self' methods on any nested sub-models." /* help */
136 ,NULL /* user_data */
137 ,NULL /* destroy fn */
138 );
139 #endif
140 return result;
141 }
142
143 /* return 0 on success */
144 int LoadArchiveLibrary(CONST char *partialpath, CONST char *initfunc){
145
146 #ifdef DYNAMIC_PACKAGES
147 struct FilePath *fp1;
148 int result;
149 struct ImportHandler *handler=NULL;
150
151 /**
152 @TODO
153 * modify SearchArchiveLibraryPath to use the ImportHandler array
154 in each directory in the path.
155 * when a file is found, return information about which ImportHandler
156 should be used to open it, then make the call.
157 */
158
159 /* CONSOLE_DEBUG("Searching for external library '%s'",partialpath); */
160
161 importhandler_createlibrary();
162
163 fp1 = importhandler_findinpath(
164 partialpath, ASC_DEFAULTPATH, PATHENVIRONMENTVAR,&handler
165 );
166 if(fp1==NULL){
167 CONSOLE_DEBUG("External library '%s' not found",partialpath);
168 ERROR_REPORTER_NOLINE(ASC_USER_ERROR,"External library '%s' not found.",partialpath);
169 return 1; /* failure */
170 }
171
172 asc_assert(handler!=NULL);
173
174 /* CONSOLE_DEBUG("About to import external library..."); */
175 /* note the import handler will deal with all the initfunc execution, etc etc */
176 result = (*(handler->importfn))(fp1,initfunc,partialpath);
177 if(result){
178 //CONSOLE_DEBUG("Error %d when importing external library of type '%s'",result,handler->name);
179 ERROR_REPORTER_HERE(ASC_PROG_ERROR,"Error importing external library '%s'",partialpath);
180 ospath_free(fp1);
181 return 1;
182 }
183
184 ospath_free(fp1);
185 return 0;
186 #else
187
188 (void)partialname; (void)initfunc;
189
190 # if defined(STATIC_PACKAGES)
191 ERROR_REPORTER_HERE(ASC_PROG_NOTE,"LoadArchiveLibrary disabled: STATIC_PACKAGES, no need to load dynamically.\n");
192 return 0;
193 # elif defined(NO_PACKAGES)
194 ERROR_REPORTER_HERE(ASC_PROG_ERROR,"LoadArchiveLibrary disabled: NO_PACKAGES");
195 return 1;
196 # else
197 # error "Invalid package linking flags"
198 # endif
199
200 #endif
201 }
202
203 /*---------------------------------------------
204 STATIC_PACKAGES code only...
205
206 Declare the functions which we are expected to be able to call.
207 */
208 #ifndef NO_PACKAGES
209 # ifdef STATIC_PACKAGES
210
211 #include <packages/kvalues.h>
212 #include <packages/bboxtest.h>
213 #include <packages/bisect.h>
214 #include <packages/sensitivity.h>
215
216 # endif
217 #endif
218
219 #ifdef STATIC_PACKAGES
220 /**
221 Load all statically-linked packages
222
223 @return 0 on success, >0 if any CreateUserFunction calls failed.
224 */
225 static int StaticPackages_Init(void){
226 int result = 0;
227
228 result += sensitivity_register();
229 result += kvalues_register();
230 result += bboxtest_register();
231
232 return result;
233 }
234 #endif
235
236 /**
237 This is a general purpose function that will load whatever user
238 functions are required according to the compile-time settings.
239
240 If NO_PACKAGES, nothing will be loaded. If DYNAMIC_PACKAGES, then
241 just the builtin packages will be loaded. If STATIC_PACKAGES then
242 builtin plus those called in 'StaticPackages_Init' will be loaded.
243 */
244 void AddUserFunctions(void){
245
246 CONSOLE_DEBUG("ADDING USER FUNCTIONS");
247
248 #ifdef NO_PACKAGES
249 # ifdef __GNUC__
250 # warning "NO_PACKAGES was defined so external packages are disabled"
251 # endif
252 ERROR_REPORTER_NOLINE(ASC_PROG_NOTE,"AddUserFunctions disabled at compile-time.");
253 #else
254
255 CONSOLE_DEBUG("ADDING BUILTINS");
256 /* Builtins are always statically linked */
257 if (Builtins_Init()) {
258 ERROR_REPORTER_NOLINE(ASC_PROG_WARNING
259 ,"Problem in Builtins_Init: Some user functions not created"
260 );
261 }
262
263 # ifdef DYNAMIC_PACKAGES
264
265 CONSOLE_DEBUG("ADDING DYNAMIC PACKAGES...");
266 /* do nothing. WHY? */
267
268 # elif defined(STATIC_PACKAGES)
269 # ifdef __GNUC__
270 # warning "STATIC PACKAGES"
271 # endif
272
273 CONSOLE_DEBUG("ADDING STATIC PACKAGES FUNCTIONS...");
274 /*The following need to be reimplemented but are basically useful as is. */
275 if (StaticPackages_Init()) {
276 ERROR_REPORTER_NOLINE(ASC_PROG_WARNING
277 ,"Problem in StaticPackages_Init(): Some user functions not created"
278 );
279 }
280
281 # endif
282 #endif
283 }
284

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22