Reicher82 / python / sbpython3

Forked from python/sbpython3 3 years ago
Clone

Blame sofort/cfgtest/cfgtest.sh

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