Blame sofort/cfgtest.sh

5dc983
# cfgtest.sh: sofort's config test framework,
5dc983
# for use from within a project's custom cfgdefs.sh.
5dc983
5dc983
# in the common scenario, target-specific tests are preceded
5dc983
# by a single invocation of cfgtest_target_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
5dc983
# mb_cfgtest_cfgtype: the type of the current test (target/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
5dc983
cfgtest_target_section()
5dc983
{
5dc983
	mb_cfgtest_cc=$(make -s -f "$mb_pwd/Makefile.tmp" .display-cc)
5dc983
	mb_cfgtest_cflags=$(make -s -f "$mb_pwd/Makefile.tmp" .display-cflags)
5dc983
	mb_cfgtest_cfgtype='target'
5dc983
5dc983
	cfgtest_comment 'target-specific tests'
5dc983
}
5dc983
5dc983
5dc983
cfgtest_native_section()
5dc983
{
5dc983
	mb_cfgtest_cc="$mb_native_cc"
5dc983
	mb_cfgtest_cflags="$mb_native_cc_cflags"
5dc983
	mb_cfgtest_cfgtype='native'
5dc983
5dc983
	cfgtest_comment 'native system tests'
5dc983
}
5dc983
5dc983
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
{
5dc983
	if [ $mb_cfgtest_cfgtype = 'target' ]; then
5dc983
		mb_internal_makevar='CFLAGS_CONFIG'
5dc983
	else
5dc983
		mb_internal_makevar='NATIVE_CC_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
{
5dc983
	if [ $mb_cfgtest_cfgtype = 'target' ]; then
5dc983
		mb_internal_makevar='LDFLAGS_CONFIG'
5dc983
	else
5dc983
		mb_internal_makevar='NATIVE_CC_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
{
5dc983
	$mb_cfgtest_cc -E -xc -             \
5dc983
			$mb_cfgtest_cflags  \
5dc983
			--include="$@"      \
5dc983
		< /dev/null                 \
5dc983
		> /dev/null 2>/dev/null     \
5dc983
	|| return
5dc983
2090f5
	mb_internal_str=$(printf '%s%s' '-DHAVE_' "$@"    \
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
5dc983
}
5dc983
5dc983
5dc983
cfgtest_header_absence()
5dc983
{
5dc983
	$mb_cfgtest_cc -E -xc -             \
5dc983
			$mb_cfgtest_cflags  \
5dc983
			--include="$@"      \
5dc983
		< /dev/null                 \
5dc983
		> /dev/null 2>/dev/null     \
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
5dc983
}
5dc983
5dc983
5dc983
cfgtest_interface_presence()
5dc983
{
5dc983
	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
5dc983
	printf 'void * addr = &%;;' "$@"                  \
5dc983
			| $mb_cfgtest_cc -S -xc - -o -    \
5dc983
			  $mb_cfgtest_cflags              \
5dc983
			  $mb_internal_cflags             \
5dc983
                > /dev/null 2>/dev/null                   \
5dc983
	|| return 1
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
5dc983
	return 0
5dc983
}
5dc983
e30826
e30826
cfgtest_decl_presence()
e30826
{
e30826
	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
e30826
	printf 'void * any = (void *)%s;' "$@"            \
e30826
			| $mb_cfgtest_cc -S -xc - -o -    \
e30826
			  $mb_cfgtest_cflags              \
e30826
			  $mb_internal_cflags             \
e30826
                > /dev/null 2>/dev/null                   \
e30826
	|| return 1
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
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
e30826
	return 0
e30826
}
e30826
e30826
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
}