shithub: mc

Download patch

ref: cdeec2192c82b9cc5fb46d4f1da96dd03a24b4f8
parent: 9f9d134454de4f796e3e7338a2c59e0ee59a3560
author: Ori Bernstein <ori@eigenstate.org>
date: Fri Oct 3 14:35:20 EDT 2014

Support platform specific myr files.

    foo+blah.myr will compile to foo.o, foo.use
    foo+blah.s will compile to foo.s

    The blah is a platform string, in one of the following forms:

        system-arch
        system
        arch

--- a/build.myr
+++ b/build.myr
@@ -94,8 +94,8 @@
 	u = builddep(&dg, lib)
 	l = builddep(&dg, archive)
 	if  u || l
-		archivelib(&dg, lib, inputs)
 		mergeuse(&dg, lib, inputs)
+		archivelib(&dg, lib, inputs)
 	;;
 	std.slfree(archive)
 }
@@ -142,7 +142,7 @@
 	if std.hassuffix(src, ".myr")
 		run(["6m", src][:], "")
 	elif std.hassuffix(src, ".s")
-		o = swapsuffix(src, ".s", ".o")
+		o = srcswapsuffix(src, ".o")
 		run(["as", src, "-o", o][:], "")
 		std.slfree(o)
 	else
@@ -152,7 +152,6 @@
 
 const linkbin = {dg, bin, srcfiles, ldscript, rt
 	var cmd
-	var obj
 
 	cmd = [][:]
 
@@ -175,14 +174,7 @@
 
 	/* input.o list.o... */
 	for f in srcfiles
-		if std.hassuffix(f, ".myr")
-			obj = swapsuffix(f, ".myr", ".o")
-		elif std.hassuffix(f, ".s")
-			obj = swapsuffix(f, ".s", ".o")
-		else
-			std.fatal(1, "unknown file type for %s\n", f)
-		;;
-		cmd = std.slpush(cmd, obj)
+		cmd = std.slpush(cmd, srcswapsuffix(f, ".o"))
 	;;
 
 	/* -l lib... */
@@ -213,13 +205,7 @@
 	cmd = std.slpush(cmd, std.sldup("-rcs"))
 	cmd = std.slpush(cmd, std.fmt("lib%s.a", lib))
 	for f in files
-		if std.hassuffix(f, ".myr")
-			obj = swapsuffix(f, ".myr", ".o")
-		elif std.hassuffix(f, ".s")
-			obj = swapsuffix(f, ".s", ".o")
-		else
-			std.fatal(1, "unknown file type for %s\n", f)
-		;;
+		obj = srcswapsuffix(f, ".o")
 		cmd = std.slpush(cmd, obj)
 	;;
 	run(cmd, "")
@@ -235,7 +221,7 @@
 	cmd = std.slpush(cmd, std.sldup(lib))
 	for f in files
 		if std.hassuffix(f, ".myr")
-			cmd = std.slpush(cmd, swapsuffix(f, ".myr", ".use"))
+			cmd = std.slpush(cmd, srcswapsuffix(f, ".use"))
 		elif !std.hassuffix(f, ".s")
 			std.fatal(1, "unknown file type for %s\n", f)
 		;;
--- a/deps.myr
+++ b/deps.myr
@@ -5,9 +5,10 @@
 use "config.use"
 use "opts.use"
 use "types.use"
+use "util.use"
 
 pkg bld =
-	const myrdeps	: (dg : depgraph#, targ : byte[:], leaves : byte[:][:], islib : bool	-> bool)
+	const myrdeps	: (dg : depgraph#, targ : byte[:], srcs : byte[:][:], islib : bool	-> bool)
 ;;
 
 type dep = union
@@ -17,9 +18,11 @@
 
 var usepat	: regex.regex#
 
-const myrdeps = {dg, targ, leaves, islib
+const myrdeps = {dg, targ, srcs, islib
 	var seentab, donetab
-	var obj, usefile, out, useout
+	var out, useout
+	var objs, uses
+	var i
 
 	match regex.compile("^\\s*use\\s+((\\<\\S+\\>)|(\"(\\S+)\")).*")
 	| `std.Ok re:	usepat = re
@@ -40,23 +43,25 @@
 		out = std.sldup(targ)
 		useout = ""
 	;;
-	for src in leaves
-		obj = srcswapsuffix(src, ".o")
-		std.htput(dg.sources, obj, src)
-		pushdep(dg, obj, out)
-		pushdep(dg, src, obj)
-		if std.hassuffix(src, ".myr")
-			usefile = srcswapsuffix(src, ".use")
-			std.htput(dg.sources, usefile, src)
-			pushdep(dg, src, usefile)
-			if islib
-				pushdep(dg, usefile, useout)
-			;;
-		else
-			usefile = ""
+	objs = swapall(srcs, ".o")
+	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])
 		;;
-		srcdeps(dg, seentab, donetab, src, obj, usefile)
 	;;
+	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(dg, seentab, donetab, srcs[i], objs[i], uses[i])
+	;;
 	dumpgraph(dg)
 	std.htfree(seentab)
 	std.htfree(donetab)
@@ -63,6 +68,16 @@
 	-> 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
 
@@ -120,7 +135,12 @@
 		;;
 		->
 	;;
-	src = swapsuffix(usefile, ".use", ".myr")
+	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(g, seen, done, src, "", usefile)
@@ -230,24 +250,6 @@
 	sl = std.htgetv(dg.deps, dst, [][:])
 	sl = std.slpush(sl, src)
 	std.htput(dg.deps, dst, sl)
-}
-
-const srcswapsuffix = {s, new
-	if std.hassuffix(s, ".myr")
-		-> swapsuffix(s, ".myr", new)
-	elif std.hassuffix(s, ".s")
-		-> swapsuffix(s, ".s", new)
-	else
-		std.fatal(1, "unrecognized source %s\n", s)
-	;;
-}
-
-const swapsuffix = {s, suffix, new
-	if !std.hassuffix(s, suffix)
-		std.die("swapping suffix on string without appropriate suffix\n")
-	;;
-	s = s[:s.len - suffix.len]
-	-> std.strcat(s, new)
 }
 
 const rdstr = {f
--- a/opts.myr
+++ b/opts.myr
@@ -22,6 +22,10 @@
 	var opt_ld	: byte[:]
 	var opt_ar	: byte[:]
 
+	var sysarchstr	: byte[:]
+	var archstr	: byte[:]
+	var sysstr	: byte[:]
+
 	const initopts	: (-> void)
 ;;
 
@@ -42,6 +46,11 @@
 var opt_ld	= "ld"
 var opt_ar	= "ar"
 
+/* derived */
+var sysarchstr	= ""
+var archstr	= ""
+var sysstr	= ""
+
 const initopts = {
 	var un
 
@@ -68,6 +77,11 @@
 	opt_ld = std.getenvv("MYR_LD", "ld")
 	opt_ar = std.getenvv("MYR_AR", "ar")
 	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.pathcat(config.Instroot, "/lib/myr/_myrrt.o")
 }
 
--- a/parse.myr
+++ b/parse.myr
@@ -1,6 +1,7 @@
 use std
 
 use "types.use"
+use "util.use"
 
 pkg bld =
 	const parse	: (p : parser#	-> bool)
@@ -86,7 +87,13 @@
 	;;
 
 	match wordlist(p)
-	| `std.Some wl: inputs = wl
+	| `std.Some wl: 
+		inputs = [][:]
+		for w in wl
+			if isplatform(w)
+				inputs = std.slpush(inputs, w)
+			;;
+		;;
 	| `std.None: failparse(p, "expected list of file names after '%s %s'\n", targ, name)
 	;;
 
@@ -215,7 +222,8 @@
 const wordchar = {c
 	-> std.isalnum(c) || \
 		c == '.' || c == '_' || c == '$' || c == '-' || \
-		c == '/' || c == ':' || c == '!' || c == '~'
+		c == '/' || c == ':' || c == '!' || c == '~' || \
+		c == '+'
 }
 
 const skipspace = {p : parser#
--- a/util.myr
+++ b/util.myr
@@ -1,10 +1,14 @@
 use std
 
+use "opts.use"
+
 pkg bld =
 	const run	: (cmd : byte[:][:], dir : byte[:] -> void)
 	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 isplatform	: (src : byte[:] -> bool)
 ;;
 
 const run = {cmd, dir
@@ -74,6 +78,29 @@
 	-> 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 isplatform = {src
+	var base, platf, suff
+
+	(base, platf, suff) = srcsplit(src)
+	-> platf.len == 0 || \
+		std.sleq(platf, sysarchstr) ||  \
+		std.sleq(platf, sysstr) ||  \
+		std.sleq(platf, archstr)
+}
+
 const strlistfree = {sl
 	for s in sl
 		std.slfree(s)
@@ -80,3 +107,4 @@
 	;;
 	std.slfree(sl)
 }
+