Blame sofort/cfgtest/cfgtest.sh

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