Blame sofort/cfgtest/cfgtest.sh

784103
# cfgtest.sh: sofort's config test framework,
784103
# for use from within a project's custom cfgdefs.sh.
784103
784103
# this file is covered by COPYING.SOFORT.
784103
784103
# in the common scenario, host-specific tests are preceded
784103
# by a single invocation of cfgtest_host_section, whereas
784103
# native (build) system tests are preceded by the invocation
784103
# of cfgtest_native_section.
784103
784103
# cfgdefs fraework variables:
784103
# mb_cfgtest_cc:      the compiler used for the current test
784103
# mb_cfgtest_cflags:  the compiler flags used for the current test
784103
# mb_cfgtest_cfgtype: the type of the current test (host/native)
784103
# mb_cfgtest_makevar: the make variable affected by the current test
784103
# mb_cfgtest_headers: headers for ad-hoc inclusion with the current test
784103
784103
784103
cfgtest_newline()
784103
{
784103
	printf '\n' >> $mb_pwd/cfgdefs.mk
784103
}
784103
784103
784103
cfgtest_comment()
784103
{
784103
	mb_internal_str='#'
784103
784103
	for mb_internal_arg ; do
784103
		mb_internal_str="$mb_internal_str $mb_internal_arg"
784103
	done
784103
784103
	printf '%s\n' "$mb_internal_str" >> $mb_pwd/cfgdefs.mk
784103
}
784103
784103
784103
cfgtest_host_section()
784103
{
784103
	mb_cfgtest_cc="$ccenv_host_cc"
784103
	mb_cfgtest_cfgtype='host'
784103
	mb_cfgtest_cflags=$(${mb_make} -n -f "$mb_pwd/Makefile.tmp" .cflags-host)
784103
	mb_cfgtest_cflags="${mb_cfgtest_cflags#*: }"
784103
784103
	mb_cfgtest_ldflags="$mb_ldflags_cmdline"
784103
	mb_cfgtest_ldflags="$mb_cfgtest_ldflags $mb_ldflags_debug"
784103
	mb_cfgtest_ldflags="$mb_cfgtest_ldflags $mb_ldflags_common"
784103
	mb_cfgtest_ldflags="$mb_cfgtest_ldflags $mb_ldflags_strict"
784103
	mb_cfgtest_ldflags="$mb_cfgtest_ldflags $mb_ldflags_config"
784103
	mb_cfgtest_ldflags="$mb_cfgtest_ldflags $mb_ldflags_sysroot"
784103
	mb_cfgtest_ldflags="$mb_cfgtest_ldflags $mb_ldflags_path"
784103
	mb_cfgtest_ldflags="$mb_cfgtest_ldflags $mb_ldflags_last"
784103
784103
	cfgtest_comment 'host-specific tests'
784103
}
784103
784103
784103
cfgtest_native_section()
784103
{
784103
	mb_cfgtest_cc="$mb_native_cc"
784103
	mb_cfgtest_cfgtype='native'
784103
	mb_cfgtest_cflags=$(${mb_make} -n -f "$mb_pwd/Makefile.tmp" .cflags-native)
784103
	mb_cfgtest_cflags="${mb_cfgtest_cflags#*: }"
784103
	mb_cfgtest_ldflags="$mb_native_ldflags"
784103
784103
	cfgtest_comment 'native system tests'
784103
}
784103
784103
784103
cfgtest_prolog()
784103
{
784103
	cfgtest_line_dots='.......................'
784103
	cfgtest_line_dots="${cfgtest_line_dots}${cfgtest_line_dots}"
784103
	cfgtest_tool_desc=" == trying ${mb_cfgtest_cfgtype} ${1}: ${2}"
784103
	cfgtest_tool_dlen="${#cfgtest_line_dots}"
784103
784103
	printf '\n%s\n' '________________________' >&3
784103
	printf "cfgtest: probing for ${mb_cfgtest_cfgtype} ${1}: ${2}\n\n" >&3
784103
	printf "%${cfgtest_tool_dlen}.${cfgtest_tool_dlen}s" \
784103
		"${cfgtest_tool_desc}  ${mb_line_dots}"
784103
}
784103
784103
784103
cfgtest_epilog()
784103
{
784103
	cfgtest_line_dots='.......................'
784103
	cfgtest_tool_dlen="$((${#cfgtest_line_dots} - ${#2}))"
784103
784103
	printf "%${cfgtest_tool_dlen}.${cfgtest_tool_dlen}s  %s.\n" \
784103
		"${cfgtest_line_dots}" "${2}"
784103
784103
	if [ "${1}" = 'snippet' ] && [ -f = 'a.out' ]; then
784103
		rm -f 'a.out'
784103
	fi
784103
784103
	if [ "${1}" = 'snippet' ] && [ "${2}" = '(error)' ]; then
784103
		printf '\n\ncfgtest: the %s compiler %s %s.\n' \
784103
			"$mb_cfgtest_cfgtype"                 \
784103
			'failed to compile the above code'   \
784103
			"${1}" >&3
784103
		printf '%s\n' '------------------------' >&3
784103
		return 1
784103
	fi
784103
784103
	if [ "${2}" = '-----' ]; then
784103
		printf '\n\ncfgtest: %s %s is missing or cannot be found.\n' "${1}" "${3}" >&3
784103
		printf '%s\n' '------------------------' >&3
784103
		return 1
784103
	elif [ "${1}" = 'size-of-type' ] && [ "${2}" = '(error)' ]; then
784103
		printf '\n\ncfgtest: could not determine size of type `%s.\n' "${3}'" >&3
784103
		printf '%s\n' '------------------------' >&3
784103
		return 1
784103
	elif [ "${2}" = '(error)' ]; then
784103
		printf '\n\ncfgtest: %s `%s is not defined or cannot be used.\n' "${1}" "${3}'" >&3
784103
		printf '%s\n' '------------------------' >&3
784103
		return 1
784103
	fi
784103
}
784103
784103
784103
cfgtest_entity_size_prolog()
784103
{
784103
	cfgtest_line_dots='.......................'
784103
	cfgtest_line_dots="${cfgtest_line_dots}${cfgtest_line_dots}"
784103
	cfgtest_tool_desc=" == checking size of ${mb_cfgtest_cfgtype} type: ${@}"
784103
	cfgtest_tool_dlen="${#cfgtest_line_dots}"
784103
784103
	printf '\n%s\n' '________________________' >&3
784103
	printf "cfgtest: checking size of ${mb_cfgtest_cfgtype} type: ${@}\n\n" >&3
784103
784103
	printf "%${cfgtest_tool_dlen}.${cfgtest_tool_dlen}s" \
784103
		"${cfgtest_tool_desc}  ${mb_line_dots}"
784103
}
784103
784103
784103
cfgtest_makevar_append()
784103
{
784103
	mb_internal_str='+='
784103
784103
	for mb_internal_arg ; do
784103
		if ! [ -z "$mb_internal_arg" ]; then
784103
			mb_internal_str="$mb_internal_str $mb_internal_arg"
784103
		fi
784103
	done
784103
784103
	printf '%-24s%s\n' "$mb_cfgtest_makevar" "$mb_internal_str" \
784103
		>> $mb_pwd/cfgdefs.mk
784103
784103
	unset cfgtest_internal_unit_test
784103
}
784103
784103
784103
cfgtest_cflags_append()
784103
{
784103
	if [ $mb_cfgtest_cfgtype = 'host' ]; then
784103
		mb_internal_makevar='CFLAGS_CONFIG'
784103
	else
784103
		mb_internal_makevar='NATIVE_CFLAGS'
784103
	fi
784103
784103
	mb_cfgtest_makevar_saved=$mb_cfgtest_makevar
784103
	mb_cfgtest_makevar=$mb_internal_makevar
784103
784103
	cfgtest_makevar_append "$@"
784103
	mb_cfgtest_makevar=$mb_cfgtest_makevar_saved
784103
}
784103
784103
784103
cfgtest_ldflags_append()
784103
{
784103
	if [ $mb_cfgtest_cfgtype = 'host' ]; then
784103
		mb_internal_makevar='LDFLAGS_CONFIG'
784103
	else
784103
		mb_internal_makevar='NATIVE_LDFLAGS'
784103
	fi
784103
784103
	mb_cfgtest_makevar_saved=$mb_cfgtest_makevar
784103
	mb_cfgtest_makevar=$mb_internal_makevar
784103
784103
	cfgtest_makevar_append "$@"
784103
	mb_cfgtest_makevar=$mb_cfgtest_makevar_saved
784103
}
784103
784103
784103
cfgtest_common_init()
784103
{
784103
	# cfgtest variables
784103
	if [ "${1:-}" = 'asm' ]; then
784103
		cfgtest_fmt='%s -c -xc - -o a.out'
784103
	elif [ "${1:-}" = 'lib' ]; then
784103
		cfgtest_fmt='%s -xc - -o a.out'
784103
	else
784103
		cfgtest_fmt='%s -S -xc - -o -'
784103
	fi
784103
784103
	if [ "${1:-}" = 'lib' ]; then
784103
		cfgtest_cmd=$(printf "$cfgtest_fmt %s %s %s" \
784103
			"$mb_cfgtest_cc"                     \
784103
			"$mb_cfgtest_cflags"                 \
784103
			"$mb_cfgtest_ldflags"                \
784103
			"$cfgtest_libs")
784103
	else
784103
		cfgtest_cmd=$(printf "$cfgtest_fmt %s" \
784103
			"$mb_cfgtest_cc"               \
784103
			"$mb_cfgtest_cflags")
784103
	fi
784103
784103
	if [ -z "$mb_cfgtest_headers" ] || [ "${1:-}" = 'lib' ]; then
784103
		cfgtest_inc=
784103
		cfgtest_src="$cfgtest_code_snippet"
784103
	else
784103
		cfgtest_inc=$(printf '#include <%s>\n' $mb_cfgtest_headers)
784103
		cfgtest_src=$(printf '%s\n_\n' "$cfgtest_inc" \
784103
			| m4 -D_="$cfgtest_code_snippet")
784103
	fi
784103
784103
	# config.log
784103
	printf "$cfgtest_fmt" "$mb_cfgtest_cc" >&3
784103
784103
	for cfgtest_cflag in $mb_cfgtest_cflags; do
784103
		printf ' \\\n\t%s' "$cfgtest_cflag" >&3
784103
	done
784103
784103
	if [ "${1:-}" = 'lib' ]; then
784103
		for cfgtest_lib in $cfgtest_libs; do
784103
			printf ' \\\n\t%s' "$cfgtest_lib" >&3
784103
		done
784103
	fi
784103
784103
	printf ' \\\n'                           >&3
784103
	printf '<< _SRCEOF\n%s\n' "$cfgtest_src" >&3
784103
	printf '_SRCEOF \n\n\n'                  >&3
784103
}
784103
784103
784103
cfgtest_header_presence()
784103
{
784103
	#init
784103
	cfgtest_prolog 'header' "${1}"
784103
784103
	cfgtest_code_snippet=$(printf '#include <%s>\n' "${1}")
784103
784103
	cfgtest_common_init
784103
784103
	# execute
784103
	printf '%s' "$cfgtest_src"                  \
784103
		| eval $(printf '%s' "$cfgtest_cmd") \
784103
		> /dev/null 2>&3                      \
784103
	|| cfgtest_epilog 'header' '-----' "<${1}>"    \
784103
	|| return
784103
784103
	# result
784103
	mb_internal_str=$(printf '%s%s' '-DHAVE_' "${1}"  \
784103
			| sed -e 's/\./_/g' -e 's@/@_@g'  \
784103
			| tr "[:lower:]" "[:upper:]")
784103
784103
	if [ -z ${cfgtest_internal_unit_test:-} ]; then
784103
		cfgtest_cflags_append "$mb_internal_str"
784103
	else
784103
		cfgtest_makevar_append "$mb_internal_str"
784103
	fi
784103
784103
	printf 'cfgtest: %s header <%s> was found and may be included.\n' \
784103
		"$mb_cfgtest_cfgtype" "${1}" >&3
784103
	printf '%s\n' '------------------------' >&3
784103
784103
	cfgtest_epilog 'header' "${1}"
784103
}
784103
784103
784103
cfgtest_header_absence()
784103
{
784103
	#init
784103
	cfgtest_prolog 'header absence' "${1}"
784103
784103
	cfgtest_code_snippet=$(printf '#include <%s>\n' "${1}")
784103
784103
	cfgtest_common_init
784103
784103
	# execute
784103
	printf '%s' "$cfgtest_src"                  \
784103
		| eval $(printf '%s' "$cfgtest_cmd") \
784103
		> /dev/null 2>&3                      \
784103
	&& printf 'cfgtest: %s header <%s>: no error.' \
784103
		"$mb_cfgtest_cfgtype" "${1}" >&3        \
784103
	&& cfgtest_epilog 'header' "${1}"                \
784103
	&& return
784103
784103
	# result
784103
	mb_internal_str=$(printf '%s%s' '-DHAVE_NO_' "$@" \
784103
			| sed -e 's/\./_/g' -e 's@/@_@g'  \
784103
			| tr "[:lower:]" "[:upper:]")
784103
784103
	if [ -z ${cfgtest_internal_unit_test:-} ]; then
784103
		cfgtest_cflags_append "$mb_internal_str"
784103
	else
784103
		cfgtest_makevar_append "$mb_internal_str"
784103
	fi
784103
784103
	printf 'cfgtest: %s header <%s> may not be included.\n' \
784103
		"$mb_cfgtest_cfgtype" "${1}" >&3
784103
	printf '%s\n' '------------------------' >&3
784103
784103
	cfgtest_epilog 'header' '-----'
784103
}
784103
784103
784103
cfgtest_interface_presence()
784103
{
784103
	# init
784103
	cfgtest_prolog 'interface' "${1}"
784103
784103
	cfgtest_code_snippet=$(printf 'void * addr = &%;;\n' "${1}")
784103
784103
	cfgtest_common_init
784103
784103
	# execute
784103
	printf '%s' "$cfgtest_src"                    \
784103
		| eval $(printf '%s' "$cfgtest_cmd")   \
784103
		> /dev/null 2>&3                        \
784103
	|| cfgtest_epilog 'interface' '(error)' "${1}"   \
784103
	|| return
784103
784103
	# result
784103
	mb_internal_str=$(printf '%s%s' '-DHAVE_' "$@"  \
784103
			| sed -e 's/\./_/g'             \
784103
			| tr "[:lower:]" "[:upper:]")
784103
784103
	if [ -z ${cfgtest_internal_unit_test:-} ]; then
784103
		cfgtest_cflags_append "$mb_internal_str"
784103
	else
784103
		cfgtest_makevar_append "$mb_internal_str"
784103
	fi
784103
784103
	printf 'cfgtest: %s interface `%s'"'"' is available.\n' \
784103
		"$mb_cfgtest_cfgtype" "${1}" >&3
784103
	printf '%s\n' '------------------------' >&3
784103
784103
	cfgtest_epilog 'interface' "${1}"
784103
784103
	return 0
784103
}
784103
784103
784103
cfgtest_decl_presence()
784103
{
784103
	# init
784103
	cfgtest_prolog 'decl' "${1}"
784103
784103
	cfgtest_code_snippet=$(printf 'void * any = (void *)(%s);' "${1}")
784103
784103
	cfgtest_common_init
784103
784103
	# execute
784103
	printf '%s' "$cfgtest_src"                  \
784103
		| eval $(printf '%s' "$cfgtest_cmd") \
784103
		> /dev/null 2>&3                      \
784103
	|| cfgtest_epilog 'decl' '(error)' "${1}"      \
784103
	|| return
784103
784103
	# does the argument solely consist of the macro or enum member name?
784103
	mb_internal_str=$(printf '%s' "$@" | tr -d '[a-z][A-Z][0-9][_]')
784103
784103
	if [ -n "$mb_internal_str" ]; then
784103
		cfgtest_epilog 'decl' '(defined)'
784103
		return 0
784103
	fi
784103
784103
	# result
784103
	mb_internal_str=$(printf '%s%s' '-DHAVE_DECL_' "$@"  \
784103
			| sed -e 's/\./_/g'                  \
784103
			| tr "[:lower:]" "[:upper:]")
784103
784103
	if [ -z ${cfgtest_internal_unit_test:-} ]; then
784103
		cfgtest_cflags_append "$mb_internal_str"
784103
	else
784103
		cfgtest_makevar_append "$mb_internal_str"
784103
	fi
784103
784103
	printf 'cfgtest: `%s'"'"' is defined for the %s system.\n' \
784103
		"${1}" "$mb_cfgtest_cfgtype" >&3
784103
	printf '%s\n' '------------------------' >&3
784103
784103
	cfgtest_epilog 'decl' '(defined)'
784103
784103
	return 0
784103
}
784103
784103
784103
cfgtest_type_size()
784103
{
784103
	cfgtest_entity_size_prolog "$@"
784103
784103
	mb_internal_size=''
784103
	mb_internal_test='char x[(sizeof(%s) == %s) ? 1 : -1];'
784103
784103
	for mb_internal_guess in 8 4 2 1 16 32 64 128; do
784103
		if [ -z $mb_internal_size ]; then
784103
			printf '# guess %s ===>\n' "$mb_internal_guess" >&3
784103
784103
			mb_internal_type="$@"
784103
784103
			cfgtest_code_snippet=$(printf "$mb_internal_test" \
784103
				"$mb_internal_type" "$mb_internal_guess")
784103
784103
			cfgtest_common_init
784103
784103
			printf '%s' "$cfgtest_src"                  \
784103
				| eval $(printf '%s' "$cfgtest_cmd") \
784103
				> /dev/null 2>&3                      \
784103
			&& mb_internal_size=$mb_internal_guess
784103
784103
			printf '\n' >&3
784103
		fi
784103
	done
784103
784103
	# unrecognized type, or type size not within range
784103
	if [ -z $mb_internal_size ]; then
784103
		cfgtest_epilog 'size-of-type' '(error)' "@"
784103
		return 1
784103
	fi
784103
784103
	# -DSIZEOF_TYPE=SIZE
784103
	mb_internal_str=$(printf '%s%s=%s' '-DSIZEOF_'        \
784103
				"$mb_internal_type"           \
784103
				"$mb_internal_size"           \
784103
			| sed -e 's/\ /_/g' -e 's/*/P/g'      \
784103
			| tr "[:lower:]" "[:upper:]")
784103
784103
	if [ -z ${cfgtest_internal_unit_test:-} ]; then
784103
		cfgtest_cflags_append "$mb_internal_str"
784103
	else
784103
		cfgtest_makevar_append "$mb_internal_str"
784103
	fi
784103
784103
	printf 'cfgtest: size of type `%s'"'"' determined to be %s\n' \
784103
		"${@}" "$mb_internal_size" >&3
784103
	printf '%s\n' '------------------------' >&3
784103
784103
	cfgtest_epilog 'size-of-type' "$mb_internal_size"
784103
784103
	return 0
784103
}
784103
784103
784103
cfgtest_code_snippet_asm()
784103
{
784103
	# init
784103
	cfgtest_prolog 'support of code snippet' '<...>'
784103
784103
	cfgtest_code_snippet="$@"
784103
784103
	cfgtest_common_init 'asm'
784103
784103
	# execute
784103
	cfgtest_ret=1
784103
784103
	printf '%s' "$cfgtest_src"                  \
784103
		| eval $(printf '%s' "$cfgtest_cmd") \
784103
		> /dev/null 2>&3                      \
784103
	|| cfgtest_epilog 'snippet' '(error)'          \
784103
	|| return
784103
784103
	# result
784103
	cfgtest_ret=0
784103
784103
	printf 'cfgtest: %s compiler: above code snippet compiled successfully.\n\n' \
784103
		"$mb_cfgtest_cfgtype" >&3
784103
784103
	cfgtest_epilog 'snippet' '(ok)'
784103
784103
	return 0
784103
}
784103
784103
784103
cfgtest_library_presence()
784103
{
784103
	# init
784103
	cfgtest_libs=
784103
	cfgtest_spc=
784103
784103
	for cfgtest_lib in ${@}; do
784103
		cfgtest_libs="$cfgtest_libs$cfgtest_spc$cfgtest_lib"
784103
		cfgtest_spc=' '
784103
	done
784103
784103
	if [ "${1}" = "$cfgtest_libs" ]; then
784103
		cfgtest_prolog 'library' "${1#*-l}"
784103
	else
784103
		cfgtest_prolog 'lib module' '(see config.log)'
784103
	fi
784103
784103
	cfgtest_code_snippet='int main(void){return 0;}'
784103
784103
	cfgtest_common_init 'lib'
784103
784103
	# execute
784103
	printf '%s' "$cfgtest_src"                  \
784103
		| eval $(printf '%s' "$cfgtest_cmd") \
784103
		> /dev/null 2>&3                      \
784103
	|| cfgtest_epilog 'library' '-----' "$@"       \
784103
	|| return 1
784103
784103
	# result
784103
	printf 'cfgtest: `%s'"'"' was accepted by the linker driver.\n' \
784103
		"$cfgtest_libs" >&3
784103
	printf '%s\n' '------------------------' >&3
784103
784103
	cfgtest_epilog 'library' '(present)'
784103
784103
	return 0
784103
}
784103
784103
784103
cfgtest_unit_header_presence()
784103
{
784103
	cfgtest_internal_unit_test='unit_test'
784103
	cfgtest_header_presence "$@" || return 1
784103
	return 0
784103
}
784103
784103
784103
cfgtest_unit_header_absence()
784103
{
784103
	cfgtest_internal_unit_test='unit_test'
784103
	cfgtest_header_absence "$@" || return 1
784103
	return 0
784103
}
784103
784103
784103
cfgtest_unit_interface_presence()
784103
{
784103
	cfgtest_internal_unit_test='unit_test'
784103
	cfgtest_interface_presence "$@" || return 1
784103
	return 0
784103
}
784103
784103
784103
cfgtest_unit_decl_presence()
784103
{
784103
	cfgtest_internal_unit_test='unit_test'
784103
	cfgtest_decl_presence "$@" || return 1
784103
	return 0
784103
}
784103
784103
784103
cfgtest_unit_type_size()
784103
{
784103
	cfgtest_internal_unit_test='unit_test'
784103
	cfgtest_type_size "$@" || return 1
784103
	return 0
784103
}