Blame sofort/ccenv/ccenv.sh

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