annotate src/compiler.sml @ 1774:27fdd78bd2f5

Avoid redundant rollbacks
author Adam Chlipala <adam@chlipala.net>
date Sat, 02 Jun 2012 10:54:49 -0400
parents b5f5e8d439c7
children 85a87f155e7b
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
adamc@1082 404 fun parseUrp' accLibs fname =
adam@1296 405 if not (Posix.FileSys.access (fname ^ ".urp", []) orelse Posix.FileSys.access (fname ^ "/lib.urp", []))
adam@1296 406 andalso Posix.FileSys.access (fname ^ ".ur", []) then
adam@1296 407 let
adam@1296 408 val job = {prefix = "/",
adam@1296 409 database = NONE,
adam@1296 410 sources = [fname],
adam@1296 411 exe = fname ^ ".exe",
adam@1296 412 sql = NONE,
adam@1396 413 debug = Settings.getDebug (),
adam@1296 414 profile = false,
adam@1296 415 timeout = 60,
adam@1296 416 ffi = [],
adam@1296 417 link = [],
adam@1725 418 linker = NONE,
adam@1296 419 headers = [],
adam@1296 420 scripts = [],
adam@1296 421 clientToServer = [],
adam@1296 422 effectful = [],
adam@1296 423 benignEffectful = [],
adam@1296 424 clientOnly = [],
adam@1296 425 serverOnly = [],
adam@1296 426 jsFuncs = [],
adam@1296 427 rewrites = [{pkind = Settings.Any,
adam@1296 428 kind = Settings.Prefix,
adam@1752 429 from = capitalize (OS.Path.file fname) ^ "/", to = "",
adam@1752 430 hyphenate = false}],
adam@1296 431 filterUrl = [],
adam@1296 432 filterMime = [],
adam@1465 433 filterRequest = [],
adam@1465 434 filterResponse = [],
adam@1296 435 protocol = NONE,
adam@1296 436 dbms = NONE,
adam@1296 437 sigFile = NONE,
adam@1296 438 safeGets = [],
adam@1332 439 onError = NONE,
adam@1332 440 minHeap = 0}
adam@1296 441 in
adam@1296 442 institutionalizeJob job;
adam@1296 443 {Job = job, Libs = []}
adam@1296 444 end
adam@1296 445 else
adam@1296 446 let
adam@1296 447 val pathmap = ref (!pathmap)
adam@1296 448 val bigLibs = ref []
adamc@767 449
adam@1296 450 fun pu filename =
adam@1296 451 let
adam@1338 452 val filename = OS.Path.mkAbsolute {path = filename, relativeTo = OS.FileSys.getDir ()}
adam@1338 453
adam@1296 454 val dir = OS.Path.dir filename
adam@1296 455 fun opener () = TextIO.openIn (OS.Path.joinBaseExt {base = filename, ext = SOME "urp"})
adamc@1151 456
adam@1296 457 val inf = opener ()
adamc@1151 458
adam@1296 459 fun hasSpaceLine () =
adam@1331 460 case inputCommentableLine inf of
adam@1603 461 Content s => s = "debug" orelse s = "profile"
adam@1603 462 orelse CharVector.exists (fn ch => ch = #" " orelse ch = #"\t") s orelse hasSpaceLine ()
adam@1605 463 | EndOfFile => false
adam@1605 464 | OnlyComment => hasSpaceLine ()
adamc@1151 465
adam@1296 466 val hasBlankLine = hasSpaceLine ()
adamc@1151 467
adam@1296 468 val inf = (TextIO.closeIn inf; opener ())
adamc@767 469
adam@1296 470 fun pathify fname =
adam@1296 471 if size fname > 0 andalso String.sub (fname, 0) = #"$" then
adam@1296 472 let
adam@1296 473 val fname' = Substring.extract (fname, 1, NONE)
adam@1296 474 val (befor, after) = Substring.splitl (fn ch => ch <> #"/") fname'
adam@1296 475 in
adam@1340 476 case M.find (!pathmap, Substring.string befor) of
adam@1340 477 NONE => fname
adam@1340 478 | SOME rep => rep ^ Substring.string after
adam@1296 479 end
adam@1296 480 else
adam@1296 481 fname
adam@1296 482
adam@1296 483 fun relify fname =
adamc@794 484 let
adam@1296 485 val fname = pathify fname
adamc@794 486 in
adam@1296 487 OS.Path.concat (dir, fname)
adam@1296 488 handle OS.Path.Path => fname
adamc@794 489 end
adamc@767 490
adam@1296 491 fun libify path =
adam@1296 492 (if Posix.FileSys.access (path ^ ".urp", []) then
adam@1296 493 path
adam@1296 494 else
adam@1296 495 path ^ "/lib")
adam@1296 496 handle SysErr => path
adamc@767 497
adam@1296 498 fun libify' path =
adam@1296 499 (if Posix.FileSys.access (relify path ^ ".urp", []) then
adam@1296 500 path
adam@1296 501 else
adam@1296 502 path ^ "/lib")
adam@1296 503 handle SysErr => path
adamc@767 504
adam@1296 505 val absDir = OS.Path.mkAbsolute {path = dir, relativeTo = OS.FileSys.getDir ()}
adamc@794 506
adam@1296 507 fun relifyA fname =
adam@1296 508 OS.Path.mkAbsolute {path = pathify fname, relativeTo = absDir}
adamc@794 509
adam@1296 510 fun readSources acc =
adam@1331 511 case inputCommentableLine inf of
adam@1603 512 Content line =>
adam@1296 513 let
adam@1296 514 val acc = if CharVector.all Char.isSpace line then
adam@1296 515 acc
adam@1296 516 else
adam@1296 517 let
adam@1296 518 val fname = String.implode (List.filter (fn x => not (Char.isSpace x))
adam@1296 519 (String.explode line))
adam@1296 520 val fname = relifyA fname
adam@1296 521 in
adam@1296 522 fname :: acc
adam@1296 523 end
adam@1296 524 in
adam@1296 525 readSources acc
adam@1296 526 end
adam@1606 527 | OnlyComment => readSources acc
adam@1606 528 | EndOfFile => rev acc
adamc@794 529
adam@1637 530 val prefix = ref (case Settings.getUrlPrefixFull () of "/" => NONE | s => SOME s)
adam@1296 531 val database = ref (Settings.getDbstring ())
adam@1296 532 val exe = ref (Settings.getExe ())
adam@1296 533 val sql = ref (Settings.getSql ())
adam@1296 534 val debug = ref (Settings.getDebug ())
adam@1296 535 val profile = ref false
adam@1296 536 val timeout = ref NONE
adam@1296 537 val ffi = ref []
adam@1296 538 val link = ref []
adam@1725 539 val linker = ref NONE
adam@1296 540 val headers = ref []
adam@1296 541 val scripts = ref []
adam@1296 542 val clientToServer = ref []
adam@1296 543 val effectful = ref []
adam@1296 544 val benignEffectful = ref []
adam@1296 545 val clientOnly = ref []
adam@1296 546 val serverOnly = ref []
adam@1296 547 val jsFuncs = ref []
adam@1296 548 val rewrites = ref []
adam@1296 549 val url = ref []
adam@1296 550 val mime = ref []
adam@1465 551 val request = ref []
adam@1465 552 val response = ref []
adam@1296 553 val libs = ref []
adam@1296 554 val protocol = ref NONE
adam@1296 555 val dbms = ref NONE
adam@1296 556 val sigFile = ref (Settings.getSigFile ())
adam@1296 557 val safeGets = ref []
adam@1296 558 val onError = ref NONE
adam@1332 559 val minHeap = ref 0
adamc@794 560
adam@1296 561 fun finish sources =
adam@1296 562 let
adam@1296 563 val job = {
adam@1296 564 prefix = Option.getOpt (!prefix, "/"),
adam@1296 565 database = !database,
adam@1296 566 exe = Option.getOpt (!exe, OS.Path.joinBaseExt {base = OS.Path.base filename,
adam@1296 567 ext = SOME "exe"}),
adam@1296 568 sql = !sql,
adam@1296 569 debug = !debug,
adam@1296 570 profile = !profile,
adam@1296 571 timeout = Option.getOpt (!timeout, 60),
adam@1296 572 ffi = rev (!ffi),
adam@1296 573 link = rev (!link),
adam@1725 574 linker = !linker,
adam@1296 575 headers = rev (!headers),
adam@1296 576 scripts = rev (!scripts),
adam@1296 577 clientToServer = rev (!clientToServer),
adam@1296 578 effectful = rev (!effectful),
adam@1296 579 benignEffectful = rev (!benignEffectful),
adam@1296 580 clientOnly = rev (!clientOnly),
adam@1296 581 serverOnly = rev (!serverOnly),
adam@1296 582 jsFuncs = rev (!jsFuncs),
adam@1296 583 rewrites = rev (!rewrites),
adam@1296 584 filterUrl = rev (!url),
adam@1296 585 filterMime = rev (!mime),
adam@1465 586 filterRequest = rev (!request),
adam@1465 587 filterResponse = rev (!response),
adam@1296 588 sources = sources,
adam@1296 589 protocol = !protocol,
adam@1296 590 dbms = !dbms,
adam@1296 591 sigFile = !sigFile,
adam@1296 592 safeGets = rev (!safeGets),
adam@1332 593 onError = !onError,
adam@1332 594 minHeap = !minHeap
adam@1296 595 }
adamc@794 596
adam@1296 597 fun mergeO f (old, new) =
adam@1296 598 case (old, new) of
adam@1296 599 (NONE, _) => new
adam@1296 600 | (_, NONE) => old
adam@1296 601 | (SOME v1, SOME v2) => SOME (f (v1, v2))
adamc@794 602
adam@1296 603 fun same desc = mergeO (fn (x : string, y) =>
adam@1296 604 (if x = y then
adam@1296 605 ()
adam@1296 606 else
adam@1296 607 ErrorMsg.error ("Multiple "
adam@1296 608 ^ desc ^ " values that don't agree");
adam@1296 609 x))
adamc@794 610
adam@1296 611 fun merge (old : job, new : job) = {
adam@1637 612 prefix = case #prefix old of
adam@1637 613 "/" => #prefix new
adam@1637 614 | pold => case #prefix new of
adam@1637 615 "/" => pold
adam@1637 616 | pnew => (if pold = pnew then
adam@1637 617 ()
adam@1637 618 else
adam@1637 619 ErrorMsg.error ("Multiple prefix values that don't agree: "
adam@1637 620 ^ pold ^ ", " ^ pnew);
adam@1637 621 pold),
adam@1296 622 database = mergeO (fn (old, _) => old) (#database old, #database new),
adam@1296 623 exe = #exe old,
adam@1296 624 sql = #sql old,
adam@1296 625 debug = #debug old orelse #debug new,
adam@1296 626 profile = #profile old orelse #profile new,
adam@1296 627 timeout = #timeout old,
adam@1296 628 ffi = #ffi old @ #ffi new,
adam@1296 629 link = #link old @ #link new,
adam@1725 630 linker = mergeO (fn (_, new) => new) (#linker old, #linker new),
adam@1296 631 headers = #headers old @ #headers new,
adam@1296 632 scripts = #scripts old @ #scripts new,
adam@1296 633 clientToServer = #clientToServer old @ #clientToServer new,
adam@1296 634 effectful = #effectful old @ #effectful new,
adam@1296 635 benignEffectful = #benignEffectful old @ #benignEffectful new,
adam@1296 636 clientOnly = #clientOnly old @ #clientOnly new,
adam@1296 637 serverOnly = #serverOnly old @ #serverOnly new,
adam@1296 638 jsFuncs = #jsFuncs old @ #jsFuncs new,
adam@1296 639 rewrites = #rewrites old @ #rewrites new,
adam@1296 640 filterUrl = #filterUrl old @ #filterUrl new,
adam@1296 641 filterMime = #filterMime old @ #filterMime new,
adam@1465 642 filterRequest = #filterRequest old @ #filterRequest new,
adam@1465 643 filterResponse = #filterResponse old @ #filterResponse new,
adam@1296 644 sources = #sources new
adam@1296 645 @ List.filter (fn s => List.all (fn s' => s' <> s) (#sources new))
adam@1296 646 (#sources old),
adam@1296 647 protocol = mergeO #2 (#protocol old, #protocol new),
adam@1296 648 dbms = mergeO #2 (#dbms old, #dbms new),
adam@1296 649 sigFile = mergeO #2 (#sigFile old, #sigFile new),
adam@1296 650 safeGets = #safeGets old @ #safeGets new,
adam@1332 651 onError = mergeO #2 (#onError old, #onError new),
adam@1332 652 minHeap = Int.max (#minHeap old, #minHeap new)
adam@1296 653 }
adamc@794 654 in
adam@1296 655 if accLibs then
adam@1296 656 foldr (fn (job', job) => merge (job, job')) job (!libs)
adam@1296 657 else
adam@1296 658 job
adamc@794 659 end
adamc@794 660
adam@1296 661 fun parsePkind s =
adam@1296 662 case s of
adam@1296 663 "all" => Settings.Any
adam@1296 664 | "url" => Settings.Url
adam@1296 665 | "table" => Settings.Table
adam@1296 666 | "sequence" => Settings.Sequence
adam@1296 667 | "view" => Settings.View
adam@1296 668 | "relation" => Settings.Relation
adam@1296 669 | "cookie" => Settings.Cookie
adam@1296 670 | "style" => Settings.Style
adam@1296 671 | _ => (ErrorMsg.error "Bad path kind spec";
adam@1296 672 Settings.Any)
adam@1296 673
adam@1296 674 fun parseFrom s =
adam@1296 675 if size s > 1 andalso String.sub (s, size s - 2) = #"/" andalso String.sub (s, size s - 1) = #"*" then
adam@1296 676 (Settings.Prefix, String.substring (s, 0, size s - 1))
adam@1296 677 else
adam@1296 678 (Settings.Exact, s)
adam@1296 679
adam@1296 680 fun parseFkind s =
adam@1296 681 case s of
adam@1296 682 "url" => url
adam@1296 683 | "mime" => mime
adam@1465 684 | "requestHeader" => request
adam@1465 685 | "responseHeader" => response
adam@1296 686 | _ => (ErrorMsg.error "Bad filter kind";
adam@1296 687 url)
adam@1296 688
adam@1296 689 fun parsePattern s =
adam@1296 690 if size s > 0 andalso String.sub (s, size s - 1) = #"*" then
adam@1296 691 (Settings.Prefix, String.substring (s, 0, size s - 1))
adam@1296 692 else
adam@1296 693 (Settings.Exact, s)
adam@1296 694
adam@1296 695 fun read () =
adam@1331 696 case inputCommentableLine inf of
adam@1603 697 EndOfFile => finish []
adam@1603 698 | OnlyComment => read ()
adam@1603 699 | Content "" => finish (readSources [])
adam@1603 700 | Content line =>
adam@1296 701 let
adam@1296 702 val (cmd, arg) = Substring.splitl (fn x => not (Char.isSpace x)) (Substring.full line)
adam@1296 703 val cmd = Substring.string (trim cmd)
adam@1296 704 val arg = Substring.string (trim arg)
adam@1296 705
adam@1296 706 fun ffiS () =
adam@1296 707 case String.fields (fn ch => ch = #".") arg of
adam@1296 708 [m, x] => (m, x)
adam@1296 709 | _ => (ErrorMsg.error (cmd ^ " argument not of the form Module.func");
adam@1296 710 ("", ""))
adam@1296 711
adam@1296 712 fun ffiM () =
adam@1296 713 case String.fields (fn ch => ch = #"=") arg of
adam@1296 714 [f, s] =>
adam@1433 715 let
adam@1433 716 val f = trimS f
adam@1433 717 val s = trimS s
adam@1433 718 in
adam@1433 719 case String.fields (fn ch => ch = #".") f of
adam@1433 720 [m, x] => ((m, x), s)
adam@1433 721 | _ => (ErrorMsg.error (cmd ^ " argument not of the form Module.func=func'");
adam@1433 722 (("", ""), ""))
adam@1433 723 end
adam@1296 724 | _ => (ErrorMsg.error (cmd ^ " argument not of the form Module.func=func'");
adam@1296 725 (("", ""), ""))
adam@1296 726 in
adam@1296 727 case cmd of
adam@1336 728 "prefix" => prefix := SOME arg
adam@1296 729 | "database" =>
adam@1296 730 (case !database of
adam@1296 731 NONE => database := SOME arg
adam@1296 732 | SOME _ => ())
adam@1296 733 | "dbms" =>
adam@1296 734 (case !dbms of
adam@1296 735 NONE => dbms := SOME arg
adam@1296 736 | SOME _ => ())
adam@1296 737 | "sigfile" =>
adam@1296 738 (case !sigFile of
adam@1296 739 NONE => sigFile := SOME arg
adam@1296 740 | SOME _ => ())
adam@1296 741 | "exe" =>
adam@1296 742 (case !exe of
adam@1296 743 NONE => exe := SOME (relify arg)
adam@1296 744 | SOME _ => ())
adam@1296 745 | "sql" =>
adam@1296 746 (case !sql of
adam@1296 747 NONE => sql := SOME (relify arg)
adam@1296 748 | SOME _ => ())
adam@1296 749 | "debug" => debug := true
adam@1296 750 | "profile" => profile := true
adam@1296 751 | "timeout" =>
adam@1296 752 (case !timeout of
adam@1296 753 NONE => ()
adam@1296 754 | SOME _ => ErrorMsg.error "Duplicate 'timeout' directive";
adam@1296 755 timeout := SOME (valOf (Int.fromString arg)))
adam@1296 756 | "ffi" => ffi := relify arg :: !ffi
adam@1296 757 | "link" => let
adam@1296 758 val arg = if size arg >= 1
adam@1296 759 andalso String.sub (arg, 0) = #"-" then
adam@1296 760 arg
adam@1296 761 else
adam@1296 762 relifyA arg
adam@1296 763 in
adam@1296 764 link := arg :: !link
adam@1296 765 end
adam@1725 766 | "linker" => linker := SOME arg
adam@1296 767 | "include" => headers := relifyA arg :: !headers
adam@1296 768 | "script" => scripts := arg :: !scripts
adam@1296 769 | "clientToServer" => clientToServer := ffiS () :: !clientToServer
adam@1296 770 | "safeGet" => safeGets := arg :: !safeGets
adam@1296 771 | "effectful" => effectful := ffiS () :: !effectful
adam@1296 772 | "benignEffectful" => benignEffectful := ffiS () :: !benignEffectful
adam@1296 773 | "clientOnly" => clientOnly := ffiS () :: !clientOnly
adam@1296 774 | "serverOnly" => serverOnly := ffiS () :: !serverOnly
adam@1296 775 | "jsFunc" => jsFuncs := ffiM () :: !jsFuncs
adam@1296 776 | "rewrite" =>
adam@1296 777 let
adam@1752 778 fun doit (pkind, from, to, hyph) =
adam@1296 779 let
adam@1296 780 val pkind = parsePkind pkind
adam@1296 781 val (kind, from) = parseFrom from
adam@1296 782 in
adam@1752 783 rewrites := {pkind = pkind, kind = kind, from = from, to = to, hyphenate = hyph} :: !rewrites
adam@1296 784 end
adam@1296 785 in
adam@1296 786 case String.tokens Char.isSpace arg of
adam@1752 787 [pkind, from, to, "[-]"] => doit (pkind, from, to, true)
adam@1752 788 | [pkind, from, "[-]"] => doit (pkind, from, "", true)
adam@1752 789 | [pkind, from, to] => doit (pkind, from, to, false)
adam@1752 790 | [pkind, from] => doit (pkind, from, "", false)
adam@1296 791 | _ => ErrorMsg.error "Bad 'rewrite' syntax"
adam@1296 792 end
adam@1296 793 | "allow" =>
adam@1296 794 (case String.tokens Char.isSpace arg of
adam@1296 795 [fkind, pattern] =>
adam@1296 796 let
adam@1296 797 val fkind = parseFkind fkind
adam@1296 798 val (kind, pattern) = parsePattern pattern
adam@1296 799 in
adam@1296 800 fkind := {action = Settings.Allow, kind = kind, pattern = pattern} :: !fkind
adam@1296 801 end
adam@1296 802 | _ => ErrorMsg.error "Bad 'allow' syntax")
adam@1296 803 | "deny" =>
adam@1296 804 (case String.tokens Char.isSpace arg of
adam@1296 805 [fkind, pattern] =>
adam@1296 806 let
adam@1296 807 val fkind = parseFkind fkind
adam@1296 808 val (kind, pattern) = parsePattern pattern
adam@1296 809 in
adam@1296 810 fkind := {action = Settings.Deny, kind = kind, pattern = pattern} :: !fkind
adam@1296 811 end
adam@1296 812 | _ => ErrorMsg.error "Bad 'deny' syntax")
adam@1296 813 | "library" => if accLibs then
adam@1296 814 libs := pu (libify (relify arg)) :: !libs
adam@1296 815 else
adam@1296 816 bigLibs := libify' arg :: !bigLibs
adam@1296 817 | "path" =>
adam@1296 818 (case String.fields (fn ch => ch = #"=") arg of
adam@1338 819 [n, v] => ((pathmap := M.insert (!pathmap, n, OS.Path.mkAbsolute {path = v, relativeTo = dir}))
adam@1338 820 handle OS.Path.Path => ErrorMsg.error "Invalid 'path' directory argument")
adam@1296 821 | _ => ErrorMsg.error "path argument not of the form name=value'")
adam@1296 822 | "onError" =>
adam@1296 823 (case String.fields (fn ch => ch = #".") arg of
adam@1296 824 m1 :: (fs as _ :: _) =>
adam@1296 825 onError := SOME (m1, List.take (fs, length fs - 1), List.last fs)
adam@1296 826 | _ => ErrorMsg.error "invalid 'onError' argument")
adam@1307 827 | "limit" =>
adam@1307 828 (case String.fields Char.isSpace arg of
adam@1307 829 [class, num] =>
adam@1307 830 (case Int.fromString num of
adam@1307 831 NONE => ErrorMsg.error ("invalid limit number '" ^ num ^ "'")
adam@1307 832 | SOME n =>
adam@1307 833 if n < 0 then
adam@1307 834 ErrorMsg.error ("invalid limit number '" ^ num ^ "'")
adam@1307 835 else
adam@1307 836 Settings.addLimit (class, n))
adam@1307 837 | _ => ErrorMsg.error "invalid 'limit' arguments")
adam@1332 838 | "minHeap" =>
adam@1332 839 (case Int.fromString arg of
adam@1332 840 NONE => ErrorMsg.error ("invalid min heap '" ^ arg ^ "'")
adam@1332 841 | SOME n => minHeap := n)
adam@1393 842 | "alwaysInline" => Settings.addAlwaysInline arg
adam@1478 843 | "noXsrfProtection" => Settings.addNoXsrfProtection arg
adam@1629 844 | "timeFormat" => Settings.setTimeFormat arg
adam@1296 845
adam@1296 846 | _ => ErrorMsg.error ("Unrecognized command '" ^ cmd ^ "'");
adam@1296 847 read ()
adam@1296 848 end
adam@1296 849
adam@1296 850 val job = if hasBlankLine then
adam@1296 851 read ()
adam@1296 852 else
adam@1296 853 finish (readSources [])
adam@1296 854 in
adam@1296 855 TextIO.closeIn inf;
adam@1296 856 institutionalizeJob job;
adam@1296 857 job
adam@1296 858 end
adam@1296 859 in
adam@1296 860 {Job = pu fname, Libs = !bigLibs}
adam@1296 861 end
adamc@767 862
adamc@1082 863 fun p_job' {Job = j, Libs = _ : string list} = p_job j
adamc@1082 864
adamc@270 865 val parseUrp = {
adamc@1083 866 func = #Job o parseUrp' true,
adamc@270 867 print = p_job
adamc@270 868 }
adamc@270 869
adamc@1082 870 val parseUrp' = {
adamc@1083 871 func = parseUrp' false,
adamc@1082 872 print = p_job'
adamc@1082 873 }
adamc@1082 874
adamc@270 875 val toParseJob = transform parseUrp "parseJob"
adamc@1082 876 val toParseJob' = transform parseUrp' "parseJob'"
adamc@1082 877
adamc@1082 878 fun op o (tr2 : ('b, 'c) transform, tr1 : ('a, 'b) transform) = {
adamc@1082 879 func = fn input => case #func tr1 input of
adamc@1082 880 NONE => NONE
adamc@1082 881 | SOME v => #func tr2 v,
adamc@1082 882 print = #print tr2,
adamc@1082 883 time = fn (input, pmap) => let
adamc@1082 884 val (ro, pmap) = #time tr1 (input, pmap)
adamc@1082 885 in
adamc@1082 886 case ro of
adamc@1082 887 NONE => (NONE, pmap)
adamc@1082 888 | SOME v => #time tr2 (v, pmap)
adamc@1082 889 end
adamc@1082 890 }
adamc@270 891
adamc@1090 892 structure SM = BinaryMapFn(struct
adamc@1090 893 type ord_key = string
adamc@1090 894 val compare = String.compare
adamc@1090 895 end)
adamc@1090 896
adamc@1090 897 val moduleRoots = ref ([] : (string * string) list)
adamc@1090 898 fun addModuleRoot (k, v) = moduleRoots := (k, v) :: !moduleRoots
adamc@1090 899
adam@1743 900 structure SK = struct
adam@1743 901 type ord_key = string
adam@1743 902 val compare = String.compare
adam@1743 903 end
adam@1743 904
adam@1743 905 structure SS = BinarySetFn(SK)
adam@1743 906 structure SM = BinaryMapFn(SK)
adamc@1090 907
adam@1773 908 exception MissingFile of string
adam@1773 909
adamc@201 910 val parse = {
adam@1294 911 func = fn {database, sources = fnames, ffi, onError, ...} : job =>
adamc@201 912 let
adamc@1090 913 val mrs = !moduleRoots
adamc@1090 914
adamc@834 915 val anyErrors = ref false
adamc@834 916 fun checkErrors () = anyErrors := (!anyErrors orelse ErrorMsg.anyErrors ())
adam@1343 917 fun nameOf fname =
adam@1343 918 let
adam@1343 919 val fname = OS.Path.file fname
adam@1343 920 val fst = if size fname = 0 then #"!" else String.sub (fname, 0)
adam@1343 921 in
adam@1343 922 if not (Char.isAlpha fst) then
adam@1343 923 ErrorMsg.error ("Filename doesn't start with letter: " ^ fname)
adam@1343 924 else if CharVector.exists (fn ch => not (Char.isAlphaNum ch) andalso ch <> #"_") fname then
adam@1343 925 ErrorMsg.error ("Filename contains a character that isn't alphanumeric or underscore: " ^ fname)
adam@1343 926 else
adam@1343 927 ();
adam@1343 928 capitalize fname
adam@1343 929 end
adamc@109 930
adamc@764 931 fun parseFfi fname =
adamc@764 932 let
adamc@764 933 val mname = nameOf fname
adamc@764 934 val urs = OS.Path.joinBaseExt {base = fname, ext = SOME "urs"}
adamc@764 935
adamc@764 936 val loc = {file = urs,
adamc@764 937 first = ErrorMsg.dummyPos,
adamc@764 938 last = ErrorMsg.dummyPos}
adamc@764 939
adamc@764 940 val sgn = (Source.SgnConst (#func parseUrs urs), loc)
adamc@764 941 in
adamc@834 942 checkErrors ();
adam@1733 943 (Source.DFfiStr (mname, sgn, if !Elaborate.incremental then SOME (OS.FileSys.modTime urs) else NONE), loc)
adamc@764 944 end
adamc@764 945
adamc@1090 946 val defed = ref SS.empty
adamc@1092 947 val fulls = ref SS.empty
adamc@1090 948
adamc@201 949 fun parseOne fname =
adamc@201 950 let
adamc@201 951 val mname = nameOf fname
adamc@244 952 val ur = OS.Path.joinBaseExt {base = fname, ext = SOME "ur"}
adamc@244 953 val urs = OS.Path.joinBaseExt {base = fname, ext = SOME "urs"}
adamc@56 954
adam@1773 955 val () = if Posix.FileSys.access (ur, []) then
adam@1773 956 ()
adam@1773 957 else
adam@1773 958 raise MissingFile ur
adam@1773 959
adamc@201 960 val sgnO =
adamc@244 961 if Posix.FileSys.access (urs, []) then
adamc@244 962 SOME (Source.SgnConst (#func parseUrs urs),
adamc@244 963 {file = urs,
adamc@201 964 first = ErrorMsg.dummyPos,
adamc@201 965 last = ErrorMsg.dummyPos})
adamc@834 966 before checkErrors ()
adamc@201 967 else
adamc@201 968 NONE
adamc@56 969
adamc@244 970 val loc = {file = ur,
adamc@201 971 first = ErrorMsg.dummyPos,
adamc@201 972 last = ErrorMsg.dummyPos}
adamc@56 973
adam@1738 974 val urt = OS.FileSys.modTime ur
adam@1738 975 val urst = (OS.FileSys.modTime urs) handle _ => urt
adam@1738 976
adamc@244 977 val ds = #func parseUr ur
adam@1738 978 val d = (Source.DStr (mname, sgnO, if !Elaborate.incremental then SOME (if Time.> (urt, urst) then urt else urst) else NONE,
adam@1733 979 (Source.StrConst ds, loc)), loc)
adamc@1090 980
adamc@1126 981 val fname = OS.Path.mkCanonical fname
adamc@1090 982 val d = case List.find (fn (root, name) =>
adamc@1090 983 String.isPrefix (root ^ "/") fname) mrs of
adamc@1090 984 NONE => d
adamc@1090 985 | SOME (root, name) =>
adamc@1090 986 let
adamc@1090 987 val fname = String.extract (fname, size root + 1, NONE)
adamc@1090 988 val pieces = name :: String.tokens (fn ch => ch = #"/") fname
adamc@1090 989 val pieces = List.filter (fn s => size s > 0
adamc@1090 990 andalso Char.isAlpha (String.sub (s, 0)))
adamc@1090 991 pieces
adamc@1090 992 val pieces = map capitalize pieces
adamc@1092 993 val full = String.concatWith "." pieces
adamc@1090 994
adamc@1090 995 fun makeD prefix pieces =
adamc@1090 996 case pieces of
adamc@1090 997 [] => (ErrorMsg.error "Empty module path";
adamc@1090 998 (Source.DStyle "Boo", loc))
adamc@1090 999 | [_] => d
adamc@1090 1000 | piece :: pieces =>
adamc@1090 1001 let
adamc@1146 1002 val this = case prefix of
adamc@1146 1003 "" => piece
adamc@1146 1004 | _ => prefix ^ "." ^ piece
adamc@1090 1005 val old = SS.member (!defed, this)
adamc@1146 1006
adamc@1146 1007 fun notThere (ch, s) =
adamc@1146 1008 Substring.isEmpty (#2 (Substring.splitl
adamc@1146 1009 (fn ch' => ch' <> ch) s))
adamc@1146 1010
adamc@1146 1011 fun simOpen () =
adamc@1146 1012 SS.foldl (fn (full, ds) =>
adamc@1146 1013 if String.isPrefix (this ^ ".") full
adamc@1146 1014 andalso notThere (#".",
adamc@1146 1015 Substring.extract (full,
adamc@1146 1016 size
adamc@1146 1017 this + 1,
adamc@1146 1018 NONE)) then
adamc@1146 1019 let
adamc@1146 1020 val parts = String.tokens
adamc@1146 1021 (fn ch => ch = #".") full
adamc@1146 1022
adamc@1146 1023 val part = List.last parts
adamc@1146 1024
adamc@1146 1025 val imp = if length parts >= 2 then
adamc@1146 1026 (Source.StrProj
adamc@1146 1027 ((Source.StrVar
adamc@1146 1028 (List.nth (parts,
adamc@1146 1029 length
adamc@1146 1030 parts
adamc@1146 1031 - 2)),
adamc@1146 1032 loc),
adamc@1146 1033 part), loc)
adamc@1146 1034 else
adamc@1146 1035 (Source.StrVar part, loc)
adamc@1146 1036 in
adam@1732 1037 (Source.DStr (part, NONE, NONE, imp),
adamc@1146 1038 loc) :: ds
adamc@1146 1039 end
adamc@1146 1040 else
adamc@1146 1041 ds) [] (!fulls)
adamc@1090 1042 in
adamc@1090 1043 defed := SS.add (!defed, this);
adam@1732 1044 (Source.DStr (piece, NONE, NONE,
adamc@1090 1045 (Source.StrConst (if old then
adamc@1146 1046 simOpen ()
adamc@1146 1047 @ [makeD this pieces]
adamc@1090 1048 else
adamc@1146 1049 [makeD this pieces]), loc)),
adamc@1090 1050 loc)
adamc@1090 1051 end
adamc@1090 1052 in
adamc@1092 1053 if SS.member (!fulls, full) then
adamc@1092 1054 ErrorMsg.error ("Rooted module " ^ full ^ " has multiple versions.")
adamc@1092 1055 else
adamc@1092 1056 ();
adamc@1146 1057
adamc@1090 1058 makeD "" pieces
adamc@1146 1059 before ignore (foldl (fn (new, path) =>
adamc@1146 1060 let
adamc@1146 1061 val new' = case path of
adamc@1146 1062 "" => new
adamc@1146 1063 | _ => path ^ "." ^ new
adamc@1146 1064 in
adamc@1146 1065 fulls := SS.add (!fulls, new');
adamc@1146 1066 new'
adamc@1146 1067 end) "" pieces)
adamc@1090 1068 end
adamc@201 1069 in
adamc@834 1070 checkErrors ();
adamc@1090 1071 d
adam@1773 1072 end handle MissingFile fname => (ErrorMsg.error ("Missing source file: " ^ fname);
adam@1773 1073 (Source.DSequence "", ErrorMsg.dummySpan))
adamc@56 1074
adamc@764 1075 val dsFfi = map parseFfi ffi
adamc@201 1076 val ds = map parseOne fnames
adamc@1090 1077 val loc = ErrorMsg.dummySpan
adamc@201 1078 in
adamc@834 1079 if !anyErrors then
adamc@834 1080 ErrorMsg.error "Parse failure"
adamc@834 1081 else
adamc@834 1082 ();
adamc@834 1083
adamc@201 1084 let
adamc@1126 1085 val final = List.last fnames
adamc@1126 1086 val final = case List.find (fn (root, name) =>
adamc@1126 1087 String.isPrefix (root ^ "/") final) mrs of
adamc@1126 1088 NONE => (Source.StrVar (nameOf final), loc)
adamc@1126 1089 | SOME (root, name) =>
adamc@1126 1090 let
adamc@1126 1091 val m = (Source.StrVar name, loc)
adamc@1126 1092 val final = String.extract (final, size root + 1, NONE)
adamc@1264 1093 val fields = String.fields (fn ch => ch = #"/") final
adamc@1264 1094 val fields = List.filter (fn s => size s = 0
adamc@1264 1095 orelse not (Char.isDigit (String.sub (s, 0))))
adamc@1264 1096 fields
adamc@1126 1097 in
adamc@1126 1098 foldl (fn (x, m) => (Source.StrProj (m, capitalize x), loc))
adamc@1264 1099 m fields
adamc@1126 1100 end
adamc@271 1101
adamc@764 1102 val ds = dsFfi @ ds
adamc@1126 1103 @ [(Source.DExport final, loc)]
adamc@1090 1104
adamc@1090 1105 val ds = case database of
adamc@1090 1106 NONE => ds
adamc@1090 1107 | SOME s => (Source.DDatabase s, loc) :: ds
adam@1294 1108
adam@1294 1109 val ds = case onError of
adam@1294 1110 NONE => ds
adam@1294 1111 | SOME v => ds @ [(Source.DOnError v, loc)]
adamc@201 1112 in
adam@1738 1113 ignore (List.foldl (fn (d, used) =>
adam@1738 1114 case #1 d of
adam@1738 1115 Source.DStr (x, _, _, _) =>
adam@1743 1116 (case SM.find (used, x) of
adam@1743 1117 SOME loc =>
adam@1743 1118 (ErrorMsg.error ("Duplicate top-level module name " ^ x);
adam@1743 1119 Print.prefaces "Files" [("Previous", Print.PD.string (ErrorMsg.spanToString loc)),
adam@1743 1120 ("Current", Print.PD.string (ErrorMsg.spanToString (#2 d)))];
adam@1743 1121 used)
adam@1743 1122 | NONE =>
adam@1743 1123 SM.insert (used, x, #2 d))
adam@1743 1124 | _ => used) SM.empty ds);
adamc@1090 1125 ds
adamc@201 1126 end handle Empty => ds
adamc@201 1127 end,
adamc@201 1128 print = SourcePrint.p_file
adamc@201 1129 }
adamc@56 1130
adamc@270 1131 val toParse = transform parse "parse" o toParseJob
adamc@38 1132
adamc@201 1133 val elaborate = {
adamc@201 1134 func = fn file => let
ezyang@1739 1135 val basisF = Settings.libFile "basis.urs"
ezyang@1739 1136 val topF = Settings.libFile "top.urs"
ezyang@1739 1137 val topF' = Settings.libFile "top.ur"
adam@1732 1138
adam@1732 1139 val basis = #func parseUrs basisF
adam@1732 1140 val topSgn = #func parseUrs topF
adam@1732 1141 val topStr = #func parseUr topF'
adam@1732 1142
adam@1732 1143 val tm1 = OS.FileSys.modTime topF
adam@1732 1144 val tm2 = OS.FileSys.modTime topF'
adamc@201 1145 in
adam@1732 1146 Elaborate.elabFile basis (OS.FileSys.modTime basisF)
adam@1732 1147 topStr topSgn (if Time.< (tm1, tm2) then tm2 else tm1)
adam@1732 1148 ElabEnv.empty file
adamc@201 1149 end,
adamc@201 1150 print = ElabPrint.p_file ElabEnv.empty
adamc@201 1151 }
adamc@5 1152
adamc@270 1153 val toElaborate = transform elaborate "elaborate" o toParse
adamc@201 1154
adamc@448 1155 val unnest = {
adamc@448 1156 func = Unnest.unnest,
adamc@448 1157 print = ElabPrint.p_file ElabEnv.empty
adamc@448 1158 }
adamc@448 1159
adamc@448 1160 val toUnnest = transform unnest "unnest" o toElaborate
adamc@448 1161
adamc@313 1162 val termination = {
adamc@313 1163 func = (fn file => (Termination.check file; file)),
adamc@313 1164 print = ElabPrint.p_file ElabEnv.empty
adamc@313 1165 }
adamc@313 1166
adamc@448 1167 val toTermination = transform termination "termination" o toUnnest
adamc@313 1168
adamc@201 1169 val explify = {
adamc@201 1170 func = Explify.explify,
adamc@201 1171 print = ExplPrint.p_file ExplEnv.empty
adamc@201 1172 }
adamc@201 1173
adamc@625 1174 val toExplify = transform explify "explify" o toUnnest
adamc@201 1175
adamc@201 1176 val corify = {
adamc@201 1177 func = Corify.corify,
adamc@201 1178 print = CorePrint.p_file CoreEnv.empty
adamc@201 1179 }
adamc@201 1180
adamc@270 1181 val toCorify = transform corify "corify" o toExplify
adamc@201 1182
adamc@482 1183 (*val reduce_local = {
adamc@482 1184 func = ReduceLocal.reduce,
adamc@482 1185 print = CorePrint.p_file CoreEnv.empty
adamc@482 1186 }
adamc@482 1187
adamc@482 1188 val toReduce_local = transform reduce_local "reduce_local" o toCorify*)
adamc@482 1189
adamc@443 1190 val especialize = {
adamc@443 1191 func = ESpecialize.specialize,
adamc@443 1192 print = CorePrint.p_file CoreEnv.empty
adamc@443 1193 }
adamc@443 1194
adamc@454 1195 val core_untangle = {
adamc@454 1196 func = CoreUntangle.untangle,
adamc@454 1197 print = CorePrint.p_file CoreEnv.empty
adamc@454 1198 }
adamc@454 1199
adamc@794 1200 val toCore_untangle = transform core_untangle "core_untangle" o toCorify
adamc@454 1201
adamc@202 1202 val shake = {
adamc@202 1203 func = Shake.shake,
adamc@202 1204 print = CorePrint.p_file CoreEnv.empty
adamc@202 1205 }
adamc@39 1206
adamc@454 1207 val toShake1 = transform shake "shake1" o toCore_untangle
adamc@110 1208
adam@1362 1209 val toEspecialize1' = transform especialize "especialize1'" o toShake1
adam@1362 1210 val toShake1' = transform shake "shake1'" o toEspecialize1'
adam@1362 1211
adamc@607 1212 val rpcify = {
adamc@607 1213 func = Rpcify.frob,
adamc@607 1214 print = CorePrint.p_file CoreEnv.empty
adamc@607 1215 }
adamc@607 1216
adam@1362 1217 val toRpcify = transform rpcify "rpcify" o toShake1'
adamc@607 1218
adamc@642 1219 val toCore_untangle2 = transform core_untangle "core_untangle2" o toRpcify
adamc@642 1220 val toShake2 = transform shake "shake2" o toCore_untangle2
adamc@1181 1221
adamc@1186 1222 val toEspecialize1 = transform especialize "especialize1" o toShake2
adamc@1181 1223
adamc@1062 1224 val toCore_untangle3 = transform core_untangle "core_untangle3" o toEspecialize1
adamc@1062 1225 val toShake3 = transform shake "shake3" o toCore_untangle3
adamc@642 1226
adamc@202 1227 val tag = {
adamc@202 1228 func = Tag.tag,
adamc@202 1229 print = CorePrint.p_file CoreEnv.empty
adamc@202 1230 }
adamc@193 1231
adamc@1062 1232 val toTag = transform tag "tag" o toShake3
adamc@20 1233
adamc@202 1234 val reduce = {
adamc@202 1235 func = Reduce.reduce,
adamc@202 1236 print = CorePrint.p_file CoreEnv.empty
adamc@202 1237 }
adamc@25 1238
adamc@692 1239 val toReduce = transform reduce "reduce" o toTag
adamc@23 1240
adamc@1186 1241 val toShakey = transform shake "shakey" o toReduce
adamc@1186 1242
adamc@1186 1243 val unpoly = {
adamc@1186 1244 func = Unpoly.unpoly,
adamc@1186 1245 print = CorePrint.p_file CoreEnv.empty
adamc@1186 1246 }
adamc@1186 1247
adamc@1186 1248 val toUnpoly = transform unpoly "unpoly" o toShakey
adamc@315 1249
adamc@202 1250 val specialize = {
adamc@202 1251 func = Specialize.specialize,
adamc@202 1252 print = CorePrint.p_file CoreEnv.empty
adamc@202 1253 }
adamc@132 1254
adamc@1186 1255 val toSpecialize = transform specialize "specialize" o toUnpoly
adamc@131 1256
adamc@1062 1257 val toShake4 = transform shake "shake4" o toSpecialize
adamc@133 1258
adamc@1062 1259 val toEspecialize2 = transform especialize "especialize2" o toShake4
adamc@1272 1260 val toShake4' = transform shake "shake4'" o toEspecialize2
adamc@1272 1261 val toUnpoly2 = transform unpoly "unpoly2" o toShake4'
adamc@1276 1262 val toSpecialize2 = transform specialize "specialize2" o toUnpoly2
adamc@1276 1263 val toShake4'' = transform shake "shake4'" o toSpecialize2
adamc@1272 1264 val toEspecialize3 = transform especialize "especialize3" o toShake4''
adamc@794 1265
adamc@1272 1266 val toReduce2 = transform reduce "reduce2" o toEspecialize3
adamc@898 1267
adamc@1062 1268 val toShake5 = transform shake "shake5" o toReduce2
adamc@794 1269
adamc@692 1270 val marshalcheck = {
adamc@692 1271 func = (fn file => (MarshalCheck.check file; file)),
adamc@692 1272 print = CorePrint.p_file CoreEnv.empty
adamc@692 1273 }
adamc@692 1274
adamc@1062 1275 val toMarshalcheck = transform marshalcheck "marshalcheck" o toShake5
adamc@692 1276
adamc@732 1277 val effectize = {
adamc@732 1278 func = Effective.effectize,
adamc@732 1279 print = CorePrint.p_file CoreEnv.empty
adamc@732 1280 }
adamc@732 1281
adamc@732 1282 val toEffectize = transform effectize "effectize" o toMarshalcheck
adamc@732 1283
adamc@1170 1284 val css = {
adamc@1170 1285 func = Css.summarize,
adamc@1170 1286 print = fn _ => Print.box []
adamc@1170 1287 }
adamc@1170 1288
adamc@1170 1289 val toCss = transform css "css" o toShake5
adamc@1170 1290
adamc@202 1291 val monoize = {
adamc@202 1292 func = Monoize.monoize CoreEnv.empty,
adamc@202 1293 print = MonoPrint.p_file MonoEnv.empty
adamc@202 1294 }
adamc@134 1295
adamc@732 1296 val toMonoize = transform monoize "monoize" o toEffectize
adamc@96 1297
adamc@202 1298 val mono_opt = {
adamc@910 1299 func = MonoOpt.optimize,
adamc@202 1300 print = MonoPrint.p_file MonoEnv.empty
adamc@202 1301 }
adamc@29 1302
adamc@270 1303 val toMono_opt1 = transform mono_opt "mono_opt1" o toMonoize
adamc@5 1304
adamc@202 1305 val untangle = {
adamc@202 1306 func = Untangle.untangle,
adamc@202 1307 print = MonoPrint.p_file MonoEnv.empty
adamc@202 1308 }
adamc@1 1309
adamc@270 1310 val toUntangle = transform untangle "untangle" o toMono_opt1
adamc@38 1311
adamc@202 1312 val mono_reduce = {
adamc@202 1313 func = MonoReduce.reduce,
adamc@202 1314 print = MonoPrint.p_file MonoEnv.empty
adamc@202 1315 }
adamc@16 1316
adamc@270 1317 val toMono_reduce = transform mono_reduce "mono_reduce" o toUntangle
adamc@39 1318
adamc@202 1319 val mono_shake = {
adamc@202 1320 func = MonoShake.shake,
adamc@202 1321 print = MonoPrint.p_file MonoEnv.empty
adamc@202 1322 }
adamc@110 1323
adamc@270 1324 val toMono_shake = transform mono_shake "mono_shake1" o toMono_reduce
adamc@193 1325
adamc@572 1326 val toMono_opt2 = transform mono_opt "mono_opt2" o toMono_shake
adamc@572 1327
adamc@1200 1328 val iflow = {
adamc@1235 1329 func = (fn file => (if !doIflow then Iflow.check file else (); file)),
adamc@1200 1330 print = MonoPrint.p_file MonoEnv.empty
adamc@1200 1331 }
adamc@1200 1332
adamc@1200 1333 val toIflow = transform iflow "iflow" o toMono_opt2
adamc@1200 1334
adamc@567 1335 val jscomp = {
adamc@567 1336 func = JsComp.process,
adamc@567 1337 print = MonoPrint.p_file MonoEnv.empty
adamc@567 1338 }
adamc@567 1339
adamc@1200 1340 val toJscomp = transform jscomp "jscomp" o toIflow
adamc@567 1341
adamc@910 1342 val toMono_opt3 = transform mono_opt "mono_opt3" o toJscomp
adamc@20 1343
adamc@506 1344 val fuse = {
adamc@506 1345 func = Fuse.fuse,
adamc@506 1346 print = MonoPrint.p_file MonoEnv.empty
adamc@506 1347 }
adamc@506 1348
adamc@572 1349 val toFuse = transform fuse "fuse" o toMono_opt3
adamc@506 1350
adamc@506 1351 val toUntangle2 = transform untangle "untangle2" o toFuse
adamc@506 1352
adamc@601 1353 val toMono_reduce2 = transform mono_reduce "mono_reduce2" o toUntangle2
adamc@601 1354 val toMono_shake2 = transform mono_shake "mono_shake2" o toMono_reduce2
adamc@916 1355 val toMono_opt4 = transform mono_opt "mono_opt4" o toMono_shake2
adamc@1017 1356 val toMono_reduce3 = transform mono_reduce "mono_reduce3" o toMono_opt4
adamc@1185 1357 val toFuse2 = transform fuse "fuse2" o toMono_reduce3
adamc@916 1358 val toUntangle3 = transform untangle "untangle3" o toFuse2
adamc@916 1359 val toMono_shake3 = transform mono_shake "mono_shake3" o toUntangle3
adamc@506 1360
adamc@377 1361 val pathcheck = {
adamc@377 1362 func = (fn file => (PathCheck.check file; file)),
adamc@377 1363 print = MonoPrint.p_file MonoEnv.empty
adamc@377 1364 }
adamc@377 1365
adamc@916 1366 val toPathcheck = transform pathcheck "pathcheck" o toMono_shake3
adamc@377 1367
adam@1595 1368 val sidecheck = {
adam@1595 1369 func = SideCheck.check,
adam@1595 1370 print = MonoPrint.p_file MonoEnv.empty
adam@1595 1371 }
adam@1595 1372
adam@1595 1373 val toSidecheck = transform sidecheck "sidecheck" o toPathcheck
adam@1595 1374
adamc@202 1375 val cjrize = {
adamc@202 1376 func = Cjrize.cjrize,
adamc@202 1377 print = CjrPrint.p_file CjrEnv.empty
adamc@202 1378 }
adamc@23 1379
adam@1595 1380 val toCjrize = transform cjrize "cjrize" o toSidecheck
adamc@29 1381
adamc@643 1382 val scriptcheck = {
adamc@643 1383 func = ScriptCheck.classify,
adamc@643 1384 print = CjrPrint.p_file CjrEnv.empty
adamc@643 1385 }
adamc@643 1386
adamc@643 1387 val toScriptcheck = transform scriptcheck "scriptcheck" o toCjrize
adamc@643 1388
adamc@282 1389 val prepare = {
adamc@282 1390 func = Prepare.prepare,
adamc@282 1391 print = CjrPrint.p_file CjrEnv.empty
adamc@282 1392 }
adamc@282 1393
adamc@643 1394 val toPrepare = transform prepare "prepare" o toScriptcheck
adamc@282 1395
adamc@879 1396 val checknest = {
adamc@879 1397 func = fn f => if #supportsNestedPrepared (Settings.currentDbms ()) then f else Checknest.annotate f,
adamc@879 1398 print = CjrPrint.p_file CjrEnv.empty
adamc@879 1399 }
adamc@879 1400
adamc@879 1401 val toChecknest = transform checknest "checknest" o toPrepare
adamc@879 1402
adamc@274 1403 val sqlify = {
adamc@274 1404 func = Cjrize.cjrize,
adamc@274 1405 print = CjrPrint.p_sql CjrEnv.empty
adamc@274 1406 }
adamc@274 1407
adamc@274 1408 val toSqlify = transform sqlify "sqlify" o toMono_opt2
adamc@274 1409
adam@1473 1410 val escapeFilename = String.translate (fn #" " => "\\ " | #"\"" => "\\\"" | #"'" => "\\'" | ch => str ch)
adam@1473 1411
adam@1540 1412 val beforeC = ref (fn () => ())
adam@1540 1413
adam@1725 1414 fun compileC {cname, oname, ename, libs, profile, debug, linker, link = link'} =
adamc@183 1415 let
adamc@855 1416 val proto = Settings.currentProtocol ()
adamc@1095 1417
adam@1368 1418 val lib = if Settings.getStaticLinking () then
ezyang@1739 1419 " " ^ !Settings.configLib ^ "/" ^ #linkStatic proto ^ " " ^ !Settings.configLib ^ "/liburweb.a"
adam@1368 1420 else
ezyang@1739 1421 "-L" ^ !Settings.configLib ^ " " ^ #linkDynamic proto ^ " -lurweb"
adamc@378 1422
adam@1558 1423 val opt = if debug then
adam@1558 1424 ""
adam@1558 1425 else
adam@1558 1426 " -O3"
adam@1558 1427
adam@1558 1428 val compile = Config.ccompiler ^ " " ^ Config.ccArgs ^ " " ^ Config.pthreadCflags ^ " -Wimplicit -Werror -Wno-unused-value"
ezyang@1739 1429 ^ opt ^ " -I " ^ !Settings.configInclude
adamc@1096 1430 ^ " " ^ #compile proto
adam@1473 1431 ^ " -c " ^ escapeFilename cname ^ " -o " ^ escapeFilename oname
adamc@1096 1432
adam@1725 1433 val linker = Option.getOpt (linker, Config.ccompiler ^ " -Werror" ^ opt ^ " " ^ Config.ccArgs ^ " " ^ Config.pthreadCflags ^ " " ^ Config.pthreadLibs)
adam@1725 1434
adam@1725 1435 val link = linker
adam@1659 1436 ^ " " ^ lib ^ " " ^ escapeFilename oname ^ " " ^ libs ^ " -lm " ^ Config.openssl ^ " -o " ^ escapeFilename ename
adamc@502 1437
adamc@502 1438 val (compile, link) =
adamc@502 1439 if profile then
adamc@502 1440 (compile ^ " -pg", link ^ " -pg")
adamc@502 1441 else
adamc@502 1442 (compile, link)
adamc@742 1443
adamc@742 1444 val (compile, link) =
adamc@742 1445 if debug then
adamc@742 1446 (compile ^ " -g", link ^ " -g")
adamc@742 1447 else
adamc@742 1448 (compile, link)
adamc@764 1449
adamc@764 1450 val link = foldl (fn (s, link) => link ^ " " ^ s) link link'
adam@1394 1451
adam@1394 1452 fun system s =
adam@1394 1453 (if debug then
adam@1394 1454 print (s ^ "\n")
adam@1394 1455 else
adam@1394 1456 ();
adam@1394 1457 OS.Process.isSuccess (OS.Process.system s))
adamc@183 1458 in
adam@1540 1459 !beforeC ();
adam@1394 1460 system compile andalso system link
adamc@183 1461 end
adamc@183 1462
adamc@202 1463 fun compile job =
adamc@879 1464 case run toChecknest job of
adamc@1045 1465 NONE => false
adamc@29 1466 | SOME file =>
adamc@202 1467 let
adamc@274 1468 val job = valOf (run (transform parseUrp "parseUrp") job)
adamc@102 1469
adamc@274 1470 val (cname, oname, cleanup) =
adamc@274 1471 if #debug job then
adamc@457 1472 ("/tmp/webapp.c", "/tmp/webapp.o", fn () => ())
adamc@274 1473 else
adamc@274 1474 let
adamc@274 1475 val dir = OS.FileSys.tmpName ()
adamc@403 1476 val () = if OS.FileSys.access (dir, []) then
adamc@403 1477 OS.FileSys.remove dir
adamc@403 1478 else
adamc@403 1479 ()
adamc@457 1480 val cname = OS.Path.joinDirFile {dir = dir, file = "webapp.c"}
adamc@457 1481 val oname = OS.Path.joinDirFile {dir = dir, file = "webapp.o"}
adamc@274 1482 in
adamc@274 1483 OS.FileSys.mkDir dir;
adamc@274 1484 (cname, oname,
adam@1700 1485 fn () => (if OS.Process.isSuccess (OS.Process.system ("rm -rf " ^ dir)) then
adam@1700 1486 ()
adam@1700 1487 else
adam@1700 1488 raise Fail ("Unable to delete temporary directory " ^ dir)))
adamc@274 1489 end
adamc@274 1490 val ename = #exe job
adamc@202 1491 in
adamc@274 1492 let
adamc@274 1493 val outf = TextIO.openOut cname
adamc@274 1494 val s = TextIOPP.openOut {dst = outf, wid = 80}
adamc@432 1495
adamc@432 1496 val hasDb = List.exists (fn (Cjr.DDatabase _, _) => true | _ => false) (#1 file)
adamc@432 1497 val libs =
adamc@432 1498 if hasDb then
adamc@866 1499 #link (Settings.currentDbms ())
adamc@432 1500 else
adamc@432 1501 ""
adamc@274 1502 in
adamc@274 1503 Print.fprint s (CjrPrint.p_file CjrEnv.empty file);
adamc@415 1504 TextIO.output1 (outf, #"\n");
adamc@274 1505 TextIO.closeOut outf;
adamc@102 1506
adam@1381 1507 if ErrorMsg.anyErrors () then
adam@1381 1508 false
adam@1381 1509 else
adam@1381 1510 (case #sql job of
adam@1381 1511 NONE => ()
adam@1381 1512 | SOME sql =>
adam@1381 1513 let
adam@1381 1514 val outf = TextIO.openOut sql
adam@1381 1515 val s = TextIOPP.openOut {dst = outf, wid = 80}
adam@1381 1516 in
adam@1381 1517 Print.fprint s (CjrPrint.p_sql CjrEnv.empty file);
adam@1381 1518 TextIO.closeOut outf
adam@1381 1519 end;
adamc@274 1520
adam@1381 1521 compileC {cname = cname, oname = oname, ename = ename, libs = libs,
adam@1725 1522 profile = #profile job, debug = #debug job, linker = #linker job, link = #link job}
adam@1381 1523
adam@1381 1524 before cleanup ())
adamc@274 1525 end
adamc@274 1526 handle ex => (((cleanup ()) handle _ => ()); raise ex)
adamc@202 1527 end
adamc@29 1528
adamc@1045 1529 fun compiler job =
adamc@1045 1530 if compile job then
adamc@1045 1531 ()
adamc@1045 1532 else
adamc@1045 1533 OS.Process.exit OS.Process.failure
adamc@1045 1534
adamc@1266 1535 fun moduleOf fname =
adamc@1266 1536 let
adamc@1266 1537 val mrs = !moduleRoots
adamc@1266 1538 val fname = OS.Path.mkCanonical fname
adamc@1266 1539 in
adamc@1266 1540 case List.find (fn (root, _) => String.isPrefix (root ^ "/") fname) mrs of
adamc@1266 1541 NONE => capitalize (OS.Path.base (OS.Path.file fname))
adamc@1266 1542 | SOME (root, name) =>
adamc@1266 1543 let
adamc@1266 1544 val fname = OS.Path.base fname
adamc@1266 1545 val fname = String.extract (fname, size root + 1, NONE)
adamc@1266 1546 val fs = String.fields (fn ch => ch = #"/") fname
adamc@1266 1547 val fs = List.filter (CharVector.exists (fn ch => not (Char.isDigit ch))) fs
adamc@1266 1548 val fs = map capitalize fs
adamc@1266 1549 in
adamc@1266 1550 String.concatWith "." (name :: fs)
adamc@1266 1551 end
adamc@1266 1552 end
adamc@1266 1553
adamc@1 1554 end