shithub: mc

ref: 4e2a253a02cd6cf934f2acbb4b725db5971cd337
dir: /main.myr/

View raw version
use std

use "parse.use"
use "build.use"
use "opts.use"

const main = {args : byte[:][:]
	var p : bld.parser#
	var optctx

	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
		| ('f', arg): bld.opt_bldfile = 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
		;;
	;;

	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 = loadbuild(bld.opt_bldfile)
		if optctx.args.len == 0
			bld.buildall(p)
		else
			for cmd in optctx.args
				match cmd
				| "all":	bld.buildall(p)
				| "clean":	bld.cleanall(p)
				| "install":	bld.install(p)
				| "uninstall":	bld.uninstall(p)
				| "test":	bld.test(p)
				| target:	bld.build(p, target)
				;;
			;;
		;;
	;;
}

const loadbuild =  {path
	var p

	p = std.alloc()
	p.line = 1
	p.fname = path
	match std.slurp(path)
	| `std.Ok d:	p.data = d
	| `std.Fail _:	std.fatal(1, "could not open file 'bldfile'\n")
	;;
	p.rest = p.data
	bld.parse(p)

	-> 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)
}