Blame sofort/cfgtest/cfgtest.sh

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