Blame sofort/cfgtest/cfgtest.sh

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