#!/bin/bash
##
##  configure
##
##  This script is the front-end to the build system. It provides a similar
##  interface to standard configure scripts with some extra bits for dealing
##  with toolchains that differ from the standard POSIX interface and
##  for extracting subsets of the source tree. In theory, reusable parts
##  of this script were intended to live in build/make/configure.sh,
##  but in practice, the line is pretty blurry.
##
##  This build system is based in part on the FFmpeg configure script.
##

#source_path="`dirname \"$0\"`"
source_path=${0%/*}
. "${source_path}/build/make/configure.sh"

show_help(){
    show_help_pre
    cat << EOF
Advanced options:
  ${toggle_libs}                  don't build libraries
  ${toggle_examples}              don't build examples
  --libc=PATH                     path to alternate libc
  ${toggle_fast_unaligned}        don't use unaligned accesses, even when
                                  supported by hardware [auto]
  ${toggle_codec_srcs}            in/exclude codec library source code
  ${toggle_debug_libs}            in/exclude debug version of libraries
  ${toggle_md5}                   support for output of checksum data
  ${toggle_static_msvcrt}         use static MSVCRT (VS builds only)
  ${toggle_vp8}                   VP8 codec support
  ${toggle_psnr}                  output of PSNR data, if supported (encoders)
  ${toggle_mem_tracker}           track memory usage
  ${toggle_postproc}              postprocessing
  ${toggle_multithread}           multithreaded encoding and decoding.
  ${toggle_spatial_resampling}    spatial sampling (scaling) support
  ${toggle_realtime_only}         enable this option while building for real-time encoding
  ${toggle_runtime_cpu_detect}    runtime cpu detection
  ${toggle_shared}                shared library support
  ${toggle_arm_asm_detok}         assembly version of the detokenizer (ARM platforms only)

Codecs:
  Codecs can be selectively enabled or disabled individually, or by family:
      --disable-<codec>
  is equivalent to:
      --disable-<codec>-encoder
      --disable-<codec>-decoder

  Codecs available in this distribution:
EOF
#restore editor state '

    local family;
    local last_family;
    local c;
    local str;
    for c in ${CODECS}; do
        family=${c%_*}
        if [ "${family}" != "${last_family}" ]; then
            [ -z "${str}" ] || echo "${str}"
            str="$(printf '    %10s:' ${family})"
        fi
        str="${str} $(printf '%10s' ${c#*_})"
        last_family=${family}
    done
    echo "${str}"
    show_help_post
}

##
## BEGIN APPLICATION SPECIFIC CONFIGURATION
##

# all_platforms is a list of all supported target platforms. Maintain
# alphabetically by architecture, generic-gnu last.
all_platforms="${all_platforms} armv5te-linux-rvct"
all_platforms="${all_platforms} armv5te-linux-gcc"
all_platforms="${all_platforms} armv5te-symbian-gcc"
all_platforms="${all_platforms} armv5te-wince-vs8"
all_platforms="${all_platforms} armv6-darwin-gcc"
all_platforms="${all_platforms} armv6-linux-rvct"
all_platforms="${all_platforms} armv6-linux-gcc"
all_platforms="${all_platforms} armv6-symbian-gcc"
all_platforms="${all_platforms} armv6-wince-vs8"
all_platforms="${all_platforms} iwmmxt-linux-rvct"
all_platforms="${all_platforms} iwmmxt-linux-gcc"
all_platforms="${all_platforms} iwmmxt-wince-vs8"
all_platforms="${all_platforms} iwmmxt2-linux-rvct"
all_platforms="${all_platforms} iwmmxt2-linux-gcc"
all_platforms="${all_platforms} iwmmxt2-wince-vs8"
all_platforms="${all_platforms} armv7-darwin-gcc"    #neon Cortex-A8
all_platforms="${all_platforms} armv7-linux-rvct"    #neon Cortex-A8
all_platforms="${all_platforms} armv7-linux-gcc"     #neon Cortex-A8
all_platforms="${all_platforms} mips32-linux-gcc"
all_platforms="${all_platforms} ppc32-darwin8-gcc"
all_platforms="${all_platforms} ppc32-darwin9-gcc"
all_platforms="${all_platforms} ppc64-darwin8-gcc"
all_platforms="${all_platforms} ppc64-darwin9-gcc"
all_platforms="${all_platforms} ppc64-linux-gcc"
all_platforms="${all_platforms} x86-darwin8-gcc"
all_platforms="${all_platforms} x86-darwin8-icc"
all_platforms="${all_platforms} x86-darwin9-gcc"
all_platforms="${all_platforms} x86-darwin9-icc"
all_platforms="${all_platforms} x86-linux-gcc"
all_platforms="${all_platforms} x86-linux-icc"
all_platforms="${all_platforms} x86-solaris-gcc"
all_platforms="${all_platforms} x86-win32-gcc"
all_platforms="${all_platforms} x86-win32-vs7"
all_platforms="${all_platforms} x86-win32-vs8"
all_platforms="${all_platforms} x86-win32-vs9"
all_platforms="${all_platforms} x86_64-darwin9-gcc"
all_platforms="${all_platforms} x86_64-linux-gcc"
all_platforms="${all_platforms} x86_64-linux-icc"
all_platforms="${all_platforms} x86_64-solaris-gcc"
all_platforms="${all_platforms} x86_64-win64-vs8"
all_platforms="${all_platforms} x86_64-win64-vs9"
all_platforms="${all_platforms} universal-darwin8-gcc"
all_platforms="${all_platforms} universal-darwin9-gcc"
all_platforms="${all_platforms} generic-gnu"

# all_targets is a list of all targets that can be configured
# note that these should be in dependency order for now.
all_targets="libs examples docs"

# all targets available are enabled, by default.
for t in ${all_targets}; do
    [ -f ${source_path}/${t}.mk ] && enable ${t}
done

# check installed doxygen version
doxy_version=$(doxygen --version 2>/dev/null)
doxy_major=${doxy_version%%.*}
if [ ${doxy_major:-0} -ge 1 ]; then
    doxy_version=${doxy_version#*.}
    doxy_minor=${doxy_version%%.*}
    doxy_patch=${doxy_version##*.}

    [ $doxy_major -gt 1 ] && enable doxygen
    [ $doxy_minor -gt 5 ] && enable doxygen
    [ $doxy_minor -eq 5 ] && [ $doxy_patch -ge 3 ] && enable doxygen
fi

# install everything except the sources, by default. sources will have
# to be enabled when doing dist builds, since that's no longer a common
# case.
enabled doxygen && php -v >/dev/null 2>&1 && enable install_docs
enable install_bins
enable install_libs

enable optimizations
enable fast_unaligned #allow unaligned accesses, if supported by hw
enable md5
enable spatial_resampling
enable multithread

[ -d ${source_path}/../include ] && enable alt_tree_layout
for d in vp8; do
    [ -d ${source_path}/${d} ] && disable alt_tree_layout;
done

if ! enabled alt_tree_layout; then
# development environment
[ -d ${source_path}/vp8 ] && CODECS="${CODECS} vp8_encoder vp8_decoder"
else
# customer environment
[ -f ${source_path}/../include/vpx/vp8cx.h ] && CODECS="${CODECS} vp8_encoder"
[ -f ${source_path}/../include/vpx/vp8dx.h ] && CODECS="${CODECS} vp8_decoder"

[ -f ${source_path}/../lib/*/*mt.lib ] && soft_enable static_msvcrt
fi

CODECS="$(echo ${CODECS} | tr ' ' '\n')"
CODEC_FAMILIES="$(for c in ${CODECS}; do echo ${c%_*}; done | sort | uniq)"

ARCH_LIST="
    arm
    mips
    x86
    x86_64
    ppc32
    ppc64
"
ARCH_EXT_LIST="
    armv5te
    armv6
    armv7
    iwmmxt
    iwmmxt2

    mips32

    mmx
    sse
    sse2
    sse3
    ssse3

    altivec
"
HAVE_LIST="
    ${ARCH_EXT_LIST}
    vpx_ports
    stdint_h
    alt_tree_layout
    pthread_h
    sys_mman_h
"
CONFIG_LIST="
    external_build
    install_docs
    install_bins
    install_libs
    install_srcs
    debug
    gprof
    gcov
    rvct
    gcc
    msvs
    pic
    big_endian

    codec_srcs
    debug_libs
    fast_unaligned
    mem_manager
    mem_tracker
    mem_checks
    md5

    dequant_tokens
    dc_recon
    runtime_cpu_detect
    postproc
    multithread
    psnr
    ${CODECS}
    ${CODEC_FAMILIES}
    encoders
    decoders
    static_msvcrt
    spatial_resampling
    realtime_only
    shared
    arm_asm_detok
"
CMDLINE_SELECT="
    extra_warnings
    werror
    install_docs
    install_bins
    install_libs
    install_srcs
    debug
    gprof
    gcov
    pic
    optimizations
    ccache
    runtime_cpu_detect

    libs
    examples
    libc
    fast_unaligned
    codec_srcs
    debug_libs
    md5

    dequant_tokens
    dc_recon
    postproc
    multithread
    psnr
    ${CODECS}
    ${CODEC_FAMILIES}
    static_msvcrt
    mem_tracker
    spatial_resampling
    realtime_only
    shared
    arm_asm_detok
"

process_cmdline() {
    for opt do
        optval="${opt#*=}"
        case "$opt" in
        --disable-codecs) for c in ${CODECS}; do disable $c; done ;;
        *) process_common_cmdline $opt
        ;;
        esac
    done
}

post_process_cmdline() {
    local c

    # If the codec family is disabled, disable all components of that family.
    # If the codec family is enabled, enable all components of that family.
    log_echo "Configuring selected codecs"
    for c in ${CODECS}; do
        disabled ${c%%_*} && disable ${c}
        enabled ${c%%_*} && enable ${c}
    done

    # Enable all detected codecs, if they haven't been disabled
    for c in ${CODECS}; do soft_enable $c; done

    # Enable the codec family if any component of that family is enabled
    for c in ${CODECS}; do
        enabled $c && enable ${c%_*}
    done

    # Set the {en,de}coders variable if any algorithm in that class is enabled
    for c in ${CODECS}; do
        enabled ${c} && enable ${c##*_}s
    done


}


process_targets() {
    enabled child || write_common_config_banner
    enabled universal || write_common_target_config_h  ${BUILD_PFX}vpx_config.h

    # TODO: add host tools target (obj_int_extract, etc)

    # For fat binaries, call configure recursively to configure for each
    # binary architecture to be included.
    if enabled universal; then
        # Call configure (ourselves) for each subarchitecture
        for arch in $fat_bin_archs; do
            BUILD_PFX=${arch}/ toolchain=${arch} $self --child $cmdline_args || exit $?
        done
    fi

    # The write_common_config (config.mk) logic is deferred until after the
    # recursive calls to configure complete, becuase we want our universal
    # targets to be executed last.
    write_common_config_targets
    enabled universal && echo "FAT_ARCHS=${fat_bin_archs}" >> config.mk

    # Calculate the default distribution name, based on the enabled features
    local cf
    local DIST_DIR=vpx
    for cf in $CODEC_FAMILIES; do
        if enabled ${cf}_encoder && enabled ${cf}_decoder; then
            DIST_DIR="${DIST_DIR}-${cf}"
        elif enabled ${cf}_encoder; then
            DIST_DIR="${DIST_DIR}-${cf}cx"
        elif enabled ${cf}_decoder; then
            DIST_DIR="${DIST_DIR}-${cf}dx"
        fi
    done
    enabled debug_libs && DIST_DIR="${DIST_DIR}-debug"
    enabled codec_srcs && DIST_DIR="${DIST_DIR}-src"
    ! enabled postproc && DIST_DIR="${DIST_DIR}-nopost"
    ! enabled multithread && DIST_DIR="${DIST_DIR}-nomt"
    ! enabled install_docs && DIST_DIR="${DIST_DIR}-nodocs"
    DIST_DIR="${DIST_DIR}-${tgt_isa}-${tgt_os}"
    case "${tgt_os}" in
    win*) enabled static_msvcrt && DIST_DIR="${DIST_DIR}mt" || DIST_DIR="${DIST_DIR}md"
          DIST_DIR="${DIST_DIR}-${tgt_cc}"
          ;;
    esac
    if [ -f "${source_path}/build/make/version.sh" ]; then
        local ver=`"$source_path/build/make/version.sh" --bare $source_path`
        DIST_DIR="${DIST_DIR}-${ver}"
        ver=${ver%%-*}
        VERSION_PATCH=${ver##*.}
        ver=${ver%.*}
        VERSION_MINOR=${ver##*.}
        ver=${ver#v}
        VERSION_MAJOR=${ver%.*}
    fi
    enabled child || cat <<EOF >> config.mk
ifeq (\$(MAKECMDGOALS),dist)
DIST_DIR?=${DIST_DIR}
else
DIST_DIR?=\$(DESTDIR)${prefix}
endif
LIBSUBDIR=${libdir##${prefix}/}

VERSION_MAJOR=${VERSION_MAJOR}
VERSION_MINOR=${VERSION_MINOR}
VERSION_PATCH=${VERSION_PATCH}

CONFIGURE_ARGS=${CONFIGURE_ARGS}
EOF
    enabled child || echo "CONFIGURE_ARGS?=${CONFIGURE_ARGS}" >> config.mk

    #
    # Write makefiles for all enabled targets
    #
    for tgt in libs examples docs solution; do
        local tgt_fn="$tgt-$toolchain.mk"

        if enabled $tgt; then
            echo "Creating makefiles for ${toolchain} ${tgt}"
            write_common_target_config_mk $tgt_fn ${BUILD_PFX}vpx_config.h
            #write_${tgt}_config
        fi
    done

}

process_detect() {
    if enabled shared; then
        # Can only build shared libs on a subset of platforms. Doing this check
        # here rather than at option parse time because the target auto-detect
        # magic happens after the command line has been parsed.
        enabled linux || die "--enable-shared only supported on ELF for now"
    fi
    if [ -z "$CC" ]; then
        echo "Bypassing toolchain for environment detection."
        enable external_build
        check_header() {
            log fake_check_header "$@"
            header=$1
            shift
            var=`echo $header | sed 's/[^A-Za-z0-9_]/_/g'`
            disable $var
            case $header in
                stdio.h)
                    true;
                ;;
                *)
                    local result=false
                    for d in "$@"; do
                        [ -f "${d##-I}/$header" ] && result=true && break
                    done
                    ${result:-true}
            esac && enable $var
        }
        check_ld() {
            true
        }
    fi
    check_header stdio.h || die "Unable to invoke compiler: ${CC} ${CFLAGS}"
    check_ld <<EOF || die "Toolchain is unable to link executables"
int main(void) {return 0;}
EOF
    # check system headers
    check_header stdint.h
    check_header pthread.h
    check_header sys/mman.h

    check_header vpx/vpx_integer.h -I${source_path} && enable vpx_ports
}

process_toolchain() {
    process_common_toolchain

    # Handle universal binaries for this architecture
    case $toolchain in
        universal-darwin*)
            local darwin_ver=${tgt_os##darwin}
            fat_bin_archs="$fat_bin_archs ppc32-${tgt_os}-gcc"

            # Intel
            fat_bin_archs="$fat_bin_archs x86-${tgt_os}-${tgt_cc}"
            if [ $darwin_ver -gt 8 ]; then
                fat_bin_archs="$fat_bin_archs x86_64-${tgt_os}-${tgt_cc}"
            fi
            ;;
    esac


    # Enable some useful compiler flags
    if enabled gcc; then
        enabled werror && check_add_cflags -Werror
        check_add_cflags -Wall
        check_add_cflags -Wdeclaration-after-statement
        check_add_cflags -Wdisabled-optimization
        check_add_cflags -Wpointer-arith
        check_add_cflags -Wtype-limits
        check_add_cflags -Wcast-qual
        enabled extra_warnings || check_add_cflags -Wno-unused
    fi

    if enabled icc; then
        enabled werror && check_add_cflags -Werror
        check_add_cflags -Wall
        check_add_cflags -Wpointer-arith

        # ICC has a number of floating point optimizations that we disable
        # in favor of deterministic output WRT to other compilers
        add_cflags -fp-model precise
    fi

    # Enable extra, harmless warnings. These might provide additional insight
    # to what the compiler is doing and why, but in general, but they shouldn't
    # be treated as fatal, even if we're treating warnings as errors.
    GCC_EXTRA_WARNINGS="
        -Wdisabled-optimization
        -Winline
    "
    enabled gcc && EXTRA_WARNINGS="${GCC_EXTRA_WARNINGS}"
    RVCT_EXTRA_WARNINGS="
        --remarks
    "
    enabled rvct && EXTRA_WARNINGS="${RVCT_EXTRA_WARNINGS}"
    if enabled extra_warnings; then
        for w in ${EXTRA_WARNINGS}; do
            check_add_cflags ${w}
            enabled gcc && enabled werror && check_add_cflags -Wno-error=${w}
        done
    fi

    # ccache only really works on gcc toolchains
    enabled gcc || soft_disable ccache
    if enabled mips; then
        enable dequant_tokens
        enable dc_recon
    fi

    # Enable the postbuild target if building for visual studio.
    case "$tgt_cc" in
        vs*) enable msvs
             enable solution
             vs_version=${tgt_cc##vs}
             all_targets="${all_targets} solution"
        ;;
    esac

    # Other toolchain specific defaults
    case $toolchain in x86*|ppc*|universal*) soft_enable postproc;; esac
}


##
## END APPLICATION SPECIFIC CONFIGURATION
##
CONFIGURE_ARGS="$@"
process "$@"
cat <<EOF > ${BUILD_PFX}vpx_config.c
static const char* const cfg = "$CONFIGURE_ARGS";
const char *vpx_codec_build_config(void) {return cfg;}
EOF