1 % (c) 2022-2026 Lehrstuhl fuer Softwaretechnik und Programmiersprachen,
2 % Heinrich Heine Universitaet Duesseldorf
3 % This software is licenced under EPL 1.0 (http://www.eclipse.org/org/documents/epl-v10.html)
4
5 :- module(pathes_lib, [lib_component_full_path/4,
6 lib_component_available/6,
7 check_lib_component_available/1,
8 unavailable_extension/2,
9 available_extension/1,
10 check_lib_contents/2, check_lib_contents/3,
11 install_lib_component/2,
12 safe_load_foreign_resource/2]).
13
14 % TO DO: interface with lib_component_implements_extension/2
15
16
17 :- use_module(module_information,[module_info/2]).
18 :- module_info(group,typechecker).
19 :- module_info(description,'This module checks the contents of the lib folder and can install some missing components').
20
21 :- use_module(library(file_systems)).
22 :- use_module(library(system)).
23 :- use_module(library(lists)).
24 :- use_module(pathes, [runtime_application_path/1]).
25 :- use_module(pathes_extensions_db, [compile_time_unavailable_extension/2,
26 prob_extension/1, lib_component_implements_extension/4, lib_file_suffix/3]).
27 :- use_module(tools_platform, [host_platform/1, host_processor/1]).
28 :- use_module(tools_printing,[format_with_colour_nl/4]).
29 :- use_module(error_manager).
30 :- use_module(preferences, [get_preference/2]).
31 :- use_module(probsrc(tools_strings),[ajoin/2, ajoin_with_sep/3]).
32 :- use_module(system_call).
33 :- use_module(debug,[debug_format/3]).
34
35 % The predicates in this module can only be called at runtime,
36 % because they check whether certain files are present in the lib directory.
37 % For compile-time checks,
38 % use compile_time_unavailable_extension/2 from the pathes_extensions_db module instead
39 % (possibly with an additional runtime check).
40
41 add_file_suffix(Name,Kind,FullFile) :-
42 host_platform(Platform),
43 lib_file_suffix(Kind,Platform,Suffix),
44 atom_concat(Name,Suffix,FullFile).
45
46 lib_component_full_path(executable,Name,ltsmin_extension,FullFile) :-
47 !,
48 get_preference(path_to_ltsmin,LTSMinRel),
49 runtime_application_path(ProBHome),
50 absolute_file_name(LTSMinRel,LTSMinAbs,[relative_to(ProBHome)]),
51 add_file_suffix(Name,executable,File),
52 absolute_file_name(File,FullFile,[relative_to(LTSMinAbs)]).
53 lib_component_full_path(Kind,Name,_,FullFile) :-
54 add_file_suffix(Name,Kind,File),
55 absolute_file_name(prob_lib(File),FullFile).
56
57 check_lib_component_available(Component) :-
58 lib_component_available(Component,_Kind,_Name,FullFile,_ProBExtension,Available),
59 (Available=true
60 -> add_message(Component,'File for component is available: ',FullFile)
61 ; add_warning(Component,'File for component does not exist: ',FullFile)
62 ).
63
64 lib_component_available(C,Kind,Name,FullFile,ProBExtension,Available) :-
65 lib_component_implements_extension(C,Kind,Name,ProBExtension),
66 lib_component_full_path(Kind,Name,ProBExtension,FullFile),
67 (file_exists(FullFile) -> Available=true ; Available=false).
68
69 unavailable_extension(E, Reason) :- compile_time_unavailable_extension(E, Reason), !.
70 unavailable_extension(E, Reason) :-
71 lib_component_available(_Component, _Kind, _Name, FullFile, E, false),
72 atom_concat('required file missing: ', FullFile, Reason).
73
74 available_extension(E) :-
75 prob_extension(E),
76 \+ unavailable_extension(E,_).
77
78 prob_lib_folder(F) :- absolute_file_name(prob_lib('/'),F).
79
80 check_lib_contents(Stream,Verbose) :-
81 check_lib_contents(Stream,_,Verbose).
82 check_lib_contents(Stream,Comp,Verbose) :-
83 prob_lib_folder(FF),
84 (Verbose=silent -> true ; format_with_colour_nl(Stream,[blue],'Checking contents of lib folder: ~w',[FF])),
85 lib_component_available(Comp,Kind,_Name,_FullFile,ProBExtension,Available),
86 (Available=true
87 -> (Verbose=verbose -> format_with_colour_nl(Stream,[green],'~w (~w) found',[Comp,Kind])
88 ; true
89 )
90 ; format_with_colour_nl(Stream,[red],'~w (~w) not found, ~w not available',[Comp,Kind,ProBExtension]),
91 format_with_colour_nl(Stream,[blue],' * to install component run: probcli --install ~w~n',[Comp])
92 ),
93 fail.
94 check_lib_contents(_,_,_).
95
96
97 % url(Component,Platform,Processor,URL,File,Kind)
98 url(ltsmin,linux,_,
99 'https://github.com/utwente-fmt/ltsmin/releases/download/v3.0.2/','ltsmin-v3.0.2-linux.tgz',tgz).
100 url(ltsmin,darwin,_, % these are Intel binaries; but they run under Rosetta
101 'https://github.com/utwente-fmt/ltsmin/releases/download/v3.0.2/','ltsmin-v3.0.2-osx.tgz',tgz).
102
103 url(ltl2ba,linux,x86_64,'https://stups.hhu-hosting.de/downloads/ltl2ba_pl/sicstus4.9/linux-x86_64/','ltl2ba.so',bundle).
104 url(ltl2ba,darwin,_,'https://stups.hhu-hosting.de/downloads/ltl2ba_pl/sicstus4.9/macos/','ltl2ba.bundle',bundle).
105 url(ltl2ba,windows,x86_64,'https://stups.hhu-hosting.de/downloads/ltl2ba_pl/sicstus4.9/windows-x86_64/','ltl2ba.dll',bundle).
106 url(setlog,_,_,'https://stups.hhu-hosting.de/downloads/setlog_wrapper/','setlog-cli.qlf',qlf).
107 url(z3,darwin,aarch64,Z3,'z3-5.3.3-arm64-osx-13.3.zip',
108 zip('z3-5.3.3-arm64-osx-13.3/bin/libz3.dylib')) :- z3_url(Z3).
109 url(z3,darwin,x86_64,Z3,'z3-5.3.3-x64-osx-13.3.zip',
110 zip('z3-5.3.3-x64-osx-13.3/bin/libz3.dylib')) :- z3_url(Z3).
111 url(z3,windows,x86_64,Z3,'z3-5.3.3-x64-win.zip', % libz3.dll needs the C++ runtime of Visual Studio, which is included
112 zip(['z3-5.3.3-x64-win/bin/libz3.dll','z3-5.3.3-x64-win/bin/msvcp140.dll',
113 'z3-5.3.3-x64-win/bin/vcruntime140.dll','z3-5.3.3-x64-win/bin/vcruntime140_1.dll'])) :- z3_url(Z3).
114 url(z3,linux,x86_64,Z3,'z3-5.3.3-x64-glibc-2.39.zip', % requires glibc 2.38 or later
115 zip('z3-5.3.3-x64-glibc-2.39/bin/libz3.so')) :- z3_url(Z3).
116 url(z3,linux,aarch64,Z3,'z3-5.3.3-arm64-glibc-2.38.zip',
117 zip('z3-5.3.3-arm64-glibc-2.38/bin/libz3.so')) :- z3_url(Z3).
118
119 % tktable universal bundle
120 url(tktable,darwin,_,
121 'https://stups.hhu-hosting.de/buildlibs/tktable/','tktable2.10_macos.zip',zip(all)).
122 % extract all 4 files (libtktable2.10.dylib,license.txt,pkgIndex.tcl,tcltk-license.terms)
123
124 % the shared cvc5 releases contain libcvc5 and the libraries it depends on (libpoly, GMP, MPFR) in their lib folder
125 url(cvc5,darwin,aarch64,URL,'cvc5-macOS-arm64-shared.zip',zip(Files)) :-
126 cvc5_url(URL), cvc5_libs(darwin,'cvc5-macOS-arm64-shared/lib/',Files).
127 url(cvc5,darwin,x86_64,URL,'cvc5-macOS-x86_64-shared.zip',zip(Files)) :-
128 cvc5_url(URL), cvc5_libs(darwin,'cvc5-macOS-x86_64-shared/lib/',Files).
129 url(cvc5,linux,x86_64,URL,'cvc5-Linux-x86_64-shared.zip',zip(Files)) :- % requires glibc 2.33 or later
130 cvc5_url(URL), cvc5_libs(linux,'cvc5-Linux-x86_64-shared/lib/',Files).
131 url(cvc5,linux,aarch64,URL,'cvc5-Linux-arm64-shared.zip',zip(Files)) :-
132 cvc5_url(URL), cvc5_libs(linux,'cvc5-Linux-arm64-shared/lib/',Files).
133
134
135 z3_url('https://github.com/Z3Prover/z3/releases/download/z3-5.3.3/').
136 cvc5_url('https://github.com/cvc5/cvc5/releases/download/cvc5-1.4.1/').
137
138 % cvc5_libs(Platform,FolderInZip,Files): the libraries to extract, named as the extension refers to them
139 % (the install names on macOS, the SONAMEs on Linux)
140 cvc5_libs(Platform,Folder,Files) :-
141 cvc5_lib_names(Platform,Names),
142 prefix_atoms(Names,Folder,Files).
143
144 cvc5_lib_names(darwin,['libcvc5.1.dylib','libpoly.0.dylib','libpolyxx.0.dylib','libmpfr.6.dylib','libgmp.10.dylib']).
145 cvc5_lib_names(linux,['libcvc5.so.1','libpoly.so.0','libpolyxx.so.0','libmpfr.so.6','libgmp.so.10']).
146 % Note: no Windoes rules for cvc5, because DLL built against LLVM's libc++ not compatible with MinGW build
147
148 prefix_atoms([],_,[]).
149 prefix_atoms([Name|T],Prefix,[File|FT]) :- atom_concat(Prefix,Name,File), prefix_atoms(T,Prefix,FT).
150 %ltsmin_url('https://github.com/utwente-fmt/ltsmin/releases').
151
152 url7(Component,Plat,Proc,URL,File,Localfile,Kind) :- Localfile=File,
153 url(Component,Plat,Proc,URL,File,Kind).
154 url7(plantuml,_,_,'https://github.com/plantuml/plantuml/releases/download/v1.2024.8/','plantuml-epl-1.2024.8.jar','plantuml.jar',jar).
155
156 % curl -L https://github.com/utwente-fmt/ltsmin/releases/download/v3.0.2/ltsmin-v3.0.2-osx.tgz -o ~/Desktop/ltsmin-v3.0.2-osx.tgz
157
158 install_lib_component(Component,Opts) :-
159 host_platform(Plat),
160 host_processor(Proc),
161 url7(Component,Plat,Proc,URL,File,Localfile,Kind),!,
162 install_lib_component_aux(Component,URL,File,Localfile,Kind,Opts).
163 install_lib_component(Component,_) :-
164 host_platform(Plat),
165 host_processor(Proc),
166 url(Component,Plat,RProc,_,_,_), % we know the component and there are rules
167 Proc \= RProc,!, % but not for this processor
168 add_error(install_lib_component,'No rules to install for this processor:',Component).
169 install_lib_component(Component,_) :-
170 host_platform(Plat),
171 url(Component,RPlat,_,_,_,_), % we know the component and there are rules
172 Plat \= RPlat,!, % but not for this platform
173 add_error(install_lib_component,'No rules to install on this platform:',Component).
174 install_lib_component(Component,_) :-
175 findall(C,url(C,_,_,_,_,_),Cs), sort(Cs,SCs),
176 ajoin_with_sep(SCs,',',Comps),
177 ajoin(['Cannot install unknown component ',Component,', known components:'],Msg),
178 add_error(install_lib_component,Msg,Comps).
179
180
181 install_lib_component_aux(Component,URL,File,Localfile,Kind,Opts) :-
182 (member(silent,Opts) -> true
183 ; format_with_colour_nl(user_output,[blue],'Installing component ~w from ~w',[Component,URL])),
184 ajoin([URL,File],FullURL),
185 absolute_file_name(prob_lib(Localfile),LocalLibFile),
186 my_system_call('curl',['-L',FullURL,'-o',LocalLibFile],Opts), % will not work on Windows
187 % TODO: we could use curl -I FullURL to detect if file exists; we should get HTTP/1.1 200 OK
188 prob_lib_folder(LibFolder),
189 complete_installation(Component,LibFolder,LocalLibFile,Kind,Opts),
190 !,
191 (member(dryrun,Opts) -> true ; format_with_colour_nl(user_output,green,'Installation complete',[])).
192 install_lib_component_aux(Component,_,_,_,_,_) :-
193 add_error(install_lib_component,'Installation of component failed:',Component).
194
195 % perform steps after downloading file
196 complete_installation(ltsmin,LibFolder,LocalLibFile,_,Opts) :- !,
197 my_system_call('tar',['-xvzf',LocalLibFile,'-C',LibFolder],Opts), % creates v3.0.2 folder
198 ajoin([LibFolder,'/v3.0.2/bin'],LTSMinFolder),
199 move_file(LTSMinFolder,LibFolder,'prob2lts-seq',Opts),
200 move_file(LTSMinFolder,LibFolder,'prob2lts-sym',Opts),
201 move_file(LTSMinFolder,LibFolder,'prob2lts-dist',Opts),
202 move_file(LTSMinFolder,LibFolder,'ltsmin-printtrace',Opts),
203 my_system_call('rm',['-rf',LTSMinFolder],Opts).
204 complete_installation(z3,LibFolder,LocalLibFile,zip(FilesToExtract),Opts) :- !,
205 unzip_files(LibFolder,LocalLibFile,FilesToExtract,Opts),
206 format_with_colour_nl(user_output,[blue],'Make sure that the library in ~w can be found.',[LibFolder]),
207 ld_library_path(LDPATH),
208 format_with_colour_nl(user_output,[blue],'You need to set the environment variable ~w, e.g., as follows:',[LDPATH]),
209 format_with_colour_nl(user_output,[blue],' ~w=~w:$~w probcli -repl',[LDPATH,LibFolder,LDPATH]).
210 complete_installation(_Ext,LibFolder,LocalLibFile,zip(FilesToExtract),Opts) :- !,
211 % the cvc5 extension finds the libraries in the lib folder via its rpath, no environment variable is needed
212 unzip_files(LibFolder,LocalLibFile,FilesToExtract,Opts).
213 complete_installation(_,_,_,_,_). % nothing to do
214
215 % extract a single file or a list of files from a zip archive into LibFolder and delete the archive
216 unzip_files(LibFolder,ZipFile,FilesToExtract,Opts) :-
217 (FilesToExtract = [_|_] -> Files = FilesToExtract
218 ; FilesToExtract = all -> Files = [] ; Files = [FilesToExtract]),
219 append(['-j', '-o', '-d', LibFolder, ZipFile],Files,Args), % -j: junk paths, -o: overwrite existing files
220 my_system_call('unzip',Args,Opts),
221 my_system_call('rm',['-rf',ZipFile],Opts).
222
223 ld_library_path(Path) :- host_platform(P), ld_library_path(P,Path).
224 ld_library_path(linux,'LD_LIBRARY_PATH').
225 ld_library_path(darwin,'DYLD_LIBRARY_PATH').
226 ld_library_path(windows,'PATH').
227
228 move_file(From,To,File,Opts) :-
229 ajoin([From,'/',File],FF), %path join
230 my_system_call('mv',[FF,To],Opts).
231
232 my_system_call(Cmd,Options,Opts) :-
233 ajoin_with_sep(Options,' ',OL),
234 (member(dryrun,Opts)
235 -> format('~w ~w~n',[Cmd,OL])
236 ; absolute_file_name(path(Cmd),
237 FullCmd,
238 [access(exist),extensions(['.exe','']),solutions(all),file_errors(fail)])
239 ->
240 format_with_colour_nl(user_output,[blue],' --> ~w ~w',[FullCmd,OL]),
241 system_call(FullCmd,Options,_OutputText,ErrText,Exit),
242 (Exit=exit(0) -> true
243 ; format_with_colour_nl(user_error,[red],'~w command failed (~w) : ~s',[Cmd,Exit,ErrText]),
244 fail
245 )
246 ; add_error(pathes_lib,'Cannot execute command:',Cmd),
247 format_with_colour_nl(user_error,[blue],'Try executing the command by hand:~n ~w ~w~n',[Cmd,OL]),
248 fail
249 ).
250
251 % --------------------
252
253 safe_load_foreign_resource(SourceModule,Lib) :-
254 % print(loading_foreign_resource(Lib)),nl,flush_output,
255 catch(safe_load_foreign_resource2(SourceModule,Lib), E, (
256 host_processor(Proc),
257 (host_platform(darwin) -> host_processor(Proc),
258 ajoin(['Could not load library ',Lib,
259 ', be sure to set LD_LIBRARY_PATH to include the lib folder of ProB for the right architecture (',
260 Proc,'). Exception:'],Msg)
261 ; host_platform(linux) ->
262 ajoin(['Could not load library ',Lib,
263 ', be sure to set LD_LIBRARY_PATH to include the lib folder of ProB on Linux. Exception:'],Msg)
264 ; ajoin(['Could not load library ',Lib, '. Exception:'],Msg)
265 ),
266 safe_add_error(Lib,Msg,E),
267 fail
268 )).
269
270 % check for existence error
271 % we need to prefix load_foreign_resource with the right module
272 safe_load_foreign_resource2(SourceModule,Lib) :-
273 E=error(existence_error(_,_),_),
274 debug_format(9,'Loading foreign resource module ~w from ~w~n',[SourceModule,Lib]),
275 flush_output, % flush output in case of crash (e.g., Killed: 9 on macOS)
276 catch(SourceModule:load_foreign_resource(library(Lib)), E, (
277 prob_lib_folder(LibF),
278 ajoin(['Could not load library ',Lib,
279 ', check that it is available in the lib folder ',LibF,
280 ' of ProB (you can run probcli -check_lib). Exception:'],Msg),
281 safe_add_error(Lib,Msg,E),
282 check_lib_component_available(Lib),
283 fail
284 )).
285
286 safe_add_error(Lib,Msg,E) :-
287 E2=error(existence_error(_,_),_),
288 catch(error_manager:add_error(Lib,Msg,E), E2, % maybe because counter library could not be loaded
289 format_with_colour_nl(user_error,[red],'~w ~w~n',[Msg,E])).