Blame configure

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