Blame sofort/ccenv/ccenv.sh

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