firasuke / cross / slibtool

Forked from cross/slibtool 3 months ago
Clone

Blame sofort/cfgtest/cfgtest.sh

2352e9
# cfgtest.sh: sofort's config test framework,
2352e9
# for use from within a project's custom cfgdefs.sh.
2352e9
2352e9
# this file is covered by COPYING.SOFORT.
2352e9
2352e9
# in the common scenario, host-specific tests are preceded
2352e9
# by a single invocation of cfgtest_host_section, whereas
2352e9
# native (build) system tests are preceded by the invocation
2352e9
# of cfgtest_native_section.
2352e9
2352e9
# cfgdefs fraework variables:
2352e9
# mb_cfgtest_cc:      the compiler used for the current test
5195d6
# mb_cfgtest_pkgconf: the pkgconf utility used for the current test
2352e9
# mb_cfgtest_cflags:  the compiler flags used for the current test
2352e9
# mb_cfgtest_cfgtype: the type of the current test (host/native)
2352e9
# mb_cfgtest_makevar: the make variable affected by the current test
2352e9
# mb_cfgtest_headers: headers for ad-hoc inclusion with the current test
2352e9
2352e9
2352e9
cfgtest_newline()
2352e9
{
2352e9
	printf '\n' >> $mb_pwd/cfgdefs.mk
2352e9
}
2352e9
2352e9
2352e9
cfgtest_comment()
2352e9
{
2352e9
	mb_internal_str='#'
2352e9
2352e9
	for mb_internal_arg ; do
2352e9
		mb_internal_str="$mb_internal_str $mb_internal_arg"
2352e9
	done
2352e9
2352e9
	printf '%s\n' "$mb_internal_str" >> $mb_pwd/cfgdefs.mk
2352e9
}
2352e9
2352e9
2352e9
cfgtest_host_section()
2352e9
{
2352e9
	mb_cfgtest_cc="$ccenv_host_cc"
5195d6
	mb_cfgtest_pkgconf="${ccenv_host_pkgconf:-false}"
2352e9
	mb_cfgtest_cfgtype='host'
a8e128
	mb_cfgtest_stdin_input=${ccenv_host_stdin_input:-}
046953
	mb_cfgtest_environment=${ccenv_host_cc_environment:-}
38c831
38c831
	mb_cfgtest_cflags=$(${mb_make} -n -f "$mb_pwd/Makefile.tmp" \
38c831
		OS_DSO_EXRULES=default                              \
38c831
		OS_SONAME=symlink                                   \
38c831
		OS_ARCHIVE_EXT='.a'                                 \
38c831
		.cflags-host)
38c831
2352e9
	mb_cfgtest_cflags="${mb_cfgtest_cflags#*: }"
2352e9
2bacaa
	mb_cfgtest_ldflags=$(${mb_make} -n -f "$mb_pwd/Makefile.tmp" \
2bacaa
		OS_DSO_EXRULES=default                               \
2bacaa
		OS_SONAME=symlink                                    \
2bacaa
		OS_ARCHIVE_EXT='.a'                                  \
2bacaa
		.ldflags-host)
2bacaa
2bacaa
	mb_cfgtest_ldflags="${mb_cfgtest_ldflags#*: }"
2352e9
}
2352e9
2352e9
2352e9
cfgtest_native_section()
2352e9
{
38c831
	mb_cfgtest_cc="$ccenv_native_cc"
5195d6
	mb_cfgtest_pkgconf="${ccenv_native_pkgconf:-false}"
2352e9
	mb_cfgtest_cfgtype='native'
a8e128
	mb_cfgtest_stdin_input=${ccenv_native_stdin_input:-}
046953
	mb_cfgtest_environment=${ccenv_native_cc_environment:-}
38c831
38c831
	mb_cfgtest_cflags=$(${mb_make} -n -f "$mb_pwd/Makefile.tmp" \
38c831
		OS_DSO_EXRULES=default                              \
38c831
		OS_SONAME=symlink                                   \
38c831
		OS_ARCHIVE_EXT='.a'                                 \
38c831
		.cflags-native)
38c831
2352e9
	mb_cfgtest_cflags="${mb_cfgtest_cflags#*: }"
2bacaa
2bacaa
	mb_cfgtest_ldflags=$(${mb_make} -n -f "$mb_pwd/Makefile.tmp" \
2bacaa
		OS_DSO_EXRULES=default                               \
2bacaa
		OS_SONAME=symlink                                    \
2bacaa
		OS_ARCHIVE_EXT='.a'                                  \
2bacaa
		.ldflags-native)
2bacaa
2bacaa
	mb_cfgtest_ldflags="${mb_cfgtest_ldflags#*: }"
2352e9
}
2352e9
2352e9
2352e9
cfgtest_prolog()
2352e9
{
d44b0b
	cfgtest_line_dots='...........................'
2352e9
	cfgtest_line_dots="${cfgtest_line_dots}${cfgtest_line_dots}"
2352e9
	cfgtest_tool_desc=" == trying ${mb_cfgtest_cfgtype} ${1}: ${2}"
2352e9
	cfgtest_tool_dlen="${#cfgtest_line_dots}"
2352e9
2352e9
	printf '\n%s\n' '________________________' >&3
2352e9
	printf "cfgtest: probing for ${mb_cfgtest_cfgtype} ${1}: ${2}\n\n" >&3
23e269
23e269
	if [ "${cfgtest_silent:-}" != 'yes' ]; then
23e269
		printf "%${cfgtest_tool_dlen}.${cfgtest_tool_dlen}s" \
23e269
			"${cfgtest_tool_desc}  ${mb_line_dots}"
23e269
	fi
2352e9
}
2352e9
2352e9
2352e9
cfgtest_epilog()
2352e9
{
d44b0b
	cfgtest_line_dots='...............'
2352e9
	cfgtest_tool_dlen="$((${#cfgtest_line_dots} - ${#2}))"
2352e9
23e269
	if [ "${cfgtest_silent:-}" != 'yes' ]; then
23e269
		printf "%${cfgtest_tool_dlen}.${cfgtest_tool_dlen}s  %s.\n" \
23e269
			"${cfgtest_line_dots}" "${2}"
23e269
	fi
2352e9
e56ec0
	if [ "${1}" = 'snippet' ] && [ -f 'a.out' ]; then
2352e9
		rm -f 'a.out'
2352e9
	fi
2352e9
a8e128
	if [ "$mb_cfgtest_stdin_input" = 'no' ]; then
a8e128
		rm 'cfgtest_c3RyaWN0X21vZGUK.c'
a8e128
	fi
a8e128
2352e9
	if [ "${1}" = 'snippet' ] && [ "${2}" = '(error)' ]; then
2352e9
		printf '\n\ncfgtest: the %s compiler %s %s.\n' \
2352e9
			"$mb_cfgtest_cfgtype"                 \
2352e9
			'failed to compile the above code'   \
2352e9
			"${1}" >&3
2352e9
		printf '%s\n' '------------------------' >&3
2352e9
		return 1
2352e9
	fi
2352e9
5195d6
	if [ "${2}" = '-----' ] || [ "${2}" = '(missing)' ]; then
2352e9
		printf '\n\ncfgtest: %s %s is missing or cannot be found.\n' "${1}" "${3}" >&3
2352e9
		printf '%s\n' '------------------------' >&3
2352e9
		return 1
2352e9
	elif [ "${1}" = 'size-of-type' ] && [ "${2}" = '(error)' ]; then
2352e9
		printf '\n\ncfgtest: could not determine size of type `%s.\n' "${3}'" >&3
2352e9
		printf '%s\n' '------------------------' >&3
2352e9
		return 1
7cb7d7
	elif [ "${1}" = 'switch' ] && [ "${2}" = '(error)' ]; then
7cb7d7
		printf '\n\ncfgtest: the switch `%s is not supported by the %s compiler.\n' \
7cb7d7
			"${3}'" "$mb_cfgtest_cfgtype" >&3
7cb7d7
		printf '%s\n' '------------------------' >&3
7cb7d7
		return 1
2352e9
	elif [ "${2}" = '(error)' ]; then
2352e9
		printf '\n\ncfgtest: %s `%s is not defined or cannot be used.\n' "${1}" "${3}'" >&3
2352e9
		printf '%s\n' '------------------------' >&3
2352e9
		return 1
2352e9
	fi
2352e9
}
2352e9
2352e9
2352e9
cfgtest_entity_size_prolog()
2352e9
{
2352e9
	cfgtest_line_dots='.......................'
2352e9
	cfgtest_line_dots="${cfgtest_line_dots}${cfgtest_line_dots}"
2352e9
	cfgtest_tool_desc=" == checking size of ${mb_cfgtest_cfgtype} type: ${@}"
2352e9
	cfgtest_tool_dlen="${#cfgtest_line_dots}"
2352e9
2352e9
	printf '\n%s\n' '________________________' >&3
2352e9
	printf "cfgtest: checking size of ${mb_cfgtest_cfgtype} type: ${@}\n\n" >&3
2352e9
2352e9
	printf "%${cfgtest_tool_dlen}.${cfgtest_tool_dlen}s" \
2352e9
		"${cfgtest_tool_desc}  ${mb_line_dots}"
2352e9
}
2352e9
2352e9
350ce2
cfgtest_makevar_set()
350ce2
{
350ce2
	if [ -n "${@}" ]; then
350ce2
		mb_internal_str='= '
350ce2
	else
350ce2
		mb_internal_str='='
350ce2
	fi
350ce2
350ce2
	printf '%-25s%s%s\n' "$mb_cfgtest_makevar" "$mb_internal_str" "${@}" \
350ce2
		>> $mb_pwd/cfgdefs.mk
350ce2
350ce2
	unset cfgtest_internal_unit_test
350ce2
}
350ce2
350ce2
2352e9
cfgtest_makevar_append()
2352e9
{
2352e9
	mb_internal_str='+='
2352e9
2352e9
	for mb_internal_arg ; do
2352e9
		if ! [ -z "$mb_internal_arg" ]; then
2352e9
			mb_internal_str="$mb_internal_str $mb_internal_arg"
2352e9
		fi
2352e9
	done
2352e9
2352e9
	printf '%-24s%s\n' "$mb_cfgtest_makevar" "$mb_internal_str" \
2352e9
		>> $mb_pwd/cfgdefs.mk
2352e9
2352e9
	unset cfgtest_internal_unit_test
2352e9
}
2352e9
2352e9
2352e9
cfgtest_cflags_append()
2352e9
{
2352e9
	if [ $mb_cfgtest_cfgtype = 'host' ]; then
2352e9
		mb_internal_makevar='CFLAGS_CONFIG'
2352e9
	else
2352e9
		mb_internal_makevar='NATIVE_CFLAGS'
2352e9
	fi
2352e9
2352e9
	mb_cfgtest_makevar_saved=$mb_cfgtest_makevar
2352e9
	mb_cfgtest_makevar=$mb_internal_makevar
2352e9
2352e9
	cfgtest_makevar_append "$@"
2352e9
	mb_cfgtest_makevar=$mb_cfgtest_makevar_saved
2352e9
}
2352e9
2352e9
2352e9
cfgtest_ldflags_append()
2352e9
{
2352e9
	if [ $mb_cfgtest_cfgtype = 'host' ]; then
2352e9
		mb_internal_makevar='LDFLAGS_CONFIG'
2352e9
	else
2352e9
		mb_internal_makevar='NATIVE_LDFLAGS'
2352e9
	fi
2352e9
2352e9
	mb_cfgtest_makevar_saved=$mb_cfgtest_makevar
2352e9
	mb_cfgtest_makevar=$mb_internal_makevar
2352e9
2352e9
	cfgtest_makevar_append "$@"
2352e9
	mb_cfgtest_makevar=$mb_cfgtest_makevar_saved
2352e9
}
2352e9
2352e9
2352e9
cfgtest_common_init()
2352e9
{
2352e9
	# cfgtest variables
b9c199
	cfgtest_type="${1:-}"
b9c199
a8e128
	if [ "$mb_cfgtest_stdin_input" = 'no' ]; then
a8e128
		if [ "$cfgtest_type" = 'lib' ]; then
a8e128
			cfgtest_fmt='%s cfgtest_c3RyaWN0X21vZGUK.c -o a.out'
1be777
		elif [ "$cfgtest_type" = 'ldflag' ]; then
1be777
			cfgtest_fmt='%s cfgtest_c3RyaWN0X21vZGUK.c -o a.out'
a8e128
		else
a8e128
			cfgtest_fmt='%s -c cfgtest_c3RyaWN0X21vZGUK.c -o a.out'
a8e128
		fi
a8e128
	elif [ "$cfgtest_type" = 'asm' ]; then
2352e9
		cfgtest_fmt='%s -c -xc - -o a.out'
b9c199
	elif [ "$cfgtest_type" = 'lib' ]; then
2352e9
		cfgtest_fmt='%s -xc - -o a.out'
1be777
	elif [ "$cfgtest_type" = 'ldflag' ]; then
1be777
		cfgtest_fmt='%s -xc - -o a.out'
2352e9
	else
2352e9
		cfgtest_fmt='%s -S -xc - -o -'
2352e9
	fi
2352e9
7cb7d7
b9c199
	if [ "$cfgtest_type" = 'lib' ]; then
2352e9
		cfgtest_cmd=$(printf "$cfgtest_fmt %s %s %s" \
2352e9
			"$mb_cfgtest_cc"                     \
2352e9
			"$mb_cfgtest_cflags"                 \
2352e9
			"$mb_cfgtest_ldflags"                \
2352e9
			"$cfgtest_libs")
7cb7d7
1be777
	elif [ "$cfgtest_type" = 'ldflag' ]; then
1be777
		cfgtest_cmd=$(printf "$cfgtest_fmt %s %s %s" \
1be777
			"$mb_cfgtest_cc"                     \
1be777
			"$mb_cfgtest_cflags"                 \
1be777
			"$mb_cfgtest_ldflags"                \
1be777
			"$cfgtest_switches")
1be777
7cb7d7
	elif [ "$cfgtest_type" = 'switch' ]; then
7cb7d7
		cfgtest_cmd=$(printf "$cfgtest_fmt %s %s" \
7cb7d7
			"$mb_cfgtest_cc"                  \
7cb7d7
			"$mb_cfgtest_cflags"              \
7cb7d7
			"$cfgtest_switches")
2352e9
	else
2352e9
		cfgtest_cmd=$(printf "$cfgtest_fmt %s" \
2352e9
			"$mb_cfgtest_cc"               \
2352e9
			"$mb_cfgtest_cflags")
2352e9
	fi
2352e9
7cb7d7
2a3cd2
	if [ -n "$mb_cfgtest_headers" ]; then
2352e9
		cfgtest_inc=$(printf '#include <%s>\n' $mb_cfgtest_headers)
2352e9
		cfgtest_src=$(printf '%s\n_\n' "$cfgtest_inc" \
2352e9
			| m4 -D_="$cfgtest_code_snippet")
2a3cd2
	else
2a3cd2
		cfgtest_inc=
2a3cd2
		cfgtest_src="$cfgtest_code_snippet"
2352e9
	fi
2352e9
7cb7d7
2352e9
	# config.log
2352e9
	printf "$cfgtest_fmt" "$mb_cfgtest_cc" >&3
2352e9
70a0d1
	for cfgtest_cflag in $(printf '%s' "$mb_cfgtest_cflags"); do
2352e9
		printf ' \\\n\t%s' "$cfgtest_cflag" >&3
2352e9
	done
2352e9
f3f1d3
	for cfgtest_ldflag in $(printf '%s' "$mb_cfgtest_ldflags"); do
f3f1d3
		printf ' \\\n\t%s' "$cfgtest_ldflag" >&3
f3f1d3
	done
f3f1d3
b9c199
	if [ "$cfgtest_type" = 'lib' ]; then
70a0d1
		for cfgtest_lib in $(printf '%s' "$cfgtest_libs"); do
2352e9
			printf ' \\\n\t%s' "$cfgtest_lib" >&3
2352e9
		done
7cb7d7
1be777
	elif [ "$cfgtest_type" = 'switch' ] || [ "$cfgtest_type" = 'ldflag' ]; then
70a0d1
		for cfgtest_switch in $(printf '%s' "$cfgtest_switches"); do
7cb7d7
			printf ' \\\n\t%s' "$cfgtest_switch" >&3
7cb7d7
		done
2352e9
	fi
2352e9
2352e9
	printf ' \\\n'                           >&3
2352e9
	printf '<< _SRCEOF\n%s\n' "$cfgtest_src" >&3
eec173
	printf '_SRCEOF\n\n\n'                   >&3
a8e128
a8e128
	if [ "$mb_cfgtest_stdin_input" = 'no' ]; then
a8e128
		printf '%s' "$cfgtest_src" > 'cfgtest_c3RyaWN0X21vZGUK.c'
a8e128
		cfgtest_src=
a8e128
	fi
2352e9
}
2352e9
2352e9
2352e9
cfgtest_header_presence()
2352e9
{
2352e9
	#init
2352e9
	cfgtest_prolog 'header' "${1}"
2352e9
2352e9
	cfgtest_code_snippet=$(printf '#include <%s>\n' "${1}")
2352e9
2352e9
	cfgtest_common_init
2352e9
2352e9
	# execute
2352e9
	printf '%s' "$cfgtest_src"                  \
2352e9
		| eval $(printf '%s' "$cfgtest_cmd") \
2352e9
		> /dev/null 2>&3                      \
2352e9
	|| cfgtest_epilog 'header' '-----' "<${1}>"    \
2352e9
	|| return
2352e9
2352e9
	# result
2352e9
	mb_internal_str=$(printf '%s%s' '-DHAVE_' "${1}"  \
2352e9
			| sed -e 's/\./_/g' -e 's@/@_@g'  \
2352e9
			| tr "[:lower:]" "[:upper:]")
2352e9
2352e9
	if [ -z ${cfgtest_internal_unit_test:-} ]; then
2352e9
		cfgtest_cflags_append "$mb_internal_str"
2352e9
	else
2352e9
		cfgtest_makevar_append "$mb_internal_str"
2352e9
	fi
2352e9
2352e9
	printf 'cfgtest: %s header <%s> was found and may be included.\n' \
2352e9
		"$mb_cfgtest_cfgtype" "${1}" >&3
2352e9
	printf '%s\n' '------------------------' >&3
2352e9
2352e9
	cfgtest_epilog 'header' "${1}"
2352e9
}
2352e9
2352e9
2352e9
cfgtest_header_absence()
2352e9
{
2352e9
	#init
2352e9
	cfgtest_prolog 'header absence' "${1}"
2352e9
2352e9
	cfgtest_code_snippet=$(printf '#include <%s>\n' "${1}")
2352e9
2352e9
	cfgtest_common_init
2352e9
2352e9
	# execute
2352e9
	printf '%s' "$cfgtest_src"                  \
2352e9
		| eval $(printf '%s' "$cfgtest_cmd") \
2352e9
		> /dev/null 2>&3                      \
2352e9
	&& printf 'cfgtest: %s header <%s>: no error.' \
2352e9
		"$mb_cfgtest_cfgtype" "${1}" >&3        \
2352e9
	&& cfgtest_epilog 'header' "${1}"                \
2352e9
	&& return
2352e9
2352e9
	# result
2352e9
	mb_internal_str=$(printf '%s%s' '-DHAVE_NO_' "$@" \
2352e9
			| sed -e 's/\./_/g' -e 's@/@_@g'  \
2352e9
			| tr "[:lower:]" "[:upper:]")
2352e9
2352e9
	if [ -z ${cfgtest_internal_unit_test:-} ]; then
2352e9
		cfgtest_cflags_append "$mb_internal_str"
2352e9
	else
2352e9
		cfgtest_makevar_append "$mb_internal_str"
2352e9
	fi
2352e9
2352e9
	printf 'cfgtest: %s header <%s> may not be included.\n' \
2352e9
		"$mb_cfgtest_cfgtype" "${1}" >&3
2352e9
	printf '%s\n' '------------------------' >&3
2352e9
57357f
	cfgtest_epilog 'header' '-----' "${1}"
2352e9
}
2352e9
2352e9
2352e9
cfgtest_interface_presence()
2352e9
{
2352e9
	# init
2352e9
	cfgtest_prolog 'interface' "${1}"
2352e9
2352e9
	cfgtest_code_snippet=$(printf 'void * addr = &%;;\n' "${1}")
2352e9
2352e9
	cfgtest_common_init
2352e9
2352e9
	# execute
2352e9
	printf '%s' "$cfgtest_src"                    \
2352e9
		| eval $(printf '%s' "$cfgtest_cmd")   \
2352e9
		> /dev/null 2>&3                        \
2352e9
	|| cfgtest_epilog 'interface' '(error)' "${1}"   \
2352e9
	|| return
2352e9
2352e9
	# result
2352e9
	mb_internal_str=$(printf '%s%s' '-DHAVE_' "$@"  \
2352e9
			| sed -e 's/\./_/g'             \
2352e9
			| tr "[:lower:]" "[:upper:]")
2352e9
2352e9
	if [ -z ${cfgtest_internal_unit_test:-} ]; then
2352e9
		cfgtest_cflags_append "$mb_internal_str"
2352e9
	else
2352e9
		cfgtest_makevar_append "$mb_internal_str"
2352e9
	fi
2352e9
2352e9
	printf 'cfgtest: %s interface `%s'"'"' is available.\n' \
2352e9
		"$mb_cfgtest_cfgtype" "${1}" >&3
2352e9
	printf '%s\n' '------------------------' >&3
2352e9
2352e9
	cfgtest_epilog 'interface' "${1}"
2352e9
2352e9
	return 0
2352e9
}
2352e9
2352e9
2352e9
cfgtest_decl_presence()
2352e9
{
2352e9
	# init
2352e9
	cfgtest_prolog 'decl' "${1}"
2352e9
2352e9
	cfgtest_code_snippet=$(printf 'void * any = (void *)(%s);' "${1}")
2352e9
2352e9
	cfgtest_common_init
2352e9
2352e9
	# execute
2352e9
	printf '%s' "$cfgtest_src"                  \
2352e9
		| eval $(printf '%s' "$cfgtest_cmd") \
2352e9
		> /dev/null 2>&3                      \
2352e9
	|| cfgtest_epilog 'decl' '(error)' "${1}"      \
2352e9
	|| return
2352e9
2352e9
	# does the argument solely consist of the macro or enum member name?
2352e9
	mb_internal_str=$(printf '%s' "$@" | tr -d '[a-z][A-Z][0-9][_]')
2352e9
2352e9
	if [ -n "$mb_internal_str" ]; then
2352e9
		cfgtest_epilog 'decl' '(defined)'
2352e9
		return 0
2352e9
	fi
2352e9
2352e9
	# result
2352e9
	mb_internal_str=$(printf '%s%s' '-DHAVE_DECL_' "$@"  \
2352e9
			| sed -e 's/\./_/g'                  \
2352e9
			| tr "[:lower:]" "[:upper:]")
2352e9
2352e9
	if [ -z ${cfgtest_internal_unit_test:-} ]; then
2352e9
		cfgtest_cflags_append "$mb_internal_str"
2352e9
	else
2352e9
		cfgtest_makevar_append "$mb_internal_str"
2352e9
	fi
2352e9
2352e9
	printf 'cfgtest: `%s'"'"' is defined for the %s system.\n' \
2352e9
		"${1}" "$mb_cfgtest_cfgtype" >&3
2352e9
	printf '%s\n' '------------------------' >&3
2352e9
2352e9
	cfgtest_epilog 'decl' '(defined)'
2352e9
2352e9
	return 0
2352e9
}
2352e9
2352e9
2352e9
cfgtest_type_size()
2352e9
{
2352e9
	cfgtest_entity_size_prolog "$@"
2352e9
2352e9
	mb_internal_size=''
2352e9
	mb_internal_test='char x[(sizeof(%s) == %s) ? 1 : -1];'
2352e9
2352e9
	for mb_internal_guess in 8 4 2 1 16 32 64 128; do
2352e9
		if [ -z $mb_internal_size ]; then
2352e9
			printf '# guess %s ===>\n' "$mb_internal_guess" >&3
2352e9
2352e9
			mb_internal_type="$@"
2352e9
2352e9
			cfgtest_code_snippet=$(printf "$mb_internal_test" \
2352e9
				"$mb_internal_type" "$mb_internal_guess")
2352e9
2352e9
			cfgtest_common_init
2352e9
2352e9
			printf '%s' "$cfgtest_src"                  \
2352e9
				| eval $(printf '%s' "$cfgtest_cmd") \
2352e9
				> /dev/null 2>&3                      \
2352e9
			&& mb_internal_size=$mb_internal_guess
2352e9
2352e9
			printf '\n' >&3
2352e9
		fi
2352e9
	done
2352e9
2352e9
	# unrecognized type, or type size not within range
2352e9
	if [ -z $mb_internal_size ]; then
2352e9
		cfgtest_epilog 'size-of-type' '(error)' "@"
2352e9
		return 1
2352e9
	fi
2352e9
2352e9
	# -DSIZEOF_TYPE=SIZE
2352e9
	mb_internal_str=$(printf '%s%s=%s' '-DSIZEOF_'        \
2352e9
				"$mb_internal_type"           \
2352e9
				"$mb_internal_size"           \
2352e9
			| sed -e 's/\ /_/g' -e 's/*/P/g'      \
2352e9
			| tr "[:lower:]" "[:upper:]")
2352e9
2352e9
	if [ -z ${cfgtest_internal_unit_test:-} ]; then
2352e9
		cfgtest_cflags_append "$mb_internal_str"
2352e9
	else
2352e9
		cfgtest_makevar_append "$mb_internal_str"
2352e9
	fi
2352e9
2352e9
	printf 'cfgtest: size of type `%s'"'"' determined to be %s\n' \
2352e9
		"${@}" "$mb_internal_size" >&3
2352e9
	printf '%s\n' '------------------------' >&3
2352e9
2352e9
	cfgtest_epilog 'size-of-type' "$mb_internal_size"
2352e9
2352e9
	return 0
2352e9
}
2352e9
2352e9
2352e9
cfgtest_code_snippet_asm()
2352e9
{
2352e9
	# init
2352e9
	cfgtest_prolog 'support of code snippet' '<...>'
2352e9
2352e9
	cfgtest_code_snippet="$@"
2352e9
2352e9
	cfgtest_common_init 'asm'
2352e9
2352e9
	# execute
2352e9
	cfgtest_ret=1
2352e9
2352e9
	printf '%s' "$cfgtest_src"                  \
2352e9
		| eval $(printf '%s' "$cfgtest_cmd") \
2352e9
		> /dev/null 2>&3                      \
2352e9
	|| cfgtest_epilog 'snippet' '(error)'          \
2352e9
	|| return
2352e9
2352e9
	# result
2352e9
	cfgtest_ret=0
2352e9
2352e9
	printf 'cfgtest: %s compiler: above code snippet compiled successfully.\n\n' \
2352e9
		"$mb_cfgtest_cfgtype" >&3
2352e9
2352e9
	cfgtest_epilog 'snippet' '(ok)'
2352e9
2352e9
	return 0
2352e9
}
2352e9
2352e9
399275
cfgtest_macro_definition()
399275
{
399275
	# init
399275
	cfgtest_prolog 'macro definition' "${1}"
399275
399275
	cfgtest_code_snippet=$(printf '%s\n'      \
399275
		"#ifndef ${1}"                     \
399275
		"#error macro ${1} is not defined." \
399275
		"#endif")
399275
399275
	cfgtest_common_init 'macro'
399275
399275
	# execute
399275
	cfgtest_ret=1
399275
399275
	printf '%s' "$cfgtest_src"                  \
399275
		| eval $(printf '%s' "$cfgtest_cmd") \
399275
		> /dev/null 2>&3                      \
399275
	|| cfgtest_epilog 'macro' '(error)' "${1}"     \
399275
	|| return
399275
399275
	# result
399275
	cfgtest_ret=0
399275
399275
	printf 'cfgtest: %s compiler: above macro definition test compiled successfully.\n\n' \
399275
		"$mb_cfgtest_cfgtype" >&3
399275
399275
	cfgtest_epilog 'macro' '(defined)'
399275
399275
	return 0
399275
}
399275
399275
2352e9
cfgtest_library_presence()
2352e9
{
2352e9
	# init
2352e9
	cfgtest_libs=
2352e9
	cfgtest_spc=
2352e9
2352e9
	for cfgtest_lib in ${@}; do
2352e9
		cfgtest_libs="$cfgtest_libs$cfgtest_spc$cfgtest_lib"
2352e9
		cfgtest_spc=' '
2352e9
	done
2352e9
2352e9
	if [ "${1}" = "$cfgtest_libs" ]; then
2352e9
		cfgtest_prolog 'library' "${1#*-l}"
2352e9
	else
2352e9
		cfgtest_prolog 'lib module' '(see config.log)'
2352e9
	fi
2352e9
c2598a
	if [ "$mb_cfgtest_environment" = 'freestanding' ]; then
c2598a
		if [ -z "ccenv_cc_underscore" ]; then
c2598a
			cfgtest_code_snippet='int start(void){return 0;}'
c2598a
		else
c2598a
			cfgtest_code_snippet='int _start(void){return 0;}'
c2598a
		fi
c2598a
	else
c2598a
		cfgtest_code_snippet='int main(void){return 0;}'
c2598a
	fi
2352e9
2352e9
	cfgtest_common_init 'lib'
2352e9
2352e9
	# execute
2352e9
	printf '%s' "$cfgtest_src"                  \
2352e9
		| eval $(printf '%s' "$cfgtest_cmd") \
2352e9
		> /dev/null 2>&3                      \
2352e9
	|| cfgtest_epilog 'library' '-----' "$@"       \
2352e9
	|| return 1
2352e9
2352e9
	# result
2352e9
	printf 'cfgtest: `%s'"'"' was accepted by the linker driver.\n' \
2352e9
		"$cfgtest_libs" >&3
2352e9
	printf '%s\n' '------------------------' >&3
2352e9
2352e9
	cfgtest_epilog 'library' '(present)'
2352e9
2352e9
	return 0
2352e9
}
2352e9
2352e9
5195d6
cfgtest_package_exists()
5195d6
{
5195d6
	# init
5195d6
	cfgtest_pkg=
5195d6
5195d6
	for cfgtest_arg in ${@}; do
5195d6
		case "$cfgtest_arg" in
5195d6
			-*)
5195d6
				;;
5195d6
5195d6
			*)
5195d6
				cfgtest_pkg="$cfgtest_arg"
5195d6
				;;
5195d6
		esac
5195d6
	done
5195d6
5195d6
	cfgtest_prolog 'package' "$cfgtest_pkg"
5195d6
5195d6
	# execute
5195d6
	"$mb_cfgtest_pkgconf" "${@}"                         \
5195d6
		> /dev/null 2>&3                              \
5195d6
	|| cfgtest_epilog 'package' '(missing)' "$cfgtest_pkg" \
5195d6
	|| return 1
5195d6
5195d6
	# result
5195d6
	printf 'cfgtest: package `%s'"'"' was found.\n' \
5195d6
		"$cfgtest_pkg" >&3
5195d6
	printf '%s\n' '------------------------' >&3
5195d6
5195d6
	cfgtest_epilog 'package' '(exists)' "$cfgtest_pkg"
5195d6
5195d6
	return 0
5195d6
}
5195d6
5195d6
5195d6
cfgtest_package_config()
5195d6
{
5195d6
	# init
5195d6
	if ! cfgtest_package_exists "${@}"; then
5195d6
		return 0
5195d6
	fi
5195d6
5195d6
	cfgtest_pkg=
5195d6
5195d6
	for cfgtest_arg in ${@}; do
5195d6
		case "$cfgtest_arg" in
5195d6
			-*)
5195d6
				;;
5195d6
5195d6
			*)
5195d6
				cfgtest_pkg="$cfgtest_arg"
5195d6
				;;
5195d6
		esac
5195d6
	done
5195d6
5195d6
	cfgtest_makevar_prefix=
5195d6
	cfgtest_pkgconf_prefix=
5195d6
5195d6
	if [ "$mb_cfgtest_cfgtype" = 'native' ]; then
5195d6
		cfgtest_makevar_prefix='_NATIVE'
5195d6
	else
5195d6
		if [ -n "${mb_sysroot}" ]; then
5195d6
			cfgtest_pkgconf_prefix="--define-variable=prefix=${mb_sysroot}"
5195d6
		fi
5195d6
	fi
5195d6
5195d6
	cfgtest_newline
5195d6
	cfgtest_comment 'package config:' "$cfgtest_pkg"
5195d6
5195d6
	# foo.pc
5195d6
	cfgtest_pkgconf_path=$("$mb_cfgtest_pkgconf" \
5195d6
			--path "${@}"                 \
5195d6
		2>/dev/null || true)
5195d6
5195d6
	if [ -z "$cfgtest_pkgconf_path" ]; then
5195d6
		cfgtest_pkgconf_path=$("$mb_cfgtest_pkgconf" \
5195d6
				--debug "${@}" 2>&1           \
5195d6
				| grep ".pc'$"                 \
5195d6
				| head -n1                      \
5195d6
			|| true)
5195d6
5195d6
		cfgtest_pkgconf_path="${cfgtest_pkgconf_path##* \'}"
5195d6
		cfgtest_pkgconf_path="${cfgtest_pkgconf_path%%\'}"
5195d6
	fi
5195d6
5195d6
	mb_cfgtest_makevar=$(printf '%s_PKGCONF_%s'             \
5195d6
			"$cfgtest_makevar_prefix" "$cfgtest_pkg" \
5195d6
		| tr '[[:lower:]]' '[[:upper:]]'                  \
5195d6
		| sed -e 's/-/_/g')
5195d6
5195d6
	cfgtest_makevar_set "$cfgtest_pkgconf_path"
5195d6
5195d6
	# --cflags
5195d6
	cfgtest_pkgconf_path=$("$mb_cfgtest_pkgconf" \
5195d6
			"$cfgtest_pkgconf_prefix"     \
5195d6
			--cflags "${@}"                \
5195d6
		| sed 's/[ \t]*$//')
5195d6
5195d6
	mb_cfgtest_makevar=$(printf '%s_CFLAGS_%s'              \
5195d6
			"$cfgtest_makevar_prefix" "$cfgtest_pkg" \
5195d6
		| tr '[[:lower:]]' '[[:upper:]]'                  \
5195d6
		| sed -e 's/-/_/g')
5195d6
5195d6
	cfgtest_makevar_set "$cfgtest_pkgconf_path"
5195d6
5195d6
	# --ldflags
5195d6
	cfgtest_pkgconf_path=$("$mb_cfgtest_pkgconf" \
5195d6
			"$cfgtest_pkgconf_prefix"     \
5195d6
			--libs "${@}"                  \
5195d6
		| sed 's/[ \t]*$//')
5195d6
5195d6
	mb_cfgtest_makevar=$(printf '%s_LDFLAGS_%s'             \
5195d6
			"$cfgtest_makevar_prefix" "$cfgtest_pkg" \
5195d6
		| tr '[[:lower:]]' '[[:upper:]]'                  \
5195d6
		| sed -e 's/-/_/g')
5195d6
5195d6
	cfgtest_makevar_set "$cfgtest_pkgconf_path"
5195d6
5195d6
	return 0
5195d6
}
5195d6
5195d6
7cb7d7
cfgtest_compiler_switch()
7cb7d7
{
7cb7d7
	# init
7cb7d7
	cfgtest_switches=
7cb7d7
	cfgtest_spc=
7cb7d7
70a0d1
	for cfgtest_switch in $(printf '%s' "${@}"); do
7cb7d7
		cfgtest_switches="$cfgtest_switches$cfgtest_spc$cfgtest_switch"
7cb7d7
		cfgtest_spc=' '
7cb7d7
	done
7cb7d7
2a63f0
	if [ -n "${cfgtest_switch_arg:-}" ]; then
2a63f0
		cfgtest_prolog 'compiler switch' "${cfgtest_switches%=*}"
2a63f0
2a63f0
	elif [ "${1}" = "$cfgtest_switches" ]; then
7cb7d7
		cfgtest_prolog 'compiler switch' "$cfgtest_switches"
2a63f0
7cb7d7
	else
7cb7d7
		cfgtest_prolog 'compiler switch combination' "$cfgtest_switches"
7cb7d7
	fi
7cb7d7
1be777
	case "${1}" in
1be777
		-Wl,*)
046953
			if [ "$mb_cfgtest_environment" = 'freestanding' ]; then
046953
				cfgtest_switches="$cfgtest_switches -nostdlib -nostartfiles"
046953
046953
				if [ -z "ccenv_cc_underscore" ]; then
046953
					cfgtest_code_snippet='int start(void){return 0;}'
046953
				else
046953
					cfgtest_code_snippet='int _start(void){return 0;}'
046953
				fi
046953
			else
046953
				cfgtest_code_snippet='int main(void){return 0;}'
046953
			fi
046953
1be777
			cfgtest_common_init 'ldflag'
1be777
			;;
1be777
1be777
		*)
1be777
			cfgtest_code_snippet=
1be777
			cfgtest_common_init 'switch'
1be777
			;;
1be777
	esac
7cb7d7
7cb7d7
	# execute
7cb7d7
	printf '%s' "$cfgtest_src"                  \
7cb7d7
		| eval $(printf '%s' "$cfgtest_cmd") \
7cb7d7
		> /dev/null 2>&3                      \
7cb7d7
	|| cfgtest_epilog 'switch' '(error)' "$@"      \
7cb7d7
	|| return 1
7cb7d7
7cb7d7
	# result
7cb7d7
	printf 'cfgtest: the switch `%s was accepted by the compier.\n' \
7cb7d7
		"$cfgtest_switches'" >&3
7cb7d7
	printf '%s\n' '------------------------' >&3
7cb7d7
7cb7d7
	cfgtest_epilog 'switch' '(accepted)'
7cb7d7
7cb7d7
	return 0
7cb7d7
}
7cb7d7
7cb7d7
2a63f0
cfgtest_compiler_switch_arg()
2a63f0
{
2a63f0
	cfgtest_switch_arg='yes'
2a63f0
2a63f0
	if cfgtest_compiler_switch "${@}"; then
2a63f0
		cfgtest_ret=0
2a63f0
	else
2a63f0
		cfgtest_ret=1
2a63f0
	fi
2a63f0
2a63f0
	unset cfgtest_switch_arg
2a63f0
2a63f0
	return $cfgtest_ret
2a63f0
}
2a63f0
2a63f0
2352e9
cfgtest_unit_header_presence()
2352e9
{
2352e9
	cfgtest_internal_unit_test='unit_test'
2352e9
	cfgtest_header_presence "$@" || return 1
2352e9
	return 0
2352e9
}
2352e9
2352e9
2352e9
cfgtest_unit_header_absence()
2352e9
{
2352e9
	cfgtest_internal_unit_test='unit_test'
2352e9
	cfgtest_header_absence "$@" || return 1
2352e9
	return 0
2352e9
}
2352e9
2352e9
2352e9
cfgtest_unit_interface_presence()
2352e9
{
2352e9
	cfgtest_internal_unit_test='unit_test'
2352e9
	cfgtest_interface_presence "$@" || return 1
2352e9
	return 0
2352e9
}
2352e9
2352e9
2352e9
cfgtest_unit_decl_presence()
2352e9
{
2352e9
	cfgtest_internal_unit_test='unit_test'
2352e9
	cfgtest_decl_presence "$@" || return 1
2352e9
	return 0
2352e9
}
2352e9
2352e9
2352e9
cfgtest_unit_type_size()
2352e9
{
2352e9
	cfgtest_internal_unit_test='unit_test'
2352e9
	cfgtest_type_size "$@" || return 1
2352e9
	return 0
2352e9
}