ref: 76091805ade2a06dcdda8e348a085a89bc51468f
author: Ori Bernstein <ori@eigenstate.org>
date: Sun Jan 4 07:27:00 EST 2015
..And give it the right name.
--- /dev/null
+++ b/Makefile
@@ -1,0 +1,19 @@
+MYRBIN=mbld
+
+MYRSRC= \
+ build.myr \
+ clean.myr \
+ config.myr \
+ deps.myr \
+ fsel.myr \
+ install.myr \
+ main.myr \
+ opts.myr \
+ parse.myr \
+ subdir.myr \
+ util.myr \
+ types.myr
+
+include config.mk
+include mk/myr.mk
+
--- /dev/null
+++ b/bldfile
@@ -1,0 +1,22 @@
+# the mbld binary
+bin mbld =
+ build.myr
+ clean.myr
+ config.myr # config, as generated by the ./configure script
+ config+plan9-x64.myr # config, as hardcoded for Plan 9
+ deps.myr
+ fsel.myr
+ install.myr
+ main.myr
+ opts.myr
+ parse.myr
+ subdir.myr
+ types.myr
+ util.myr
+;;
+
+# we want to generate the config.myr file if we haven't configured yet.
+gen config.myr = ./configure;;
+
+man = mbld.1;;
+
--- /dev/null
+++ b/build.myr
@@ -1,0 +1,403 @@
+use std
+
+use "config.use"
+use "deps.use"
+use "opts.use"
+use "parse.use"
+use "types.use"
+use "util.use"
+use "subdir.use"
+
+pkg bld =
+ const buildall : (p : parser# -> bool)
+ const genall : (p : parser# -> bool)
+ const test : (p : parser# -> bool)
+ const build : (p : parser#, target : byte[:] -> bool)
+ const buildbin : (p : parser#, bt : myrtarg# -> void)
+ const buildlib : (p : parser#, lt : myrtarg# -> void)
+;;
+
+const buildall = {p
+ for t in p.targs
+ match t
+ | `Bin bt:
+ buildbin(p, bt)
+ | `Lib lt:
+ buildlib(p, lt)
+ | `Gen gt:
+ genfiles(p, gt)
+ | `Sub subs:
+ subdirs(p, subs, `std.None)
+ | `Man m:
+ /* nothing needed */
+ ;;
+ ;;
+ -> true
+}
+
+const genall = {p
+ for t in p.targs
+ match t
+ | `Gen gt: run(gt.cmd, "")
+ | _: /* skip */
+ ;;
+ ;;
+ /* genfiles will exit if the build fails; always return true */
+ -> true
+}
+
+const test = {p
+ std.fatal(1, "testing not yet supported\n")
+ -> false
+}
+
+const build = {p, targ
+ var found
+
+ found = false
+ for t in p.targs
+ match t
+ | `Bin bt:
+ if std.sleq(bt.name, targ)
+ buildbin(p, bt)
+ ;;
+ | `Lib lt:
+ if std.sleq(lt.name, targ)
+ buildlib(p, lt)
+ found = true
+ ;;
+ | `Gen gt:
+ for n in gt.out
+ if std.sleq(n, targ)
+ run(gt.cmd, "")
+ ;;
+ ;;
+ | `Sub subs:
+ found = true
+ subdirs(p, subs, `std.Some targ)
+ | `Man m:
+ found = true
+ /* nothing needed */
+ ;;
+ ;;
+ if !found
+ std.fatal(1, "%s: no such target\n", targ)
+ ;;
+ -> found
+}
+
+const buildbin = {p, targ
+ var dg
+
+ if targ.built
+ ->
+ ;;
+ if targ.libdeps.len > 0
+ if !hasinc(targ.incpath, ".")
+ targ.incpath = std.slpush(targ.incpath, ".")
+ ;;
+ for l in targ.libdeps
+ build(p, l)
+ ;;
+ ;;
+ std.put("%s...\n", targ.name)
+ if !myrdeps(p, &dg, targ.name, targ.inputs, targ.incpath, false)
+ std.fatal(1, "Could not load dependencies for %s\n", targ.name)
+ ;;
+ if !std.hthas(dg.deps, targ.name)
+ std.fatal(1, "no target declared for %s\n", targ.name)
+ ;;
+ if builddep(p, &dg, targ.name, targ.incpath)
+ linkbin(&dg, targ.name, targ.inputs, targ.ldscript, targ.runtime, targ.incpath, targ.libdeps)
+ ;;
+ targ.built = true
+}
+
+const hasinc = {path, t
+ for e in path
+ if std.sleq(e, t)
+ -> true
+ ;;
+ ;;
+ -> false
+}
+
+const buildlib = {p, targ
+ var archive
+ var u, l
+ var dg
+ var lib
+
+ if targ.built
+ ->
+ ;;
+ lib = targ.name
+ std.put("lib%s.a...\n", lib)
+ archive = std.fmt("lib%s.a", lib)
+ if !myrdeps(p, &dg, lib, targ.inputs, targ.incpath, true)
+ std.fatal(1, "Could not load dependencies for %s\n", lib)
+ ;;
+ if !std.hthas(dg.deps, lib)
+ std.fatal(1, "no target declared for %s\n", lib)
+ ;;
+ u = builddep(p, &dg, targ.name, targ.incpath)
+ l = builddep(p, &dg, archive, targ.incpath)
+ if u || l
+ mergeuse(&dg, lib, targ.inputs, targ.incpath)
+ archivelib(&dg, lib, targ.inputs, targ.incpath)
+ ;;
+ std.slfree(archive)
+ targ.built = true
+}
+
+const genfiles = {p, gt
+ for f in gt.out
+ if !std.fexists(f)
+ run(gt.cmd, "")
+ ->
+ ;;
+ ;;
+}
+
+const builddep = {p, dg, out, incs
+ var stale
+
+ stale = false
+ /* short circuit walking the dep tree if we've already built this. */
+ if std.htgetv(dg.updated, out, false)
+ -> false
+ ;;
+
+ match std.htget(dg.deps, out)
+ | `std.Some deps:
+ for d in deps
+ if builddep(p, dg, d, incs)
+ stale = true
+ ;;
+ if !std.fexists(d)
+ match std.htget(p.gensrc, d)
+ | `std.Some gt: run(gt.cmd, "")
+ | `std.None: std.fatal(1, "no input file %s\n", d)
+ ;;
+ ;;
+ if !isfresh(d, out)
+ stale = true
+ ;;
+ ;;
+ | `std.None:
+ ;;
+
+ match std.htget(dg.sources, out)
+ | `std.Some src:
+ if stale
+ compile(src, incs)
+ ;;
+ std.htput(dg.updated, out, true)
+ | `std.None:
+ ;;
+ -> stale
+}
+
+const compile = {src, incs
+ var o
+ var cmd
+
+ cmd = [][:]
+ if std.hassuffix(src, ".myr")
+ cmd = std.slpush(cmd, "6m")
+ for inc in incs
+ cmd = std.slpush(cmd, "-I")
+ cmd = std.slpush(cmd, inc)
+ ;;
+ if opt_genasm
+ cmd = std.slpush(cmd, "-S")
+ ;;
+ cmd = std.slpush(cmd, src)
+ run(cmd, "")
+ std.slfree(cmd)
+ elif std.hassuffix(src, ".s")
+ o = srcswapsuffix(src, config.Objsuffix)
+ for c in config.Ascmd
+ cmd = std.slpush(cmd, c)
+ ;;
+ cmd = std.slpush(cmd,"-o")
+ cmd = std.slpush(cmd, o)
+ cmd = std.slpush(cmd, src)
+ run(cmd, "")
+ std.slfree(o)
+ else
+ std.fatal(1, "Unknown file type for %s\n", src)
+ ;;
+}
+
+const linkbin = {dg, bin, srcfiles, ldscript, rt, incs, extralibs
+ var cmd
+
+ cmd = [][:]
+
+ /* ld -o bin */
+ for c in config.Linkcmd
+ cmd = std.slpush(cmd, std.sldup(c))
+ ;;
+ cmd = std.slpush(cmd, std.sldup(bin))
+
+ /* [-T script] */
+ if ldscript.len > 0
+ cmd = std.slpush(cmd, std.sldup("-T"))
+ cmd = std.slpush(cmd, std.sldup(ldscript))
+ ;;
+
+ if rt.len != 0
+ cmd = std.slpush(cmd, std.sldup(rt))
+ else
+ cmd = std.slpush(cmd, std.sldup(opt_runtime))
+ ;;
+
+ /* input.o list.o... */
+ for f in srcfiles
+ cmd = std.slpush(cmd, srcswapsuffix(f, config.Objsuffix))
+ ;;
+
+ /* -L path -l lib... */
+ cmd = addlibs(cmd, dg.libs, incs)
+ for l in extralibs
+ cmd = std.slpush(cmd, std.fmt("-l%s", l))
+ ;;
+
+
+ /* special for OSX: it warns if we don't add this */
+ if std.sleq(opt_sys, "osx")
+ cmd = std.slpush(cmd, std.sldup("-macosx_version_min"))
+ cmd = std.slpush(cmd, std.sldup("10.6"))
+ ;;
+
+ run(cmd, "")
+ strlistfree(cmd)
+}
+
+const archivelib = {dg, lib, files, incs
+ var cmd
+ var obj
+
+ cmd = [][:]
+ for c in config.Arcmd
+ cmd = std.slpush(cmd, std.sldup(c))
+ ;;
+ cmd = std.slpush(cmd, std.fmt("lib%s.a", lib))
+ for f in files
+ obj = srcswapsuffix(f, config.Objsuffix)
+ cmd = std.slpush(cmd, obj)
+ ;;
+ run(cmd, "")
+ strlistfree(cmd)
+}
+
+const mergeuse = {dg, lib, files, incs
+ var cmd
+
+ cmd = [][:]
+ cmd = std.slpush(cmd, std.sldup(opt_muse))
+ cmd = std.slpush(cmd, std.sldup("-o"))
+ cmd = std.slpush(cmd, std.sldup(lib))
+ for f in files
+ if std.hassuffix(f, ".myr")
+ cmd = std.slpush(cmd, srcswapsuffix(f, ".use"))
+ elif !std.hassuffix(f, ".s")
+ std.fatal(1, "unknown file type for %s\n", f)
+ ;;
+ ;;
+ run(cmd, "")
+ strlistfree(cmd)
+}
+
+const addlibs = {cmd, libgraph, incs
+ var looped : std.htab(byte[:], bool)#
+ var marked : std.htab(byte[:], bool)#
+ var libs
+ var head
+
+ /* -L incpath... */
+ if !config.Directlib
+ for inc in incs
+ cmd = std.slpush(cmd, std.fmt("-L%s", inc))
+ ;;
+ cmd = std.slpush(cmd, std.fmt("-L%s%s", opt_instroot, "/lib/myr"))
+ ;;
+
+ libs = std.htkeys(libgraph)
+ looped = std.mkht(std.strhash, std.streq)
+ marked = std.mkht(std.strhash, std.streq)
+ head = cmd.len
+
+ for lib in libs
+ cmd = visit(cmd, head, libgraph, lib, looped, marked, incs)
+ ;;
+
+ -> cmd
+}
+
+const visit = {cmd, head, g, lib, looped, marked, incs
+ if std.hthas(looped, lib)
+ std.fatal(1, "cycle in library graph involving \"%s\"\n", lib)
+ elif std.hthas(marked, lib)
+ -> cmd
+ ;;
+
+ std.htput(looped, lib, true)
+ for dep in std.htgetv(g, lib, [][:])
+ cmd = visit(cmd, head, g, dep, looped, marked, incs)
+ ;;
+ std.htdel(looped, lib)
+ std.htput(marked, lib, true)
+ -> putlib(cmd, head, lib, incs)
+}
+
+const putlib = {cmd, head, lib, incs
+ var buf : byte[512]
+ var n, p
+
+ if !config.Directlib
+ -> std.slput(cmd, head, std.fmt("-l%s", lib))
+ ;;
+
+ n = std.bfmt(buf[:], "lib%s.a", lib)
+ for i in incs
+ p = std.pathjoin([i, "lib/myr", buf[:n]][:])
+ if std.fexists(p)
+ -> std.slput(cmd, head, p)
+ ;;
+ std.slfree(p)
+ ;;
+ p = std.pathjoin([opt_instroot, "lib/myr", buf[:n]][:])
+ if std.fexists(p)
+ -> std.slput(cmd, head, p)
+ ;;
+ std.fatal(1, "could not find library lib%s.a", lib)
+}
+
+
+
+
+const isfresh = {src, dst
+ var srcmt, dstmt
+
+ /*
+ OSX only has single second resolution on modification
+ times. Since most builds happen within one second of each
+ other, if we treat equal times as outdated, we do a lot of
+ spurious rebuilding.
+
+ So, we treat times where both secs and nsecs are equal as
+ up to date.
+ */
+ match std.fmtime(src)
+ | `std.Some mt: srcmt = mt
+ | `std.None: std.fatal(1, "could not stat %s\n", src)
+ ;;
+ match std.fmtime(dst)
+ | `std.Some mt: dstmt = mt
+ | `std.None: -> false
+ ;;
+ -> srcmt <= dstmt
+}
+
--- /dev/null
+++ b/clean.myr
@@ -1,0 +1,78 @@
+use std
+
+use "config.use"
+use "deps.use"
+use "opts.use"
+use "parse.use"
+use "subdir.use"
+use "types.use"
+
+pkg bld =
+ const cleanall : (p : parser# -> bool)
+ const clean : (p : parser#, targ : byte[:] -> bool)
+;;
+
+const cleanall = {p
+ for t in p.targs
+ match t
+ | `Bin bt:
+ cleanup(p, bt, bt.inputs, true)
+ | `Lib lt:
+ cleanup(p, lt, lt.inputs, true)
+ | `Gen gt:
+ for f in gt.out
+ if std.remove(f)
+ std.put("\tclean %s\n", f)
+ ;;
+ ;;
+ | `Sub subs:
+ subdirs(p, subs, `std.None)
+ | `Man m:
+ ;;
+ ;;
+ -> true
+}
+
+const clean = {p, targ
+ for t in p.targs
+ match t
+ | `Bin bt:
+ if std.sleq(bt.name, targ)
+ cleanup(p, bt, bt.inputs, true)
+ ;;
+ | `Lib lt:
+ if std.sleq(lt.name, targ)
+ cleanup(p, lt, lt.inputs, true)
+ ;;
+ | `Gen gt:
+ | `Sub subs:
+ subdirs(p, subs, `std.Some targ)
+ | `Man m:
+ ;;
+ ;;
+ -> true
+}
+
+const cleanup = {p, targ, leaves, islib
+ var mchammer_files /* cant touch this */
+ var keys
+ var dg
+
+ if !myrdeps(p, &dg, targ.name, leaves, targ.incpath, islib)
+ std.fatal(1, "Could not load dependencies for %s\n", targ.name)
+ ;;
+ mchammer_files = std.mkht(std.strhash, std.streq)
+ for l in leaves
+ if !std.hthas(p.gensrc, l)
+ std.htput(mchammer_files, l, true)
+ ;;
+ ;;
+
+ keys = std.htkeys(dg.deps)
+ for k in keys
+ if !std.htgetv(mchammer_files, k, false) && std.remove(k)
+ std.put("\tclean %s\n", k)
+ ;;
+ ;;
+}
+
--- /dev/null
+++ b/config+plan9-x64.myr
@@ -1,0 +1,11 @@
+pkg config =
+ const Instroot = "/amd64"
+ const Sys = "Plan9"
+ const Objsuffix = ".6"
+ const Linkcmd = ["6l", "-lo"]
+ const Arcmd = ["ar", "vu"]
+ const Ascmd = ["6a"]
+ const Directlib = true
+ const Runtime = "_myrrt.6"
+ const Manpath = "man/"
+;;
--- /dev/null
+++ b/configure
@@ -1,0 +1,65 @@
+#!/bin/sh
+
+prefix="/usr/local"
+
+for i in `seq 300`; do
+ echo "Lots of output to emulate automake... ok"
+ echo "Testing for things you'll never use... fail"
+ echo "Satisfying the fortran77 lobby... ok"
+ echo "Burning CPU time checking for the bloody obvious... ok"
+done
+echo "Automake emulated successfully"
+
+INST_ROOT='/usr/local'
+
+for arg in $*; do
+ shift 1
+ case $arg in
+ "--prefix" | "-p")
+ prefix=shift $*
+ ;;
+ --prefix=*)
+ prefix=`echo $arg | sed 's/^--prefix=//g'`
+ ;;
+ "--help" | "-h")
+ echo "Usage:"
+ echo " --prefix | -p: The prefix to install to"
+ break;
+ ;;
+ *) echo "Unrecognized argument $arg";;
+ esac
+done
+
+OS=`uname`
+
+echo export INST_ROOT=$prefix > config.mk
+
+echo "pkg config = " > config.myr
+echo "const Instroot = \"$prefix\"" >> config.myr
+echo "const Objsuffix = \".o\"" >> config.myr
+echo "const Linkcmd = [\"ld\", \"-o\"]" >> config.myr
+echo "const Arcmd = [\"ar\", \"-rcs\"]" >> config.myr
+echo "const Ascmd = [\"as\", \"-g\"]" >> config.myr
+echo "const Directlib = false" >> config.myr
+echo "const Runtime = \"_myrrt.o\"" >> config.myr
+echo "const Manpath = \"share/man/man\"" >> config.myr
+case $OS in
+ *Linux*)
+ echo 'export SYS=linux' >> config.mk
+ echo 'const Sys = "Linux"' >> config.myr
+ ;;
+ *Darwin*)
+ echo 'export SYS=osx' >> config.mk
+ echo 'const Sys = "OSX"' >> config.myr
+ ;;
+ *)
+ echo 'Unknown architecture.'
+ ;;
+esac
+echo ";;" >> config.myr
+
+cat << EOF
+ Building with:
+ prefix=$prefix
+EOF
+
--- /dev/null
+++ b/deps.myr
@@ -1,0 +1,278 @@
+use std
+use regex
+use bio
+
+use "config.use"
+use "opts.use"
+use "types.use"
+use "util.use"
+
+pkg bld =
+ const myrdeps : (p : parser#, dg : depgraph#, targ : byte[:], srcs : byte[:][:], incs : byte[:][:], islib : bool -> bool)
+
+
+ /* a bit ugly: initialized from main() */
+ var usepat : regex.regex#
+
+;;
+var usepat : regex.regex#
+
+type dep = union
+ `Local byte[:]
+ `Lib byte[:]
+;;
+
+const myrdeps = {p, dg, targ, srcs, incs, islib
+ var seentab, donetab
+ var out, useout
+ var objs, uses
+ var i
+
+ dg.deps = std.mkht(std.strhash, std.streq)
+ dg.libs = std.mkht(std.strhash, std.streq)
+ dg.sources = std.mkht(std.strhash, std.streq)
+ dg.updated = std.mkht(std.strhash, std.streq)
+ seentab = std.mkht(std.strhash, std.streq)
+ donetab = std.mkht(std.strhash, std.streq)
+ /* direct dependencies of binary */
+ if islib
+ out = std.fmt("lib%s.a", targ)
+ useout = std.sldup(targ)
+ else
+ out = std.sldup(targ)
+ useout = ""
+ ;;
+ objs = swapall(srcs, config.Objsuffix)
+ uses = swapall(srcs, ".use")
+ for i = 0; i < srcs.len; i++
+ std.htput(dg.sources, objs[i], srcs[i])
+ pushdep(dg, srcs[i], objs[i])
+ if std.hassuffix(srcs[i], ".myr")
+ std.htput(dg.sources, uses[i], srcs[i])
+ pushdep(dg, srcs[i], uses[i])
+ ;;
+ ;;
+ for i = 0; i < srcs.len; i++
+ pushdep(dg, objs[i], out)
+ if islib && std.hassuffix(srcs[i], ".myr")
+ pushdep(dg, uses[i], useout)
+ ;;
+ ;;
+ for i = 0; i < srcs.len; i++
+ srcdeps(p, dg, seentab, donetab, srcs[i], objs[i], uses[i], incs)
+ ;;
+ dumpgraph(dg)
+ std.htfree(seentab)
+ std.htfree(donetab)
+ -> true
+}
+
+const swapall = {srcs, suff
+ var sl
+
+ sl = [][:]
+ for s in srcs
+ sl = std.slpush(sl, srcswapsuffix(s, suff))
+ ;;
+ -> sl
+}
+
+const dumpgraph = {dg
+ var keys
+
+ if !opt_debug
+ ->
+ ;;
+ keys = std.htkeys(dg.deps)
+ std.put("digraph dg {\n")
+ for k in keys
+ for v in std.htgetv(dg.deps, k, ["WTFUNKNOWN!"][:])
+ std.put("\t\"%s\" -> \"%s\";\n", k, v)
+ ;;
+ ;;
+ std.put("}\n")
+}
+
+const srcdeps = {p, g, seen, done, path, obj, usefile, incs
+ var deps
+
+ if std.hthas(done, path)
+ ->
+ elif std.htgetv(seen, path, false)
+ std.fput(1, "dependency loop involving %s\n", path)
+ std.exit(1)
+ ;;
+ deps = getdeps(p, path)
+ std.htput(seen, path, true)
+ for d in deps
+ match d
+ | `Lib lib:
+ scrapelibs(g, lib, incs)
+ | `Local l:
+ if !std.hassuffix(l, ".use")
+ std.fatal(1, "usefile dependency \"%s\" of \"%s\" is not a usefile\n", l, path)
+ ;;
+ if obj.len != 0
+ pushdep(g, l, obj)
+ ;;
+ if usefile.len != 0
+ pushdep(g, l, usefile)
+ ;;
+ addusedep(p, g, seen, done, l, incs)
+ ;;
+ ;;
+ std.htput(seen, path, false)
+ std.htput(done, path, true)
+}
+
+const addusedep = {p, g, seen, done, usefile, incs
+ var src
+
+ if std.hthas(done, usefile)
+ if opt_debug
+ std.put("already loaded deps for %s\n", usefile)
+ ;;
+ ->
+ ;;
+ match std.htget(g.sources, usefile)
+ | `std.Some path:
+ src = std.sldup(path)
+ | `std.None:
+ src = swapsuffix(usefile, ".use", ".myr")
+ ;;
+ pushdep(g, src, usefile)
+ std.htput(g.sources, usefile, src)
+ srcdeps(p, g, seen, done, src, "", usefile, incs)
+ std.htput(done, usefile, true)
+}
+
+const getdeps = {p, path
+ var f
+ var deps : dep[:]
+
+ deps = [][:]
+ if !std.fexists(path)
+ match std.htget(p.gensrc, path)
+ | `std.Some gt: run(gt.cmd, "")
+ | `std.None: std.fatal(1, "no input file %s\n", path)
+ ;;
+ ;;
+ match bio.open(path, bio.Rd)
+ | `std.Some fd: f = fd
+ | `std.None: std.fatal(1, "could not open %s\n", path)
+ ;;
+
+ while true
+ match bio.readln(f)
+ | `std.Some ln:
+ deps = depname(deps, ln)
+ std.slfree(ln)
+ | `std.None:
+ bio.close(f)
+ -> deps
+ ;;
+ ;;
+}
+
+const scrapelibs = {dg, lib, incs
+ var deps, d
+ var f
+ var done
+
+ if std.hthas(dg.libs, lib)
+ ->
+ ;;
+
+ deps = [][:]
+ f = openlib(lib, incs)
+ match bio.getc(f)
+ | `std.Some 'U': /* nothing */
+ | `std.Some _: std.fatal(1, "library %s is not usefile\n", lib)
+ | `std.None: std.fatal(1, "library %s is not usefile\n", lib)
+ ;;
+ std.slfree(rdstr(f))
+ done = false
+ while !done
+ match bio.getc(f)
+ | `std.Some 'L':
+ d = rdstr(f)
+ deps = std.slpush(deps, d)
+ | `std.Some _: done = true
+ | `std.None: done = true
+ ;;
+ ;;
+ bio.close(f)
+ std.htput(dg.libs, lib, deps)
+ for dep in deps
+ scrapelibs(dg, dep, incs)
+ ;;
+}
+
+const openlib = {lib, incs
+ var path
+
+ for p in incs
+ path = std.pathjoin([p, lib][:])
+ match bio.open(path, bio.Rd)
+ | `std.Some file:
+ -> file
+ | `std.None:
+ /* nothing */
+ ;;
+ ;;
+ path = std.pathjoin([opt_instroot, "/lib/myr", lib][:])
+ match bio.open(path, bio.Rd)
+ | `std.Some file:
+ -> file
+ | `std.None:
+ /* nothing */
+ ;;
+ std.fatal(1, "could not find library %s.\n", lib)
+}
+
+const depname = {deps, ln
+ /*
+ the regex pattern does some contortions to either grab
+ an unquoted path and put it into uses[4], or a quoted
+ path, and put it (minus the quotes) into uses[2]
+ */
+ match regex.exec(usepat, ln)
+ | `std.Some uses:
+ if uses[2].len > 0
+ deps = std.slpush(deps, `Local std.sldup(uses[2]))
+ else
+ deps = std.slpush(deps, `Lib std.sldup(uses[4]))
+ ;;
+ | `std.None:
+ /* nothing to do */
+ ;;
+ -> deps
+}
+
+
+/* pushes a dep into the dependency list */
+const pushdep = {dg, src, dst
+ var sl
+
+ if opt_debug
+ std.put("%s <= %s\n", dst, src)
+ ;;
+ std.assert(dst.len < 200, "BUG!")
+ sl = std.htgetv(dg.deps, dst, [][:])
+ sl = std.slpush(sl, src)
+ std.htput(dg.deps, dst, sl)
+}
+
+const rdstr = {f
+ var len : uint32
+ var sl
+
+ match bio.getbe(f)
+ | `std.Some l:
+ len = l
+ sl = std.slalloc(len castto(std.size))
+ | `std.None: std.die("string length not readable")
+ ;;
+ bio.read(f, sl)
+ -> sl
+}
--- /dev/null
+++ b/fsel.myr
@@ -1,0 +1,104 @@
+use std
+
+use "opts.use"
+
+pkg bld =
+ type fsel = struct
+ filematch : std.htab(byte[:], int)#
+ filebest : std.htab(byte[:], byte[:])#
+ sysattrs : std.htab(byte[:], bool)#
+ ;;
+
+ const mkfsel : (-> fsel#)
+ const fseladd : (fsel : fsel#, file : byte[:] -> void)
+ const fselfin : (fsel : fsel# -> byte[:][:])
+;;
+
+const mkfsel = {
+ var fsel
+
+ fsel = std.alloc()
+ fsel.filematch = std.mkht(std.strhash, std.streq)
+ fsel.filebest = std.mkht(std.strhash, std.streq)
+ fsel.sysattrs = std.mkht(std.strhash, std.streq)
+ addsysattrs(fsel.sysattrs)
+ -> fsel
+}
+
+const fseladd = {fsel, f
+ var basename, attrs
+ var nmatch, curbest
+ var attrlist
+
+ match std.strfind(f, "+")
+ | `std.Some i:
+ basename = f[:i]
+ match std.strrfind(f[i+1:], ".")
+ | `std.Some j: attrs = f[i+1:][:j]
+ | `std.None: std.fatal(1, "unrecognized type for file %s\n", f)
+ ;;
+ | `std.None:
+ match std.strrfind(f, ".")
+ | `std.None: std.fatal(1, "unrecognized type for file %s\n", f)
+ | `std.Some i:
+ basename = f[:i]
+ attrs = ""
+ ;;
+ ;;
+
+ nmatch = 0
+ attrlist = std.strsplit(attrs, "-")
+ for a in attrlist
+ if std.hthas(fsel.sysattrs, a)
+ nmatch++
+ else
+ nmatch = -1
+ break
+ ;;
+ ;;
+ std.slfree(attrlist)
+ curbest = std.htgetv(fsel.filematch, basename, -1)
+ if curbest < nmatch
+ std.htput(fsel.filematch, basename, nmatch)
+ std.htput(fsel.filebest, basename, f)
+ ;;
+}
+
+const fselfin = {fsel
+ var keys, nmatch, ret
+
+ keys = std.htkeys(fsel.filematch)
+ ret = [][:]
+ for k in keys
+ nmatch = std.htgetv(fsel.filematch, k, -1)
+ if nmatch == -1
+ std.fatal(1, "no applicable file for '%s'\n", k)
+ ;;
+ ret = std.slpush(ret, std.htgetv(fsel.filebest, k, ""))
+ ;;
+ std.htfree(fsel.filematch)
+ std.htfree(fsel.filebest)
+ std.htfree(fsel.sysattrs)
+ -> ret
+}
+
+const addsysattrs = {sa
+ var attrs
+ match opt_sys
+ | "freebsd": attrs = ["freebsd", "posixy"][:]
+ | "linux": attrs = ["linux", "posixy"][:]
+ | "plan9": attrs = ["plan9"][:]
+ | unknown: std.fatal(1, "unknown system %s\n", unknown)
+ ;;
+ for a in attrs
+ std.htput(sa, a, true)
+ ;;
+
+ match opt_arch
+ | "x64": attrs = ["x64"][:]
+ | unknown: std.fatal(1, "unknown arch %s\n", unknown)
+ ;;
+ for a in attrs
+ std.htput(sa, a, true)
+ ;;
+}
--- /dev/null
+++ b/install.myr
@@ -1,0 +1,91 @@
+use std
+
+use "config.use"
+use "deps.use"
+use "opts.use"
+use "parse.use"
+use "subdir.use"
+use "types.use"
+use "util.use"
+
+pkg bld =
+ const install : (p : parser# -> bool)
+ const uninstall : (p : parser# -> bool)
+;;
+
+const install = {p
+ -> movetargs(p, false)
+}
+
+const uninstall = {p
+ -> movetargs(p, true)
+}
+
+const movetargs = {p, delete
+ var libarchive
+
+ for t in p.targs
+ match t
+ | `Bin bt:
+ movefile(delete, bt.name, opt_instroot, opt_destdir, "bin")
+ | `Lib lt:
+ movefile(delete, lt.name, opt_instroot, opt_destdir, "lib/myr")
+ libarchive = std.fmt("lib%s.a", lt.name)
+ movefile(delete, libarchive, opt_instroot, opt_destdir, "lib/myr")
+ std.slfree(libarchive)
+ | `Gen gt:
+ /* nothing to do (?) */
+ | `Sub subs:
+ subdirs(p, subs, `std.None)
+ | `Man mans:
+ /* FIXME: figure out man section by number */
+ for m in mans
+ moveman(delete, m)
+ ;;
+ ;;
+ ;;
+ -> true
+}
+
+
+const movefile = {delete, file, instdir, destdir, prefix
+ var path
+
+ path = std.pathjoin([destdir, instdir, prefix, file][:])
+ if delete
+ std.put("\t%s deleted\n", path)
+ if !std.remove(path)
+ std.put("\t\tno such file %s\n", file)
+ ;;
+ else
+ std.put("\t%s => %s\n", file, path)
+ std.remove(path)
+ match std.slurp(file)
+ | `std.Fail m: std.fatal(1, "Could not open %s for reading\n", file)
+ | `std.Ok buf:
+ if !std.blat(path, buf)
+ std.put("Could not write %s\n", file)
+ ;;
+ std.slfree(buf)
+ ;;
+ ;;
+ std.slfree(path)
+}
+
+const moveman = {delete, man
+ var sect, manrel
+
+ match std.strrfind(man, ".")
+ | `std.None:
+ std.fatal(1, "manpage %s has no section\n", man)
+ | `std.Some s:
+ sect = s + 1
+ if s + 1 == man.len
+ std.fatal(1, "manpage %s missing suffix\n", man)
+ ;;
+ ;;
+
+ manrel = std.fmt("%s%s", opt_manpath, man[sect:])
+ movefile(delete, man, opt_instroot, opt_destdir, manrel)
+ std.slfree(manrel)
+}
--- /dev/null
+++ b/main.myr
@@ -1,0 +1,121 @@
+use std
+use regex
+
+use "build.use"
+use "clean.use"
+use "config.use"
+use "deps.use"
+use "install.use"
+use "opts.use"
+use "parse.use"
+use "types.use"
+
+const main = {args : byte[:][:]
+ var p : bld.parser#
+ var mt : bld.myrtarg
+ var targname
+ var bintarg
+ var optctx
+
+ optctx = std.optinit("hb:l:s:Sr:I:C:A:M:L:R:d", args)
+ bld.initopts()
+ while !std.optdone(optctx)
+ match std.optnext(optctx)
+ | ('h', arg): usage(args[0])
+ | ('s', arg): bld.opt_ldscript = arg
+ | ('f', arg): bld.opt_bldfile = arg
+ | ('I', arg): bld.opt_incpaths = std.slpush(bld.opt_incpaths, arg)
+ | ('S', _): bld.opt_genasm = true
+ | ('R', arg): bld.opt_instroot = arg
+ | ('b', arg):
+ targname = arg
+ bintarg = true
+ | ('l', arg):
+ targname = arg
+ bintarg = false
+ | ('r', arg):
+ if std.sleq(arg, "none")
+ bld.opt_runtime = ""
+ else
+ bld.opt_runtime = arg
+ ;;
+ /*
+ internal undocumented args; used by compiler suite for
+ building with an uninstalled compiler.
+ */
+ | ('d', arg): bld.opt_debug = true
+ | ('C', arg): bld.opt_mc = arg
+ | ('M', arg): bld.opt_muse = arg
+ | _: std.die("got invalid arg\n")
+ ;;
+ ;;
+
+ match regex.compile("^\\s*use\\s+((\\<\\S+\\>)|(\"(\\S+)\")).*")
+ | `std.Ok re: bld.usepat = re
+ | `std.Fail f: std.fatal(1, "Failed to compile use pattern regex\n")
+ ;;
+
+ if targname.len != 0
+ mt = [
+ .name=targname,
+ .inputs=optctx.args,
+ .runtime=bld.opt_runtime,
+ .incpath=bld.opt_incpaths,
+ .ldscript=bld.opt_ldscript,
+ .libdeps=[][:]
+ ]
+ p = std.zalloc()
+ if bintarg
+ bld.buildbin(p, &mt)
+ else
+ bld.buildlib(p, &mt)
+ ;;
+ std.free(p)
+ else
+ p = loadbuild(bld.opt_bldfile)
+ p.cmd = args
+ /*bld.configure()*/
+ /* default: buildall */
+ if optctx.args.len == 0
+ bld.buildall(p)
+ else
+ for cmd in optctx.args
+ match cmd
+ | "all": bld.buildall(p)
+ | "gen": bld.genall(p)
+ | "clean": bld.cleanall(p)
+ | "install": bld.install(p)
+ | "uninstall": bld.uninstall(p)
+ | "test": bld.test(p)
+ | target: bld.build(p, target)
+ ;;
+ ;;
+ ;;
+ ;;
+}
+
+const loadbuild = {path
+ var p
+
+ p = std.zalloc()
+ p.line = 1
+ p.fname = path
+ match std.slurp(path)
+ | `std.Ok d: p.data = d
+ | `std.Fail _: std.fatal(1, "could not open file 'bldfile'\n")
+ ;;
+ p.rest = p.data
+ p.gensrc = std.mkht(std.strhash, std.streq)
+ bld.parse(p)
+
+ -> p
+}
+const usage = {prog
+ std.put("%s [-h] [-I path] [-l lib] [-b bin] inputs...\n", prog)
+ std.put("\t-h\tprint this help\n")
+ std.put("\t-b bin\tBuild a binary called 'bin'\n")
+ std.put("\t-l lib\tBuild a library called 'name'\n")
+ std.put("\t-s script\tUse the linker script 'script' when linking\n")
+ std.put("\t-I path\tAdd 'path' to use search path\n")
+ std.exit(0)
+}
--- /dev/null
+++ b/mbld.1
@@ -1,0 +1,81 @@
+.TH MBLD 1
+.SH NAME
+mbld
+.SH SYNOPSIS
+.B mbld
+.I [all | clean | install | uninstall | test]
+.I -[hblIsfrR]
+.I [file...]
+.br
+.SH DESCRIPTION
+.PP
+The 'mbld' tool takes as input a list of Myrddin or assembly sources,
+and compiles them in the correct dependency order into either a library or
+an executable.
+
+.PP
+By default, it reads from an input file called 'bldfile', but if given the
+option '-b' or '-l', it will build a binary or library, respectively, from
+the arguments specified on the command lines.
+
+.PP
+Myrbuild will default to building for the current architecture.
+
+.PP
+The myrbuild options are:
+
+.TP
+.B -h
+Print a summary of the available options.
+
+.TP
+.B -b name
+Compile source into a binary named 'name'. If neither this option nor
+the '-l' option are given, myrbuild will create a binary called 'a.out'.
+
+.TP
+.B -l 'name'
+Compile source given into a library called 'lib<name>.a', and a matching
+usefile called 'name'. Only static libraries are currently supported.
+
+.TP
+.B -s 'script'
+Pass the linker script 'script' to the linker. If this option is not
+provided, no script is passed to the linker.
+
+.TP
+.B -r 'rt'
+Compile a binary using the runtime 'rt'. If the runtime name given
+is 'none', then no runtime will be linked. If this option is not provided,
+then the default runtime in '$INSTALL_ROOT/myr/lib/_myrrt.o' will be
+used.
+
+.TP
+.B -I path
+Add 'path' to the search path for unquoted use statments. This option
+does not affect the search path for local usefiles, which are always
+searched relative to the compiler's current working directory. Without
+any options, the search path defaults to /usr/include/myr.
+
+.SH EXAMPLE
+.EX
+ mbld
+ mbld -l foo bar.myr baz.myr
+.EE
+
+.SH FILES
+The source for muse is available from
+.B git://git.eigenstate.org/git/ori/mc.git
+and lives in the
+.I myrbuild/
+directory within the source tree.
+
+.SH SEE ALSO
+.IR mc(1)
+.IR muse(1)
+.IR ld(1)
+.IR as(1)
+
+.SH BUGS
+.PP
+None known.
--- /dev/null
+++ b/mk/myr.mk
@@ -1,0 +1,42 @@
+ifneq ($(MYRLIB),)
+ _LIBNAME=lib$(MYRLIB).a
+endif
+
+all: $(_LIBNAME) $(MYRBIN)
+
+$(_LIBNAME): $(MYRSRC) $(ASMSRC)
+ myrbuild -l $(MYRLIB) $^
+
+$(MYRBIN): $(MYRSRC) $(ASMSRC)
+ myrbuild -b $(MYRBIN) $^
+
+OBJ=$(MYRSRC:.myr=.o) $(ASMSRC:.s=.o)
+JUNKASM=$(MYRSRC:.myr=.s)
+USE=$(MYRSRC:.myr=.use) $(MYRLIB)
+.PHONY: clean install install-bin install-lib
+clean:
+ rm -f $(OBJ)
+ rm -f $(USE)
+ rm -f $(JUNKASM) $(CLEANEXTRA)
+ rm -f $(_LIBNAME) $(MYRBIN)
+
+install: install-bin install-lib
+
+install-bin: $(MYRBIN)
+ @if [ ! -z "$(MYRBIN)" ]; then \
+ echo install $(MYRBIN) $(INST_ROOT)/bin; \
+ mkdir -p $(INST_ROOT)/bin; \
+ install $(MYRBIN) $(INST_ROOT)/bin; \
+ fi
+
+install-lib: $(_LIBNAME)
+ @if [ ! -z "$(_LIBNAME)" ]; then \
+ echo install -m 644 $(_LIBNAME) $(INST_ROOT)/lib/myr; \
+ echo install -m 644 $(MYRLIB) $(INST_ROOT)/lib/myr; \
+ mkdir -p $(INST_ROOT)/lib/myr; \
+ install -m 644 $(_LIBNAME) $(INST_ROOT)/lib/myr; \
+ install -m 644 $(MYRLIB) $(INST_ROOT)/lib/myr; \
+ fi
+
+config.mk:
+ ./configure
--- /dev/null
+++ b/opts.myr
@@ -1,0 +1,84 @@
+use std
+
+use "config.use"
+
+pkg bld =
+ var opt_arch : byte[:]
+ var opt_sys : byte[:]
+ var opt_runtime : byte[:]
+ var opt_genasm : bool
+ var opt_ldscript : byte[:]
+ var opt_incpaths : byte[:][:]
+ var opt_instroot : byte[:]
+ var opt_manpath : byte[:]
+ var opt_destdir : byte[:]
+ var opt_bldfile : byte[:]
+ var opt_debug : bool
+
+ /* undocumented/unsupported opts */
+ var opt_mc : byte[:]
+ var opt_muse : byte[:]
+
+ var sysarchstr : byte[:]
+ var archstr : byte[:]
+ var sysstr : byte[:]
+
+ const initopts : (-> void)
+;;
+
+var opt_arch = ""
+var opt_sys = ""
+var opt_binname = ""
+var opt_libname = ""
+var opt_runtime = ""
+var opt_ldscript = ""
+var opt_incpaths /* FIXME: taking a constant slice is a nonconstant initializer */
+var opt_instroot = ""
+var opt_manpath = ""
+var opt_destdir = ""
+var opt_debug = false
+var opt_bldfile = "bldfile"
+var opt_mc = "6m"
+var opt_as = "as"
+var opt_muse = "muse"
+var opt_ld = "ld"
+var opt_ar = "ar"
+var opt_genasm = false
+
+/* derived */
+var sysarchstr = ""
+var archstr = ""
+var sysstr = ""
+
+const initopts = {
+ var si
+
+ std.getsysinfo(&si)
+ match si.system
+ | "Linux": opt_sys = "linux"
+ | "Darwin": opt_sys = "osx"
+ | "FreeBSD": opt_sys = "freebsd"
+ | "Plan9": opt_sys = "plan9"
+ | unknown: std.fatal(1, "unknown system \"%s\"\n", unknown)
+ ;;
+
+ match si.arch
+ | "x86_64": opt_arch = "x64"
+ | "amd64": opt_arch = "x64"
+ | unknown: std.fatal(1, "unknown architecture \"%s\"\n", unknown)
+ ;;
+
+ opt_incpaths = [][:]
+ opt_instroot = config.Instroot
+ opt_manpath = config.Manpath
+ opt_destdir = std.getenvv("DESTDIR", "")
+ opt_mc = std.getenvv("MYR_MC", "6m")
+ opt_muse = std.getenvv("MYR_MUSE", "muse")
+
+ sysarchstr = std.fmt("+%s-%s", opt_sys, opt_arch)
+ sysstr = std.fmt("+%s", opt_sys)
+ archstr = std.fmt("+%s", opt_arch)
+
+ opt_runtime = std.pathjoin([opt_instroot, "lib/myr", config.Runtime][:])
+}
+
--- /dev/null
+++ b/parse.myr
@@ -1,0 +1,390 @@
+use std
+
+use "types.use"
+use "util.use"
+use "opts.use"
+use "fsel.use"
+
+pkg bld =
+ const parse : (p : parser# -> bool)
+;;
+
+const failparse = {p : parser#, msg, args : ...
+ var buf : byte[1024]
+ var ap
+ var n
+
+ ap = std.vastart(&args)
+ n = std.bfmtv(buf[:], msg, ap)
+ std.fput(1, "%s:%i: %s", p.fname, p.line, buf[:n])
+ std.exit(1)
+}
+
+const parse = {p
+ while true
+ skipspace(p)
+ if !target(p)
+ break
+ ;;
+ ;;
+ skipspace(p)
+ if p.rest.len > 0
+ failparse(p, "junk in file near %s\n", p.rest[:std.min(p.rest.len, 10)])
+ -> false
+ else
+ -> true
+ ;;
+}
+
+const target = {p : parser#
+ match word(p)
+ | `std.Some "bin": bintarget(p)
+ | `std.Some "lib": libtarget(p)
+ | `std.Some "gen": gentarget(p)
+ | `std.Some "sub": subtarget(p)
+ | `std.Some "man": mantarget(p)
+ | `std.Some targtype: failparse(p, "unknown targtype type %s\n", targtype)
+ | `std.None: -> false
+ ;;
+ -> true
+}
+
+/* bintarget: myrtarget */
+const bintarget = {p
+ p.targs = std.slpush(p.targs, `Bin myrtarget(p, "bin"))
+}
+
+/* libtarget: myrtarget */
+const libtarget = {p
+ p.targs = std.slpush(p.targs, `Lib myrtarget(p, "lib"))
+}
+
+/* subtarget : anontarget */
+const subtarget = {p
+ p.targs = std.slpush(p.targs, `Sub anontarget(p, "sub"))
+}
+
+/* mantarget: anontarget */
+const mantarget = {p
+ p.targs = std.slpush(p.targs, `Man anontarget(p, "man"))
+}
+
+/* gentarget: wordlist = wordlist ;; */
+const gentarget = {p
+ var outlist, cmdlist
+ var gt
+
+ match wordlist(p)
+ | `std.None: failparse(p, "gen target missing output files\n")
+ | `std.Some out:
+ outlist = out
+ ;;
+
+ skipspace(p)
+ if !matchc(p, '=')
+ failparse(p, "expected '=' after '%s %s'\n", cmdlist, outlist[outlist.len-1])
+ ;;
+
+ match wordlist(p)
+ | `std.None: failparse(p, "gen target missing command\n")
+ | `std.Some cmd:
+ cmdlist = cmd
+ ;;
+
+ if !matchc(p, ';') || !matchc(p, ';')
+ failparse(p, "expected ';;' terminating genfile command, got %c\n", peekc(p))
+ ;;
+
+ gt = std.mk([.out = outlist, .cmd=cmdlist])
+ for o in outlist
+ std.htput(p.gensrc, o, gt)
+ ;;
+ p.targs = std.slpush(p.targs, `Gen gt)
+}
+
+/*
+myrtarget: name '=' inputlist ';;'
+ | name attrlist = inputlist ';;'
+*/
+const myrtarget = {p, targ
+ var ldscript, runtime, inst, incpath
+ var name, inputs, libdeps, attrs
+ var fsel
+
+ match word(p)
+ | `std.Some n: name = n
+ | `std.None: failparse(p, "expected target name after '%s'\n", targ)
+ ;;
+
+ skipspace(p)
+ if matchc(p, '{')
+ match attrlist(p)
+ | `std.Some al: attrs = al
+ | `std.None: failparse(p, "invalid attr list for %s %s\n", targ, name)
+ ;;
+ else
+ attrs = [][:]
+ ;;
+
+ skipspace(p)
+ if !matchc(p, '=')
+ failparse(p, "expected '=' after '%s %s'\n", targ, name)
+ ;;
+
+ match inputlist(p)
+ | `std.Some (wl, libs):
+ fsel = mkfsel()
+ libdeps = libs
+ for w in wl
+ fseladd(fsel, w)
+ ;;
+ inputs = fselfin(fsel)
+ std.slfree(wl)
+ | `std.None: failparse(p, "expected list of file names after '%s %s'\n", targ, name)
+ ;;
+
+ skipspace(p)
+ if !matchc(p, ';') || !matchc(p, ';')
+ failparse(p, "expected ';;' terminating input list, got %c\n", peekc(p))
+ ;;
+
+ inst = true
+ ldscript = ""
+ runtime = ""
+ incpath = [][:]
+ for elt in attrs
+ match elt
+ | ("ldscript", lds): ldscript = std.sldup(lds)
+ | ("runtime", rt): runtime = std.sldup(rt)
+ | ("inc", path): incpath = std.slpush(incpath, std.sldup(path))
+ | ("noinst", val):
+ if val.len != 0
+ failparse(p, "noinst attr does not take argument\n")
+ ;;
+ inst = false
+ | (invalid, _):
+ std.fatal(1, "got invalid attr '%s'\n", invalid)
+ ;;
+ ;;
+ -> std.mk([
+ .name=name,
+ .inputs=inputs,
+ .libdeps=libdeps,
+ .install=inst,
+ .ldscript=ldscript,
+ .runtime=runtime,
+ .incpath=incpath,
+ .built=false
+ ])
+}
+
+/* anontarget: '=' wordlist ';;' */
+const anontarget = {p, targ
+ var inputs
+
+ inputs = [][:]
+ skipspace(p)
+ if !matchc(p, '=')
+ failparse(p, "expected '=' after '%s' target\n", targ)
+ ;;
+
+ match wordlist(p)
+ | `std.None: failparse(p, "expected list of file names after '%s' target\n", targ)
+ | `std.Some wl: inputs = wl
+ ;;
+ skipspace(p)
+ if !matchc(p, ';') || !matchc(p, ';')
+ failparse(p, "expected ';;' terminating input list\n")
+ ;;
+ -> inputs
+}
+
+/*
+attrlist: attrs '}'
+
+attrs : EMPTY
+ | attrs attr
+
+attr : name
+ | name '=' name
+*/
+const attrlist = {p
+ var al
+
+ al = [][:]
+ while true
+ match word(p)
+ | `std.Some k:
+ skipspace(p)
+ if matchc(p, '=')
+ match word(p)
+ | `std.Some v:
+ al = std.slpush(al, (k, v))
+ | `std.None:
+ failparse(p, "invalid attr in attribute list\n")
+ ;;
+ else
+ al = std.slpush(al, (k, [][:]))
+ ;;
+ | `std.None: break
+ ;;
+ ;;
+ if !matchc(p, '}')
+ failparse(p, "expected '}' at end of attr list\n")
+ ;;
+ if al.len == 0
+ -> `std.None
+ else
+ -> `std.Some al
+ ;;
+}
+
+/*
+inputlist: EMPTY
+ | inputlist input
+
+input : word
+ | "lib" word
+*/
+const inputlist = {p
+ var wl, libs
+
+ wl = [][:]
+ libs = [][:]
+ while true
+ match word(p)
+ | `std.Some "lib":
+ match word(p)
+ | `std.Some l: libs = std.slpush(libs, l)
+ | `std.None: failparse(p, "expected lib name after 'lib'\n")
+ ;;
+ | `std.Some w: wl = std.slpush(wl, w)
+ | `std.None: break
+ ;;
+ ;;
+ if wl.len == 0
+ -> `std.None
+ else
+ -> `std.Some (wl, libs)
+ ;;
+}
+
+/* wordlist: EMPTY | wordlist word */
+const wordlist = {p
+ var wl
+
+ wl = [][:]
+ while true
+ match word(p)
+ | `std.Some w: wl = std.slpush(wl, w)
+ | `std.None: break
+ ;;
+ ;;
+ if wl.len == 0
+ -> `std.None
+ else
+ -> `std.Some wl
+ ;;
+}
+
+/* word: /wordchar*/
+const word = {p : parser#
+ var c, n
+ var start
+
+ skipspace(p)
+
+ c = peekc(p)
+ if c == '"'
+ n = 0
+ nextc(p)
+ start = p.rest
+ while p.rest.len > 0
+ c = peekc(p)
+ if c == '"'
+ nextc(p)
+ goto done
+ elif c == '\\'
+ c = nextc(p)
+ ;;
+ nextc(p)
+ n += std.charlen(c)
+ ;;
+ failparse(p, "input ended within quoted word\n")
+ else
+ n = 0
+ start = p.rest
+ while p.rest.len > 0
+ c = peekc(p)
+ if wordchar(c)
+ nextc(p)
+ n += std.charlen(c)
+ else
+ break
+ ;;
+ ;;
+ ;;
+:done
+ if n > 0
+ -> `std.Some std.sldup(start[:n])
+ else
+ -> `std.None
+ ;;
+}
+
+const wordchar = {c
+ -> std.isalnum(c) || \
+ c == '.' || c == '_' || c == '$' || c == '-' || \
+ c == '/' || c == ':' || c == '!' || c == '~' || \
+ c == '+'
+}
+
+const skipspace = {p : parser#
+ var c, r
+
+ r = p.rest
+ while r.len > 0
+ c = peekc(p)
+ match c
+ | ' ': nextc(p)
+ | '\t': nextc(p)
+ | '\n':
+ nextc(p)
+ p.line++
+ | '#':
+ while p.rest.len > 0 && peekc(p) != '\n'
+ nextc(p)
+ ;;
+ | _:
+ break
+ ;;
+ ;;
+}
+
+const matchc = {p, c
+ var chr, s
+
+ if p.rest.len == 0
+ -> false
+ ;;
+ (chr, s) = std.striter(p.rest)
+ if c == chr
+ p.rest = s
+ -> true
+ else
+ -> false
+ ;;
+}
+
+const peekc = {p
+ -> std.decode(p.rest)
+}
+
+const nextc = {p
+ var c, s
+
+ (c, s) = std.striter(p.rest)
+ p.rest = s
+ -> c
+}
+
--- /dev/null
+++ b/subdir.myr
@@ -1,0 +1,25 @@
+use std
+
+use "types.use"
+use "util.use"
+
+pkg bld =
+ const subdirs : (p : parser#, subs : byte[:][:], targ : std.option(byte[:]) -> void)
+;;
+
+const subdirs = {p, subs, targ
+ for s in subs
+ match targ
+ | `std.None: /* nothing */
+ | `std.Some t:
+ if !std.sleq(t, s)
+ continue
+ ;;
+ ;;
+
+ std.put("Entering directory %s\n", s)
+ run(p.cmd, s)
+ std.put("Leaving directory %s\n", s)
+ ;;
+}
+
--- /dev/null
+++ b/types.myr
@@ -1,0 +1,51 @@
+use std
+
+pkg bld =
+ type parser = struct
+ cmd : byte[:][:]
+ /* parse input */
+ data : byte[:]
+ rest : byte[:]
+ fname : byte[:]
+ line : int
+
+ /* build params */
+ targs : targ[:]
+ gensrc : std.htab(byte[:], gentarg#)#
+ prefix : byte[:]
+ system : byte[:]
+ arch : byte[:]
+ ;;
+
+ type depgraph = struct
+ roots : byte[:][:]
+ deps : std.htab(byte[:], byte[:][:])#
+ libs : std.htab(byte[:], byte[:][:])#
+ sources : std.htab(byte[:], byte[:])#
+ updated : std.htab(byte[:], bool)#
+ ;;
+
+ type myrtarg = struct
+ name : byte[:]
+ inputs : byte[:][:]
+ libdeps : byte[:][:]
+ built : bool
+ install : bool
+ runtime : byte[:]
+ incpath : byte[:][:]
+ ldscript : byte[:]
+ ;;
+
+ type gentarg = struct
+ out : byte[:][:]
+ cmd : byte[:][:]
+ ;;
+
+ type targ = union
+ `Bin myrtarg#
+ `Lib myrtarg#
+ `Gen gentarg#
+ `Sub byte[:][:]
+ `Man byte[:][:]
+ ;;
+;;
--- /dev/null
+++ b/util.myr
@@ -1,0 +1,100 @@
+use std
+
+use "opts.use"
+
+pkg bld =
+ const run : (cmd : byte[:][:], dir : byte[:] -> void)
+ const printcmd
+ const srcsplit : (src : byte[:] -> (byte[:], byte[:], byte[:]))
+ const swapsuffix : (f : byte[:], suff : byte[:], newsuff : byte[:] -> byte[:])
+ const srcswapsuffix : (f : byte[:], newsuff : byte[:] -> byte[:])
+ const strlistfree : (sl : byte[:][:] -> void)
+;;
+
+const run = {cmd, dir
+ var pid
+
+ printcmd(cmd)
+ pid = std.fork()
+ if pid == -1
+ std.fatal(1, "could not fork command\n")
+ elif pid == 0
+ if dir.len > 0
+ if !std.chdir(dir)
+ std.fatal(1, "unable to enter directory %s\n", dir)
+ ;;
+ ;;
+ if std.execvp(cmd[0], cmd) < 0
+ std.fatal(1, "failed to exec %s\n", cmd[0])
+ ;;
+ else
+ match std.wait(pid)
+ | `std.Wsuccess: /* nothing */
+ | `std.Wfailure: std.fatal(1, "%s did not execute successfully\n", cmd[0])
+ | `std.Wsignalled: std.fatal(1, "%s exited with signal\n", cmd[0])
+ | `std.Waiterror: std.fatal(1, "failed to wait for %s\n", cmd[0])
+ ;;
+ ;;
+}
+
+const printcmd = {lst
+ if lst.len > 0
+ std.put("\t")
+ std.put("%s\t", lst[0])
+ for l in lst[1:]
+ std.put("%s ", l)
+ ;;
+ ;;
+ std.put("\n")
+}
+
+const srcsplit = {src
+ var platf, suff
+
+ platf = ""
+ suff = ""
+ match std.strrfind(src, ".")
+ | `std.Some i:
+ suff = src[i:]
+ src = src[:i]
+ | `std.None:
+ /* no suffix to trim */
+ ;;
+
+ match std.strrfind(src, "+")
+ | `std.Some i:
+ platf = src[i:]
+ src = src[:i]
+ | `std.None:
+ /* no platform to trim */
+ ;;
+ -> (src, platf, suff)
+}
+
+const swapsuffix = {f, suff, newsuff
+ if std.hassuffix(f, suff)
+ f = f[:f.len - suff.len]
+ ;;
+ -> std.fmt("%s%s", f, newsuff)
+}
+
+const srcswapsuffix = {src, new
+ var base, platf, suff
+
+ (base, platf, suff) = srcsplit(src)
+ if std.sleq(suff, ".myr")
+ -> std.strcat(base, new)
+ elif std.sleq(suff, ".s")
+ -> std.strcat(base, new)
+ else
+ std.fatal(1, "unrecognized source %s\n", src)
+ ;;
+}
+
+const strlistfree = {sl
+ for s in sl
+ std.slfree(s)
+ ;;
+ std.slfree(sl)
+}
+