diff options
author | Ori Bernstein <ori@eigenstate.org> | 2015-04-13 01:25:54 -0700 |
---|---|---|
committer | Ori Bernstein <ori@eigenstate.org> | 2015-04-13 01:29:42 -0700 |
commit | 52585564bf5a46db0ae6c9fe4e20307e014de848 (patch) | |
tree | a0cbd5529dcb351d1c1c059d44379e8e5b85b1f8 /mbld/util.myr | |
parent | 2f5a88d9b79271f78ec71cf7f796ca5fb74de89e (diff) | |
download | mc-52585564bf5a46db0ae6c9fe4e20307e014de848.tar.gz |
Move mbld to subdirectory of itself for merging.
Diffstat (limited to 'mbld/util.myr')
-rw-r--r-- | mbld/util.myr | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/mbld/util.myr b/mbld/util.myr new file mode 100644 index 0000000..57872b1 --- /dev/null +++ b/mbld/util.myr @@ -0,0 +1,123 @@ +use std + +use "opts.use" +use "types.use" + +pkg bld = + const run : (cmd : 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 gettarg : (tab : std.htab(byte[:], targ)#, n : byte[:] -> targ) + const setdir : (b : build#, dir : byte[:] -> void) +;; + +const run = {cmd + var pid + + printcmd(cmd) + pid = std.fork() + if pid == -1 + std.fatal(1, "could not fork command\n") + elif pid == 0 + 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, "FAIL: \"%s\"\n", std.strjoin(cmd, " ")) + | `std.Wsignalled: std.fatal(1, "CRASH: \"%s\"\n", std.strjoin(cmd, " ")) + | `std.Waiterror: std.fatal(1, "WAT: \"%s\"\n", std.strjoin(cmd, " ")) + ;; + ;; +} + +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) +} + +const gettarg = {tab, n + match std.htget(tab, n) + | `std.None: std.fatal(1, "internal: nonexistent %s\n", n) + | `std.Some t: -> t + ;; +} + +const setdir = {b, dir + var p + + if !std.sleq(b.curdir, dir) + p = std.pathcat(b.basedir, dir) + if b.curdir.len != 0 + std.put("Leaving directory %s\n", b.curdir) + ;; + + std.put("Entering directory '%s'\n", dir) + if !std.chdir(p) + std.fatal(1, "could not cd into %s\n") + ;; + b.curdir = dir + std.slfree(p) + ;; +} + |