Blame sofort/ccenv/ccenv.sh

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