Blame sofort/cfgtest/cfgtest.sh

3ae283
# cfgtest.sh: sofort's config test framework,
3ae283
# for use from within a project's custom cfgdefs.sh.
3ae283
df319c
# in the common scenario, host-specific tests are preceded
df319c
# by a single invocation of cfgtest_host_section, whereas
3ae283
# native (build) system tests are preceded by the invocation
3ae283
# of cfgtest_native_section.
3ae283
3ae283
# cfgdefs fraework variables:
3ae283
# mb_cfgtest_cc:      the compiler used for the current test
3ae283
# mb_cfgtest_cflags:  the compiler flags used for the current test
df319c
# mb_cfgtest_cfgtype: the type of the current test (host/native)
3ae283
# mb_cfgtest_makevar: the make variable affected by the current test
3ae283
# mb_cfgtest_headers: headers for ad-hoc inclusion with the current test
3ae283
3ae283
3ae283
cfgtest_newline()
3ae283
{
3ae283
	printf '\n' >> $mb_pwd/cfgdefs.mk
3ae283
}
3ae283
3ae283
3ae283
cfgtest_comment()
3ae283
{
3ae283
	mb_internal_str='#'
3ae283
3ae283
	for mb_internal_arg ; do
3ae283
		mb_internal_str="$mb_internal_str $mb_internal_arg"
3ae283
	done
3ae283
3ae283
	printf '%s\n' "$mb_internal_str" >> $mb_pwd/cfgdefs.mk
3ae283
}
3ae283
3ae283
df319c
cfgtest_host_section()
3ae283
{
53faa0
	mb_cfgtest_cc="$ccenv_host_cc"
df319c
	mb_cfgtest_cfgtype='host'
948962
	mb_cfgtest_cflags=$(make -s -f "$mb_pwd/Makefile.tmp" .cflags-host)
3ae283
df319c
	cfgtest_comment 'host-specific tests'
3ae283
}
3ae283
3ae283
3ae283
cfgtest_native_section()
3ae283
{
3ae283
	mb_cfgtest_cc="$mb_native_cc"
3ae283
	mb_cfgtest_cfgtype='native'
948962
	mb_cfgtest_cflags=$(make -s -f "$mb_pwd/Makefile.tmp" .cflags-native)
3ae283
3ae283
	cfgtest_comment 'native system tests'
3ae283
}
3ae283
3ae283
d90a2d
cfgtest_prolog()
d90a2d
{
d90a2d
	cfgtest_line_dots='.......................'
d90a2d
	cfgtest_line_dots="${cfgtest_line_dots}${cfgtest_line_dots}"
d90a2d
	cfgtest_tool_desc=" == trying ${mb_cfgtest_cfgtype} ${1}: ${2}"
d90a2d
	cfgtest_tool_dlen="${#cfgtest_line_dots}"
d90a2d
d90a2d
	printf '\n%s\n' '________________________' >&3
d90a2d
	printf "cfgtest: probing for ${mb_cfgtest_cfgtype} ${1}: ${2}\n\n" >&3
d90a2d
	printf "%${cfgtest_tool_dlen}.${cfgtest_tool_dlen}s" \
d90a2d
		"${cfgtest_tool_desc}  ${mb_line_dots}"
d90a2d
}
d90a2d
d90a2d
d90a2d
cfgtest_epilog()
d90a2d
{
d90a2d
	cfgtest_line_dots='.......................'
67f07d
	cfgtest_tool_dlen="$((${#cfgtest_line_dots} - ${#2}))"
d90a2d
d90a2d
	printf "%${cfgtest_tool_dlen}.${cfgtest_tool_dlen}s  %s.\n" \
67f07d
		"${cfgtest_line_dots}" "${2}"
d90a2d
67f07d
	if [ "${2}" = '-----' ]; then
67f07d
		printf '\n\ncfgtest: %s is missing or cannot be found.\n' "${1}" >&3
fe2ddd
		printf '%s\n' '------------------------' >&3
fe2ddd
		return 1
67f07d
	elif [ "${2}" = '(error)' ]; then
67f07d
		printf '\n\ncfgtest: %s is not defined or cannot be used.\n' "${1}" >&3
d90a2d
		printf '%s\n' '------------------------' >&3
d90a2d
		return 1
d90a2d
	fi
d90a2d
}
d90a2d
d90a2d
3ae283
cfgtest_makevar_append()
3ae283
{
3ae283
	mb_internal_str='+='
3ae283
3ae283
	for mb_internal_arg ; do
3ae283
		if ! [ -z "$mb_internal_arg" ]; then
3ae283
			mb_internal_str="$mb_internal_str $mb_internal_arg"
3ae283
		fi
3ae283
	done
3ae283
3ae283
	printf '%-24s%s\n' "$mb_cfgtest_makevar" "$mb_internal_str" \
3ae283
		>> $mb_pwd/cfgdefs.mk
3ae283
3ae283
	unset cfgtest_internal_unit_test
3ae283
}
3ae283
3ae283
3ae283
cfgtest_cflags_append()
3ae283
{
df319c
	if [ $mb_cfgtest_cfgtype = 'host' ]; then
3ae283
		mb_internal_makevar='CFLAGS_CONFIG'
3ae283
	else
3ae283
		mb_internal_makevar='NATIVE_CFLAGS'
3ae283
	fi
3ae283
3ae283
	mb_cfgtest_makevar_saved=$mb_cfgtest_makevar
3ae283
	mb_cfgtest_makevar=$mb_internal_makevar
3ae283
3ae283
	cfgtest_makevar_append "$@"
3ae283
	mb_cfgtest_makevar=$mb_cfgtest_makevar_saved
3ae283
}
3ae283
3ae283
3ae283
cfgtest_ldflags_append()
3ae283
{
df319c
	if [ $mb_cfgtest_cfgtype = 'host' ]; then
3ae283
		mb_internal_makevar='LDFLAGS_CONFIG'
3ae283
	else
3ae283
		mb_internal_makevar='NATIVE_LDFLAGS'
3ae283
	fi
3ae283
3ae283
	mb_cfgtest_makevar_saved=$mb_cfgtest_makevar
3ae283
	mb_cfgtest_makevar=$mb_internal_makevar
3ae283
3ae283
	cfgtest_makevar_append "$@"
3ae283
	mb_cfgtest_makevar=$mb_cfgtest_makevar_saved
3ae283
}
3ae283
3ae283
3ae283
cfgtest_header_presence()
3ae283
{
d90a2d
	cfgtest_prolog 'header' "${1}"
d90a2d
d90a2d
	printf '%s -E -xc - \\\n' "$mb_cfgtest_cc"  >&3
d90a2d
d90a2d
	for cfgtest_cflag in $mb_cfgtest_cflags; do
d90a2d
		printf '\t%s \\\n' "$cfgtest_cflag" >&3
d90a2d
	done
d90a2d
d90a2d
	printf '\t%s\n\n' '--include='"${1}" >&3
d90a2d
d90a2d
	cfgtest_cmd=$(printf '%s -E -xc - %s %s'     \
d90a2d
		"$mb_cfgtest_cc" "$mb_cfgtest_cflags" \
d90a2d
		'--include='"${1}")
d90a2d
67f07d
	$(printf '%s' "$cfgtest_cmd")    \
67f07d
		< /dev/null               \
67f07d
		> /dev/null 2>&3           \
67f07d
	|| cfgtest_epilog 'header' '-----'  \
3ae283
	|| return
3ae283
d90a2d
	mb_internal_str=$(printf '%s%s' '-DHAVE_' "${1}"  \
3ae283
			| sed -e 's/\./_/g' -e 's@/@_@g'  \
3ae283
			| tr "[:lower:]" "[:upper:]")
3ae283
3ae283
	if [ -z ${cfgtest_internal_unit_test:-} ]; then
3ae283
		cfgtest_cflags_append "$mb_internal_str"
3ae283
	else
3ae283
		cfgtest_makevar_append "$mb_internal_str"
3ae283
	fi
d90a2d
d90a2d
	printf 'cfgtest: %s header <%s> was found and may be included.\n' \
d90a2d
		"$mb_cfgtest_cfgtype" "${1}" >&3
d90a2d
	printf '%s\n' '------------------------' >&3
d90a2d
67f07d
	cfgtest_epilog 'header' "${1}"
3ae283
}
3ae283
3ae283
3ae283
cfgtest_header_absence()
3ae283
{
d90a2d
	cfgtest_prolog 'header absence' "${1}"
d90a2d
d90a2d
	printf '%s -E -xc - \\\n' "$mb_cfgtest_cc"  >&3
d90a2d
d90a2d
	for cfgtest_cflag in $mb_cfgtest_cflags; do
d90a2d
		printf '\t%s \\\n' "$cfgtest_cflag" >&3
d90a2d
	done
d90a2d
d90a2d
	printf '\t%s\n\n' '--include='"${1}" >&3
d90a2d
d90a2d
	cfgtest_cmd=$(printf '%s -E -xc - %s %s'     \
d90a2d
		"$mb_cfgtest_cc" "$mb_cfgtest_cflags" \
d90a2d
		'--include='"${1}")
d90a2d
67f07d
	$(printf '%s' "$cfgtest_cmd")  \
67f07d
		< /dev/null             \
67f07d
		> /dev/null 2>&3         \
67f07d
	&& cfgtest_epilog 'header' "${1}" \
3ae283
	&& return
3ae283
3ae283
	mb_internal_str=$(printf '%s%s' '-DHAVE_NO_' "$@" \
3ae283
			| sed -e 's/\./_/g' -e 's@/@_@g'  \
3ae283
			| tr "[:lower:]" "[:upper:]")
3ae283
3ae283
	if [ -z ${cfgtest_internal_unit_test:-} ]; then
3ae283
		cfgtest_cflags_append "$mb_internal_str"
3ae283
	else
3ae283
		cfgtest_makevar_append "$mb_internal_str"
3ae283
	fi
d90a2d
d90a2d
	printf 'cfgtest: %s header <%s> may not be included.\n' \
d90a2d
		"$mb_cfgtest_cfgtype" "${1}" >&3
d90a2d
	printf '%s\n' '------------------------' >&3
d90a2d
67f07d
	cfgtest_epilog 'header' '-----'
3ae283
}
3ae283
3ae283
3ae283
cfgtest_interface_presence()
3ae283
{
fe2ddd
	cfgtest_prolog 'interface' "${1}"
fe2ddd
fe2ddd
	mb_internal_cflags=
3ae283
3ae283
	for mb_header in $mb_cfgtest_headers; do
3ae283
		mb_internal_cflags="$mb_internal_cflags --include=$mb_header"
3ae283
	done
3ae283
fe2ddd
	cfgtest_code_snippet=$(printf 'void * addr = &%;;' "${1}")
fe2ddd
fe2ddd
	printf 'printf %s "%s" \\\n' "'%s'" "$cfgtest_code_snippet" >&3
fe2ddd
	printf '| %s -S -xc - -o -' "$mb_cfgtest_cc"  >&3
fe2ddd
fe2ddd
	for cfgtest_cflag in $mb_cfgtest_cflags; do
fe2ddd
		printf ' \\\n\t%s' "$cfgtest_cflag" >&3
fe2ddd
	done
fe2ddd
fe2ddd
	for cfgtest_cflag in $mb_internal_cflags; do
fe2ddd
		printf ' \\\n\t%s' "$cfgtest_cflag" >&3
fe2ddd
	done
fe2ddd
fe2ddd
	printf '\n\n' >&3
fe2ddd
fe2ddd
	cfgtest_cmd=$(printf '%s -S -xc - -o - %s %s'  \
fe2ddd
		"$mb_cfgtest_cc" "$mb_cfgtest_cflags" \
fe2ddd
		"$mb_internal_cflags")
fe2ddd
fe2ddd
	printf '%s' "$cfgtest_code_snippet"     \
fe2ddd
		| $(printf '%s' "$cfgtest_cmd") \
fe2ddd
                > /dev/null 2>&3                \
67f07d
       || cfgtest_epilog 'interface' '(error)'  \
fe2ddd
       || return
3ae283
3ae283
	mb_internal_str=$(printf '%s%s' '-DHAVE_' "$@"  \
3ae283
			| sed -e 's/\./_/g'             \
3ae283
			| tr "[:lower:]" "[:upper:]")
3ae283
3ae283
	if [ -z ${cfgtest_internal_unit_test:-} ]; then
3ae283
		cfgtest_cflags_append "$mb_internal_str"
3ae283
	else
3ae283
		cfgtest_makevar_append "$mb_internal_str"
3ae283
	fi
3ae283
fe2ddd
	printf 'cfgtest: %s interface `%s'"'"' is available.\n' \
fe2ddd
		"$mb_cfgtest_cfgtype" "${1}" >&3
fe2ddd
	printf '%s\n' '------------------------' >&3
fe2ddd
67f07d
	cfgtest_epilog 'interface' "${1}"
fe2ddd
3ae283
	return 0
3ae283
}
3ae283
3ae283
3ae283
cfgtest_decl_presence()
3ae283
{
67f07d
	cfgtest_prolog 'decl' "${1}"
67f07d
67f07d
	mb_internal_cflags=
3ae283
3ae283
	for mb_header in $mb_cfgtest_headers; do
3ae283
		mb_internal_cflags="$mb_internal_cflags --include=$mb_header"
3ae283
	done
3ae283
67f07d
	cfgtest_code_snippet=$(printf 'void * any = (void *)(%s);' "${1}")
67f07d
67f07d
	printf 'printf %s "%s" \\\n' "'%s'" "$cfgtest_code_snippet" >&3
67f07d
	printf '| %s -S -xc - -o -' "$mb_cfgtest_cc"  >&3
67f07d
67f07d
	for cfgtest_cflag in $mb_cfgtest_cflags; do
67f07d
		printf ' \\\n\t%s' "$cfgtest_cflag" >&3
67f07d
	done
67f07d
67f07d
	for cfgtest_cflag in $mb_internal_cflags; do
67f07d
		printf ' \\\n\t%s' "$cfgtest_cflag" >&3
67f07d
	done
67f07d
67f07d
	printf '\n\n' >&3
67f07d
67f07d
	cfgtest_cmd=$(printf '%s -S -xc - -o - %s %s' \
67f07d
		"$mb_cfgtest_cc" "$mb_cfgtest_cflags" \
67f07d
		"$mb_internal_cflags")
67f07d
67f07d
	printf '%s' "$cfgtest_code_snippet"     \
67f07d
		| $(printf '%s' "$cfgtest_cmd") \
67f07d
                > /dev/null 2>&3                \
67f07d
       || cfgtest_epilog 'decl' '(error)'     \
67f07d
       || return
3ae283
3ae283
	# does the argument solely consist of the macro or enum member name?
3ae283
	mb_internal_str=$(printf '%s' "$@" | tr -d '[a-z][A-Z][0-9][_]')
3ae283
3ae283
	if [ -n "$mb_internal_str" ]; then
67f07d
		cfgtest_epilog 'decl' '(defined)'
3ae283
		return 0
3ae283
	fi
3ae283
3ae283
	mb_internal_str=$(printf '%s%s' '-DHAVE_DECL_' "$@"  \
3ae283
			| sed -e 's/\./_/g'                  \
3ae283
			| tr "[:lower:]" "[:upper:]")
3ae283
3ae283
	if [ -z ${cfgtest_internal_unit_test:-} ]; then
3ae283
		cfgtest_cflags_append "$mb_internal_str"
3ae283
	else
3ae283
		cfgtest_makevar_append "$mb_internal_str"
3ae283
	fi
3ae283
67f07d
	printf 'cfgtest: `%s'"'"' is defined for the %s system.\n' \
67f07d
		"${1}" "$mb_cfgtest_cfgtype" >&3
67f07d
	printf '%s\n' '------------------------' >&3
67f07d
67f07d
	cfgtest_epilog 'decl' '(defined)'
67f07d
3ae283
	return 0
3ae283
}
3ae283
3ae283
3ae283
cfgtest_type_size()
3ae283
{
3ae283
	mb_internal_cflags=''
3ae283
	mb_internal_size=''
3ae283
	mb_internal_test='char x[(sizeof(%s) == %s) ? 1 : -1];'
3ae283
3ae283
	for mb_header in $mb_cfgtest_headers; do
3ae283
		mb_internal_cflags="$mb_internal_cflags --include=$mb_header"
3ae283
	done
3ae283
3ae283
	for mb_internal_guess in 8 4 2 1 16 32 64 128; do
3ae283
		if [ -z $mb_internal_size ]; then
3ae283
			mb_internal_type="$@"
3ae283
3ae283
			mb_internal_str=$(printf "$mb_internal_test"    \
3ae283
				"$mb_internal_type"                     \
3ae283
				"$mb_internal_guess")
3ae283
3ae283
			printf '%s' "$mb_internal_str"                  \
3ae283
					| $mb_cfgtest_cc -S -xc - -o -  \
3ae283
					  $mb_cfgtest_cflags            \
3ae283
					  $mb_internal_cflags           \
3ae283
				> /dev/null 2>/dev/null                 \
3ae283
			&& mb_internal_size=$mb_internal_guess
3ae283
		fi
3ae283
	done
3ae283
3ae283
	# unrecognized type, or type size not within range
3ae283
	if [ -z $mb_internal_size ]; then
3ae283
		return 1
3ae283
	fi
3ae283
3ae283
	# -DSIZEOF_TYPE=SIZE
3ae283
	mb_internal_str=$(printf '%s%s=%s' '-DSIZEOF_'        \
3ae283
				"$mb_internal_type"           \
3ae283
				"$mb_internal_size"           \
3ae283
			| sed -e 's/\ /_/g' -e 's/*/P/g'      \
3ae283
			| tr "[:lower:]" "[:upper:]")
3ae283
3ae283
	if [ -z ${cfgtest_internal_unit_test:-} ]; then
3ae283
		cfgtest_cflags_append "$mb_internal_str"
3ae283
	else
3ae283
		cfgtest_makevar_append "$mb_internal_str"
3ae283
	fi
3ae283
3ae283
	return 0
3ae283
}
3ae283
3ae283
3ae283
cfgtest_code_snippet()
3ae283
{
3ae283
	mb_internal_cflags=''
3ae283
	mb_internal_test="$@"
3ae283
3ae283
	for mb_header in $mb_cfgtest_headers; do
3ae283
		mb_internal_cflags="$mb_internal_cflags --include=$mb_header"
3ae283
	done
3ae283
3ae283
	printf '%s' "$mb_internal_test"                 \
3ae283
			| $mb_cfgtest_cc -S -xc - -o -  \
3ae283
			  $mb_cfgtest_cflags            \
3ae283
			  $mb_internal_cflags           \
3ae283
		> /dev/null 2>/dev/null                 \
3ae283
	|| return 1
3ae283
3ae283
	return 0
3ae283
}
3ae283
3ae283
3ae283
cfgtest_library_presence()
3ae283
{
3ae283
	printf 'int main(void){return 0;}'                \
3ae283
			| $mb_cfgtest_cc -o a.out -xc -   \
3ae283
			  $mb_cfgtest_cflags              \
3ae283
			  $@                              \
3ae283
                > /dev/null 2>/dev/null                   \
3ae283
	|| return 1
3ae283
3ae283
	rm -f a.out
3ae283
3ae283
	return 0
3ae283
}
3ae283
3ae283
3ae283
cfgtest_unit_header_presence()
3ae283
{
3ae283
	cfgtest_internal_unit_test='unit_test'
3ae283
	cfgtest_header_presence "$@" || return 1
3ae283
	return 0
3ae283
}
3ae283
3ae283
3ae283
cfgtest_unit_header_absence()
3ae283
{
3ae283
	cfgtest_internal_unit_test='unit_test'
3ae283
	cfgtest_header_absence "$@" || return 1
3ae283
	return 0
3ae283
}
3ae283
3ae283
3ae283
cfgtest_unit_interface_presence()
3ae283
{
3ae283
	cfgtest_internal_unit_test='unit_test'
3ae283
	cfgtest_interface_presence "$@" || return 1
3ae283
	return 0
3ae283
}
3ae283
3ae283
3ae283
cfgtest_unit_decl_presence()
3ae283
{
3ae283
	cfgtest_internal_unit_test='unit_test'
3ae283
	cfgtest_decl_presence "$@" || return 1
3ae283
	return 0
3ae283
}
3ae283
3ae283
3ae283
cfgtest_unit_type_size()
3ae283
{
3ae283
	cfgtest_internal_unit_test='unit_test'
3ae283
	cfgtest_type_size "$@" || return 1
3ae283
	return 0
3ae283
}