File: arg.ml

package info (click to toggle)
js-of-ocaml 5.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 32,020 kB
  • sloc: ml: 91,250; javascript: 57,289; ansic: 315; makefile: 271; lisp: 23; sh: 6; perl: 4
file content (127 lines) | stat: -rw-r--r-- 4,033 bytes parent folder | download
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
(* Js_of_ocaml compiler
 * http://www.ocsigen.org/js_of_ocaml/
 * Copyright (C) 2014 Hugo Heuzard
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, with linking exception;
 * either version 2.1 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *)

open Js_of_ocaml_compiler
open Js_of_ocaml_compiler.Stdlib
open Cmdliner

type 'a on_off =
  { enable : 'a
  ; disable : 'a
  }

type t =
  { debug : string list on_off
  ; optim : string list on_off
  ; quiet : bool
  ; werror : bool
  ; custom_header : string option
  }

let debug =
  lazy
    (let doc = "enable debug [$(docv)]." in
     let all = List.map (Debug.available ()) ~f:(fun s -> s, s) in
     let arg =
       Arg.(value & opt_all (list (enum all)) [] & info [ "debug" ] ~docv:"SECTION" ~doc)
     in
     Term.(const List.flatten $ arg))

let enable =
  lazy
    (let doc = "Enable optimization [$(docv)]." in
     let all = List.map (Config.Flag.available ()) ~f:(fun s -> s, s) in
     let arg =
       Arg.(value & opt_all (list (enum all)) [] & info [ "enable" ] ~docv:"OPT" ~doc)
     in
     Term.(const List.flatten $ arg))

let disable =
  lazy
    (let doc = "Disable optimization [$(docv)]." in
     let all = List.map (Config.Flag.available ()) ~f:(fun s -> s, s) in
     let arg =
       Arg.(value & opt_all (list (enum all)) [] & info [ "disable" ] ~docv:"OPT" ~doc)
     in
     Term.(const List.flatten $ arg))

let pretty =
  let doc = "Pretty print the output." in
  Arg.(value & flag & info [ "pretty" ] ~doc)

let debuginfo =
  let doc = "Output debug information." in
  Arg.(value & flag & info [ "debuginfo"; "debug-info" ] ~doc)

let noinline =
  let doc = "Disable inlining." in
  Arg.(value & flag & info [ "noinline"; "no-inline" ] ~doc)

let is_quiet =
  let doc = "suppress non-error messages." in
  Arg.(value & flag & info [ "quiet"; "q" ] ~doc)

let is_werror =
  let doc = "turn all warnings into errors." in
  Arg.(value & flag & info [ "Werror" ] ~doc)

let custom_header =
  let doc =
    "Provide a custom header for the generated JavaScript file, useful for making the \
     script an executable file with #!/usr/bin/env node"
  in
  Arg.(value & opt (some string) None & info [ "custom-header" ] ~doc)

let t =
  lazy
    Term.(
      const (fun debug enable disable pretty debuginfo noinline quiet werror c_header ->
          let enable = if pretty then "pretty" :: enable else enable in
          let enable = if debuginfo then "debuginfo" :: enable else enable in
          let disable = if noinline then "inline" :: disable else disable in
          let disable_if_pretty name disable =
            if pretty && not (List.mem name ~set:enable) then name :: disable else disable
          in
          let disable = disable_if_pretty "shortvar" disable in
          let disable = disable_if_pretty "share" disable in
          { debug = { enable = debug; disable = [] }
          ; optim = { enable; disable }
          ; quiet
          ; werror
          ; custom_header = c_header
          })
      $ Lazy.force debug
      $ Lazy.force enable
      $ Lazy.force disable
      $ pretty
      $ debuginfo
      $ noinline
      $ is_quiet
      $ is_werror
      $ custom_header)

let on_off on off t =
  List.iter ~f:on t.enable;
  List.iter ~f:off t.disable

let eval t =
  Config.Flag.(on_off enable disable t.optim);
  Debug.(on_off enable disable t.debug);
  quiet := t.quiet;
  werror := t.werror