Blame sofort/ccenv/ccenv.sh

68bef4
7e34aa
# ccenv.sh: sofort's tool-finding bits,
7e34aa
# invoked from within the project-agnostic configure script.
7e34aa
37dc93
# this file is covered by COPYING.SOFORT.
37dc93
7e34aa
# invocation and names of binary tools:
7e34aa
# agnostic names (ar, nm, objdump, ...);
7e34aa
# target-prefixed agnostic names (x86_64-nt64-midipix-ar, ...);
7e34aa
# branded names (llvm-ar, llvm-nm, llvm-objdump, ...);
7e34aa
# target-prefixed branded names (x86_64-linux-gnu-gcc-ar, ...);
7e34aa
# target-specifying branded tools (llvm-ar --target=x86_64-linux, ...).
7e34aa
7e34aa
# cross-compilation: default search order:
7e34aa
# target-prefixed agnostic tools;
7e34aa
# target-prefixed branded tools, starting with the prefix
7e34aa
# most commonly associated with the selected compiler (that is,
7e34aa
# ``gcc'' when using gcc, and ``llvm'' when using clang);
7e34aa
# target-speficying branded tools, starting once again with the
7e34aa
# prefix most commonly associated with the selected compiler.
7e34aa
7e34aa
# internal variables of interest:
7e34aa
# ccenv_cfgtype: the type of host being tested (host/native)
7e34aa
# ccenv_cfgfile: the configuration file for the host being tested
7e34aa
# ccenv_cflags:  the comprehensive cflags for the host being tested
7e34aa
# ccenv_cchost:  the host being tested, as reported by -dumpmachine
7e34aa
68bef4
# variables available to cfgdefs.sh and cfgfini.sh:
68bef4
# ccenv_host_cflags:    expanded cflags, valid for the host compiler
68bef4
# ccenv_host_ldflags:   expanded ldflags, valid for the host compiler
4ab93f
# ccenv_host_sysroot:   host sysroot, as reported by the host compiler
68bef4
# ccenv_native_cflags:  expanded cflags, valid for the native compiler
68bef4
# ccenv_native_ldflags: expanded ldflags, valid for the native compiler
4ab93f
# ccenv_native_sysroot: native sysroot, as reported by the native compiler
7e34aa
7e34aa
ccenv_usage()
7e34aa
{
7e34aa
	cat "$mb_project_dir"/sofort/ccenv/ccenv.usage
7e34aa
	exit 0
7e34aa
}
7e34aa
7e34aa
7e34aa
ccenv_newline()
7e34aa
{
7e34aa
	printf '\n' >> "$ccenv_cfgfile"
7e34aa
}
7e34aa
7e34aa
7e34aa
ccenv_comment()
7e34aa
{
7e34aa
	ccenv_internal_str='#'
7e34aa
7e34aa
	for ccenv_internal_arg ; do
7e34aa
		ccenv_internal_str="$ccenv_internal_str $ccenv_internal_arg"
7e34aa
	done
7e34aa
7e34aa
	printf '%s\n' "$ccenv_internal_str" >> "$ccenv_cfgfile"
7e34aa
}
7e34aa
7e34aa
28f188
ccenv_tool_prolog()
28f188
{
ad20a7
	ccenv_line_dots='.....................................'
841120
	ccenv_tool_desc=" == checking for ${1}"
28f188
	ccenv_tool_dlen="${#ccenv_line_dots}"
28f188
15a870
	printf '\n%s\n' '________________________' >&3
15a870
	printf "ccenv: checking for ${1}\n\n" >&3
28f188
	printf "%${ccenv_tool_dlen}.${ccenv_tool_dlen}s" \
ad20a7
		"${ccenv_tool_desc}  ${mb_line_dots}"
28f188
}
28f188
28f188
28f188
ccenv_tool_epilog()
28f188
{
28f188
	ccenv_line_dots='................................'
28f188
	ccenv_tool_dlen="$((${#ccenv_line_dots} - ${#1}))"
28f188
884e09
	case ${ccenv_tool_dlen} in
884e09
		0 | -* )
884e09
			ccenv_tool_dlen='3' ;;
884e09
	esac
884e09
ad20a7
	printf "%${ccenv_tool_dlen}.${ccenv_tool_dlen}s  %s.\n" \
28f188
		"${ccenv_line_dots}" "${1}"
15a870
15a870
	if [ "${1}" = 'false' ]; then
15a870
		printf '\n\nccenv: not (yet) found.\n' >&3
15a870
	else
15a870
		printf "\n\nccenv : found $(command -v ${1}).\n" >&3
15a870
	fi
15a870
15a870
	printf '%s\n' '------------------------' >&3
28f188
}
28f188
28f188
0ab1aa
ccenv_tool_variant_epilog()
0ab1aa
{
0ab1aa
	ccenv_expr=${1}='${'${1}':-false}'
0ab1aa
	eval "$ccenv_expr"
0ab1aa
0ab1aa
	ccenv_expr='${'${1}'}'
0ab1aa
	eval ccenv_tool_epilog "$ccenv_expr"
0ab1aa
}
0ab1aa
0ab1aa
28f188
ccenv_attr_prolog()
28f188
{
ad20a7
	ccenv_line_dots=' .....................................'
ad20a7
	ccenv_attr_desc=" == detect ${ccenv_cfgtype} ${1}"
28f188
	ccenv_attr_dlen="${#ccenv_line_dots}"
28f188
28f188
	printf "%${ccenv_attr_dlen}.${ccenv_attr_dlen}s" \
28f188
		"${ccenv_attr_desc} ${ccenv_line_dots}"
15a870
15a870
	printf '\n%s\n' '________________________' >&3
15a870
	printf "ccenv: detecting ${1}\n\n" >&3
28f188
}
28f188
28f188
28f188
ccenv_attr_epilog()
28f188
{
15a870
	ccenv_line_dots='................................'
ad20a7
	ccenv_tool_dlen="$((${#ccenv_line_dots} - 1 - ${#1}))"
15a870
884e09
	case ${ccenv_tool_dlen} in
884e09
		0 | -* )
884e09
			ccenv_tool_dlen='3' ;;
884e09
	esac
884e09
ad20a7
	printf "%${ccenv_tool_dlen}.${ccenv_tool_dlen}s  %s.\n" \
15a870
		"${ccenv_line_dots}" "${1}"
15a870
15a870
	printf '\n\nccenv: detected result: %s\n' "${1}" >&3
15a870
	printf '%s\n' '------------------------' >&3
28f188
}
28f188
28f188
7e34aa
ccenv_find_tool()
7e34aa
{
7e34aa
	if [ -z "$ccenv_prefixes" ]; then
e2dba3
		for ccenv_candidate in $(printf '%s' "$ccenv_candidates"); do
accdd4
			ccenv_cmd_args="${@:-}"
accdd4
accdd4
			if [ -z "$ccenv_cmd_args" ]; then
d3b49f
				if command -v "$ccenv_candidate" > /dev/null; then
d3b49f
					ccenv_tool="$ccenv_candidate"
150610
					return 0
749dc9
				fi
150610
			else
d3b49f
				if command -v "$ccenv_candidate" > /dev/null; then
15a870
					if "$ccenv_candidate" $@ > /dev/null 2>&3; then
d3b49f
						ccenv_tool="$ccenv_candidate"
150610
						return 0
749dc9
					fi
749dc9
				fi
150610
			fi
7e34aa
		done
7e34aa
7e34aa
		ccenv_tool=false
7e34aa
7e34aa
		return 0
7e34aa
	fi
7e34aa
e2dba3
	for ccenv_prefix in $(printf '%s' "$ccenv_prefixes"); do
e2dba3
		for ccenv_candidate in $(printf '%s' "$ccenv_candidates"); do
7e34aa
			ccenv_tool="$ccenv_prefix$ccenv_candidate"
749dc9
749dc9
			if command -v "$ccenv_tool" > /dev/null; then
749dc9
				return 0
749dc9
			fi
7e34aa
		done
7e34aa
	done
7e34aa
e2dba3
	for ccenv_candidate in $(printf '%s' "$ccenv_candidates"); do
d3b49f
		if command -v "$ccenv_candidate" > /dev/null; then
d3b49f
			ccenv_tool="$ccenv_candidate"
749dc9
			return 0
749dc9
		fi
7e34aa
	done
7e34aa
7e34aa
	ccenv_tool=false
7e34aa
7e34aa
	return 0
7e34aa
}
7e34aa
7e34aa
7e34aa
ccenv_set_primary_tools()
7e34aa
{
7e34aa
	ccenv_core_tools="ar nm objdump ranlib size strip strings objcopy"
400106
	ccenv_hack_tools="addr2line cov elfedit readelf readobj otool"
ad6d9c
	ccenv_peep_tools="perk mdso dlltool windmc windres pkgconf"
7e34aa
e2dba3
	for __tool in $(printf '%s' "$ccenv_core_tools $ccenv_hack_tools $ccenv_peep_tools"); do
28f188
		ccenv_tool_prolog "$__tool"
28f188
7e34aa
		if [ -n "$mb_agnostic" ]; then
7e34aa
			ccenv_candidates=" $__tool"
7e34aa
7e34aa
		elif [ -n "$mb_zealous" ]; then
7e34aa
			ccenv_candidates="$mb_zealous-$__tool"
7e34aa
7e34aa
		elif [ "$mb_toolchain" = 'gcc' ]; then
7e34aa
			ccenv_candidates="gcc-$__tool"
7e34aa
			ccenv_candidates="$ccenv_candidates $__tool"
7e34aa
			ccenv_candidates="$ccenv_candidates llvm-$__tool"
7e34aa
7e34aa
		elif [ "$mb_toolchain" = 'llvm' ]; then
7e34aa
			ccenv_candidates="llvm-$__tool"
7e34aa
			ccenv_candidates="$ccenv_candidates $__tool"
7e34aa
			ccenv_candidates="$ccenv_candidates gcc-$__tool"
7e34aa
7e34aa
		elif [ -n "$mb_toolchain" ]; then
7e34aa
			ccenv_candidates="$mb_toolchain-$__tool"
7e34aa
			ccenv_candidates="$ccenv_candidates $__tool"
7e34aa
			ccenv_candidates="$ccenv_candidates gcc-$__tool"
7e34aa
			ccenv_candidates="$ccenv_candidates llvm-$__tool"
7e34aa
15c823
		elif [ "$__tool" = 'pkgconf' ]; then
15c823
			ccenv_candidates="$__tool pkg-config"
15c823
7e34aa
		else
7e34aa
			ccenv_candidates="$__tool"
7e34aa
			ccenv_candidates="$ccenv_candidates gcc-$__tool"
7e34aa
			ccenv_candidates="$ccenv_candidates llvm-$__tool"
7e34aa
		fi
7e34aa
7e34aa
		if [ "$ccenv_cfgtype" = 'host' ]; then
7e34aa
			ccenv_var_prefix='mb_'
7e34aa
		else
7e34aa
			ccenv_var_prefix='mb_native_'
7e34aa
		fi
7e34aa
ad22a7
		ccenv_tool=
7e34aa
		ccenv_var_name=$ccenv_var_prefix$__tool
7e34aa
		ccenv_var_expr='${'$ccenv_var_name':-}'
7e34aa
		eval ccenv_var_val=$ccenv_var_expr
7e34aa
7e34aa
		if [ -n "$ccenv_var_val" ]; then
7e34aa
			eval ccenv_$__tool="$ccenv_var_val"
ad22a7
			ccenv_tool="$ccenv_var_val"
7e34aa
		else
7e34aa
			ccenv_find_tool
7e34aa
			eval ccenv_$__tool="$ccenv_tool"
7e34aa
		fi
28f188
28f188
		ccenv_tool_epilog "$ccenv_tool"
7e34aa
	done
7e34aa
7e34aa
	# windrc
7e34aa
	ccenv_windrc="$ccenv_windres"
1a35f5
1a35f5
	# archive format preamble
88a931
	if [ -n "$ccenv_dumpmachine_switch" ]; then
0c4e6f
		ccenv_libgcc_path=$($ccenv_cc -print-file-name=libgcc.a \
0c4e6f
			2>/dev/null)
0c4e6f
0c4e6f
		if [ -n "$ccenv_libgcc_path" ]; then
0c4e6f
			ccenv_libgcc_a_header=$(od -b -N8             \
0c4e6f
				$($ccenv_cc -print-file-name=libgcc.a) \
0c4e6f
				| head -n1)
0c4e6f
		else
0c4e6f
			ccenv_libgcc_a_header=
0c4e6f
		fi
88a931
	else
88a931
		ccenv_libgcc_a_header=
88a931
	fi
88a931
88a931
	# ar (default)
1a35f5
	ccenv_cc_arfmt='common'
1a35f5
1a35f5
	# ar (big)
1a35f5
	ccenv_bigaf_header=$(printf '%s\n' '<bigaf>' | od -b | head -n1)
1a35f5
1a35f5
	if [ "$ccenv_libgcc_a_header" = "$ccenv_bigaf_header" ]; then
1a35f5
		ccenv_cc_arfmt='bigaf'
1a35f5
1a35f5
		for __tool in $(printf '%s' "$ccenv_core_tools"); do
1a35f5
			ccenv_var_name=ccenv_$__tool
1a35f5
			ccenv_var_expr='${'$ccenv_var_name':-}'
1a35f5
			eval ccenv_var_val="$ccenv_var_expr"
1a35f5
1a35f5
			if [ "$ccenv_var_val" != false ]; then
1a35f5
				ccenv_var_val="$ccenv_var_val -X64"
1a35f5
				ccenv_var_expr='${ccenv_var_val:-}'
1a35f5
				eval ccenv_$__tool="$ccenv_var_expr"
1a35f5
			fi
1a35f5
		done
1a35f5
	fi
1a35f5
1a35f5
	# ar (small)
1a35f5
	ccenv_aiaff_header=$(printf '%s\n' '<aiaff>' | od -b | head -n1)
1a35f5
1a35f5
	if [ "$ccenv_libgcc_a_header" = "$ccenv_aiaff_header" ]; then
1a35f5
		ccenv_cc_arfmt='aiaff'
1a35f5
1a35f5
		for __tool in $(printf '%s' "$ccenv_core_tools"); do
1a35f5
			ccenv_var_name=ccenv_$__tool
1a35f5
			ccenv_var_expr='${'$ccenv_var_name':-}'
1a35f5
			eval ccenv_var_val="$ccenv_var_expr"
1a35f5
1a35f5
			if [ "$ccenv_var_val" != false ]; then
1a35f5
				ccenv_var_val="$ccenv_var_val -X32"
1a35f5
				ccenv_var_expr='${ccenv_var_val:-}'
1a35f5
				eval ccenv_$__tool="$ccenv_var_expr"
1a35f5
			fi
1a35f5
		done
1a35f5
	fi
7e34aa
}
7e34aa
7e34aa
ccenv_set_tool_variants()
7e34aa
{
7e34aa
	# as (asm)
28f188
	ccenv_tool_prolog 'as (asm)'
7e34aa
	ccenv_candidates=as
7e34aa
	ccenv_find_tool
7e34aa
7e34aa
	if [ "$ccenv_tool" = false ]; then
7e34aa
		ccenv_as_asm=
7e34aa
	else
1a0605
		$ccenv_tool --help 2>&1 | grep -i '.bc assembler' \
7e34aa
		|| ccenv_as_asm="$ccenv_tool"
7e34aa
	fi
7e34aa
0ab1aa
	ccenv_tool_variant_epilog 'ccenv_as_asm'
28f188
7e34aa
	# as (ll)
28f188
	ccenv_tool_prolog 'as (ll)'
7e34aa
	ccenv_candidates=llvm-as
7e34aa
	ccenv_find_tool
7e34aa
7e34aa
	if [ "$ccenv_tool" != false ]; then
7e34aa
		ccenv_as_ll="$ccenv_tool"
7e34aa
	fi
7e34aa
0ab1aa
	ccenv_tool_variant_epilog 'ccenv_as_ll'
28f188
7e34aa
	# as (mc)
28f188
	ccenv_tool_prolog 'as (mc)'
7e34aa
	ccenv_candidates=llvm-mc
7e34aa
	ccenv_find_tool
7e34aa
7e34aa
	if [ "$ccenv_tool" != false ]; then
7e34aa
		ccenv_as_mc="$ccenv_tool"
7e34aa
	fi
7e34aa
0ab1aa
	ccenv_tool_variant_epilog 'ccenv_as_mc'
28f188
7e34aa
	# ld (bfd)
28f188
	ccenv_tool_prolog 'ld (bfd)'
7e34aa
	ccenv_candidates=ld.bfd
7e34aa
	ccenv_find_tool
7e34aa
7e34aa
	if [ "$ccenv_tool" != false ]; then
7e34aa
		ccenv_ld_bfd="$ccenv_tool"
7e34aa
	fi
7e34aa
0ab1aa
	ccenv_tool_variant_epilog 'ccenv_ld_bfd'
28f188
7e34aa
	# ld (gold)
28f188
	ccenv_tool_prolog 'ld (gold)'
7e34aa
	ccenv_candidates=ld.gold
7e34aa
	ccenv_find_tool
7e34aa
7e34aa
	if [ "$ccenv_tool" != false ]; then
7e34aa
		ccenv_ld_gold="$ccenv_tool"
7e34aa
	fi
7e34aa
0ab1aa
	ccenv_tool_variant_epilog 'ccenv_ld_gold'
28f188
7e34aa
	# ld (lld)
28f188
	ccenv_tool_prolog 'ld (lld)'
7e34aa
	ccenv_candidates=lld
7e34aa
	ccenv_find_tool
7e34aa
7e34aa
	if [ "$ccenv_tool" != false ]; then
7e34aa
		ccenv_ld_lld="$ccenv_tool"
7e34aa
	fi
7e34aa
0ab1aa
	ccenv_tool_variant_epilog 'ccenv_ld_lld'
28f188
7e34aa
	# objdump (bfd)
28f188
	ccenv_tool_prolog 'objdump (bfd)'
7e34aa
	ccenv_candidates=objdump
7e34aa
	ccenv_find_tool
7e34aa
305cb2
	if $ccenv_tool --version | grep -i Binutils > /dev/null; then
305cb2
		ccenv_objdump_bfd="$ccenv_tool"
305cb2
	fi
7e34aa
0ab1aa
	ccenv_tool_variant_epilog 'ccenv_objdump_bfd'
28f188
7e34aa
	# objdump (llvm)
28f188
	ccenv_tool_prolog 'objdump (llvm)'
7e34aa
	ccenv_candidates=llvm-objdump
7e34aa
	ccenv_find_tool
7e34aa
305cb2
	if $ccenv_tool --version | grep -i LLVM > /dev/null; then
305cb2
		ccenv_objdump_llvm="$ccenv_tool"
305cb2
	fi
7e34aa
0ab1aa
	ccenv_tool_variant_epilog 'ccenv_objdump_llvm'
28f188
7e34aa
	# readelf (bfd)
28f188
	ccenv_tool_prolog 'readelf (bfd)'
7e34aa
	ccenv_candidates=readelf
7e34aa
	ccenv_find_tool
7e34aa
305cb2
	if $ccenv_tool --version | grep -i Binutils > /dev/null; then
305cb2
		ccenv_readelf_bfd="$ccenv_tool"
305cb2
	fi
7e34aa
0ab1aa
	ccenv_tool_variant_epilog 'ccenv_readelf_bfd'
28f188
7e34aa
	# readelf (llvm)
28f188
	ccenv_tool_prolog 'readelf (llvm)'
7e34aa
	ccenv_candidates=llvm-readelf
7e34aa
	ccenv_find_tool
7e34aa
305cb2
	if $ccenv_tool --version | grep -i LLVM > /dev/null; then
305cb2
		ccenv_readelf_llvm="$ccenv_tool"
305cb2
	fi
7e34aa
0ab1aa
	ccenv_tool_variant_epilog 'ccenv_readelf_llvm'
28f188
7e34aa
	# as
7e34aa
	if [ -n "$ccenv_cc" ]; then
a77aa5
		ccenv_as='$('"$ccenv_makevar_prefix"'CC) -c -x assembler'
7e34aa
	elif [ -n "$mb_agnostic" ]; then
7e34aa
		ccenv_as='$('"$ccenv_makevar_prefix"'AS_ASM)'
7e34aa
	elif [ "$mb_zealous" = 'gcc' ]; then
7e34aa
		ccenv_as='$('"$ccenv_makevar_prefix"'AS_ASM)'
7e34aa
	elif [ -n "$mb_zealous" = 'llvm' ]; then
7e34aa
		ccenv_as='$('"$ccenv_makevar_prefix"'AS_MC)'
7e34aa
	elif [ "$mb_toolchain" = 'gcc' ]; then
7e34aa
		ccenv_as='$('"$ccenv_makevar_prefix"'AS_ASM)'
7e34aa
	elif [ "$mb_toolchain" = 'llvm' ]; then
7e34aa
		ccenv_as='$('"$ccenv_makevar_prefix"'AS_MC)'
7e34aa
	fi
7e34aa
7e34aa
	# ld
7e34aa
	if [ -n "$ccenv_cc" ]; then
7e34aa
		ccenv_ld='$('"$ccenv_makevar_prefix"'CC) -nostdlib -nostartfiles'
7e34aa
	fi
7e34aa
}
7e34aa
7e34aa
ccenv_set_c_compiler_candidates()
7e34aa
{
7e34aa
	if   [ -n "$mb_compiler" ]; then
7e34aa
		ccenv_candidates="$mb_compiler"
7e34aa
7e34aa
	elif [ -n "$mb_agnostic" ]; then
7e34aa
		ccenv_candidates="c99 c11 cc"
7e34aa
7e34aa
	elif [ "$mb_zealous" = 'gcc' ]; then
7e34aa
		ccenv_candidates="gcc"
7e34aa
7e34aa
	elif [ "$mb_zealous" = 'llvm' ]; then
7e34aa
		ccenv_candidates="clang"
7e34aa
7e34aa
	elif [ "$mb_toolchain" = 'gcc' ]; then
7e34aa
		ccenv_candidates="gcc c99 c11 cc clang"
7e34aa
7e34aa
	elif [ "$mb_toolchain" = 'llvm' ]; then
7e34aa
		ccenv_candidates="clang c99 c11 cc gcc"
7e34aa
7e34aa
	elif [ -n "$mb_toolchain" ]; then
7e34aa
		ccenv_candidates="$mb_toolchain c99 c11 cc gcc clang"
7e34aa
7e34aa
	else
0d4904
		ccenv_candidates="cc gcc clang c99 c11"
7e34aa
	fi
7e34aa
}
7e34aa
7e34aa
7e34aa
ccenv_set_cc()
7e34aa
{
28f188
	ccenv_tool_prolog 'C compiler'
28f188
7e34aa
	if [ -z "$ccenv_cc" ]; then
7e34aa
		ccenv_set_c_compiler_candidates
88a931
		ccenv_find_tool
7e34aa
		ccenv_cc="$ccenv_tool"
7e34aa
	fi
7e34aa
88a931
7e34aa
	if [ "$ccenv_cc" = false ] && [ -n "$mb_compiler" ]; then
7e34aa
		ccenv_cc="$mb_compiler"
7e34aa
	fi
7e34aa
88a931
	ccenv_tool_epilog "$ccenv_cc"
88a931
88a931
88a931
	if [ $ccenv_cfgtype = 'host' ]; then
88a931
		ccenv_host_cc="$ccenv_cc"
88a931
		cfgtest_host_section
88a931
		ccenv_host_cc=
88a931
	else
88a931
		ccenv_native_cc="$ccenv_cc"
88a931
		cfgtest_native_section
88a931
		ccenv_native_cc=
88a931
	fi
88a931
3b8211
	cfgtest_silent='yes'
3b8211
88a931
	if cfgtest_compiler_switch -dumpmachine ; then
88a931
		ccenv_dumpmachine_switch='-dumpmachine'
88a931
	else
88a931
		ccenv_dumpmachine_switch=
88a931
	fi
88a931
421f33
	if cfgtest_code_snippet_asm 'typedef int dummy;' ; then
421f33
		eval ccenv_${ccenv_cfgtype}_stdin_input='yes'
421f33
	else
421f33
		eval ccenv_${ccenv_cfgtype}_stdin_input='no'
421f33
	fi
421f33
3b8211
	unset cfgtest_silent
3b8211
7e34aa
	ccenv_cc_cmd="$ccenv_cc"
da4366
	ccenv_errors=
7e34aa
7e34aa
	if [ "$ccenv_cfgtype" = 'native' ]; then
65aba7
		ccenv_host=
65aba7
e3d996
		if [ -n "$mb_native_host" ]; then
e3d996
			ccenv_host="$mb_native_host"
e3d996
e3d996
		elif [ -n "$ccenv_dumpmachine_switch" ]; then
88a931
			ccenv_host=$(eval $ccenv_cc $(printf '%s' "$ccenv_cflags") \
88a931
				$ccenv_dumpmachine_switch 2>&3)
65aba7
65aba7
		elif command -v slibtool > /dev/null 2>&1; then
4dcd96
			ccenv=$(slibtool --dumpmachine 2>/dev/null || true)
65aba7
		fi
65aba7
65aba7
		if [ -z "$ccenv_host" ]; then
e3d996
			ccenv_machine=$(uname -m 2>/dev/null)
e3d996
			ccenv_system=$(uname -s 2>/dev/null)
e3d996
e3d996
			ccenv_machine="${ccenv_machine:-unknown}"
e3d996
			ccenv_system="${ccenv_system:-anyos}"
e3d996
e3d996
			ccenv_host=$(printf '%s' "${ccenv_machine}-unknown-${ccenv_system}" \
2b311e
				| tr '[[:upper:]]' '[[:lower:]]')
88a931
		fi
88a931
308fbf
		ccenv_cchost=$ccenv_host
7e34aa
		return 0
7e34aa
	fi
7e34aa
88a931
7e34aa
	if [ -n "$mb_cchost" ]; then
7e34aa
		ccenv_host="$mb_cchost"
7e34aa
	elif [ -n "$mb_host" ]; then
7e34aa
		ccenv_host="$mb_host"
7e34aa
	else
7e34aa
		ccenv_host=
7e34aa
	fi
7e34aa
88a931
	if [ -z "$ccenv_host" ] && [ -n "$ccenv_dumpmachine_switch" ]; then
88a931
		ccenv_host=$(eval $ccenv_cc $(printf '%s' "$ccenv_cflags") \
88a931
			$ccenv_dumpmachine_switch 2>&3)
7e34aa
		ccenv_cchost=$ccenv_host
88a931
88a931
	elif [ -z "$ccenv_host" ]; then
65aba7
		# no -dumpmachine support and no --host argument implies native build
65aba7
		if command -v slibtool > /dev/null 2>&1; then
4dcd96
			ccenv=$(slibtool --dumpmachine 2>/dev/null || true)
65aba7
		fi
e3d996
65aba7
		if [ -z "$ccenv_host" ]; then
65aba7
			ccenv_machine=$(uname -m 2>/dev/null)
65aba7
			ccenv_system=$(uname -s 2>/dev/null)
65aba7
65aba7
			ccenv_machine="${ccenv_machine:-unknown}"
65aba7
			ccenv_system="${ccenv_system:-anyos}"
e3d996
65aba7
			ccenv_host=$(printf '%s' "${ccenv_machine}-unknown-${ccenv_system}" \
65aba7
				| tr '[[:upper:]]' '[[:lower:]]')
65aba7
		fi
e3d996
88a931
		ccenv_cchost=$ccenv_host
88a931
88a931
	elif [ -n "$ccenv_dumpmachine_switch" ]; then
47d854
		ccenv_tmp=$(mktemp ./tmp_XXXXXXXXXXXXXXXX)
7e34aa
		ccenv_cmd="$ccenv_cc --target=$ccenv_host -E -xc -"
7e34aa
7e34aa
		if [ -z "$mb_user_cc" ]; then
2c1fff
			$(printf %s "$ccenv_cmd") < /dev/null > /dev/null \
7e34aa
				2>"$ccenv_tmp" || true
7e34aa
7e34aa
			ccenv_errors=$(cat "$ccenv_tmp")
7e34aa
7e34aa
			if [ -z "$ccenv_errors" ]; then
88a931
				ccenv_tool_prolog 'C compiler for host'
7e34aa
				ccenv_tflags="--target=$ccenv_host"
7e34aa
				ccenv_cc="$ccenv_cc $ccenv_tflags"
88a931
				ccenv_tool_epilog "$ccenv_cc"
15a870
			else
15a870
				printf '%s' "$ccenv_errors" >&3
7e34aa
			fi
7e34aa
		fi
7e34aa
47d854
		rm -f "$ccenv_tmp"
47d854
		unset ccenv_tmp
47d854
88a931
		ccenv_cchost=$(eval $ccenv_cc $(printf '%s' "$ccenv_cflags") \
88a931
			$ccenv_dumpmachine_switch 2>&3)
7e34aa
	fi
7e34aa
d59e66
	if [ -z "$ccenv_dumpmachine_switch" ] && [ -n "$ccenv_host" ]; then
d59e66
		ccenv_cchost="$ccenv_host"
d59e66
d59e66
	elif [ "$ccenv_cchost" != "$ccenv_host" ]; then
15a870
		printf 'error!\n' >&2
7e34aa
		printf 'ccenv:\n' >&2
7e34aa
		printf 'ccenv: ccenv_host:   %s \n' $ccenv_host >&2
7e34aa
		printf 'ccenv: ccenv_cchost: %s \n' $ccenv_cchost >&2
7e34aa
7e34aa
		if [ -z "$ccenv_tflags" ]; then
7e34aa
			printf 'ccenv:\n' >&2
7e34aa
			printf 'ccenv: ccenv_host and ccenv_cchost do not match, most likely because:\n' >&2
7e34aa
			printf 'ccenv: (1) you explicitly set CC (or passed --compiler=...)\n' >&2
7e34aa
			printf 'ccenv: (2) the selected compiler does not accept --target=...\n' >&2
7e34aa
			printf 'ccenv: (3) the host reported by -dumpmachine differs from the one you requested.\n' >&2
7e34aa
		fi
7e34aa
7e34aa
		if [ -n "$ccenv_errors" ]; then
7e34aa
			printf 'ccenv:\n' >&2
7e34aa
			printf 'ccenv: something went wrong, see the command and compiler message below.\n\n' >&2
7e34aa
			printf 'cmd: %s < /dev/null > /dev/null\n' "$ccenv_cmd" >&2
7e34aa
			printf '%s\n\n' "$ccenv_errors" >&2
7e34aa
		else
7e34aa
			printf 'ccenv:\n' >&2
7e34aa
			printf 'ccenv: something went wrong, bailing out.\n\n' >&2
7e34aa
		fi
7e34aa
7e34aa
		return 2
7e34aa
	fi
7e34aa
}
7e34aa
7e34aa
ccenv_set_cpp()
7e34aa
{
28f188
	ccenv_tool_prolog 'C pre-processor'
28f188
7e34aa
	case "$ccenv_cc_cmd" in
7e34aa
		cc | c99 | c11 | gcc)
7e34aa
			ccenv_cpp_prefix=
7e34aa
			ccenv_candidates="cpp" ;;
7e34aa
7e34aa
		clang )
7e34aa
			ccenv_cpp_prefix=
7e34aa
			ccenv_candidates="clang-cpp" ;;
7e34aa
7e34aa
		*-cc )
7e34aa
			ccenv_cpp_prefix=${ccenv_cc_cmd%-cc*}-
7e34aa
			ccenv_candidates="${ccenv_cpp_prefix}cpp" ;;
7e34aa
7e34aa
		*-c99 )
7e34aa
			ccenv_cpp_prefix=${ccenv_cc_cmd%-c99*}-
7e34aa
			ccenv_candidates="${ccenv_cpp_prefix}cpp" ;;
7e34aa
7e34aa
		*-c11 )
7e34aa
			ccenv_cpp_prefix=${ccenv_cc_cmd%-c11*}-
7e34aa
			ccenv_candidates="${ccenv_cpp_prefix}cpp" ;;
7e34aa
7e34aa
		*-gcc )
7e34aa
			ccenv_cpp_prefix=${ccenv_cc_cmd%-gcc*}-
7e34aa
			ccenv_candidates="${ccenv_cpp_prefix}cpp" ;;
7e34aa
7e34aa
		*-clang )
7e34aa
			ccenv_cpp_prefix=${ccenv_cc_cmd%-clang*}-
7e34aa
			ccenv_candidates="${ccenv_cpp_prefix}clang-cpp" ;;
7e34aa
7e34aa
		* )
7e34aa
			ccenv_cpp="$ccenv_cc -E"
28f188
			ccenv_tool_epilog "$ccenv_cpp"
7e34aa
			return 0
7e34aa
	esac
7e34aa
7e34aa
	ccenv_find_tool
7e34aa
7e34aa
	if [ "$ccenv_tool" = false ]; then
7e34aa
		ccenv_cpp="$ccenv_cc -E"
7e34aa
	elif [ -n "$ccenv_tflags" ]; then
7e34aa
		ccenv_cpp="$ccenv_tool $ccenv_tflags"
7e34aa
	else
7e34aa
		ccenv_cpp="$ccenv_tool"
7e34aa
	fi
28f188
28f188
	ccenv_tool_epilog "$ccenv_cpp"
7e34aa
}
7e34aa
7e34aa
ccenv_set_cxx()
7e34aa
{
28f188
	ccenv_tool_prolog 'C++ compiler'
28f188
7e34aa
	case "$ccenv_cc_cmd" in
7e34aa
		cc | c99 | c11 )
7e34aa
			ccenv_cxx_prefix=
7e34aa
			ccenv_candidates="cxx c++" ;;
7e34aa
7e34aa
		gcc )
7e34aa
			ccenv_cxx_prefix=
7e34aa
			ccenv_candidates="g++" ;;
7e34aa
7e34aa
		clang )
7e34aa
			ccenv_cxx_prefix=
7e34aa
			ccenv_candidates="clang++" ;;
7e34aa
7e34aa
		*-gcc )
8f3d46
			ccenv_cxx_prefix=${ccenv_cc_cmd%-gcc*}-
8f3d46
			ccenv_candidates="${ccenv_cxx_prefix}g++" ;;
7e34aa
7e34aa
		*-clang )
8f3d46
			ccenv_cxx_prefix=${ccenv_cc_cmd%-clang*}-
8f3d46
			ccenv_candidates="${ccenv_cxx_prefix}clang++" ;;
8f3d46
8f3d46
		/*cc | /*c99 | /*c11 )
8f3d46
			ccenv_cxx_prefix=${ccenv_cc_cmd%/*}
8f3d46
			ccenv_candidates="${ccenv_cxx_prefix}/cxx"
8f3d46
			ccenv_candidates="${ccenv_candidates} ${ccenv_cxx_prefix}/c++" ;;
8f3d46
8f3d46
		/*gcc )
8f3d46
			ccenv_cxx_prefix=${ccenv_cc_cmd%/*}
8f3d46
			ccenv_candidates="${ccenv_cxx_prefix}/g++" ;;
7e34aa
8f3d46
		/*clang )
8f3d46
			ccenv_cxx_prefix=${ccenv_cc_cmd%/*}
8f3d46
			ccenv_candidates="${ccenv_cxx_prefix}/clang++" ;;
7e34aa
7e34aa
		* )
7e34aa
			ccenv_cxx="$ccenv_cc -xc++"
28f188
			ccenv_tool_epilog "$ccenv_cxx"
7e34aa
			return 0
7e34aa
	esac
7e34aa
7e34aa
	ccenv_find_tool
7e34aa
7e34aa
	if [ "$ccenv_tool" = false ]; then
7e34aa
		ccenv_cxx="$ccenv_cc -xc++"
7e34aa
	elif [ -n "$ccenv_tflags" ]; then
7e34aa
		ccenv_cxx="$ccenv_tool $ccenv_tflags"
7e34aa
	else
7e34aa
		ccenv_cxx="$ccenv_tool"
7e34aa
	fi
28f188
28f188
	ccenv_tool_epilog "$ccenv_cxx"
7e34aa
}
7e34aa
7e34aa
ccenv_set_cc_host()
7e34aa
{
6e1ae7
	ccenv_attr_prolog 'system'
7e34aa
	ccenv_cc_host="$ccenv_cchost"
28f188
	ccenv_attr_epilog "$ccenv_cc_host"
7e34aa
}
7e34aa
7e34aa
ccenv_set_cc_bits()
7e34aa
{
28f188
	ccenv_attr_prolog 'bits'
28f188
7e34aa
	ccenv_internal_size=
7e34aa
	ccenv_internal_type='void *'
7e34aa
	ccenv_internal_test='char x[(sizeof(%s) == %s/8) ? 1 : -1];'
7e34aa
7e34aa
	for ccenv_internal_guess in 64 32 128; do
d77248
		if [ -z "${ccenv_internal_size:-}" ]; then
ca7f9d
			ccenv_internal_str=$(printf "$ccenv_internal_test"  \
ca7f9d
				"$ccenv_internal_type"                      \
7e34aa
				"$ccenv_internal_guess")
7e34aa
503b4f
			ccenv_expr='ccenv_stdin_input=$ccenv_'${ccenv_cfgtype}'_stdin_input'
503b4f
			eval ${ccenv_expr}
503b4f
503b4f
			if [ "$ccenv_stdin_input" = 'yes' ]; then
9bb7fe
				printf '%s' "$ccenv_internal_str"                   \
9bb7fe
						| eval $ccenv_cc -S -xc - -o -      \
9bb7fe
						  $(printf '%s' "$ccenv_cflags")    \
9bb7fe
					> /dev/null 2>&3                            \
9bb7fe
				&& ccenv_internal_size=$ccenv_internal_guess
9bb7fe
			else
9bb7fe
				ccenv_tmpname='ccenv/c3RyaWN0X21vZGUK.c'
9bb7fe
9bb7fe
				printf '%s' "$ccenv_internal_str" \
9bb7fe
					> "$ccenv_tmpname"
9bb7fe
9bb7fe
				$ccenv_cc -c "$ccenv_tmpname" -o a.out \
9bb7fe
					> /dev/null 2>&3                \
9bb7fe
				&& ccenv_internal_size=$ccenv_internal_guess
9bb7fe
9bb7fe
				rm "$ccenv_tmpname"
9bb7fe
			fi
7e34aa
		fi
7e34aa
	done
7e34aa
7e34aa
	ccenv_cc_bits=$ccenv_internal_size
28f188
28f188
	ccenv_attr_epilog "$ccenv_cc_bits"
7e34aa
}
7e34aa
7e34aa
ccenv_set_cc_underscore()
7e34aa
{
28f188
	ccenv_attr_prolog 'prepended underscores'
28f188
7e34aa
	ccenv_fn_name='ZmYaXyWbVe_UuTnSdReQrPsOcNoNrLe'
7e34aa
	ccenv_fn_code='int %s(void){return 0;}'
7e34aa
de1f45
	ccenv_tmpname='ccenv/c3RyaWN0X21vZGUK.c'
de1f45
de1f45
	printf "$ccenv_fn_code" $ccenv_fn_name \
de1f45
		> "$ccenv_tmpname"
de1f45
de1f45
	$ccenv_cc -c "$ccenv_tmpname" -o a.out \
de1f45
		> /dev/null 2>&3
de1f45
de1f45
	if "$ccenv_nm" a.out | grep          \
de1f45
			-e "^_$ccenv_fn_name" \
de1f45
			-e " _$ccenv_fn_name"  \
de1f45
			> /dev/null; then
305cb2
		ccenv_cc_underscore='_'
28f188
		ccenv_attr_epilog 'yes'
de1f45
	else
de1f45
		ccenv_attr_epilog 'no'
305cb2
	fi
7e34aa
de1f45
	rm "$ccenv_tmpname"
de1f45
	rm a.out
28f188
7e34aa
	return 0
7e34aa
}
7e34aa
7e34aa
ccenv_create_framework_executable()
7e34aa
{
bf7ba6
	if [ "$ccenv_cfgtype" = 'host' ]; then
bf7ba6
		if [ "$mb_freestanding" = 'yes' ]; then
bf7ba6
			return 1
bf7ba6
		fi
bf7ba6
	fi
bf7ba6
7e34aa
	if [ -f $ccenv_image ]; then
7e34aa
		mv $ccenv_image $ccenv_image.tmp
7e34aa
		rm -f $ccenv_image.tmp
7e34aa
	fi
7e34aa
7ada1e
	ccenv_tmpname='ccenv/c3RyaWN0X21vZGUK.c'
7ada1e
7e34aa
	printf 'int main(void){return 0;}'  \
7ada1e
		> "$ccenv_tmpname"
7e34aa
7ada1e
	if $ccenv_cc "$ccenv_tmpname" -o $ccenv_image 2>&3; then
7ada1e
		ccenv_ret=0
bf7ba6
		ccenv_cc_environment='hosted'
7ada1e
	else
7ada1e
		ccenv_ret=1
7ada1e
	fi
7ada1e
7ada1e
	rm "$ccenv_tmpname"
7ada1e
7ada1e
	return $ccenv_ret
7e34aa
}
7e34aa
7e34aa
ccenv_create_freestanding_executable()
7e34aa
{
7e34aa
	if [ -f $ccenv_image ]; then
7e34aa
		mv $ccenv_image $ccenv_image.tmp
7e34aa
		rm -f $ccenv_image.tmp
7e34aa
	fi
7e34aa
7e34aa
	if [ -z "ccenv_cc_underscore" ]; then
7e34aa
		ccenv_start_fn='_start'
7e34aa
	else
7e34aa
		ccenv_start_fn='start'
7e34aa
	fi
7e34aa
7ada1e
	ccenv_tmpname='ccenv/c3RyaWN0X21vZGUK.c'
7e34aa
7ada1e
	printf 'int %s(void){return 0;}' "$ccenv_start_fn" \
7ada1e
		> "$ccenv_tmpname"
7e34aa
7ada1e
	if $ccenv_cc "$ccenv_tmpname"         \
7ada1e
			-ffreestanding         \
7ada1e
			-nostdlib -nostartfiles \
7ada1e
			-o $ccenv_image          \
7ada1e
			2>&3; then
7ada1e
		ccenv_ret=0
bf7ba6
		ccenv_cc_environment='freestanding'
7ada1e
	else
7ada1e
		ccenv_ret=1
7ada1e
	fi
7ada1e
7ada1e
	rm "$ccenv_tmpname"
7ada1e
7ada1e
	return $ccenv_ret
7e34aa
}
7e34aa
28f188
ccenv_set_cc_binfmt_error()
28f188
{
28f188
	ccenv_attr_epilog '(unable to create executable)'
28f188
}
28f188
7e34aa
ccenv_set_cc_binfmt()
7e34aa
{
7e34aa
	ccenv_use_perk=
400106
	ccenv_use_otool=
7e34aa
	ccenv_use_readelf=
7e34aa
	ccenv_use_readobj=
7e34aa
	ccenv_use_bfd_objdump=
7e34aa
	ccenv_use_llvm_objdump=
7e34aa
28f188
	ccenv_attr_prolog 'binary format'
28f188
7e34aa
	ccenv_create_framework_executable               \
7e34aa
		|| ccenv_create_freestanding_executable \
28f188
		|| ccenv_set_cc_binfmt_error            \
7e34aa
		|| return 0
7e34aa
7e34aa
	# PE / perk
7e34aa
	if [ -n "$ccenv_perk" ]; then
15a870
		if $ccenv_perk $ccenv_image 2>&3; then
305cb2
			ccenv_cc_binfmt='PE'
305cb2
			ccenv_use_perk=yes
305cb2
		fi
7e34aa
	fi
7e34aa
7e34aa
	# ELF / readelf
7e34aa
	if [ -n "$ccenv_readelf" ] && [ -z "$ccenv_cc_binfmt" ]; then
15a870
		if $ccenv_readelf -h $ccenv_image 2>&3               \
305cb2
				| grep 'Magic:' | sed -e 's/[ ]*//g' \
305cb2
				| grep 'Magic:7f454c46'              \
305cb2
					> /dev/null; then
305cb2
			ccenv_cc_binfmt='ELF'
305cb2
			ccenv_use_readelf=yes
305cb2
		fi
7e34aa
	fi
7e34aa
7e34aa
	# a marble of astonishing design:
7e34aa
	# llvm-readelf also parses PE and Mach-O
7e34aa
7e34aa
	if [ -n "$ccenv_readelf_llvm" ]; then
7e34aa
		ccenv_readany="$ccenv_readelf_llvm"
7e34aa
	else
7e34aa
		ccenv_readany="$ccenv_readelf"
7e34aa
	fi
7e34aa
7e34aa
	# PE / readelf
7e34aa
	if [ -n "$ccenv_readany" ] && [ -z "$ccenv_cc_binfmt" ]; then
15a870
		if $ccenv_readany -h $ccenv_image 2>&3               \
305cb2
				| grep 'Magic:' | sed -e 's/[ ]*//g' \
305cb2
				| grep 'Magic:MZ'                    \
305cb2
					> /dev/null; then
305cb2
			ccenv_cc_binfmt='PE'
305cb2
			ccenv_use_readelf=yes
305cb2
		fi
7e34aa
	fi
7e34aa
400106
	# MACHO-64 / otool
400106
	if [ -n "$ccenv_otool" ] && [ -z "$ccenv_cc_binfmt" ]; then
15a870
		if $ccenv_otool -hv $ccenv_image 2>&3        \
305cb2
				| grep -i 'MH_MAGIC_64'      \
305cb2
					> /dev/null; then
305cb2
			ccenv_cc_binfmt='MACHO'
305cb2
			ccenv_use_otool=yes
305cb2
		fi
400106
	fi
400106
400106
	# MACHO-32 / otool
400106
	if [ -n "$ccenv_otool" ] && [ -z "$ccenv_cc_binfmt" ]; then
15a870
		if $ccenv_otool -hv $ccenv_image 2>&3        \
305cb2
				| grep -i 'MH_MAGIC'         \
305cb2
					> /dev/null; then
305cb2
			ccenv_cc_binfmt='MACHO'
305cb2
			ccenv_use_otool=yes
305cb2
		fi
400106
	fi
400106
7e34aa
	# MACHO-64 / readelf
7e34aa
	if [ -n "$ccenv_readany" ] && [ -z "$ccenv_cc_binfmt" ]; then
15a870
		if $ccenv_readany -h $ccenv_image 2>&3                    \
305cb2
				| grep -i 'Magic:' | sed -e 's/[ ]*//g'   \
305cb2
				| grep -i '(0xfeedfacf)'                  \
305cb2
					> /dev/null; then
305cb2
			ccenv_cc_binfmt='MACHO'
305cb2
			ccenv_use_readelf=yes
305cb2
		fi
7e34aa
	fi
7e34aa
7e34aa
	# MACHO-32 / readelf
7e34aa
	if [ -n "$ccenv_readany" ] && [ -z "$ccenv_cc_binfmt" ]; then
15a870
		if $ccenv_readany -h $ccenv_image 2>&3                    \
305cb2
				| grep -i 'Magic:' | sed -e 's/[ ]*//g'   \
305cb2
				| grep -i '(0xcafebabe)'                  \
305cb2
					> /dev/null; then
305cb2
			ccenv_cc_binfmt='MACHO'
305cb2
			ccenv_use_readelf=yes
305cb2
		fi
7e34aa
	fi
7e34aa
7e34aa
	# MACHO / readobj
7e34aa
	if [ -n "$ccenv_readobj" ] && [ -z "$ccenv_cc_binfmt" ]; then
15a870
		if $ccenv_readobj $ccenv_image 2>&3                  \
1098c4
				| grep -i 'Format:'                  \
1098c4
				| sed  -e 's/ /_/g'                  \
305cb2
				| grep -i '_Mach-O_'                 \
305cb2
					> /dev/null; then
305cb2
			ccenv_cc_binfmt='MACHO'
305cb2
			ccenv_use_readobj=yes
305cb2
		fi
7e34aa
	fi
7e34aa
7e34aa
	# MACHO / objdump (llvm)
7e34aa
	if [ -n "$ccenv_objdump" ] && [ -z "$ccenv_cc_binfmt" ]; then
305cb2
		if $ccenv_objdump -section-headers $ccenv_image  \
15a870
					2>&3                     \
305cb2
				| grep -i 'file format Mach-O'   \
305cb2
					> /dev/null; then
305cb2
			ccenv_cc_binfmt='MACHO'
305cb2
			ccenv_use_objdump=yes
305cb2
		fi
7e34aa
	fi
7e34aa
7e34aa
	# MACHO / objdump (bfd)
7e34aa
	if [ -n "$ccenv_objdump" ] && [ -z "$ccenv_cc_binfmt" ]; then
15a870
		$ccenv_objdump -h  $ccenv_image 2>&3              \
7e34aa
			| grep -i 'file format Mach-O'            \
7e34aa
				> /dev/null                       \
7e34aa
		&& ccenv_cc_binfmt='MACHO'                        \
7e34aa
		&& ccenv_use_objdump=yes
7e34aa
	fi
7e34aa
7e34aa
	# PE / objdump (bfd)
7e34aa
	if [ -n "$ccenv_objdump" ] && [ -z "$ccenv_cc_binfmt" ]; then
15a870
		if $ccenv_objdump -h  $ccenv_image 2>&3        \
305cb2
				| grep -i 'file format pei-'   \
305cb2
					> /dev/null; then
305cb2
			ccenv_cc_binfmt='PE'
305cb2
			ccenv_use_bfd_objdump=yes
305cb2
		fi
7e34aa
	fi
28f188
28f188
	ccenv_attr_epilog "$ccenv_cc_binfmt"
7e34aa
}
7e34aa
7e34aa
ccenv_set_os_pe()
7e34aa
{
bf7ba6
	if [ "$ccenv_cc_environment" = 'freestanding' ]; then
7e34aa
		case "$ccenv_cchost" in
7e34aa
			*-midipix | *-midipix-* )
7e34aa
				ccenv_os='midipix' ;;
7e34aa
			*-mingw | *-mingw32 | *-mingw64 )
7e34aa
				ccenv_os='mingw' ;;
7e34aa
			*-mingw-* | *-mingw32-* | *-mingw64 )
7e34aa
				ccenv_os='mingw' ;;
7e34aa
			*-msys | *-msys2 | *-msys-* | *-msys2-* )
7e34aa
				ccenv_os='msys' ;;
7e34aa
			*-cygwin | *-cygwin-* )
7e34aa
				ccenv_os='cygwin' ;;
7e34aa
		esac
7e34aa
	fi
7e34aa
7e34aa
	if [ -n "$ccenv_os" ]; then
7e34aa
		return 0
7e34aa
	fi
7e34aa
7e34aa
	if [ -n "$ccenv_use_perk" ]; then
7e34aa
		ccenv_framework=$($ccenv_perk -y $ccenv_image)
7e34aa
		ccenv_os=${ccenv_framework#*-*-*-*}
7e34aa
	fi
7e34aa
7e34aa
	if [ -z "$ccenv_os" ] && [ -n "$ccenv_objdump_bfd" ]; then
7e34aa
		$ccenv_objdump_bfd -x $ccenv_image | grep -i 'DLL Name' \
7e34aa
			| grep 'cygwin1.dll' > /dev/null                \
7e34aa
		&& ccenv_os='cygwin'
7e34aa
	fi
7e34aa
7e34aa
	if [ -z "$ccenv_os" ] && [ -n "$ccenv_objdump_bfd" ]; then
7e34aa
		$ccenv_objdump_bfd -x $ccenv_image | grep -i 'DLL Name' \
7e34aa
			| grep 'msys-2.0.dll' > /dev/null               \
7e34aa
		&& ccenv_os='msys'
7e34aa
	fi
7e34aa
7e34aa
	if [ -z "$ccenv_os" ] && [ -n "$ccenv_objdump_bfd" ]; then
7e34aa
		$ccenv_objdump_bfd -x $ccenv_image          \
7e34aa
			| grep -i 'DLL Name' | grep '.CRT'  \
7e34aa
				> /dev/null                 \
7e34aa
		&& $ccenv_objdump_bfd -x $ccenv_image       \
7e34aa
			| grep -i 'DLL Name' | grep '.bss'  \
7e34aa
				> /dev/null                 \
7e34aa
		&& $ccenv_objdump_bfd -x $ccenv_image       \
7e34aa
			| grep -i 'DLL Name' | grep '.tls'  \
7e34aa
				> /dev/null                 \
7e34aa
		&& ccenv_os='mingw'
7e34aa
	fi
7e34aa
}
7e34aa
7e34aa
ccenv_set_os_macho()
7e34aa
{
7e34aa
	case "$ccenv_cchost" in
7e34aa
		*-apple-darwin* )
7e34aa
			ccenv_os='darwin' ;;
7e34aa
	esac
7e34aa
}
7e34aa
7e34aa
ccenv_set_os()
7e34aa
{
28f188
	ccenv_attr_prolog 'os name'
28f188
7e34aa
	case "$ccenv_cc_binfmt" in
7e34aa
		PE )
7e34aa
			ccenv_set_os_pe ;;
7e34aa
		MACHO )
7e34aa
			ccenv_set_os_macho ;;
7e34aa
	esac
7e34aa
7e34aa
	if [ -n "$ccenv_os" ]; then
28f188
		ccenv_attr_epilog "$ccenv_os"
7e34aa
		return 0
7e34aa
	fi
7e34aa
7e34aa
	case "$ccenv_cchost" in
7e34aa
		*-*-*-* )
18920b
			ccenv_tip=${ccenv_cchost%-*}
7e34aa
			ccenv_os=${ccenv_tip#*-*-}
7e34aa
			;;
18920b
		*-*-musl | *-*-gnu )
18920b
			ccenv_tip=${ccenv_cchost%-*}
7e34aa
			ccenv_os=${ccenv_tip#*-}
7e34aa
			;;
18920b
		*-*-* )
18920b
			ccenv_os=${ccenv_cchost#*-*-}
18920b
			;;
18920b
		*-* )
18920b
			ccenv_os=${ccenv_cchost#*-}
18920b
			;;
7e34aa
	esac
7e34aa
7e34aa
	if [ -z "$ccenv_os" ]; then
7e34aa
		ccenv_os='anyos'
7e34aa
	fi
28f188
28f188
	ccenv_attr_epilog "$ccenv_os"
7e34aa
}
7e34aa
7e34aa
ccenv_set_os_flags()
7e34aa
{
7e34aa
	case "$ccenv_os" in
7e34aa
		darwin )
7e34aa
			ccenv_cflags_os='-D_DARWIN_C_SOURCE'
7e34aa
			ccenv_cflags_pic='-fPIC'
7e34aa
			;;
7e34aa
		midipix )
7e34aa
			ccenv_cflags_os=
7e34aa
			ccenv_cflags_pic='-fPIC'
7e34aa
			;;
7e34aa
		cygwin )
7e34aa
			ccenv_cflags_os=
7e34aa
			ccenv_cflags_pic=
7e34aa
			;;
7e34aa
		msys | msys* | mingw | mingw* )
7e34aa
			ccenv_cflags_os='-U__STRICT_ANSI__'
7e34aa
			ccenv_cflags_pic=
7e34aa
			;;
7e34aa
		* )
7e34aa
			ccenv_cflags_os=
7e34aa
			ccenv_cflags_pic='-fPIC'
7e34aa
			;;
7e34aa
	esac
7e34aa
}
7e34aa
7e34aa
ccenv_set_os_semantics()
7e34aa
{
7e34aa
	# binary_format - core_api - ex_api - dependency_resolution
7e34aa
28f188
	ccenv_attr_prolog 'os semantics'
28f188
7e34aa
	case "$ccenv_os" in
7e34aa
		linux )
7e34aa
			ccenv_os_semantics='elf-posix-linux-ldso'
7e34aa
			;;
7e34aa
		bsd )
7e34aa
			ccenv_os_semantics='elf-posix-bsd-ldso'
7e34aa
			;;
7e34aa
		darwin )
7e34aa
			ccenv_os_semantics='macho-posix-osx-ldso'
7e34aa
			;;
7e34aa
		midipix )
7e34aa
			ccenv_os_semantics='pe-posix-winnt-ldso'
7e34aa
			;;
7e34aa
		cygwin )
7e34aa
			ccenv_os_semantics='pe-hybrid-winnt-unsafe'
7e34aa
			;;
7e34aa
		msys )
7e34aa
			ccenv_os_semantics='pe-hybrid-winnt-unsafe'
7e34aa
			;;
7e34aa
		mingw )
7e34aa
			ccenv_os_semantics='pe-win32-winnt-unsafe'
7e34aa
			;;
7e34aa
	esac
7e34aa
7e34aa
	if [ -n "$ccenv_os_semantics" ]; then
28f188
		ccenv_attr_epilog "$ccenv_os_semantics"
7e34aa
		return 0
7e34aa
	fi
7e34aa
7e34aa
	if [ -n "$ccenv_cc_binfmt" ]; then
7e34aa
		ccenv_os_semantics_pattern='%s-posix-anyos-unknown'
7e34aa
		ccenv_os_semantics=$(printf                   \
7e34aa
				"$ccenv_os_semantics_pattern"  \
7e34aa
				"$ccenv_cc_binfmt"              \
7e34aa
			| tr '[:upper:]' '[:lower:]')
7e34aa
	else
7e34aa
		ccenv_os_semantics='unknown-posix-anyos-unknown'
7e34aa
	fi
28f188
28f188
	ccenv_attr_epilog "$ccenv_os_semantics"
7e34aa
}
7e34aa
821557
ccenv_set_os_dso_format()
821557
{
821557
	ccenv_attr_prolog 'os dso format'
821557
821557
	case "$ccenv_cc_arfmt" in
821557
		common )
821557
			ccenv_cc_sofmt="$ccenv_cc_binfmt"
821557
			;;
821557
821557
		bigaf )
821557
			ccenv_libgcc_s_a_header=$(od -b -N8             \
821557
				$($ccenv_cc -print-file-name=libgcc_s.a) \
821557
					2>/dev/null                       \
821557
					| head -n1)
821557
821557
			ccenv_libgcc_s_so_header=$(od -b -N8             \
821557
				$($ccenv_cc -print-file-name=libgcc_s.so) \
821557
					2>/dev/null                        \
821557
					| head -n1)
821557
821557
			if [ "$ccenv_libgcc_s_a_header" = "$ccenv_bigaf_header" ]; then
821557
				ccenv_cc_sofmt='bigaf'
821557
			elif [ "$ccenv_libgcc_s_so_header" = "$ccenv_bigaf_header" ]; then
821557
				ccenv_cc_sofmt='bigaf'
821557
			else
821557
				ccenv_cc_sofmt="$ccenv_cc_binfmt"
821557
			fi
821557
			;;
821557
821557
		aiaff )
821557
			ccenv_libgcc_s_a_header=$(od -b -N8               \
821557
				$($ccenv_cc -print-file-name=libgcc_s.a) \
821557
					| head -n1)
821557
821557
			ccenv_libgcc_s_so_header=$(od -b -N8               \
821557
				$($ccenv_cc -print-file-name=libgcc_s.so) \
821557
					| head -n1)
821557
821557
			if [ "$ccenv_libgcc_s_a_header" = "$ccenv_aiaff_header" ]; then
821557
				ccenv_cc_sofmt='aiaff'
821557
			elif [ "$ccenv_libgcc_s_so_header" = "$ccenv_aiaff_header" ]; then
821557
				ccenv_cc_sofmt='aiaff'
821557
			else
821557
				ccenv_cc_sofmt="$ccenv_cc_binfmt"
821557
			fi
821557
			;;
821557
	esac
821557
e13b1a
	if [ "$ccenv_cfgtype" = 'host' ]; then
e13b1a
		case "$ccenv_cc_sofmt" in
e13b1a
			bigaf | aiaff )
e13b1a
				mb_shared_lib_cmd='$(AR) -rcs'
e13b1a
				mb_shared_lib_ldflags=
e13b1a
				;;
e13b1a
e13b1a
			* )
e13b1a
				mb_shared_lib_cmd='$(CC) -shared -o'
e13b1a
				mb_shared_lib_ldflags='$(LDFLAGS_SHARED)'
e13b1a
				;;
e13b1a
		esac
e13b1a
	fi
e13b1a
821557
	ccenv_attr_epilog "$ccenv_cc_sofmt"
821557
}
821557
7e34aa
ccenv_set_os_dso_exrules()
7e34aa
{
28f188
	ccenv_attr_prolog 'os dso exrules'
28f188
7e34aa
	case "$ccenv_os" in
7e34aa
		midipix )
7e34aa
			ccenv_os_dso_exrules='pe-mdso'
7e34aa
			;;
7e34aa
		* )
7e34aa
			if [ "$ccenv_cc_binfmt" = 'PE' ]; then
7e34aa
				ccenv_os_dso_exrules='pe-dlltool'
7e34aa
			else
7e34aa
				ccenv_os_dso_exrules='default'
7e34aa
			fi
7e34aa
	esac
28f188
28f188
	ccenv_attr_epilog "$ccenv_os_dso_exrules"
7e34aa
}
7e34aa
7e34aa
ccenv_set_os_dso_linkage()
7e34aa
{
7e34aa
	# todo: PIC, PIE, and friends
28f188
	ccenv_attr_prolog 'os linkage'
7e34aa
	ccenv_os_dso_linkage='default'
28f188
	ccenv_attr_epilog "$ccenv_os_dso_linkage"
7e34aa
}
7e34aa
7e34aa
ccenv_set_os_dso_patterns_darwin()
7e34aa
{
7e34aa
	ccenv_os_app_prefix=
7e34aa
	ccenv_os_app_suffix=
7e34aa
7e34aa
	ccenv_os_lib_prefix=lib
7e34aa
	ccenv_os_lib_suffix=.dylib
7e34aa
7e34aa
	ccenv_os_implib_ext=.invalid
7e34aa
	ccenv_os_libdef_ext=.invalid
7e34aa
7e34aa
	ccenv_os_archive_ext=.a
7e34aa
	ccenv_os_soname=symlink
7e34aa
7e34aa
	ccenv_os_lib_prefixed_suffix=
7e34aa
	ccenv_os_lib_suffixed_suffix='$(OS_LIB_SUFFIX)'
7e34aa
}
7e34aa
7e34aa
ccenv_set_os_dso_patterns_mdso()
7e34aa
{
7e34aa
	ccenv_os_app_prefix=
7e34aa
	ccenv_os_app_suffix=
7e34aa
7e34aa
	ccenv_os_lib_prefix=lib
7e34aa
	ccenv_os_lib_suffix=.so
7e34aa
7e34aa
	ccenv_os_implib_ext=.lib.a
7e34aa
	ccenv_os_libdef_ext=.so.def
7e34aa
7e34aa
	ccenv_os_archive_ext=.a
7e34aa
	ccenv_os_soname=symlink
7e34aa
7e34aa
	ccenv_os_lib_prefixed_suffix='$(OS_LIB_SUFFIX)'
7e34aa
	ccenv_os_lib_suffixed_suffix=
7e34aa
}
7e34aa
7e34aa
ccenv_set_os_dso_patterns_dlltool()
7e34aa
{
7e34aa
	ccenv_os_app_prefix=
7e34aa
	ccenv_os_app_suffix=.exe
7e34aa
7e34aa
	ccenv_os_lib_prefix=lib
7e34aa
	ccenv_os_lib_suffix=.dll
7e34aa
7e34aa
	ccenv_os_implib_ext=.dll.a
7e34aa
	ccenv_os_libdef_ext=.def
7e34aa
7e34aa
	ccenv_os_archive_ext=.a
7e34aa
	ccenv_os_soname=copy
7e34aa
7e34aa
	ccenv_os_lib_prefixed_suffix='$(OS_LIB_SUFFIX)'
7e34aa
	ccenv_os_lib_suffixed_suffix=
7e34aa
}
7e34aa
7e34aa
ccenv_set_os_dso_patterns_default()
7e34aa
{
7e34aa
	ccenv_os_app_prefix=
7e34aa
	ccenv_os_app_suffix=
7e34aa
7e34aa
	ccenv_os_lib_prefix=lib
7e34aa
	ccenv_os_lib_suffix=.so
7e34aa
7e34aa
	ccenv_os_implib_ext=.invalid
7e34aa
	ccenv_os_libdef_ext=.invalid
7e34aa
7e34aa
	ccenv_os_archive_ext=.a
7e34aa
	ccenv_os_soname=symlink
7e34aa
7e34aa
	ccenv_os_lib_prefixed_suffix='$(OS_LIB_SUFFIX)'
7e34aa
	ccenv_os_lib_suffixed_suffix=
7e34aa
}
7e34aa
7e34aa
ccenv_set_os_dso_patterns()
7e34aa
{
7e34aa
	# sover: .so.x.y.z
7e34aa
	# verso: .x.y.z.so
7e34aa
7e34aa
	case "$ccenv_os" in
7e34aa
		darwin )
7e34aa
			ccenv_set_os_dso_patterns_darwin
7e34aa
			;;
7e34aa
		midipix )
7e34aa
			ccenv_set_os_dso_patterns_mdso
7e34aa
			;;
7e34aa
		cygwin | msys | mingw )
7e34aa
			ccenv_set_os_dso_patterns_dlltool
7e34aa
			;;
7e34aa
		* )
7e34aa
			ccenv_set_os_dso_patterns_default
7e34aa
			;;
7e34aa
	esac
7e34aa
}
7e34aa
b39a10
ccenv_set_os_pe_switches()
b39a10
{
6173e9
	if [ "$ccenv_cc_binfmt" = 'PE' ] && [ -z "$ccenv_pe_subsystem" ]; then
b39a10
		case "$ccenv_os" in
b39a10
			midipix | mingw )
b39a10
				ccenv_pe_subsystem='windows'
b39a10
				;;
b39a10
			* )
b39a10
				ccenv_pe_subsystem='console'
b39a10
				;;
b39a10
		esac
b39a10
	fi
b39a10
}
b39a10
7e34aa
ccenv_output_defs()
7e34aa
{
7e34aa
	ccenv_in="$mb_project_dir/sofort/ccenv/ccenv.in"
7e34aa
	ccenv_mk="$mb_pwd/ccenv/$ccenv_cfgtype.mk"
7c7144
	ccenv_tmp=
7e34aa
b39a10
	if [ "$ccenv_cc_binfmt" = 'PE' ]; then
b39a10
		ccenv_pe="$mb_project_dir/sofort/ccenv/pedefs.in"
b39a10
		ccenv_in="$ccenv_in $ccenv_pe"
b39a10
	fi
b39a10
7e34aa
	if [ $ccenv_cfgtype = 'native' ]; then
7e34aa
47d854
		ccenv_tmp=$(mktemp ./tmp_XXXXXXXXXXXXXXXX)
7e34aa
635fc9
		sed                                      \
ba3859
				-e 's/^[[:space:]]*$/@/g' \
635fc9
				-e 's/^/NATIVE_/'          \
635fc9
				-e 's/NATIVE_@//g'          \
635fc9
				-e 's/NATIVE_#/#/g'          \
635fc9
				-e 's/       =/=/g'           \
635fc9
				-e 's/       +=/+=/g'          \
40ecd6
			$(printf '%s ' $ccenv_in)               \
40ecd6
				> "$ccenv_tmp"
7e34aa
7e34aa
		ccenv_in="$ccenv_tmp"
47d854
	else
7c7144
		unset ccenv_tmp
7e34aa
	fi
7e34aa
7c7961
	ccenv_var_defs=
d346c1
	ccenv_sed_substs="-e s/@ccenv_cfgtype@/${ccenv_cfgtype}/g"
7c7961
7e34aa
	ccenv_vars=$(cut -d'=' -f1 "$mb_project_dir/sofort/ccenv/ccenv.vars" \
7c7961
		| grep -v '^#');
7e34aa
d346c1
	ccenv_exvars="ccenv_makevar_prefix"
7e34aa
7c7961
	for __var in $(printf '%s' "$ccenv_vars $ccenv_exvars"); do
7c7961
		ccenv_sed_subst=$(printf '%s %s%s%s' \
7c7961
				'-e' "'s^@$__var@"    \
7c7961
				"^___${__var}___"      \
7c7961
				"^g'")
7c7961
7c7961
		ccenv_sed_substs="$ccenv_sed_substs $ccenv_sed_subst"
7c7961
7c7961
		ccenv_var_def=$(printf '%s%s="${%s}"' "-D" "___${__var}___" "${__var}")
7c7961
		eval ccenv_var_defs='"$ccenv_var_defs "$ccenv_var_def'
7c7961
	done
7e34aa
7c7961
	eval sed $ccenv_sed_substs $(printf '%s ' $ccenv_in) \
7c7961
			| eval m4 $ccenv_var_defs -           \
7c7961
			| sed -e 's/[[:blank:]]*$//g'          \
7e34aa
		> "$ccenv_mk"
7e34aa
819fc1
	if [ "$ccenv_cfgtype" = 'host' ]; then
e2dba3
		for __var in $(printf '%s' "$ccenv_vars"); do
819fc1
			ccenv_src_var=$__var
819fc1
			ccenv_dst_var=mb_${__var#*ccenv_}
819fc1
			ccenv_var_expr='${'$ccenv_src_var':-}'
819fc1
			eval $ccenv_dst_var=$ccenv_var_expr
819fc1
819fc1
		done
819fc1
819fc1
		mb_host=$ccenv_host
819fc1
		mb_cchost=$ccenv_cchost
819fc1
	else
e2dba3
		for __var in $(printf '%s' "$ccenv_vars"); do
819fc1
			ccenv_src_var=$__var
819fc1
			ccenv_dst_var=mb_native_${__var#*ccenv_}
819fc1
			ccenv_var_expr='${'$ccenv_src_var':-}'
819fc1
			eval "$ccenv_dst_var=$ccenv_var_expr"
819fc1
		done
819fc1
819fc1
		mb_native_host=$ccenv_host
819fc1
		mb_native_cchost=$ccenv_cchost
819fc1
	fi
47d854
47d854
	if [ -n "${ccenv_tmp:-}" ]; then
47d854
		rm -f "$ccenv_tmp"
47d854
		unset ccenv_tmp
47d854
	fi
e7d9c8
68bef4
68bef4
	if [ "${ccenv_cfgtype}" = 'host' ]; then
68bef4
		ccenv_cflags=$(${mb_make} -n -f "$mb_pwd/Makefile.tmp" .cflags-host)
68bef4
		ccenv_ldflags=$(${mb_make} -n -f "$mb_pwd/Makefile.tmp" .ldflags-host)
68bef4
	else
68bef4
		ccenv_cflags=$(${mb_make} -n -f "$mb_pwd/Makefile.tmp" .cflags-native)
68bef4
		ccenv_ldflags=$(${mb_make} -n -f "$mb_pwd/Makefile.tmp" .ldflags-native)
68bef4
	fi
68bef4
68bef4
	ccenv_cflags="${ccenv_cflags#*: }"
68bef4
	ccenv_ldflags="${ccenv_ldflags#*: }"
68bef4
4ab93f
	ccenv_sysroot=$(eval $ccenv_cc $(printf '%s' "$ccenv_cflags") \
4ab93f
		-print-sysroot 2>/dev/null || true)
4ab93f
68bef4
	eval 'ccenv_'${ccenv_cfgtype}'_cflags'=\'$ccenv_cflags\'
68bef4
	eval 'ccenv_'${ccenv_cfgtype}'_ldflags'=\'$ccenv_ldflags\'
4ab93f
	eval 'ccenv_'${ccenv_cfgtype}'_sysroot'=\'$ccenv_sysroot\'
68bef4
e7d9c8
	eval 'ccenv_'${ccenv_cfgtype}'_cc'=\'$ccenv_cc\'
0705e8
	eval 'ccenv_'${ccenv_cfgtype}'_cc_environment'=\'$ccenv_cc_environment\'
69f10e
	eval 'ccenv_'${ccenv_cfgtype}'_dumpmachine_switch'=\'$ccenv_dumpmachine_switch\'
c8f64e
	eval 'ccenv_'${ccenv_cfgtype}'_pkgconf'=\'$ccenv_pkgconf\'
68bef4
7e34aa
}
7e34aa
b0a311
ccenv_set_cc_sysroot_vars()
b0a311
{
b0a311
	if [ "$ccenv_cfgtype" = 'native' ] || [ -z "$mb_sysroot" ]; then
b0a311
		return 0
b0a311
	fi
b0a311
b0a311
	cfgtest_host_section
b0a311
	ccenv_switch_var="--sysroot=${mb_sysroot}"
b0a311
b0a311
	if cfgtest_compiler_switch_arg "${ccenv_switch_var}"; then
b0a311
		printf '\n# %s sysroot: cflags and ldflags\n' "$ccenv_cfgtype" \
b0a311
			>> "$ccenv_mk"
b0a311
b0a311
		for ccenv_make_var in '_CFLAGS_SYSROOT' '_LDFLAGS_SYSROOT'; do
b0a311
			printf '%-40s= %s\n' "${ccenv_make_var}" "${ccenv_switch_var}" \
b0a311
				>> "$ccenv_mk"
b0a311
		done
b0a311
	else
b0a311
		printf '\n# %s sysroot: cflags and ldflags %s\n' "$ccenv_cfgtype" \
b0a311
			'(not supported: see config.log)'                          \
b0a311
			>> "$ccenv_mk"
b0a311
b0a311
		for ccenv_make_var in '_CFLAGS_SYSROOT' '_LDFLAGS_SYSROOT'; do
b0a311
			printf '%-40s=\n' "${ccenv_make_var}" \
b0a311
				>> "$ccenv_mk"
b0a311
		done
b0a311
	fi
b0a311
}
b0a311
b4c4db
ccenv_set_cc_switch_vars()
b4c4db
{
e806d1
	printf '\n# %s cflags: supported compiler switches\n' "$ccenv_cfgtype" \
e806d1
		>> "$ccenv_mk"
e806d1
b4c4db
	if [ -f $mb_project_dir/project/config/ccswitch.strs ]; then
6fd301
		ccenv_switch_vars=$(cat                            \
b4c4db
			$mb_project_dir/sofort/ccenv/ccswitch.strs  \
b4c4db
			$mb_project_dir/project/config/ccswitch.strs \
6fd301
		| grep -v -e '^#' -e '^-Wl,'                          \
6fd301
		| sort -u)
b4c4db
	else
5ab9c0
		ccenv_switch_vars=$(grep -v -e '^#' -e '^-Wl,'     \
b4c4db
			$mb_project_dir/sofort/ccenv/ccswitch.strs  \
b4c4db
			| sort -u)
b4c4db
	fi
b4c4db
b4c4db
	if [ $ccenv_cfgtype = 'host' ]; then
b4c4db
		ccenv_makevar_prefix='_CFLAGS_'
b4c4db
		cfgtest_host_section
b4c4db
	else
b4c4db
		ccenv_makevar_prefix='_NATIVE_CFLAGS_'
b4c4db
		cfgtest_native_section
b4c4db
	fi
b4c4db
b4c4db
	for ccenv_switch_var in $(printf '%s' "$ccenv_switch_vars"); do
b4c4db
		ccenv_make_var=${ccenv_switch_var%=}
b4c4db
		ccenv_make_var=${ccenv_make_var%,}
b4c4db
b4c4db
		ccenv_make_var=${ccenv_make_var##---}
b4c4db
		ccenv_make_var=${ccenv_make_var##--}
b4c4db
		ccenv_make_var=${ccenv_make_var##-}
b4c4db
b4c4db
		ccenv_make_var=$(printf '%s' "$ccenv_make_var" \
b4c4db
			| sed -e 's/=/_/g' -e 's/-/_/g' -e 's/,/_/g')
b4c4db
b4c4db
		ccenv_make_var="${ccenv_makevar_prefix}${ccenv_make_var}"
b4c4db
b4c4db
		if cfgtest_compiler_switch "$ccenv_switch_var"; then
b4c4db
			ccenv_switch_var=${ccenv_switch_var%=}
b4c4db
			ccenv_switch_var=${ccenv_switch_var%,}
b4c4db
5a94bd
			printf '%-40s= %s\n' "${ccenv_make_var}" "${ccenv_switch_var}" \
b4c4db
				>> "$ccenv_mk"
b4c4db
		else
5a94bd
			printf '%-40s=\n' "${ccenv_make_var}" \
b4c4db
				>> "$ccenv_mk"
b4c4db
		fi
b4c4db
	done
b4c4db
}
b4c4db
5ab9c0
ccenv_set_cc_linker_switch_vars()
5ab9c0
{
5ab9c0
	printf '\n# %s ldflags: supported compiler switches\n' "$ccenv_cfgtype" \
5ab9c0
		>> "$ccenv_mk"
5ab9c0
5ab9c0
	if [ -f $mb_project_dir/project/config/ccswitch.strs ]; then
6fd301
		ccenv_switch_vars=$(cat                            \
5ab9c0
			$mb_project_dir/sofort/ccenv/ccswitch.strs  \
5ab9c0
			$mb_project_dir/project/config/ccswitch.strs \
6fd301
			| grep -e '^-Wl,'                             \
5ab9c0
			| sort -u)
5ab9c0
	else
5ab9c0
		ccenv_switch_vars=$(grep -e '^-Wl,'                \
5ab9c0
			$mb_project_dir/sofort/ccenv/ccswitch.strs  \
5ab9c0
			| sort -u)
5ab9c0
	fi
5ab9c0
5ab9c0
	if [ $ccenv_cfgtype = 'host' ]; then
5ab9c0
		ccenv_makevar_prefix='_LDFLAGS_'
5ab9c0
		cfgtest_host_section
5ab9c0
	else
5ab9c0
		ccenv_makevar_prefix='_NATIVE_LDFLAGS_'
5ab9c0
		cfgtest_native_section
5ab9c0
	fi
5ab9c0
5ab9c0
	for ccenv_switch_var in $(printf '%s' "$ccenv_switch_vars"); do
5ab9c0
		ccenv_make_var=${ccenv_switch_var%=}
5ab9c0
		ccenv_make_var=${ccenv_make_var%,}
5ab9c0
5ab9c0
		ccenv_make_var=${ccenv_make_var##---}
5ab9c0
		ccenv_make_var=${ccenv_make_var##--}
5ab9c0
		ccenv_make_var=${ccenv_make_var##-}
5ab9c0
5ab9c0
		ccenv_make_var=$(printf '%s' "$ccenv_make_var" \
5ab9c0
			| sed -e 's/=/_/g' -e 's/-/_/g' -e 's/,/_/g')
5ab9c0
5ab9c0
		ccenv_make_var="${ccenv_makevar_prefix}${ccenv_make_var}"
5ab9c0
5ab9c0
		if cfgtest_compiler_switch "$ccenv_switch_var"; then
5ab9c0
			ccenv_switch_var=${ccenv_switch_var%=}
5ab9c0
			ccenv_switch_var=${ccenv_switch_var%,}
5ab9c0
5ab9c0
			printf '%-40s= %s\n' "${ccenv_make_var}" "${ccenv_switch_var}" \
5ab9c0
				>> "$ccenv_mk"
5ab9c0
		else
5ab9c0
			printf '%-40s=\n' "${ccenv_make_var}" \
5ab9c0
				>> "$ccenv_mk"
5ab9c0
		fi
5ab9c0
	done
5ab9c0
}
5ab9c0
f4f58f
ccenv_dso_verify()
f4f58f
{
f4f58f
	ccenv_str='int foo(int x){return ++x;}'
f4f58f
	ccenv_cmd="$ccenv_cc -xc - -shared -o a.out"
f4f58f
f4f58f
	rm -f a.out
f4f58f
2c1fff
	printf '%s' "$ccenv_str" | $(printf %s "$ccenv_cmd") \
15a870
		> /dev/null 2>&3              \
f4f58f
	|| mb_disable_shared=yes
f4f58f
f4f58f
	rm -f a.out
f4f58f
}
f4f58f
7e34aa
ccenv_clean_up()
7e34aa
{
7e34aa
	rm -f $ccenv_image
7e34aa
}
7e34aa
7e34aa
ccenv_common_init()
7e34aa
{
7e34aa
	. "$mb_project_dir/sofort/ccenv/ccenv.vars"
7e34aa
7e34aa
	ccenv_cfgtype=$1
7e34aa
	ccenv_cfgfile="$mb_pwd/ccenv/$ccenv_cfgtype.mk"
308fbf
	ccenv_cchost=
7e34aa
7e34aa
	if [ $ccenv_cfgtype = 'native' ]; then
7e34aa
		ccenv_makevar_prefix='NATIVE_'
7e34aa
		ccenv_image='./ccenv/native.a.out'
7e34aa
	else
7e34aa
		ccenv_makevar_prefix=
7e34aa
		ccenv_image='./ccenv/host.a.out'
7e34aa
	fi
7e34aa
7e34aa
	if [ $ccenv_cfgtype = 'native' ]; then
7e34aa
		ccenv_prefixes=
7e34aa
	elif [ -n "$mb_cross_compile" ]; then
7e34aa
		ccenv_prefixes="$mb_cross_compile"
7e34aa
	elif [ -n "$mb_host" ]; then
7e34aa
		ccenv_prefixes="$mb_host-"
7e34aa
	else
7e34aa
		ccenv_prefixes=
7e34aa
	fi
7e34aa
7e34aa
	if [ $ccenv_cfgtype = 'host' ]; then
7e34aa
		ccenv_tflags=
daf8f2
		ccenv_cflags=$(${mb_make} -n -f "$mb_pwd/Makefile.tmp" \
f228da
			OS_DSO_EXRULES=default                          \
f228da
			OS_SONAME=symlink                                \
8b1884
			OS_ARCHIVE_EXT='.a'                               \
f228da
			.cflags-host)
248936
daf8f2
		ccenv_cflags="${ccenv_cflags#*: }"
daf8f2
7e34aa
		ccenv_cc="$mb_user_cc"
7e34aa
		ccenv_cpp="$mb_user_cpp"
7e34aa
		ccenv_cxx="$mb_user_cxx"
b39a10
b39a10
		ccenv_pe_subsystem="$mb_pe_subsystem"
b39a10
		ccenv_pe_image_base="$mb_pe_image_base"
7e34aa
	else
7e34aa
		ccenv_tflags=
daf8f2
		ccenv_cflags=$(${mb_make} -n -f "$mb_pwd/Makefile.tmp" \
f228da
			OS_DSO_EXRULES=default                          \
f228da
			OS_SONAME=symlink                                \
8b1884
			OS_ARCHIVE_EXT='.a'                               \
f228da
			.cflags-native)
f228da
daf8f2
		ccenv_cflags="${ccenv_cflags#*: }"
daf8f2
7e34aa
		ccenv_cc="$mb_native_cc"
7e34aa
		ccenv_cpp="$mb_native_cpp"
7e34aa
		ccenv_cxx="$mb_native_cxx"
b39a10
b39a10
		ccenv_pe_subsystem="$mb_native_pe_subsystem"
b39a10
		ccenv_pe_image_base="$mb_native_pe_image_base"
7e34aa
	fi
7e34aa
}
7e34aa
7e34aa
ccenv_set_characteristics()
7e34aa
{
7e34aa
	ccenv_set_cc_host
7e34aa
	ccenv_set_cc_bits
7e34aa
	ccenv_set_cc_binfmt
28f188
	ccenv_set_cc_underscore
7e34aa
}
7e34aa
7e34aa
ccenv_set_toolchain_variables()
7e34aa
{
7e34aa
	ccenv_common_init $1
7e34aa
	ccenv_set_cc
7e34aa
	ccenv_set_cpp
7e34aa
	ccenv_set_cxx
7e34aa
	ccenv_set_primary_tools
7e34aa
	ccenv_set_tool_variants
7e34aa
	ccenv_set_characteristics
7e34aa
7e34aa
	ccenv_set_os
7e34aa
	ccenv_set_os_flags
7e34aa
	ccenv_set_os_semantics
821557
	ccenv_set_os_dso_format
7e34aa
	ccenv_set_os_dso_exrules
7e34aa
	ccenv_set_os_dso_linkage
7e34aa
	ccenv_set_os_dso_patterns
b39a10
	ccenv_set_os_pe_switches
7e34aa
7e34aa
	ccenv_output_defs
7e34aa
	ccenv_clean_up
b4c4db
b0a311
	ccenv_set_cc_sysroot_vars
b4c4db
	ccenv_set_cc_switch_vars
5ab9c0
	ccenv_set_cc_linker_switch_vars
7e34aa
}
7e34aa
7e34aa
ccenv_set_host_variables()
7e34aa
{
28f188
	output_script_status ${mb_script} \
609c40
		'detect and query host (targeted) system'
28f188
7e34aa
	ccenv_set_toolchain_variables 'host'
f4f58f
	ccenv_dso_verify
7e34aa
}
7e34aa
7e34aa
ccenv_set_native_variables()
7e34aa
{
28f188
	output_script_status ${mb_script} \
609c40
		'detect and query native (local build) system'
28f188
52f7e5
	if [ _$mb_ccenv_skip_native != _yes ]; then
52f7e5
		ccenv_set_toolchain_variables 'native'
52f7e5
	fi
7e34aa
}