annotate src/compiler.sml @ 1791:2f8b8952fe27

Add more global mouse event hooks
author Adam Chlipala <adam@chlipala.net>
date Mon, 23 Jul 2012 08:38:39 -0400
parents 85a87f155e7b
children 3d922a28370b
rev   line source
adam@1677 1 (* Copyright (c) 2008-2012, Adam Chlipala
adamc@1 2 * All rights reserved.
adamc@1 3 *
adamc@1 4 * Redistribution and use in source and binary forms, with or without
adamc@1 5 * modification, are permitted provided that the following conditions are met:
adamc@1 6 *
adamc@1 7 * - Redistributions of source code must retain the above copyright notice,
adamc@1 8 * this list of conditions and the following disclaimer.
adamc@1 9 * - Redistributions in binary form must reproduce the above copyright notice,
adamc@1 10 * this list of conditions and the following disclaimer in the documentation
adamc@1 11 * and/or other materials provided with the distribution.
adamc@1 12 * - The names of contributors may not be used to endorse or promote products
adamc@1 13 * derived from this software without specific prior written permission.
adamc@1 14 *
adamc@1 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
adamc@1 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
adamc@1 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
adamc@1 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
adamc@1 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@1 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@1 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@1 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@1 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@1 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@1 25 * POSSIBILITY OF SUCH DAMAGE.
adamc@1 26 *)
adamc@1 27
adamc@1 28 structure Compiler :> COMPILER = struct
adamc@1 29
adamc@244 30 structure UrwebLrVals = UrwebLrValsFn(structure Token = LrParser.Token)
adamc@244 31 structure Lex = UrwebLexFn(structure Tokens = UrwebLrVals.Tokens)
adamc@244 32 structure UrwebP = Join(structure ParserData = UrwebLrVals.ParserData
adamc@1 33 structure Lex = Lex
adamc@1 34 structure LrParser = LrParser)
adamc@1 35
adamc@270 36 type job = {
adamc@385 37 prefix : string,
adamc@270 38 database : string option,
adamc@274 39 sources : string list,
adamc@274 40 exe : string,
adamc@274 41 sql : string option,
adamc@502 42 debug : bool,
adamc@673 43 profile : bool,
adamc@764 44 timeout : int,
adamc@764 45 ffi : string list,
adamc@764 46 link : string list,
adam@1725 47 linker : string option,
adamc@765 48 headers : string list,
adamc@766 49 scripts : string list,
adamc@765 50 clientToServer : Settings.ffi list,
adamc@765 51 effectful : Settings.ffi list,
adamc@1171 52 benignEffectful : Settings.ffi list,
adamc@765 53 clientOnly : Settings.ffi list,
adamc@765 54 serverOnly : Settings.ffi list,
adamc@768 55 jsFuncs : (Settings.ffi * string) list,
adamc@769 56 rewrites : Settings.rewrite list,
adamc@769 57 filterUrl : Settings.rule list,
adamc@866 58 filterMime : Settings.rule list,
adam@1465 59 filterRequest : Settings.rule list,
adam@1465 60 filterResponse : Settings.rule list,
adamc@866 61 protocol : string option,
adamc@1164 62 dbms : string option,
adamc@1183 63 sigFile : string option,
adam@1294 64 safeGets : string list,
adam@1332 65 onError : (string * string list * string) option,
adam@1332 66 minHeap : int
adamc@270 67 }
adamc@201 68
adamc@201 69 type ('src, 'dst) phase = {
adamc@201 70 func : 'src -> 'dst,
adamc@201 71 print : 'dst -> Print.PD.pp_desc
adamc@201 72 }
adamc@201 73
adamc@201 74 type pmap = (string * Time.time) list
adamc@201 75
adamc@201 76 type ('src, 'dst) transform = {
adamc@201 77 func : 'src -> 'dst option,
adamc@201 78 print : 'dst -> Print.PD.pp_desc,
adamc@201 79 time : 'src * pmap -> 'dst option * pmap
adamc@201 80 }
adamc@201 81
adamc@1079 82 val debug = ref false
adam@1677 83 val dumpSource = ref false
adamc@1240 84 val doIflow = ref false
adamc@1079 85
adam@1677 86 val doDumpSource = ref (fn () => ())
adam@1677 87
adamc@201 88 fun transform (ph : ('src, 'dst) phase) name = {
adamc@201 89 func = fn input => let
adamc@1079 90 val () = if !debug then
adamc@1079 91 print ("Starting " ^ name ^ "....\n")
adamc@1079 92 else
adamc@1079 93 ()
adamc@201 94 val v = #func ph input
adamc@201 95 in
adamc@1079 96 if !debug then
adamc@1079 97 print ("Finished " ^ name ^ ".\n")
adamc@1079 98 else
adamc@1079 99 ();
adamc@201 100 if ErrorMsg.anyErrors () then
adam@1677 101 (!doDumpSource ();
adam@1677 102 doDumpSource := (fn () => ());
adam@1677 103 NONE)
adamc@201 104 else
adam@1677 105 (if !dumpSource then
adam@1677 106 doDumpSource := (fn () => Print.eprint (#print ph v))
adam@1677 107 else
adam@1677 108 ();
adam@1677 109 SOME v)
adamc@201 110 end,
adamc@201 111 print = #print ph,
adamc@201 112 time = fn (input, pmap) => let
adamc@1186 113 val () = if !debug then
adamc@1186 114 print ("Starting " ^ name ^ "....\n")
adamc@1186 115 else
adamc@1186 116 ()
adamc@201 117 val befor = Time.now ()
adamc@201 118 val v = #func ph input
adamc@201 119 val elapsed = Time.- (Time.now (), befor)
adamc@201 120 in
adamc@1186 121 if !debug then
adamc@1186 122 print ("Finished " ^ name ^ ".\n")
adamc@1186 123 else
adamc@1186 124 ();
adamc@201 125 (if ErrorMsg.anyErrors () then
adamc@201 126 NONE
adamc@201 127 else
adamc@201 128 SOME v,
adamc@201 129 (name, elapsed) :: pmap)
adamc@201 130 end
adamc@201 131 }
adamc@201 132
adamc@346 133 fun check (tr : ('src, 'dst) transform) x = (ErrorMsg.resetErrors ();
adamc@346 134 ignore (#func tr x))
adamc@346 135
adamc@280 136 fun run (tr : ('src, 'dst) transform) x = (ErrorMsg.resetErrors ();
adamc@280 137 #func tr x)
adamc@201 138
adamc@201 139 fun runPrint (tr : ('src, 'dst) transform) input =
adamc@280 140 (ErrorMsg.resetErrors ();
adamc@280 141 case #func tr input of
adamc@280 142 NONE => print "Failure\n"
adamc@280 143 | SOME v =>
adamc@280 144 (print "Success\n";
adamc@280 145 Print.print (#print tr v);
adamc@280 146 print "\n"))
adamc@201 147
adam@1362 148 fun runPrintToFile (tr : ('src, 'dst) transform) input fname =
adam@1362 149 (ErrorMsg.resetErrors ();
adam@1362 150 case #func tr input of
adam@1362 151 NONE => print "Failure\n"
adam@1362 152 | SOME v =>
adam@1362 153 let
adam@1362 154 val outf = TextIO.openOut fname
adam@1362 155 val str = Print.openOut {dst = outf, wid = 80}
adam@1362 156 in
adam@1362 157 print "Success\n";
adam@1362 158 Print.fprint str (#print tr v);
adam@1362 159 Print.PD.PPS.closeStream str;
adam@1362 160 TextIO.closeOut outf
adam@1362 161 end)
adam@1362 162
adamc@201 163 fun time (tr : ('src, 'dst) transform) input =
adamc@55 164 let
adamc@201 165 val (_, pmap) = #time tr (input, [])
adamc@201 166 in
adamc@201 167 app (fn (name, time) =>
adamc@201 168 print (name ^ ": " ^ LargeReal.toString (Time.toReal time) ^ "\n")) (rev pmap);
adamc@201 169 print ("TOTAL: " ^ LargeReal.toString (Time.toReal (foldl Time.+ Time.zeroTime (map #2 pmap))) ^ "\n");
adamc@201 170 print "\n"
adamc@201 171 end
adamc@55 172
adamc@201 173 fun timePrint (tr : ('src, 'dst) transform) input =
adamc@201 174 let
adamc@201 175 val (ro, pmap) = #time tr (input, [])
adamc@55 176 in
adamc@201 177 app (fn (name, time) =>
adamc@201 178 print (name ^ ": " ^ LargeReal.toString (Time.toReal time) ^ "\n")) (rev pmap);
adamc@201 179 print ("TOTAL: " ^ LargeReal.toString (Time.toReal (foldl Time.+ Time.zeroTime (map #2 pmap))) ^ "\n");
adamc@201 180 print "\n";
adamc@201 181 case ro of
adamc@201 182 NONE => print "Failure\n"
adamc@201 183 | SOME v =>
adamc@201 184 (print "Success\n";
adamc@201 185 Print.print (#print tr v);
adamc@201 186 print "\n")
adamc@55 187 end
adamc@55 188
adam@1362 189 fun runPrintCoreFuncs (tr : ('src, Core.file) transform) input =
adam@1362 190 (ErrorMsg.resetErrors ();
adam@1362 191 case #func tr input of
adam@1362 192 NONE => print "Failure\n"
adam@1362 193 | SOME file =>
adam@1362 194 (print "Success\n";
adam@1362 195 app (fn (d, _) =>
adam@1362 196 case d of
adam@1362 197 Core.DVal (x, _, t, _, _) => Print.preface(x, CorePrint.p_con CoreEnv.empty t)
adam@1362 198 | Core.DValRec xts => app (fn (x, _, t, _, _) => Print.preface(x, CorePrint.p_con CoreEnv.empty t)) xts
adam@1362 199 | _ => ()) file))
adam@1362 200
adamc@244 201 val parseUrs =
adamc@201 202 {func = fn filename => let
adamc@201 203 val fname = OS.FileSys.tmpName ()
adamc@201 204 val outf = TextIO.openOut fname
adamc@201 205 val () = TextIO.output (outf, "sig\n")
adamc@201 206 val inf = TextIO.openIn filename
adamc@201 207 fun loop () =
adamc@201 208 case TextIO.inputLine inf of
adamc@201 209 NONE => ()
adamc@201 210 | SOME line => (TextIO.output (outf, line);
adamc@201 211 loop ())
adamc@201 212 val () = loop ()
adamc@201 213 val () = TextIO.closeIn inf
adamc@201 214 val () = TextIO.closeOut outf
adamc@201 215
adamc@201 216 val () = (ErrorMsg.resetErrors ();
adamc@201 217 ErrorMsg.resetPositioning filename;
adamc@201 218 Lex.UserDeclarations.initialize ())
adamc@201 219 val file = TextIO.openIn fname
adamc@201 220 fun get _ = TextIO.input file
adamc@201 221 fun parseerror (s, p1, p2) = ErrorMsg.errorAt' (p1, p2) s
adamc@201 222 val lexer = LrParser.Stream.streamify (Lex.makeLexer get)
adamc@244 223 val (absyn, _) = UrwebP.parse (30, lexer, parseerror, ())
adamc@201 224 in
adamc@201 225 TextIO.closeIn file;
adamc@201 226 case absyn of
adamc@201 227 [(Source.DSgn ("?", (Source.SgnConst sgis, _)), _)] => sgis
adamc@201 228 | _ => (ErrorMsg.errorAt {file = filename,
adamc@201 229 first = {line = 0,
adamc@201 230 char = 0},
adamc@201 231 last = {line = 0,
adamc@201 232 char = 0}} "Not a signature";
adamc@201 233 [])
adamc@201 234 end
adamc@201 235 handle LrParser.ParseError => [],
adamc@201 236 print = Print.p_list_sep Print.PD.newline SourcePrint.p_sgn_item}
adamc@55 237
adamc@1 238 (* The main parsing routine *)
adamc@244 239 val parseUr = {
adamc@201 240 func = fn filename =>
adamc@201 241 let
adamc@201 242 val () = (ErrorMsg.resetErrors ();
adamc@201 243 ErrorMsg.resetPositioning filename;
adamc@201 244 Lex.UserDeclarations.initialize ())
adamc@201 245 val file = TextIO.openIn filename
adamc@201 246 fun get _ = TextIO.input file
adamc@201 247 fun parseerror (s, p1, p2) = ErrorMsg.errorAt' (p1, p2) s
adamc@201 248 val lexer = LrParser.Stream.streamify (Lex.makeLexer get)
adamc@244 249 val (absyn, _) = UrwebP.parse (30, lexer, parseerror, ())
adamc@201 250 in
adamc@201 251 TextIO.closeIn file;
adamc@201 252 case absyn of
adamc@201 253 [(Source.DSgn ("?", _), _)] =>
adamc@201 254 (ErrorMsg.errorAt {file = filename,
adamc@201 255 first = {line = 0,
adamc@201 256 char = 0},
adamc@201 257 last = {line = 0,
adamc@201 258 char = 0}} "File starts with 'sig'";
adamc@201 259 [])
adamc@201 260 | _ => absyn
adamc@201 261 end
adamc@201 262 handle LrParser.ParseError => [],
adamc@201 263 print = SourcePrint.p_file}
adamc@56 264
adamc@768 265 fun p_job ({prefix, database, exe, sql, sources, debug, profile,
adamc@768 266 timeout, ffi, link, headers, scripts,
adamc@1171 267 clientToServer, effectful, benignEffectful, clientOnly, serverOnly, jsFuncs, ...} : job) =
adamc@270 268 let
adamc@270 269 open Print.PD
adamc@270 270 open Print
adamc@765 271
adamc@765 272 fun p_ffi name = p_list_sep (box []) (fn (m, s) =>
adamc@765 273 box [string name, space, string m, string ".", string s, newline])
adamc@270 274 in
adamc@274 275 box [if debug then
adamc@274 276 box [string "DEBUG", newline]
adamc@274 277 else
adamc@274 278 box [],
adamc@502 279 if profile then
adamc@502 280 box [string "PROFILE", newline]
adamc@502 281 else
adamc@502 282 box [],
adamc@274 283 case database of
adamc@270 284 NONE => string "No database."
adamc@270 285 | SOME db => string ("Database: " ^ db),
adamc@270 286 newline,
adamc@274 287 string "Exe: ",
adamc@274 288 string exe,
adamc@274 289 newline,
adamc@274 290 case sql of
adamc@274 291 NONE => string "No SQL file."
adamc@274 292 | SOME sql => string ("SQL fle: " ^ sql),
adamc@673 293 newline,
adamc@673 294 string "Timeout: ",
adamc@673 295 string (Int.toString timeout),
adamc@673 296 newline,
adamc@764 297 p_list_sep (box []) (fn s => box [string "Ffi", space, string s, newline]) ffi,
adamc@764 298 p_list_sep (box []) (fn s => box [string "Header", space, string s, newline]) headers,
adamc@766 299 p_list_sep (box []) (fn s => box [string "Script", space, string s, newline]) scripts,
adamc@764 300 p_list_sep (box []) (fn s => box [string "Link", space, string s, newline]) link,
adamc@765 301 p_ffi "ClientToServer" clientToServer,
adamc@765 302 p_ffi "Effectful" effectful,
adamc@1171 303 p_ffi "BenignEffectful" benignEffectful,
adamc@765 304 p_ffi "ClientOnly" clientOnly,
adamc@765 305 p_ffi "ServerOnly" serverOnly,
adamc@765 306 p_list_sep (box []) (fn ((m, s), s') =>
adamc@765 307 box [string "JsFunc", space, string m, string ".", string s,
adamc@765 308 space, string "=", space, string s', newline]) jsFuncs,
adamc@270 309 string "Sources:",
adamc@270 310 p_list string sources,
adamc@270 311 newline]
adamc@270 312 end
adamc@270 313
adamc@270 314 fun trim s =
adamc@270 315 let
adamc@270 316 val (_, s) = Substring.splitl Char.isSpace s
adamc@270 317 val (s, _) = Substring.splitr Char.isSpace s
adamc@270 318 in
adamc@270 319 s
adamc@270 320 end
adamc@270 321
adam@1433 322 val trimS = Substring.string o trim o Substring.full
adam@1433 323
adamc@794 324 structure M = BinaryMapFn(struct
adamc@794 325 type ord_key = string
adamc@794 326 val compare = String.compare
adamc@794 327 end)
adamc@794 328
ezyang@1739 329 (* XXX ezyang: pathmap gets initialized /really early/, before
ezyang@1739 330 * we do any options parsing. So libUr will always point to the
ezyang@1739 331 * default. We override it explicitly in enableBoot *)
ezyang@1739 332 val pathmap = ref (M.insert (M.empty, "", Settings.libUr ()))
adamc@1089 333
adamc@1089 334 fun addPath (k, v) = pathmap := M.insert (!pathmap, k, v)
adamc@1089 335
ezyang@1739 336 (* XXX ezyang: this is not right; it probably doesn't work in
ezyang@1739 337 * the case of separate build and src trees *)
ezyang@1739 338 fun enableBoot () =
ezyang@1739 339 (Settings.configBin := OS.Path.joinDirFile {dir = Config.builddir, file = "bin"};
ezyang@1739 340 Settings.configSrcLib := OS.Path.joinDirFile {dir = Config.builddir, file = "lib"};
ezyang@1739 341 (* joinDirFile is annoying... (ArcError; it doesn't like
ezyang@1739 342 * slashes in file) *)
ezyang@1739 343 Settings.configLib := Config.builddir ^ "/src/c/.libs";
ezyang@1739 344 Settings.configInclude := OS.Path.joinDirFile {dir = Config.builddir ^ "/include", file = "urweb"};
ezyang@1739 345 Settings.configSitelisp := Config.builddir ^ "/src/elisp";
ezyang@1739 346 addPath ("", Settings.libUr ()))
ezyang@1739 347
adam@1296 348 fun capitalize "" = ""
adam@1296 349 | capitalize s = str (Char.toUpper (String.sub (s, 0))) ^ String.extract (s, 1, NONE)
adam@1296 350
adam@1296 351 fun institutionalizeJob (job : job) =
adam@1483 352 (Settings.setDebug (#debug job);
adam@1483 353 Settings.setUrlPrefix (#prefix job);
adam@1296 354 Settings.setTimeout (#timeout job);
adam@1296 355 Settings.setHeaders (#headers job);
adam@1296 356 Settings.setScripts (#scripts job);
adam@1296 357 Settings.setClientToServer (#clientToServer job);
adam@1296 358 Settings.setEffectful (#effectful job);
adam@1296 359 Settings.setBenignEffectful (#benignEffectful job);
adam@1296 360 Settings.setClientOnly (#clientOnly job);
adam@1296 361 Settings.setServerOnly (#serverOnly job);
adam@1296 362 Settings.setJsFuncs (#jsFuncs job);
adam@1296 363 Settings.setRewriteRules (#rewrites job);
adam@1296 364 Settings.setUrlRules (#filterUrl job);
adam@1296 365 Settings.setMimeRules (#filterMime job);
adam@1465 366 Settings.setRequestHeaderRules (#filterRequest job);
adam@1465 367 Settings.setResponseHeaderRules (#filterResponse job);
adam@1296 368 Option.app Settings.setProtocol (#protocol job);
adam@1296 369 Option.app Settings.setDbms (#dbms job);
adam@1296 370 Settings.setSafeGets (#safeGets job);
adam@1332 371 Settings.setOnError (#onError job);
adam@1408 372 Settings.setMinHeap (#minHeap job);
adam@1408 373 Settings.setSigFile (#sigFile job))
adam@1296 374
adam@1603 375 datatype commentableLine =
adam@1603 376 EndOfFile
adam@1603 377 | OnlyComment
adam@1603 378 | Content of string
adam@1603 379
adam@1331 380 fun inputCommentableLine inf =
adam@1603 381 case TextIO.inputLine inf of
adam@1603 382 NONE => EndOfFile
adam@1603 383 | SOME s =>
adam@1603 384 let
adam@1603 385 val (befor, after) = Substring.splitl (fn ch => ch <> #"#") (Substring.full s)
adam@1603 386 in
adam@1603 387 if not (Substring.isEmpty after)
adam@1603 388 andalso Substring.foldl (fn (ch, b) => b andalso Char.isSpace ch) true befor then
adam@1603 389 OnlyComment
adam@1603 390 else
adam@1603 391 let
adam@1603 392 val s = #1 (Substring.splitr (not o Char.isSpace) befor)
adam@1603 393 in
adam@1603 394 Content (Substring.string (if Substring.size s > 0 andalso Char.isSpace (Substring.sub (s, Substring.size s - 1)) then
adam@1603 395 if Substring.size s > 1 andalso Char.isSpace (Substring.sub (s, Substring.size s - 2)) then
adam@1603 396 Substring.trimr 2 s
adam@1603 397 else
adam@1603 398 Substring.trimr 1 s
adam@1603 399 else
adam@1603 400 s))
adam@1603 401 end
adam@1603 402 end
adam@1331 403
adam@1780 404 val lastUrp = ref ""
adam@1780 405
adamc@1082 406 fun parseUrp' accLibs fname =
adam@1780 407 (if !lastUrp = fname then
adam@1780 408 ()
adam@1780 409 else
adam@1780 410 ModDb.reset ();
adam@1780 411 lastUrp := fname;
adam@1780 412 if not (Posix.FileSys.access (fname ^ ".urp", []) orelse Posix.FileSys.access (fname ^ "/lib.urp", []))
adam@1780 413 andalso Posix.FileSys.access (fname ^ ".ur", []) then
adam@1780 414 let
adam@1780 415 val job = {prefix = "/",
adam@1780 416 database = NONE,
adam@1780 417 sources = [fname],
adam@1780 418 exe = fname ^ ".exe",
adam@1780 419 sql = NONE,
adam@1780 420 debug = Settings.getDebug (),
adam@1780 421 profile = false,
adam@1780 422 timeout = 60,
adam@1780 423 ffi = [],
adam@1780 424 link = [],
adam@1780 425 linker = NONE,
adam@1780 426 headers = [],
adam@1780 427 scripts = [],
adam@1780 428 clientToServer = [],
adam@1780 429 effectful = [],
adam@1780 430 benignEffectful = [],
adam@1780 431 clientOnly = [],
adam@1780 432 serverOnly = [],
adam@1780 433 jsFuncs = [],
adam@1780 434 rewrites = [{pkind = Settings.Any,
adam@1780 435 kind = Settings.Prefix,
adam@1780 436 from = capitalize (OS.Path.file fname) ^ "/", to = "",
adam@1780 437 hyphenate = false}],
adam@1780 438 filterUrl = [],
adam@1780 439 filterMime = [],
adam@1780 440 filterRequest = [],
adam@1780 441 filterResponse = [],
adam@1780 442 protocol = NONE,
adam@1780 443 dbms = NONE,
adam@1780 444 sigFile = NONE,
adam@1780 445 safeGets = [],
adam@1780 446 onError = NONE,
adam@1780 447 minHeap = 0}
adam@1780 448 in
adam@1780 449 institutionalizeJob job;
adam@1780 450 {Job = job, Libs = []}
adam@1780 451 end
adam@1780 452 else
adam@1780 453 let
adam@1780 454 val pathmap = ref (!pathmap)
adam@1780 455 val bigLibs = ref []
adamc@767 456
adam@1780 457 fun pu filename =
adam@1780 458 let
adam@1780 459 val filename = OS.Path.mkAbsolute {path = filename, relativeTo = OS.FileSys.getDir ()}
adam@1338 460
adam@1780 461 val dir = OS.Path.dir filename
adam@1780 462 fun opener () = TextIO.openIn (OS.Path.joinBaseExt {base = filename, ext = SOME "urp"})
adamc@1151 463
adam@1780 464 val inf = opener ()
adamc@1151 465
adam@1780 466 fun hasSpaceLine () =
adam@1780 467 case inputCommentableLine inf of
adam@1780 468 Content s => s = "debug" orelse s = "profile"
adam@1780 469 orelse CharVector.exists (fn ch => ch = #" " orelse ch = #"\t") s orelse hasSpaceLine ()
adam@1780 470 | EndOfFile => false
adam@1780 471 | OnlyComment => hasSpaceLine ()
adamc@1151 472
adam@1780 473 val hasBlankLine = hasSpaceLine ()
adamc@1151 474
adam@1780 475 val inf = (TextIO.closeIn inf; opener ())
adamc@767 476
adam@1780 477 fun pathify fname =
adam@1780 478 if size fname > 0 andalso String.sub (fname, 0) = #"$" then
adam@1780 479 let
adam@1780 480 val fname' = Substring.extract (fname, 1, NONE)
adam@1780 481 val (befor, after) = Substring.splitl (fn ch => ch <> #"/") fname'
adam@1780 482 in
adam@1780 483 case M.find (!pathmap, Substring.string befor) of
adam@1780 484 NONE => fname
adam@1780 485 | SOME rep => rep ^ Substring.string after
adam@1780 486 end
adam@1780 487 else
adam@1780 488 fname
adam@1296 489
adam@1780 490 fun relify fname =
adam@1780 491 let
adam@1780 492 val fname = pathify fname
adam@1780 493 in
adam@1780 494 OS.Path.concat (dir, fname)
adam@1780 495 handle OS.Path.Path => fname
adam@1780 496 end
adamc@767 497
adam@1780 498 fun libify path =
adam@1780 499 (if Posix.FileSys.access (path ^ ".urp", []) then
adam@1780 500 path
adam@1780 501 else
adam@1780 502 path ^ "/lib")
adam@1780 503 handle SysErr => path
adam@1780 504
adam@1780 505 fun libify' path =
adam@1780 506 (if Posix.FileSys.access (relify path ^ ".urp", []) then
adam@1780 507 path
adam@1780 508 else
adam@1780 509 path ^ "/lib")
adam@1780 510 handle SysErr => path
adam@1780 511
adam@1780 512 val absDir = OS.Path.mkAbsolute {path = dir, relativeTo = OS.FileSys.getDir ()}
adam@1780 513
adam@1780 514 fun relifyA fname =
adam@1780 515 OS.Path.mkAbsolute {path = pathify fname, relativeTo = absDir}
adam@1780 516
adam@1780 517 fun readSources acc =
adam@1780 518 case inputCommentableLine inf of
adam@1780 519 Content line =>
adam@1780 520 let
adam@1780 521 val acc = if CharVector.all Char.isSpace line then
adam@1780 522 acc
adam@1780 523 else
adam@1780 524 let
adam@1780 525 val fname = String.implode (List.filter (fn x => not (Char.isSpace x))
adam@1780 526 (String.explode line))
adam@1780 527 val fname = relifyA fname
adam@1780 528 in
adam@1780 529 fname :: acc
adam@1780 530 end
adam@1780 531 in
adam@1780 532 readSources acc
adam@1780 533 end
adam@1780 534 | OnlyComment => readSources acc
adam@1780 535 | EndOfFile => rev acc
adam@1780 536
adam@1780 537 val prefix = ref (case Settings.getUrlPrefixFull () of "/" => NONE | s => SOME s)
adam@1780 538 val database = ref (Settings.getDbstring ())
adam@1780 539 val exe = ref (Settings.getExe ())
adam@1780 540 val sql = ref (Settings.getSql ())
adam@1780 541 val debug = ref (Settings.getDebug ())
adam@1780 542 val profile = ref false
adam@1780 543 val timeout = ref NONE
adam@1780 544 val ffi = ref []
adam@1780 545 val link = ref []
adam@1780 546 val linker = ref NONE
adam@1780 547 val headers = ref []
adam@1780 548 val scripts = ref []
adam@1780 549 val clientToServer = ref []
adam@1780 550 val effectful = ref []
adam@1780 551 val benignEffectful = ref []
adam@1780 552 val clientOnly = ref []
adam@1780 553 val serverOnly = ref []
adam@1780 554 val jsFuncs = ref []
adam@1780 555 val rewrites = ref []
adam@1780 556 val url = ref []
adam@1780 557 val mime = ref []
adam@1780 558 val request = ref []
adam@1780 559 val response = ref []
adam@1780 560 val libs = ref []
adam@1780 561 val protocol = ref NONE
adam@1780 562 val dbms = ref NONE
adam@1780 563 val sigFile = ref (Settings.getSigFile ())
adam@1780 564 val safeGets = ref []
adam@1780 565 val onError = ref NONE
adam@1780 566 val minHeap = ref 0
adam@1780 567
adam@1780 568 fun finish sources =
adam@1780 569 let
adam@1780 570 val job = {
adam@1780 571 prefix = Option.getOpt (!prefix, "/"),
adam@1780 572 database = !database,
adam@1780 573 exe = Option.getOpt (!exe, OS.Path.joinBaseExt {base = OS.Path.base filename,
adam@1780 574 ext = SOME "exe"}),
adam@1780 575 sql = !sql,
adam@1780 576 debug = !debug,
adam@1780 577 profile = !profile,
adam@1780 578 timeout = Option.getOpt (!timeout, 60),
adam@1780 579 ffi = rev (!ffi),
adam@1780 580 link = rev (!link),
adam@1780 581 linker = !linker,
adam@1780 582 headers = rev (!headers),
adam@1780 583 scripts = rev (!scripts),
adam@1780 584 clientToServer = rev (!clientToServer),
adam@1780 585 effectful = rev (!effectful),
adam@1780 586 benignEffectful = rev (!benignEffectful),
adam@1780 587 clientOnly = rev (!clientOnly),
adam@1780 588 serverOnly = rev (!serverOnly),
adam@1780 589 jsFuncs = rev (!jsFuncs),
adam@1780 590 rewrites = rev (!rewrites),
adam@1780 591 filterUrl = rev (!url),
adam@1780 592 filterMime = rev (!mime),
adam@1780 593 filterRequest = rev (!request),
adam@1780 594 filterResponse = rev (!response),
adam@1780 595 sources = sources,
adam@1780 596 protocol = !protocol,
adam@1780 597 dbms = !dbms,
adam@1780 598 sigFile = !sigFile,
adam@1780 599 safeGets = rev (!safeGets),
adam@1780 600 onError = !onError,
adam@1780 601 minHeap = !minHeap
adam@1780 602 }
adam@1780 603
adam@1780 604 fun mergeO f (old, new) =
adam@1780 605 case (old, new) of
adam@1780 606 (NONE, _) => new
adam@1780 607 | (_, NONE) => old
adam@1780 608 | (SOME v1, SOME v2) => SOME (f (v1, v2))
adam@1780 609
adam@1780 610 fun same desc = mergeO (fn (x : string, y) =>
adam@1780 611 (if x = y then
adam@1780 612 ()
adam@1780 613 else
adam@1780 614 ErrorMsg.error ("Multiple "
adam@1780 615 ^ desc ^ " values that don't agree");
adam@1780 616 x))
adam@1780 617
adam@1780 618 fun merge (old : job, new : job) = {
adam@1780 619 prefix = case #prefix old of
adam@1780 620 "/" => #prefix new
adam@1780 621 | pold => case #prefix new of
adam@1780 622 "/" => pold
adam@1780 623 | pnew => (if pold = pnew then
adam@1780 624 ()
adam@1780 625 else
adam@1780 626 ErrorMsg.error ("Multiple prefix values that don't agree: "
adam@1780 627 ^ pold ^ ", " ^ pnew);
adam@1780 628 pold),
adam@1780 629 database = mergeO (fn (old, _) => old) (#database old, #database new),
adam@1780 630 exe = #exe old,
adam@1780 631 sql = #sql old,
adam@1780 632 debug = #debug old orelse #debug new,
adam@1780 633 profile = #profile old orelse #profile new,
adam@1780 634 timeout = #timeout old,
adam@1780 635 ffi = #ffi old @ #ffi new,
adam@1780 636 link = #link old @ #link new,
adam@1780 637 linker = mergeO (fn (_, new) => new) (#linker old, #linker new),
adam@1780 638 headers = #headers old @ #headers new,
adam@1780 639 scripts = #scripts old @ #scripts new,
adam@1780 640 clientToServer = #clientToServer old @ #clientToServer new,
adam@1780 641 effectful = #effectful old @ #effectful new,
adam@1780 642 benignEffectful = #benignEffectful old @ #benignEffectful new,
adam@1780 643 clientOnly = #clientOnly old @ #clientOnly new,
adam@1780 644 serverOnly = #serverOnly old @ #serverOnly new,
adam@1780 645 jsFuncs = #jsFuncs old @ #jsFuncs new,
adam@1780 646 rewrites = #rewrites old @ #rewrites new,
adam@1780 647 filterUrl = #filterUrl old @ #filterUrl new,
adam@1780 648 filterMime = #filterMime old @ #filterMime new,
adam@1780 649 filterRequest = #filterRequest old @ #filterRequest new,
adam@1780 650 filterResponse = #filterResponse old @ #filterResponse new,
adam@1780 651 sources = #sources new
adam@1780 652 @ List.filter (fn s => List.all (fn s' => s' <> s) (#sources new))
adam@1780 653 (#sources old),
adam@1780 654 protocol = mergeO #2 (#protocol old, #protocol new),
adam@1780 655 dbms = mergeO #2 (#dbms old, #dbms new),
adam@1780 656 sigFile = mergeO #2 (#sigFile old, #sigFile new),
adam@1780 657 safeGets = #safeGets old @ #safeGets new,
adam@1780 658 onError = mergeO #2 (#onError old, #onError new),
adam@1780 659 minHeap = Int.max (#minHeap old, #minHeap new)
adam@1780 660 }
adam@1780 661 in
adam@1780 662 if accLibs then
adam@1780 663 foldr (fn (job', job) => merge (job, job')) job (!libs)
adam@1780 664 else
adam@1780 665 job
adam@1780 666 end
adam@1780 667
adam@1780 668 fun parsePkind s =
adam@1780 669 case s of
adam@1780 670 "all" => Settings.Any
adam@1780 671 | "url" => Settings.Url
adam@1780 672 | "table" => Settings.Table
adam@1780 673 | "sequence" => Settings.Sequence
adam@1780 674 | "view" => Settings.View
adam@1780 675 | "relation" => Settings.Relation
adam@1780 676 | "cookie" => Settings.Cookie
adam@1780 677 | "style" => Settings.Style
adam@1780 678 | _ => (ErrorMsg.error "Bad path kind spec";
adam@1780 679 Settings.Any)
adam@1780 680
adam@1780 681 fun parseFrom s =
adam@1780 682 if size s > 1 andalso String.sub (s, size s - 2) = #"/" andalso String.sub (s, size s - 1) = #"*" then
adam@1780 683 (Settings.Prefix, String.substring (s, 0, size s - 1))
adam@1296 684 else
adam@1780 685 (Settings.Exact, s)
adamc@767 686
adam@1780 687 fun parseFkind s =
adam@1780 688 case s of
adam@1780 689 "url" => url
adam@1780 690 | "mime" => mime
adam@1780 691 | "requestHeader" => request
adam@1780 692 | "responseHeader" => response
adam@1780 693 | _ => (ErrorMsg.error "Bad filter kind";
adam@1780 694 url)
adam@1780 695
adam@1780 696 fun parsePattern s =
adam@1780 697 if size s > 0 andalso String.sub (s, size s - 1) = #"*" then
adam@1780 698 (Settings.Prefix, String.substring (s, 0, size s - 1))
adam@1296 699 else
adam@1780 700 (Settings.Exact, s)
adamc@767 701
adam@1780 702 fun read () =
adam@1780 703 case inputCommentableLine inf of
adam@1780 704 EndOfFile => finish []
adam@1780 705 | OnlyComment => read ()
adam@1780 706 | Content "" => finish (readSources [])
adam@1780 707 | Content line =>
adam@1780 708 let
adam@1780 709 val (cmd, arg) = Substring.splitl (fn x => not (Char.isSpace x)) (Substring.full line)
adam@1780 710 val cmd = Substring.string (trim cmd)
adam@1780 711 val arg = Substring.string (trim arg)
adamc@794 712
adam@1780 713 fun ffiS () =
adam@1780 714 case String.fields (fn ch => ch = #".") arg of
adam@1780 715 [m, x] => (m, x)
adam@1780 716 | _ => (ErrorMsg.error (cmd ^ " argument not of the form Module.func");
adam@1780 717 ("", ""))
adamc@794 718
adam@1780 719 fun ffiM () =
adam@1780 720 case String.fields (fn ch => ch = #"=") arg of
adam@1780 721 [f, s] =>
adam@1780 722 let
adam@1780 723 val f = trimS f
adam@1780 724 val s = trimS s
adam@1780 725 in
adam@1780 726 case String.fields (fn ch => ch = #".") f of
adam@1780 727 [m, x] => ((m, x), s)
adam@1780 728 | _ => (ErrorMsg.error (cmd ^ " argument not of the form Module.func=func'");
adam@1780 729 (("", ""), ""))
adam@1780 730 end
adam@1780 731 | _ => (ErrorMsg.error (cmd ^ " argument not of the form Module.func=func'");
adam@1780 732 (("", ""), ""))
adam@1780 733 in
adam@1780 734 case cmd of
adam@1780 735 "prefix" => prefix := SOME arg
adam@1780 736 | "database" =>
adam@1780 737 (case !database of
adam@1780 738 NONE => database := SOME arg
adam@1780 739 | SOME _ => ())
adam@1780 740 | "dbms" =>
adam@1780 741 (case !dbms of
adam@1780 742 NONE => dbms := SOME arg
adam@1780 743 | SOME _ => ())
adam@1780 744 | "sigfile" =>
adam@1780 745 (case !sigFile of
adam@1780 746 NONE => sigFile := SOME arg
adam@1780 747 | SOME _ => ())
adam@1780 748 | "exe" =>
adam@1780 749 (case !exe of
adam@1780 750 NONE => exe := SOME (relify arg)
adam@1780 751 | SOME _ => ())
adam@1780 752 | "sql" =>
adam@1780 753 (case !sql of
adam@1780 754 NONE => sql := SOME (relify arg)
adam@1780 755 | SOME _ => ())
adam@1780 756 | "debug" => debug := true
adam@1780 757 | "profile" => profile := true
adam@1780 758 | "timeout" =>
adam@1780 759 (case !timeout of
adam@1780 760 NONE => ()
adam@1780 761 | SOME _ => ErrorMsg.error "Duplicate 'timeout' directive";
adam@1780 762 timeout := SOME (valOf (Int.fromString arg)))
adam@1780 763 | "ffi" => ffi := relify arg :: !ffi
adam@1780 764 | "link" => let
adam@1780 765 val arg = if size arg >= 1
adam@1780 766 andalso String.sub (arg, 0) = #"-" then
adam@1780 767 arg
adam@1780 768 else
adam@1780 769 relifyA arg
adam@1780 770 in
adam@1780 771 link := arg :: !link
adam@1780 772 end
adam@1780 773 | "linker" => linker := SOME arg
adam@1780 774 | "include" => headers := relifyA arg :: !headers
adam@1780 775 | "script" => scripts := arg :: !scripts
adam@1780 776 | "clientToServer" => clientToServer := ffiS () :: !clientToServer
adam@1780 777 | "safeGet" => safeGets := arg :: !safeGets
adam@1780 778 | "effectful" => effectful := ffiS () :: !effectful
adam@1780 779 | "benignEffectful" => benignEffectful := ffiS () :: !benignEffectful
adam@1780 780 | "clientOnly" => clientOnly := ffiS () :: !clientOnly
adam@1780 781 | "serverOnly" => serverOnly := ffiS () :: !serverOnly
adam@1780 782 | "jsFunc" => jsFuncs := ffiM () :: !jsFuncs
adam@1780 783 | "rewrite" =>
adam@1780 784 let
adam@1780 785 fun doit (pkind, from, to, hyph) =
adam@1780 786 let
adam@1780 787 val pkind = parsePkind pkind
adam@1780 788 val (kind, from) = parseFrom from
adam@1780 789 in
adam@1780 790 rewrites := {pkind = pkind, kind = kind, from = from, to = to, hyphenate = hyph} :: !rewrites
adam@1780 791 end
adam@1780 792 in
adam@1780 793 case String.tokens Char.isSpace arg of
adam@1780 794 [pkind, from, to, "[-]"] => doit (pkind, from, to, true)
adam@1780 795 | [pkind, from, "[-]"] => doit (pkind, from, "", true)
adam@1780 796 | [pkind, from, to] => doit (pkind, from, to, false)
adam@1780 797 | [pkind, from] => doit (pkind, from, "", false)
adam@1780 798 | _ => ErrorMsg.error "Bad 'rewrite' syntax"
adam@1780 799 end
adam@1780 800 | "allow" =>
adam@1780 801 (case String.tokens Char.isSpace arg of
adam@1780 802 [fkind, pattern] =>
adam@1780 803 let
adam@1780 804 val fkind = parseFkind fkind
adam@1780 805 val (kind, pattern) = parsePattern pattern
adam@1780 806 in
adam@1780 807 fkind := {action = Settings.Allow, kind = kind, pattern = pattern} :: !fkind
adam@1780 808 end
adam@1780 809 | _ => ErrorMsg.error "Bad 'allow' syntax")
adam@1780 810 | "deny" =>
adam@1780 811 (case String.tokens Char.isSpace arg of
adam@1780 812 [fkind, pattern] =>
adam@1780 813 let
adam@1780 814 val fkind = parseFkind fkind
adam@1780 815 val (kind, pattern) = parsePattern pattern
adam@1780 816 in
adam@1780 817 fkind := {action = Settings.Deny, kind = kind, pattern = pattern} :: !fkind
adam@1780 818 end
adam@1780 819 | _ => ErrorMsg.error "Bad 'deny' syntax")
adam@1780 820 | "library" => if accLibs then
adam@1780 821 libs := pu (libify (relify arg)) :: !libs
adam@1780 822 else
adam@1780 823 bigLibs := libify' arg :: !bigLibs
adam@1780 824 | "path" =>
adam@1780 825 (case String.fields (fn ch => ch = #"=") arg of
adam@1780 826 [n, v] => ((pathmap := M.insert (!pathmap, n, OS.Path.mkAbsolute {path = v, relativeTo = dir}))
adam@1780 827 handle OS.Path.Path => ErrorMsg.error "Invalid 'path' directory argument")
adam@1780 828 | _ => ErrorMsg.error "path argument not of the form name=value'")
adam@1780 829 | "onError" =>
adam@1780 830 (case String.fields (fn ch => ch = #".") arg of
adam@1780 831 m1 :: (fs as _ :: _) =>
adam@1780 832 onError := SOME (m1, List.take (fs, length fs - 1), List.last fs)
adam@1780 833 | _ => ErrorMsg.error "invalid 'onError' argument")
adam@1780 834 | "limit" =>
adam@1780 835 (case String.fields Char.isSpace arg of
adam@1780 836 [class, num] =>
adam@1780 837 (case Int.fromString num of
adam@1780 838 NONE => ErrorMsg.error ("invalid limit number '" ^ num ^ "'")
adam@1780 839 | SOME n =>
adam@1780 840 if n < 0 then
adam@1780 841 ErrorMsg.error ("invalid limit number '" ^ num ^ "'")
adam@1780 842 else
adam@1780 843 Settings.addLimit (class, n))
adam@1780 844 | _ => ErrorMsg.error "invalid 'limit' arguments")
adam@1780 845 | "minHeap" =>
adam@1780 846 (case Int.fromString arg of
adam@1780 847 NONE => ErrorMsg.error ("invalid min heap '" ^ arg ^ "'")
adam@1780 848 | SOME n => minHeap := n)
adam@1780 849 | "alwaysInline" => Settings.addAlwaysInline arg
adam@1780 850 | "noXsrfProtection" => Settings.addNoXsrfProtection arg
adam@1780 851 | "timeFormat" => Settings.setTimeFormat arg
adamc@794 852
adam@1780 853 | _ => ErrorMsg.error ("Unrecognized command '" ^ cmd ^ "'");
adam@1780 854 read ()
adam@1780 855 end
adamc@794 856
adam@1780 857 val job = if hasBlankLine then
adam@1780 858 read ()
adam@1780 859 else
adam@1780 860 finish (readSources [])
adam@1780 861 in
adam@1780 862 TextIO.closeIn inf;
adam@1780 863 institutionalizeJob job;
adam@1780 864 job
adam@1780 865 end
adam@1780 866 in
adam@1780 867 {Job = pu fname, Libs = !bigLibs}
adam@1780 868 end)
adamc@767 869
adamc@1082 870 fun p_job' {Job = j, Libs = _ : string list} = p_job j
adamc@1082 871
adamc@270 872 val parseUrp = {
adamc@1083 873 func = #Job o parseUrp' true,
adamc@270 874 print = p_job
adamc@270 875 }
adamc@270 876
adamc@1082 877 val parseUrp' = {
adamc@1083 878 func = parseUrp' false,
adamc@1082 879 print = p_job'
adamc@1082 880 }
adamc@1082 881
adamc@270 882 val toParseJob = transform parseUrp "parseJob"
adamc@1082 883 val toParseJob' = transform parseUrp' "parseJob'"
adamc@1082 884
adamc@1082 885 fun op o (tr2 : ('b, 'c) transform, tr1 : ('a, 'b) transform) = {
adamc@1082 886 func = fn input => case #func tr1 input of
adamc@1082 887 NONE => NONE
adamc@1082 888 | SOME v => #func tr2 v,
adamc@1082 889 print = #print tr2,
adamc@1082 890 time = fn (input, pmap) => let
adamc@1082 891 val (ro, pmap) = #time tr1 (input, pmap)
adamc@1082 892 in
adamc@1082 893 case ro of
adamc@1082 894 NONE => (NONE, pmap)
adamc@1082 895 | SOME v => #time tr2 (v, pmap)
adamc@1082 896 end
adamc@1082 897 }
adamc@270 898
adamc@1090 899 structure SM = BinaryMapFn(struct
adamc@1090 900 type ord_key = string
adamc@1090 901 val compare = String.compare
adamc@1090 902 end)
adamc@1090 903
adamc@1090 904 val moduleRoots = ref ([] : (string * string) list)
adamc@1090 905 fun addModuleRoot (k, v) = moduleRoots := (k, v) :: !moduleRoots
adamc@1090 906
adam@1743 907 structure SK = struct
adam@1743 908 type ord_key = string
adam@1743 909 val compare = String.compare
adam@1743 910 end
adam@1743 911
adam@1743 912 structure SS = BinarySetFn(SK)
adam@1743 913 structure SM = BinaryMapFn(SK)
adamc@1090 914
adam@1773 915 exception MissingFile of string
adam@1773 916
adamc@201 917 val parse = {
adam@1294 918 func = fn {database, sources = fnames, ffi, onError, ...} : job =>
adamc@201 919 let
adamc@1090 920 val mrs = !moduleRoots
adamc@1090 921
adamc@834 922 val anyErrors = ref false
adamc@834 923 fun checkErrors () = anyErrors := (!anyErrors orelse ErrorMsg.anyErrors ())
adam@1343 924 fun nameOf fname =
adam@1343 925 let
adam@1343 926 val fname = OS.Path.file fname
adam@1343 927 val fst = if size fname = 0 then #"!" else String.sub (fname, 0)
adam@1343 928 in
adam@1343 929 if not (Char.isAlpha fst) then
adam@1343 930 ErrorMsg.error ("Filename doesn't start with letter: " ^ fname)
adam@1343 931 else if CharVector.exists (fn ch => not (Char.isAlphaNum ch) andalso ch <> #"_") fname then
adam@1343 932 ErrorMsg.error ("Filename contains a character that isn't alphanumeric or underscore: " ^ fname)
adam@1343 933 else
adam@1343 934 ();
adam@1343 935 capitalize fname
adam@1343 936 end
adamc@109 937
adamc@764 938 fun parseFfi fname =
adamc@764 939 let
adamc@764 940 val mname = nameOf fname
adamc@764 941 val urs = OS.Path.joinBaseExt {base = fname, ext = SOME "urs"}
adamc@764 942
adamc@764 943 val loc = {file = urs,
adamc@764 944 first = ErrorMsg.dummyPos,
adamc@764 945 last = ErrorMsg.dummyPos}
adamc@764 946
adamc@764 947 val sgn = (Source.SgnConst (#func parseUrs urs), loc)
adamc@764 948 in
adamc@834 949 checkErrors ();
adam@1733 950 (Source.DFfiStr (mname, sgn, if !Elaborate.incremental then SOME (OS.FileSys.modTime urs) else NONE), loc)
adamc@764 951 end
adamc@764 952
adamc@1090 953 val defed = ref SS.empty
adamc@1092 954 val fulls = ref SS.empty
adamc@1090 955
adamc@201 956 fun parseOne fname =
adamc@201 957 let
adamc@201 958 val mname = nameOf fname
adamc@244 959 val ur = OS.Path.joinBaseExt {base = fname, ext = SOME "ur"}
adamc@244 960 val urs = OS.Path.joinBaseExt {base = fname, ext = SOME "urs"}
adamc@56 961
adam@1773 962 val () = if Posix.FileSys.access (ur, []) then
adam@1773 963 ()
adam@1773 964 else
adam@1773 965 raise MissingFile ur
adam@1773 966
adamc@201 967 val sgnO =
adamc@244 968 if Posix.FileSys.access (urs, []) then
adamc@244 969 SOME (Source.SgnConst (#func parseUrs urs),
adamc@244 970 {file = urs,
adamc@201 971 first = ErrorMsg.dummyPos,
adamc@201 972 last = ErrorMsg.dummyPos})
adamc@834 973 before checkErrors ()
adamc@201 974 else
adamc@201 975 NONE
adamc@56 976
adamc@244 977 val loc = {file = ur,
adamc@201 978 first = ErrorMsg.dummyPos,
adamc@201 979 last = ErrorMsg.dummyPos}
adamc@56 980
adam@1738 981 val urt = OS.FileSys.modTime ur
adam@1738 982 val urst = (OS.FileSys.modTime urs) handle _ => urt
adam@1738 983
adamc@244 984 val ds = #func parseUr ur
adam@1738 985 val d = (Source.DStr (mname, sgnO, if !Elaborate.incremental then SOME (if Time.> (urt, urst) then urt else urst) else NONE,
adam@1733 986 (Source.StrConst ds, loc)), loc)
adamc@1090 987
adamc@1126 988 val fname = OS.Path.mkCanonical fname
adamc@1090 989 val d = case List.find (fn (root, name) =>
adamc@1090 990 String.isPrefix (root ^ "/") fname) mrs of
adamc@1090 991 NONE => d
adamc@1090 992 | SOME (root, name) =>
adamc@1090 993 let
adamc@1090 994 val fname = String.extract (fname, size root + 1, NONE)
adamc@1090 995 val pieces = name :: String.tokens (fn ch => ch = #"/") fname
adamc@1090 996 val pieces = List.filter (fn s => size s > 0
adamc@1090 997 andalso Char.isAlpha (String.sub (s, 0)))
adamc@1090 998 pieces
adamc@1090 999 val pieces = map capitalize pieces
adamc@1092 1000 val full = String.concatWith "." pieces
adamc@1090 1001
adamc@1090 1002 fun makeD prefix pieces =
adamc@1090 1003 case pieces of
adamc@1090 1004 [] => (ErrorMsg.error "Empty module path";
adamc@1090 1005 (Source.DStyle "Boo", loc))
adamc@1090 1006 | [_] => d
adamc@1090 1007 | piece :: pieces =>
adamc@1090 1008 let
adamc@1146 1009 val this = case prefix of
adamc@1146 1010 "" => piece
adamc@1146 1011 | _ => prefix ^ "." ^ piece
adamc@1090 1012 val old = SS.member (!defed, this)
adamc@1146 1013
adamc@1146 1014 fun notThere (ch, s) =
adamc@1146 1015 Substring.isEmpty (#2 (Substring.splitl
adamc@1146 1016 (fn ch' => ch' <> ch) s))
adamc@1146 1017
adamc@1146 1018 fun simOpen () =
adamc@1146 1019 SS.foldl (fn (full, ds) =>
adamc@1146 1020 if String.isPrefix (this ^ ".") full
adamc@1146 1021 andalso notThere (#".",
adamc@1146 1022 Substring.extract (full,
adamc@1146 1023 size
adamc@1146 1024 this + 1,
adamc@1146 1025 NONE)) then
adamc@1146 1026 let
adamc@1146 1027 val parts = String.tokens
adamc@1146 1028 (fn ch => ch = #".") full
adamc@1146 1029
adamc@1146 1030 val part = List.last parts
adamc@1146 1031
adamc@1146 1032 val imp = if length parts >= 2 then
adamc@1146 1033 (Source.StrProj
adamc@1146 1034 ((Source.StrVar
adamc@1146 1035 (List.nth (parts,
adamc@1146 1036 length
adamc@1146 1037 parts
adamc@1146 1038 - 2)),
adamc@1146 1039 loc),
adamc@1146 1040 part), loc)
adamc@1146 1041 else
adamc@1146 1042 (Source.StrVar part, loc)
adamc@1146 1043 in
adam@1732 1044 (Source.DStr (part, NONE, NONE, imp),
adamc@1146 1045 loc) :: ds
adamc@1146 1046 end
adamc@1146 1047 else
adamc@1146 1048 ds) [] (!fulls)
adamc@1090 1049 in
adamc@1090 1050 defed := SS.add (!defed, this);
adam@1732 1051 (Source.DStr (piece, NONE, NONE,
adamc@1090 1052 (Source.StrConst (if old then
adamc@1146 1053 simOpen ()
adamc@1146 1054 @ [makeD this pieces]
adamc@1090 1055 else
adamc@1146 1056 [makeD this pieces]), loc)),
adamc@1090 1057 loc)
adamc@1090 1058 end
adamc@1090 1059 in
adamc@1092 1060 if SS.member (!fulls, full) then
adamc@1092 1061 ErrorMsg.error ("Rooted module " ^ full ^ " has multiple versions.")
adamc@1092 1062 else
adamc@1092 1063 ();
adamc@1146 1064
adamc@1090 1065 makeD "" pieces
adamc@1146 1066 before ignore (foldl (fn (new, path) =>
adamc@1146 1067 let
adamc@1146 1068 val new' = case path of
adamc@1146 1069 "" => new
adamc@1146 1070 | _ => path ^ "." ^ new
adamc@1146 1071 in
adamc@1146 1072 fulls := SS.add (!fulls, new');
adamc@1146 1073 new'
adamc@1146 1074 end) "" pieces)
adamc@1090 1075 end
adamc@201 1076 in
adamc@834 1077 checkErrors ();
adamc@1090 1078 d
adam@1773 1079 end handle MissingFile fname => (ErrorMsg.error ("Missing source file: " ^ fname);
adam@1773 1080 (Source.DSequence "", ErrorMsg.dummySpan))
adamc@56 1081
adamc@764 1082 val dsFfi = map parseFfi ffi
adamc@201 1083 val ds = map parseOne fnames
adamc@1090 1084 val loc = ErrorMsg.dummySpan
adamc@201 1085 in
adamc@834 1086 if !anyErrors then
adamc@834 1087 ErrorMsg.error "Parse failure"
adamc@834 1088 else
adamc@834 1089 ();
adamc@834 1090
adamc@201 1091 let
adamc@1126 1092 val final = List.last fnames
adamc@1126 1093 val final = case List.find (fn (root, name) =>
adamc@1126 1094 String.isPrefix (root ^ "/") final) mrs of
adamc@1126 1095 NONE => (Source.StrVar (nameOf final), loc)
adamc@1126 1096 | SOME (root, name) =>
adamc@1126 1097 let
adamc@1126 1098 val m = (Source.StrVar name, loc)
adamc@1126 1099 val final = String.extract (final, size root + 1, NONE)
adamc@1264 1100 val fields = String.fields (fn ch => ch = #"/") final
adamc@1264 1101 val fields = List.filter (fn s => size s = 0
adamc@1264 1102 orelse not (Char.isDigit (String.sub (s, 0))))
adamc@1264 1103 fields
adamc@1126 1104 in
adamc@1126 1105 foldl (fn (x, m) => (Source.StrProj (m, capitalize x), loc))
adamc@1264 1106 m fields
adamc@1126 1107 end
adamc@271 1108
adamc@764 1109 val ds = dsFfi @ ds
adamc@1126 1110 @ [(Source.DExport final, loc)]
adamc@1090 1111
adamc@1090 1112 val ds = case database of
adamc@1090 1113 NONE => ds
adamc@1090 1114 | SOME s => (Source.DDatabase s, loc) :: ds
adam@1294 1115
adam@1294 1116 val ds = case onError of
adam@1294 1117 NONE => ds
adam@1294 1118 | SOME v => ds @ [(Source.DOnError v, loc)]
adamc@201 1119 in
adam@1738 1120 ignore (List.foldl (fn (d, used) =>
adam@1738 1121 case #1 d of
adam@1738 1122 Source.DStr (x, _, _, _) =>
adam@1743 1123 (case SM.find (used, x) of
adam@1743 1124 SOME loc =>
adam@1743 1125 (ErrorMsg.error ("Duplicate top-level module name " ^ x);
adam@1743 1126 Print.prefaces "Files" [("Previous", Print.PD.string (ErrorMsg.spanToString loc)),
adam@1743 1127 ("Current", Print.PD.string (ErrorMsg.spanToString (#2 d)))];
adam@1743 1128 used)
adam@1743 1129 | NONE =>
adam@1743 1130 SM.insert (used, x, #2 d))
adam@1743 1131 | _ => used) SM.empty ds);
adamc@1090 1132 ds
adamc@201 1133 end handle Empty => ds
adamc@201 1134 end,
adamc@201 1135 print = SourcePrint.p_file
adamc@201 1136 }
adamc@56 1137
adamc@270 1138 val toParse = transform parse "parse" o toParseJob
adamc@38 1139
adamc@201 1140 val elaborate = {
adamc@201 1141 func = fn file => let
ezyang@1739 1142 val basisF = Settings.libFile "basis.urs"
ezyang@1739 1143 val topF = Settings.libFile "top.urs"
ezyang@1739 1144 val topF' = Settings.libFile "top.ur"
adam@1732 1145
adam@1732 1146 val basis = #func parseUrs basisF
adam@1732 1147 val topSgn = #func parseUrs topF
adam@1732 1148 val topStr = #func parseUr topF'
adam@1732 1149
adam@1732 1150 val tm1 = OS.FileSys.modTime topF
adam@1732 1151 val tm2 = OS.FileSys.modTime topF'
adamc@201 1152 in
adam@1732 1153 Elaborate.elabFile basis (OS.FileSys.modTime basisF)
adam@1732 1154 topStr topSgn (if Time.< (tm1, tm2) then tm2 else tm1)
adam@1732 1155 ElabEnv.empty file
adamc@201 1156 end,
adamc@201 1157 print = ElabPrint.p_file ElabEnv.empty
adamc@201 1158 }
adamc@5 1159
adamc@270 1160 val toElaborate = transform elaborate "elaborate" o toParse
adamc@201 1161
adamc@448 1162 val unnest = {
adamc@448 1163 func = Unnest.unnest,
adamc@448 1164 print = ElabPrint.p_file ElabEnv.empty
adamc@448 1165 }
adamc@448 1166
adamc@448 1167 val toUnnest = transform unnest "unnest" o toElaborate
adamc@448 1168
adamc@313 1169 val termination = {
adamc@313 1170 func = (fn file => (Termination.check file; file)),
adamc@313 1171 print = ElabPrint.p_file ElabEnv.empty
adamc@313 1172 }
adamc@313 1173
adamc@448 1174 val toTermination = transform termination "termination" o toUnnest
adamc@313 1175
adamc@201 1176 val explify = {
adamc@201 1177 func = Explify.explify,
adamc@201 1178 print = ExplPrint.p_file ExplEnv.empty
adamc@201 1179 }
adamc@201 1180
adamc@625 1181 val toExplify = transform explify "explify" o toUnnest
adamc@201 1182
adamc@201 1183 val corify = {
adamc@201 1184 func = Corify.corify,
adamc@201 1185 print = CorePrint.p_file CoreEnv.empty
adamc@201 1186 }
adamc@201 1187
adamc@270 1188 val toCorify = transform corify "corify" o toExplify
adamc@201 1189
adamc@482 1190 (*val reduce_local = {
adamc@482 1191 func = ReduceLocal.reduce,
adamc@482 1192 print = CorePrint.p_file CoreEnv.empty
adamc@482 1193 }
adamc@482 1194
adamc@482 1195 val toReduce_local = transform reduce_local "reduce_local" o toCorify*)
adamc@482 1196
adamc@443 1197 val especialize = {
adamc@443 1198 func = ESpecialize.specialize,
adamc@443 1199 print = CorePrint.p_file CoreEnv.empty
adamc@443 1200 }
adamc@443 1201
adamc@454 1202 val core_untangle = {
adamc@454 1203 func = CoreUntangle.untangle,
adamc@454 1204 print = CorePrint.p_file CoreEnv.empty
adamc@454 1205 }
adamc@454 1206
adamc@794 1207 val toCore_untangle = transform core_untangle "core_untangle" o toCorify
adamc@454 1208
adamc@202 1209 val shake = {
adamc@202 1210 func = Shake.shake,
adamc@202 1211 print = CorePrint.p_file CoreEnv.empty
adamc@202 1212 }
adamc@39 1213
adamc@454 1214 val toShake1 = transform shake "shake1" o toCore_untangle
adamc@110 1215
adam@1362 1216 val toEspecialize1' = transform especialize "especialize1'" o toShake1
adam@1362 1217 val toShake1' = transform shake "shake1'" o toEspecialize1'
adam@1362 1218
adamc@607 1219 val rpcify = {
adamc@607 1220 func = Rpcify.frob,
adamc@607 1221 print = CorePrint.p_file CoreEnv.empty
adamc@607 1222 }
adamc@607 1223
adam@1362 1224 val toRpcify = transform rpcify "rpcify" o toShake1'
adamc@607 1225
adamc@642 1226 val toCore_untangle2 = transform core_untangle "core_untangle2" o toRpcify
adamc@642 1227 val toShake2 = transform shake "shake2" o toCore_untangle2
adamc@1181 1228
adamc@1186 1229 val toEspecialize1 = transform especialize "especialize1" o toShake2
adamc@1181 1230
adamc@1062 1231 val toCore_untangle3 = transform core_untangle "core_untangle3" o toEspecialize1
adamc@1062 1232 val toShake3 = transform shake "shake3" o toCore_untangle3
adamc@642 1233
adamc@202 1234 val tag = {
adamc@202 1235 func = Tag.tag,
adamc@202 1236 print = CorePrint.p_file CoreEnv.empty
adamc@202 1237 }
adamc@193 1238
adamc@1062 1239 val toTag = transform tag "tag" o toShake3
adamc@20 1240
adamc@202 1241 val reduce = {
adamc@202 1242 func = Reduce.reduce,
adamc@202 1243 print = CorePrint.p_file CoreEnv.empty
adamc@202 1244 }
adamc@25 1245
adamc@692 1246 val toReduce = transform reduce "reduce" o toTag
adamc@23 1247
adamc@1186 1248 val toShakey = transform shake "shakey" o toReduce
adamc@1186 1249
adamc@1186 1250 val unpoly = {
adamc@1186 1251 func = Unpoly.unpoly,
adamc@1186 1252 print = CorePrint.p_file CoreEnv.empty
adamc@1186 1253 }
adamc@1186 1254
adamc@1186 1255 val toUnpoly = transform unpoly "unpoly" o toShakey
adamc@315 1256
adamc@202 1257 val specialize = {
adamc@202 1258 func = Specialize.specialize,
adamc@202 1259 print = CorePrint.p_file CoreEnv.empty
adamc@202 1260 }
adamc@132 1261
adamc@1186 1262 val toSpecialize = transform specialize "specialize" o toUnpoly
adamc@131 1263
adamc@1062 1264 val toShake4 = transform shake "shake4" o toSpecialize
adamc@133 1265
adamc@1062 1266 val toEspecialize2 = transform especialize "especialize2" o toShake4
adamc@1272 1267 val toShake4' = transform shake "shake4'" o toEspecialize2
adamc@1272 1268 val toUnpoly2 = transform unpoly "unpoly2" o toShake4'
adamc@1276 1269 val toSpecialize2 = transform specialize "specialize2" o toUnpoly2
adamc@1276 1270 val toShake4'' = transform shake "shake4'" o toSpecialize2
adamc@1272 1271 val toEspecialize3 = transform especialize "especialize3" o toShake4''
adamc@794 1272
adamc@1272 1273 val toReduce2 = transform reduce "reduce2" o toEspecialize3
adamc@898 1274
adamc@1062 1275 val toShake5 = transform shake "shake5" o toReduce2
adamc@794 1276
adamc@692 1277 val marshalcheck = {
adamc@692 1278 func = (fn file => (MarshalCheck.check file; file)),
adamc@692 1279 print = CorePrint.p_file CoreEnv.empty
adamc@692 1280 }
adamc@692 1281
adamc@1062 1282 val toMarshalcheck = transform marshalcheck "marshalcheck" o toShake5
adamc@692 1283
adamc@732 1284 val effectize = {
adamc@732 1285 func = Effective.effectize,
adamc@732 1286 print = CorePrint.p_file CoreEnv.empty
adamc@732 1287 }
adamc@732 1288
adamc@732 1289 val toEffectize = transform effectize "effectize" o toMarshalcheck
adamc@732 1290
adamc@1170 1291 val css = {
adamc@1170 1292 func = Css.summarize,
adamc@1170 1293 print = fn _ => Print.box []
adamc@1170 1294 }
adamc@1170 1295
adamc@1170 1296 val toCss = transform css "css" o toShake5
adamc@1170 1297
adamc@202 1298 val monoize = {
adamc@202 1299 func = Monoize.monoize CoreEnv.empty,
adamc@202 1300 print = MonoPrint.p_file MonoEnv.empty
adamc@202 1301 }
adamc@134 1302
adamc@732 1303 val toMonoize = transform monoize "monoize" o toEffectize
adamc@96 1304
adamc@202 1305 val mono_opt = {
adamc@910 1306 func = MonoOpt.optimize,
adamc@202 1307 print = MonoPrint.p_file MonoEnv.empty
adamc@202 1308 }
adamc@29 1309
adamc@270 1310 val toMono_opt1 = transform mono_opt "mono_opt1" o toMonoize
adamc@5 1311
adamc@202 1312 val untangle = {
adamc@202 1313 func = Untangle.untangle,
adamc@202 1314 print = MonoPrint.p_file MonoEnv.empty
adamc@202 1315 }
adamc@1 1316
adamc@270 1317 val toUntangle = transform untangle "untangle" o toMono_opt1
adamc@38 1318
adamc@202 1319 val mono_reduce = {
adamc@202 1320 func = MonoReduce.reduce,
adamc@202 1321 print = MonoPrint.p_file MonoEnv.empty
adamc@202 1322 }
adamc@16 1323
adamc@270 1324 val toMono_reduce = transform mono_reduce "mono_reduce" o toUntangle
adamc@39 1325
adamc@202 1326 val mono_shake = {
adamc@202 1327 func = MonoShake.shake,
adamc@202 1328 print = MonoPrint.p_file MonoEnv.empty
adamc@202 1329 }
adamc@110 1330
adamc@270 1331 val toMono_shake = transform mono_shake "mono_shake1" o toMono_reduce
adamc@193 1332
adamc@572 1333 val toMono_opt2 = transform mono_opt "mono_opt2" o toMono_shake
adamc@572 1334
adamc@1200 1335 val iflow = {
adamc@1235 1336 func = (fn file => (if !doIflow then Iflow.check file else (); file)),
adamc@1200 1337 print = MonoPrint.p_file MonoEnv.empty
adamc@1200 1338 }
adamc@1200 1339
adamc@1200 1340 val toIflow = transform iflow "iflow" o toMono_opt2
adamc@1200 1341
adamc@567 1342 val jscomp = {
adamc@567 1343 func = JsComp.process,
adamc@567 1344 print = MonoPrint.p_file MonoEnv.empty
adamc@567 1345 }
adamc@567 1346
adamc@1200 1347 val toJscomp = transform jscomp "jscomp" o toIflow
adamc@567 1348
adamc@910 1349 val toMono_opt3 = transform mono_opt "mono_opt3" o toJscomp
adamc@20 1350
adamc@506 1351 val fuse = {
adamc@506 1352 func = Fuse.fuse,
adamc@506 1353 print = MonoPrint.p_file MonoEnv.empty
adamc@506 1354 }
adamc@506 1355
adamc@572 1356 val toFuse = transform fuse "fuse" o toMono_opt3
adamc@506 1357
adamc@506 1358 val toUntangle2 = transform untangle "untangle2" o toFuse
adamc@506 1359
adamc@601 1360 val toMono_reduce2 = transform mono_reduce "mono_reduce2" o toUntangle2
adamc@601 1361 val toMono_shake2 = transform mono_shake "mono_shake2" o toMono_reduce2
adamc@916 1362 val toMono_opt4 = transform mono_opt "mono_opt4" o toMono_shake2
adamc@1017 1363 val toMono_reduce3 = transform mono_reduce "mono_reduce3" o toMono_opt4
adamc@1185 1364 val toFuse2 = transform fuse "fuse2" o toMono_reduce3
adamc@916 1365 val toUntangle3 = transform untangle "untangle3" o toFuse2
adamc@916 1366 val toMono_shake3 = transform mono_shake "mono_shake3" o toUntangle3
adamc@506 1367
adamc@377 1368 val pathcheck = {
adamc@377 1369 func = (fn file => (PathCheck.check file; file)),
adamc@377 1370 print = MonoPrint.p_file MonoEnv.empty
adamc@377 1371 }
adamc@377 1372
adamc@916 1373 val toPathcheck = transform pathcheck "pathcheck" o toMono_shake3
adamc@377 1374
adam@1595 1375 val sidecheck = {
adam@1595 1376 func = SideCheck.check,
adam@1595 1377 print = MonoPrint.p_file MonoEnv.empty
adam@1595 1378 }
adam@1595 1379
adam@1595 1380 val toSidecheck = transform sidecheck "sidecheck" o toPathcheck
adam@1595 1381
adamc@202 1382 val cjrize = {
adamc@202 1383 func = Cjrize.cjrize,
adamc@202 1384 print = CjrPrint.p_file CjrEnv.empty
adamc@202 1385 }
adamc@23 1386
adam@1595 1387 val toCjrize = transform cjrize "cjrize" o toSidecheck
adamc@29 1388
adamc@643 1389 val scriptcheck = {
adamc@643 1390 func = ScriptCheck.classify,
adamc@643 1391 print = CjrPrint.p_file CjrEnv.empty
adamc@643 1392 }
adamc@643 1393
adamc@643 1394 val toScriptcheck = transform scriptcheck "scriptcheck" o toCjrize
adamc@643 1395
adamc@282 1396 val prepare = {
adamc@282 1397 func = Prepare.prepare,
adamc@282 1398 print = CjrPrint.p_file CjrEnv.empty
adamc@282 1399 }
adamc@282 1400
adamc@643 1401 val toPrepare = transform prepare "prepare" o toScriptcheck
adamc@282 1402
adamc@879 1403 val checknest = {
adamc@879 1404 func = fn f => if #supportsNestedPrepared (Settings.currentDbms ()) then f else Checknest.annotate f,
adamc@879 1405 print = CjrPrint.p_file CjrEnv.empty
adamc@879 1406 }
adamc@879 1407
adamc@879 1408 val toChecknest = transform checknest "checknest" o toPrepare
adamc@879 1409
adamc@274 1410 val sqlify = {
adamc@274 1411 func = Cjrize.cjrize,
adamc@274 1412 print = CjrPrint.p_sql CjrEnv.empty
adamc@274 1413 }
adamc@274 1414
adamc@274 1415 val toSqlify = transform sqlify "sqlify" o toMono_opt2
adamc@274 1416
adam@1473 1417 val escapeFilename = String.translate (fn #" " => "\\ " | #"\"" => "\\\"" | #"'" => "\\'" | ch => str ch)
adam@1473 1418
adam@1540 1419 val beforeC = ref (fn () => ())
adam@1540 1420
adam@1725 1421 fun compileC {cname, oname, ename, libs, profile, debug, linker, link = link'} =
adamc@183 1422 let
adamc@855 1423 val proto = Settings.currentProtocol ()
adamc@1095 1424
adam@1368 1425 val lib = if Settings.getStaticLinking () then
ezyang@1739 1426 " " ^ !Settings.configLib ^ "/" ^ #linkStatic proto ^ " " ^ !Settings.configLib ^ "/liburweb.a"
adam@1368 1427 else
ezyang@1739 1428 "-L" ^ !Settings.configLib ^ " " ^ #linkDynamic proto ^ " -lurweb"
adamc@378 1429
adam@1558 1430 val opt = if debug then
adam@1558 1431 ""
adam@1558 1432 else
adam@1558 1433 " -O3"
adam@1558 1434
adam@1558 1435 val compile = Config.ccompiler ^ " " ^ Config.ccArgs ^ " " ^ Config.pthreadCflags ^ " -Wimplicit -Werror -Wno-unused-value"
ezyang@1739 1436 ^ opt ^ " -I " ^ !Settings.configInclude
adamc@1096 1437 ^ " " ^ #compile proto
adam@1473 1438 ^ " -c " ^ escapeFilename cname ^ " -o " ^ escapeFilename oname
adamc@1096 1439
adam@1725 1440 val linker = Option.getOpt (linker, Config.ccompiler ^ " -Werror" ^ opt ^ " " ^ Config.ccArgs ^ " " ^ Config.pthreadCflags ^ " " ^ Config.pthreadLibs)
adam@1725 1441
adam@1725 1442 val link = linker
adam@1659 1443 ^ " " ^ lib ^ " " ^ escapeFilename oname ^ " " ^ libs ^ " -lm " ^ Config.openssl ^ " -o " ^ escapeFilename ename
adamc@502 1444
adamc@502 1445 val (compile, link) =
adamc@502 1446 if profile then
adamc@502 1447 (compile ^ " -pg", link ^ " -pg")
adamc@502 1448 else
adamc@502 1449 (compile, link)
adamc@742 1450
adamc@742 1451 val (compile, link) =
adamc@742 1452 if debug then
adamc@742 1453 (compile ^ " -g", link ^ " -g")
adamc@742 1454 else
adamc@742 1455 (compile, link)
adamc@764 1456
adamc@764 1457 val link = foldl (fn (s, link) => link ^ " " ^ s) link link'
adam@1394 1458
adam@1394 1459 fun system s =
adam@1394 1460 (if debug then
adam@1394 1461 print (s ^ "\n")
adam@1394 1462 else
adam@1394 1463 ();
adam@1394 1464 OS.Process.isSuccess (OS.Process.system s))
adamc@183 1465 in
adam@1540 1466 !beforeC ();
adam@1394 1467 system compile andalso system link
adamc@183 1468 end
adamc@183 1469
adamc@202 1470 fun compile job =
adamc@879 1471 case run toChecknest job of
adamc@1045 1472 NONE => false
adamc@29 1473 | SOME file =>
adamc@202 1474 let
adamc@274 1475 val job = valOf (run (transform parseUrp "parseUrp") job)
adamc@102 1476
adamc@274 1477 val (cname, oname, cleanup) =
adamc@274 1478 if #debug job then
adamc@457 1479 ("/tmp/webapp.c", "/tmp/webapp.o", fn () => ())
adamc@274 1480 else
adamc@274 1481 let
adamc@274 1482 val dir = OS.FileSys.tmpName ()
adamc@403 1483 val () = if OS.FileSys.access (dir, []) then
adamc@403 1484 OS.FileSys.remove dir
adamc@403 1485 else
adamc@403 1486 ()
adamc@457 1487 val cname = OS.Path.joinDirFile {dir = dir, file = "webapp.c"}
adamc@457 1488 val oname = OS.Path.joinDirFile {dir = dir, file = "webapp.o"}
adamc@274 1489 in
adamc@274 1490 OS.FileSys.mkDir dir;
adamc@274 1491 (cname, oname,
adam@1700 1492 fn () => (if OS.Process.isSuccess (OS.Process.system ("rm -rf " ^ dir)) then
adam@1700 1493 ()
adam@1700 1494 else
adam@1700 1495 raise Fail ("Unable to delete temporary directory " ^ dir)))
adamc@274 1496 end
adamc@274 1497 val ename = #exe job
adamc@202 1498 in
adamc@274 1499 let
adamc@274 1500 val outf = TextIO.openOut cname
adamc@274 1501 val s = TextIOPP.openOut {dst = outf, wid = 80}
adamc@432 1502
adamc@432 1503 val hasDb = List.exists (fn (Cjr.DDatabase _, _) => true | _ => false) (#1 file)
adamc@432 1504 val libs =
adamc@432 1505 if hasDb then
adamc@866 1506 #link (Settings.currentDbms ())
adamc@432 1507 else
adamc@432 1508 ""
adamc@274 1509 in
adamc@274 1510 Print.fprint s (CjrPrint.p_file CjrEnv.empty file);
adamc@415 1511 TextIO.output1 (outf, #"\n");
adamc@274 1512 TextIO.closeOut outf;
adamc@102 1513
adam@1381 1514 if ErrorMsg.anyErrors () then
adam@1381 1515 false
adam@1381 1516 else
adam@1381 1517 (case #sql job of
adam@1381 1518 NONE => ()
adam@1381 1519 | SOME sql =>
adam@1381 1520 let
adam@1381 1521 val outf = TextIO.openOut sql
adam@1381 1522 val s = TextIOPP.openOut {dst = outf, wid = 80}
adam@1381 1523 in
adam@1381 1524 Print.fprint s (CjrPrint.p_sql CjrEnv.empty file);
adam@1381 1525 TextIO.closeOut outf
adam@1381 1526 end;
adamc@274 1527
adam@1381 1528 compileC {cname = cname, oname = oname, ename = ename, libs = libs,
adam@1725 1529 profile = #profile job, debug = #debug job, linker = #linker job, link = #link job}
adam@1381 1530
adam@1381 1531 before cleanup ())
adamc@274 1532 end
adamc@274 1533 handle ex => (((cleanup ()) handle _ => ()); raise ex)
adamc@202 1534 end
adamc@29 1535
adamc@1045 1536 fun compiler job =
adamc@1045 1537 if compile job then
adamc@1045 1538 ()
adamc@1045 1539 else
adamc@1045 1540 OS.Process.exit OS.Process.failure
adamc@1045 1541
adamc@1266 1542 fun moduleOf fname =
adamc@1266 1543 let
adamc@1266 1544 val mrs = !moduleRoots
adamc@1266 1545 val fname = OS.Path.mkCanonical fname
adamc@1266 1546 in
adamc@1266 1547 case List.find (fn (root, _) => String.isPrefix (root ^ "/") fname) mrs of
adamc@1266 1548 NONE => capitalize (OS.Path.base (OS.Path.file fname))
adamc@1266 1549 | SOME (root, name) =>
adamc@1266 1550 let
adamc@1266 1551 val fname = OS.Path.base fname
adamc@1266 1552 val fname = String.extract (fname, size root + 1, NONE)
adamc@1266 1553 val fs = String.fields (fn ch => ch = #"/") fname
adamc@1266 1554 val fs = List.filter (CharVector.exists (fn ch => not (Char.isDigit ch))) fs
adamc@1266 1555 val fs = map capitalize fs
adamc@1266 1556 in
adamc@1266 1557 String.concatWith "." (name :: fs)
adamc@1266 1558 end
adamc@1266 1559 end
adamc@1266 1560
adamc@1 1561 end