Blame sofort/cfgtest/cfgtest.sh

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