shithub: mc

ref: b65a821ba2d8c753e974b02e541229dfd6754a6a
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"

const main = {args : byte[:][:]
	var b : bld.build#
	var mt : bld.myrtarg
	var targname
	var bintarg
	var optctx
	var libpath

	optctx = std.optinit("hb:l:s:Sr:I:C:A:M:L:R:d", args)
	bld.initopts()
	while !std.optdone(optctx)
		match std.optnext(optctx)
		| ('h', arg): usage(args[0])
		| ('s', arg): bld.opt_ldscript = arg
		| ('f', arg): bld.opt_bldfile = arg
		| ('I', arg): bld.opt_incpaths = std.slpush(bld.opt_incpaths, arg)
		| ('S', _): bld.opt_genasm = true
		| ('R', arg): bld.opt_instroot = 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("got invalid arg\n")
		;;
	;;
	if bld.opt_instroot.len > 0 && !std.sleq(bld.opt_instroot, "none")
		libpath = std.pathcat(bld.opt_instroot, "lib/myr")
		bld.opt_incpaths = std.slpush(bld.opt_incpaths, libpath)
	;;

	match regex.compile("^\\s*use\\s+((\\<\\S+\\>)|(\"(\\S+)\")).*")
	| `std.Ok re:	bld.usepat = re
	| `std.Fail f:	std.fatal(1, "Failed to compile use pattern regex\n")
	;;

	b = mkbuild()
	if targname.len != 0
		mt = [
			.name=targname,
			.inputs=optctx.args,
			.runtime=bld.opt_runtime,
			.incpath=bld.opt_incpaths,
			.ldscript=bld.opt_ldscript,
			.libdeps=[][:]
		]
		if bintarg
			bld.buildbin(b, &mt, true)
		else
			bld.buildlib(b, &mt)
		;;
	else
		findproj(b, bld.opt_bldfile)
		bld.load(b)
		/*bld.configure()*/
		/* default: buildall */
		if optctx.args.len == 0
			bld.buildall(b)
		else
			for cmd in optctx.args
				match cmd
				| "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.build(b, target)
				;;
			;;
		;;
	;;
}

const mkbuild = {
	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
}

const findproj = {b, bldfile
	if !findbase(b, bldfile) || !std.chdir(b.basedir)
		std.fatal(1, "could not find %s\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)
			b.basedir = dir
			b.bldfile = bld
			-> true
		;;
		p = std.pathcat(dir, "..")
		std.slfree(dir)
		dir = p
	;;
	-> false
}

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