Reicher82 / toolchain / sbmake

Forked from toolchain/sbmake 3 years ago
Clone
Blob Blame History Raw
# in projects where [ $mb_use_custom_cfgdefs = yes ],
# cfgdefs.sh is invoked from within ./configure via
# . $mb_project_dir/project/cfgdefs.sh

# a successful return from cfgdefs.sh will be followed
# by a second invocation of the config_copy() function,
# reflecting any changes to common config variables
# made by cfgdefs.sh.

# finally, cfgdefs.sh may update the contents of the
# config-time generated cfgdefs.mk.


# sofort's config test framework
. "$mb_project_dir/sofort/cfgtest/cfgtest.sh"


for arg ; do
	case "$arg" in
		*)
			error_msg ${arg#}: "unsupported config argument."
			exit 2
	esac
done


cfgdefs_bootstrap()
{
	cfgdefs_bootstrap_done=${cfgdefs_bootstrap_done:-}

	if [ _$cfgdefs_bootstrap_done = _yes ]; then
		return 0
	fi

	cfgdefs_objs=

	cfgdefs_utilsrc='make.c'

	cfgdefs_sources="arch.c buf.c compat.c cond.c dir.c enum.c for.c hash.c job.c"
	cfgdefs_sources="$cfgdefs_sources lst.c main.c make_malloc.c meta.c metachar.c parse.c"
	cfgdefs_sources="$cfgdefs_sources str.c suff.c targ.c trace.c util.c var.c"

	cfgdefs_cflags="-I${mb_source_dir}/src/internal"
	cfgdefs_cflags="$cfgdefs_cflags -D_PATH_DEFSYSPATH=\"${mb_source_dir}/mk\""
	cfgdefs_cflags="$cfgdefs_cflags $cfgdefs_added_defs"

	cfgdefs_make="$mb_pwd/bootstrap/make"

	mkdir -p "$mb_pwd/bootstrap"

	printf '\n\n'

	for src in $cfgdefs_utilsrc; do
		obj=${src%%.c}.o
		cfgdefs_objs="$cfgdefs_objs $mb_pwd/bootstrap/${obj}"

		set -x

		"$mb_native_cc" $mb_native_cflags $cfgdefs_cflags \
			-c "$mb_source_dir/src/${src}"             \
			-o "$mb_pwd/bootstrap/${obj}"

		set +x
	done

	for src in $cfgdefs_sources; do
		obj=${src%%.c}.o
		cfgdefs_objs="$cfgdefs_objs $mb_pwd/bootstrap/${obj}"

		set -x

		"$mb_native_cc" $mb_native_cflags $cfgdefs_cflags \
			-c "$mb_source_dir/src/logic/${src}"       \
			-o "$mb_pwd/bootstrap/${obj}"

		set +x
	done

	"$mb_native_cc" $mb_native_cflags $cfgdefs_cflags \
		$mb_native_ldflags $cfgdefs_objs           \
		-o "$cfgdefs_make"

	mb_make="$cfgdefs_make"

	cfgdefs_bootstrap_done='yes'
}


cfgdefs_output_custom_defs()
{
	cat "$mb_project_dir/project/config/cfgdefs.in" > cfgdefs.mk
}


cfgdefs_perform_common_tests()
{
	# bootstrap
	cfgdefs_added_defs=

	# interfaces
	mb_cfgtest_headers='stdio.h'
	cfgtest_interface_presence 'vsnprintf'

	if [ $? = '0' ]; then
		cfgdefs_added_defs="$cfgdefs_added_defs -DHAVE_VSNPRINTF"
	fi

	mb_cfgtest_headers='string.h'
	cfgtest_interface_presence 'strerror'

	if [ $? = '0' ]; then
		cfgdefs_added_defs="$cfgdefs_added_defs -DHAVE_STRERROR"
	fi

	mb_cfgtest_headers='sys/types.h unistd.h'
	cfgtest_interface_presence 'setpgid'

	if [ $? = '0' ]; then
		cfgdefs_added_defs="$cfgdefs_added_defs -DHAVE_SETPGID"
	fi

	mb_cfgtest_headers='sys/time.h sys/resource.h'
	cfgtest_interface_presence 'setrlimit'

	if [ $? = '0' ]; then
		cfgdefs_added_defs="$cfgdefs_added_defs -DHAVE_SETRLIMIT"
	fi

	mb_cfgtest_headers='stdlib.h'
	cfgtest_interface_presence 'setenv'

	if [ $? = '0' ]; then
		cfgdefs_added_defs="$cfgdefs_added_defs -DHAVE_SETENV"
	fi

	cfgtest_newline
}


cfgdefs_perform_native_tests()
{
	# init
	cfgtest_newline
	cfgtest_native_section

	# common tests
	cfgdefs_perform_common_tests

	# pretty cfgdefs.mk
	cfgtest_newline
}

cfgdefs_perform_target_tests()
{
	# init
	cfgtest_newline
	cfgtest_host_section

	# common tests
	cfgdefs_perform_common_tests

	# pretty cfgdefs.mk
	cfgtest_newline
}

# cfgdefs.in --> cfgdefs.mk
cfgdefs_output_custom_defs

# strict: some tests might fail
set +e

# bootstrapping cflags
cfgdefs_perform_native_tests

# strict: restore mode
set -e

# bootstrap
cfgdefs_bootstrap

# strict: some tests might fail
set +e

# target-specific tests
cfgdefs_perform_target_tests

# strict: restore mode
set -e

# all done
return 0