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

Contents of /trunk/ascend/compiler/packages.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2018 - (show annotations) (download) (as text)
Wed Apr 29 03:38:10 2009 UTC (11 years, 2 months ago) by jpye
File MIME type: text/x-csrc
File size: 6713 byte(s)
Fixed compile for new header file locations <ascend/compiler/xxx.h> etc.
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 #include <math.h>
41 #include <ctype.h> /* was compiler/actype.h */
42
43 #include <ascend/utilities/ascConfig.h>
44 #include <ascend/utilities/config.h> /* NEW */
45
46 #ifndef ASC_DEFAULT_ASCENDLIBRARY
47 # error "Where is ASC_DEFAULT_ASCENDLIBRARY???"
48 #endif
49
50 #include <ascend/general/ospath.h>
51
52
53 #include <ascend/utilities/ascMalloc.h>
54 #include <ascend/utilities/ascEnvVar.h>
55 #include <ascend/compiler/importhandler.h>
56 #include <ascend/utilities/ascPanic.h>
57 #include <ascend/general/list.h>
58 #include "symtab.h"
59
60
61 #include "functype.h"
62 #include "expr_types.h"
63 #include "extcall.h"
64 #include "mathinst.h"
65 #include "instance_enum.h"
66 #include "instquery.h"
67 #include "atomvalue.h"
68 #include "find.h"
69 #include "rel_blackbox.h"
70 #include "vlist.h"
71 #include "relation.h"
72 #include "safe.h"
73 #include "relation_util.h"
74 #include "extfunc.h"
75 #include <ascend/packages/sensitivity.h>
76 #include <ascend/packages/ascFreeAllVars.h>
77 #include <ascend/packages/defaultall.h>
78 #include "module.h"
79 #include "packages.h"
80 #include "defaultpaths.h"
81
82 /* #define PACKAGE_DEBUG */
83
84 /*
85 Initialise the slv data structures used when calling external fns
86 */
87 void Init_BBoxInterp(struct BBoxInterp *interp)
88 {
89 if (interp){
90 interp->status = calc_all_ok;
91 interp->user_data = NULL;
92 interp->task = bb_none;
93 }
94 }
95
96 /*---------------------------------------------
97 BUILT-IN PACKAGES...
98 */
99
100 /**
101 Load builtin packages (those that are compiled into libascend)
102
103 @return 0 if success, 1 if failure.
104 */
105 static
106 int Builtins_Init(void){
107 int result = 0;
108
109 /* ERROR_REPORTER_DEBUG("Loading function asc_free_all_variables\n"); */
110 result = CreateUserFunctionMethod("asc_free_all_variables"
111 ,Asc_FreeAllVars
112 ,1 /* num of args */
113 ,"Unset 'fixed' flag of all items of type 'solver_var'" /* help */
114 ,NULL /* user_data */
115 ,NULL /* destroy fn */
116 );
117
118 /* ERROR_REPORTER_DEBUG("Registering EXTERNAL asc_default_self"); */
119 result = CreateUserFunctionMethod("defaultself_visit_childatoms"
120 ,defaultself_visit_childatoms
121 ,1 /* num of args */
122 ,"Set local child atoms to their ATOMs' DEFAULT values; recurse into arrays." /* help */
123 ,NULL /* user_data */
124 ,NULL /* destroy fn */
125 );
126
127 /* ERROR_REPORTER_DEBUG("Registering EXTERNAL asc_default_all"); */
128 result = CreateUserFunctionMethod("defaultself_visit_submodels"
129 ,defaultself_visit_submodels
130 ,1 /* num of args */
131 ,"Call 'default_self' methods on any nested sub-models." /* help */
132 ,NULL /* user_data */
133 ,NULL /* destroy fn */
134 );
135
136 return result;
137 }
138
139 /* return 0 on success */
140 int package_load(CONST char *partialpath, CONST char *initfunc){
141
142 struct FilePath *fp1;
143 int result;
144 struct ImportHandler *handler=NULL;
145 static char *default_solvers_path = NULL;
146 static char *default_library_path = NULL;
147 if(!default_solvers_path){
148 default_solvers_path = get_default_solvers_path();
149 CONSOLE_DEBUG("Default ASCENDSOLVERS set to '%s'", default_solvers_path);
150 }
151 if(!default_library_path){
152 default_library_path = get_default_library_path();
153 CONSOLE_DEBUG("Default ASCENDLIBRARY set to '%s'", default_library_path);
154 }
155
156 #ifdef PACKAGE_DEBUG
157 CONSOLE_DEBUG("Searching for external library '%s'",partialpath);
158 #endif
159
160 importhandler_createlibrary();
161
162 /* search in the ASCENDSOLVERS directory/ies first */
163 fp1 = importhandler_findinpath(
164 partialpath, default_solvers_path, ASC_ENV_SOLVERS,&handler
165 );
166
167 /* next, search in the ASCENDLIBRARY */
168 if(fp1==NULL){
169 fp1 = importhandler_findinpath(
170 partialpath, default_library_path, ASC_ENV_LIBRARY,&handler
171 );
172 if(fp1==NULL){
173 CONSOLE_DEBUG("External library '%s' not found",partialpath);
174 ERROR_REPORTER_NOLINE(ASC_USER_ERROR,"External library '%s' not found.",partialpath);
175 return 1; /* failure */
176 }
177 #ifdef PACKAGE_DEBUG
178 else{
179 CONSOLE_DEBUG("FOUND in $ASCENDLIBRARY");
180 }
181 }else{
182 CONSOLE_DEBUG("Found in pwd or in $ASCENDSOLVERS");
183 #endif
184 }
185
186 asc_assert(handler!=NULL);
187
188 #ifdef PACKAGE_DEBUG
189 CONSOLE_DEBUG("About to import external library...");
190 #endif
191
192 /* run the import handlers' importfn to do the actual loading, registration etc. */
193 result = (*(handler->importfn))(fp1,initfunc,partialpath);
194 if(result){
195 #ifdef PACKAGE_DEBUG
196 CONSOLE_DEBUG("Error %d when importing external library of type '%s'",result,handler->name);
197 #endif
198 ERROR_REPORTER_HERE(ASC_PROG_ERROR,"Error importing external library '%s'",partialpath);
199 ospath_free(fp1);
200 return 1;
201 }
202
203 ospath_free(fp1);
204 return 0;
205 }
206
207 /*---------------------------------------------
208 STATIC_PACKAGES code only...
209
210 Declare the functions which we are expected to be able to call.
211 */
212
213 /**
214 This is a general purpose function that will load whatever user
215 functions are required according to the compile-time settings.
216
217 If NO_PACKAGES, nothing will be loaded. If DYNAMIC_PACKAGES, then
218 just the builtin packages will be loaded. If STATIC_PACKAGES then
219 builtin plus those called in 'StaticPackages_Init' will be loaded.
220 */
221 void AddUserFunctions(void){
222
223 /* Builtins are always statically linked */
224 if (Builtins_Init()) {
225 ERROR_REPORTER_NOLINE(ASC_PROG_WARNING
226 ,"Problem in Builtins_Init: Some user functions not created"
227 );
228 }
229 }
230

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