Blame configure

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