Blame sofort/ccenv/ccenv.sh

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