text
stringlengths
0
601k
let get_ncpus ( ) = let on_fail ( ) = 1 in let decode_int = Topkg_codec ( . dec_result int ) int in if Sys . win32 then match Topkg_os . Env . var " NUMBER_OF_PROCESSORS " with | Some s -> decode_int s |> Topkg_log . on_error_msg ~ level : Topkg_log . Debug ~ use : on_fail | None -> on_fail ( ) else let run_tool name args ~ on_fail = Topkg_log . on_error_msg ~ level : Topkg_log . Debug ~ use : on_fail @@ Topkg_os . Cmd ( . run_out Topkg_cmd ( . tool name ` Build_os %% of_list args ) args |> out_string |> success >>= decode_int ) decode_int in run_tool " getconf " [ " _NPROCESSORS_ONLN ] " ~ on_fail ( : fun ( ) -> run_tool " sysctl " [ " - n " ; " hw . ncpu ] " ~ on_fail ) on_fail
let jobs = let doc = " Allow to run ( $ docv ) docv commands at once when building . " in let absent ( ) = Ok None in discovered_key " jobs " ( some int ) int ~ absent ~ doc ~ docv " : JOBS "
let jobs c = match value c jobs with match build_context c with | ` Dev -> get_ncpus ( ) | _ -> default_jobs
module OCaml = struct type conf = t let conf fmt = " OCaml % s conf : " ^^ fmt let conf_key fmt = conf ( " key % s : " ^^ fmt ) fmt type t = { os : os ; mutable conf : ( string * string ) string list ; } let empty os = { os ; conf = [ ] } let read_config c os = let parse_line acc l = match Topkg_string . cut ~ sep : ' : ' l with | Some ( k , v ) v -> ( k , String . trim v ) v :: acc | None -> Topkg_log . warn ( fun m -> m ( conf " cannot parse line % S ) " ( os_to_string os ) os l ) l ; acc in begin let ocamlc = tool ? conf : c " ocamlc " os in Topkg_os . Cmd ( . run_out Topkg_cmd ( . ocamlc % " - config ) " |> to_lines ) to_lines >>= fun lines -> Ok ( List ( . rev ( fold_left parse_line [ ] lines ) lines ) lines ) lines >>= fun conf -> Ok { os ; conf } end |> R . reword_error_msg ~ replace : true ( fun msg -> R . msgf ( conf " % s ) " ( os_to_string os ) os msg ) msg |> Topkg_log . on_error_msg ~ level : Topkg_log . Warning ~ use ( : fun ( ) -> empty os ) os let cache = Hashtbl . create 2 let v c os = try Hashtbl . find cache ( c , os ) os with Not_found -> let config = read_config ( Some c ) c os in Hashtbl . add cache ( c , os ) os config ; config let add_discovery k v c = c . conf <- ( k , v ) v :: c . conf let find k c = try Some ( List . assoc k c . conf ) conf with Not_found -> None let get ~ absent k c = match find k c with | Some v -> v | None -> Topkg_log . warn ( fun m -> m ( conf_key " undefined , using % S ) " ( os_to_string c . os ) os k absent ) absent ; absent let get_string_with_discovery k c ~ discover = match find k c with | Some v -> v | None -> let v = discover k c in add_discovery k v c ; v let get_bool_with_discovery k c ~ discover = let maybe_v = match find k c with | None -> None | Some v -> try Some ( bool_of_string v ) v with | Invalid_argument _ -> Topkg_log . warn ( fun m -> m ( conf_key " could not parse boolean , @ trying to discover ) " ( os_to_string c . os ) os k ) k ; None in match maybe_v with | Some v -> v | None -> let v = discover k c in add_discovery k ( string_of_bool v ) v c ; v let get_int_with_discovery k c ~ discover = let maybe_v = match find k c with | None -> None | Some v -> try Some ( int_of_string v ) v with | Failure _ -> Topkg_log . warn ( fun m -> m ( conf_key " could not parse integer , @ trying to discover ) " ( os_to_string c . os ) os k ) k ; None in match maybe_v with | Some v -> v | None -> let v = discover k c in add_discovery k ( string_of_int v ) v c ; v let find_stdlib c = find " standard_library " c let get_bool_stdlib_file_exists_discovery k c ~ file ~ on_error = get_bool_with_discovery k c ~ discover : begin fun k c -> match find_stdlib c with | None -> Topkg_log . warn ( fun m -> m ( conf_key " undefined , stdlib dir not found for discovery @ using % B ) " ( os_to_string c . os ) os k on_error ) on_error ; on_error | Some stdlib_dir -> match Topkg_os . File . exists ( Topkg_fpath ( . stdlib_dir // file c ) c ) c with | Ok exist -> exist | Error ( ` Msg e ) e -> Topkg_log . warn ( fun m -> m ( conf_key " undefined , @ discovery error : % s , @ using % B ) " ( os_to_string c . os ) os k e on_error ) on_error ; on_error end let version c = let dumb_version = 0 , 0 , 0 , None in let k = " version " in match find k c with | None -> Topkg_log . warn ( fun m -> m ( conf_key " missing , using 0 . 0 . 0 ) " ( os_to_string c . os ) os k ) k ; dumb_version | Some version -> match Topkg_string . parse_version version with | Some version -> version | None -> Topkg_log . warn ( fun m -> m ( conf_key " cannot parse from % S , using 0 . 0 . 0 ) " ( os_to_string c . os ) os k version ) version ; dumb_version let ext_obj c = get ~ absent " . : o " " ext_obj " c let ext_asm c = get ~ absent " . : s " " ext_asm " c let ext_lib c = get ~ absent " . : a " " ext_lib " c let ext_dll c = get ~ absent " . : so " " ext_dll " c let ext_exe c = get_string_with_discovery " ext_exe " c ~ discover : begin fun k c -> let find_c_toolchain c = match find " ccomp_type " c , find " os_type " c with | None , _ | _ , None -> None | Some ccomp_type , Some os_type -> match ccomp_type , os_type with | " msvc " , _ -> Some ` Win_msvc | " cc " , " Win32 " -> Some ` Win_cc | _ , _ -> Some ` Other in match find_c_toolchain c with | Some ( ` Win_msvc | ` Win_cc ) Win_cc -> " . exe " | Some ` Other -> " " | None -> Topkg_log . warn ( fun m -> m ( conf_key " undefined and @ no C toolchain @ detected , @ using ) " " " \\ ( os_to_string c . os ) os k ) ; ; " " end let native c = let file c = " libasmrun " ^ ( ext_lib c ) c in get_bool_stdlib_file_exists_discovery " native " c ~ file ~ on_error : false let native_dynlink c = let file _ = " dynlink . cmxa " in get_bool_stdlib_file_exists_discovery " natdynlink " c ~ file ~ on_error : false let supports_shared_libraries c = let key = " supports_shared_libraries " in let asm_dynlib c = " libasmrun_shared " ^ ( ext_dll c ) c in let caml_dynlib c = " libcamlrun_shared " ^ ( ext_dll c ) c in let on_error = false in get_bool_stdlib_file_exists_discovery key c ~ file : asm_dynlib ~ on_error || get_bool_stdlib_file_exists_discovery key c ~ file : caml_dynlib ~ on_error let word_size c = let sizeof_ptr_of_config_h file = let err l = R . error_msgf " could not parse SIZEOF_PTR from % S in % s " l file in let rec parse = function | [ ] -> R . error_msgf " could not find SIZEOF_PTR in % s " file | l :: ls -> let l = Topkg_string . trim l in let affix = " # define SIZEOF_PTR " in let is_size_of_ptr = Topkg_string . is_prefix ~ affix l in if not is_size_of_ptr then parse ls else match Topkg_string . cut ~ rev : true ~ sep : ' ' l with | None -> err l | Some ( _ , size ) size -> try Ok ( int_of_string size * 8 ) 8 with Failure _ -> err l in Topkg_os . File . read file >>= fun conf -> Ok ( Topkg_string . cuts ~ sep : ' \ n ' conf ) conf >>= fun lines -> parse lines in get_int_with_discovery " word_size " c ~ discover : begin fun k c -> let on_error = 64 in match find_stdlib c with | None -> Topkg_log . warn ( fun m -> m ( conf_key " undefined , stdlib dir not found for discovery @ using % d ) " ( os_to_string c . os ) os k on_error ) on_error ; on_error | Some stdlib_dir -> let config_h = " caml / config . h " in match Topkg_os . File . must_exist Topkg_fpath ( . stdlib_dir // config_h ) config_h >>= fun conf -> sizeof_ptr_of_config_h conf with | Ok v -> v | Error ( ` Msg e ) e -> Topkg_log . warn ( fun m -> m ( conf_key " undefined , @ discovery error : % s , @ using % d ) " ( os_to_string c . os ) os k e on_error ) on_error ; on_error end let dump ppf c = let pp_elt ppf ( k , v ) v = Format . fprintf ppf ( " % S , % S ) S " k v in let rec loop = function | [ ] -> ( ) | v :: vs -> if vs = [ ] then ( Format . fprintf ppf [ " @% a ] " @ pp_elt v ) v else ( Format . fprintf ppf [ " @% a ] ; @@ " pp_elt v ; loop vs ) vs in Format . fprintf ppf [ " @< 1 [ " ; > loop c . conf ; Format . fprintf ppf ] ] " " @ end
type watermark = string * [ ` String of string | ` Name | ` Version | ` Version_num | ` Vcs of [ ` Commit_id ] | ` Opam of Topkg_fpath . t option * string * string ]
let opam_fields file = ( Topkg_opam . File . fields file ) file |> R . reword_error_msg ~ replace : true ( fun msg -> R . msgf " Watermarks : % s " msg ) msg |> Topkg_log . on_error_msg ~ level : Topkg_log . Warning ~ use ( : fun ( ) -> [ ] )
let opam_field = let find k m = try Some ( List . assoc k m ) m with Not_found -> None in let opam_memo = ref [ ] in let rec opam_field file field = match find file ! opam_memo with | None -> opam_memo := ( file , ( opam_fields file ) file ) file :: ! opam_memo ; opam_field file field | Some fields -> match find field fields with | Some vs -> vs | None -> Topkg_log . warn ( fun m -> m " file % s : opam field % S undefined or unsupported " file field ) field ; [ " UNDEFINED ] " in opam_field
let vcs_commit_id ( ) = ( Topkg_vcs . get ( ) >>= fun repo -> Topkg_vcs . head ~ dirty : true repo ) repo |> R . reword_error_msg ~ replace : true ( fun msg -> R . msgf " Watermarks : VCS commit id determination : % s " msg ) msg |> Topkg_log . on_error_msg ~ level : Topkg_log . Warning ~ use ( : fun ( ) -> " UNDEFINED ) "
let define_watermarks ~ name ~ version ~ opam watermarks = let define ( id , v ) v = let ( id , v as def ) def = match v with | ` String s -> ( id , s ) s | ` Version -> ( id , version ) version | ` Version_num -> ( id , Topkg_string . drop_initial_v version ) version | ` Name -> ( id , name ) name | ` Vcs ` Commit_id -> ( id , vcs_commit_id ( ) ) | ` Opam ( file , field , sep ) sep -> let file = match file with None -> opam | Some file -> file in ( id , String . concat sep ( opam_field file field ) field ) field in Topkg_log . info ( fun m -> m " Watermark % s = % S " id v ) v ; def in List . map define watermarks
let watermark_file ws file = Topkg_os . File . read file >>= fun content -> Topkg_os . File . write_subst file ws content >>= fun ( ) -> Topkg_log . info ( fun m -> m " Watermarked % s " file ) file ; Ok ( )
let rec watermark_files ws = function
let default_watermarks = let space = " " in let comma = " , " in [ " NAME " , ` Name ; " VERSION " , ` Version ; " VERSION_NUM " , ` Version_num ; " VCS_COMMIT_ID " , ` Vcs ` Commit_id ; " PKG_MAINTAINER " , ` Opam ( None , " maintainer " , comma ) comma ; " PKG_AUTHORS " , ` Opam ( None , " authors " , comma ) comma ; " PKG_HOMEPAGE " , ` Opam ( None , " homepage " , comma ) comma ; " PKG_ISSUES " , ` Opam ( None , " bug - reports " , space ) space ; " PKG_DOC " , ` Opam ( None , " doc " , space ) space ; " PKG_LICENSE " , ` Opam ( None , " license " , comma ) comma ; " PKG_REPO " , ` Opam ( None , " dev - repo " , space ) space ; ]
let default_files_to_watermark = let is_file f = Topkg_os . File . exists f |> Topkg_log . on_error_msg ~ use ( : fun _ -> false ) false in let is_binary_ext ext = let module Set = Set . Make ( String ) String in let exts = Set ( . empty |> add " . eps " |> add " . flv " |> add " . gif " |> add " . ico " |> add " . jpeg " |> add " . jpg " |> add " . mov " |> add " . mp3 " |> add " . mp4 " |> add " . otf " |> add " . pdf " |> add " . png " |> add " . ps " |> add " . ttf " |> add " . woff ) " in Set . mem ext exts in let keep f = not ( is_binary_ext @@ Topkg_fpath . get_ext f ) f && is_file f in fun ( ) -> Topkg_vcs . get ( ) >>= fun repo -> Topkg_vcs . tracked_files repo >>= fun files -> Ok ( List . filter keep files ) files
let default_massage ( ) = Ok ( )
let default_exclude_paths ( ) = Ok [ " . git " ; " . gitignore " ; " . gitattributes " ; " . hg " ; " . hgignore " ; " build " ; " Makefile " ; " _build ] "
type t = { watermarks : watermark list ; files_to_watermark : unit -> Topkg_fpath . t list result ; massage : unit -> unit result ; exclude_paths : unit -> Topkg_fpath . t list result ; uri : string option ; }
let v ( ? watermarks = default_watermarks ) default_watermarks ( ? files_to_watermark = default_files_to_watermark ) default_files_to_watermark ( ? massage = fun ( ) -> Ok ( ) ) ( ? exclude_paths = default_exclude_paths ) default_exclude_paths ? uri ( ) = { watermarks ; files_to_watermark ; massage ; exclude_paths ; uri }
let watermarks d = d . watermarks
let files_to_watermark d = d . files_to_watermark
let massage d = d . massage
let exclude_paths d = d . exclude_paths
let uri d = d . uri
let codec = let uri = Topkg_codec ( . with_kind " uri " @@ option string ) string in let fields = let stub ( ) = invalid_arg " not executable outside package definition " in ( fun d -> d . uri ) uri , ( fun uri -> { watermarks = [ ] ; files_to_watermark = stub ; massage = stub ; exclude_paths = stub ; uri } ) in Topkg_codec . version 0 @@ Topkg_codec ( . view ~ kind " : distrib " fields uri ) uri
let dir_sep_prefix s = Topkg_string . is_prefix ~ affix : Filename . dir_sep s || ( String . length s > 0 && s [ . 0 ] 0 = ' / ' ) '
let dir_sep_suffix s = Topkg_string . is_suffix ~ affix : Filename . dir_sep s || ( String . length s > 0 && s [ . String . length s - 1 ] 1 = ' / ' ) '
let append = fun p q -> match p with | " " -> q | p -> match q with | " " -> p | q -> if dir_sep_prefix q then q else if dir_sep_suffix p then ( p ^ q ) q else ( p ^ " " / ^ q ) q
let ( // ) = append
let is_dir_path p = match p with let is_suffix affix = Topkg_string . is_suffix ~ affix p in List . exists is_suffix [ " " ; / " . . " ; / ] " . " /
let is_file_path p = not ( is_dir_path p ) p
let basename s = Filename . basename s
let dirname s = Filename . dirname s
let last_dot_index s = try Some ( String . rindex s ' . ' ) ' with Not_found -> None
let get_ext s = match last_dot_index s with
let has_ext e p = Topkg_string . is_suffix ~ affix : e p
let rem_ext s = match last_dot_index s with
type move_scheme = { field : [ ` Test of bool * Topkg_fpath . t option * Topkg_cmd . t | Topkg_opam . Install . field ] ; auto_bin : bool ; force : bool ; built : bool ; exts : Topkg_fexts . t ; src : string ; dst : string ; debugger_support : bool ; }
type t = move_scheme list
let flatten ls = let rec push acc = function v :: vs -> push ( v :: acc ) acc vs | [ ] -> acc in let rec loop acc = function | l :: ls -> loop ( push acc l ) l ls | [ ] -> acc in loop [ ] ls
let split_ext s = match Topkg_string . cut ~ rev : true s ~ sep : ' . ' with
let bin_drop_exts native = if native then [ ] else Topkg_fexts . ext " . native "
let lib_drop_exts native native_dynlink supports_shared_libraries = match native with | false -> Topkg_fexts ( . c_library @ exts [ " . cmx " ; " . cmxa " ; " . cmxs ] ) " | true -> match supports_shared_libraries with | false -> ` Dll :: Topkg_fexts . ext " . cmxs " | true -> match native_dynlink with | false -> Topkg_fexts . ext " . cmxs " | true -> [ ]
let to_build ? header c os i = let bdir = Topkg_conf . build_dir c in let debugger_support = Topkg_conf . debugger_support c in let build_tests = Topkg_conf . build_tests c in let ocaml_conf = Topkg_conf . OCaml . v c os in let native = Topkg_conf . OCaml . native ocaml_conf in let native_dylink = Topkg_conf . OCaml . native_dynlink ocaml_conf in let supports_shared_libraries = Topkg_conf . OCaml . supports_shared_libraries ocaml_conf in let ext_to_string = Topkg_fexts . ext_to_string ocaml_conf in let file_to_str ( n , ext ) ext = Topkg_string . strf " % s % s " n ( ext_to_string ext ) ext in let maybe_build = [ " . cmti " ; " . cmt " ] in let bin_drops = List . map ext_to_string ( bin_drop_exts native ) native in let lib_drops = List . map ext_to_string ( lib_drop_exts native native_dylink supports_shared_libraries ) supports_shared_libraries in let add acc m = if m . debugger_support && not debugger_support then acc else let mv ( targets , moves , tests as acc ) acc src dst = let src = file_to_str src in let drop = not m . force && match m . field with | ` Bin -> List . exists ( Filename . check_suffix src ) src bin_drops | ` Lib | ` Lib_root | ` Stublibs -> List . exists ( Filename . check_suffix src ) src lib_drops | _ -> false in if drop then ( targets , moves , tests ) tests else let dst = file_to_str dst in let maybe = List . exists ( Filename . check_suffix src ) src maybe_build in let targets = if m . built && not maybe then src :: targets else targets in let src = if m . built then Topkg_string . strf " % s /% s " bdir src else src in match m . field with | ` Test ( run , dir , args ) args -> if not build_tests then acc else let test = Topkg_test . v src ~ args ~ run ~ dir in ( targets , moves , test :: tests ) tests | # Topkg_opam . Install . field as field -> let move = ( field , Topkg_opam . Install . move ~ maybe src ~ dst ) dst in ( targets , move :: moves , tests ) tests in let src , dst = if not m . auto_bin then m . src , m . dst else ( ( if native then m . src ^ " . native " else m . src ^ " . byte ) " , m . dst ^ ( ext_to_string ` Exe ) Exe ) Exe in if m . exts = [ ] then mv acc ( split_ext src ) src ( split_ext dst ) dst else let expand acc ext = mv acc ( src , ext ) ext ( dst , ext ) ext in List . fold_left expand acc m . exts in let targets , moves , tests = List . fold_left add ( [ ] , [ ] , [ ] ) ( flatten i ) i in let tests = if build_tests then Some tests else None in targets , ( ( ` Header header ) header , moves ) moves , tests
type field = ? force : bool -> ? built : bool -> ? cond : bool -> ? exts : Topkg_fexts . t -> ? dst : string -> string -> t
let _field field ( ? debugger_support = false ) false ( ? auto = true ) true ( ? force = false ) false ( ? built = true ) true ( ? cond = true ) true ( ? exts = [ ] ) ? dst src = if not cond then [ ] else let dst = match dst with | None -> Topkg_fpath . basename src | Some dst -> if Topkg_fpath . is_file_path dst then dst else dst ^ ( Topkg_fpath . basename src ) src in [ { field ; auto_bin = auto ; force ; built ; exts ; src ; dst ; debugger_support ; } ]
let field field = _field ~ debugger_support : false ~ auto : false field
let field_exec field ? auto ? force ? built ? cond ? exts ? dst src = _field field ~ debugger_support : false ? auto ? force ? built ? cond ? exts ? dst src
let bin = field_exec ` Bin
let doc = field ` Doc
let etc = field ` Etc
let lib = field ` Lib
let lib_root = field ` Lib_root
let libexec = field_exec ` Libexec
let libexec_root = field_exec ` Libexec_root
let man = field ` Man
let misc = field ` Misc
let sbin = field_exec ` Sbin
let share = field ` Share
let share_root = field ` Share_root
let stublibs = field ` Stublibs
let toplevel = field ` Toplevel
let unknown name = field ( ` Unknown name ) name
let test ( ? run = true ) true ? dir ( ? args = Topkg_cmd . empty ) empty = field_exec ( ` Test ( run , dir , args ) args ) args
let parse_mllib contents = let lines = Topkg_string . cuts ~ sep : ' \ n ' contents in let add_mod acc l = let path = String . trim @@ match Topkg_string . cut ~ sep : ' # ' l with | None -> l | Some ( p , _ ) -> p in if path = " " then acc else let mod_name = Topkg_string . capitalize_ascii @@ Topkg_fpath . basename path in ( mod_name , path ) path :: acc in List . fold_left add_mod [ ] lines
let field_of_field field = match ( List . hd ( field ) ) " " . field with | ` Test ( run , dir , args ) args -> assert false | # Topkg_opam . Install . field as field -> field
let mllib ( ? field = lib ) lib ( ? cond = true ) true ( ? cma = true ) true ( ? cmxa = true ) true ( ? cmxs = true ) true ? api ? dst_dir mllib = if not cond then [ ] else let debugger_support_field = _field ~ debugger_support : true ~ auto : false ( field_of_field field ) field in let lib_dir = Topkg_fpath . dirname mllib in let lib_base = Topkg_fpath . rem_ext mllib in let dst f = match dst_dir with | None -> None | Some dir -> Some ( Topkg_fpath . append dir ( Topkg_fpath . basename f ) f ) f in let api mllib_content = let mod_names = List . map fst mllib_content in match api with | None -> mod_names | Some api -> let in_mllib i = List . mem ( Topkg_string . capitalize_ascii i ) i mod_names in let api , orphans = List . partition in_mllib api in let warn o = Topkg_log . warn ( fun m -> m " mllib % s : unknown interface % s " mllib o ) o in List . iter warn orphans ; api in let library = let add_if cond v vs = if cond then v :: vs else vs in let exts = add_if cma ( ` Ext " . cma ) " @@ add_if cmxa ( ` Ext " . cmxa ) " @@ add_if cmxs ( ` Ext " . cmxs ) " @@ add_if ( cmxa || cmxs ) cmxs ` Lib [ ] in field ? dst ( : dst lib_base ) lib_base ~ exts lib_base in let add_mods acc mllib_content = let api = api mllib_content in let add_mod acc ( m , path ) path = let fname = Topkg_string . uncapitalize_ascii ( Topkg_fpath . basename path ) path in let fpath = match Topkg_fpath . dirname path with | " . " -> Topkg_fpath . append lib_dir fname | parent -> Topkg_fpath ( . append lib_dir ( append parent fname ) fname ) fname in let dst = dst fname in let exts , debugger_support_exts = match List . mem m api with | true -> Topkg_fexts . api , Topkg_fexts . exts [ " . ml " ; " . cmt ] " | false -> Topkg_fexts . cmx , Topkg_fexts . exts [ " . ml " ; " . cmi " ; " . cmt " ; ] in field ? dst ~ exts fpath :: debugger_support_field ? dst ~ exts : debugger_support_exts fpath :: acc in List . fold_left add_mod acc mllib_content in begin Topkg_os . File . read mllib >>= fun contents -> Ok ( parse_mllib contents ) contents >>= fun mllib_content -> Ok ( flatten @@ add_mods [ library ] library mllib_content ) mllib_content end |> Topkg_log . on_error_msg ~ use ( : fun ( ) -> [ ] )
let parse_clib contents = let lines = Topkg_string . cuts ~ sep : ' \ n ' contents in let add_obj_path acc l = let path = String . trim @@ match Topkg_string . cut ~ sep : ' # ' l with | None -> l | Some ( p , _ ) -> p in if path = " " then acc else path :: acc in List . fold_left add_obj_path [ ] lines
let clib ( ? dllfield = stublibs ) stublibs ( ? libfield = lib ) lib ( ? cond = true ) true ? lib_dst_dir clib = if not cond then [ ] else let debugger_support_field = _field ~ debugger_support : true ~ auto : false ( field_of_field lib ) lib in let lib_dir = Topkg_fpath . dirname clib in let lib_base = let base = Topkg_fpath ( . basename @@ rem_ext clib ) clib in if Topkg_string . is_prefix ~ affix " : lib " base then Ok ( Topkg_string . with_index_range ~ first : 3 base ) base else R . error_msgf " % s : OCamlbuild . clib file must start with ' lib ' " clib in let lib_dst f = match lib_dst_dir with | None -> None | Some dir -> Some ( Topkg_fpath . append dir ( Topkg_fpath . basename f ) f ) f in let add_debugger_support cobjs = let add_cobj acc path = let fname = Topkg_fpath ( . rem_ext @@ basename path ) path in let fpath = match Topkg_fpath . dirname path with | " . " -> Topkg_fpath . append lib_dir fname | parent -> Topkg_fpath ( . append lib_dir ( append parent fname ) fname ) fname in let dst = lib_dst fname in debugger_support_field ? dst ~ exts ( : Topkg_fexts . ext " . c ) " fpath :: acc in List . fold_left add_cobj [ ] cobjs in begin lib_base >>= fun lib_base -> Topkg_os . File . read clib >>= fun contents -> let cobjs = parse_clib contents in let lib = Topkg_fpath . append lib_dir ( " lib " ^ lib_base ) lib_base in let lib = libfield ~ exts : Topkg_fexts . c_library lib ? dst ( : lib_dst lib ) lib in let dll = Topkg_fpath . append lib_dir ( " dll " ^ lib_base ) lib_base in let dll = dllfield ~ exts : Topkg_fexts . c_dll_library dll in Ok ( flatten @@ lib :: dll :: add_debugger_support cobjs ) cobjs end |> Topkg_log . on_error_msg ~ use ( : fun ( ) -> [ ] )
let codec : t Topkg_codec . t = let fields = ( fun _ -> ( ) ) , ( fun ( ) -> [ ] ) in Topkg_codec . version 0 @@ Topkg_codec ( . view ~ kind " : install " fields unit ) unit
type ' a t = { cmd : Topkg_cmd . t ; codec : ' a Topkg_codec . t ; answer : Topkg_fpath . t ; }
let v ? answer cmd codec = let answer = match answer with | Some a -> a | None -> ( Topkg_os . File . tmp ( ) ) |> R . reword_error_msg ~ replace : true ( fun m -> R . msgf " Could not create IPC answer file : % s , using stdout " m ) m |> Topkg_log . on_error_msg ~ use ( : fun ( ) -> Topkg_os . File . dash ) dash in let cmd = Topkg_cmd ( . v answer %% cmd ) cmd in { cmd ; codec ; answer }
let cmd ipc = ipc . cmd
let codec ipc = ipc . codec
let answer ipc = ipc . answer
let error_args args = R . error_msgf " IPC : % a , unknown arguments " Topkg_cmd . dump ( Topkg_cmd . of_list args ) args
let pkg ( ) = v Topkg_cmd ( . v " pkg ) " Topkg_pkg . codec
let answer_pkg answer p = Topkg_codec . write answer Topkg_pkg . codec p
let lint_custom_codec = Topkg_codec ( . option @@ list @@ result_error_msg @@ msg ) msg
let lint_custom ( ) = let cmd = Topkg_cmd ( . v " lint " % " custom ) " in v cmd lint_custom_codec
let answer_lint_custom answer p = let custom_run = match ( Topkg_pkg . lint_custom p ) p with | None -> None | Some custom -> Some ( custom ( ) ) in Topkg_codec . write answer lint_custom_codec custom_run
let distrib_prepared_codec = Topkg_codec . version 0 @@ Topkg_codec ( . with_kind " prepared " @@ result_error_msg ( list fpath ) fpath ) fpath
let distrib_prepare ~ dist_build_dir ~ name ~ version ~ opam ~ opam_adds = let cmd = Topkg_cmd ( . v " distrib " % " prepare " % " dist - build - dir " % dist_build_dir % " name " % name % " version " % version % " opam " % opam % " opam - adds " % opam_adds ) opam_adds in v cmd distrib_prepared_codec
let answer_distrib_prepare answer p ~ dist_build_dir ~ name ~ version ~ opam ~ opam_adds = Topkg_codec . write answer distrib_prepared_codec @@ Topkg_pkg . distrib_prepare p ~ dist_build_dir ~ name ~ version ~ opam ~ opam_adds
let write_answer cmd p = match Topkg_cmd . to_list cmd with answer_pkg answer p answer_lint_custom answer p " dist - build - dir " :: dist_build_dir :: " name " :: name :: " version " :: version :: " opam " :: opam :: " opam - adds " :: opam_adds :: [ ] -> answer_distrib_prepare answer p ~ dist_build_dir ~ name ~ version ~ opam ~ opam_adds error_args args
type level = App | Error | Warning | Info | Debug
let exec = match Array . length Sys . argv with
let _level = let default = Some Warning in let init = try match Sys . getenv " TOPKG_VERBOSITY " with | l when Topkg_string . is_prefix ~ affix " : quiet " l -> None | l when Topkg_string . is_prefix ~ affix " : error " l -> Some Error | l when Topkg_string . is_prefix ~ affix " : warning " l -> Some Warning | l when Topkg_string . is_prefix ~ affix " : info " l -> Some Info | l when Topkg_string . is_prefix ~ affix " : debug " l -> Some Debug | l -> Format . eprintf " % s : [ @ TOPKG_VERBOSITY env var unknown value : % S ] . " @@ exec l ; default with Not_found | Sys_error _ -> default in ref init
let level ( ) = ! _level
let set_level l = _level := l
type ' a msgf = ( ? header : string -> ( ' a , Format . formatter , unit ) unit format -> ' a ) ' a -> unit
let _err_count = ref 0
let err_count ( ) = ! _err_count
let _warn_count = ref 0
let warn_count ( ) = ! _warn_count
let pp_level_header ppf ( h , l ) l = match h with Format . pp_print_string ppf begin match l with | App -> " " | Error -> [ " ERROR ] ERROR " | Warning -> [ " WARNING ] WARNING " | Info -> [ " INFO ] INFO " | Debug -> [ " DEBUG ] DEBUG " end
let msg level msgf = match ! _level with if level = Error then incr _err_count else if level = Warning then incr _warn_count else ( ) ( if level = Error then incr _err_count else if level = Warning then incr _warn_count else ( ) ) ; let pr = if level = App then Format . printf else Format . eprintf in msgf @@ ( fun ? header fmt -> pr ( " % s : % a [ " @ ^^ fmt ^^ ] ) " . " @@ exec pp_level_header ( header , level ) level ) level
let app msgf = msg App msgf
let err msgf = msg Error msgf
let warn msgf = msg Warning msgf
let info msgf = msg Info msgf
let debug msgf = msg Debug msgf