forked from dbuenzli/zipc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
B0.ml
137 lines (122 loc) · 4.86 KB
/
B0.ml
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
open B0_kit.V000
open Result.Syntax
(* OCaml library names *)
let zipc = B0_ocaml.libname "zipc"
let brr = B0_ocaml.libname "brr" (* Only for test *)
let cmdliner = B0_ocaml.libname "cmdliner"
let unix = B0_ocaml.libname "unix"
let zipc_lib =
let srcs = [ `Dir ~/"src" ] in
let requires = [] in
B0_ocaml.lib zipc ~name:"zipc-lib" ~srcs ~requires
(* Tools *)
let zipc_tool =
let srcs = [`File ~/"test/zipc_tool.ml"] in
let requires = [cmdliner; unix; zipc] in
let meta =
(* FIXME b0: don't let jsoo builds downgrade everything to bytecode *)
B0_meta.empty
|> B0_meta.add B0_ocaml.Code.needs `Native
in
B0_ocaml.exe "zipc" ~meta ~public:true ~srcs ~requires
(* Tests *)
let test ~src ~doc =
let src = Fpath.(v "test" / src) in
let srcs = [ `File src ] in
let requires = [zipc] in
let meta = B0_meta.empty |> B0_meta.(tag test) in
B0_ocaml.exe (Fpath.basename ~no_ext:true src) ~doc ~meta ~srcs ~requires
let test' = test ~src:"test.ml" ~doc:"Basic tests"
let speed_crc_tests = test ~src:"test_crc_speed.ml" ~doc:"CRCs speed test"
let examples = test ~src:"examples.ml" ~doc:"Documentation examples"
let jsoo =
let doc = "Test unzip in the browser" in
let srcs = [ `File ~/"test/jsoo_unzip.ml" ] in
let requires = [brr; zipc] in
let meta = B0_meta.empty |> B0_meta.(tag test) in
B0_jsoo.web "jsoo_unzip" ~doc ~meta ~srcs ~requires
let jsoo =
let doc = "Test zip creation in the browser" in
let srcs = [ `File ~/"test/jsoo_zip.ml" ] in
let requires = [brr; zipc] in
let meta = B0_meta.empty |> B0_meta.(tag test) in
B0_jsoo.web "jsoo_zip" ~doc ~meta ~srcs ~requires
(* Actions *)
let time_inflate =
let doc = "Time unzip and zipc inflation check (-t) on the same archive" in
B0_action.make' "time-inflate" ~units:[zipc_tool] ~doc @@ fun _ env ~args ->
let* args =
if not (Cmd.is_empty args) then Ok args else
let test = B0_env.in_scope_dir env ~/"tmp/silesia.zip" in
let* exists = Os.File.exists test in
if exists then Ok (Cmd.(path test)) else
Fmt.error "Need to specify a ZIP archive"
in
let* time = B0_env.get_cmd env Cmd.(arg "time" % "-h") in
let* zipc = B0_env.unit_cmd env zipc_tool in
let* unzip = B0_env.get_cmd env (Cmd.arg "unzip") in
let stdout = Os.Cmd.out_null in
Log.app (fun m -> m "%a vs %a" Fmt.code' "unzip" Fmt.code' "zipc");
let* () = Os.Cmd.run ~stdout Cmd.(time %% unzip % "-t" %% args) in
let* () = Os.Cmd.run ~stdout Cmd.(time %% zipc % "unzip" % "-t" %% args) in
Ok ()
let zipc_for_each =
let doc = "Invoke zipc on parallel on files in a given null separated list" in
B0_action.make "zipc-for-each" ~units:[zipc_tool] ~doc @@ fun a env ~args ->
let run list args =
Log.if_error ~use:B0_cli.Exit.some_error @@
let* time = B0_env.get_cmd env Cmd.(arg "time" % "-h") in
let* xargs = B0_env.get_cmd env Cmd.(arg "xargs" % "-0" % "-P0" % "-L1") in
let* zipc = B0_env.unit_cmd env zipc_tool in
let stdin = match list with
| "-" -> Os.Cmd.in_stdin | file -> Os.Cmd.in_file (Fpath.v list)
in
let* () = Os.Cmd.run ~stdin Cmd.(time %% xargs %% zipc %% list args) in
Ok B0_cli.Exit.ok
in
(* XXX b0: streamline the arg parsing, do a mini getopts. *)
let open Cmdliner in
let list =
let doc =
"$(docv) is the file with files to invoke $(b,zipc). File paths must \
be null separated. Use $(b,-) for stdin."
in
Arg.(required & pos 0 (some string) None & info [] ~doc ~docv:"FILE")
in
let args' =
let doc =
"$(docv) are $(b,zipc)'s arguments. Specify them after $(b,--)."
in
Arg.(value & pos_right 0 string [] & info [] ~doc ~docv:"ARGS")
in
B0_action.eval_cmdliner_term a env Term.(const run $ list $ args') ~args
(* Packs *)
let default =
let meta =
B0_meta.empty
|> B0_meta.(add authors) ["The zipc programmers"]
|> B0_meta.(add maintainers)
["Daniel Bünzli <daniel.buenzl i@erratique.ch>"]
|> B0_meta.(add homepage) "https://erratique.ch/software/zipc"
|> B0_meta.(add online_doc) "https://erratique.ch/software/zipc/doc"
|> B0_meta.(add licenses) ["ISC"]
|> B0_meta.(add repo) "git+https://erratique.ch/repos/zipc.git"
|> B0_meta.(add issues) "https://github.com/dbuenzli/zipc/issues"
|> B0_meta.(add description_tags)
["codec"; "zip"; "deflate"; "zlib"; "org:erratique"; ]
|> B0_meta.tag B0_opam.tag
|> B0_meta.add B0_opam.build
{|[["ocaml" "pkg/pkg.ml" "build" "--dev-pkg" "%{dev}%"
"--with-cmdliner" "%{cmdliner:installed}%"]]|}
|> B0_meta.add B0_opam.depopts ["cmdliner", ""]
|> B0_meta.add B0_opam.conflicts [ "cmdliner", {|< "1.1.0"|}]
|> B0_meta.add B0_opam.depends
[ "ocaml", {|>= "4.14.0"|};
"ocamlfind", {|build|};
"ocamlbuild", {|build|};
"topkg", {|build & >= "1.0.3"|};
"b0", {|dev & with-test|};
]
in
B0_pack.make "default" ~doc:"The zipc package" ~meta ~locked:true @@
B0_unit.list ()