shithub: mc

Download patch

ref: 0eac125e778c6d7052e7d2f45539a8c15daa679a
parent: 566577d22229f3cf155f96b1c1d4be59b300a8ac
author: Ori Bernstein <ori@eigenstate.org>
date: Tue Sep 23 08:31:25 EDT 2014

Add support for command line build options.

--- a/build.myr
+++ b/build.myr
@@ -8,23 +8,18 @@
 
 pkg bld =
 	const build	: (p : parser# -> bool)
+	const buildbin	: (bin : byte[:], inputs : byte[:][:] -> void)
+	const buildlib	: (lib : byte[:], inputs : byte[:][:] -> void)
 ;;
 
 const build = {p
-	var dg
 
 	for t in p.targs
 		match t
 		| `Bin (b, leaves):
-			if !myrdeps(&dg, b, leaves, false)
-				std.fatal(1, "Could not load dependencies for %s\n", b)
-			;;
-			buildbin(&dg, b, leaves)
+			buildbin(b, leaves)
 		| `Lib (l, leaves):
-			if !myrdeps(&dg, l, leaves, true)
-				std.fatal(1, "Could not load dependencies for %s\n", l)
-			;;
-			buildlib(&dg, l, leaves)
+			buildlib(l, leaves)
 		| `Sub sub:
 			dosub(sub)
 		| `Man m:
@@ -34,25 +29,37 @@
 	-> true
 }
 
-const buildbin = {dg, bin, inputs
+const buildbin = {bin, inputs
+	var dg
+
+	if !myrdeps(&dg, bin, inputs, false)
+		std.fatal(1, "Could not load dependencies for %s\n", bin)
+	;;
 	match std.htget(dg.deps, bin)
 	| `std.Some deps:
 		for d in deps
-			builddep(dg, d, bin)
+			builddep(&dg, d, bin)
 		;;
-		linkbin(dg, bin, inputs, "")
+		linkbin(&dg, bin, inputs, "")
 	| `std.None:
+		std.fatal(1, "No such binary %s\n", bin)
 	;;
 }
 
-const buildlib = {dg, lib, inputs
+const buildlib = {lib, inputs
+	var dg
+
+	if !myrdeps(&dg, lib, inputs, true)
+		std.fatal(1, "Could not load dependencies for %s\n", lib)
+	;;
 	match std.htget(dg.deps, lib)
 	| `std.Some deps:
 		for d in deps
-			builddep(dg, d, lib)
+			builddep(&dg, d, lib)
 		;;
-		archivelib(dg, lib, inputs)
+		archivelib(&dg, lib, inputs)
 	| `std.None:
+		std.fatal(1, "No such library %s\n", lib)
 	;;
 }
 
--- a/config.myr
+++ b/config.myr
@@ -1,4 +1,4 @@
 pkg config = 
-const Instroot = "/usr/local"
+const Instroot = "/Users/orib/bin"
 const Sys = "OSX"
 ;;
--- a/main.myr
+++ b/main.myr
@@ -1,19 +1,64 @@
 use std
+
 use "parse.use"
 use "build.use"
+use "opts.use"
 
 const main = {args : byte[:][:]
 	var p : bld.parser#
+	var optctx
 
-	p = std.alloc()
-	p.line = 1
-	p.fname = "bldfile"
-	match std.slurp("bldfile")
-	| `std.Ok d:	p.data = d
-	| `std.Fail _:	std.fatal(1, "could not open file 'bldfile'\n")
+	optctx = std.optinit("hb:l:s:r:I:C:A:M:L:R:", args)
+	while !std.optdone(optctx)
+		match std.optnext(optctx)
+		| ('h', arg): usage(args[0])
+		| ('b', arg): bld.opt_binname = arg
+		| ('l', arg): bld.opt_libname = arg
+		| ('s', arg): bld.opt_ldscript = arg
+		| ('I', arg): bld.opt_incpaths = std.slpush(bld.opt_incpaths, arg)
+		| ('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.
+		*/
+		| ('C', arg): bld.opt_mc = arg
+		| ('A', arg): bld.opt_as = arg
+		| ('M', arg): bld.opt_muse = arg
+		| ('L', arg): bld.opt_ld = arg
+		| ('R', arg): bld.opt_ar = arg
+		;;
 	;;
-	p.rest = p.data
 
-	bld.parse(p)
-	bld.build(p)
+	if bld.opt_binname.len != 0
+		bld.buildbin(bld.opt_binname, optctx.args)
+	elif bld.opt_libname.len != 0
+		bld.buildlib(bld.opt_libname, optctx.args)
+	else
+		p = std.alloc()
+		p.line = 1
+		p.fname = "bldfile"
+		match std.slurp("bldfile")
+		| `std.Ok d:	p.data = d
+		| `std.Fail _:	std.fatal(1, "could not open file 'bldfile'\n")
+		;;
+		p.rest = p.data
+
+		bld.parse(p)
+		bld.build(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)
 }
--- a/opts.myr
+++ b/opts.myr
@@ -2,10 +2,30 @@
 	var opt_arch 	: byte[:]
 	var opt_sys	: byte[:]
 	var opt_targ	: byte[:]
+	var opt_binname	: byte[:]
+	var opt_libname	: byte[:]
+	var opt_runtime	: byte[:]
+	var opt_ldscript	: byte[:]
 	var opt_incpaths	: byte[:][:]
+
+	/* undocumented/unsupported opts */
+	var opt_mc	: byte[:]
+	var opt_as	: byte[:]
+	var opt_muse	: byte[:]
+	var opt_ld	: byte[:]
+	var opt_ar	: byte[:]
 ;;
 
-var opt_arch 	: byte[:]
-var opt_sys	: byte[:]
-var opt_targ	: byte[:]
-var opt_incpaths	: byte[:][:]
+var opt_arch 
+var opt_sys
+var opt_targ
+var opt_binname
+var opt_libname
+var opt_runtime
+var opt_ldscript
+var opt_incpaths
+var opt_mc	= "6m"
+var opt_as	= "as"
+var opt_muse	= "muse"
+var opt_ld	= "ld"
+var opt_ar	= "ar"