Blame sofort/ccenv/ccenv.sh

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