text
stringlengths
12
786k
let removeDir path = removeDirContent path ; Unix . rmdir ( fp_to_string path ) ; ( )
let iterate f path = let entries = Sys . readdir ( fp_to_string path ) in Array . fast_sort String . compare entries ; Array . iter ( fun ent -> f ( fn ent ) ) entries ; ( )
let list_dir_pred_map ( p : filename -> ' a option ) path : ' a list = let accum = ref [ ] in iterate ( fun ent -> match p ent with | None -> ( ) | Some e -> accum := e :: ! accum ) path ; ! accum
let list_dir_pred ( p : filename -> bool ) path : filename list = list_dir_pred_map ( fun e -> if p e then Some e else None ) path
let list_dir = list_dir_pred ( const true )
let list_dir_path_pred p path = let entries = List . filter p ( Array . to_list ( Sys . readdir ( fp_to_string path ) ) ) in let sorted = List . fast_sort String . compare entries in List . map ( fun ent -> path </> fn ent ) sorted
let list_dir_path = list_dir_path_pred ( const true )
let getModificationTime path = try ( Unix . stat ( fp_to_string path ) ) . Unix . st_mtime with _ -> 0 . 0
let exists path = Sys . file_exists ( fp_to_string path )
let is_dir path = try Sys . is_directory ( fp_to_string path ) with _ -> false
let mkdirSafe path perm = if Sys . file_exists ( fp_to_string path ) then ( if Sys . is_directory ( fp_to_string path ) then false else failwith ( " directory " ^ ( fp_to_string path ) ^ " cannot be created : file already exists " ) ) else ( Unix . mkdir ( fp_to_string path ) ...
let mkdirSafe_ path perm = let ( _ : bool ) = mkdirSafe path perm in ( )
let rec mkdirSafeRecursive path perm = if not ( is_dir path ) then ( if path_length path > 1 then ( mkdirSafeRecursive ( path_dirname path ) perm ; mkdirSafe_ path perm ) )
let create_or_empty_dir path = let created = mkdirSafe path 0o755 in if not created then removeDirContent path ; ( )
let write_no_partial fd b o l = let len = ref l in let ofs = ref o in while ! len > 0 do let written = Unix . write fd ( bytes_of_string b ) ! ofs ! len in if written = 0 then raise WriteFailed ; ofs := ! ofs + written ; len := ! len - written done
let withfile path openflags perms f = let fd = Unix . openfile ( fp_to_string path ) openflags perms in finally ( fun ( ) -> f fd ) ( fun ( ) -> Unix . close fd )
let writeFile path s = withfile path [ Unix . O_WRONLY ; Unix . O_CREAT ; Unix . O_TRUNC ] 0o644 ( fun fd -> write_no_partial fd s 0 ( String . length s ) )
let readFile path = let buf = Buffer . create 1024 in let b = bytes_make 1024 ' ' in withfile path [ Unix . O_RDONLY ] 0o644 ( fun fd -> let isDone = ref false in while not ! isDone do let r = Unix . read fd b 0 1024 in if r > 0 then buffer_add_subbytes buf b 0 r else isDone := true done...
let copy_file src dst = mkdirSafeRecursive ( path_dirname dst ) 0o755 ; let s = bytes_make 4096 ' ' in let srcStat = Unix . stat ( fp_to_string src ) in let operm = srcStat . Unix . st_perm in withfile dst [ Unix . O_WRONLY ; Unix . O_CREAT ] operm ( fun fdDst -> withfile src [ ...
let copy_to_dir src dst = copy_file src ( dst <//> src )
let copy_many_files srcs dst = List . iter ( fun src -> copy_to_dir src dst ) srcs
let rec mktemp_dir_in prefix = let s = bytes_make 4 ' ' in let fd = Unix . openfile " / dev / urandom " [ Unix . O_RDONLY ] 0o640 in let r = ref 0 in while ! r < 4 do let n = Unix . read fd s ! r ( 4 - ! r ) in if n = 0 then r := 4 else r := n + ! r done ; Unix . close ...
type t = FileML | FileMLI | FileH | FileC | FileCMX | FileCMO | FileCMI | FileCMA | FileCMXA | FileCMXS | FileCMT | FileCMTI | FileO | FileA | FileSO | FileEXE | FileOther of string
let of_string s = match s with | " ml " -> FileML | " mli " -> FileMLI | " h " -> FileH | " c " -> FileC | " cmx " -> FileCMX | " cmo " -> FileCMO | " cmi " -> FileCMI | " cma " -> FileCMA | " cmxa " -> FileCMXA | " cmxs " -> FileCMXS | " cmt " -> FileC...
let to_string fty = match fty with | FileML -> " ml " | FileMLI -> " mli " | FileH -> " h " | FileC -> " c " | FileCMX -> " cmx " | FileCMO -> " cmo " | FileCMI -> " cmi " | FileCMA -> " cma " | FileCMXA -> " cmxa " | FileCMXS -> " cmxs " | FileCMT -> " c...
type id = { fdep_ty : t ; fdep_path : filepath }
let make_id ( ty , p ) = { fdep_ty = ty ; fdep_path = p }
let get_id fdep = ( fdep . fdep_ty , fdep . fdep_path )
let get_type fdep = fdep . fdep_ty
let get_path fdep = fdep . fdep_path
let of_filename ( name : filename ) : t = try let nameUnpack = fn_to_string name in let len = String . length ( Filename . chop_extension nameUnpack ) in of_string ( String . sub nameUnpack ( len + 1 ) ( String . length nameUnpack - len - 1 ) ) with Invalid_argument _ -> FileEXE
let of_filepath ( path : filepath ) : t = of_filename ( path_basename path )
let replace_extension ( name : filename ) ext = let extStr = to_string ext in try let choppedName = Filename . chop_extension ( fn_to_string name ) in fn ( String . concat " . " [ choppedName ; extStr ] ) with Invalid_argument _ -> fn ( fn_to_string name ^ " . " ^ extStr )
let replace_extension_path path ext = let dir = path_dirname path in dir </> replace_extension ( path_basename path ) ext
type error = | Cannot_run_ocamlfind | Dependency_not_found of string * string | Package_not_found of string | Cannot_parse_query of string * string
let error x = raise ( Findlib_error x )
let string_of_error = function | Cannot_run_ocamlfind -> " Cannot run Ocamlfind . " | Dependency_not_found ( p , d ) -> Printf . sprintf " Ocamlfind returned " \% s " \ as a dependency for package " \% s " \ but does \ | Package_not_found p -> Printf . sprintf " Findlib package n...
let report_error e = prerr_endline ( string_of_error e ) ; exit 2
type package = { name : string ; description : string ; version : string ; archives_byte : string ; archives_native : string ; link_options : string ; location : string ; dependencies : package list ; }
let packages = Hashtbl . create 42
let run_and_parse lexer command = Printf . ksprintf ( fun command -> lexer & Lexing . from_string & run_and_read command ) command
let run_and_read command = Printf . ksprintf run_and_read command
let rec query name = try Hashtbl . find packages name with Not_found -> try let n , d , v , a_byte , lo , l = run_and_parse Lexers . ocamlfind_query " % s query - l - predicates byte % s " ocamlfind name in let a_native = run_and_parse Lexers . trim_blanks " % s query - a - format - pr...
let list ( ) = run_and_parse Lexers . blank_sep_strings " % s list | cut - d ' ' - f1 " ocamlfind
let topological_closure l = let add l x = if List . mem x l then l else x :: l in let l = List . fold_left begin fun acc p -> add ( List . fold_left add acc p . dependencies ) p end [ ] l in List . rev l
let add_atom a l = match a , l with | A " " , _ -> l | _ -> a :: l
let compile_flags l = let pkgs = topological_closure l in let locations = List . fold_left begin fun acc p -> SSet . add p . location acc end SSet . empty pkgs in let flags = [ ] in let flags = List . fold_left begin fun acc l -> add_atom ( P l ) ( add_atom ( A " - I " ) acc ) end ...
let link_flags f l = let pkgs = topological_closure l in let locations = List . fold_left begin fun acc p -> SSet . add p . location acc end SSet . empty pkgs in let flags = [ ] in let flags = List . fold_left begin fun acc l -> add_atom ( P l ) ( add_atom ( A " - I " ) acc ) end f...
let link_flags_byte = link_flags ( fun x -> x . archives_byte )
let link_flags_native = link_flags ( fun x -> x . archives_native )
type t = { path : filepath list ; destdir : filepath option ; all : ( string * string option ) list ; loaded : bool }
let default = { all = [ ] ; path = [ ] ; destdir = None ; loaded = false }
let conf = ref default
let parse_file path = let content = Filesystem . readFile path in let unquote s = match s with | None -> failwith ( " unknown configuration key with no value " ) | Some x -> string_init 1 ( string_drop 1 x ) in let kvs = List . map Utils . toKVeq ( string_lines_noempty content ) in let p...
let get_program_config ( ) = match Process . run [ " ocamlfind " ; " printconf " ; " conf " ] with | Process . Failure err -> failwith ( " ocamlfind printconf failed err " ^ err ) | Process . Success ( out , _ , _ ) -> match string_lines_noempty out with | [ x ] ...
let get_paths ( ) = try [ fp ( Sys . getenv " OCAMLFIND_CONF " ) ] with Not_found -> try get_program_config ( ) with _ -> [ fp " / etc / findlib . conf " ; fp " / etc / ocamlfind . conf " ]
let get_system ( ) = let paths = get_paths ( ) in try let found_path = List . find Filesystem . exists paths in parse_file found_path with Not_found -> default
let load ( ) = match Gconf . get_env ( " findlib - path " ) with | None -> conf := get_system ( ) | Some p -> conf := parse_file ( fp p )
let get_paths ( ) = ( ! conf ) . path
let get_destdir ( ) = ( ! conf ) . destdir
module type S = sig type t = private # c end ; ;
module M : S = struct type t = c end
module type S ' = S with type t = c ; ;
module type S2 = S with type t = private # d ; ;
module M2 : S = struct type t = d end ; ;
module M3 : S = struct type t = private # d end ; ;
module T1 = struct type ( ' a , ' b ) a = [ ` A of ' a | ` B of ' b ] type ( ' a , ' b ) b = [ ` Z | ( ' a , ' b ) a ] end
module type T2 = sig type a and b val evala : a -> int val evalb : b -> int end
module type T3 = sig type a0 = private [ > ( a0 , b0 ) T1 . a ] and b0 = private [ > ( a0 , b0 ) T1 . b ] end
module type T4 = sig include T3 include T2 with type a = a0 and type b = b0 end
module F ( X : T4 ) = struct type a = X . a and b = X . b let a = X . evala ( ` B ` Z ) let b = X . evalb ( ` A ( ` B ` Z ) ) let a2b ( x : a ) : b = ` A x let b2a ( x : b ) : a = ` B x end
module M4 = struct type a = [ ` A of a | ` B of b | ` ZA ] and b = [ ` A of a | ` B of b | ` Z ] type a0 = a type b0 = b let rec eval0 = function ` A a -> evala a | ` B b -> evalb b and evala : a -> int = function # T1 . a as x -> 1 + eval0 x | ` ZA -> 3 and evalb : b...
module M6 : sig class ci : int -> object val x : int method x : int method move : int -> unit end type c = private # ci val create : int -> c class ci x = object val mutable x : int = x method x = x method move d = x <- x + d end type c = ci let create = new ci end
let f ( x : M6 . c ) = x # move 3 ; x # x ; ;
module M : sig type t = private [ > ` A of bool ] end = struct type t = [ ` A of int ] end
type call_kind = | Indirect | Direct of Closure_id . t
type const = | Int of int | Char of char
type apply = { func : Variable . t ; args : Variable . t list ; kind : call_kind ; dbg : Debuginfo . t ; reg_close : Lambda . region_close ; mode : Lambda . alloc_mode ; inlined : Lambda . inlined_attribute ; specialise : Lambda . specialise_attribute ; probe : Lambda . probe...
type assign = { being_assigned : Mutable_variable . t ; new_value : Variable . t ; }
type send = { kind : Lambda . meth_kind ; meth : Variable . t ; obj : Variable . t ; args : Variable . t list ; dbg : Debuginfo . t ; reg_close : Lambda . region_close ; mode : Lambda . alloc_mode ; }
type specialised_to = { var : Variable . t ; projection : Projection . t option ; }
type t = | Var of Variable . t | Let of let_expr | Let_mutable of let_mutable | Let_rec of ( Variable . t * named ) list * t | Apply of apply | Send of send | Assign of assign | If_then_else of Variable . t * t * t | Switch of Variable . t * switch | String_switch of Variable . t * ( st...
type program_body = | Let_symbol of Symbol . t * constant_defining_value * program_body | Let_rec_symbol of ( Symbol . t * constant_defining_value ) list * program_body | Initialize_symbol of Symbol . t * Tag . t * t list * program_body | Effect of t * program_body | End of Symbol . t
type program = { imported_symbols : Symbol . Set . t ; program_body : program_body ; }
let print_specialised_to ppf ( spec_to : specialised_to ) = match spec_to . projection with | None -> fprintf ppf " % a " Variable . print spec_to . var | Some projection -> fprintf ppf " % a ( = % a ) " Variable . print spec_to . var Projection . print projection
let rec lam ppf ( flam : t ) = match flam with | Var ( id ) -> Variable . print ppf id | Apply ( { func ; args ; kind ; inlined ; probe ; dbg } ) -> let direct ppf ( ) = match kind with | Indirect -> ( ) | Direct closure_id -> fprintf ppf " [ *% a ] " Closure_id ....
let print_function_declarations ppf ( fd : function_declarations ) = let funs ppf = Variable . Map . iter ( print_function_declaration ppf ) in fprintf ppf " [ @< 2 ( >% a ) ( origin = % a ) ] " @ funs fd . funs Set_of_closures_origin . print fd . set_of_closures_origin
let print ppf flam = fprintf ppf " % a . " @ lam flam
let print_function_declaration ppf ( var , decl ) = print_function_declaration ppf var decl
let print_constant_defining_value ppf ( const : constant_defining_value ) = match const with | Allocated_const const -> fprintf ppf " ( Allocated_const % a ) " Allocated_const . print const | Block ( tag , [ ] ) -> fprintf ppf " ( Atom ( tag % d ) ) " ( Tag . to_int tag )...
let rec print_program_body ppf ( program : program_body ) = let symbol_binding ppf ( symbol , constant_defining_value ) = fprintf ppf " [ @< 2 ( >% a @ % a ) ] " @ Symbol . print symbol print_constant_defining_value constant_defining_value in match program with | Let_symbol ( symbol ...
let print_program ppf program = Symbol . Set . iter ( fun symbol -> fprintf ppf " [ @ import_symbol @ % a ] . " @@ Symbol . print symbol ) program . imported_symbols ; print_program_body ppf program . program_body
let rec variables_usage ? ignore_uses_as_callee ? ignore_uses_as_argument ? ignore_uses_in_project_var ~ all_used_variables tree = match tree with | Var var -> Variable . Set . singleton var | _ -> let free = ref Variable . Set . empty in let bound = ref Variable . Set . empty in let free_variab...
let free_variables ? ignore_uses_as_callee ? ignore_uses_as_argument ? ignore_uses_in_project_var tree = variables_usage ? ignore_uses_as_callee ? ignore_uses_as_argument ? ignore_uses_in_project_var ~ all_used_variables : false tree
let free_variables_named ? ignore_uses_in_project_var named = variables_usage_named ? ignore_uses_in_project_var ~ all_used_variables : false named
let used_variables ? ignore_uses_as_callee ? ignore_uses_as_argument ? ignore_uses_in_project_var tree = variables_usage ? ignore_uses_as_callee ? ignore_uses_as_argument ? ignore_uses_in_project_var ~ all_used_variables : true tree
let used_variables_named ? ignore_uses_in_project_var named = variables_usage_named ? ignore_uses_in_project_var ~ all_used_variables : true named
let create_let var defining_expr body : t = begin match ! Clflags . dump_flambda_let with | None -> ( ) | Some stamp -> Variable . debug_when_stamp_matches var ~ stamp ~ f ( : fun ( ) -> Printf . eprintf " Creation of [ Let ] with stamp % d :\ n % s \ n " %! stamp ( Printexc . ...
let map_defining_expr_of_let let_expr ~ f = let defining_expr = f let_expr . defining_expr in if defining_expr == let_expr . defining_expr then Let let_expr else let free_vars_of_defining_expr = free_variables_named defining_expr in Let { var = let_expr . var ; defining_expr ; body = let_expr . bod...
let iter_lets t ~ for_defining_expr ~ for_last_body ~ for_each_let = let rec loop ( t : t ) = match t with | Let { var ; defining_expr ; body ; _ } -> for_each_let t ; for_defining_expr var defining_expr ; loop body | t -> for_last_body t in loop t