shithub: mc

Download patch

ref: 07f5aa5bbf9445c1ff188a6f75a6fa162119cc35
parent: 5f46919fc6a471d9a75bb25fae64aecefc9ec864
author: Ori Bernstein <ori@eigenstate.org>
date: Mon Dec 28 18:11:39 EST 2015

Add the '-R' option to mbld.

	You can now build and run directly as one step.

--- a/doc/mbld.txt
+++ b/doc/mbld.txt
@@ -39,7 +39,9 @@
 To build, install, and test this binary, the following commands can be run:
 
     mbld                # builds everything
+    mbld gen            # generates 'gen' targets
     mbld install        # installs
+    mbld uninstall      # uninstalls
     mbld test           # runs tests
     mbld clean          # removes generated files
 
@@ -125,6 +127,12 @@
 However, any attempts to use members of foo/ as part of the build from
 within bar/ will raise an error, as you are attempting to reference a
 build from outside of the project.
+
+Flags
+-----
+
+	-T	list all targets
+	-t	build with build tag 't
 
 Commands
 --------
--- a/lib/std/bld.sub
+++ b/lib/std/bld.sub
@@ -33,6 +33,7 @@
 	ipparse.myr
 	mk.myr
 	mkpath.myr
+	mktemp.myr
 	now.myr
 	option.myr
 	optparse.myr
--- /dev/null
+++ b/lib/std/mktemp.myr
@@ -1,0 +1,51 @@
+use "die.use"
+use "alloc.use"
+use "env.use"
+use "errno.use"
+use "fmt.use"
+use "option.use"
+use "pathjoin.use"
+use "memops.use"
+use "rand.use"
+use "result.use"
+use "sldup.use"
+use "syswrap.use"
+use "types.use"
+
+pkg std =
+	const mktemp	: (base : byte[:], opt : fdopt, mode : int64 -> std.result((fd, byte[:]), errno))
+;;
+
+const Retries = 100
+
+const mktemp = {base, opt, mode
+	var tmpdir, path, uniq
+	var v : uint64
+
+	match std.getenv("TMPDIR")
+	| `std.Some d:	tmpdir = d
+	| `std.None:	tmpdir = std.sldup("/tmp")
+	;;
+
+	for var i = 0; i < Retries; i++
+		v = std.randnum()
+		uniq = fmt("{}{}", base, v)
+		path = pathcat(tmpdir, uniq)
+		match std.openmode(path, opt | Ocreat, mode)
+		| `Fail e:
+			if e != Eexist
+				std.slfree(uniq)
+				std.slfree(tmpdir)
+				-> `Fail e
+			;;
+		| `Ok fd:
+			std.slfree(uniq)
+			std.slfree(tmpdir)
+			-> `Ok (fd, path)
+		;;
+		std.slfree(uniq)
+		std.slfree(path)
+	;;
+	std.slfree(tmpdir)
+	-> `Fail Eexist
+}
--- a/mbld/build.myr
+++ b/mbld/build.myr
@@ -129,8 +129,8 @@
 		src = std.htkeys(dg.sources)
 
 		incs = std.sldup(targ.incpath)
-		if opt_instroot.len > 0 && !std.sleq(opt_instroot, "none")
-			libpath = std.pathcat(bld.opt_instroot, config.Libpath)
+		if opt_instbase.len > 0 && !std.sleq(opt_instbase, "none")
+			libpath = std.pathcat(bld.opt_instbase, config.Libpath)
 			incs = std.slpush(incs, libpath)
 		;;
 		linkbin(dg, targ.name, src, targ.ldscript, targ.runtime, incs, targ.libdeps)
@@ -442,7 +442,7 @@
 		std.slfree(p)
 	;;
 
-	p = std.pathjoin([opt_instroot, config.Libpath, lib][:])
+	p = std.pathjoin([opt_instbase, config.Libpath, lib][:])
 	if std.fexists(p)
 		-> `std.Some p
 	;;
@@ -468,7 +468,7 @@
 			for inc in targ.incpath
 				std.fput(1, "\t{}\n", inc)
 			;;
-			std.fput(1, "\t{}/{}\n", opt_instroot, config.Libpath)
+			std.fput(1, "\t{}/{}\n", opt_instbase, config.Libpath)
 			std.exit(1)
 		;;
 	;;
--- a/mbld/deps.myr
+++ b/mbld/deps.myr
@@ -369,7 +369,7 @@
 		| `std.Fail m:	/* nothing */
 		;;
 	;;
-	path = std.pathjoin([opt_instroot, config.Libpath, lib][:])
+	path = std.pathjoin([opt_instbase, config.Libpath, lib][:])
 	match  bio.open(path, bio.Rd)
 	| `std.Ok file:	-> file
 	| `std.Fail m:	/* nothing */
--- a/mbld/install.myr
+++ b/mbld/install.myr
@@ -57,7 +57,7 @@
 	var path
 
 	setdir(b, dir)
-	path = std.pathjoin([opt_destdir, opt_instroot, prefix, file][:])
+	path = std.pathjoin([opt_destdir, opt_instbase, prefix, file][:])
 	if rm
 		std.put("\trm {}\n", path)
 		if !std.remove(path)
--- a/mbld/main.myr
+++ b/mbld/main.myr
@@ -15,16 +15,14 @@
 
 const main = {args : byte[:][:]
 	var b : bld.build#
-	var mt : bld.myrtarg
 	var cleanfirst
 	var dumponly
-	var targname
 	var bintarg
+	var targname
+	var runsrc
 	var cmd 
 	var tags
 
-	dumponly = false
-	cleanfirst = false
 	cmd = std.optparse(args, &[
 		.argdesc = "[inputs...]",
 		.opts = [
@@ -36,6 +34,7 @@
 			[.opt='I', .arg="inc", .desc="add 'inc' to your include path"],
 			[.opt='R', .arg="root", .desc="install into 'root'"],
 			[.opt='b', .arg="bin", .desc="compile binary named 'bin' from inputs"],
+			[.opt='G', .arg="bin", .desc="compile and run binary 'bin' from inputs"],
 			[.opt='l', .arg="lib", .desc="compile lib named 'lib' from inputs"],
 			[.opt='r', .arg="rt", .desc="link against runtime 'rt' instead of default"],
 			[.opt='C', .arg="mc", .desc="compile with 'mc' instead of the default compiler"],
@@ -43,8 +42,12 @@
 		][:]
 	])
 
-	targname = ""
 	tags = [][:]
+	runsrc = ""
+	targname = ""
+	dumponly = false
+	cleanfirst = false
+
 	bld.initopts()
 	for opt in cmd.opts
 		match opt
@@ -52,8 +55,9 @@
 		| ('S', ""):	bld.opt_genasm = true
 		| ('c', ""):	cleanfirst = true
 		| ('I', arg):	bld.opt_incpaths = std.slpush(bld.opt_incpaths, arg)
-		| ('R', arg):	bld.opt_instroot = arg
+		| ('B', arg):	bld.opt_instbase = arg
 		| ('t', tag):	tags = std.slpush(tags, tag)
+		| ('R', arg):	runsrc = arg
 		| ('b', arg):
 			targname = arg
 			bintarg = true
@@ -80,21 +84,17 @@
 
 	b = mkbuild(tags)
 	if targname.len != 0
-		mt = [
-			.name=targname,
-			.inputs=cmd.args,
-			.runtime=bld.opt_runtime,
-			.incpath=bld.opt_incpaths,
-			.libdeps=[][:]
-		]
-		if cleanfirst
-			bld.cleanmyr(b, &mt)
+		buildimm(b, targname, cmd.args, bintarg, cleanfirst)
+	elif runsrc.len != 0
+		match std.mktemp("run", std.Ordwr, 0o755)
+		| `std.Ok (fd, tmp):
+			buildimm(b, "/tmp/foo", [runsrc][:], true, cleanfirst)
+			runcmd("/tmp/foo", cmd.args)
+			std.remove(tmp)
+			std.close(fd)
+		| `std.Fail e:
+			std.put("unable to open tmp file: {}\n", e)
 		;;
-		if bintarg
-			bld.buildbin(b, &mt, true)
-		else
-			bld.buildlib(b, &mt)
-		;;
 	elif dumponly
 		findproj(b, "bld.proj")
 		bld.load(b)
@@ -125,6 +125,30 @@
 			;;
 		;;
 	;;
+}
+
+const buildimm = {b, targ, inputs, bintarg, cleanfirst
+	var mt : bld.myrtarg
+
+	mt = [
+		.name=targ,
+		.inputs=inputs,
+		.runtime=bld.opt_runtime,
+		.incpath=bld.opt_incpaths,
+		.libdeps=[][:]
+	]
+	if cleanfirst
+		bld.cleanmyr(b, &mt)
+	;;
+	if bintarg
+		bld.buildbin(b, &mt, true)
+	else
+		bld.buildlib(b, &mt)
+	;;
+}
+
+const runcmd = {bin, args
+	bld.run(std.sljoin([bin][:], args))
 }
 
 const mkbuild = {tags
--- a/mbld/mbld.1
+++ b/mbld/mbld.1
@@ -4,11 +4,12 @@
 .SH SYNOPSIS
 .B mbld
 .I [all | clean | install | uninstall | test]
-.I -[hsSfr]
+.I -[?csSfr]
 .I [-b bin]
 .I [-l lib]
+.I [-R src]
 .I [-I inc]
-.I [-R root]
+.I [-B base]
 .I [-r runtime]
 .I [file... | targets...]
 .br
@@ -31,10 +32,14 @@
 The myrbuild options are:
 
 .TP
-.B -h
+.B -[h|?]
 Print a summary of the available options.
 
 .TP
+.B -c
+cleans the code before building. This applies to both 
+
+.TP
 .B -b \fIbinname\fP
 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'.
@@ -53,6 +58,12 @@
 for the target platform), and a matching usefile called 'name'. Only static
 libraries are currently supported. Ignores the contents of \fIbld.proj\fP
 and \fIbld.sub\fP if they exist.
+
+.TP
+.B -R
+.I src
+Compile source given into a binary in temporary storage, and then execute it
+with the command line arguments passed in.
 
 .TP
 .B -S
--- a/mbld/opts.myr
+++ b/mbld/opts.myr
@@ -8,7 +8,7 @@
 	var opt_runtime	: byte[:]
 	var opt_genasm	: bool
 	var opt_incpaths	: byte[:][:]
-	var opt_instroot	: byte[:]
+	var opt_instbase	: byte[:]
 	var opt_manpath	: byte[:]
 	var opt_destdir	: byte[:]
 	var opt_outdir	: byte[:]
@@ -31,7 +31,7 @@
 var opt_libname	= ""
 var opt_runtime	= ""
 var opt_incpaths	/* FIXME: taking a constant slice is a nonconstant initializer */
-var opt_instroot	= ""
+var opt_instbase	= ""
 var opt_manpath	= ""
 var opt_destdir	= ""
 var opt_debug	= false
@@ -61,7 +61,7 @@
 	;;
 
 	opt_incpaths = [][:]
-	opt_instroot = config.Instroot
+	opt_instbase = config.Instroot
 	opt_manpath = config.Manpath
 	opt_destdir = std.getenvv("DESTDIR", "")
 	opt_mc = std.getenvv("MYR_MC", "6m")
@@ -68,7 +68,7 @@
 	opt_muse = std.getenvv("MYR_MUSE", "muse")
 	opt_runtime = std.getenvv("MYR_RT", "")
 	if opt_runtime.len == 0
-		opt_runtime = std.pathjoin([opt_instroot, config.Libpath, config.Runtime][:]) 
+		opt_runtime = std.pathjoin([opt_instbase, config.Libpath, config.Runtime][:]) 
 	;;
 }