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
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
32ff4c
	cfgtest_cflags_append "$mb_internal_str"
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
32ff4c
	cfgtest_cflags_append "$mb_internal_str"
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
502550
	cfgtest_cflags_append "$mb_internal_str"
502550
32ff4c
	return 0
32ff4c
}
32ff4c
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
}