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 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
|
open Fugue
open Filepath
open Printf
module Predicate = struct
type t =
| Byte
| Native
| Toploop
| CreateToploop
| Plugin
| Mt
| Mt_vm
| Mt_posix
| Gprof
| Autolink
| Syntax
| Preprocessor
| Camlp4o
| Camlp4r
| Ppx_driver
| Neg of t
| Unknown of string
let rec to_string = function
| Byte -> "byte"
| Native -> "native"
| Toploop -> "toploop"
| CreateToploop -> "create_toploop"
| Plugin -> "plugin"
| Mt -> "mt"
| Mt_vm -> "mt_vm"
| Mt_posix -> "mt_posix"
| Gprof -> "gprof"
| Autolink -> "autolink"
| Syntax -> "syntax"
| Preprocessor -> "preprocessor"
| Camlp4o -> "camlp4o"
| Camlp4r -> "camlp4r"
| Ppx_driver -> "ppx_driver"
| Neg t -> "-" ^ to_string t
| Unknown s -> s
let rec of_string s =
if s.[0] = '-' then
Neg (of_string (String.sub s 1 (String.length s - 1)))
else
match
s
with
| "byte" -> Byte
| "native" -> Native
| "toploop" -> Toploop
| "create_toploop" -> CreateToploop
| "plugin" -> Plugin
| "mt" -> Mt
| "mt_vm" -> Mt_vm
| "mt_posix" -> Mt_posix
| "gprof" -> Gprof
| "autolink" -> Autolink
| "syntax" -> Syntax
| "preprocessor" -> Preprocessor
| "camlp4o" -> Camlp4o
| "camlp4r" -> Camlp4r
| "ppx_driver" -> Ppx_driver
| _ as s -> Unknown s
end
exception LibraryNotFound of string
exception SubpackageNotFound of string
exception ArchiveNotFound of filepath * Libname.t * Predicate.t list
exception MetaParseError of filepath * string
module Pkg = struct
(* preliminaries structures, adjust as needed by meta. *)
type t = {
name : string;
requires : (Predicate.t list * Libname.t list) list;
directory : string;
description : string;
exists_if : string;
preprocessor : string;
ppx : (Predicate.t list * string) option;
ppxopt : (Predicate.t list * string) list;
browse_interface : string;
type_of_threads : string;
archives : (Predicate.t list * string) list;
warning : (Predicate.t list * string) list;
append_archives : (Predicate.t list * string) list;
version : string;
assignment : (string * string) list;
linkopts : (Predicate.t list option * string) list;
subs : t list;
}
type meta_t = Filepath.filepath * t
let make name =
{
name;
requires = [];
directory = "";
description = "";
preprocessor = "";
ppx = None;
ppxopt = [];
linkopts = [];
browse_interface = "";
type_of_threads = "";
exists_if = "";
archives = [];
append_archives = [];
warning = [];
version = "";
assignment = [];
subs = [];
}
let rec iter f package =
f package;
List.iter (iter f) package.subs
let rec find subs pkg =
match subs with
| [] -> pkg
| x :: xs ->
find xs
(try List.find (fun spkg -> spkg.name = x) pkg.subs
with Not_found -> raise (SubpackageNotFound x))
let get_syntaxes pkg =
list_filter_map
(fun (preds, s) ->
if List.mem Predicate.Syntax preds then
Some (list_remove Predicate.Syntax preds, s)
else
None)
pkg.archives
let satisfy preds constraints =
List.for_all
(fun p ->
match p with
| Predicate.Neg n -> not (List.mem n constraints)
| _ -> List.mem p constraints)
preds
let is_syntax_ pkg = get_syntaxes pkg <> []
let is_syntax (_, rootPkg) dep = is_syntax_ (find dep.Libname.subnames rootPkg)
let get_archive_with_filter (_, rootPkg) dep preds =
let pkg = find dep.Libname.subnames rootPkg in
let preds_set = Hashtbl.create (List.length preds) in
List.iter (fun p -> Hashtbl.replace preds_set p ()) preds;
let fulfills archive_preds =
List.for_all
(fun p ->
match p with
| Predicate.Neg n -> not (Hashtbl.mem preds_set n)
| _ -> Hashtbl.mem preds_set p)
archive_preds
in
let rec best_archive best_n best_value archives =
match archives with
| [] -> if best_n >= 0 then [ best_value ] else []
| ((archive_preds, _) as archive) :: rest ->
if fulfills archive_preds && List.length archive_preds > best_n then
best_archive (List.length archive_preds) archive rest
else
best_archive best_n best_value rest
in
let rec all_append_archives archives =
match archives with
| [] -> []
| ((archive_preds, _) as archive) :: rest ->
if fulfills archive_preds then
archive :: all_append_archives rest
else
all_append_archives rest
in
let res =
if pkg.archives = [] then
[]
else
best_archive (-1) (List.hd pkg.archives) pkg.archives
in
res @ all_append_archives pkg.append_archives
let get_archive (path, root) dep preds =
let pkg = find dep.Libname.subnames root in
try snd (List.find (fun (e, _) -> list_eq_noorder e preds) pkg.archives)
with Not_found -> raise (ArchiveNotFound (path, dep, preds))
let write path package =
let out = Buffer.create 1024 in
let append = Buffer.add_string out in
let preds_to_string preds =
if preds = [] then
""
else
"(" ^ String.concat "," (List.map Predicate.to_string preds) ^ ")"
in
let rec write_one indent pkg =
let indent_str = String.make indent ' ' in
let output_field field name =
if field <> "" then
append (sprintf "%s%s = \"%s\"\n" indent_str name field)
in
output_field pkg.description "description";
output_field pkg.version "version";
output_field pkg.browse_interface "browse_interface";
output_field pkg.exists_if "exists_if";
List.iter
(fun (preds, deps) ->
let dep_str = String.concat "," (List.map (fun dep -> Libname.to_string dep) deps) in
append (sprintf "%srequires%s = \"%s\"\n" indent_str (preds_to_string preds) dep_str))
pkg.requires;
List.iter
(fun (preds, v) ->
append (sprintf "%sarchive%s = \"%s\"\n" indent_str (preds_to_string preds) v))
pkg.archives;
List.iter
(fun (preds, v) ->
append (sprintf "%sarchive%s += \"%s\"\n" indent_str (preds_to_string preds) v))
pkg.append_archives;
List.iter
(fun (preds_opt, v) ->
let preds_str = match preds_opt with
| None -> ""
| Some preds -> preds_to_string preds
in
append (sprintf "%slinkopts%s = \"%s\"\n" indent_str preds_str v))
pkg.linkopts;
List.iter
(fun spkg ->
append (sprintf "%spackage \"%s\" (\n" indent_str spkg.name);
write_one (indent + 2) spkg;
append (sprintf "%s)\n" indent_str))
pkg.subs
in
write_one 0 package;
Filesystem.write_file path (Buffer.contents out)
end
type t = filepath * Pkg.t
let path_warning = ref false
(** META File Tokenizer and Parser
This module handles tokenizing and parsing OCamlfind META files. META files describe OCaml
package metadata including dependencies, compilation flags, and library locations. *)
module Token = struct
(** Token types for META file lexer *)
type t =
| ID of string
| S of string
| LPAREN
| RPAREN
| MINUS
| DOT
| EQ
| PLUSEQ
| COMMA
let to_string = function
| ID s -> "ID[" ^ s ^ "]"
| S s -> "\"" ^ s ^ "\""
| LPAREN -> "("
| RPAREN -> ")"
| MINUS -> "-"
| DOT -> "."
| EQ -> "="
| PLUSEQ -> "+="
| COMMA -> ","
let char_table =
hashtbl_from_list
[ ('(', LPAREN); (')', RPAREN); ('=', EQ); (',', COMMA); ('.', DOT); ('-', MINUS) ]
let is_token_char c = Hashtbl.mem char_table c
let get_token_char c = Hashtbl.find char_table c
let is_ident_char c = char_is_alphanum c || c == '_' || c == '-'
(** Tokenize META file content
Converts META file string into a list of tokens. Handles comments, strings, identifiers, and
operators.
@param name Package name (for error messages)
@param s META file content
@return List of tokens *)
let tokenize name s =
let line = ref 1 in
let lineoff = ref 0 in
let len = String.length s in
let eat_comment o =
let i = ref (o + 1) in
while !i < len && s.[!i] <> '\n' do
i := !i + 1
done;
line := !line + 1;
lineoff := !i + 1;
!i + 1
in
let parse_ident o =
let i = ref (o + 1) in
while !i < len && is_ident_char s.[!i] do
i := !i + 1
done;
(String.sub s o (!i - o), !i)
in
let parse_string o =
let i = ref (o + 1) in
let buf = Buffer.create 32 in
let in_escape = ref false in
while !i < len && (!in_escape || s.[!i] <> '"') do
(if (not !in_escape) && s.[!i] = '\\' then
in_escape := true
else
let c =
if !in_escape then
match
s.[!i]
with
| '\\' -> '\\'
| 'n' -> '\n'
| 't' -> '\t'
| 'r' -> '\r'
| '"' -> '"'
| _ -> s.[!i]
else
s.[!i]
in
in_escape := false;
Buffer.add_char buf c);
i := !i + 1
done;
(* Check if string was properly closed *)
if !i >= len then
let s = sprintf "%d.%d: meta lexing error: unclosed string literal" !line (o - !lineoff) in
raise (MetaParseError (name, s))
else
(Buffer.contents buf, !i + 1)
in
let rec loop o =
if o >= len then
[]
else if s.[o] == ' ' || s.[o] == '\t' then
loop (o + 1)
else if s.[o] == '\n' then (
line := !line + 1;
lineoff := o + 1;
loop (o + 1))
else if s.[o] == '#' then
loop (eat_comment o)
else if s.[o] == '"' then
let s, no = parse_string o in
S s :: loop no
else if is_token_char s.[o] then
get_token_char s.[o] :: loop (o + 1)
else if s.[o] == '+' && o + 1 < len && s.[o + 1] == '=' then
PLUSEQ :: loop (o + 2)
else if (s.[o] >= 'a' && s.[o] <= 'z') || (s.[o] >= 'A' && s.[o] <= 'Z') || s.[o] == '-' then
let id, no = parse_ident o in
ID id :: loop no
else
let s =
sprintf "%d.%d: meta lexing error: undefined character '%c'" !line (o - !lineoff) s.[o]
in
raise (MetaParseError (name, s))
in
loop 0
let rec parse_predicate = function
| COMMA :: ID s :: xs ->
let l, r = parse_predicate xs in
(Predicate.of_string s :: l, r)
| COMMA :: MINUS :: ID s :: xs ->
let l, r = parse_predicate xs in
(Predicate.Neg (Predicate.of_string s) :: l, r)
| xs -> ([], xs)
let parse_predicate_list name field = function
| LPAREN :: RPAREN :: xs -> ([], xs)
| LPAREN :: ID s :: xs -> (
let preds, xs2 = parse_predicate xs in
match xs2 with
| RPAREN :: xs3 -> (Predicate.of_string s :: preds, xs3)
| _ -> raise (MetaParseError (name, "expecting ')' after " ^ field ^ "'s predicate")))
| LPAREN :: MINUS :: ID s :: xs -> (
let preds, xs2 = parse_predicate xs in
match xs2 with
| RPAREN :: xs3 -> (Predicate.Neg (Predicate.of_string s) :: preds, xs3)
| _ -> raise (MetaParseError (name, "expecting ')' after " ^ field ^ "'s predicate")))
| xs -> ([], xs)
let rec parse pkg_name acc expecting_rparen = function
| [] ->
if expecting_rparen then
raise (MetaParseError (pkg_name, "unclosed package block (missing closing parenthesis)"))
else
(acc, [])
| RPAREN :: xs -> (acc, xs)
| ID "package" :: S name :: LPAREN :: xs ->
let pkg, xs2 = parse pkg_name (Pkg.make name) true xs in
let nacc = { acc with Pkg.subs = acc.Pkg.subs @ [ pkg ] } in
parse pkg_name nacc expecting_rparen xs2
| ID "requires" :: xs -> (
let preds, xs2 = parse_predicate_list pkg_name "requires" xs in
match xs2 with
| PLUSEQ :: S reqs :: xs3 | EQ :: S reqs :: xs3 ->
let deps =
List.map (fun r -> Libname.of_string r)
$ (List.filter (fun x -> x <> "")
$ String_utils.split_pred (fun c -> match c with ',' | ' ' | '\n' | '\r' | '\t' -> true | _ -> false) reqs
)
in
parse pkg_name
{ acc with Pkg.requires = (preds, List.rev deps) :: acc.Pkg.requires }
expecting_rparen xs3
| _ ->
raise
(MetaParseError
( pkg_name,
"parsing requires failed: expected '=' or '+=' followed by quoted dependency \
list" )))
| ID "directory" :: EQ :: S dir :: xs ->
parse pkg_name { acc with Pkg.directory = dir } expecting_rparen xs
| ID "description" :: EQ :: S dir :: xs ->
parse pkg_name { acc with Pkg.description = dir } expecting_rparen xs
| ID "browse_interfaces" :: EQ :: S _ :: xs -> parse pkg_name acc expecting_rparen xs
| ID "warning" :: xs -> (
let preds, xs2 = parse_predicate_list pkg_name "archive" xs in
match xs2 with
| EQ :: S v :: xs3 ->
let nacc = { acc with Pkg.warning = acc.Pkg.warning @ [ (preds, v) ] } in
parse pkg_name nacc expecting_rparen xs3
| _ ->
raise
(MetaParseError
(pkg_name, "parsing warning failed: expected '=' followed by quoted string")))
| ID "archive" :: xs -> (
let preds, xs2 = parse_predicate_list pkg_name "archive" xs in
match xs2 with
| PLUSEQ :: S v :: xs3 ->
let nacc =
{ acc with Pkg.append_archives = acc.Pkg.append_archives @ [ (preds, v) ] }
in
parse pkg_name nacc expecting_rparen xs3
| EQ :: S v :: xs3 ->
let nacc = { acc with Pkg.archives = acc.Pkg.archives @ [ (preds, v) ] } in
parse pkg_name nacc expecting_rparen xs3
| _ ->
raise
(MetaParseError
(pkg_name, "parsing archive failed: expected '=' or '+=' followed by quoted string"))
)
| ID "plugin" :: xs -> (
let preds, xs2 = parse_predicate_list pkg_name "plugin" xs in
let preds = Predicate.Plugin :: preds in
match xs2 with
| PLUSEQ :: S v :: xs3 ->
let nacc =
{ acc with Pkg.append_archives = acc.Pkg.append_archives @ [ (preds, v) ] }
in
parse pkg_name nacc expecting_rparen xs3
| EQ :: S v :: xs3 ->
let nacc = { acc with Pkg.archives = acc.Pkg.archives @ [ (preds, v) ] } in
parse pkg_name nacc expecting_rparen xs3
| _ ->
raise
(MetaParseError
( pkg_name,
"parsing plugin failed: expected '=' or '+=' followed by quoted plugin path" )))
| ID "preprocessor" :: EQ :: S v :: xs ->
parse pkg_name { acc with Pkg.preprocessor = v } expecting_rparen xs
| ID "ppx" :: xs -> (
let preds, xs2 = parse_predicate_list pkg_name "ppx" xs in
match xs2 with
| EQ :: S v :: xs3 ->
parse pkg_name { acc with Pkg.ppx = Some (preds, v) } expecting_rparen xs3
| _ ->
raise
(MetaParseError
(pkg_name, "parsing ppx failed: expected '=' followed by quoted preprocessor path"))
)
| ID "ppxopt" :: xs -> (
let preds, xs2 = parse_predicate_list pkg_name "ppxopt" xs in
match xs2 with
| PLUSEQ :: S v :: xs3 | EQ :: S v :: xs3 ->
parse pkg_name
{ acc with Pkg.ppxopt = acc.Pkg.ppxopt @ [ (preds, v) ] }
expecting_rparen xs3
| _ ->
raise
(MetaParseError
(pkg_name, "parsing ppxopt failed: expected '=' or '+=' followed by quoted options"))
)
| ID "version" :: EQ :: S v :: xs ->
parse pkg_name { acc with Pkg.version = v } expecting_rparen xs
| ID "exists_if" :: EQ :: S v :: xs ->
parse pkg_name { acc with Pkg.exists_if = v } expecting_rparen xs
| ID "error" :: LPAREN :: xs -> (
let rec consume = function
| RPAREN :: zs -> zs
| _ :: zs -> consume zs
| [] ->
raise
(MetaParseError
(pkg_name, "unexpected EOF in error field (missing closing parenthesis)"))
in
match consume xs with
| EQ :: S _ :: xs2 -> parse pkg_name acc expecting_rparen xs2
| _ ->
raise
(MetaParseError
(pkg_name, "parsing error field failed, expected '=' after closing parenthesis")))
| ID "linkopts" :: xs -> (
let preds, xs2 = parse_predicate_list pkg_name "linkopts" xs in
match xs2 with
| EQ :: S s :: xs3 ->
parse pkg_name
{
acc with
Pkg.linkopts = ((if preds = [] then None else Some preds), s) :: acc.Pkg.linkopts;
}
expecting_rparen xs3
| _ ->
raise
(MetaParseError (pkg_name, "parsing linkopts failed, expected '=' after predicates")))
| ID stuff :: EQ :: S stuffVal :: xs ->
parse pkg_name
{ acc with Pkg.assignment = (stuff, stuffVal) :: acc.Pkg.assignment }
expecting_rparen xs
| x :: xs ->
raise
(MetaParseError
( pkg_name,
"unknown token '" ^ to_string x ^ "' in meta file\n"
^ String.concat " " (List.map to_string xs) ))
end
(* meta files are supposed to be small, so don't bother with
* a real efficient and incremental read/lex/parse routine.
*
* this can be improve later on-needed basis
*)
let parse name content pkg_name =
fst (Token.parse name (Pkg.make pkg_name) false (Token.tokenize name content))
let read path name =
let meta_content = Filesystem.read_file path in
parse path meta_content name
(* get the META file path associated to a library *)
let find_lib_path name =
if !path_warning then (
eprintf "warning: obuild META search paths and ocaml config mismatch\n\n";
eprintf " The ocamlfind configuration file used doesn't list the ocaml standard library \n";
eprintf " as part of his search paths. something fishy is going on\n";
eprintf " You can solve the issue by:\n";
eprintf " * pointing OCAMLFIND_CONF environment to the right configuration file\n";
eprintf
" * making sure that the ocamlfind program in your path is the right one (ocamlfind \
printconf)\n";
eprintf "\n";
eprintf " this is likely to cause various compilation problems\n";
(* then we ignore further warnings *)
path_warning := false);
let rec find_ret l =
match l with
| [] -> raise (LibraryNotFound name)
| p :: ps ->
let inDir = p </> fn name </> fn "META" in
let asMetaext = p </> (fn "META" <.> name) in
if Filesystem.exists inDir then
inDir
else if Filesystem.exists asMetaext then
asMetaext
else
find_ret ps
in
find_ret (FindlibConf.get_paths ())
let find_lib name : t =
let path = find_lib_path name in
(path, read path name)
let resolve_directory stdlib basePath directory =
match directory with
| "" | "." -> basePath
| "^" -> path_dirname basePath
| o -> (
match o.[0] with
| '^' -> path_dirname basePath <//> fp (String_utils.drop 1 o)
| '+' -> stdlib <//> fp (String_utils.drop 1 o)
| _ ->
let fpo = fp o in
if Filepath.is_absolute fpo then
fpo
else
basePath <//> fpo)
let get_include_dir_with_subpath stdlib ((path, pkg) : t) subnames : filepath =
let basePath = path_dirname path in
let rec buildPath currentPath remainingSubnames currentPkg =
match remainingSubnames with
| [] -> currentPath
| subname :: rest -> (
try
let subpkg = List.find (fun spkg -> spkg.Pkg.name = subname) currentPkg.Pkg.subs in
let newPath = resolve_directory stdlib currentPath subpkg.Pkg.directory in
buildPath newPath rest subpkg
with Not_found -> raise (SubpackageNotFound subname))
in
buildPath basePath subnames pkg
let get_include_dir stdlib ((path, pkg) : t) : filepath =
resolve_directory stdlib (path_dirname path) pkg.Pkg.directory
|