text
stringlengths
0
601k
let to_rev_list line = line
let to_list line = List . rev line
let of_list ? slip line = match slip with
let dump ppf cmd = let pp_elt ppf s = Format . fprintf ppf " % s " ( Filename . quote s ) s in let rec loop = function | [ ] -> ( ) | v :: vs -> if vs = [ ] then pp_elt ppf v else ( Format . fprintf ppf " % a @ " pp_elt v ; loop vs ) vs in Format . fprintf ppf [ " @< 1 [ " ; > loop ( List . rev cmd ) cmd ; Format . fprintf ppf ] ] " " @
type error = Corrupted of ( string * string ) string | Version of int * int
let pp_error ppf = function Format . fprintf ppf " corrupted % s in % S " kind v Format . fprintf ppf " version mismatch , expected % d found % d " exp fnd
let err ~ kind v = raise ( Error ( Corrupted ( kind , v ) v ) v ) v
let err_version ~ exp ~ fnd = raise ( Error ( Version ( exp , fnd ) fnd ) fnd ) fnd
type ' a t = { kind : string ; enc : ' a -> string ; dec : string -> ' a ; }
let v ~ kind ~ enc ~ dec = { kind ; enc ; dec }
let kind c = c . kind
let enc c = c . enc
let dec c = c . dec
let with_kind kind c = { c with kind }
let dec_result c s = try Ok ( dec c s ) s with R . error_msgf " Decode % s : % a Input data : % S " ( kind c ) c pp_error err s
let write file c v = Topkg_os . File . write file ( enc c v ) v |> R . reword_error_msg ~ replace : true ( fun err -> R . msgf " Encode % s to % s : % s " ( kind c ) c file err ) err
let read file c = Topkg_os . File . read file >>= fun s -> try Ok ( dec c s ) s with | Error e -> R . error_msgf " Decode % s from % s : % a " ( kind c ) c file pp_error e
let tail s = Topkg_string . with_index_range ~ first : 1 s
let unit = let kind = " unit " in let enc = function ( ) -> " \ x00 " in let dec = function " \ x00 " -> ( ) | s -> err ~ kind s in v ~ kind ~ enc ~ dec
let const c = let kind = " const " in let enc = function _ -> " " in let dec = function " " -> c | s -> err ~ kind s in v ~ kind ~ enc ~ dec
let bool = let kind = " bool " in let enc = function false -> " \ x00 " | true -> " \ x01 " in let dec = function " \ x00 " -> false | " \ x01 " -> true | s -> err ~ kind s in v ~ kind ~ enc ~ dec
let int = let kind = " int " in let enc = string_of_int in let dec s = try int_of_string s with Failure _ -> err ~ kind s in v ~ kind ~ enc ~ dec
let string = let kind = " string " in let enc s = s in let dec s = s in v ~ kind ~ enc ~ dec
let option some = let kind = Printf . sprintf ( " % s ) s option " ( kind some ) some in let enc = function None -> " \ x00 " | Some v -> " \ x01 " ^ ( enc some v ) v in let dec s = match Topkg_string . head s with | Some ' \ x00 ' -> None | Some ' \ x01 ' -> Some ( dec some ( tail s ) s ) s | _ -> err ~ kind s in v ~ kind ~ enc ~ dec
let result ~ ok ~ error = let kind = Printf . sprintf ( " % s , % s ) s result " ( kind ok ) ok ( kind error ) error in let enc = function | Ok v -> " \ x00 " ^ ( enc ok v ) v | Error e -> " \ x01 " ^ ( enc error e ) e in let dec s = match Topkg_string . head s with | Some ' \ x00 ' -> Ok ( dec ok ( tail s ) s ) s | Some ' \ x01 ' -> Error ( dec error ( tail s ) s ) s | _ -> err ~ kind s in v ~ kind ~ enc ~ dec
let list el = let kind = Printf . sprintf ( " % s ) s list " ( kind el ) el in let enc vs = let b = Buffer . create 255 in let rec loop = function | [ ] -> Buffer . add_char b ' \ x00 ' | v :: vs -> let venc = ( enc el ) el v in let venc_len = String . length venc in Buffer . add_char b ' \ x01 ' ; Buffer . add_string b ( string_of_int venc_len ) venc_len ; Buffer . add_char b ' \ x01 ' ; Buffer . add_string b venc ; loop vs in loop vs ; Buffer . contents b in let dec s = let rec loop acc s = match Topkg_string . head s with | Some ' \ x00 ' -> acc | Some ' \ x01 ' -> begin match Topkg_string . find_byte ~ start : 1 ' \ x01 ' s with | None -> err ~ kind s | Some one -> try let last = one - 1 in let len = Topkg_string . with_index_range ~ first : 1 ~ last s in let len = int_of_string len in let first = one + 1 in let last = first + len - 1 in let venc = Topkg_string . with_index_range ~ first ~ last s in let rest = Topkg_string . with_index_range ~ first ( : last + 1 ) 1 s in loop ( ( dec el venc ) venc :: acc ) acc rest with Failure _ -> err ~ kind s end | _ -> err ~ kind s in List . rev ( loop [ ] s ) s in v ~ kind ~ enc ~ dec
let seq = list string
let pair c0 c1 = let kind = Printf . sprintf " % s * % s " ( kind c0 ) c0 ( kind c1 ) c1 in let enc ( v0 , v1 ) v1 = enc seq [ enc c0 v0 ; enc c1 v1 ] v1 in let dec s = match dec seq s with | [ lenc ; renc ] renc -> ( dec c0 lenc ) lenc , ( dec c1 renc ) renc | _ -> err ~ kind s in v ~ kind ~ enc ~ dec
let t3 c0 c1 c2 = let kind = Printf . sprintf " % s * % s * % s " ( kind c0 ) c0 ( kind c1 ) c1 ( kind c2 ) c2 in let seq = list string in let enc ( v0 , v1 , v2 ) v2 = enc seq [ enc c0 v0 ; enc c1 v1 ; enc c2 v2 ] v2 in let dec s = match ( dec seq ) seq s with | [ v0 ; v1 ; v2 ] v2 -> ( dec c0 v0 ) v0 , ( dec c1 v1 ) v1 , ( dec c2 v2 ) v2 | _ -> err ~ kind s in v ~ kind ~ enc ~ dec
let t4 c0 c1 c2 c3 = let kind = Printf . sprintf " % s * % s * % s * % s " ( kind c0 ) c0 ( kind c1 ) c1 ( kind c2 ) c2 ( kind c3 ) c3 in let seq = list string in let enc ( v0 , v1 , v2 , v3 ) v3 = enc seq [ enc c0 v0 ; enc c1 v1 ; enc c2 v2 ; enc c3 v3 ] v3 in let dec s = match ( dec seq ) seq s with | [ v0 ; v1 ; v2 ; v3 ] v3 -> ( dec c0 v0 ) v0 , ( dec c1 v1 ) v1 , ( dec c2 v2 ) v2 , ( dec c3 v3 ) v3 | _ -> err ~ kind s in v ~ kind ~ enc ~ dec
let t5 c0 c1 c2 c3 c4 = let kind = Printf . sprintf " % s * % s * % s * % s * % s " ( kind c0 ) c0 ( kind c1 ) c1 ( kind c2 ) c2 ( kind c3 ) c3 ( kind c4 ) c4 in let seq = list string in let enc ( v0 , v1 , v2 , v3 , v4 ) v4 = enc seq [ enc c0 v0 ; enc c1 v1 ; enc c2 v2 ; enc c3 v3 ; enc c4 v4 ] v4 in let dec s = match ( dec seq ) seq s with | [ v0 ; v1 ; v2 ; v3 ; v4 ] v4 -> ( dec c0 v0 ) v0 , ( dec c1 v1 ) v1 , ( dec c2 v2 ) v2 , ( dec c3 v3 ) v3 , ( dec c4 v4 ) v4 | _ -> err ~ kind s in v ~ kind ~ enc ~ dec
let alt ~ kind tag cs = let l = Array . length cs in if l > 256 then invalid_arg @@ Topkg_string . strf " too many codecs ( % d ) d " l ; let enc v = let tag = tag v in Printf . sprintf " % c % s " ( Char . chr tag ) tag ( enc cs ( . tag ) tag v ) v in let dec s = match Topkg_string . head s with | None -> err ~ kind s | Some tag -> let tag = Char . code tag in if tag < Array . length cs then dec cs ( . tag ) tag ( tail s ) s else err ~ kind s in v ~ kind ~ enc ~ dec
let version version = let enc_version = string_of_int version in fun c -> let kind = Printf . sprintf ( " % s ) s v % s " ( kind c ) c enc_version in let enc v = String . concat " \ x00 " [ enc_version ; enc c v ] v in let dec s = match Topkg_string . cut ~ sep : ' \ x00 ' s with | None -> err ~ kind s | Some ( fnd_version , s ) s -> try let fnd = int_of_string fnd_version in if fnd <> version then err_version ~ exp : version ~ fnd else dec c s with | Failure _ -> err ~ kind s in v ~ kind ~ enc ~ dec
let view ? kind : k ( inj , proj ) proj c = let kind = match k with None -> kind c | Some k -> k in let enc v = enc c ( inj v ) v in let dec s = proj ( dec c s ) s in v ~ kind ~ enc ~ dec
let msg = let msg = ( fun ( ` Msg m ) m -> m ) m , ( fun m -> ` Msg m ) m in view msg string
let result_error_msg ok = result ~ ok ~ error : msg
let cmd = let cmd = ( fun cmd -> Topkg_cmd . to_list cmd ) cmd , ( fun l -> Topkg_cmd . of_list l ) l in view cmd ( list string ) string
type ' a conv = { parse : ( string -> ' a result ) result ; print : ( Format . formatter -> ' a -> unit ) unit ; docv : string ; }
let conv ( ? docv = " VALUE ) " parse print = { parse ; print ; docv }
let conv_parser conv = conv . parse
let conv_printer conv = conv . print
let conv_docv conv = conv . docv
let conv_with_docv conv ~ docv = { conv with docv }
let bool = let parse s = try Ok ( bool_of_string s ) s with | Invalid_argument _ -> R . error_msgf " % S : Can ' t parse boolean value " s in conv ~ docv " : BOOL " parse Format . pp_print_bool
let int = let parse s = try Ok ( int_of_string s ) s with | Failure _ -> R . error_msgf " % S : Can ' t parse integer value " s in conv ~ docv " : INT " parse Format . pp_print_int
let string = conv ~ docv " : STRING " ( fun s -> Ok s ) s Format . pp_print_string
let fpath = conv_with_docv string ~ docv " : PATH "
let some ( ? none = ) " " conv = let parse s = match conv . parse s with | Ok v -> Ok ( Some v ) v | Error _ as e -> e in let print ppf = function | None -> Format . pp_print_string ppf none | Some v -> conv . print ppf v in { conv with parse ; print }
let univ ( type s ) s ( ) = let module M = struct exception E of s option end in ( fun x -> M . E ( Some x ) x ) x , ( function M . E x -> x | _ -> None ) None
let key_id = let count = ref ( - 1 ) 1 in fun ( ) -> incr count ; ! count
type ' a absent = Value of ' a | Discover of ( unit -> ' a result ) result
type ' a key = { id : int ; name : string ; conv : ' a conv ; absent : ' a absent ; env : string option ; to_univ : ' a -> univ ; of_univ : univ -> ' a option ; doc : string ; }
module Key = struct type t = V : ' a key -> t let compare ( V k0 ) k0 ( V k1 ) k1 = ( compare : int -> int -> int ) int k0 . id k1 . id end
module Kset = Set . Make ( Key ) Key
module Kmap = Map . Make ( Key ) Key
let key_index = ref Kset . empty
let cli_opts_of_key_index ( ) = let add_key ( Key . V k as key ) key acc = ( " " -- ^ k . name , key ) key :: acc in Kset . fold add_key ! key_index [ ]
let _key ? docv ( ? doc = " Undocumented ) " ? env name conv absent = let id = key_id ( ) in let to_univ , of_univ = univ ( ) in let conv = match docv with | None -> conv | Some docv -> conv_with_docv conv ~ docv in let key = { id ; name ; conv ; absent ; env ; to_univ ; of_univ ; doc } in key_index := Kset . add ( Key . V key ) key ! key_index ; key
let key ? docv ? doc ? env name conv ~ absent = _key ? docv ? doc ? env name conv ( Value absent ) absent
let discovered_key ? docv ? doc ? env name conv ~ absent = _key ? docv ? doc ? env name conv ( Discover absent ) absent
let key_absent_value k = let absent_field k = match k . absent with | Value v -> v | Discover discover -> match discover ( ) with | Ok v -> v | Error ( ` Msg m ) m -> failwith ( Topkg_string . strf " key % s : % s " k . name m ) m in match k . env with | None -> absent_field k | Some var -> match Topkg_os . Env . var var with | None -> absent_field k | Some ev -> match conv_parser k . conv ev with | Ok v -> v | Error ( ` Msg m ) m -> failwith ( Topkg_string . strf " key % s : env % s : % s " k . name var m ) m
let with_pkg ( ? default = true ) true pkg = let doc = Topkg_string . strf " true if package % s is installed . " pkg in key ( " with " - ^ pkg ) pkg bool ~ absent : default ~ doc
let pkg_name = let doc = " The name ( $ docv ) docv of the package ( and hence the opam install \ file ) file . If absent provided by the package description . " in let absent ( ) = assert false in discovered_key " pkg - name " string ~ absent ~ doc ~ docv " : NAME "
let build_dir = let doc = " Specifies the build directory ( $ docv ) docv . " in let absent ( ) = assert false in discovered_key " build - dir " string ~ absent ~ doc ~ docv " : BUILD_DIR "
let vcs = let doc = " Specifies if the package directory is VCS managed . " in let absent ( ) = Topkg_vcs . find ~ dir " . " : ( ) >>= function | None -> Ok false | Some _ -> Ok true in discovered_key " vcs " bool ~ absent ~ doc
let pinned = let doc = " Deprecated , use -- dev - pkg . The semantics is the same . " in key " pinned " bool ~ absent : false ~ doc
let dev_pkg = let doc = " Specifies that the build is a dev package build ( e . g . in opam ) opam . " in key " dev - pkg " bool ~ absent : false ~ doc
let tests = let doc = " Specifies whether tests should be built . If absent depends \ on the build context , true for development and false otherwise . " in let absent ( ) = Ok None in discovered_key " tests " ( some bool ) bool ~ absent ~ doc
let debug = let doc = " Debug build . Save debugging information in build artefacts . This \ key should not be specified explicitly in your package build \ instructions . " in key " debug " bool ~ env " : TOPKG_CONF_DEBUG " ~ absent : true ~ doc
let debugger_support = let doc = " Debugger support . Build and install build artefacts needed \ by debuggers . This key should not be specified explicitly in \ your package build instructions . " in key " debugger - support " bool ~ env " : TOPKG_CONF_DEBUGGER_SUPPORT " ~ absent : false ~ doc
let profile = let doc = " Profiling build . Include run - time profiling support in build \ artefacts . This key should not be specified explicitly \ in your package build instructions . " in key " profile " bool ~ env " : TOPKG_CONF_PROFILE " ~ absent : false ~ doc
let toolchain = let doc = " Specifies the ocamlfind toolchain . " in key " toolchain " ( some string ) string ~ env " : TOPKG_CONF_TOOLCHAIN " ~ absent : None ~ doc
let pp_cli_opt ppf opt_name absent env doc docv = let prf = Format . fprintf in let pp_doc ppf doc = let subst = function " docv " -> docv | s -> Topkg_string . strf ( " $% s ) s " s in let b = Buffer . create 244 in let doc = try Buffer . add_substitute b subst doc ; Buffer . contents b with Not_found -> doc in Topkg_string . pp_text ppf doc in let pp_absent absent env ppf ( ) = match absent , env with | " " , None -> ( ) | " " , Some var -> prf ppf " @ ( or % s env ) env " var | absent , None -> prf ppf " @ ( absent =% s ) s " absent | absent , Some var -> prf ppf " @ ( absent =% s or % s env ) env " absent var in prf ppf [ " @< v4 [ >@% s % s % a ] [ , @@@% a ] ] " @@ opt_name docv ( pp_absent absent env ) env ( ) pp_doc doc
let pp_key ppf k = let absent = match k . absent with | Discover _ -> " discovered " | Value v -> Topkg_string . strf [ " @< h >% a ] " @ ( conv_printer k . conv ) conv v in let opt_name = match k . name with | " pkg - name " -> " - n NAME , -- pkg - name " | n -> Topkg_string . strf " --% s " n in let docv = conv_docv k . conv in pp_cli_opt ppf opt_name absent k . env k . doc docv
let pp_keys_cli_opts ppf ( ) = let pp_key is_first ( Key . V k ) k = if is_first then ( ) else Format . pp_print_cut ppf ( ) ; pp_key ppf k ; false in let by_name ( Key . V k ) k ( Key . V k ' ) k ' = compare k . name k ' . name in let keys = List . sort by_name ( Kset . elements ! key_index ) key_index in Format . fprintf ppf [ " @< v " ; > ignore ( List . fold_left pp_key true keys ) keys ; Format . fprintf ppf ] " " ; @ ( )
type t = univ Kmap . t
let mem k c = Kmap . mem ( Key . V k ) k c
let add k v c = Kmap . add ( Key . V k ) k ( k . to_univ v ) v c
let rem k c = Kmap . remove ( Key . V k ) k c
let find k c = try k . of_univ ( Kmap . find ( Key . V k ) k c ) c with Not_found -> None
let value c k = match find k c with invalid_arg ( Topkg_string . strf " configuration key % s undefined , did you create a key after the call to Pkg . describe " ? k . name ) name
let pp_value c ppf k = k . conv . print ppf ( value c k ) k
let dump ppf c = let dump_binding ( Key . V k ) k v is_first = if is_first then ( ) else Format . pp_print_cut ppf ( ) ; match k . of_univ v with | None -> assert false | Some v -> Format . fprintf ppf " % s : [ @% a ] " @ k . name ( conv_printer k . conv ) conv v ; false in Format . fprintf ppf [ " @< v " ; > ignore ( Kmap . fold dump_binding c true ) true ; Format . fprintf ppf ] " " ; @ ( )
let of_cli_args ~ pkg_name : name ~ build_dir : bdir args = let cli_opts = cli_opts_of_key_index ( ) in let cli_opts = ( " - n " , Key . V pkg_name ) pkg_name :: cli_opts in let strf = Topkg_string . strf in let rec parse_keys conf = function | key :: def :: defs -> begin match try Some ( List . assoc key cli_opts ) cli_opts with Not_found -> None with | None -> failwith ( Topkg_string . strf " key % s : Unknown key . " key ) key | Some ( Key . V k ) k -> if mem k conf then failwith ( strf " key % s : Repeated definition . " key ) key else match ( conv_parser k . conv ) conv def with | Ok v -> parse_keys ( add k v conf ) conf defs | Error ( ` Msg e ) e -> failwith ( strf " key % s : % s . " key e ) e end | [ ] -> conf | key :: [ ] -> failwith ( strf " key % s : No value specified . " key ) key in let add_if_absent ( Key . V k ) k conf = if mem k conf then conf else add k ( key_absent_value k ) k conf in let ensure_pkg_name_and_bdir conf = let conf = if mem pkg_name conf then conf else add pkg_name name conf in if mem build_dir conf then conf else add build_dir bdir conf in try let cli_conf = ensure_pkg_name_and_bdir ( parse_keys empty args ) args in Ok ( Kset . fold add_if_absent ! key_index cli_conf ) cli_conf with | Failure e -> R . error_msg e
let pkg_name c = value c pkg_name
let build_dir c = value c build_dir
let toolchain c = value c toolchain
let vcs c = value c vcs
let pinned c = value c pinned
let dev_pkg c = value c dev_pkg
type build_context = [ ` Dev | ` Distrib | ` Pin ]
let build_context c = if not ( vcs c ) c then ` Distrib else if ( pinned c || dev_pkg c ) c then ` Pin else ` Dev
let build_tests c = match value c tests with match build_context c with | ` Dev -> true | _ -> false
let debug c = value c debug
let debugger_support c = value c debugger_support
let profile c = value c profile
type os = [ ` Build_os | ` Host_os ]
let os_tool_env name os = let pre = match os with ` Build_os -> " BUILD_OS_ " | ` Host_os -> " HOST_OS_ " in pre ^ Topkg_string . uppercase_ascii name
let ocamlfindable ? conf name os = match name with let toolchain = match conf with | None -> Topkg_cmd . empty | Some c -> match os , toolchain c with | ` Host_os , Some toolchain -> Topkg_cmd ( . v " - toolchain " % toolchain ) toolchain | _ -> Topkg_cmd . empty in Some Topkg_cmd ( . v " ocamlfind " %% toolchain % tool ) tool
let tool ? conf name os = match Topkg_os . Env . var ( os_tool_env name os ) os with match Topkg_os . Env . var ( os_bin_dir_env os ) os with | Some path -> Topkg_cmd . v Topkg_fpath ( . path // name ) name | None -> match Topkg_os . Env . var ( os_suff_env os ) os with | Some suff -> Topkg_cmd . v ( name ^ suff ) suff | None -> match ocamlfindable ? conf name os with | Some cmd -> cmd | None -> Topkg_cmd . v name