Blame sofort/cfgtest/cfgtest.sh

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