Blame sofort/cfgtest.sh

32ff4c
# cfgtest.sh: sofort's config test framework,
32ff4c
# for use from within a project's custom cfgdefs.sh.
32ff4c
32ff4c
# in the common scenario, target-specific tests are preceded
32ff4c
# by a single invocation of cfgtest_target_section, whereas
32ff4c
# native (build) system tests are preceded by the invocation
32ff4c
# of cfgtest_native_section.
32ff4c
32ff4c
# cfgdefs fraework variables:
32ff4c
# mb_cfgtest_cc:      the compiler used for the current test
32ff4c
# mb_cfgtest_cflags:  the compiler flags used for the current test
32ff4c
# mb_cfgtest_cfgtype: the type of the current test (target/native)
32ff4c
# mb_cfgtest_makevar: the make variable affected by the current test
32ff4c
# mb_cfgtest_headers: headers for ad-hoc inclusion with the current test
32ff4c
32ff4c
32ff4c
cfgtest_newline()
32ff4c
{
32ff4c
	printf '\n' >> $mb_pwd/cfgdefs.mk
32ff4c
}
32ff4c
32ff4c
32ff4c
cfgtest_comment()
32ff4c
{
32ff4c
	mb_internal_str='#'
32ff4c
32ff4c
	for mb_internal_arg ; do
32ff4c
		mb_internal_str="$mb_internal_str $mb_internal_arg"
32ff4c
	done
32ff4c
32ff4c
	printf '%s\n' "$mb_internal_str" >> $mb_pwd/cfgdefs.mk
32ff4c
}
32ff4c
32ff4c
32ff4c
cfgtest_target_section()
32ff4c
{
32ff4c
	mb_cfgtest_cc=$(make -s -f "$mb_pwd/Makefile.tmp" .display-cc)
32ff4c
	mb_cfgtest_cflags=$(make -s -f "$mb_pwd/Makefile.tmp" .display-cflags)
32ff4c
	mb_cfgtest_cfgtype='target'
32ff4c
32ff4c
	cfgtest_comment 'target-specific tests'
32ff4c
}
32ff4c
32ff4c
32ff4c
cfgtest_native_section()
32ff4c
{
32ff4c
	mb_cfgtest_cc="$mb_native_cc"
32ff4c
	mb_cfgtest_cflags="$mb_native_cc_cflags"
32ff4c
	mb_cfgtest_cfgtype='native'
32ff4c
32ff4c
	cfgtest_comment 'native system tests'
32ff4c
}
32ff4c
32ff4c
32ff4c
cfgtest_makevar_append()
32ff4c
{
32ff4c
	mb_internal_str='+='
32ff4c
32ff4c
	for mb_internal_arg ; do
68a078
		if ! [ -z "$mb_internal_arg" ]; then
68a078
			mb_internal_str="$mb_internal_str $mb_internal_arg"
68a078
		fi
32ff4c
	done
32ff4c
32ff4c
	printf '%-24s%s\n' "$mb_cfgtest_makevar" "$mb_internal_str" \
32ff4c
		>> $mb_pwd/cfgdefs.mk
3eaf7b
3eaf7b
	unset cfgtest_internal_unit_test
32ff4c
}
32ff4c
32ff4c
32ff4c
cfgtest_cflags_append()
32ff4c
{
32ff4c
	if [ $mb_cfgtest_cfgtype = 'target' ]; then
32ff4c
		mb_internal_makevar='CFLAGS_CONFIG'
32ff4c
	else
32ff4c
		mb_internal_makevar='NATIVE_CC_CFLAGS'
32ff4c
	fi
32ff4c
32ff4c
	mb_cfgtest_makevar_saved=$mb_cfgtest_makevar
32ff4c
	mb_cfgtest_makevar=$mb_internal_makevar
32ff4c
32ff4c
	cfgtest_makevar_append "$@"
32ff4c
	mb_cfgtest_makevar=$mb_cfgtest_makevar_saved
32ff4c
}
32ff4c
32ff4c
32ff4c
cfgtest_ldflags_append()
32ff4c
{
32ff4c
	if [ $mb_cfgtest_cfgtype = 'target' ]; then
32ff4c
		mb_internal_makevar='LDFLAGS_CONFIG'
32ff4c
	else
32ff4c
		mb_internal_makevar='NATIVE_CC_LDFLAGS'
32ff4c
	fi
32ff4c
32ff4c
	mb_cfgtest_makevar_saved=$mb_cfgtest_makevar
32ff4c
	mb_cfgtest_makevar=$mb_internal_makevar
32ff4c
32ff4c
	cfgtest_makevar_append "$@"
32ff4c
	mb_cfgtest_makevar=$mb_cfgtest_makevar_saved
32ff4c
}
32ff4c
32ff4c
32ff4c
cfgtest_header_presence()
32ff4c
{
32ff4c
	$mb_cfgtest_cc -E -xc -             \
32ff4c
			$mb_cfgtest_cflags  \
32ff4c
			--include="$@"      \
32ff4c
		< /dev/null                 \
32ff4c
		> /dev/null 2>/dev/null     \
32ff4c
	|| return
32ff4c
c457f5
	mb_internal_str=$(printf '%s%s' '-DHAVE_' "$@"    \
c457f5
			| sed -e 's/\./_/g' -e 's@/@_@g'  \
32ff4c
			| tr "[:lower:]" "[:upper:]")
32ff4c
f1eb02
	if [ -z ${cfgtest_internal_unit_test:-} ]; then
3eaf7b
		cfgtest_cflags_append "$mb_internal_str"
3eaf7b
	else
3eaf7b
		cfgtest_makevar_append "$mb_internal_str"
3eaf7b
	fi
32ff4c
}
32ff4c
32ff4c
32ff4c
cfgtest_header_absence()
32ff4c
{
32ff4c
	$mb_cfgtest_cc -E -xc -             \
32ff4c
			$mb_cfgtest_cflags  \
32ff4c
			--include="$@"      \
32ff4c
		< /dev/null                 \
32ff4c
		> /dev/null 2>/dev/null     \
32ff4c
	&& return
32ff4c
32ff4c
	mb_internal_str=$(printf '%s%s' '-DHAVE_NO_' "$@" \
c457f5
			| sed -e 's/\./_/g' -e 's@/@_@g'  \
32ff4c
			| tr "[:lower:]" "[:upper:]")
32ff4c
f1eb02
	if [ -z ${cfgtest_internal_unit_test:-} ]; then
3eaf7b
		cfgtest_cflags_append "$mb_internal_str"
3eaf7b
	else
3eaf7b
		cfgtest_makevar_append "$mb_internal_str"
3eaf7b
	fi
32ff4c
}
32ff4c
32ff4c
32ff4c
cfgtest_interface_presence()
32ff4c
{
32ff4c
	mb_internal_cflags=''
32ff4c
32ff4c
	for mb_header in $mb_cfgtest_headers; do
32ff4c
		mb_internal_cflags="$mb_internal_cflags --include=$mb_header"
32ff4c
	done
32ff4c
32ff4c
	printf 'void * addr = &%;;' "$@"                  \
32ff4c
			| $mb_cfgtest_cc -S -xc - -o -    \
32ff4c
			  $mb_cfgtest_cflags              \
32ff4c
			  $mb_internal_cflags             \
32ff4c
                > /dev/null 2>/dev/null                   \
32ff4c
	|| return 1
32ff4c
502550
	mb_internal_str=$(printf '%s%s' '-DHAVE_' "$@"  \
502550
			| sed -e 's/\./_/g'             \
502550
			| tr "[:lower:]" "[:upper:]")
502550
f1eb02
	if [ -z ${cfgtest_internal_unit_test:-} ]; then
3eaf7b
		cfgtest_cflags_append "$mb_internal_str"
3eaf7b
	else
3eaf7b
		cfgtest_makevar_append "$mb_internal_str"
3eaf7b
	fi
502550
32ff4c
	return 0
32ff4c
}
32ff4c
73a6fd
73a6fd
cfgtest_decl_presence()
73a6fd
{
73a6fd
	mb_internal_cflags=''
73a6fd
73a6fd
	for mb_header in $mb_cfgtest_headers; do
73a6fd
		mb_internal_cflags="$mb_internal_cflags --include=$mb_header"
73a6fd
	done
73a6fd
73a6fd
	printf 'void * any = (void *)%s;' "$@"            \
73a6fd
			| $mb_cfgtest_cc -S -xc - -o -    \
73a6fd
			  $mb_cfgtest_cflags              \
73a6fd
			  $mb_internal_cflags             \
73a6fd
                > /dev/null 2>/dev/null                   \
73a6fd
	|| return 1
73a6fd
73a6fd
	# does the argument solely consist of the macro or enum member name?
73a6fd
	mb_internal_str=$(printf '%s' "$@" | tr -d '[a-z][A-Z][0-9][_]')
73a6fd
73a6fd
	if [ -n "$mb_internal_str" ]; then
73a6fd
		return 0
73a6fd
	fi
73a6fd
73a6fd
	mb_internal_str=$(printf '%s%s' '-DHAVE_DECL_' "$@"  \
73a6fd
			| sed -e 's/\./_/g'                  \
73a6fd
			| tr "[:lower:]" "[:upper:]")
73a6fd
f1eb02
	if [ -z ${cfgtest_internal_unit_test:-} ]; then
3eaf7b
		cfgtest_cflags_append "$mb_internal_str"
3eaf7b
	else
3eaf7b
		cfgtest_makevar_append "$mb_internal_str"
3eaf7b
	fi
73a6fd
73a6fd
	return 0
73a6fd
}
73a6fd
73a6fd
32ff4c
cfgtest_library_presence()
32ff4c
{
32ff4c
	printf 'int main(void){return 0;}'                \
32ff4c
			| $mb_cfgtest_cc -o a.out -xc -   \
32ff4c
			  $mb_cfgtest_cflags              \
32ff4c
			  $@                              \
32ff4c
                > /dev/null 2>/dev/null                   \
32ff4c
	|| return 1
32ff4c
32ff4c
	rm -f a.out
32ff4c
32ff4c
	return 0
32ff4c
}
3eaf7b
3eaf7b
3eaf7b
cfgtest_unit_header_presence()
3eaf7b
{
3eaf7b
	cfgtest_internal_unit_test='unit_test'
3eaf7b
	cfgtest_header_presence "$@" || return 1
3eaf7b
	return 0
3eaf7b
}
3eaf7b
3eaf7b
3eaf7b
cfgtest_unit_header_absence()
3eaf7b
{
3eaf7b
	cfgtest_internal_unit_test='unit_test'
3eaf7b
	cfgtest_header_absence "$@" || return 1
3eaf7b
	return 0
3eaf7b
}
3eaf7b
3eaf7b
3eaf7b
cfgtest_unit_interface_presence()
3eaf7b
{
3eaf7b
	cfgtest_internal_unit_test='unit_test'
3eaf7b
	cfgtest_interface_presence "$@" || return 1
3eaf7b
	return 0
3eaf7b
}
3eaf7b
3eaf7b
3eaf7b
cfgtest_unit_decl_presence()
3eaf7b
{
3eaf7b
	cfgtest_internal_unit_test='unit_test'
3eaf7b
	cfgtest_decl_presence "$@" || return 1
3eaf7b
	return 0
3eaf7b
}