Debian Codegraph @

mlnlffigen/main.sml

raw
1 (* Copyright (C) 2020,2022 Matthew Fluet.
2 * Copyright (C) 2005-2008 Henry Cejtin, Matthew Fluet, Suresh
3 * Jagannathan, and Stephen Weeks.
4 *
5 * MLton is released under a HPND-style license.
6 * See the file MLton-LICENSE for details.
7 *)
8
9 structure Main : sig val main : unit -> unit end =
10 struct
11
12 structure RE =
13 RegExpFn (structure P = AwkSyntax
14 structure E = DfaEngine)
15
16 fun makeOptions {usage} =
17 let
18 open Popt Control
19 in
20 List.map
21 ([(Expert, "debug", " {false|true}", "",
22 boolRef debug),
23 (Normal, "allSU", " {false|true}",
24 "generate ML definitions for all #include-d struct and union definitions",
25 boolRef allSU),
26 (Normal, "collect", " {true|false}",
27 "collect enum constants from unnamed enumerateions",
28 boolRef collect_enums),
29 (Normal, "cppopt", " <opt>",
30 "pass option to preprocessor",
31 SpaceString (fn s => List.push (cppopts, s))),
32 (Normal, "dir", " <dir>",
33 "output directory for generated files",
34 SpaceString (fn s => dir := s)),
35 (Normal, "enum-constructors", " {false|true}",
36 "when possible, make the ML representation type of enumerations a datatype",
37 boolRef enum_cons),
38 (Normal, "gensym", " <string>",
39 "suffix for \"gensym-ed\" generated ML structure names",
40 SpaceString (fn s => gensym := s)),
41 (Normal, "heavy", "",
42 "suppress 'light' versions of function wrappers and field accessors",
43 None (fn () => weight := {heavy = true, light = false})),
44 (Normal, "include", " <file>",
45 "include file in the generated .mlb file",
46 SpaceString (fn s => List.push (extramembers, s))),
47 (Normal, "libhandle", " <longid>",
48 "Use the <longid> to refer to the handle to the shared library",
49 SpaceString (fn s => libhandle := s)),
50 (Normal, "light", "",
51 "suppress 'heavy' versions of function wrappers and field accessors",
52 None (fn () => weight := {heavy = false, light = true})),
53 (Normal, "linkage", " {archive|dynamic|shared}",
54 "how to link C objects",
55 SpaceString (fn s =>
56 if s = "archive" orelse s = "static"
57 then linkage := Linkage.Archive
58 else if s = "dynamic"
59 then linkage := Linkage.Dynamic
60 else if s = "shared"
61 then linkage := Linkage.Shared
62 else usage (concat ["invalid -linkage arg: ", s]))),
63 (Normal, "match", " <re>",
64 "generate ML definitions for #include-d definitions matching <re>",
65 SpaceString (fn re =>
66 let
67 val regexp =
68 SOME (RE.compileString re)
69 handle RegExpSyntax.CannotCompile => NONE
70 in
71 case regexp of
72 SOME regexp =>
73 let
74 val scanFn = RE.prefix regexp
75 fun matchFn s =
76 let
77 val n = String.length s
78 fun getc i =
79 if (i < n)
80 then SOME (String.sub (s, i), i + 1)
81 else NONE
82 in
83 case scanFn getc 0 of
84 NONE => false
85 | SOME (x, k) => k = n
86 end
87 in
88 match := matchFn
89 end
90 | NONE => usage (concat ["invalid -match arg: ", re])
91 end)),
92 (Normal, "mlbfile", " <file>",
93 "name of the generated .mlb file",
94 SpaceString (fn s => mlbfile := s)),
95 (Normal, "namedargs", " {false|true}",
96 "generate function wrappers with named arguments",
97 boolRef namedargs),
98 (Normal, "prefix", " <string>",
99 "prefix for generated ML structure names",
100 SpaceString (fn s => prefix := s)),
101 (Normal, "target", " <arch>-<os>",
102 "platform that executable will run on",
103 SpaceString (fn s =>
104 (case Target.fromString s of
105 NONE =>
106 usage (concat ["invalid -target arg: ", s])
107 | SOME t =>
108 (case Target.make t of
109 NONE => usage (concat ["unsupported -target arg: ", s])
110 | SOME z => target := SOME z)))),
111 (Normal, "width", " 75",
112 "output line width for pretty-printing",
113 intRef width)],
114 fn (style, name, arg, desc, opt) =>
115 {arg = arg, desc = desc, name = name, opt = opt, style = style})
116 end
117
118 val mainUsage = "mlnlffigen [option ...] C-file ..."
119 val {parse, usage} =
120 Popt.makeUsage {mainUsage = mainUsage,
121 makeOptions = makeOptions,
122 showExpert = fn () => !Control.debug}
123
124 fun commandLine (_, args) =
125 let
126 val rest = parse args
127 val () = if Option.isNone (!Control.target)
128 then usage "no -target specified"
129 else ()
130 in
131 case rest of
132 Result.No msg => usage msg
133 | Result.Yes [] => usage "no C-file(s)"
134 | Result.Yes cfiles => Gen.gen {cfiles = cfiles}
135 end
136
137 val main = CommandLine.make commandLine
138
139 end