Blame sofort/ccenv/ccenv.sh

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