#!/bin/bash # mg2d configure script # partially inspired by/based on the FFmpeg configure script die() { echo "$@" >&2 exit 1 } bootstrap() { local source_dir [ -e configure ] && die "Cannot build from the source dir, create a separate build directory" source_dir=$(realpath $(dirname "$0")) [ -d "$source_dir" ] || die "Cannot locate source directory" [ -h src ] || ln -s "$source_dir" src [ -h Makefile ] || ln -s "$source_dir/Makefile" echo $source_dir } toupper() { echo "$@" | tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ } is_in() { value=$1 shift for var in $*; do [ $var = $value ] && return 0 done return 1 } probe_arch() { arch=$(cc -dumpmachine | sed 's/-.*//') } check_mpi() { pkg-config --exists mpi || return 1 mpi_cflags=$(pkg-config --cflags mpi) mpi_libs=$(pkg-config --libs mpi) } check_nasm() { if ! command -v nasm &> /dev/null; then echo "nasm not found, assembly code will not be built" return 1 fi } check_component() { name=$1 shift eval "check_$name" $@ && eval "have_$name=yes" } source_dir=$(bootstrap) CMDLINE_SET=" cc " cc=cc for arg; do name="${arg%%=*}" name="${name#--}" name=$(echo "$name" | sed 's/-/_/g') val="${arg#*=}" if is_in $name $CMDLINE_SET; then eval $name='$val' continue fi case "$name" in extra_cflags) extra_cflags=$val $extra_cflags ;; extra_ldflags) extra_ldflags=$val $extra_ldflags ;; *) die "Unknown commandline argument: $arg" ;; esac done probe_arch COMPONENTS_REQUIRED=" mpi " COMPONENTS_OPTIONAL=" nasm " COMPONENTS=" $COMPONENTS_REQUIRED $COMPONENTS_OPTIONAL " for c in $COMPONENTS_REQUIRED; do check_component $c || die "Required component $c not found" done for c in $COMPONENTS_OPTIONAL; do check_component $c done cat > config.mak < config.h < config.asm << EOF EOF case "$arch" in x86_64) echo "ARCH_X86_64 = 1" >> config.mak echo "#define ARCH_X86_64 1" >> config.h echo "%define ARCH_X86_64 1" >> config.asm ;; *) echo "ARCH_X86_64 = 0" >> config.mak echo "#define ARCH_X86_64 0" >> config.h echo "%define ARCH_X86_64 0" >> config.asm ;; esac for c in $COMPONENTS; do C=$(toupper $c) have_ref=have_$c if [ -n "${!have_ref}" ]; then val_mak=yes val_c=1 else val_mak=no val_c=0 fi echo "HAVE_$C = $val_mak" >> config.mak echo "#define HAVE_$C $val_c" >> config.h done echo "#endif // MG2D_CONFIG_H" >> config.h