Blame sofort/ccenv/ccenv.sh

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