| 1 |
open Dune_site.Private_ |
| 2 |
module Data = Dune_site_plugins_data |
| 3 |
|
| 4 |
let meta_fn = "META" |
| 5 |
|
| 6 |
let readdir = |
| 7 |
let ( / ) = Filename.concat in |
| 8 |
let readdir_noexn dir = |
| 9 |
try Sys.readdir dir with |
| 10 |
| Sys_error _ -> [||] |
| 11 |
in |
| 12 |
fun dirs -> |
| 13 |
List.concat |
| 14 |
(List.map |
| 15 |
(fun dir -> |
| 16 |
List.filter |
| 17 |
(fun entry -> Sys.file_exists (dir / entry / meta_fn)) |
| 18 |
(Array.to_list (readdir_noexn dir))) |
| 19 |
dirs) |
| 20 |
;; |
| 21 |
|
| 22 |
let rec lookup dirs file = |
| 23 |
match dirs with |
| 24 |
| [] -> None |
| 25 |
| dir :: dirs -> |
| 26 |
let file' = Filename.concat dir file in |
| 27 |
if Sys.file_exists file' then Some file' else lookup dirs file |
| 28 |
;; |
| 29 |
|
| 30 |
module type S = sig |
| 31 |
val paths : string list |
| 32 |
val list : unit -> string list |
| 33 |
val load_all : unit -> unit |
| 34 |
val load : string -> unit |
| 35 |
end |
| 36 |
|
| 37 |
let rec check_predicates predicates = |
| 38 |
match Sys.backend_type, predicates with |
| 39 |
| _, [] -> true |
| 40 |
| Sys.Native, Meta_parser.Pos "byte" :: _ -> false |
| 41 |
| Sys.Bytecode, Meta_parser.Pos "native" :: _ -> false |
| 42 |
| Sys.Native, Meta_parser.Pos "native" :: predicates -> check_predicates predicates |
| 43 |
| Sys.Bytecode, Meta_parser.Pos "byte" :: predicates -> check_predicates predicates |
| 44 |
| Sys.Native, Meta_parser.Neg "native" :: _ -> false |
| 45 |
| Sys.Bytecode, Meta_parser.Neg "byte" :: _ -> false |
| 46 |
| Sys.Native, Meta_parser.Neg "byte" :: predicates -> check_predicates predicates |
| 47 |
| Sys.Bytecode, Meta_parser.Neg "native" :: predicates -> check_predicates predicates |
| 48 |
| _, Meta_parser.Pos pred :: predicates -> |
| 49 |
Data.findlib_predicates_set_by_dune pred && check_predicates predicates |
| 50 |
| _, Meta_parser.Neg pred :: predicates -> |
| 51 |
(not (Data.findlib_predicates_set_by_dune pred)) && check_predicates predicates |
| 52 |
;; |
| 53 |
|
| 54 |
let check_predicates_with_plugin predicates = |
| 55 |
let rec aux predicates has_plugin acc = |
| 56 |
match predicates with |
| 57 |
| [] -> has_plugin && check_predicates acc |
| 58 |
| Meta_parser.Pos "plugin" :: predicates -> aux predicates true acc |
| 59 |
| predicate :: predicates -> aux predicates has_plugin (predicate :: acc) |
| 60 |
in |
| 61 |
aux predicates false [] |
| 62 |
;; |
| 63 |
|
| 64 |
let rec get_plugin plugins requires entries = |
| 65 |
match entries with |
| 66 |
| [] -> List.rev plugins, List.rev requires |
| 67 |
| Meta_parser.Comment _ :: entries -> get_plugin plugins requires entries |
| 68 |
| Package _ :: entries -> get_plugin plugins requires entries |
| 69 |
| Rule { var = "plugin"; predicates; action = Set; value } :: entries |
| 70 |
when check_predicates predicates -> get_plugin [ value ] requires entries |
| 71 |
| Rule { var = "plugin"; predicates; action = Add; value } :: entries |
| 72 |
when check_predicates predicates -> get_plugin (value :: plugins) requires entries |
| 73 |
(* archive(native|byte,plugin) is the way used in the wild before findlib |
| 74 |
supported plugins *) |
| 75 |
| Rule { var = "archive"; predicates; action = Set; value } :: entries |
| 76 |
when check_predicates_with_plugin predicates -> get_plugin [ value ] requires entries |
| 77 |
| Rule { var = "archive"; predicates; action = Add; value } :: entries |
| 78 |
when check_predicates_with_plugin predicates -> |
| 79 |
get_plugin (value :: plugins) requires entries |
| 80 |
| Rule { var = "requires"; predicates; action = Set; value } :: entries |
| 81 |
when check_predicates predicates -> get_plugin plugins [ value ] entries |
| 82 |
| Rule { var = "requires"; predicates; action = Add; value } :: entries |
| 83 |
when check_predicates predicates -> get_plugin plugins (value :: requires) entries |
| 84 |
| Rule _ :: entries -> get_plugin plugins requires entries |
| 85 |
;; |
| 86 |
|
| 87 |
exception Thread_library_required_by_plugin_but_not_required_by_main_executable |
| 88 |
|
| 89 |
exception |
| 90 |
Library_not_found of |
| 91 |
{ search_paths : string list |
| 92 |
; prefix : string list |
| 93 |
; name : string |
| 94 |
} |
| 95 |
|
| 96 |
exception |
| 97 |
Plugin_not_found of |
| 98 |
{ search_paths : string list |
| 99 |
; name : string |
| 100 |
} |
| 101 |
|
| 102 |
let () = |
| 103 |
Printexc.register_printer (function |
| 104 |
| Thread_library_required_by_plugin_but_not_required_by_main_executable -> |
| 105 |
Some |
| 106 |
(Format.asprintf |
| 107 |
"%a" |
| 108 |
Format.pp_print_text |
| 109 |
"It is not possible to dynamically link a plugin which uses the thread \ |
| 110 |
library with an executable not already linked with the thread library.") |
| 111 |
| Plugin_not_found { search_paths; name } -> |
| 112 |
Some |
| 113 |
(Format.sprintf |
| 114 |
"The plugin %S can't be found in the search paths %S." |
| 115 |
name |
| 116 |
(String.concat ":" search_paths)) |
| 117 |
| Library_not_found { search_paths; prefix = []; name } -> |
| 118 |
Some |
| 119 |
(Format.sprintf |
| 120 |
"The library %S can't be found in the search paths %S." |
| 121 |
name |
| 122 |
(String.concat ":" search_paths)) |
| 123 |
| Library_not_found { search_paths; prefix; name } -> |
| 124 |
Some |
| 125 |
(Format.sprintf |
| 126 |
"The sub-library %S can't be found in the library %s in the search paths %S." |
| 127 |
name |
| 128 |
(String.concat "." prefix) |
| 129 |
(String.concat ":" search_paths)) |
| 130 |
| _ -> None) |
| 131 |
;; |
| 132 |
|
| 133 |
let rec find_library ~dirs ~prefix ~suffix directory meta = |
| 134 |
let rec find_directory directory = function |
| 135 |
| [] -> directory |
| 136 |
| Meta_parser.Rule { var = "directory"; predicates = []; action = Set; value } :: _ -> |
| 137 |
(match directory with |
| 138 |
| None -> Some value |
| 139 |
| Some old -> Some (Filename.concat old value)) |
| 140 |
| _ :: entries -> find_directory directory entries |
| 141 |
in |
| 142 |
match suffix with |
| 143 |
| [] -> find_directory directory meta, meta |
| 144 |
| pkg :: suffix -> |
| 145 |
let directory = find_directory directory meta in |
| 146 |
let rec aux pkg = function |
| 147 |
| [] -> |
| 148 |
raise |
| 149 |
(Library_not_found { search_paths = dirs; prefix = List.rev prefix; name = pkg }) |
| 150 |
| Meta_parser.Package { name = Some name; entries } :: _ when String.equal name pkg |
| 151 |
-> find_library ~dirs ~prefix:(pkg :: prefix) ~suffix directory entries |
| 152 |
| _ :: entries -> aux pkg entries |
| 153 |
in |
| 154 |
aux pkg meta |
| 155 |
;; |
| 156 |
|
| 157 |
let extract_words s ~is_word_char = |
| 158 |
let rec skip_blanks i = |
| 159 |
if i = String.length s |
| 160 |
then [] |
| 161 |
else if is_word_char s.[i] |
| 162 |
then parse_word i (i + 1) |
| 163 |
else skip_blanks (i + 1) |
| 164 |
and parse_word i j = |
| 165 |
if j = String.length s |
| 166 |
then [ StringLabels.sub s ~pos:i ~len:(j - i) ] |
| 167 |
else if is_word_char s.[j] |
| 168 |
then parse_word i (j + 1) |
| 169 |
else StringLabels.sub s ~pos:i ~len:(j - i) :: skip_blanks (j + 1) |
| 170 |
in |
| 171 |
skip_blanks 0 |
| 172 |
;; |
| 173 |
|
| 174 |
let extract_comma_space_separated_words s = |
| 175 |
extract_words s ~is_word_char:(function |
| 176 |
| ',' | ' ' | '\t' | '\n' -> false |
| 177 |
| _ -> true) |
| 178 |
;; |
| 179 |
|
| 180 |
let split_all l = List.concat (List.map extract_comma_space_separated_words l) |
| 181 |
|
| 182 |
let find_plugin ~dirs ~dir ~suffix (meta : Meta_parser.t) = |
| 183 |
let directory, meta = |
| 184 |
find_library ~dirs ~prefix:(Option.to_list meta.name) ~suffix None meta.entries |
| 185 |
in |
| 186 |
let plugins, requires = get_plugin [] [] meta in |
| 187 |
let directory = |
| 188 |
match directory with |
| 189 |
| None -> dir |
| 190 |
| Some pkg_dir -> |
| 191 |
if pkg_dir.[0] = '+' || pkg_dir.[0] = '^' |
| 192 |
then |
| 193 |
Filename.concat |
| 194 |
(Lazy.force Helpers.stdlib) |
| 195 |
(String.sub pkg_dir 1 (String.length pkg_dir - 1)) |
| 196 |
else if Filename.is_relative pkg_dir |
| 197 |
then Filename.concat dir pkg_dir |
| 198 |
else pkg_dir |
| 199 |
in |
| 200 |
let plugins = split_all plugins in |
| 201 |
let requires = split_all requires in |
| 202 |
directory, plugins, requires |
| 203 |
;; |
| 204 |
|
| 205 |
let load file ~pkg = |
| 206 |
let entries = |
| 207 |
let ic = open_in file in |
| 208 |
try |
| 209 |
let lb = Lexing.from_channel ic in |
| 210 |
lb.lex_curr_p <- { pos_fname = file; pos_lnum = 1; pos_bol = 0; pos_cnum = 0 }; |
| 211 |
let r = Meta_parser.Parse.entries lb 0 [] in |
| 212 |
close_in ic; |
| 213 |
r |
| 214 |
with |
| 215 |
| exn -> |
| 216 |
close_in ic; |
| 217 |
raise exn |
| 218 |
in |
| 219 |
{ Meta_parser.name = Some pkg; entries } |
| 220 |
;; |
| 221 |
|
| 222 |
let lookup_and_load_one_dir ~dir ~pkg = |
| 223 |
let meta_file = Filename.concat dir meta_fn in |
| 224 |
if Sys.file_exists meta_file |
| 225 |
then Some (load meta_file ~pkg) |
| 226 |
else ( |
| 227 |
(* Alternative layout *) |
| 228 |
let dir = Filename.dirname dir in |
| 229 |
let meta_file = Filename.concat dir (meta_fn ^ "." ^ pkg) in |
| 230 |
if Sys.file_exists meta_file then Some (load meta_file ~pkg) else None) |
| 231 |
;; |
| 232 |
|
| 233 |
let split ~dirs name = |
| 234 |
match String.split_on_char '.' name with |
| 235 |
| [] -> raise (Library_not_found { search_paths = dirs; prefix = []; name }) |
| 236 |
| pkg :: rest -> pkg, rest |
| 237 |
;; |
| 238 |
|
| 239 |
let lookup_and_summarize alldirs name = |
| 240 |
let pkg, suffix = split ~dirs:alldirs name in |
| 241 |
let rec loop dirs = |
| 242 |
match dirs with |
| 243 |
| [] -> |
| 244 |
List.assoc_opt pkg Data.builtin_library |
| 245 |
|> (function |
| 246 |
| None -> raise (Library_not_found { search_paths = alldirs; prefix = []; name }) |
| 247 |
| Some meta -> |
| 248 |
find_plugin ~dirs:alldirs ~dir:(Lazy.force Helpers.stdlib) ~suffix meta) |
| 249 |
| dir :: dirs -> |
| 250 |
let dir = Filename.concat dir pkg in |
| 251 |
(match lookup_and_load_one_dir ~dir ~pkg with |
| 252 |
| None -> loop dirs |
| 253 |
| Some p -> find_plugin ~dirs:alldirs ~dir ~suffix p) |
| 254 |
in |
| 255 |
loop alldirs |
| 256 |
;; |
| 257 |
|
| 258 |
let loaded_libraries = |
| 259 |
lazy |
| 260 |
(let h = Hashtbl.create 10 in |
| 261 |
List.iter (fun s -> Hashtbl.add h s ()) Data.already_linked_libraries; |
| 262 |
h) |
| 263 |
;; |
| 264 |
|
| 265 |
let load_gen ~load_requires dirs name = |
| 266 |
let loaded_libraries = Lazy.force loaded_libraries in |
| 267 |
if not (Hashtbl.mem loaded_libraries name) |
| 268 |
then ( |
| 269 |
if name = "threads" |
| 270 |
then raise Thread_library_required_by_plugin_but_not_required_by_main_executable; |
| 271 |
Hashtbl.add loaded_libraries name (); |
| 272 |
let directory, plugins, requires = lookup_and_summarize dirs name in |
| 273 |
List.iter load_requires requires; |
| 274 |
List.iter |
| 275 |
(fun p -> |
| 276 |
let file = Filename.concat directory p in |
| 277 |
Dune_site_backend.Linker.load file) |
| 278 |
plugins) |
| 279 |
;; |
| 280 |
|
| 281 |
let rec load_requires name = load_gen ~load_requires (Lazy.force Helpers.ocamlpath) name |
| 282 |
|
| 283 |
let load_plugin plugin_paths name = |
| 284 |
match lookup plugin_paths (Filename.concat name meta_fn) with |
| 285 |
| None -> raise (Plugin_not_found { search_paths = plugin_paths; name }) |
| 286 |
| Some meta_file -> |
| 287 |
let meta = load meta_file ~pkg:name in |
| 288 |
let plugins, requires = get_plugin [] [] meta.entries in |
| 289 |
assert (plugins = []); |
| 290 |
let requires = split_all requires in |
| 291 |
List.iter load_requires requires |
| 292 |
;; |
| 293 |
|
| 294 |
module Make (X : sig |
| 295 |
val paths : string list |
| 296 |
end) : S = struct |
| 297 |
include X |
| 298 |
|
| 299 |
let list () = List.sort String.compare (readdir paths) |
| 300 |
let load name = load_plugin paths name |
| 301 |
let load_all () = List.iter load (list ()) |
| 302 |
end |
| 303 |
|
| 304 |
let load = load_requires |
| 305 |
|
| 306 |
let available name = |
| 307 |
Hashtbl.mem (Lazy.force loaded_libraries) name |
| 308 |
|| |
| 309 |
let ocamlpath = Lazy.force Helpers.ocamlpath in |
| 310 |
try |
| 311 |
ignore (lookup_and_summarize ocamlpath name); |
| 312 |
true |
| 313 |
with |
| 314 |
| _ -> |
| 315 |
(* CR - What exceptions are being swallowed here? *) |
| 316 |
false |
| 317 |
;; |