Blame sofort/ccenv/ccenv.sh

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