Blame sofort/ccenv/ccenv.sh

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