shithub: mc

ref: 62375ca259df19a32b791ab67256e24a5c75c1db
dir: /mbld/fsel.myr/

View raw version
use std

use "opts.use"

pkg bld =
	type syssel(@a) = struct
		file	: byte[:]
		line	: int
		targ	: byte[:]
		sysattrs	: std.htab(byte[:], bool)#
		_match	: std.htab(byte[:], int)#
		_best	: std.htab(byte[:], @a)#
	;;

	const mkfsel	: (f : byte[:], line : int, targ : byte[:] -> syssel(byte[:])#)
	const fseladd	: (fsel : syssel(byte[:])#, file : byte[:] -> void)
	const fselfin	: (fsel : syssel(byte[:])# -> byte[:][:])
;;

const mkfsel = {file, line, targ
	var fsel

	fsel = std.mk([
		.file = file,
		.line = line,
		.targ = targ,
		._match = std.mkht(std.strhash, std.streq),
		._best = std.mkht(std.strhash, std.streq),
		.sysattrs = std.mkht(std.strhash, std.streq),
	])
	addsysattrs(fsel.sysattrs)
	-> fsel
}

const fseladd = {fsel, f
	var basename, attrs
	var nmatch, curbest
	var attrlist

	match std.strfind(f, "+")
	| `std.Some i:
		basename = f[:i]
		match std.strrfind(f[i+1:], ".")
		| `std.Some j:	attrs = f[i+1:][:j]
		| `std.None:	std.fatal("unrecognized type for file {}\n", f)
		;;
	| `std.None:
		match std.strrfind(f, ".")
		| `std.None:	std.fatal("unrecognized type for file {}\n", f)
		| `std.Some i:
			basename = f[:i]
			attrs = ""
		;;
	;;

	nmatch = 0
	attrlist = std.strsplit(attrs, "-")
	for a in attrlist
		if std.hthas(fsel.sysattrs, a)
			nmatch++
		else
			nmatch = -1
			break
		;;
	;;
	std.slfree(attrlist)
	curbest = std.htgetv(fsel._match, basename, -1)
	if curbest < nmatch
		std.htput(fsel._match, basename, nmatch)
		std.htput(fsel._best, basename, f)
	;;
}

const fselfin = {fsel
	var keys, nmatch, ret

	keys = std.htkeys(fsel._match)
	ret = [][:]
	for k in keys
		nmatch = std.htgetv(fsel._match, k, -1)
		if nmatch == -1
			std.fatal("{}:{}: target {}, no applicable file for '{}'\n", fsel.file, fsel.line, fsel.targ, k)
		;;
		ret = std.slpush(ret, std.htgetv(fsel._best, k, ""))
	;;
	std.htfree(fsel._match)
	std.htfree(fsel._best)
	std.htfree(fsel.sysattrs)
	-> ret
}

const addsysattrs = {sa
	var attrs
	match opt_sys
	| "freebsd":	attrs = ["freebsd", "posixy"][:]
	| "osx":	attrs = ["osx", "posixy"][:]
	| "linux":	attrs = ["linux", "posixy"][:]
	| "plan9":	attrs = ["plan9"][:]
	| unknown:	std.fatal("unknown system \"{}\"\n", unknown)
	;;
	for a in attrs
		std.htput(sa, a, true)
	;;

	match opt_arch
	| "x64":	attrs = ["x64"][:]
	| unknown:	std.fatal("unknown arch {}\n", unknown)
	;;
	for a in attrs
		std.htput(sa, a, true)
	;;
}