Blame sofort/cfgtest/cfgtest.sh

7d6ac9
# cfgtest.sh: sofort's config test framework,
7d6ac9
# for use from within a project's custom cfgdefs.sh.
7d6ac9
7d6ac9
# this file is covered by COPYING.SOFORT.
7d6ac9
7d6ac9
# in the common scenario, host-specific tests are preceded
7d6ac9
# by a single invocation of cfgtest_host_section, whereas
7d6ac9
# native (build) system tests are preceded by the invocation
7d6ac9
# of cfgtest_native_section.
7d6ac9
7d6ac9
# cfgdefs fraework variables:
7d6ac9
# mb_cfgtest_cc:      the compiler used for the current test
28b42a
# mb_cfgtest_pkgconf: the pkgconf utility used for the current test
7d6ac9
# mb_cfgtest_cflags:  the compiler flags used for the current test
7d6ac9
# mb_cfgtest_cfgtype: the type of the current test (host/native)
7d6ac9
# mb_cfgtest_makevar: the make variable affected by the current test
7d6ac9
# mb_cfgtest_headers: headers for ad-hoc inclusion with the current test
7d6ac9
7d6ac9
7d6ac9
cfgtest_newline()
7d6ac9
{
7d6ac9
	printf '\n' >> $mb_pwd/cfgdefs.mk
7d6ac9
}
7d6ac9
7d6ac9
7d6ac9
cfgtest_comment()
7d6ac9
{
7d6ac9
	mb_internal_str='#'
7d6ac9
7d6ac9
	for mb_internal_arg ; do
7d6ac9
		mb_internal_str="$mb_internal_str $mb_internal_arg"
7d6ac9
	done
7d6ac9
7d6ac9
	printf '%s\n' "$mb_internal_str" >> $mb_pwd/cfgdefs.mk
7d6ac9
}
7d6ac9
7d6ac9
7d6ac9
cfgtest_host_section()
7d6ac9
{
7d6ac9
	mb_cfgtest_cc="$ccenv_host_cc"
28b42a
	mb_cfgtest_pkgconf="${ccenv_host_pkgconf:-false}"
7d6ac9
	mb_cfgtest_cfgtype='host'
1f9231
	mb_cfgtest_stdin_input=${ccenv_host_stdin_input:-}
482add
	mb_cfgtest_environment=${ccenv_host_cc_environment:-}
40ac7a
40ac7a
	mb_cfgtest_cflags=$(${mb_make} -n -f "$mb_pwd/Makefile.tmp" \
40ac7a
		OS_DSO_EXRULES=default                              \
40ac7a
		OS_SONAME=symlink                                   \
40ac7a
		OS_ARCHIVE_EXT='.a'                                 \
40ac7a
		.cflags-host)
40ac7a
7d6ac9
	mb_cfgtest_cflags="${mb_cfgtest_cflags#*: }"
7d6ac9
654162
	mb_cfgtest_ldflags=$(${mb_make} -n -f "$mb_pwd/Makefile.tmp" \
654162
		OS_DSO_EXRULES=default                               \
654162
		OS_SONAME=symlink                                    \
654162
		OS_ARCHIVE_EXT='.a'                                  \
654162
		.ldflags-host)
654162
654162
	mb_cfgtest_ldflags="${mb_cfgtest_ldflags#*: }"
7d6ac9
}
7d6ac9
7d6ac9
7d6ac9
cfgtest_native_section()
7d6ac9
{
40ac7a
	mb_cfgtest_cc="$ccenv_native_cc"
28b42a
	mb_cfgtest_pkgconf="${ccenv_native_pkgconf:-false}"
7d6ac9
	mb_cfgtest_cfgtype='native'
1f9231
	mb_cfgtest_stdin_input=${ccenv_native_stdin_input:-}
482add
	mb_cfgtest_environment=${ccenv_native_cc_environment:-}
40ac7a
40ac7a
	mb_cfgtest_cflags=$(${mb_make} -n -f "$mb_pwd/Makefile.tmp" \
40ac7a
		OS_DSO_EXRULES=default                              \
40ac7a
		OS_SONAME=symlink                                   \
40ac7a
		OS_ARCHIVE_EXT='.a'                                 \
40ac7a
		.cflags-native)
40ac7a
7d6ac9
	mb_cfgtest_cflags="${mb_cfgtest_cflags#*: }"
654162
654162
	mb_cfgtest_ldflags=$(${mb_make} -n -f "$mb_pwd/Makefile.tmp" \
654162
		OS_DSO_EXRULES=default                               \
654162
		OS_SONAME=symlink                                    \
654162
		OS_ARCHIVE_EXT='.a'                                  \
654162
		.ldflags-native)
654162
654162
	mb_cfgtest_ldflags="${mb_cfgtest_ldflags#*: }"
7d6ac9
}
7d6ac9
7d6ac9
7d6ac9
cfgtest_prolog()
7d6ac9
{
f89cb8
	cfgtest_line_dots='...........................'
7d6ac9
	cfgtest_line_dots="${cfgtest_line_dots}${cfgtest_line_dots}"
7d6ac9
	cfgtest_tool_desc=" == trying ${mb_cfgtest_cfgtype} ${1}: ${2}"
7d6ac9
	cfgtest_tool_dlen="${#cfgtest_line_dots}"
7d6ac9
7d6ac9
	printf '\n%s\n' '________________________' >&3
7d6ac9
	printf "cfgtest: probing for ${mb_cfgtest_cfgtype} ${1}: ${2}\n\n" >&3
f24afa
f24afa
	if [ "${cfgtest_silent:-}" != 'yes' ]; then
f24afa
		printf "%${cfgtest_tool_dlen}.${cfgtest_tool_dlen}s" \
f24afa
			"${cfgtest_tool_desc}  ${mb_line_dots}"
f24afa
	fi
7d6ac9
}
7d6ac9
7d6ac9
7d6ac9
cfgtest_epilog()
7d6ac9
{
f89cb8
	cfgtest_line_dots='...............'
7d6ac9
	cfgtest_tool_dlen="$((${#cfgtest_line_dots} - ${#2}))"
7d6ac9
f24afa
	if [ "${cfgtest_silent:-}" != 'yes' ]; then
f24afa
		printf "%${cfgtest_tool_dlen}.${cfgtest_tool_dlen}s  %s.\n" \
f24afa
			"${cfgtest_line_dots}" "${2}"
f24afa
	fi
7d6ac9
776394
	if [ "${1}" = 'snippet' ] && [ -f 'a.out' ]; then
7d6ac9
		rm -f 'a.out'
7d6ac9
	fi
7d6ac9
1f9231
	if [ "$mb_cfgtest_stdin_input" = 'no' ]; then
1f9231
		rm 'cfgtest_c3RyaWN0X21vZGUK.c'
1f9231
	fi
1f9231
7d6ac9
	if [ "${1}" = 'snippet' ] && [ "${2}" = '(error)' ]; then
7d6ac9
		printf '\n\ncfgtest: the %s compiler %s %s.\n' \
7d6ac9
			"$mb_cfgtest_cfgtype"                 \
7d6ac9
			'failed to compile the above code'   \
7d6ac9
			"${1}" >&3
7d6ac9
		printf '%s\n' '------------------------' >&3
7d6ac9
		return 1
7d6ac9
	fi
7d6ac9
28b42a
	if [ "${2}" = '-----' ] || [ "${2}" = '(missing)' ]; then
7d6ac9
		printf '\n\ncfgtest: %s %s is missing or cannot be found.\n' "${1}" "${3}" >&3
7d6ac9
		printf '%s\n' '------------------------' >&3
7d6ac9
		return 1
7d6ac9
	elif [ "${1}" = 'size-of-type' ] && [ "${2}" = '(error)' ]; then
7d6ac9
		printf '\n\ncfgtest: could not determine size of type `%s.\n' "${3}'" >&3
7d6ac9
		printf '%s\n' '------------------------' >&3
7d6ac9
		return 1
69e224
	elif [ "${1}" = 'switch' ] && [ "${2}" = '(error)' ]; then
69e224
		printf '\n\ncfgtest: the switch `%s is not supported by the %s compiler.\n' \
69e224
			"${3}'" "$mb_cfgtest_cfgtype" >&3
69e224
		printf '%s\n' '------------------------' >&3
69e224
		return 1
7d6ac9
	elif [ "${2}" = '(error)' ]; then
7d6ac9
		printf '\n\ncfgtest: %s `%s is not defined or cannot be used.\n' "${1}" "${3}'" >&3
7d6ac9
		printf '%s\n' '------------------------' >&3
7d6ac9
		return 1
7d6ac9
	fi
7d6ac9
}
7d6ac9
7d6ac9
7d6ac9
cfgtest_entity_size_prolog()
7d6ac9
{
7d6ac9
	cfgtest_line_dots='.......................'
7d6ac9
	cfgtest_line_dots="${cfgtest_line_dots}${cfgtest_line_dots}"
7d6ac9
	cfgtest_tool_desc=" == checking size of ${mb_cfgtest_cfgtype} type: ${@}"
7d6ac9
	cfgtest_tool_dlen="${#cfgtest_line_dots}"
7d6ac9
7d6ac9
	printf '\n%s\n' '________________________' >&3
7d6ac9
	printf "cfgtest: checking size of ${mb_cfgtest_cfgtype} type: ${@}\n\n" >&3
7d6ac9
7d6ac9
	printf "%${cfgtest_tool_dlen}.${cfgtest_tool_dlen}s" \
7d6ac9
		"${cfgtest_tool_desc}  ${mb_line_dots}"
7d6ac9
}
7d6ac9
7d6ac9
064978
cfgtest_makevar_set()
064978
{
064978
	if [ -n "${@}" ]; then
064978
		mb_internal_str='= '
064978
	else
064978
		mb_internal_str='='
064978
	fi
064978
064978
	printf '%-25s%s%s\n' "$mb_cfgtest_makevar" "$mb_internal_str" "${@}" \
064978
		>> $mb_pwd/cfgdefs.mk
064978
064978
	unset cfgtest_internal_unit_test
064978
}
064978
064978
7d6ac9
cfgtest_makevar_append()
7d6ac9
{
7d6ac9
	mb_internal_str='+='
7d6ac9
7d6ac9
	for mb_internal_arg ; do
7d6ac9
		if ! [ -z "$mb_internal_arg" ]; then
7d6ac9
			mb_internal_str="$mb_internal_str $mb_internal_arg"
7d6ac9
		fi
7d6ac9
	done
7d6ac9
7d6ac9
	printf '%-24s%s\n' "$mb_cfgtest_makevar" "$mb_internal_str" \
7d6ac9
		>> $mb_pwd/cfgdefs.mk
7d6ac9
7d6ac9
	unset cfgtest_internal_unit_test
7d6ac9
}
7d6ac9
7d6ac9
7d6ac9
cfgtest_cflags_append()
7d6ac9
{
7d6ac9
	if [ $mb_cfgtest_cfgtype = 'host' ]; then
7d6ac9
		mb_internal_makevar='CFLAGS_CONFIG'
7d6ac9
	else
7d6ac9
		mb_internal_makevar='NATIVE_CFLAGS'
7d6ac9
	fi
7d6ac9
7d6ac9
	mb_cfgtest_makevar_saved=$mb_cfgtest_makevar
7d6ac9
	mb_cfgtest_makevar=$mb_internal_makevar
7d6ac9
7d6ac9
	cfgtest_makevar_append "$@"
7d6ac9
	mb_cfgtest_makevar=$mb_cfgtest_makevar_saved
7d6ac9
}
7d6ac9
7d6ac9
7d6ac9
cfgtest_ldflags_append()
7d6ac9
{
7d6ac9
	if [ $mb_cfgtest_cfgtype = 'host' ]; then
7d6ac9
		mb_internal_makevar='LDFLAGS_CONFIG'
7d6ac9
	else
7d6ac9
		mb_internal_makevar='NATIVE_LDFLAGS'
7d6ac9
	fi
7d6ac9
7d6ac9
	mb_cfgtest_makevar_saved=$mb_cfgtest_makevar
7d6ac9
	mb_cfgtest_makevar=$mb_internal_makevar
7d6ac9
7d6ac9
	cfgtest_makevar_append "$@"
7d6ac9
	mb_cfgtest_makevar=$mb_cfgtest_makevar_saved
7d6ac9
}
7d6ac9
7d6ac9
7d6ac9
cfgtest_common_init()
7d6ac9
{
7d6ac9
	# cfgtest variables
ebc330
	cfgtest_type="${1:-}"
ebc330
1f9231
	if [ "$mb_cfgtest_stdin_input" = 'no' ]; then
1f9231
		if [ "$cfgtest_type" = 'lib' ]; then
1f9231
			cfgtest_fmt='%s cfgtest_c3RyaWN0X21vZGUK.c -o a.out'
04484d
		elif [ "$cfgtest_type" = 'ldflag' ]; then
04484d
			cfgtest_fmt='%s cfgtest_c3RyaWN0X21vZGUK.c -o a.out'
1f9231
		else
1f9231
			cfgtest_fmt='%s -c cfgtest_c3RyaWN0X21vZGUK.c -o a.out'
1f9231
		fi
1f9231
	elif [ "$cfgtest_type" = 'asm' ]; then
7d6ac9
		cfgtest_fmt='%s -c -xc - -o a.out'
ebc330
	elif [ "$cfgtest_type" = 'lib' ]; then
7d6ac9
		cfgtest_fmt='%s -xc - -o a.out'
04484d
	elif [ "$cfgtest_type" = 'ldflag' ]; then
04484d
		cfgtest_fmt='%s -xc - -o a.out'
7d6ac9
	else
7d6ac9
		cfgtest_fmt='%s -S -xc - -o -'
7d6ac9
	fi
7d6ac9
69e224
ebc330
	if [ "$cfgtest_type" = 'lib' ]; then
7d6ac9
		cfgtest_cmd=$(printf "$cfgtest_fmt %s %s %s" \
7d6ac9
			"$mb_cfgtest_cc"                     \
7d6ac9
			"$mb_cfgtest_cflags"                 \
7d6ac9
			"$mb_cfgtest_ldflags"                \
7d6ac9
			"$cfgtest_libs")
69e224
04484d
	elif [ "$cfgtest_type" = 'ldflag' ]; then
04484d
		cfgtest_cmd=$(printf "$cfgtest_fmt %s %s %s" \
04484d
			"$mb_cfgtest_cc"                     \
04484d
			"$mb_cfgtest_cflags"                 \
04484d
			"$mb_cfgtest_ldflags"                \
04484d
			"$cfgtest_switches")
04484d
69e224
	elif [ "$cfgtest_type" = 'switch' ]; then
69e224
		cfgtest_cmd=$(printf "$cfgtest_fmt %s %s" \
69e224
			"$mb_cfgtest_cc"                  \
69e224
			"$mb_cfgtest_cflags"              \
69e224
			"$cfgtest_switches")
7d6ac9
	else
7d6ac9
		cfgtest_cmd=$(printf "$cfgtest_fmt %s" \
7d6ac9
			"$mb_cfgtest_cc"               \
7d6ac9
			"$mb_cfgtest_cflags")
7d6ac9
	fi
7d6ac9
69e224
714762
	if [ -n "$mb_cfgtest_headers" ]; then
7d6ac9
		cfgtest_inc=$(printf '#include <%s>\n' $mb_cfgtest_headers)
7d6ac9
		cfgtest_src=$(printf '%s\n_\n' "$cfgtest_inc" \
7d6ac9
			| m4 -D_="$cfgtest_code_snippet")
714762
	else
714762
		cfgtest_inc=
714762
		cfgtest_src="$cfgtest_code_snippet"
7d6ac9
	fi
7d6ac9
69e224
7d6ac9
	# config.log
7d6ac9
	printf "$cfgtest_fmt" "$mb_cfgtest_cc" >&3
7d6ac9
76c495
	for cfgtest_cflag in $(printf '%s' "$mb_cfgtest_cflags"); do
7d6ac9
		printf ' \\\n\t%s' "$cfgtest_cflag" >&3
7d6ac9
	done
7d6ac9
faa556
	for cfgtest_ldflag in $(printf '%s' "$mb_cfgtest_ldflags"); do
faa556
		printf ' \\\n\t%s' "$cfgtest_ldflag" >&3
faa556
	done
faa556
ebc330
	if [ "$cfgtest_type" = 'lib' ]; then
76c495
		for cfgtest_lib in $(printf '%s' "$cfgtest_libs"); do
7d6ac9
			printf ' \\\n\t%s' "$cfgtest_lib" >&3
7d6ac9
		done
69e224
04484d
	elif [ "$cfgtest_type" = 'switch' ] || [ "$cfgtest_type" = 'ldflag' ]; then
76c495
		for cfgtest_switch in $(printf '%s' "$cfgtest_switches"); do
69e224
			printf ' \\\n\t%s' "$cfgtest_switch" >&3
69e224
		done
7d6ac9
	fi
7d6ac9
7d6ac9
	printf ' \\\n'                           >&3
7d6ac9
	printf '<< _SRCEOF\n%s\n' "$cfgtest_src" >&3
ae8d99
	printf '_SRCEOF\n\n\n'                   >&3
1f9231
1f9231
	if [ "$mb_cfgtest_stdin_input" = 'no' ]; then
1f9231
		printf '%s' "$cfgtest_src" > 'cfgtest_c3RyaWN0X21vZGUK.c'
1f9231
		cfgtest_src=
1f9231
	fi
7d6ac9
}
7d6ac9
7d6ac9
7d6ac9
cfgtest_header_presence()
7d6ac9
{
7d6ac9
	#init
7d6ac9
	cfgtest_prolog 'header' "${1}"
7d6ac9
7d6ac9
	cfgtest_code_snippet=$(printf '#include <%s>\n' "${1}")
7d6ac9
7d6ac9
	cfgtest_common_init
7d6ac9
7d6ac9
	# execute
7d6ac9
	printf '%s' "$cfgtest_src"                  \
7d6ac9
		| eval $(printf '%s' "$cfgtest_cmd") \
7d6ac9
		> /dev/null 2>&3                      \
7d6ac9
	|| cfgtest_epilog 'header' '-----' "<${1}>"    \
7d6ac9
	|| return
7d6ac9
7d6ac9
	# result
7d6ac9
	mb_internal_str=$(printf '%s%s' '-DHAVE_' "${1}"  \
7d6ac9
			| sed -e 's/\./_/g' -e 's@/@_@g'  \
7d6ac9
			| tr "[:lower:]" "[:upper:]")
7d6ac9
7d6ac9
	if [ -z ${cfgtest_internal_unit_test:-} ]; then
7d6ac9
		cfgtest_cflags_append "$mb_internal_str"
7d6ac9
	else
7d6ac9
		cfgtest_makevar_append "$mb_internal_str"
7d6ac9
	fi
7d6ac9
7d6ac9
	printf 'cfgtest: %s header <%s> was found and may be included.\n' \
7d6ac9
		"$mb_cfgtest_cfgtype" "${1}" >&3
7d6ac9
	printf '%s\n' '------------------------' >&3
7d6ac9
7d6ac9
	cfgtest_epilog 'header' "${1}"
7d6ac9
}
7d6ac9
7d6ac9
7d6ac9
cfgtest_header_absence()
7d6ac9
{
7d6ac9
	#init
7d6ac9
	cfgtest_prolog 'header absence' "${1}"
7d6ac9
7d6ac9
	cfgtest_code_snippet=$(printf '#include <%s>\n' "${1}")
7d6ac9
7d6ac9
	cfgtest_common_init
7d6ac9
7d6ac9
	# execute
7d6ac9
	printf '%s' "$cfgtest_src"                  \
7d6ac9
		| eval $(printf '%s' "$cfgtest_cmd") \
7d6ac9
		> /dev/null 2>&3                      \
7d6ac9
	&& printf 'cfgtest: %s header <%s>: no error.' \
7d6ac9
		"$mb_cfgtest_cfgtype" "${1}" >&3        \
7d6ac9
	&& cfgtest_epilog 'header' "${1}"                \
7d6ac9
	&& return
7d6ac9
7d6ac9
	# result
7d6ac9
	mb_internal_str=$(printf '%s%s' '-DHAVE_NO_' "$@" \
7d6ac9
			| sed -e 's/\./_/g' -e 's@/@_@g'  \
7d6ac9
			| tr "[:lower:]" "[:upper:]")
7d6ac9
7d6ac9
	if [ -z ${cfgtest_internal_unit_test:-} ]; then
7d6ac9
		cfgtest_cflags_append "$mb_internal_str"
7d6ac9
	else
7d6ac9
		cfgtest_makevar_append "$mb_internal_str"
7d6ac9
	fi
7d6ac9
7d6ac9
	printf 'cfgtest: %s header <%s> may not be included.\n' \
7d6ac9
		"$mb_cfgtest_cfgtype" "${1}" >&3
7d6ac9
	printf '%s\n' '------------------------' >&3
7d6ac9
f0c7b4
	cfgtest_epilog 'header' '-----' "${1}"
7d6ac9
}
7d6ac9
7d6ac9
7d6ac9
cfgtest_interface_presence()
7d6ac9
{
7d6ac9
	# init
7d6ac9
	cfgtest_prolog 'interface' "${1}"
7d6ac9
7d6ac9
	cfgtest_code_snippet=$(printf 'void * addr = &%;;\n' "${1}")
7d6ac9
7d6ac9
	cfgtest_common_init
7d6ac9
7d6ac9
	# execute
7d6ac9
	printf '%s' "$cfgtest_src"                    \
7d6ac9
		| eval $(printf '%s' "$cfgtest_cmd")   \
7d6ac9
		> /dev/null 2>&3                        \
7d6ac9
	|| cfgtest_epilog 'interface' '(error)' "${1}"   \
7d6ac9
	|| return
7d6ac9
7d6ac9
	# result
7d6ac9
	mb_internal_str=$(printf '%s%s' '-DHAVE_' "$@"  \
7d6ac9
			| sed -e 's/\./_/g'             \
7d6ac9
			| tr "[:lower:]" "[:upper:]")
7d6ac9
7d6ac9
	if [ -z ${cfgtest_internal_unit_test:-} ]; then
7d6ac9
		cfgtest_cflags_append "$mb_internal_str"
7d6ac9
	else
7d6ac9
		cfgtest_makevar_append "$mb_internal_str"
7d6ac9
	fi
7d6ac9
7d6ac9
	printf 'cfgtest: %s interface `%s'"'"' is available.\n' \
7d6ac9
		"$mb_cfgtest_cfgtype" "${1}" >&3
7d6ac9
	printf '%s\n' '------------------------' >&3
7d6ac9
7d6ac9
	cfgtest_epilog 'interface' "${1}"
7d6ac9
7d6ac9
	return 0
7d6ac9
}
7d6ac9
7d6ac9
7d6ac9
cfgtest_decl_presence()
7d6ac9
{
7d6ac9
	# init
7d6ac9
	cfgtest_prolog 'decl' "${1}"
7d6ac9
7d6ac9
	cfgtest_code_snippet=$(printf 'void * any = (void *)(%s);' "${1}")
7d6ac9
7d6ac9
	cfgtest_common_init
7d6ac9
7d6ac9
	# execute
7d6ac9
	printf '%s' "$cfgtest_src"                  \
7d6ac9
		| eval $(printf '%s' "$cfgtest_cmd") \
7d6ac9
		> /dev/null 2>&3                      \
7d6ac9
	|| cfgtest_epilog 'decl' '(error)' "${1}"      \
7d6ac9
	|| return
7d6ac9
7d6ac9
	# does the argument solely consist of the macro or enum member name?
7d6ac9
	mb_internal_str=$(printf '%s' "$@" | tr -d '[a-z][A-Z][0-9][_]')
7d6ac9
7d6ac9
	if [ -n "$mb_internal_str" ]; then
7d6ac9
		cfgtest_epilog 'decl' '(defined)'
7d6ac9
		return 0
7d6ac9
	fi
7d6ac9
7d6ac9
	# result
7d6ac9
	mb_internal_str=$(printf '%s%s' '-DHAVE_DECL_' "$@"  \
7d6ac9
			| sed -e 's/\./_/g'                  \
7d6ac9
			| tr "[:lower:]" "[:upper:]")
7d6ac9
7d6ac9
	if [ -z ${cfgtest_internal_unit_test:-} ]; then
7d6ac9
		cfgtest_cflags_append "$mb_internal_str"
7d6ac9
	else
7d6ac9
		cfgtest_makevar_append "$mb_internal_str"
7d6ac9
	fi
7d6ac9
7d6ac9
	printf 'cfgtest: `%s'"'"' is defined for the %s system.\n' \
7d6ac9
		"${1}" "$mb_cfgtest_cfgtype" >&3
7d6ac9
	printf '%s\n' '------------------------' >&3
7d6ac9
7d6ac9
	cfgtest_epilog 'decl' '(defined)'
7d6ac9
7d6ac9
	return 0
7d6ac9
}
7d6ac9
7d6ac9
7d6ac9
cfgtest_type_size()
7d6ac9
{
7d6ac9
	cfgtest_entity_size_prolog "$@"
7d6ac9
7d6ac9
	mb_internal_size=''
7d6ac9
	mb_internal_test='char x[(sizeof(%s) == %s) ? 1 : -1];'
7d6ac9
7d6ac9
	for mb_internal_guess in 8 4 2 1 16 32 64 128; do
7d6ac9
		if [ -z $mb_internal_size ]; then
7d6ac9
			printf '# guess %s ===>\n' "$mb_internal_guess" >&3
7d6ac9
7d6ac9
			mb_internal_type="$@"
7d6ac9
7d6ac9
			cfgtest_code_snippet=$(printf "$mb_internal_test" \
7d6ac9
				"$mb_internal_type" "$mb_internal_guess")
7d6ac9
7d6ac9
			cfgtest_common_init
7d6ac9
7d6ac9
			printf '%s' "$cfgtest_src"                  \
7d6ac9
				| eval $(printf '%s' "$cfgtest_cmd") \
7d6ac9
				> /dev/null 2>&3                      \
7d6ac9
			&& mb_internal_size=$mb_internal_guess
7d6ac9
7d6ac9
			printf '\n' >&3
7d6ac9
		fi
7d6ac9
	done
7d6ac9
7d6ac9
	# unrecognized type, or type size not within range
7d6ac9
	if [ -z $mb_internal_size ]; then
7d6ac9
		cfgtest_epilog 'size-of-type' '(error)' "@"
7d6ac9
		return 1
7d6ac9
	fi
7d6ac9
7d6ac9
	# -DSIZEOF_TYPE=SIZE
7d6ac9
	mb_internal_str=$(printf '%s%s=%s' '-DSIZEOF_'        \
7d6ac9
				"$mb_internal_type"           \
7d6ac9
				"$mb_internal_size"           \
7d6ac9
			| sed -e 's/\ /_/g' -e 's/*/P/g'      \
7d6ac9
			| tr "[:lower:]" "[:upper:]")
7d6ac9
7d6ac9
	if [ -z ${cfgtest_internal_unit_test:-} ]; then
7d6ac9
		cfgtest_cflags_append "$mb_internal_str"
7d6ac9
	else
7d6ac9
		cfgtest_makevar_append "$mb_internal_str"
7d6ac9
	fi
7d6ac9
7d6ac9
	printf 'cfgtest: size of type `%s'"'"' determined to be %s\n' \
7d6ac9
		"${@}" "$mb_internal_size" >&3
7d6ac9
	printf '%s\n' '------------------------' >&3
7d6ac9
7d6ac9
	cfgtest_epilog 'size-of-type' "$mb_internal_size"
7d6ac9
7d6ac9
	return 0
7d6ac9
}
7d6ac9
7d6ac9
7d6ac9
cfgtest_code_snippet_asm()
7d6ac9
{
7d6ac9
	# init
7d6ac9
	cfgtest_prolog 'support of code snippet' '<...>'
7d6ac9
7d6ac9
	cfgtest_code_snippet="$@"
7d6ac9
7d6ac9
	cfgtest_common_init 'asm'
7d6ac9
7d6ac9
	# execute
7d6ac9
	cfgtest_ret=1
7d6ac9
7d6ac9
	printf '%s' "$cfgtest_src"                  \
7d6ac9
		| eval $(printf '%s' "$cfgtest_cmd") \
7d6ac9
		> /dev/null 2>&3                      \
7d6ac9
	|| cfgtest_epilog 'snippet' '(error)'          \
7d6ac9
	|| return
7d6ac9
7d6ac9
	# result
7d6ac9
	cfgtest_ret=0
7d6ac9
7d6ac9
	printf 'cfgtest: %s compiler: above code snippet compiled successfully.\n\n' \
7d6ac9
		"$mb_cfgtest_cfgtype" >&3
7d6ac9
7d6ac9
	cfgtest_epilog 'snippet' '(ok)'
7d6ac9
7d6ac9
	return 0
7d6ac9
}
7d6ac9
7d6ac9
e4fb79
cfgtest_macro_definition()
e4fb79
{
e4fb79
	# init
e4fb79
	cfgtest_prolog 'macro definition' "${1}"
e4fb79
e4fb79
	cfgtest_code_snippet=$(printf '%s\n'      \
e4fb79
		"#ifndef ${1}"                     \
e4fb79
		"#error macro ${1} is not defined." \
e4fb79
		"#endif")
e4fb79
e4fb79
	cfgtest_common_init 'macro'
e4fb79
e4fb79
	# execute
e4fb79
	cfgtest_ret=1
e4fb79
e4fb79
	printf '%s' "$cfgtest_src"                  \
e4fb79
		| eval $(printf '%s' "$cfgtest_cmd") \
e4fb79
		> /dev/null 2>&3                      \
e4fb79
	|| cfgtest_epilog 'macro' '(error)' "${1}"     \
e4fb79
	|| return
e4fb79
e4fb79
	# result
e4fb79
	cfgtest_ret=0
e4fb79
e4fb79
	printf 'cfgtest: %s compiler: above macro definition test compiled successfully.\n\n' \
e4fb79
		"$mb_cfgtest_cfgtype" >&3
e4fb79
e4fb79
	cfgtest_epilog 'macro' '(defined)'
e4fb79
e4fb79
	return 0
e4fb79
}
e4fb79
e4fb79
7d6ac9
cfgtest_library_presence()
7d6ac9
{
7d6ac9
	# init
7d6ac9
	cfgtest_libs=
7d6ac9
	cfgtest_spc=
7d6ac9
7d6ac9
	for cfgtest_lib in ${@}; do
7d6ac9
		cfgtest_libs="$cfgtest_libs$cfgtest_spc$cfgtest_lib"
7d6ac9
		cfgtest_spc=' '
7d6ac9
	done
7d6ac9
7d6ac9
	if [ "${1}" = "$cfgtest_libs" ]; then
7d6ac9
		cfgtest_prolog 'library' "${1#*-l}"
7d6ac9
	else
7d6ac9
		cfgtest_prolog 'lib module' '(see config.log)'
7d6ac9
	fi
7d6ac9
783322
	if [ "$mb_cfgtest_environment" = 'freestanding' ]; then
783322
		if [ -z "ccenv_cc_underscore" ]; then
783322
			cfgtest_code_snippet='int start(void){return 0;}'
783322
		else
783322
			cfgtest_code_snippet='int _start(void){return 0;}'
783322
		fi
783322
	else
783322
		cfgtest_code_snippet='int main(void){return 0;}'
783322
	fi
7d6ac9
7d6ac9
	cfgtest_common_init 'lib'
7d6ac9
7d6ac9
	# execute
7d6ac9
	printf '%s' "$cfgtest_src"                  \
7d6ac9
		| eval $(printf '%s' "$cfgtest_cmd") \
7d6ac9
		> /dev/null 2>&3                      \
7d6ac9
	|| cfgtest_epilog 'library' '-----' "$@"       \
7d6ac9
	|| return 1
7d6ac9
7d6ac9
	# result
7d6ac9
	printf 'cfgtest: `%s'"'"' was accepted by the linker driver.\n' \
7d6ac9
		"$cfgtest_libs" >&3
7d6ac9
	printf '%s\n' '------------------------' >&3
7d6ac9
7d6ac9
	cfgtest_epilog 'library' '(present)'
7d6ac9
7d6ac9
	return 0
7d6ac9
}
7d6ac9
7d6ac9
28b42a
cfgtest_package_exists()
28b42a
{
28b42a
	# init
28b42a
	cfgtest_pkg=
28b42a
28b42a
	for cfgtest_arg in ${@}; do
28b42a
		case "$cfgtest_arg" in
28b42a
			-*)
28b42a
				;;
28b42a
28b42a
			*)
28b42a
				cfgtest_pkg="$cfgtest_arg"
28b42a
				;;
28b42a
		esac
28b42a
	done
28b42a
28b42a
	cfgtest_prolog 'package' "$cfgtest_pkg"
28b42a
28b42a
	# execute
28b42a
	"$mb_cfgtest_pkgconf" "${@}"                         \
28b42a
		> /dev/null 2>&3                              \
28b42a
	|| cfgtest_epilog 'package' '(missing)' "$cfgtest_pkg" \
28b42a
	|| return 1
28b42a
28b42a
	# result
28b42a
	printf 'cfgtest: package `%s'"'"' was found.\n' \
28b42a
		"$cfgtest_pkg" >&3
28b42a
	printf '%s\n' '------------------------' >&3
28b42a
28b42a
	cfgtest_epilog 'package' '(exists)' "$cfgtest_pkg"
28b42a
28b42a
	return 0
28b42a
}
28b42a
28b42a
28b42a
cfgtest_package_config()
28b42a
{
28b42a
	# init
28b42a
	if ! cfgtest_package_exists "${@}"; then
28b42a
		return 0
28b42a
	fi
28b42a
28b42a
	cfgtest_pkg=
28b42a
28b42a
	for cfgtest_arg in ${@}; do
28b42a
		case "$cfgtest_arg" in
28b42a
			-*)
28b42a
				;;
28b42a
28b42a
			*)
28b42a
				cfgtest_pkg="$cfgtest_arg"
28b42a
				;;
28b42a
		esac
28b42a
	done
28b42a
28b42a
	cfgtest_makevar_prefix=
28b42a
	cfgtest_pkgconf_prefix=
28b42a
28b42a
	if [ "$mb_cfgtest_cfgtype" = 'native' ]; then
28b42a
		cfgtest_makevar_prefix='_NATIVE'
28b42a
	else
28b42a
		if [ -n "${mb_sysroot}" ]; then
28b42a
			cfgtest_pkgconf_prefix="--define-variable=prefix=${mb_sysroot}"
28b42a
		fi
28b42a
	fi
28b42a
28b42a
	cfgtest_newline
28b42a
	cfgtest_comment 'package config:' "$cfgtest_pkg"
28b42a
28b42a
	# foo.pc
28b42a
	cfgtest_pkgconf_path=$("$mb_cfgtest_pkgconf" \
28b42a
			--path "${@}"                 \
28b42a
		2>/dev/null || true)
28b42a
28b42a
	if [ -z "$cfgtest_pkgconf_path" ]; then
28b42a
		cfgtest_pkgconf_path=$("$mb_cfgtest_pkgconf" \
28b42a
				--debug "${@}" 2>&1           \
28b42a
				| grep ".pc'$"                 \
28b42a
				| head -n1                      \
28b42a
			|| true)
28b42a
28b42a
		cfgtest_pkgconf_path="${cfgtest_pkgconf_path##* \'}"
28b42a
		cfgtest_pkgconf_path="${cfgtest_pkgconf_path%%\'}"
28b42a
	fi
28b42a
28b42a
	mb_cfgtest_makevar=$(printf '%s_PKGCONF_%s'             \
28b42a
			"$cfgtest_makevar_prefix" "$cfgtest_pkg" \
28b42a
		| tr '[[:lower:]]' '[[:upper:]]'                  \
28b42a
		| sed -e 's/-/_/g')
28b42a
28b42a
	cfgtest_makevar_set "$cfgtest_pkgconf_path"
28b42a
28b42a
	# --cflags
28b42a
	cfgtest_pkgconf_path=$("$mb_cfgtest_pkgconf" \
28b42a
			"$cfgtest_pkgconf_prefix"     \
28b42a
			--cflags "${@}"                \
28b42a
		| sed 's/[ \t]*$//')
28b42a
28b42a
	mb_cfgtest_makevar=$(printf '%s_CFLAGS_%s'              \
28b42a
			"$cfgtest_makevar_prefix" "$cfgtest_pkg" \
28b42a
		| tr '[[:lower:]]' '[[:upper:]]'                  \
28b42a
		| sed -e 's/-/_/g')
28b42a
28b42a
	cfgtest_makevar_set "$cfgtest_pkgconf_path"
28b42a
28b42a
	# --ldflags
28b42a
	cfgtest_pkgconf_path=$("$mb_cfgtest_pkgconf" \
28b42a
			"$cfgtest_pkgconf_prefix"     \
28b42a
			--libs "${@}"                  \
28b42a
		| sed 's/[ \t]*$//')
28b42a
28b42a
	mb_cfgtest_makevar=$(printf '%s_LDFLAGS_%s'             \
28b42a
			"$cfgtest_makevar_prefix" "$cfgtest_pkg" \
28b42a
		| tr '[[:lower:]]' '[[:upper:]]'                  \
28b42a
		| sed -e 's/-/_/g')
28b42a
28b42a
	cfgtest_makevar_set "$cfgtest_pkgconf_path"
28b42a
28b42a
	return 0
28b42a
}
28b42a
28b42a
69e224
cfgtest_compiler_switch()
69e224
{
69e224
	# init
69e224
	cfgtest_switches=
69e224
	cfgtest_spc=
69e224
76c495
	for cfgtest_switch in $(printf '%s' "${@}"); do
69e224
		cfgtest_switches="$cfgtest_switches$cfgtest_spc$cfgtest_switch"
69e224
		cfgtest_spc=' '
69e224
	done
69e224
69e224
	if [ "${1}" = "$cfgtest_switches" ]; then
69e224
		cfgtest_prolog 'compiler switch' "$cfgtest_switches"
69e224
	else
69e224
		cfgtest_prolog 'compiler switch combination' "$cfgtest_switches"
69e224
	fi
69e224
04484d
	case "${1}" in
04484d
		-Wl,*)
482add
			if [ "$mb_cfgtest_environment" = 'freestanding' ]; then
482add
				cfgtest_switches="$cfgtest_switches -nostdlib -nostartfiles"
482add
482add
				if [ -z "ccenv_cc_underscore" ]; then
482add
					cfgtest_code_snippet='int start(void){return 0;}'
482add
				else
482add
					cfgtest_code_snippet='int _start(void){return 0;}'
482add
				fi
482add
			else
482add
				cfgtest_code_snippet='int main(void){return 0;}'
482add
			fi
482add
04484d
			cfgtest_common_init 'ldflag'
04484d
			;;
04484d
04484d
		*)
04484d
			cfgtest_code_snippet=
04484d
			cfgtest_common_init 'switch'
04484d
			;;
04484d
	esac
69e224
69e224
	# execute
69e224
	printf '%s' "$cfgtest_src"                  \
69e224
		| eval $(printf '%s' "$cfgtest_cmd") \
69e224
		> /dev/null 2>&3                      \
69e224
	|| cfgtest_epilog 'switch' '(error)' "$@"      \
69e224
	|| return 1
69e224
69e224
	# result
69e224
	printf 'cfgtest: the switch `%s was accepted by the compier.\n' \
69e224
		"$cfgtest_switches'" >&3
69e224
	printf '%s\n' '------------------------' >&3
69e224
69e224
	cfgtest_epilog 'switch' '(accepted)'
69e224
69e224
	return 0
69e224
}
69e224
69e224
7d6ac9
cfgtest_unit_header_presence()
7d6ac9
{
7d6ac9
	cfgtest_internal_unit_test='unit_test'
7d6ac9
	cfgtest_header_presence "$@" || return 1
7d6ac9
	return 0
7d6ac9
}
7d6ac9
7d6ac9
7d6ac9
cfgtest_unit_header_absence()
7d6ac9
{
7d6ac9
	cfgtest_internal_unit_test='unit_test'
7d6ac9
	cfgtest_header_absence "$@" || return 1
7d6ac9
	return 0
7d6ac9
}
7d6ac9
7d6ac9
7d6ac9
cfgtest_unit_interface_presence()
7d6ac9
{
7d6ac9
	cfgtest_internal_unit_test='unit_test'
7d6ac9
	cfgtest_interface_presence "$@" || return 1
7d6ac9
	return 0
7d6ac9
}
7d6ac9
7d6ac9
7d6ac9
cfgtest_unit_decl_presence()
7d6ac9
{
7d6ac9
	cfgtest_internal_unit_test='unit_test'
7d6ac9
	cfgtest_decl_presence "$@" || return 1
7d6ac9
	return 0
7d6ac9
}
7d6ac9
7d6ac9
7d6ac9
cfgtest_unit_type_size()
7d6ac9
{
7d6ac9
	cfgtest_internal_unit_test='unit_test'
7d6ac9
	cfgtest_type_size "$@" || return 1
7d6ac9
	return 0
7d6ac9
}