Blame sofort/ccenv/ccenv.sh

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