Blame sofort/ccenv/ccenv.sh

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