ref: 6507bf31d345f0046a1415088e42390c8befdd97
parent: 02efc3a7d15eb97ec7b80be68474d112f65493c8
author: Ori Bernstein <ori@eigenstate.org>
date: Thu Oct 1 19:06:16 EDT 2015
Update the C glue library detection. We still need to propagate the extra libs, though.
--- a/lib/bio/bio.myr
+++ b/lib/bio/bio.myr
@@ -35,6 +35,7 @@
const flush : (f : file# -> bool)
/* seeking */
+ const seek : (f : file#, std.off -> std.off)
/* single unit operations */
const putb : (f : file#, b : byte -> std.size)
@@ -224,6 +225,11 @@
f.wend = 0
;;
-> ret
+}
+
+const seek = {f, off
+ flush(f)
+ -> std.seek(f.fd, off, std.Seekset)
}
/* writes a single byte to the output stream */
--- a/lib/regex/interp.myr
+++ b/lib/regex/interp.myr
@@ -4,6 +4,7 @@
pkg regex =
const exec : (re : regex#, str : byte[:] -> std.option(byte[:][:]))
+ const matchfree : (pat : byte[:][:] -> void)
/*
FIXME: implement. This should scan for a possible start char in the
regex and use that to optimize.
@@ -42,6 +43,10 @@
next = thr.next
thrfree(re, thr)
;;
+}
+
+const matchfree = {m
+ std.slfree(m)
}
const getmatches = {re, thr
--- a/mbld/build.myr
+++ b/mbld/build.myr
@@ -217,7 +217,7 @@
match std.htget(dg.input, out)
| `std.Some src:
if stale
- compile(src, incs)
+ compile(dg, src, incs)
;;
std.htput(dg.updated, out, true)
| `std.None:
@@ -225,7 +225,7 @@
-> stale
}
-const compile = {src, incs
+const compile = {dg, src, incs
var o
var cmd
@@ -259,6 +259,9 @@
cmd = std.slpush(cmd,"-o")
cmd = std.slpush(cmd, o)
cmd = std.slpush(cmd, src)
+ for flg in std.htgetv(dg.cflags, src, [][:])
+ cmd = std.slpush(cmd, flg)
+ ;;
run(cmd)
else
std.fatal("Unknown file type for {}\n", src)
@@ -295,6 +298,11 @@
/* -L path -l lib... */
cmd = addlibs(cmd, dg.libs, incs)
+
+ /* add extra libs */
+ for l in dg.extralibs
+ cmd = std.slpush(cmd, std.fmt("-l{}", l))
+ ;;
/* special for OSX: it warns if we don't add this */
if std.sleq(opt_sys, "osx")
--- a/mbld/deps.myr
+++ b/mbld/deps.myr
@@ -14,12 +14,13 @@
const Abiversion = 8
var usepat : regex.regex#
+var cflagpat : regex.regex#
+var clibpat : regex.regex#
const __init__ = {
- match regex.compile("^\\s*use\\s+((\\<\\S+\\>)|\"(\\S+)\").*")
- | `std.Ok re: usepat = re
- | `std.Fail f: std.fatal("Failed to compile use pattern regex\n")
- ;;
+ usepat = std.try(regex.compile("^\\s*use\\s+((\\<\\S+\\>)|\"(\\S+)\").*"))
+ cflagpat = std.try(regex.compile("/\\*\\s*CFLAGS:\\s*(.*)\\s*\\*/"))
+ clibpat = std.try(regex.compile("/\\*\\s*LIBS:\\s*(.*)\\s*\\*/"))
}
@@ -38,7 +39,8 @@
;;
const myrdeps = {b, mt, doclean, addsrc
- var objs, uses, srcs, clib
+ var objs, uses, srcs
+ var cflags, libs
var out, useout
var dg : depgraph#
var ds : depscan
@@ -52,6 +54,8 @@
.updated = std.mkht(std.strhash, std.streq),
.seen = std.mkht(std.strhash, std.streq),
.done = std.mkht(std.strhash, std.streq),
+ .cflags = std.mkht(std.strhash, std.streq),
+ .extralibs = [][:],
.dynamic = false,
])
/* direct dependencies of binary */
@@ -81,8 +85,9 @@
std.htput(dg.input, uses[i], srcs[i])
pushdep(dg, srcs[i], uses[i])
elif std.hassuffix(srcs[i], ".glue.c")
- clib = swapsuffix(srcs[i], ".glue.c", "")
- std.htput(dg.libs, clib, [][:])
+ (cflags, libs) = scrapecflags(b, dg, srcs[i])
+ std.htput(dg.cflags, srcs[i], cflags)
+ dg.extralibs = std.sljoin(dg.extralibs, libs)
dg.dynamic = true
;;
;;
@@ -89,14 +94,15 @@
for i = 0; i < srcs.len; i++
pushdep(dg, objs[i], out)
- if mt.islib && std.hassuffix(srcs[i], ".myr")
+ if !std.hassuffix(srcs[i], ".myr")
+ continue
+ ;;
+ if mt.islib
pushdep(dg, uses[i], useout)
;;
- ;;
-
- for i = 0; i < srcs.len; i++
srcdeps(b, &ds, dg, srcs[i], objs[i], uses[i])
;;
+
dumpgraph(dg)
-> dg
}
@@ -198,11 +204,76 @@
std.htput(g.done, usefile, true)
}
-const getdeps = {b, ds, path
+const scrapecflags = {b, ds, path
+ var cflags, libs, lnum
+ var f
+
+ lnum = 0
+ cflags = [][:]
+ libs = [][:]
+ f = opensrc(b, path)
+ while true
+ lnum++
+ match bio.readln(f)
+ | `std.Some ln:
+ (cflags, libs) = getcflags(ln, cflags, libs)
+ std.slfree(ln)
+ | `std.None:
+ break
+ ;;
+ ;;
+ bio.close(f)
+ -> (cflags, libs)
+}
+
+const getcflags = {ln, cflags, libs
+ var flags
+
+ match regex.exec(cflagpat, ln)
+ | `std.None:
+ | `std.Some m:
+ std.put("Got cflags {}\n", m)
+ flags = std.strtok(m[1])
+ for fl in flags
+ cflags = std.slpush(cflags, std.sldup(fl))
+ ;;
+ std.slfree(flags)
+ ;;
+
+ match regex.exec(clibpat, ln)
+ | `std.None:
+ | `std.Some m:
+ std.put("Got libs {}\n", m)
+ flags = std.strtok(m[1])
+ for fl in flags
+ libs = std.slpush(libs, std.sldup(fl))
+ ;;
+ std.slfree(flags)
+ ;;
+ -> (cflags, libs)
+}
+
+const getdeps = {b : build#, ds : depscan#, path
var deps, lnum
var f
+ lnum = 0
deps = [][:]
+ f = opensrc(b, path)
+ while true
+ lnum++
+ match bio.readln(f)
+ | `std.Some ln:
+ deps = depname(deps, ln, lnum)
+ std.slfree(ln)
+ | `std.None: break
+ ;;
+ ;;
+ bio.close(f)
+ -> deps
+}
+
+const opensrc = {b, path
if !std.fexists(path)
match std.htget(b.gensrc, path)
| `std.Some gt: run(gt.cmd)
@@ -209,24 +280,30 @@
| `std.None: std.fatal("no input file {}\n", path)
;;
;;
+
match bio.open(path, bio.Rd)
- | `std.Ok fd: f = fd
| `std.Fail m: std.fatal("could not open {}: {}\n", path, m)
+ | `std.Ok f: -> f
;;
+}
- lnum = 0
- while true
- lnum++
- match bio.readln(f)
- | `std.Some ln:
- deps = depname(deps, ln, lnum)
- std.slfree(ln)
- | `std.None:
- bio.close(f)
- -> deps
+const depname = {deps, ln, lnum
+ /*
+ 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, `Lib (std.sldup(uses[2]), lnum))
+ else
+ deps = std.slpush(deps, `Local (std.sldup(uses[3]), lnum))
;;
+ | `std.None:
+ /* nothing to do */
;;
- std.die("unreachable")
+ -> deps
}
const scrapelibs = {dg, lib, incs
@@ -290,26 +367,6 @@
;;
std.fatal("could not find library {}.\n", lib)
}
-
-const depname = {deps, ln, lnum
- /*
- 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, `Lib (std.sldup(uses[2]), lnum))
- else
- deps = std.slpush(deps, `Local (std.sldup(uses[3]), lnum))
- ;;
- | `std.None:
- /* nothing to do */
- ;;
- -> deps
-}
-
/* pushes a dep into the dependency list */
const pushdep = {dg, src, dst
--- a/mbld/types.myr
+++ b/mbld/types.myr
@@ -70,6 +70,10 @@
updated : std.htab(byte[:], bool)#
seen : std.htab(byte[:], bool)#
done : std.htab(byte[:], bool)#
+
+ /* used for linking C */
+ extralibs : byte[:][:]
+ cflags : std.htab(byte[:], byte[:][:])#
dynamic : bool
;;
;;