Blame configure

cde03b
#!/bin/sh
cde03b
# we are no longer lazy.
cde03b
cde03b
# this script respects both CFLAGS and CFLAGS_CMDLINE,
cde03b
# as well as both LDFLAGS and LDFLAGS_CMDLINE, however
cde03b
# the latter variable of each pair should be preferred.
cde03b
cde03b
usage()
cde03b
{
af3780
	cat "$mb_project_dir"/config.usage
cde03b
	exit $?
cde03b
}
cde03b
cde03b
error_msg()
cde03b
{
cde03b
	echo $@ >&2
cde03b
}
cde03b
cde03b
cde03b
init_vars()
cde03b
{
cde03b
	mb_project_dir=$(cd `dirname $0` ; pwd)
cde03b
	mb_pwd=`pwd`
cde03b
3a8a92
	if [ -z "$mb_config" ]; then
cde03b
		. $mb_project_dir/config.project || exit 2
cde03b
	else
cde03b
		. "$mb_config" || exit 2
cde03b
	fi
cde03b
cde03b
	# dirs
cde03b
	mb_prefix=$PREFIX
cde03b
	mb_bindir=$BINDIR
cde03b
	mb_libdir=$LIBDIR
cde03b
	mb_includedir=$INCLUDEDIR
cde03b
	mb_syslibdir=$LIBDIR
cde03b
	mb_mandir=$MANDIR
cde03b
	mb_docdir=$DOCDIR
cde03b
	mb_libexecdir=$LIBEXECDIR
cde03b
cde03b
cde03b
	# build
cde03b
	mb_build=$BUILD
cde03b
	mb_host=$HOST
cde03b
	mb_target=$TARGET
cde03b
	mb_arch=$ARCH
d015ab
	mb_compiler=$COMPILER
e692ec
	mb_toolchain=$TOOLCHAIN
cde03b
	mb_sysroot=$SYSROOT
cde03b
	mb_cross_compile=$CROSS_COMPILE
cde03b
	mb_shell=$SHELL
cde03b
cde03b
	# switches
cde03b
	mb_cflags=$CFLAGS
cde03b
	mb_cflags_debug=$CFLAGS_DEBUG
cde03b
	mb_cflags_common=$CFLAGS_COMMON
cde03b
	mb_cflags_cmdline=$CFLAGS_CMDLINE
cde03b
	mb_cflags_config=$CFLAGS_CONFIG
cde03b
	mb_cflags_sysroot=$CFLAGS_SYSROOT
5de097
	mb_cflags_os=$CFLAGS_OS
5de097
	mb_cflags_site=$CFLAGS_SITE
cde03b
	mb_cflags_path=$CFLAGS_PATH
cde03b
cde03b
	mb_ldflags=$LDFLAGS
cde03b
	mb_ldflags_debug=$LDFLAGS_DEBUG
cde03b
	mb_ldflags_common=$LDFLAGS_COMMON
cde03b
	mb_ldflags_cmdline=$LDFLAGS_CMDLINE
cde03b
	mb_ldflags_config=$LDFLAGS_CONFIG
cde03b
	mb_ldflags_sysroot=$LDFLAGS_SYSROOT
cde03b
	mb_ldflags_path=$LDFLAGS_PATH
cde03b
cde03b
	mb_pe_subsystem=$PE_SUBSYSTEM
cde03b
	mb_pe_image_base=$PE_IMAGE_BASE
cde03b
	mb_pe_config_defs=$PE_CONFIG_DEFS
cde03b
cde03b
	mb_elf_eh_frame=$ELF_EH_FRAME
cde03b
	mb_elf_hash_style=$ELF_HASH_STYLE
cde03b
	mb_elf_config_defs=$ELF_CONFIG_DEFS
cde03b
cde03b
	# overrides
cde03b
	mb_native_cc=$NATIVE_CC
cde03b
	mb_native_os=$NATIVE_OS
cde03b
	mb_native_os_bits=$NATIVE_OS_BITS
cde03b
	mb_native_os_underscore=$NATIVE_OS_UNDERSCORE
cde03b
cde03b
	mb_user_cc=$CC
cde03b
	mb_user_cpp=$CPP
cde03b
	mb_user_cxx=$CXX
cde03b
}
cde03b
cde03b
cde03b
verify_build_directory()
cde03b
{
3a8a92
	if [ "$mb_project_dir" = "$mb_pwd" ]; then
3a8a92
		if [ "$mb_require_out_of_tree" = yes ]; then
cde03b
			error_msg "$mb_package: out-of-tree builds are required."
cde03b
			error_msg "please invoke configure again from a clean build directory."
cde03b
			exit 2
cde03b
		else
cde03b
			mb_project_dir='.'
cde03b
		fi
cde03b
	fi
cde03b
}
cde03b
cde03b
cde03b
common_defaults()
cde03b
{
cde03b
	# dirs
cde03b
	[ -z "$mb_prefix" ] 		&& mb_prefix=$mb_default_prefix
cde03b
	[ -z "$mb_bindir" ] 		&& mb_bindir=$mb_default_bindir
cde03b
	[ -z "$mb_libdir" ] 		&& mb_libdir=$mb_default_libdir
cde03b
	[ -z "$mb_includedir" ]		&& mb_includedir=$mb_default_includedir
cde03b
	[ -z "$mb_syslibdir" ] 		&& mb_syslibdir=$mb_default_syslibdir
cde03b
	[ -z "$mb_mandir" ] 		&& mb_mandir=$mb_default_mandir
cde03b
	[ -z "$mb_docdir" ] 		&& mb_docdir=$mb_default_docdir
cde03b
	[ -z "$mb_libexecdir" ]		&& mb_libexecdir=$mb_default_libexecdir
cde03b
cde03b
	# build
cde03b
	[ -z "$mb_build" ] 		&& mb_build=$mb_default_build
cde03b
	[ -z "$mb_host" ] 		&& mb_host=$mb_default_host
cde03b
	[ -z "$mb_target" ] 		&& mb_target=$mb_default_target
cde03b
	[ -z "$mb_arch" ] 		&& mb_arch=$mb_default_arch
d015ab
	[ -z "$mb_compiler" ] 		&& mb_compiler=$mb_default_compiler
e692ec
	[ -z "$mb_toolchain" ] 		&& mb_toolchain=$mb_default_toolchain
cde03b
	[ -z "$mb_sysroot" ] 		&& mb_sysroot=$mb_default_sysroot
cde03b
	[ -z "$mb_cross_compile" ] 	&& mb_cross_compile=$mb_default_cross_compile
cde03b
	[ -z "$mb_shell" ] 		&& mb_shell=$mb_default_shell
cde03b
cde03b
	# switches
cde03b
	[ -z "$mb_cflags_debug" ]	&& mb_cflags_debug=$mb_default_cflags_debug
cde03b
	[ -z "$mb_cflags_common" ]	&& mb_cflags_common=$mb_default_cflags_common
cde03b
	[ -z "$mb_cflags_cmdline" ]	&& mb_cflags_cmdline=$mb_default_cflags_cmdline
cde03b
	[ -z "$mb_cflags_config" ]	&& mb_cflags_config=$mb_default_cflags_config
cde03b
	[ -z "$mb_cflags_sysroot" ]	&& mb_cflags_sysroot=$mb_default_cflags_sysroot
5de097
	[ -z "$mb_cflags_os" ]		&& mb_cflags_os=$mb_default_cflags_os
5de097
	[ -z "$mb_cflags_site" ]	&& mb_cflags_site=$mb_default_cflags_site
cde03b
	[ -z "$mb_cflags_path" ]	&& mb_cflags_path=$mb_default_cflags_path
cde03b
cde03b
	[ -z "$mb_ldflags_debug" ]	&& mb_ldflags_debug=$mb_default_ldflags_debug
cde03b
	[ -z "$mb_ldflags_common" ]	&& mb_ldflags_common=$mb_default_ldflags_common
cde03b
	[ -z "$mb_ldflags_cmdline" ]	&& mb_ldflags_cmdline=$mb_default_ldflags_cmdline
cde03b
	[ -z "$mb_ldflags_config" ]	&& mb_ldflags_config=$mb_default_ldflags_config
cde03b
	[ -z "$mb_ldflags_sysroot" ]	&& mb_ldflags_sysroot=$mb_default_ldflags_sysroot
cde03b
	[ -z "$mb_ldflags_path" ]	&& mb_ldflags_path=$mb_default_ldflags_path
cde03b
cde03b
	[ -z "$mb_pe_subsystem" ]	&& mb_pe_subsystem=$mb_default_pe_subsystem
cde03b
	[ -z "$mb_pe_image_base" ]	&& mb_pe_image_base=$mb_default_pe_image_base
cde03b
	[ -z "$mb_pe_config_defs" ]	&& mb_pe_config_defs=$mb_default_pe_config_defs
cde03b
cde03b
	[ -z "$mb_elf_eh_frame" ]	&& mb_elf_eh_frame=$mb_default_elf_eh_frame
cde03b
	[ -z "$mb_elf_hash_style" ]	&& mb_elf_hash_style=$mb_default_elf_hash_style
cde03b
	[ -z "$mb_elf_config_defs" ]	&& mb_elf_config_defs=$mb_default_elf_config_defs
cde03b
cde03b
	# host/target
cde03b
	[ -z "$mb_host" ] 		&& mb_host=$mb_target
cde03b
	[ -z "$mb_target" ] 		&& mb_target=$mb_host
cde03b
cde03b
	# sysroot
3a8a92
	if [ -n "$mb_sysroot" ]; then
3a8a92
		if [ -z "$mb_cflags_sysroot" ]; then
cde03b
			mb_cflags_sysroot="--sysroot=$mb_sysroot"
cde03b
		fi
cde03b
3a8a92
		if [ -z "$mb_ldflags_sysroot" ]; then
cde03b
			mb_ldflags_sysroot="-Wl,--sysroot,$mb_sysroot"
cde03b
		fi
cde03b
	fi
cde03b
cde03b
	# debug
3a8a92
	if [ "$mb_debug" = yes ]; then
3a8a92
		if [ -z "$mb_cflags_debug" ]; then
cde03b
			mb_cflags_debug='-g3 -O0'
cde03b
		fi
cde03b
	fi
cde03b
d015ab
	# compiler
3a8a92
	if [ -n "$mb_compiler" ]; then
3a8a92
		if [ -z "$mb_native_cc" ]; then
d015ab
			mb_native_cc=$mb_compiler
cde03b
		fi
cde03b
	fi
e692ec
e692ec
	# toolchain
3a8a92
	if [ -z "$mb_toolchain" ]; then
e692ec
		mb_toolchain='binutils'
e692ec
	fi
fe53cf
fe53cf
	# fallback host recipe
3a8a92
	if [ -n "$mb_host" ]; then
fe53cf
		if ! [ -f $mb_project_dir/sysinfo/host/$mb_host.mk ]; then
fe53cf
			if [ -z "$mb_cross_compile" ]; then
fe53cf
				mb_cross_compile=$mb_host-
fe53cf
			fi
fe53cf
fe53cf
			mb_host='any-host';
fe53cf
		fi
fe53cf
	fi
4d5e52
4d5e52
	# fallback compiler recipe
4d5e52
	if [ -n "$mb_compiler" ]; then
4d5e52
		if ! [ -f $mb_project_dir/sysinfo/compiler/$mb_compiler.mk ]; then
4d5e52
			mb_compiler='any-compiler'
4d5e52
		fi
4d5e52
	fi
cde03b
}
cde03b
cde03b
cde03b
native_defaults()
cde03b
{
cde03b
	# CC (when set, must be valid)
3a8a92
	if [ -n "$CC" ]; then
cde03b
		$CC -dM -E - < /dev/null > /dev/null || exit 2
cde03b
	fi
cde03b
d015ab
	# compiler
cde03b
	[ -z "$mb_native_cc" ] && mb_native_cc=$CC
cde03b
	[ -z "$mb_native_cc" ] && mb_native_cc='cc'
cde03b
	$mb_native_cc -dM -E - < /dev/null > /dev/null 2>/dev/null || mb_native_cc=
cde03b
cde03b
	[ -z "$mb_native_cc" ] && mb_native_cc='gcc'
cde03b
	$mb_native_cc -dM -E - < /dev/null > /dev/null 2>/dev/null || mb_native_cc=
cde03b
cde03b
	[ -z "$mb_native_cc" ] && mb_native_cc='clang'
cde03b
	$mb_native_cc -dM -E - < /dev/null > /dev/null 2>/dev/null || mb_native_cc=
cde03b
cde03b
	[ -z "$mb_native_cc" ] && mb_native_cc='cparser'
cde03b
	$mb_native_cc -dM -E - < /dev/null > /dev/null 2>/dev/null || mb_native_cc=
cde03b
3a8a92
	if [ -z "$mb_native_cc" ]; then
439371
		echo "configure: info: could not find a working native compiler."
439371
		mb_native_cc='false'
cde03b
	fi
cde03b
3a8a92
	if [ -z "$mb_compiler" ]; then
d015ab
		$mb_native_cc -dM -E - < /dev/null | grep '__clang__' > /dev/null && mb_compiler='clang'
cde03b
	fi
cde03b
3a8a92
	if [ -z "$mb_compiler" ]; then
d015ab
		$mb_native_cc -dM -E - < /dev/null | grep '__GCC' > /dev/null && mb_compiler='gcc'
cde03b
	fi
cde03b
3a8a92
	if [ -z "$mb_compiler" ]; then
d015ab
		$mb_native_cc -dM -E - < /dev/null | grep '__CPARSER__' > /dev/null && mb_compiler='cparser'
cde03b
	fi
cde03b
3a8a92
	if [ -z "$mb_compiler" ]; then
439371
		echo "configure: info: could not identify the native compiler."
439371
		mb_compiler='any-compiler'
cde03b
	fi
cde03b
cde03b
cde03b
	# host
3a8a92
	if [ -z "$mb_host" ]; then
cde03b
		mb_host='native'
cde03b
	fi
cde03b
cde03b
cde03b
	# target
3a8a92
	if [ -z "$mb_target" ]; then
cde03b
		mb_target='native'
cde03b
	fi
cde03b
cde03b
cde03b
	# os
cde03b
	mb_native_os=`uname | tr '[:upper:]' '[:lower:]'`
cde03b
cde03b
	mb_native_os_sizeof_pointer=`$mb_native_cc -dM -E - < /dev/null \
cde03b
			| grep __SIZEOF_POINTER__  \
cde03b
			| cut -d ' ' -f3`
cde03b
cde03b
	mb_native_os_bits=`expr '8' '*' '0'"$mb_native_os_sizeof_pointer"`
cde03b
cde03b
	if [ $mb_native_os_bits = 32 ]; then
cde03b
		mb_native_os_underscore='_'
cde03b
	else
cde03b
		mb_native_os_underscore=''
cde03b
	fi
cde03b
3a8a92
	if [ -z "$mb_native_os_sizeof_pointer" ]; then
cde03b
		error_msg "config error: could not determine size of pointer on native system."
cde03b
		exit 2
cde03b
	fi
cde03b
cde03b
	[ -z "$mb_native_os" ] 			&& mb_native_os=$mb_native_os
cde03b
	[ -z "$mb_native_os_bits" ] 		&& mb_native_os_bits=$mb_native_os_bits
cde03b
	[ -z "$mb_native_os_underscore" ]	&& mb_native_os_underscore=$mb_native_os_underscore
cde03b
}
cde03b
cde03b
cde03b
cross_defaults()
cde03b
{
3a8a92
	if [ -z "$mb_cross_compile" ] && [ "$mb_host" != native ]; then
cde03b
		mb_cross_compile=$mb_host'-'
cde03b
	fi
cde03b
}
cde03b
cde03b
cde03b
config_copy()
cde03b
{
cde03b
	sed 		-e 's^@package@^'"$mb_package"'^g' 				\
cde03b
			-e 's^@project_dir@^'"$mb_project_dir"'^g'			\
cde03b
											\
cde03b
			-e 's^@build@^'"$mb_build"'^g'					\
cde03b
			-e 's^@host@^'"$mb_host"'^g'					\
cde03b
			-e 's^@target@^'"$mb_target"'^g'				\
cde03b
			-e 's^@arch@^'"$mb_arch"'^g'					\
d015ab
			-e 's^@compiler@^'"$mb_compiler"'^g'				\
e692ec
			-e 's^@toolchain@^'"$mb_toolchain"'^g'				\
cde03b
			-e 's^@sysroot@^'"$mb_sysroot"'^g'				\
cde03b
			-e 's^@cross_compile@^'"$mb_cross_compile"'^g'			\
cde03b
			-e 's^@shell@^'"$mb_shell"'^g'					\
cde03b
											\
cde03b
			-e 's^@cflags@^'"$mb_cflags"'^g'				\
cde03b
			-e 's^@cflags_debug@^'"$mb_cflags_debug"'^g'			\
cde03b
			-e 's^@cflags_common@^'"$mb_cflags_common"'^g'			\
cde03b
			-e 's^@cflags_cmdline@^'"$mb_cflags $mb_cflags_cmdline"'^g'	\
cde03b
			-e 's^@cflags_config@^'"$mb_cflags_config"'^g'			\
cde03b
			-e 's^@cflags_sysroot@^'"$mb_cflags_sysroot"'^g'		\
5de097
			-e 's^@cflags_os@^'"$mb_cflags_os"'^g'				\
5de097
			-e 's^@cflags_site@^'"$mb_cflags_site"'^g'			\
cde03b
			-e 's^@cflags_path@^'"$mb_cflags_path"'^g'			\
cde03b
											\
cde03b
			-e 's^@ldflags@^'"$mb_ldflags"'^g'				\
cde03b
			-e 's^@ldflags_debug@^'"$mb_ldflags_debug"'^g'			\
cde03b
			-e 's^@ldflags_common@^'"$mb_ldflags_common"'^g'		\
cde03b
			-e 's^@ldflags_cmdline@^'"$mb_ldflags $mb_ldflags_cmdline"'^g'	\
cde03b
			-e 's^@ldflags_config@^'"$mb_ldflags_config"'^g'		\
cde03b
			-e 's^@ldflags_sysroot@^'"$mb_ldflags_sysroot"'^g'		\
cde03b
			-e 's^@ldflags_path@^'"$mb_ldflags_path"'^g'			\
cde03b
											\
cde03b
			-e 's^@pe_subsystem@^'"$mb_pe_subsystem"'^g'			\
cde03b
			-e 's^@pe_image\_base@^'"$mb_pe_image_base"'^g'			\
cde03b
			-e 's^@pe_config\_defs@^'"$mb_pe_config_defs"'^g'		\
cde03b
											\
cde03b
			-e 's^@elf_eh\_frame@^'"$mb_elf_eh_frame"'^g'			\
cde03b
			-e 's^@elf_hash\_style@^'"$mb_elf_hash_style"'^g'		\
cde03b
			-e 's^@elf_config\_defs@^'"$mb_elf_config_defs"'^g'		\
cde03b
											\
cde03b
			-e 's^@prefix@^'"$mb_prefix"'^g'				\
cde03b
			-e 's^@bindir@^'"$mb_bindir"'^g'				\
cde03b
			-e 's^@libdir@^'"$mb_libdir"'^g'				\
cde03b
			-e 's^@includedir@^'"$mb_includedir"'^g'			\
cde03b
			-e 's^@syslibdir@^'"$mb_syslibdir"'^g'				\
cde03b
			-e 's^@mandir@^'"$mb_mandir"'^g'				\
cde03b
			-e 's^@docdir@^'"$mb_docdir"'^g'				\
cde03b
			-e 's^@libexecdir@^'"$mb_libexecdir"'^g'			\
cde03b
											\
cde03b
			-e 's^@native_cc@^'"$mb_native_cc"'^g'				\
cde03b
			-e 's^@native_os@^'"$mb_native_os"'^g'				\
cde03b
			-e 's^@native_os_bits@^'"$mb_native_os_bits"'^g'		\
cde03b
			-e 's^@native_os_underscore@^'"$mb_native_os_underscore"'^g'	\
cde03b
											\
cde03b
			-e 's^@user_cc@^'"$mb_user_cc"'^g'				\
cde03b
			-e 's^@user_cpp@^'"$mb_user_cpp"'^g'				\
cde03b
			-e 's^@user_cxx@^'"$mb_user_cxx"'^g'				\
cde03b
		$mb_project_dir/Makefile.in > $mb_pwd/Makefile
cde03b
}
cde03b
cde03b
cde03b
config_host()
cde03b
{
cde03b
	make host.tag && return 0
cde03b
cde03b
	error_msg "configure was able to generate a Makefile for the selected host,"
d015ab
	error_msg "however the host-targeting compiler was found to be missing"
cde03b
	error_msg "at least one of the required headers or features."
cde03b
	exit 2
cde03b
}
cde03b
cde03b
cde03b
config_status()
cde03b
{
cde03b
	printf "\n\n"
cde03b
	make .display
cde03b
	printf "\nconfiguration completed successfully.\n\n"
cde03b
}
cde03b
cde03b
# one: init
cde03b
init_vars
cde03b
verify_build_directory
cde03b
cde03b
cde03b
# two: args
cde03b
for arg ; do
cde03b
	case "$arg" in
cde03b
		--help)	usage
cde03b
			;;
cde03b
cde03b
		# dirs
cde03b
		--prefix=*)
cde03b
			mb_prefix=${arg#*=}
cde03b
			;;
cde03b
		--bindir=*)
cde03b
			mb_bindir=${arg#*=}
cde03b
			;;
cde03b
		--libdir=*)
cde03b
			mb_libdir=${arg#*=}
cde03b
			;;
cde03b
		--includedir=*)
cde03b
			mb_includedir=${arg#*=}
cde03b
			;;
cde03b
		--syslibdir=*)
cde03b
			mb_syslibdir=${arg#*=}
cde03b
			;;
cde03b
		--mandir=*)
cde03b
			mb_mandir=${arg#*=}
cde03b
			;;
cde03b
		--libexecdir=*)
cde03b
			mb_libexecdir=${arg#*=}
cde03b
			;;
cde03b
cde03b
cde03b
		# build
cde03b
		--build=*)
cde03b
			mb_build=${arg#*=}
cde03b
			;;
cde03b
		--host=*)
cde03b
			mb_host=${arg#*=}
cde03b
			;;
cde03b
		--target=*)
cde03b
			mb_target=${arg#*=}
cde03b
			;;
cde03b
		--arch=*)
cde03b
			mb_arch=${arg#*=}
cde03b
			;;
d015ab
		--compiler=*)
d015ab
			mb_compiler=${arg#*=}
cde03b
			;;
e692ec
		--toolchain=*)
e692ec
			mb_toolchain=${arg#*=}
e692ec
			;;
cde03b
		--sysroot=*)
cde03b
			mb_sysroot=${arg#*=}
cde03b
			;;
cde03b
		--cross-compile=*)
cde03b
			mb_cross_compile=${arg#*=}
cde03b
			;;
cde03b
		--shell=*)
cde03b
			mb_shell=${arg#*=}
cde03b
			;;
cde03b
		--debug)
cde03b
			mb_debug='yes'
cde03b
			;;
cde03b
		*)
cde03b
			error_msg ${arg#}: "unsupported config argument."
cde03b
			exit 2
cde03b
			;;
cde03b
	esac
cde03b
done
cde03b
cde03b
cde03b
cde03b
# three: defaults
cde03b
common_defaults
cde03b
native_defaults
cde03b
cross_defaults
cde03b
cde03b
cde03b
cde03b
# four: config
cde03b
config_copy
cde03b
config_host
cde03b
config_status
cde03b
cde03b
cde03b
# all done
cde03b
exit 0