YAP 7.1.0
load_foreign.yap
Go to the documentation of this file.
1/*************************************************************************
2* *
3* YAP Prolog *
4* *
5* Yap Prolog was developed at NCCUP - Universidade do Porto *
6* *
7* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
8* *
9**************************************************************************
10* *
11* File: load_foreign.yap *
12* Last rev: 8/2/88 *
13* mods: *
14* comments: Utility predicates for load_foreign *
15* *
16*************************************************************************/
17
18/**
19 * @file load_foreign.yap
20 *
21 * @brief load predicates written in C (also C++, Java, Python, R)
22 */
23:- system_module( '$_load_foreign', [load_foreign_files/3,
25 open_shared_object/3], ['$import_foreign'/3]).
26
27:- '$do_error'/2use_system_module( '$_errors', []).
28
29:- '$do_import'/3use_system_module( '$_modules', []).
30
31
32
33/**
34
35@defgroup LoadForeign Access to Foreign Language Programs
36@ingroup fli_c_cxx
37
38@{
39
40*/
41
42/** @pred load_foreign_files( _Files_, _Libs_, _InitRoutine_)
43
44should be used, from inside YAP, to load object files produced by the C
45compiler. The argument _ObjectFiles_ should be a list of atoms
46specifying the object files to load, _Libs_ is a list (possibly
47empty) of libraries to be passed to the unix loader (`ld`) and
48InitRoutine is the name of the C routine (to be called after the files
49are loaded) to perform the necessary declarations to YAP of the
50predicates defined in the files.
51
52YAP will search for _ObjectFiles_ in the current directory first. If
53it cannot find them it will search for the files using the environment
54variable:
55
56+ YAPLIBDIR
57
58if defined, or in the default library.
59available as
60YAP supports the SWI-Prolog interface to loading foreign code, the shlib package.
61
62*/
63
64
65load_foreign_files(_Objs,_Libs,Entry) :-
66 '$check_embedded'(Entry),
67 '$check_embedded'.
68load_foreign_files(Objs,Libs,Entry) :-
69 current_source_module(M,M),
70 %G = load_foreign_files(Objs,Libs,Entry),
71 '$absfs'( Objs, [file_type(executable),
72 access(read),
73 expand(true),
74 file_errors(fail)], NewObjs),
75 '$load_libs'( Libs ),
76 '$load_foreign_files'(NewObjs,[],Entry),
77 '$load_foreign_files',
78 prolog_load_context(file, F),
79 ignore( recordzifnot( '$load_foreign_done', [F, M], _) ).
80
81'$absfs'([],_P,[]).
82'$absfs'([F|Fs],P,[NF|NFs]) :-
83 '$name_object'(F, P, NF),
84 '$name_object',
85 '$absfs'(Fs,P,NFs).
86'$absfs'([F|Fs],P,[F|NFs]) :-
87 '$absfs'(Fs,P,NFs).
88
89'$name_object'(I, P, O) :-
90 atom(I),
91 atom,
92 absolute_file_name(foreign(I), O, P).
93'$name_object'(I, P, O) :-
94 absolute_file_name(I, O, P).
95
96'$load_libs'([]).
97'$load_libs'([File|Files]) :-
98 open_shared_object(File, _Handle),
99 '$load_libs'(Files).
100
101/** @pred load_absolute_foreign_files( Files, Libs, InitRoutine)
102
103Loads object files produced by the C compiler. It is useful when no search should be performed and instead one has the full paths to the _Files_ and _Libs_.
104
105*/
106load_absolute_foreign_files(Objs,Libs,Entry) :-
107 current_source_module(M,M),
108 '$load_foreign_files'(Objs,Libs,Entry),
109 '$load_foreign_files',
110 prolog_load_context(file, F),
111 ignore( recordzifnot( '$load_foreign_done', [F, M], _) ).
112
113'$checklib_prefix'(F,F) :- is_absolute_file_name(F), is_absolute_file_name.
114'$checklib_prefix'(F, F) :-
115 sub_atom(F, 0, _, _, lib), sub_atom.
116'$checklib_prefix'(F, Lib) :-
117 atom_concat(lib, F, Lib).
118
119'$import_foreign'(F, M0, M) :-
120 M \= M0,
121 predicate_property(M0:P,built_in),
122 predicate_property(M0:P,file(F)),
123 functor(P, N, K),
124 '$do_import'(N/K-N/K, M0, M),
125 '$do_import'.
126'$import_foreign'(_F, _M0, _M).
127
128/** @pred open_shared_object(+ _File_, - _Handle_)
129
130File is the name of a shared object file (called dynamic load
131library in MS-Windows). This file is attached to the current process
132and _Handle_ is unified with a handle to the library. Equivalent to
133`open_shared_object(File, [], Handle)`. See also
134load_foreign_library/1 and `load_foreign_library/2`.
135
136On errors, an exception `shared_object`( _Action_,
137 _Message_) is raised. _Message_ is the return value from
138dlerror().
139
140
141*/
142open_shared_object(File, Handle) :-
143 open_shared_object(File, [], Handle).
144
145/** @pred open_shared_object(+ _File_, - _Handle_, + _Options_)
146
147As `open_shared_object/2`, but allows for additional flags to
148be passed. _Options_ is a list of atoms. `now` implies the
149symbols are
150resolved immediately rather than lazily (default). `global` implies
151symbols of the loaded object are visible while loading other shared
152objects (by default they are local). Note that these flags may not
153be supported by your operating system. Check the documentation of
154`dlopen()` or equivalent on your operating system. Unsupported
155flags are silently ignored.
156
157
158*/
159open_shared_object(File, Opts, Handle) :-
160 '$open_shared_opts'(Opts, open_shared_object(File, Opts, Handle), OptsI),
161 '$open_shared_object'(File, OptsI, Handle),
162 prolog_load_context(module, M),
163 ignore( recordzifnot( '$foreign', M:'$swi_foreign'(File,Opts, Handle), _) ).
164
165'$open_shared_opts'(Opts, G, _OptsI) :-
166 var(Opts), var,
167 '$do_error'(instantiation_error,G).
168'$open_shared_opts'([], _, 0) :- '$open_shared_opts'.
169'$open_shared_opts'([Opt|Opts], G, V) :-
170 '$open_shared_opts'(Opts, G, V0),
171 '$open_shared_opt'(Opt, G, OptV),
172 V0 is V \/ OptV.
173
174'$open_shared_opt'(Opt, G, _) :-
175 var(Opt), var,
176 '$do_error'(instantiation_error,G).
177'$open_shared_opt'(now, __, 1) :- '$open_shared_opt'.
178'$open_shared_opt'(global, __, 2) :- '$open_shared_opt'.
179'$open_shared_opt'(Opt, Goal, _) :-
180 '$do_error'(domain_error(open_shared_object_option,Opt),Goal).
181
182/** @pred call_shared_object_function(+ _Handle_, + _Function_)
183
184Call the named function in the loaded shared library. The function is
185called without arguments and the return-value is ignored. YAP supports
186installing foreign language predicates using calls to 'UserCCall()`,
187`PL_register_foreign()`, and friends.
188
189 */
190
191call_shared_object_function( Handle, Function) :-
192 '$call_shared_object_function'( Handle, Function),
193 prolog_load_context(module, M),
194 ignore( recordzifnot( '$foreign', M:'$swi_foreign'( Handle, Function ), _) ).
195%% @}
196
197/** @pred $slave is det
198
199Called at boot-time when Prolog is run from another language (eg, Java, Python, Android)
200*/
201
202'$slave' :-
203 getenv( '__PYVENV_LAUNCHER__', _ ),
204 use_module( library(python) ).
absolute_file_name( -File:atom, +Path:atom, +Options:list)
predicate_property( P, Prop)
call_shared_object_function(+ Handle, + Function)
load_absolute_foreign_files( Files, Libs, InitRoutine)
load_foreign_files( Files, Libs, InitRoutine)
open_shared_object(+ File, - Handle)
open_shared_object(+ File, - Handle, + Options)
use_module( +Files )
sub_atom(+ A,? Bef, ? Size, ? After, ? At_out)
ignore(: Goal)
prolog_load_context(? Key, ? Value)
Definition: consult.yap:479
atom( T)
functor( T, F, N)
var( T)