Blame sofort/ccenv/ccenv.sh

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