1 (* Copyright (C
) 2005-2008 Henry Cejtin
, Matthew Fluet
, Suresh
2 * Jagannathan
, and Stephen Weeks
.
4 * MLton is released under a BSD
-style license
.
5 * See the file MLton
-LICENSE for details
.
8 structure Main
: sig val main
: unit
-> unit
end =
12 RegExpFn (structure P
= AwkSyntax
13 structure E
= DfaEngine
)
15 fun makeOptions
{usage
} =
20 ([(Expert
, "debug", " {false|true}", "",
22 (Normal
, "allSU", " {false|true}",
23 "generate ML definitions for all #include-d struct and union definitions",
25 (Normal
, "collect", " {true|false}",
26 "collect enum constants from unnamed enumerateions",
27 boolRef collect_enums
),
28 (Normal
, "cppopt", " <opt>",
29 "pass option to preprocessor",
30 SpaceString (fn s
=> List.push (cppopts
, s
))),
31 (Normal
, "dir", " <dir>",
32 "output directory for generated files",
33 SpaceString (fn s
=> dir
:= s
)),
34 (Normal
, "enum-constructors", " {false|true}",
35 "when possible, make the ML representation type of enumerations a datatype",
37 (Normal
, "gensym", " <string>",
38 "suffix for \"gensym-ed\" generated ML structure names",
39 SpaceString (fn s
=> gensym
:= s
)),
41 "suppress 'light' versions of function wrappers and field accessors",
42 None (fn () => weight
:= {heavy
= true, light
= false})),
43 (Normal
, "include", " <file>",
44 "include file in the generated .mlb file",
45 SpaceString (fn s
=> List.push (extramembers
, s
))),
46 (Normal
, "libhandle", " <longid>",
47 "Use the <longid> to refer to the handle to the shared library",
48 SpaceString (fn s
=> libhandle
:= s
)),
50 "suppress 'heavy' versions of function wrappers and field accessors",
51 None (fn () => weight
:= {heavy
= false, light
= true})),
52 (Normal
, "linkage", " {archive|dynamic|shared}",
53 "how to link C objects",
55 if s
= "archive" orelse s
= "static"
56 then linkage
:= Linkage
.Archive
58 then linkage
:= Linkage
.Dynamic
60 then linkage
:= Linkage
.Shared
61 else usage (concat
["invalid -linkage arg: ", s
]))),
62 (Normal
, "match", " <re>",
63 "generate ML definitions for #include-d definitions matching <re>",
67 SOME (RE
.compileString re
)
68 handle RegExpSyntax
.CannotParse
=> NONE
69 | RegExpSyntax
.CannotCompile
=> NONE
74 val scanFn
= RE
.prefix regexp
77 val n
= String.length s
80 then SOME (String.sub (s
, i
), i
+ 1)
85 |
SOME (x
, k
) => k
= n
90 | NONE
=> usage (concat
["invalid -match arg: ", re
])
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",
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",
104 (case Target
.fromString s
of
106 usage (concat
["invalid -target arg: ", s
])
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",
114 fn (style
, name
, arg
, desc
, opt
) =>
115 {arg
= arg
, desc
= desc
, name
= name
, opt
= opt
, style
= style
})
118 val mainUsage
= "mlnlffigen [option ...] C-file ..."
120 Popt
.makeUsage
{mainUsage
= mainUsage
,
121 makeOptions
= makeOptions
,
122 showExpert
= fn () => !Control
.debug
}
124 val die
= Process
.fail
126 fun commandLine args
=
128 val rest
= parse args
129 val () = if Option
.isNone (!Control
.target
)
130 then usage
"no -target specified"
134 Result
.No msg
=> usage msg
135 | Result
.Yes
[] => usage
"no C-file(s)"
136 | Result
.Yes cfiles
=> Gen
.gen
{cfiles
= cfiles
}
139 val main
= Process
.makeMain commandLine