Blame sofort/ccenv/ccenv.sh

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