Source file ocsigen_loader.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
open Ocsigen_lib
exception Dynlink_error of string * exn
exception Findlib_error of string * exn
let section = Lwt_log.Section.make "ocsigen:dynlink"
let translate =
if Ocsigen_config_static.is_native then
fun filename ->
if Filename.check_suffix filename ".cmo" ||
Filename.check_suffix filename ".cma" then
(Filename.chop_extension filename) ^ ".cmxs"
else filename
else
fun filename ->
if Filename.check_suffix filename ".cmxs" then
let filename = Filename.chop_extension filename in
let cma = filename ^ ".cma" in
if Sys.file_exists cma then cma else filename ^ ".cmo"
else filename
let isloaded, addloaded =
let set = ref String.Set.empty in
((fun s -> String.Set.mem s !set),
(fun s -> set := String.Set.add s !set))
module M = Map.Make(String)
let init_functions = ref M.empty
let get_init_on_load, set_init_on_load =
let init_on_load = ref false in
((fun () -> !init_on_load), (fun b -> init_on_load := b))
let loadfile pre post force file =
let file = translate file in
try
if force then begin
pre ();
Lwt_log.ign_info_f ~section "Loading %s (will be reloaded every times)" file;
begin try
Dynlink_wrapper.loadfile file; post ()
with e ->
post (); raise e
end
end
else if not (isloaded file) then begin
pre ();
Lwt_log.ign_info_f ~section "Loading extension %s" file;
begin try
Dynlink_wrapper.loadfile file; post ()
with e ->
post (); raise e
end;
addloaded file;
end
else
Lwt_log.ign_info_f ~section "Extension %s already loaded" file
with
| e -> raise (Dynlink_error (file, e))
let id () = ()
let loadfiles pre post force modules =
let rec aux = function
| [] -> ()
| [m] -> loadfile pre post force m
| m::q -> loadfile id id false m; aux q
in aux modules
let set_module_init_function name f =
init_functions := M.add name f !init_functions;
if get_init_on_load () then f ()
let init_module pre post force name =
let f =
try
M.find name !init_functions
with Not_found as e ->
raise (Dynlink_error ("named module " ^ name, e))
in try
if force then begin
pre ();
Lwt_log.ign_info_f ~section "Initializing %s (will be initialized every time)" name;
begin try
f (); post ()
with e ->
post (); raise e
end
end
else if not (isloaded name) then begin
pre ();
Lwt_log.ign_info_f ~section "Initializing module %s " name;
begin try
f (); post ()
with e ->
post (); raise e
end;
addloaded name;
end
else
Lwt_log.ign_info_f ~section "Module %s already initialized." name
with
| e -> raise (Dynlink_error (name, e))
let () = Findlib.init ()
let ocsigen_search_path = ref []
let update_search_path () =
match !ocsigen_search_path with
| [] -> Findlib.init ()
| x -> Findlib.init ~env_ocamlpath:(String.concat ":" x) ()
let get_ocamlpath = Findlib.search_path
let set_ocamlpath lp =
ocsigen_search_path := lp;
update_search_path ()
let add_ocamlpath p =
ocsigen_search_path := p :: !ocsigen_search_path;
update_search_path ()
let findfiles =
let cmx = Pcre.regexp ~flags:[`MULTILINE; `CASELESS] "\\.cmx($| |a)" in
fun package ->
try
let preds = [
(if Ocsigen_config_static.is_native then "native" else "byte");
"plugin";
"mt"
] in
let deps =
List.filter
(fun a -> not @@
String.Set.mem a Ocsigen_config_static.builtin_packages)
(Findlib.package_deep_ancestors preds [package])
in
Lwt_log.ign_info_f ~section
"Dependencies of %s: %s" package (String.concat ", " deps);
let rec aux = function
| [] -> []
| a::q ->
let mods =
try
let raw = Findlib.package_property preds a "archive" in
let raw =
Ocsigen_lib.Netstring_pcre.global_replace
cmx ".cmxs " raw
in
List.filter ((<>) "") (String.split ~multisep:true ' ' raw)
with
| Not_found -> []
in
let base = Findlib.package_directory a in
(List.map (Findlib.resolve_path ~base) mods) @ (aux q)
in
let res = aux deps in
Lwt_log.ign_info_f ~section "Needed: %s" (String.concat ", " res);
res
with
| e -> raise (Findlib_error (package, e))
open Printf
let () = Printexc.register_printer @@
function
| Dynlink_wrapper.Error e -> Some (Dynlink_wrapper.error_message e)
| Dynlink_error (s, e) ->
Some (sprintf "Dynlink error while loading %s: %s" s (Printexc.to_string e))
| Findlib_error (s, Fl_package_base.No_such_package (s', msg)) ->
let pkg =
if s = s' then s else sprintf "%s [while trying to load %s]" s' s
in
let additional = if msg = "" then "" else sprintf " (%s)" msg in
let msg =
sprintf
"Findlib package %s not found%s: maybe you forgot <findlib path=\"...\"/>?"
pkg additional
in
Some msg
| Findlib_error (s, e) ->
Some (sprintf "Findlib error while handling %s: %s" s (Printexc.to_string e))
| _ -> None