Blame sofort/ccenv/ccenv.sh

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