Blame sofort/ccenv/ccenv.sh

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