Blame sofort/ccenv/ccenv.sh

7a17ab
# ccenv.sh: sofort's tool-finding bits,
7a17ab
# invoked from within the project-agnostic configure script.
7a17ab
7a17ab
# this file is covered by COPYING.SOFORT.
7a17ab
7a17ab
# invocation and names of binary tools:
7a17ab
# agnostic names (ar, nm, objdump, ...);
7a17ab
# target-prefixed agnostic names (x86_64-nt64-midipix-ar, ...);
7a17ab
# branded names (llvm-ar, llvm-nm, llvm-objdump, ...);
7a17ab
# target-prefixed branded names (x86_64-linux-gnu-gcc-ar, ...);
7a17ab
# target-specifying branded tools (llvm-ar --target=x86_64-linux, ...).
7a17ab
7a17ab
# cross-compilation: default search order:
7a17ab
# target-prefixed agnostic tools;
7a17ab
# target-prefixed branded tools, starting with the prefix
7a17ab
# most commonly associated with the selected compiler (that is,
7a17ab
# ``gcc'' when using gcc, and ``llvm'' when using clang);
7a17ab
# target-speficying branded tools, starting once again with the
7a17ab
# prefix most commonly associated with the selected compiler.
7a17ab
7a17ab
# internal variables of interest:
7a17ab
# ccenv_cfgtype: the type of host being tested (host/native)
7a17ab
# ccenv_cfgfile: the configuration file for the host being tested
7a17ab
# ccenv_cflags:  the comprehensive cflags for the host being tested
7a17ab
# ccenv_cchost:  the host being tested, as reported by -dumpmachine
7a17ab
7a17ab
7a17ab
ccenv_usage()
7a17ab
{
7a17ab
	cat "$mb_project_dir"/sofort/ccenv/ccenv.usage
7a17ab
	exit 0
7a17ab
}
7a17ab
7a17ab
7a17ab
ccenv_newline()
7a17ab
{
7a17ab
	printf '\n' >> "$ccenv_cfgfile"
7a17ab
}
7a17ab
7a17ab
7a17ab
ccenv_comment()
7a17ab
{
7a17ab
	ccenv_internal_str='#'
7a17ab
7a17ab
	for ccenv_internal_arg ; do
7a17ab
		ccenv_internal_str="$ccenv_internal_str $ccenv_internal_arg"
7a17ab
	done
7a17ab
7a17ab
	printf '%s\n' "$ccenv_internal_str" >> "$ccenv_cfgfile"
7a17ab
}
7a17ab
7a17ab
7a17ab
ccenv_tool_prolog()
7a17ab
{
7a17ab
	ccenv_line_dots='.....................................'
7a17ab
	ccenv_tool_desc=" == checking for ${1}"
7a17ab
	ccenv_tool_dlen="${#ccenv_line_dots}"
7a17ab
7a17ab
	printf '\n%s\n' '________________________' >&3
7a17ab
	printf "ccenv: checking for ${1}\n\n" >&3
7a17ab
	printf "%${ccenv_tool_dlen}.${ccenv_tool_dlen}s" \
7a17ab
		"${ccenv_tool_desc}  ${mb_line_dots}"
7a17ab
}
7a17ab
7a17ab
7a17ab
ccenv_tool_epilog()
7a17ab
{
7a17ab
	ccenv_line_dots='................................'
7a17ab
	ccenv_tool_dlen="$((${#ccenv_line_dots} - ${#1}))"
7a17ab
df62a5
	case ${ccenv_tool_dlen} in
df62a5
		0 | -* )
df62a5
			ccenv_tool_dlen='3' ;;
df62a5
	esac
df62a5
7a17ab
	printf "%${ccenv_tool_dlen}.${ccenv_tool_dlen}s  %s.\n" \
7a17ab
		"${ccenv_line_dots}" "${1}"
7a17ab
7a17ab
	if [ "${1}" = 'false' ]; then
7a17ab
		printf '\n\nccenv: not (yet) found.\n' >&3
7a17ab
	else
7a17ab
		printf "\n\nccenv : found $(command -v ${1}).\n" >&3
7a17ab
	fi
7a17ab
7a17ab
	printf '%s\n' '------------------------' >&3
7a17ab
}
7a17ab
7a17ab
7a17ab
ccenv_tool_variant_epilog()
7a17ab
{
7a17ab
	ccenv_expr=${1}='${'${1}':-false}'
7a17ab
	eval "$ccenv_expr"
7a17ab
7a17ab
	ccenv_expr='${'${1}'}'
7a17ab
	eval ccenv_tool_epilog "$ccenv_expr"
7a17ab
}
7a17ab
7a17ab
7a17ab
ccenv_attr_prolog()
7a17ab
{
7a17ab
	ccenv_line_dots=' .....................................'
7a17ab
	ccenv_attr_desc=" == detect ${ccenv_cfgtype} ${1}"
7a17ab
	ccenv_attr_dlen="${#ccenv_line_dots}"
7a17ab
7a17ab
	printf "%${ccenv_attr_dlen}.${ccenv_attr_dlen}s" \
7a17ab
		"${ccenv_attr_desc} ${ccenv_line_dots}"
7a17ab
7a17ab
	printf '\n%s\n' '________________________' >&3
7a17ab
	printf "ccenv: detecting ${1}\n\n" >&3
7a17ab
}
7a17ab
7a17ab
7a17ab
ccenv_attr_epilog()
7a17ab
{
7a17ab
	ccenv_line_dots='................................'
7a17ab
	ccenv_tool_dlen="$((${#ccenv_line_dots} - 1 - ${#1}))"
7a17ab
df62a5
	case ${ccenv_tool_dlen} in
df62a5
		0 | -* )
df62a5
			ccenv_tool_dlen='3' ;;
df62a5
	esac
df62a5
7a17ab
	printf "%${ccenv_tool_dlen}.${ccenv_tool_dlen}s  %s.\n" \
7a17ab
		"${ccenv_line_dots}" "${1}"
7a17ab
7a17ab
	printf '\n\nccenv: detected result: %s\n' "${1}" >&3
7a17ab
	printf '%s\n' '------------------------' >&3
7a17ab
}
7a17ab
7a17ab
7a17ab
ccenv_find_tool()
7a17ab
{
7a17ab
	if [ -z "$ccenv_prefixes" ]; then
7a17ab
		for ccenv_candidate in $(printf '%s' "$ccenv_candidates"); do
8dc2b2
			ccenv_cmd_args="${@:-}"
8dc2b2
8dc2b2
			if [ -z "$ccenv_cmd_args" ]; then
7a17ab
				if command -v "$ccenv_candidate" > /dev/null; then
7a17ab
					ccenv_tool="$ccenv_candidate"
7a17ab
					return 0
7a17ab
				fi
7a17ab
			else
7a17ab
				if command -v "$ccenv_candidate" > /dev/null; then
7a17ab
					if "$ccenv_candidate" $@ > /dev/null 2>&3; then
7a17ab
						ccenv_tool="$ccenv_candidate"
7a17ab
						return 0
7a17ab
					fi
7a17ab
				fi
7a17ab
			fi
7a17ab
		done
7a17ab
7a17ab
		ccenv_tool=false
7a17ab
7a17ab
		return 0
7a17ab
	fi
7a17ab
7a17ab
	for ccenv_prefix in $(printf '%s' "$ccenv_prefixes"); do
7a17ab
		for ccenv_candidate in $(printf '%s' "$ccenv_candidates"); do
7a17ab
			ccenv_tool="$ccenv_prefix$ccenv_candidate"
7a17ab
7a17ab
			if command -v "$ccenv_tool" > /dev/null; then
7a17ab
				return 0
7a17ab
			fi
7a17ab
		done
7a17ab
	done
7a17ab
7a17ab
	for ccenv_candidate in $(printf '%s' "$ccenv_candidates"); do
7a17ab
		if command -v "$ccenv_candidate" > /dev/null; then
7a17ab
			ccenv_tool="$ccenv_candidate"
7a17ab
			return 0
7a17ab
		fi
7a17ab
	done
7a17ab
7a17ab
	ccenv_tool=false
7a17ab
7a17ab
	return 0
7a17ab
}
7a17ab
7a17ab
7a17ab
ccenv_set_primary_tools()
7a17ab
{
7a17ab
	ccenv_core_tools="ar nm objdump ranlib size strip strings objcopy"
7a17ab
	ccenv_hack_tools="addr2line cov elfedit readelf readobj otool"
7a17ab
	ccenv_peep_tools="perk mdso dlltool windmc windres"
7a17ab
7a17ab
	for __tool in $(printf '%s' "$ccenv_core_tools $ccenv_hack_tools $ccenv_peep_tools"); do
7a17ab
		ccenv_tool_prolog "$__tool"
7a17ab
7a17ab
		if [ -n "$mb_agnostic" ]; then
7a17ab
			ccenv_candidates=" $__tool"
7a17ab
7a17ab
		elif [ -n "$mb_zealous" ]; then
7a17ab
			ccenv_candidates="$mb_zealous-$__tool"
7a17ab
7a17ab
		elif [ "$mb_toolchain" = 'gcc' ]; then
7a17ab
			ccenv_candidates="gcc-$__tool"
7a17ab
			ccenv_candidates="$ccenv_candidates $__tool"
7a17ab
			ccenv_candidates="$ccenv_candidates llvm-$__tool"
7a17ab
7a17ab
		elif [ "$mb_toolchain" = 'llvm' ]; then
7a17ab
			ccenv_candidates="llvm-$__tool"
7a17ab
			ccenv_candidates="$ccenv_candidates $__tool"
7a17ab
			ccenv_candidates="$ccenv_candidates gcc-$__tool"
7a17ab
7a17ab
		elif [ -n "$mb_toolchain" ]; then
7a17ab
			ccenv_candidates="$mb_toolchain-$__tool"
7a17ab
			ccenv_candidates="$ccenv_candidates $__tool"
7a17ab
			ccenv_candidates="$ccenv_candidates gcc-$__tool"
7a17ab
			ccenv_candidates="$ccenv_candidates llvm-$__tool"
7a17ab
7a17ab
		else
7a17ab
			ccenv_candidates="$__tool"
7a17ab
			ccenv_candidates="$ccenv_candidates gcc-$__tool"
7a17ab
			ccenv_candidates="$ccenv_candidates llvm-$__tool"
7a17ab
		fi
7a17ab
7a17ab
		if [ "$ccenv_cfgtype" = 'host' ]; then
7a17ab
			ccenv_var_prefix='mb_'
7a17ab
		else
7a17ab
			ccenv_var_prefix='mb_native_'
7a17ab
		fi
7a17ab
7a17ab
		ccenv_var_name=$ccenv_var_prefix$__tool
7a17ab
		ccenv_var_expr='${'$ccenv_var_name':-}'
7a17ab
		eval ccenv_var_val=$ccenv_var_expr
7a17ab
7a17ab
		if [ -n "$ccenv_var_val" ]; then
7a17ab
			eval ccenv_$__tool="$ccenv_var_val"
7a17ab
		else
7a17ab
			ccenv_find_tool
7a17ab
			eval ccenv_$__tool="$ccenv_tool"
7a17ab
		fi
7a17ab
7a17ab
		ccenv_tool_epilog "$ccenv_tool"
7a17ab
	done
7a17ab
7a17ab
	# windrc
7a17ab
	ccenv_windrc="$ccenv_windres"
e30786
e30786
	# archive format preamble
0dcf74
	if [ -n "$ccenv_dumpmachine_switch" ]; then
0dcf74
		ccenv_libgcc_a_header=$(od -b -N8             \
0dcf74
			$($ccenv_cc -print-file-name=libgcc.a) \
0dcf74
			| head -n1)
0dcf74
	else
0dcf74
		ccenv_libgcc_a_header=
0dcf74
	fi
0dcf74
0dcf74
	# ar (default)
e30786
	ccenv_cc_arfmt='common'
e30786
e30786
	# ar (big)
e30786
	ccenv_bigaf_header=$(printf '%s\n' '<bigaf>' | od -b | head -n1)
e30786
e30786
	if [ "$ccenv_libgcc_a_header" = "$ccenv_bigaf_header" ]; then
e30786
		ccenv_cc_arfmt='bigaf'
e30786
e30786
		for __tool in $(printf '%s' "$ccenv_core_tools"); do
e30786
			ccenv_var_name=ccenv_$__tool
e30786
			ccenv_var_expr='${'$ccenv_var_name':-}'
e30786
			eval ccenv_var_val="$ccenv_var_expr"
e30786
e30786
			if [ "$ccenv_var_val" != false ]; then
e30786
				ccenv_var_val="$ccenv_var_val -X64"
e30786
				ccenv_var_expr='${ccenv_var_val:-}'
e30786
				eval ccenv_$__tool="$ccenv_var_expr"
e30786
			fi
e30786
		done
e30786
	fi
e30786
e30786
	# ar (small)
e30786
	ccenv_aiaff_header=$(printf '%s\n' '<aiaff>' | od -b | head -n1)
e30786
e30786
	if [ "$ccenv_libgcc_a_header" = "$ccenv_aiaff_header" ]; then
e30786
		ccenv_cc_arfmt='aiaff'
e30786
e30786
		for __tool in $(printf '%s' "$ccenv_core_tools"); do
e30786
			ccenv_var_name=ccenv_$__tool
e30786
			ccenv_var_expr='${'$ccenv_var_name':-}'
e30786
			eval ccenv_var_val="$ccenv_var_expr"
e30786
e30786
			if [ "$ccenv_var_val" != false ]; then
e30786
				ccenv_var_val="$ccenv_var_val -X32"
e30786
				ccenv_var_expr='${ccenv_var_val:-}'
e30786
				eval ccenv_$__tool="$ccenv_var_expr"
e30786
			fi
e30786
		done
e30786
	fi
7a17ab
}
7a17ab
7a17ab
ccenv_set_tool_variants()
7a17ab
{
7a17ab
	# as (asm)
7a17ab
	ccenv_tool_prolog 'as (asm)'
7a17ab
	ccenv_candidates=as
7a17ab
	ccenv_find_tool
7a17ab
7a17ab
	if [ "$ccenv_tool" = false ]; then
7a17ab
		ccenv_as_asm=
7a17ab
	else
95d7f9
		$ccenv_tool --help 2>&1 | grep -i '.bc assembler' \
7a17ab
		|| ccenv_as_asm="$ccenv_tool"
7a17ab
	fi
7a17ab
7a17ab
	ccenv_tool_variant_epilog 'ccenv_as_asm'
7a17ab
7a17ab
	# as (ll)
7a17ab
	ccenv_tool_prolog 'as (ll)'
7a17ab
	ccenv_candidates=llvm-as
7a17ab
	ccenv_find_tool
7a17ab
7a17ab
	if [ "$ccenv_tool" != false ]; then
7a17ab
		ccenv_as_ll="$ccenv_tool"
7a17ab
	fi
7a17ab
7a17ab
	ccenv_tool_variant_epilog 'ccenv_as_ll'
7a17ab
7a17ab
	# as (mc)
7a17ab
	ccenv_tool_prolog 'as (mc)'
7a17ab
	ccenv_candidates=llvm-mc
7a17ab
	ccenv_find_tool
7a17ab
7a17ab
	if [ "$ccenv_tool" != false ]; then
7a17ab
		ccenv_as_mc="$ccenv_tool"
7a17ab
	fi
7a17ab
7a17ab
	ccenv_tool_variant_epilog 'ccenv_as_mc'
7a17ab
7a17ab
	# ld (bfd)
7a17ab
	ccenv_tool_prolog 'ld (bfd)'
7a17ab
	ccenv_candidates=ld.bfd
7a17ab
	ccenv_find_tool
7a17ab
7a17ab
	if [ "$ccenv_tool" != false ]; then
7a17ab
		ccenv_ld_bfd="$ccenv_tool"
7a17ab
	fi
7a17ab
7a17ab
	ccenv_tool_variant_epilog 'ccenv_ld_bfd'
7a17ab
7a17ab
	# ld (gold)
7a17ab
	ccenv_tool_prolog 'ld (gold)'
7a17ab
	ccenv_candidates=ld.gold
7a17ab
	ccenv_find_tool
7a17ab
7a17ab
	if [ "$ccenv_tool" != false ]; then
7a17ab
		ccenv_ld_gold="$ccenv_tool"
7a17ab
	fi
7a17ab
7a17ab
	ccenv_tool_variant_epilog 'ccenv_ld_gold'
7a17ab
7a17ab
	# ld (lld)
7a17ab
	ccenv_tool_prolog 'ld (lld)'
7a17ab
	ccenv_candidates=lld
7a17ab
	ccenv_find_tool
7a17ab
7a17ab
	if [ "$ccenv_tool" != false ]; then
7a17ab
		ccenv_ld_lld="$ccenv_tool"
7a17ab
	fi
7a17ab
7a17ab
	ccenv_tool_variant_epilog 'ccenv_ld_lld'
7a17ab
7a17ab
	# objdump (bfd)
7a17ab
	ccenv_tool_prolog 'objdump (bfd)'
7a17ab
	ccenv_candidates=objdump
7a17ab
	ccenv_find_tool
7a17ab
7a17ab
	if $ccenv_tool --version | grep -i Binutils > /dev/null; then
7a17ab
		ccenv_objdump_bfd="$ccenv_tool"
7a17ab
	fi
7a17ab
7a17ab
	ccenv_tool_variant_epilog 'ccenv_objdump_bfd'
7a17ab
7a17ab
	# objdump (llvm)
7a17ab
	ccenv_tool_prolog 'objdump (llvm)'
7a17ab
	ccenv_candidates=llvm-objdump
7a17ab
	ccenv_find_tool
7a17ab
7a17ab
	if $ccenv_tool --version | grep -i LLVM > /dev/null; then
7a17ab
		ccenv_objdump_llvm="$ccenv_tool"
7a17ab
	fi
7a17ab
7a17ab
	ccenv_tool_variant_epilog 'ccenv_objdump_llvm'
7a17ab
7a17ab
	# readelf (bfd)
7a17ab
	ccenv_tool_prolog 'readelf (bfd)'
7a17ab
	ccenv_candidates=readelf
7a17ab
	ccenv_find_tool
7a17ab
7a17ab
	if $ccenv_tool --version | grep -i Binutils > /dev/null; then
7a17ab
		ccenv_readelf_bfd="$ccenv_tool"
7a17ab
	fi
7a17ab
7a17ab
	ccenv_tool_variant_epilog 'ccenv_readelf_bfd'
7a17ab
7a17ab
	# readelf (llvm)
7a17ab
	ccenv_tool_prolog 'readelf (llvm)'
7a17ab
	ccenv_candidates=llvm-readelf
7a17ab
	ccenv_find_tool
7a17ab
7a17ab
	if $ccenv_tool --version | grep -i LLVM > /dev/null; then
7a17ab
		ccenv_readelf_llvm="$ccenv_tool"
7a17ab
	fi
7a17ab
7a17ab
	ccenv_tool_variant_epilog 'ccenv_readelf_llvm'
7a17ab
7a17ab
	# as
7a17ab
	if [ -n "$ccenv_cc" ]; then
7a17ab
		ccenv_as='$('"$ccenv_makevar_prefix"'CC) -c -x assembler'
7a17ab
	elif [ -n "$mb_agnostic" ]; then
7a17ab
		ccenv_as='$('"$ccenv_makevar_prefix"'AS_ASM)'
7a17ab
	elif [ "$mb_zealous" = 'gcc' ]; then
7a17ab
		ccenv_as='$('"$ccenv_makevar_prefix"'AS_ASM)'
7a17ab
	elif [ -n "$mb_zealous" = 'llvm' ]; then
7a17ab
		ccenv_as='$('"$ccenv_makevar_prefix"'AS_MC)'
7a17ab
	elif [ "$mb_toolchain" = 'gcc' ]; then
7a17ab
		ccenv_as='$('"$ccenv_makevar_prefix"'AS_ASM)'
7a17ab
	elif [ "$mb_toolchain" = 'llvm' ]; then
7a17ab
		ccenv_as='$('"$ccenv_makevar_prefix"'AS_MC)'
7a17ab
	fi
7a17ab
7a17ab
	# ld
7a17ab
	if [ -n "$ccenv_cc" ]; then
7a17ab
		ccenv_ld='$('"$ccenv_makevar_prefix"'CC) -nostdlib -nostartfiles'
7a17ab
	fi
7a17ab
}
7a17ab
7a17ab
ccenv_set_c_compiler_candidates()
7a17ab
{
7a17ab
	if   [ -n "$mb_compiler" ]; then
7a17ab
		ccenv_candidates="$mb_compiler"
7a17ab
7a17ab
	elif [ -n "$mb_agnostic" ]; then
7a17ab
		ccenv_candidates="c99 c11 cc"
7a17ab
7a17ab
	elif [ "$mb_zealous" = 'gcc' ]; then
7a17ab
		ccenv_candidates="gcc"
7a17ab
7a17ab
	elif [ "$mb_zealous" = 'llvm' ]; then
7a17ab
		ccenv_candidates="clang"
7a17ab
7a17ab
	elif [ "$mb_toolchain" = 'gcc' ]; then
7a17ab
		ccenv_candidates="gcc c99 c11 cc clang"
7a17ab
7a17ab
	elif [ "$mb_toolchain" = 'llvm' ]; then
7a17ab
		ccenv_candidates="clang c99 c11 cc gcc"
7a17ab
7a17ab
	elif [ -n "$mb_toolchain" ]; then
7a17ab
		ccenv_candidates="$mb_toolchain c99 c11 cc gcc clang"
7a17ab
7a17ab
	else
78f636
		ccenv_candidates="cc gcc clang c99 c11"
7a17ab
	fi
7a17ab
}
7a17ab
7a17ab
7a17ab
ccenv_set_cc()
7a17ab
{
7a17ab
	ccenv_tool_prolog 'C compiler'
7a17ab
7a17ab
	if [ -z "$ccenv_cc" ]; then
7a17ab
		ccenv_set_c_compiler_candidates
0dcf74
		ccenv_find_tool
7a17ab
		ccenv_cc="$ccenv_tool"
7a17ab
	fi
7a17ab
0dcf74
7a17ab
	if [ "$ccenv_cc" = false ] && [ -n "$mb_compiler" ]; then
7a17ab
		ccenv_cc="$mb_compiler"
7a17ab
	fi
7a17ab
0dcf74
	ccenv_tool_epilog "$ccenv_cc"
0dcf74
0dcf74
0dcf74
	if [ $ccenv_cfgtype = 'host' ]; then
0dcf74
		ccenv_host_cc="$ccenv_cc"
0dcf74
		cfgtest_host_section
0dcf74
		ccenv_host_cc=
0dcf74
	else
0dcf74
		ccenv_native_cc="$ccenv_cc"
0dcf74
		cfgtest_native_section
0dcf74
		ccenv_native_cc=
0dcf74
	fi
0dcf74
0dcf74
	if cfgtest_compiler_switch -dumpmachine ; then
0dcf74
		ccenv_dumpmachine_switch='-dumpmachine'
0dcf74
	else
0dcf74
		ccenv_dumpmachine_switch=
0dcf74
	fi
0dcf74
7a17ab
	ccenv_cc_cmd="$ccenv_cc"
7a17ab
	ccenv_errors=
7a17ab
7a17ab
	if [ "$ccenv_cfgtype" = 'native' ]; then
0dcf74
		if [ -n "$ccenv_dumpmachine_switch" ]; then
0dcf74
			ccenv_host=$(eval $ccenv_cc $(printf '%s' "$ccenv_cflags") \
0dcf74
				$ccenv_dumpmachine_switch 2>&3)
0dcf74
		else
0d2861
			ccenv_host=$(printf '%s' $(uname -m)-$(uname -p)-$(uname -o) \
0d2861
				| tr '[[:upper:]]' '[[:lower:]]')
0dcf74
		fi
0dcf74
7a17ab
		ccenv_cchost=$ccenv_host
7a17ab
		return 0
7a17ab
	fi
7a17ab
0dcf74
7a17ab
	if [ -n "$mb_cchost" ]; then
7a17ab
		ccenv_host="$mb_cchost"
7a17ab
	elif [ -n "$mb_host" ]; then
7a17ab
		ccenv_host="$mb_host"
7a17ab
	else
7a17ab
		ccenv_host=
7a17ab
	fi
7a17ab
0dcf74
	if [ -z "$ccenv_host" ] && [ -n "$ccenv_dumpmachine_switch" ]; then
0dcf74
		ccenv_host=$(eval $ccenv_cc $(printf '%s' "$ccenv_cflags") \
0dcf74
			$ccenv_dumpmachine_switch 2>&3)
7a17ab
		ccenv_cchost=$ccenv_host
0dcf74
0dcf74
	elif [ -z "$ccenv_host" ]; then
0dcf74
		# assume that no -dumpmachine support means native build (fixme)
0d2861
		ccenv_host=$(printf '%s' $(uname -m)-$(uname -p)-$(uname -o) \
0d2861
			| tr '[[:upper:]]' '[[:lower:]]')
0dcf74
		ccenv_cchost=$ccenv_host
0dcf74
0dcf74
	elif [ -n "$ccenv_dumpmachine_switch" ]; then
7a17ab
		ccenv_tmp=$(mktemp ./tmp_XXXXXXXXXXXXXXXX)
7a17ab
		ccenv_cmd="$ccenv_cc --target=$ccenv_host -E -xc -"
7a17ab
7a17ab
		if [ -z "$mb_user_cc" ]; then
7a17ab
			$(printf %s "$ccenv_cmd") < /dev/null > /dev/null \
7a17ab
				2>"$ccenv_tmp" || true
7a17ab
7a17ab
			ccenv_errors=$(cat "$ccenv_tmp")
7a17ab
7a17ab
			if [ -z "$ccenv_errors" ]; then
0dcf74
				ccenv_tool_prolog 'C compiler for host'
7a17ab
				ccenv_tflags="--target=$ccenv_host"
7a17ab
				ccenv_cc="$ccenv_cc $ccenv_tflags"
0dcf74
				ccenv_tool_epilog "$ccenv_cc"
7a17ab
			else
7a17ab
				printf '%s' "$ccenv_errors" >&3
7a17ab
			fi
7a17ab
		fi
7a17ab
7a17ab
		rm -f "$ccenv_tmp"
7a17ab
		unset ccenv_tmp
7a17ab
0dcf74
		ccenv_cchost=$(eval $ccenv_cc $(printf '%s' "$ccenv_cflags") \
0dcf74
			$ccenv_dumpmachine_switch 2>&3)
7a17ab
	fi
7a17ab
7a17ab
	if [ "$ccenv_cchost" != "$ccenv_host" ]; then
7a17ab
		printf 'error!\n' >&2
7a17ab
		printf 'ccenv:\n' >&2
7a17ab
		printf 'ccenv: ccenv_host:   %s \n' $ccenv_host >&2
7a17ab
		printf 'ccenv: ccenv_cchost: %s \n' $ccenv_cchost >&2
7a17ab
7a17ab
		if [ -z "$ccenv_tflags" ]; then
7a17ab
			printf 'ccenv:\n' >&2
7a17ab
			printf 'ccenv: ccenv_host and ccenv_cchost do not match, most likely because:\n' >&2
7a17ab
			printf 'ccenv: (1) you explicitly set CC (or passed --compiler=...)\n' >&2
7a17ab
			printf 'ccenv: (2) the selected compiler does not accept --target=...\n' >&2
7a17ab
			printf 'ccenv: (3) the host reported by -dumpmachine differs from the one you requested.\n' >&2
7a17ab
		fi
7a17ab
7a17ab
		if [ -n "$ccenv_errors" ]; then
7a17ab
			printf 'ccenv:\n' >&2
7a17ab
			printf 'ccenv: something went wrong, see the command and compiler message below.\n\n' >&2
7a17ab
			printf 'cmd: %s < /dev/null > /dev/null\n' "$ccenv_cmd" >&2
7a17ab
			printf '%s\n\n' "$ccenv_errors" >&2
7a17ab
		else
7a17ab
			printf 'ccenv:\n' >&2
7a17ab
			printf 'ccenv: something went wrong, bailing out.\n\n' >&2
7a17ab
		fi
7a17ab
7a17ab
		return 2
7a17ab
	fi
7a17ab
}
7a17ab
7a17ab
ccenv_set_cpp()
7a17ab
{
7a17ab
	ccenv_tool_prolog 'C pre-processor'
7a17ab
7a17ab
	case "$ccenv_cc_cmd" in
7a17ab
		cc | c99 | c11 | gcc)
7a17ab
			ccenv_cpp_prefix=
7a17ab
			ccenv_candidates="cpp" ;;
7a17ab
7a17ab
		clang )
7a17ab
			ccenv_cpp_prefix=
7a17ab
			ccenv_candidates="clang-cpp" ;;
7a17ab
7a17ab
		*-cc )
7a17ab
			ccenv_cpp_prefix=${ccenv_cc_cmd%-cc*}-
7a17ab
			ccenv_candidates="${ccenv_cpp_prefix}cpp" ;;
7a17ab
7a17ab
		*-c99 )
7a17ab
			ccenv_cpp_prefix=${ccenv_cc_cmd%-c99*}-
7a17ab
			ccenv_candidates="${ccenv_cpp_prefix}cpp" ;;
7a17ab
7a17ab
		*-c11 )
7a17ab
			ccenv_cpp_prefix=${ccenv_cc_cmd%-c11*}-
7a17ab
			ccenv_candidates="${ccenv_cpp_prefix}cpp" ;;
7a17ab
7a17ab
		*-gcc )
7a17ab
			ccenv_cpp_prefix=${ccenv_cc_cmd%-gcc*}-
7a17ab
			ccenv_candidates="${ccenv_cpp_prefix}cpp" ;;
7a17ab
7a17ab
		*-clang )
7a17ab
			ccenv_cpp_prefix=${ccenv_cc_cmd%-clang*}-
7a17ab
			ccenv_candidates="${ccenv_cpp_prefix}clang-cpp" ;;
7a17ab
7a17ab
		* )
7a17ab
			ccenv_cpp="$ccenv_cc -E"
7a17ab
			ccenv_tool_epilog "$ccenv_cpp"
7a17ab
			return 0
7a17ab
	esac
7a17ab
7a17ab
	ccenv_find_tool
7a17ab
7a17ab
	if [ "$ccenv_tool" = false ]; then
7a17ab
		ccenv_cpp="$ccenv_cc -E"
7a17ab
	elif [ -n "$ccenv_tflags" ]; then
7a17ab
		ccenv_cpp="$ccenv_tool $ccenv_tflags"
7a17ab
	else
7a17ab
		ccenv_cpp="$ccenv_tool"
7a17ab
	fi
7a17ab
7a17ab
	ccenv_tool_epilog "$ccenv_cpp"
7a17ab
}
7a17ab
7a17ab
ccenv_set_cxx()
7a17ab
{
7a17ab
	ccenv_tool_prolog 'C++ compiler'
7a17ab
7a17ab
	case "$ccenv_cc_cmd" in
7a17ab
		cc | c99 | c11 )
7a17ab
			ccenv_cxx_prefix=
7a17ab
			ccenv_candidates="cxx c++" ;;
7a17ab
7a17ab
		gcc )
7a17ab
			ccenv_cxx_prefix=
7a17ab
			ccenv_candidates="g++" ;;
7a17ab
7a17ab
		clang )
7a17ab
			ccenv_cxx_prefix=
7a17ab
			ccenv_candidates="clang++" ;;
7a17ab
7a17ab
		*-gcc )
f6f684
			ccenv_cxx_prefix=${ccenv_cc_cmd%-gcc*}-
f6f684
			ccenv_candidates="${ccenv_cxx_prefix}g++" ;;
7a17ab
7a17ab
		*-clang )
f6f684
			ccenv_cxx_prefix=${ccenv_cc_cmd%-clang*}-
f6f684
			ccenv_candidates="${ccenv_cxx_prefix}clang++" ;;
f6f684
f6f684
		/*cc | /*c99 | /*c11 )
f6f684
			ccenv_cxx_prefix=${ccenv_cc_cmd%/*}
f6f684
			ccenv_candidates="${ccenv_cxx_prefix}/cxx"
f6f684
			ccenv_candidates="${ccenv_candidates} ${ccenv_cxx_prefix}/c++" ;;
f6f684
f6f684
		/*gcc )
f6f684
			ccenv_cxx_prefix=${ccenv_cc_cmd%/*}
f6f684
			ccenv_candidates="${ccenv_cxx_prefix}/g++" ;;
7a17ab
f6f684
		/*clang )
f6f684
			ccenv_cxx_prefix=${ccenv_cc_cmd%/*}
f6f684
			ccenv_candidates="${ccenv_cxx_prefix}/clang++" ;;
7a17ab
7a17ab
		* )
7a17ab
			ccenv_cxx="$ccenv_cc -xc++"
7a17ab
			ccenv_tool_epilog "$ccenv_cxx"
7a17ab
			return 0
7a17ab
	esac
7a17ab
7a17ab
	ccenv_find_tool
7a17ab
7a17ab
	if [ "$ccenv_tool" = false ]; then
7a17ab
		ccenv_cxx="$ccenv_cc -xc++"
7a17ab
	elif [ -n "$ccenv_tflags" ]; then
7a17ab
		ccenv_cxx="$ccenv_tool $ccenv_tflags"
7a17ab
	else
7a17ab
		ccenv_cxx="$ccenv_tool"
7a17ab
	fi
7a17ab
7a17ab
	ccenv_tool_epilog "$ccenv_cxx"
7a17ab
}
7a17ab
7a17ab
ccenv_set_cc_host()
7a17ab
{
7a17ab
	ccenv_attr_prolog 'system'
7a17ab
	ccenv_cc_host="$ccenv_cchost"
7a17ab
	ccenv_attr_epilog "$ccenv_cc_host"
7a17ab
}
7a17ab
7a17ab
ccenv_set_cc_bits()
7a17ab
{
7a17ab
	ccenv_attr_prolog 'bits'
7a17ab
7a17ab
	ccenv_internal_size=
7a17ab
	ccenv_internal_type='void *'
7a17ab
	ccenv_internal_test='char x[(sizeof(%s) == %s/8) ? 1 : -1];'
7a17ab
7a17ab
	for ccenv_internal_guess in 64 32 128; do
d04d83
		if [ -z "${ccenv_internal_size:-}" ]; then
7a17ab
			ccenv_internal_str=$(printf "$ccenv_internal_test"  \
7a17ab
				"$ccenv_internal_type"                      \
7a17ab
				"$ccenv_internal_guess")
7a17ab
bfd5d9
			if [ -n "$ccenv_dumpmachine_switch" ]; then
bfd5d9
				printf '%s' "$ccenv_internal_str"                   \
bfd5d9
						| eval $ccenv_cc -S -xc - -o -      \
bfd5d9
						  $(printf '%s' "$ccenv_cflags")    \
bfd5d9
					> /dev/null 2>&3                            \
bfd5d9
				&& ccenv_internal_size=$ccenv_internal_guess
bfd5d9
			else
bfd5d9
				ccenv_tmpname='ccenv/c3RyaWN0X21vZGUK.c'
bfd5d9
bfd5d9
				printf '%s' "$ccenv_internal_str" \
bfd5d9
					> "$ccenv_tmpname"
bfd5d9
bfd5d9
				$ccenv_cc -c "$ccenv_tmpname" -o a.out \
bfd5d9
					> /dev/null 2>&3                \
bfd5d9
				&& ccenv_internal_size=$ccenv_internal_guess
bfd5d9
bfd5d9
				rm "$ccenv_tmpname"
bfd5d9
			fi
7a17ab
		fi
7a17ab
	done
7a17ab
7a17ab
	ccenv_cc_bits=$ccenv_internal_size
7a17ab
7a17ab
	ccenv_attr_epilog "$ccenv_cc_bits"
7a17ab
}
7a17ab
7a17ab
ccenv_set_cc_underscore()
7a17ab
{
7a17ab
	ccenv_attr_prolog 'prepended underscores'
7a17ab
7a17ab
	ccenv_fn_name='ZmYaXyWbVe_UuTnSdReQrPsOcNoNrLe'
7a17ab
	ccenv_fn_code='int %s(void){return 0;}'
7a17ab
f3f5bb
	ccenv_tmpname='ccenv/c3RyaWN0X21vZGUK.c'
f3f5bb
f3f5bb
	printf "$ccenv_fn_code" $ccenv_fn_name \
f3f5bb
		> "$ccenv_tmpname"
f3f5bb
f3f5bb
	$ccenv_cc -c "$ccenv_tmpname" -o a.out \
f3f5bb
		> /dev/null 2>&3
f3f5bb
f3f5bb
	if "$ccenv_nm" a.out | grep          \
f3f5bb
			-e "^_$ccenv_fn_name" \
f3f5bb
			-e " _$ccenv_fn_name"  \
f3f5bb
			> /dev/null; then
7a17ab
		ccenv_cc_underscore='_'
7a17ab
		ccenv_attr_epilog 'yes'
f3f5bb
	else
f3f5bb
		ccenv_attr_epilog 'no'
7a17ab
	fi
7a17ab
f3f5bb
	rm "$ccenv_tmpname"
f3f5bb
	rm a.out
7a17ab
7a17ab
	return 0
7a17ab
}
7a17ab
7a17ab
ccenv_create_framework_executable()
7a17ab
{
d279f3
	if [ "$ccenv_cfgtype" = 'host' ]; then
d279f3
		if [ "$mb_freestanding" = 'yes' ]; then
d279f3
			return 1
d279f3
		fi
d279f3
	fi
d279f3
7a17ab
	if [ -f $ccenv_image ]; then
7a17ab
		mv $ccenv_image $ccenv_image.tmp
7a17ab
		rm -f $ccenv_image.tmp
7a17ab
	fi
7a17ab
cea86b
	ccenv_tmpname='ccenv/c3RyaWN0X21vZGUK.c'
cea86b
7a17ab
	printf 'int main(void){return 0;}'  \
cea86b
		> "$ccenv_tmpname"
7a17ab
cea86b
	if $ccenv_cc "$ccenv_tmpname" -o $ccenv_image 2>&3; then
cea86b
		ccenv_ret=0
d279f3
		ccenv_cc_environment='hosted'
cea86b
	else
cea86b
		ccenv_ret=1
cea86b
	fi
cea86b
cea86b
	rm "$ccenv_tmpname"
cea86b
cea86b
	return $ccenv_ret
7a17ab
}
7a17ab
7a17ab
ccenv_create_freestanding_executable()
7a17ab
{
7a17ab
	if [ -f $ccenv_image ]; then
7a17ab
		mv $ccenv_image $ccenv_image.tmp
7a17ab
		rm -f $ccenv_image.tmp
7a17ab
	fi
7a17ab
7a17ab
	if [ -z "ccenv_cc_underscore" ]; then
7a17ab
		ccenv_start_fn='_start'
7a17ab
	else
7a17ab
		ccenv_start_fn='start'
7a17ab
	fi
7a17ab
cea86b
	ccenv_tmpname='ccenv/c3RyaWN0X21vZGUK.c'
7a17ab
cea86b
	printf 'int %s(void){return 0;}' "$ccenv_start_fn" \
cea86b
		> "$ccenv_tmpname"
7a17ab
cea86b
	if $ccenv_cc "$ccenv_tmpname"         \
cea86b
			-ffreestanding         \
cea86b
			-nostdlib -nostartfiles \
cea86b
			-o $ccenv_image          \
cea86b
			2>&3; then
cea86b
		ccenv_ret=0
d279f3
		ccenv_cc_environment='freestanding'
cea86b
	else
cea86b
		ccenv_ret=1
cea86b
	fi
cea86b
cea86b
	rm "$ccenv_tmpname"
cea86b
cea86b
	return $ccenv_ret
7a17ab
}
7a17ab
7a17ab
ccenv_set_cc_binfmt_error()
7a17ab
{
7a17ab
	ccenv_attr_epilog '(unable to create executable)'
7a17ab
}
7a17ab
7a17ab
ccenv_set_cc_binfmt()
7a17ab
{
7a17ab
	ccenv_use_perk=
7a17ab
	ccenv_use_otool=
7a17ab
	ccenv_use_readelf=
7a17ab
	ccenv_use_readobj=
7a17ab
	ccenv_use_bfd_objdump=
7a17ab
	ccenv_use_llvm_objdump=
7a17ab
7a17ab
	ccenv_attr_prolog 'binary format'
7a17ab
7a17ab
	ccenv_create_framework_executable               \
7a17ab
		|| ccenv_create_freestanding_executable \
7a17ab
		|| ccenv_set_cc_binfmt_error            \
7a17ab
		|| return 0
7a17ab
7a17ab
	# PE / perk
7a17ab
	if [ -n "$ccenv_perk" ]; then
7a17ab
		if $ccenv_perk $ccenv_image 2>&3; then
7a17ab
			ccenv_cc_binfmt='PE'
7a17ab
			ccenv_use_perk=yes
7a17ab
		fi
7a17ab
	fi
7a17ab
7a17ab
	# ELF / readelf
7a17ab
	if [ -n "$ccenv_readelf" ] && [ -z "$ccenv_cc_binfmt" ]; then
7a17ab
		if $ccenv_readelf -h $ccenv_image 2>&3               \
7a17ab
				| grep 'Magic:' | sed -e 's/[ ]*//g' \
7a17ab
				| grep 'Magic:7f454c46'              \
7a17ab
					> /dev/null; then
7a17ab
			ccenv_cc_binfmt='ELF'
7a17ab
			ccenv_use_readelf=yes
7a17ab
		fi
7a17ab
	fi
7a17ab
7a17ab
	# a marble of astonishing design:
7a17ab
	# llvm-readelf also parses PE and Mach-O
7a17ab
7a17ab
	if [ -n "$ccenv_readelf_llvm" ]; then
7a17ab
		ccenv_readany="$ccenv_readelf_llvm"
7a17ab
	else
7a17ab
		ccenv_readany="$ccenv_readelf"
7a17ab
	fi
7a17ab
7a17ab
	# PE / readelf
7a17ab
	if [ -n "$ccenv_readany" ] && [ -z "$ccenv_cc_binfmt" ]; then
7a17ab
		if $ccenv_readany -h $ccenv_image 2>&3               \
7a17ab
				| grep 'Magic:' | sed -e 's/[ ]*//g' \
7a17ab
				| grep 'Magic:MZ'                    \
7a17ab
					> /dev/null; then
7a17ab
			ccenv_cc_binfmt='PE'
7a17ab
			ccenv_use_readelf=yes
7a17ab
		fi
7a17ab
	fi
7a17ab
7a17ab
	# MACHO-64 / otool
7a17ab
	if [ -n "$ccenv_otool" ] && [ -z "$ccenv_cc_binfmt" ]; then
7a17ab
		if $ccenv_otool -hv $ccenv_image 2>&3        \
7a17ab
				| grep -i 'MH_MAGIC_64'      \
7a17ab
					> /dev/null; then
7a17ab
			ccenv_cc_binfmt='MACHO'
7a17ab
			ccenv_use_otool=yes
7a17ab
		fi
7a17ab
	fi
7a17ab
7a17ab
	# MACHO-32 / otool
7a17ab
	if [ -n "$ccenv_otool" ] && [ -z "$ccenv_cc_binfmt" ]; then
7a17ab
		if $ccenv_otool -hv $ccenv_image 2>&3        \
7a17ab
				| grep -i 'MH_MAGIC'         \
7a17ab
					> /dev/null; then
7a17ab
			ccenv_cc_binfmt='MACHO'
7a17ab
			ccenv_use_otool=yes
7a17ab
		fi
7a17ab
	fi
7a17ab
7a17ab
	# MACHO-64 / readelf
7a17ab
	if [ -n "$ccenv_readany" ] && [ -z "$ccenv_cc_binfmt" ]; then
7a17ab
		if $ccenv_readany -h $ccenv_image 2>&3                    \
7a17ab
				| grep -i 'Magic:' | sed -e 's/[ ]*//g'   \
7a17ab
				| grep -i '(0xfeedfacf)'                  \
7a17ab
					> /dev/null; then
7a17ab
			ccenv_cc_binfmt='MACHO'
7a17ab
			ccenv_use_readelf=yes
7a17ab
		fi
7a17ab
	fi
7a17ab
7a17ab
	# MACHO-32 / readelf
7a17ab
	if [ -n "$ccenv_readany" ] && [ -z "$ccenv_cc_binfmt" ]; then
7a17ab
		if $ccenv_readany -h $ccenv_image 2>&3                    \
7a17ab
				| grep -i 'Magic:' | sed -e 's/[ ]*//g'   \
7a17ab
				| grep -i '(0xcafebabe)'                  \
7a17ab
					> /dev/null; then
7a17ab
			ccenv_cc_binfmt='MACHO'
7a17ab
			ccenv_use_readelf=yes
7a17ab
		fi
7a17ab
	fi
7a17ab
7a17ab
	# MACHO / readobj
7a17ab
	if [ -n "$ccenv_readobj" ] && [ -z "$ccenv_cc_binfmt" ]; then
7a17ab
		if $ccenv_readobj $ccenv_image 2>&3                  \
f14c2e
				| grep -i 'Format:'                  \
f14c2e
				| sed  -e 's/ /_/g'                  \
7a17ab
				| grep -i '_Mach-O_'                 \
7a17ab
					> /dev/null; then
7a17ab
			ccenv_cc_binfmt='MACHO'
7a17ab
			ccenv_use_readobj=yes
7a17ab
		fi
7a17ab
	fi
7a17ab
7a17ab
	# MACHO / objdump (llvm)
7a17ab
	if [ -n "$ccenv_objdump" ] && [ -z "$ccenv_cc_binfmt" ]; then
7a17ab
		if $ccenv_objdump -section-headers $ccenv_image  \
7a17ab
					2>&3                     \
7a17ab
				| grep -i 'file format Mach-O'   \
7a17ab
					> /dev/null; then
7a17ab
			ccenv_cc_binfmt='MACHO'
7a17ab
			ccenv_use_objdump=yes
7a17ab
		fi
7a17ab
	fi
7a17ab
7a17ab
	# MACHO / objdump (bfd)
7a17ab
	if [ -n "$ccenv_objdump" ] && [ -z "$ccenv_cc_binfmt" ]; then
7a17ab
		$ccenv_objdump -h  $ccenv_image 2>&3              \
7a17ab
			| grep -i 'file format Mach-O'            \
7a17ab
				> /dev/null                       \
7a17ab
		&& ccenv_cc_binfmt='MACHO'                        \
7a17ab
		&& ccenv_use_objdump=yes
7a17ab
	fi
7a17ab
7a17ab
	# PE / objdump (bfd)
7a17ab
	if [ -n "$ccenv_objdump" ] && [ -z "$ccenv_cc_binfmt" ]; then
7a17ab
		if $ccenv_objdump -h  $ccenv_image 2>&3        \
7a17ab
				| grep -i 'file format pei-'   \
7a17ab
					> /dev/null; then
7a17ab
			ccenv_cc_binfmt='PE'
7a17ab
			ccenv_use_bfd_objdump=yes
7a17ab
		fi
7a17ab
	fi
7a17ab
7a17ab
	ccenv_attr_epilog "$ccenv_cc_binfmt"
7a17ab
}
7a17ab
7a17ab
ccenv_set_os_pe()
7a17ab
{
d279f3
	if [ "$ccenv_cc_environment" = 'freestanding' ]; then
7a17ab
		case "$ccenv_cchost" in
7a17ab
			*-midipix | *-midipix-* )
7a17ab
				ccenv_os='midipix' ;;
7a17ab
			*-mingw | *-mingw32 | *-mingw64 )
7a17ab
				ccenv_os='mingw' ;;
7a17ab
			*-mingw-* | *-mingw32-* | *-mingw64 )
7a17ab
				ccenv_os='mingw' ;;
7a17ab
			*-msys | *-msys2 | *-msys-* | *-msys2-* )
7a17ab
				ccenv_os='msys' ;;
7a17ab
			*-cygwin | *-cygwin-* )
7a17ab
				ccenv_os='cygwin' ;;
7a17ab
		esac
7a17ab
	fi
7a17ab
7a17ab
	if [ -n "$ccenv_os" ]; then
7a17ab
		return 0
7a17ab
	fi
7a17ab
7a17ab
	if [ -n "$ccenv_use_perk" ]; then
7a17ab
		ccenv_framework=$($ccenv_perk -y $ccenv_image)
7a17ab
		ccenv_os=${ccenv_framework#*-*-*-*}
7a17ab
	fi
7a17ab
7a17ab
	if [ -z "$ccenv_os" ] && [ -n "$ccenv_objdump_bfd" ]; then
7a17ab
		$ccenv_objdump_bfd -x $ccenv_image | grep -i 'DLL Name' \
7a17ab
			| grep 'cygwin1.dll' > /dev/null                \
7a17ab
		&& ccenv_os='cygwin'
7a17ab
	fi
7a17ab
7a17ab
	if [ -z "$ccenv_os" ] && [ -n "$ccenv_objdump_bfd" ]; then
7a17ab
		$ccenv_objdump_bfd -x $ccenv_image | grep -i 'DLL Name' \
7a17ab
			| grep 'msys-2.0.dll' > /dev/null               \
7a17ab
		&& ccenv_os='msys'
7a17ab
	fi
7a17ab
7a17ab
	if [ -z "$ccenv_os" ] && [ -n "$ccenv_objdump_bfd" ]; then
7a17ab
		$ccenv_objdump_bfd -x $ccenv_image          \
7a17ab
			| grep -i 'DLL Name' | grep '.CRT'  \
7a17ab
				> /dev/null                 \
7a17ab
		&& $ccenv_objdump_bfd -x $ccenv_image       \
7a17ab
			| grep -i 'DLL Name' | grep '.bss'  \
7a17ab
				> /dev/null                 \
7a17ab
		&& $ccenv_objdump_bfd -x $ccenv_image       \
7a17ab
			| grep -i 'DLL Name' | grep '.tls'  \
7a17ab
				> /dev/null                 \
7a17ab
		&& ccenv_os='mingw'
7a17ab
	fi
7a17ab
}
7a17ab
7a17ab
ccenv_set_os_macho()
7a17ab
{
7a17ab
	case "$ccenv_cchost" in
7a17ab
		*-apple-darwin* )
7a17ab
			ccenv_os='darwin' ;;
7a17ab
	esac
7a17ab
}
7a17ab
7a17ab
ccenv_set_os()
7a17ab
{
7a17ab
	ccenv_attr_prolog 'os name'
7a17ab
7a17ab
	case "$ccenv_cc_binfmt" in
7a17ab
		PE )
7a17ab
			ccenv_set_os_pe ;;
7a17ab
		MACHO )
7a17ab
			ccenv_set_os_macho ;;
7a17ab
	esac
7a17ab
7a17ab
	if [ -n "$ccenv_os" ]; then
7a17ab
		ccenv_attr_epilog "$ccenv_os"
7a17ab
		return 0
7a17ab
	fi
7a17ab
7a17ab
	case "$ccenv_cchost" in
7a17ab
		*-*-*-* )
7a17ab
			ccenv_tip=${ccenv_cchost%-*}
7a17ab
			ccenv_os=${ccenv_tip#*-*-}
7a17ab
			;;
7a17ab
		*-*-musl | *-*-gnu )
7a17ab
			ccenv_tip=${ccenv_cchost%-*}
7a17ab
			ccenv_os=${ccenv_tip#*-}
7a17ab
			;;
7a17ab
		*-*-* )
7a17ab
			ccenv_os=${ccenv_cchost#*-*-}
7a17ab
			;;
7a17ab
		*-* )
7a17ab
			ccenv_os=${ccenv_cchost#*-}
7a17ab
			;;
7a17ab
	esac
7a17ab
7a17ab
	if [ -z "$ccenv_os" ]; then
7a17ab
		ccenv_os='anyos'
7a17ab
	fi
7a17ab
7a17ab
	ccenv_attr_epilog "$ccenv_os"
7a17ab
}
7a17ab
7a17ab
ccenv_set_os_flags()
7a17ab
{
7a17ab
	case "$ccenv_os" in
7a17ab
		darwin )
7a17ab
			ccenv_cflags_os='-D_DARWIN_C_SOURCE'
7a17ab
			ccenv_cflags_pic='-fPIC'
7a17ab
			;;
7a17ab
		midipix )
7a17ab
			ccenv_cflags_os=
7a17ab
			ccenv_cflags_pic='-fPIC'
7a17ab
			;;
7a17ab
		cygwin )
7a17ab
			ccenv_cflags_os=
7a17ab
			ccenv_cflags_pic=
7a17ab
			;;
7a17ab
		msys | msys* | mingw | mingw* )
7a17ab
			ccenv_cflags_os='-U__STRICT_ANSI__'
7a17ab
			ccenv_cflags_pic=
7a17ab
			;;
7a17ab
		* )
7a17ab
			ccenv_cflags_os=
7a17ab
			ccenv_cflags_pic='-fPIC'
7a17ab
			;;
7a17ab
	esac
7a17ab
}
7a17ab
7a17ab
ccenv_set_os_semantics()
7a17ab
{
7a17ab
	# binary_format - core_api - ex_api - dependency_resolution
7a17ab
7a17ab
	ccenv_attr_prolog 'os semantics'
7a17ab
7a17ab
	case "$ccenv_os" in
7a17ab
		linux )
7a17ab
			ccenv_os_semantics='elf-posix-linux-ldso'
7a17ab
			;;
7a17ab
		bsd )
7a17ab
			ccenv_os_semantics='elf-posix-bsd-ldso'
7a17ab
			;;
7a17ab
		darwin )
7a17ab
			ccenv_os_semantics='macho-posix-osx-ldso'
7a17ab
			;;
7a17ab
		midipix )
7a17ab
			ccenv_os_semantics='pe-posix-winnt-ldso'
7a17ab
			;;
7a17ab
		cygwin )
7a17ab
			ccenv_os_semantics='pe-hybrid-winnt-unsafe'
7a17ab
			;;
7a17ab
		msys )
7a17ab
			ccenv_os_semantics='pe-hybrid-winnt-unsafe'
7a17ab
			;;
7a17ab
		mingw )
7a17ab
			ccenv_os_semantics='pe-win32-winnt-unsafe'
7a17ab
			;;
7a17ab
	esac
7a17ab
7a17ab
	if [ -n "$ccenv_os_semantics" ]; then
7a17ab
		ccenv_attr_epilog "$ccenv_os_semantics"
7a17ab
		return 0
7a17ab
	fi
7a17ab
7a17ab
	if [ -n "$ccenv_cc_binfmt" ]; then
7a17ab
		ccenv_os_semantics_pattern='%s-posix-anyos-unknown'
7a17ab
		ccenv_os_semantics=$(printf                   \
7a17ab
				"$ccenv_os_semantics_pattern"  \
7a17ab
				"$ccenv_cc_binfmt"              \
7a17ab
			| tr '[:upper:]' '[:lower:]')
7a17ab
	else
7a17ab
		ccenv_os_semantics='unknown-posix-anyos-unknown'
7a17ab
	fi
7a17ab
7a17ab
	ccenv_attr_epilog "$ccenv_os_semantics"
7a17ab
}
7a17ab
fe1010
ccenv_set_os_dso_format()
fe1010
{
fe1010
	ccenv_attr_prolog 'os dso format'
fe1010
fe1010
	case "$ccenv_cc_arfmt" in
fe1010
		common )
fe1010
			ccenv_cc_sofmt="$ccenv_cc_binfmt"
fe1010
			;;
fe1010
fe1010
		bigaf )
fe1010
			ccenv_libgcc_s_a_header=$(od -b -N8             \
fe1010
				$($ccenv_cc -print-file-name=libgcc_s.a) \
fe1010
					2>/dev/null                       \
fe1010
					| head -n1)
fe1010
fe1010
			ccenv_libgcc_s_so_header=$(od -b -N8             \
fe1010
				$($ccenv_cc -print-file-name=libgcc_s.so) \
fe1010
					2>/dev/null                        \
fe1010
					| head -n1)
fe1010
fe1010
			if [ "$ccenv_libgcc_s_a_header" = "$ccenv_bigaf_header" ]; then
fe1010
				ccenv_cc_sofmt='bigaf'
fe1010
			elif [ "$ccenv_libgcc_s_so_header" = "$ccenv_bigaf_header" ]; then
fe1010
				ccenv_cc_sofmt='bigaf'
fe1010
			else
fe1010
				ccenv_cc_sofmt="$ccenv_cc_binfmt"
fe1010
			fi
fe1010
			;;
fe1010
fe1010
		aiaff )
fe1010
			ccenv_libgcc_s_a_header=$(od -b -N8               \
fe1010
				$($ccenv_cc -print-file-name=libgcc_s.a) \
fe1010
					| head -n1)
fe1010
fe1010
			ccenv_libgcc_s_so_header=$(od -b -N8               \
fe1010
				$($ccenv_cc -print-file-name=libgcc_s.so) \
fe1010
					| head -n1)
fe1010
fe1010
			if [ "$ccenv_libgcc_s_a_header" = "$ccenv_aiaff_header" ]; then
fe1010
				ccenv_cc_sofmt='aiaff'
fe1010
			elif [ "$ccenv_libgcc_s_so_header" = "$ccenv_aiaff_header" ]; then
fe1010
				ccenv_cc_sofmt='aiaff'
fe1010
			else
fe1010
				ccenv_cc_sofmt="$ccenv_cc_binfmt"
fe1010
			fi
fe1010
			;;
fe1010
	esac
fe1010
88c602
	if [ "$ccenv_cfgtype" = 'host' ]; then
88c602
		case "$ccenv_cc_sofmt" in
88c602
			bigaf | aiaff )
88c602
				mb_shared_lib_cmd='$(AR) -rcs'
88c602
				mb_shared_lib_ldflags=
88c602
				;;
88c602
88c602
			* )
88c602
				mb_shared_lib_cmd='$(CC) -shared -o'
88c602
				mb_shared_lib_ldflags='$(LDFLAGS_SHARED)'
88c602
				;;
88c602
		esac
88c602
	fi
88c602
fe1010
	ccenv_attr_epilog "$ccenv_cc_sofmt"
fe1010
}
fe1010
7a17ab
ccenv_set_os_dso_exrules()
7a17ab
{
7a17ab
	ccenv_attr_prolog 'os dso exrules'
7a17ab
7a17ab
	case "$ccenv_os" in
7a17ab
		midipix )
7a17ab
			ccenv_os_dso_exrules='pe-mdso'
7a17ab
			;;
7a17ab
		* )
7a17ab
			if [ "$ccenv_cc_binfmt" = 'PE' ]; then
7a17ab
				ccenv_os_dso_exrules='pe-dlltool'
7a17ab
			else
7a17ab
				ccenv_os_dso_exrules='default'
7a17ab
			fi
7a17ab
	esac
7a17ab
7a17ab
	ccenv_attr_epilog "$ccenv_os_dso_exrules"
7a17ab
}
7a17ab
7a17ab
ccenv_set_os_dso_linkage()
7a17ab
{
7a17ab
	# todo: PIC, PIE, and friends
7a17ab
	ccenv_attr_prolog 'os linkage'
7a17ab
	ccenv_os_dso_linkage='default'
7a17ab
	ccenv_attr_epilog "$ccenv_os_dso_linkage"
7a17ab
}
7a17ab
7a17ab
ccenv_set_os_dso_patterns_darwin()
7a17ab
{
7a17ab
	ccenv_os_app_prefix=
7a17ab
	ccenv_os_app_suffix=
7a17ab
7a17ab
	ccenv_os_lib_prefix=lib
7a17ab
	ccenv_os_lib_suffix=.dylib
7a17ab
7a17ab
	ccenv_os_implib_ext=.invalid
7a17ab
	ccenv_os_libdef_ext=.invalid
7a17ab
7a17ab
	ccenv_os_archive_ext=.a
7a17ab
	ccenv_os_soname=symlink
7a17ab
7a17ab
	ccenv_os_lib_prefixed_suffix=
7a17ab
	ccenv_os_lib_suffixed_suffix='$(OS_LIB_SUFFIX)'
7a17ab
}
7a17ab
7a17ab
ccenv_set_os_dso_patterns_mdso()
7a17ab
{
7a17ab
	ccenv_os_app_prefix=
7a17ab
	ccenv_os_app_suffix=
7a17ab
7a17ab
	ccenv_os_lib_prefix=lib
7a17ab
	ccenv_os_lib_suffix=.so
7a17ab
7a17ab
	ccenv_os_implib_ext=.lib.a
7a17ab
	ccenv_os_libdef_ext=.so.def
7a17ab
7a17ab
	ccenv_os_archive_ext=.a
7a17ab
	ccenv_os_soname=symlink
7a17ab
7a17ab
	ccenv_os_lib_prefixed_suffix='$(OS_LIB_SUFFIX)'
7a17ab
	ccenv_os_lib_suffixed_suffix=
7a17ab
}
7a17ab
7a17ab
ccenv_set_os_dso_patterns_dlltool()
7a17ab
{
7a17ab
	ccenv_os_app_prefix=
7a17ab
	ccenv_os_app_suffix=.exe
7a17ab
7a17ab
	ccenv_os_lib_prefix=lib
7a17ab
	ccenv_os_lib_suffix=.dll
7a17ab
7a17ab
	ccenv_os_implib_ext=.dll.a
7a17ab
	ccenv_os_libdef_ext=.def
7a17ab
7a17ab
	ccenv_os_archive_ext=.a
7a17ab
	ccenv_os_soname=copy
7a17ab
7a17ab
	ccenv_os_lib_prefixed_suffix='$(OS_LIB_SUFFIX)'
7a17ab
	ccenv_os_lib_suffixed_suffix=
7a17ab
}
7a17ab
7a17ab
ccenv_set_os_dso_patterns_default()
7a17ab
{
7a17ab
	ccenv_os_app_prefix=
7a17ab
	ccenv_os_app_suffix=
7a17ab
7a17ab
	ccenv_os_lib_prefix=lib
7a17ab
	ccenv_os_lib_suffix=.so
7a17ab
7a17ab
	ccenv_os_implib_ext=.invalid
7a17ab
	ccenv_os_libdef_ext=.invalid
7a17ab
7a17ab
	ccenv_os_archive_ext=.a
7a17ab
	ccenv_os_soname=symlink
7a17ab
7a17ab
	ccenv_os_lib_prefixed_suffix='$(OS_LIB_SUFFIX)'
7a17ab
	ccenv_os_lib_suffixed_suffix=
7a17ab
}
7a17ab
7a17ab
ccenv_set_os_dso_patterns()
7a17ab
{
7a17ab
	# sover: .so.x.y.z
7a17ab
	# verso: .x.y.z.so
7a17ab
7a17ab
	case "$ccenv_os" in
7a17ab
		darwin )
7a17ab
			ccenv_set_os_dso_patterns_darwin
7a17ab
			;;
7a17ab
		midipix )
7a17ab
			ccenv_set_os_dso_patterns_mdso
7a17ab
			;;
7a17ab
		cygwin | msys | mingw )
7a17ab
			ccenv_set_os_dso_patterns_dlltool
7a17ab
			;;
7a17ab
		* )
7a17ab
			ccenv_set_os_dso_patterns_default
7a17ab
			;;
7a17ab
	esac
7a17ab
}
7a17ab
7a17ab
ccenv_set_os_pe_switches()
7a17ab
{
7a17ab
	if [ "$ccenv_cc_binfmt" = 'PE' ] && [ -z "$ccenv_pe_subsystem" ]; then
7a17ab
		case "$ccenv_os" in
7a17ab
			midipix | mingw )
7a17ab
				ccenv_pe_subsystem='windows'
7a17ab
				;;
7a17ab
			* )
7a17ab
				ccenv_pe_subsystem='console'
7a17ab
				;;
7a17ab
		esac
7a17ab
	fi
7a17ab
}
7a17ab
7a17ab
ccenv_output_defs()
7a17ab
{
7a17ab
	ccenv_in="$mb_project_dir/sofort/ccenv/ccenv.in"
7a17ab
	ccenv_mk="$mb_pwd/ccenv/$ccenv_cfgtype.mk"
7a17ab
	ccenv_tmp=
7a17ab
7a17ab
	if [ "$ccenv_cc_binfmt" = 'PE' ]; then
7a17ab
		ccenv_pe="$mb_project_dir/sofort/ccenv/pedefs.in"
7a17ab
		ccenv_in="$ccenv_in $ccenv_pe"
7a17ab
	fi
7a17ab
7a17ab
	if [ $ccenv_cfgtype = 'native' ]; then
7a17ab
7a17ab
		ccenv_tmp=$(mktemp ./tmp_XXXXXXXXXXXXXXXX)
7a17ab
16abfa
		sed                                      \
9e0ce1
				-e 's/^[[:space:]]*$/@/g' \
16abfa
				-e 's/^/NATIVE_/'          \
16abfa
				-e 's/NATIVE_@//g'          \
16abfa
				-e 's/NATIVE_#/#/g'          \
16abfa
				-e 's/       =/=/g'           \
16abfa
				-e 's/       +=/+=/g'          \
37f518
			$(printf '%s ' $ccenv_in)               \
37f518
				> "$ccenv_tmp"
7a17ab
7a17ab
		ccenv_in="$ccenv_tmp"
7a17ab
	else
7a17ab
		unset ccenv_tmp
7a17ab
	fi
7a17ab
d27521
	ccenv_var_defs=
d27521
	ccenv_sed_substs=
d27521
7a17ab
	ccenv_vars=$(cut -d'=' -f1 "$mb_project_dir/sofort/ccenv/ccenv.vars" \
d27521
		| grep -v '^#');
7a17ab
7a17ab
	ccenv_exvars="ccenv_cfgtype ccenv_makevar_prefix"
7a17ab
d27521
	for __var in $(printf '%s' "$ccenv_vars $ccenv_exvars"); do
d27521
		ccenv_sed_subst=$(printf '%s %s%s%s' \
d27521
				'-e' "'s^@$__var@"    \
d27521
				"^___${__var}___"      \
d27521
				"^g'")
d27521
d27521
		ccenv_sed_substs="$ccenv_sed_substs $ccenv_sed_subst"
d27521
d27521
		ccenv_var_def=$(printf '%s%s="${%s}"' "-D" "___${__var}___" "${__var}")
d27521
		eval ccenv_var_defs='"$ccenv_var_defs "$ccenv_var_def'
d27521
	done
7a17ab
d27521
	eval sed $ccenv_sed_substs $(printf '%s ' $ccenv_in) \
d27521
			| eval m4 $ccenv_var_defs -           \
d27521
			| sed -e 's/[[:blank:]]*$//g'          \
7a17ab
		> "$ccenv_mk"
7a17ab
7a17ab
	if [ "$ccenv_cfgtype" = 'host' ]; then
7a17ab
		for __var in $(printf '%s' "$ccenv_vars"); do
7a17ab
			ccenv_src_var=$__var
7a17ab
			ccenv_dst_var=mb_${__var#*ccenv_}
7a17ab
			ccenv_var_expr='${'$ccenv_src_var':-}'
7a17ab
			eval $ccenv_dst_var=$ccenv_var_expr
7a17ab
7a17ab
		done
7a17ab
7a17ab
		mb_host=$ccenv_host
7a17ab
		mb_cchost=$ccenv_cchost
7a17ab
	else
7a17ab
		for __var in $(printf '%s' "$ccenv_vars"); do
7a17ab
			ccenv_src_var=$__var
7a17ab
			ccenv_dst_var=mb_native_${__var#*ccenv_}
7a17ab
			ccenv_var_expr='${'$ccenv_src_var':-}'
7a17ab
			eval "$ccenv_dst_var=$ccenv_var_expr"
7a17ab
		done
7a17ab
7a17ab
		mb_native_host=$ccenv_host
7a17ab
		mb_native_cchost=$ccenv_cchost
7a17ab
	fi
7a17ab
7a17ab
	if [ -n "${ccenv_tmp:-}" ]; then
7a17ab
		rm -f "$ccenv_tmp"
7a17ab
		unset ccenv_tmp
7a17ab
	fi
7a17ab
}
7a17ab
7a17ab
ccenv_dso_verify()
7a17ab
{
7a17ab
	ccenv_str='int foo(int x){return ++x;}'
7a17ab
	ccenv_cmd="$ccenv_cc -xc - -shared -o a.out"
7a17ab
7a17ab
	rm -f a.out
7a17ab
7a17ab
	printf '%s' "$ccenv_str" | $(printf %s "$ccenv_cmd") \
7a17ab
		> /dev/null 2>&3              \
7a17ab
	|| mb_disable_shared=yes
7a17ab
7a17ab
	rm -f a.out
7a17ab
}
7a17ab
7a17ab
ccenv_clean_up()
7a17ab
{
7a17ab
	rm -f $ccenv_image
7a17ab
}
7a17ab
7a17ab
ccenv_common_init()
7a17ab
{
7a17ab
	. "$mb_project_dir/sofort/ccenv/ccenv.vars"
7a17ab
7a17ab
	ccenv_cfgtype=$1
7a17ab
	ccenv_cfgfile="$mb_pwd/ccenv/$ccenv_cfgtype.mk"
7a17ab
	ccenv_cchost=
7a17ab
7a17ab
	if [ $ccenv_cfgtype = 'native' ]; then
7a17ab
		ccenv_makevar_prefix='NATIVE_'
7a17ab
		ccenv_image='./ccenv/native.a.out'
7a17ab
	else
7a17ab
		ccenv_makevar_prefix=
7a17ab
		ccenv_image='./ccenv/host.a.out'
7a17ab
	fi
7a17ab
7a17ab
	if [ $ccenv_cfgtype = 'native' ]; then
7a17ab
		ccenv_prefixes=
7a17ab
	elif [ -n "$mb_cross_compile" ]; then
7a17ab
		ccenv_prefixes="$mb_cross_compile"
7a17ab
	elif [ -n "$mb_host" ]; then
7a17ab
		ccenv_prefixes="$mb_host-"
7a17ab
	else
7a17ab
		ccenv_prefixes=
7a17ab
	fi
7a17ab
7a17ab
	if [ $ccenv_cfgtype = 'host' ]; then
7a17ab
		ccenv_tflags=
80f8ab
		ccenv_cflags=$(${mb_make} -n -f "$mb_pwd/Makefile.tmp" \
c0f608
			OS_DSO_EXRULES=default                          \
c0f608
			OS_SONAME=symlink                                \
f17e60
			OS_ARCHIVE_EXT='.a'                               \
c0f608
			.cflags-host)
7a17ab
80f8ab
		ccenv_cflags="${ccenv_cflags#*: }"
80f8ab
7a17ab
		ccenv_cc="$mb_user_cc"
7a17ab
		ccenv_cpp="$mb_user_cpp"
7a17ab
		ccenv_cxx="$mb_user_cxx"
7a17ab
7a17ab
		ccenv_pe_subsystem="$mb_pe_subsystem"
7a17ab
		ccenv_pe_image_base="$mb_pe_image_base"
7a17ab
	else
7a17ab
		ccenv_tflags=
80f8ab
		ccenv_cflags=$(${mb_make} -n -f "$mb_pwd/Makefile.tmp" \
c0f608
			OS_DSO_EXRULES=default                          \
c0f608
			OS_SONAME=symlink                                \
f17e60
			OS_ARCHIVE_EXT='.a'                               \
c0f608
			.cflags-native)
c0f608
80f8ab
		ccenv_cflags="${ccenv_cflags#*: }"
80f8ab
7a17ab
		ccenv_cc="$mb_native_cc"
7a17ab
		ccenv_cpp="$mb_native_cpp"
7a17ab
		ccenv_cxx="$mb_native_cxx"
7a17ab
7a17ab
		ccenv_pe_subsystem="$mb_native_pe_subsystem"
7a17ab
		ccenv_pe_image_base="$mb_native_pe_image_base"
7a17ab
	fi
7a17ab
}
7a17ab
7a17ab
ccenv_set_characteristics()
7a17ab
{
7a17ab
	ccenv_set_cc_host
7a17ab
	ccenv_set_cc_bits
7a17ab
	ccenv_set_cc_binfmt
7a17ab
	ccenv_set_cc_underscore
7a17ab
}
7a17ab
7a17ab
ccenv_set_toolchain_variables()
7a17ab
{
7a17ab
	ccenv_common_init $1
7a17ab
	ccenv_set_cc
7a17ab
	ccenv_set_cpp
7a17ab
	ccenv_set_cxx
7a17ab
	ccenv_set_primary_tools
7a17ab
	ccenv_set_tool_variants
7a17ab
	ccenv_set_characteristics
7a17ab
7a17ab
	ccenv_set_os
7a17ab
	ccenv_set_os_flags
7a17ab
	ccenv_set_os_semantics
fe1010
	ccenv_set_os_dso_format
7a17ab
	ccenv_set_os_dso_exrules
7a17ab
	ccenv_set_os_dso_linkage
7a17ab
	ccenv_set_os_dso_patterns
7a17ab
	ccenv_set_os_pe_switches
7a17ab
7a17ab
	ccenv_output_defs
7a17ab
	ccenv_clean_up
7a17ab
7a17ab
	eval 'ccenv_'${ccenv_cfgtype}'_cc'=\'$ccenv_cc\'
7a17ab
}
7a17ab
7a17ab
ccenv_set_host_variables()
7a17ab
{
7a17ab
	output_script_status ${mb_script} \
7a17ab
		'detect and query host (targeted) system'
7a17ab
7a17ab
	ccenv_set_toolchain_variables 'host'
7a17ab
	ccenv_dso_verify
7a17ab
}
7a17ab
7a17ab
ccenv_set_native_variables()
7a17ab
{
7a17ab
	output_script_status ${mb_script} \
7a17ab
		'detect and query native (local build) system'
7a17ab
7a17ab
	if [ _$mb_ccenv_skip_native != _yes ]; then
7a17ab
		ccenv_set_toolchain_variables 'native'
7a17ab
	fi
7a17ab
}