shithub: mc

ref: c3e6c525d4716fdaa8839119f4d487f76401f272
dir: /mbld/main.myr/

View raw version
use std
use regex

use "build.use"
use "clean.use"
use "config.use"
use "deps.use"
use "install.use"
use "opts.use"
use "parse.use"
use "test.use"
use "types.use"
use "util.use"
use "syssel.use"

const main = {args : byte[:][:]
	var b : bld.build#
	var cleanfirst
	var dumponly
	var bintarg
	var targname
	var runsrc
	var cmd 
	var tags

	cmd = std.optparse(args, &[
		.argdesc = "[inputs...]",
		.opts = [
			[.opt='c', .desc="clean before building"],
			[.opt='T', .desc="list all available targets"],
			[.opt='t', .arg="tag", .desc="build with specified systag"],
			[.opt='S', .desc="generate assembly when building"],
			[.opt='d', .desc="dump debugging information for mbld"],
			[.opt='I', .arg="inc", .desc="add 'inc' to your include path"],
			[.opt='R', .arg="runsrc", .desc="source to compile and run"],
			[.opt='B', .arg="base", .desc="install into 'base'"],
			[.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"],
			[.opt='M', .arg="mu", .desc="merge uses with 'mu' instead of the default muse"],
		][:]
	])

	tags = [][:]
	runsrc = ""
	targname = ""
	dumponly = false
	cleanfirst = false

	bld.initopts()
	for opt in cmd.opts
		match opt
		| ('T', ""):	dumponly = true
		| ('S', ""):	bld.opt_genasm = true
		| ('c', ""):	cleanfirst = true
		| ('I', arg):	bld.opt_incpaths = std.slpush(bld.opt_incpaths, arg)
		| ('B', arg):	bld.opt_instbase = arg
		| ('t', tag):	tags = std.slpush(tags, tag)
		| ('R', arg):	runsrc = arg
		| ('b', arg):
			targname = arg
			bintarg = true
		| ('l', arg):
			targname = arg
			bintarg = false
		| ('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.
		*/
		| ('d', arg): bld.opt_debug = true
		| ('C', arg): bld.opt_mc = arg
		| ('M', arg): bld.opt_muse = arg
		| _:	std.die("unreachable\n")

		;;
	;;

	b = mkbuild(tags)
	if targname.len != 0
		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)
		;;
	elif dumponly
		findproj(b, "bld.proj")
		bld.load(b)
		for t in b.all
			std.put("{}\n", t)
		;;
	else
		findproj(b, "bld.proj")
		bld.load(b)
		/*bld.configure()*/
		/* default: buildall */
		if cleanfirst
			bld.cleanall(b)
		;;
		if cmd.args.len == 0
			bld.buildall(b)
		else
			for c in cmd.args
				match c
				| "all":	bld.buildall(b)
				| "gen":	bld.genall(b)
				| "clean":	bld.cleanall(b)
				| "install":	bld.install(b)
				| "uninstall":	bld.uninstall(b)
				| "test":	bld.test(b)
				| target:	bld.buildtarg(b, target)
				;;
			;;
		;;
	;;
}

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
	var b

	b = std.zalloc()
	b.targs = std.mkht(std.strhash, std.streq)
	b.gensrc = std.mkht(std.strhash, std.streq)
	b.built = std.mkht(std.strhash, std.streq)
	b.sysattrs = std.mkht(std.strhash, std.streq)
	bld.addsysattrs(b, tags)
	-> b
}

const findproj = {b, bldfile
	if !findbase(b, bldfile) || !std.chdir(b.basedir)
		std.fatal("could not find {}\n", bldfile)
	;;
	bld.setdir(b, "")
}

const findbase = {b, file
	var p, bld, dir

	dir = std.getcwd()
	while !std.sleq(dir, "/")
		bld = std.pathcat(dir, file)
		if std.fexists(bld)
			std.put("project base {}:\n", dir)
			b.basedir = dir
			b.bldfile = bld
			-> true
		;;
		p = std.pathcat(dir, "..")
		std.slfree(dir)
		dir = p
	;;
	-> false
}