language: c

matrix:
  include:
    # Linux
    - os: linux
      compiler: gcc
      dist: trusty
      sudo: required
    - os: linux
      compiler: clang
      dist: trusty
      sudo: required
    - os: linux
      compiler: gcc
      dist: precise
      sudo: required
    - os: linux
      compiler: clang
      dist: precise
      sudo: required

    # macOS
    - os: osx
      osx_image: xcode9.3beta
      language: objective-c
    - os: osx
      osx_image: xcode9.2
      language: objective-c
    - os: osx
      osx_image: xcode9.1
      language: objective-c
    - os: osx
      osx_image: xcode9
      language: objective-c
    - os: osx
      osx_image: xcode8.3
      language: objective-c
    - os: osx
      osx_image: xcode8
      language: objective-c
    - os: osx
      osx_image: xcode7.3
      language: objective-c
    - os: osx
      osx_image: xcode6.4
      language: objective-c

    # AmigaOS
    - os: linux
      dist: trusty
      env:
        - config=amigaos

    # Nintendo DS and 3DS
    #- os: linux
    #  dist: trusty
    #  env:
    #    - config=devkitarm

    # Nintendo Wii
    #- os: linux
    #  dist: trusty
    #  env:
    #    - config=devkitppc

    # PSP
    #- os: linux
    #  dist: trusty
    #  env:
    #    - config=devkitpsp

before_install:
  - if [ "$TRAVIS_OS_NAME" = "linux" -a -z "$config" ]; then
            if ! sudo apt-get -qq update >/tmp/apt_log 2>&1; then
                    cat /tmp/apt_log;
                    exit 1;
            fi;
            if ! sudo apt-get -qq install -y gobjc-multilib >/tmp/apt_log 2>&1;
            then
                    cat /tmp/apt_log;
                    exit 1;
            fi;
    fi

  - if [ "$config" = "devkitarm" ]; then
            base_url="https://download.sourceforge.net/project/devkitpro";
            wget -q "$base_url/devkitARM/devkitARM_r46/devkitARM_r46-x86_64-linux.tar.bz2"
                    "$base_url/libctru/1.4.0/libctru-1.4.0.tar.bz2"
                    "$base_url/libnds/1.7.1/libnds-1.7.1.tar.bz2"
                    "$base_url/libfat/1.1.2/libfat-nds-1.1.2.tar.bz2"
                    "$base_url/filesystem/0.9.13-1/libfilesystem-0.9.13-1.tar.bz2";
            mkdir -p "$HOME/devkitPro/libctru";
            tar -C "$HOME/devkitPro" -xjf devkitARM_r46-x86_64-linux.tar.bz2;
            tar -C "$HOME/devkitPro/libctru" -xjf libctru-1.4.0.tar.bz2;
            mkdir -p "$HOME/devkitPro/libnds";
            tar -C "$HOME/devkitPro/libnds" -xjf libnds-1.7.1.tar.bz2;
            tar -C "$HOME/devkitPro/libnds" -xjf libfat-nds-1.1.2.tar.bz2;
            tar -C "$HOME/devkitPro/libnds" -xjf libfilesystem-0.9.13-1.tar.bz2;
    fi

  - if [ "$config" = "devkitppc" ]; then
            base_url="https://download.sourceforge.net/project/devkitpro";
            wget -q "$base_url/devkitPPC/devkitPPC_r29-1/devkitPPC_r29-1-x86_64-linux.tar.bz2"
                    "$base_url/libogc/1.8.16/libogc-1.8.16.tar.bz2"
                    "$base_url/libfat/1.1.2/libfat-ogc-1.1.2.tar.bz2";

            mkdir -p "$HOME/devkitPro/libogc";
            tar -C "$HOME/devkitPro" -xjf devkitPPC_r29-1-x86_64-linux.tar.bz2;
            tar -C "$HOME/devkitPro/libogc" -xjf libogc-1.8.16.tar.bz2;
            tar -C "$HOME/devkitPro/libogc" -xjf libfat-ogc-1.1.2.tar.bz2;
    fi

  - if [ "$config" = "devkitpsp" ]; then
            wget -q https://download.sourceforge.net/project/devkitpro/devkitPSP/devkitPSP_r16-1-x86_64-linux.tar.bz2;

            mkdir -p "$HOME/devkitPro";
            tar -C "$HOME/devkitPro" -xjf devkitPSP_r16-1-x86_64-linux.tar.bz2;
    fi

  - if [ "$config" = "amigaos" ]; then
            wget -q http://franke.ms/download/amiga-toolchain-centos.tgz;
            tar -C / -xzf amiga-toolchain-centos.tgz;
    fi

script:
  - echo -e '%s/-DSTDOUT$/&_SIMPLE/\nwq' | ed -s tests/Makefile

  - build() {
            if ! git clean -fxd >/tmp/clean_log 2>&1; then
                    cat /tmp/clean_log;
                    exit 1;
            fi;
            echo ">> Configuring with $@";
            ./autogen.sh;
            if ! ./configure ac_cv_path_TPUT= "$@"; then
                    cat config.log;
                    exit 1;
            fi;
            echo ">> Building (configured with $@)";
            if ! make -j4 >/tmp/make_log 2>&1; then
                    cat /tmp/make_log;
                    exit 1;
            fi;
            echo ">> Installing (configured with $@)";
            if ! sudo PATH="$PATH" make install >/tmp/install_log 2>&1; then
                    cat /tmp/install_log;
                    exit 1;
            fi;
    }

  - if [ "$TRAVIS_OS_NAME" = "linux" -a -z "$config" ]; then
            build_32_64() {
                    build OBJC="$CC" $@;
                    build OBJC="$CC -m32" --host=i686-pc-linux-gnu $@;
            };

            build_32_64;
            build_32_64 --enable-seluid24;
            build_32_64 --disable-threads;
            build_32_64 --disable-threads --disable-sockets;
            build_32_64 --disable-threads --disable-files;
            build_32_64 --disable-threads --disable-sockets --disable-files;
            build_32_64 --disable-sockets;
            build_32_64 --disable-sockets --disable-files;
            build_32_64 --disable-files;
            build_32_64 --disable-shared;
            build_32_64 --disable-shared --enable-seluid24;
            build_32_64 --disable-compiler-tls;
            build_32_64 --disable-compiler-tls --disable-threads;
    fi

  - if [ "$TRAVIS_OS_NAME" = "osx" -a -z "$config" ]; then
            build_mac_32_64() {
                    build $@;
                    build OBJC="clang -m32" --host=i386-apple-darwin $@;
            };

            if xcodebuild -version | grep 'Xcode 6' >/dev/null; then
                    export CPPFLAGS="-D_Nullable=__nullable
                                     -D_Nonnull=__nonnull
                                     -D_Null_unspecified=__null_unspecified";
            fi;

            build_mac_32_64;
            build_mac_32_64 --disable-threads;
            build_mac_32_64 --disable-threads --disable-sockets;
            build_mac_32_64 --disable-threads --disable-files;
            build_mac_32_64 --disable-threads --disable-sockets --disable-files;
            build_mac_32_64 --disable-sockets;
            build_mac_32_64 --disable-sockets --disable-files;
            build_mac_32_64 --disable-files;
            build_mac_32_64 --disable-shared;
            build_mac_32_64 --enable-runtime;
            build_mac_32_64 --enable-runtime --enable-seluid24;
            build_mac_32_64 --enable-runtime --disable-threads;
            build_mac_32_64 --enable-runtime --disable-threads
                            --disable-sockets;
            build_mac_32_64 --enable-runtime --disable-threads --disable-files;
            build_mac_32_64 --enable-runtime --disable-threads
                            --disable-sockets --disable-files;
            build_mac_32_64 --enable-runtime --disable-sockets;
            build_mac_32_64 --enable-runtime --disable-sockets --disable-files;
            build_mac_32_64 --enable-runtime --disable-files;
            build_mac_32_64 --enable-runtime --disable-shared;
            build_mac_32_64 --enable-runtime --disable-shared --enable-seluid24;

            export IPHONEOS_DEPLOYMENT_TARGET="9.0";
            clang="clang --sysroot $(xcrun --sdk iphoneos --show-sdk-path)";
            export OBJC="$clang -arch armv7 -arch arm64";
            export OBJCPP="$clang -arch armv7 -E";
            build --host=arm-apple-darwin --enable-static;

            sysroot="$(xcrun --sdk iphonesimulator --show-sdk-path)";
            clang="clang --sysroot $sysroot";
            export OBJC="$clang -arch i386 -arch x86_64";
            export OBJCPP="$clang -arch i386 -E";
            build WRAPPER=true --host=i386-apple-darwin --enable-static;
    fi

  - if [ "$config" = "amigaos" ]; then
            export PATH="/opt/amiga/bin:$PATH";

            build --host=m68k-amigaos;
            build --host=m68k-amigaos --disable-amiga-lib;
            build --host=m68k-amigaos --enable-static;
    fi

  - if [ "$config" = "devkitarm" ]; then
            export DEVKITPRO="$HOME/devkitPro";
            export PATH="$DEVKITPRO/devkitARM/bin:$PATH";

            build --host=arm-none-eabi --with-3ds;
            build --host=arm-none-eabi --with-nds;
    fi

  - if [ "$config" = "devkitppc" ]; then
            export DEVKITPRO="$HOME/devkitPro";
            export PATH="$DEVKITPRO/devkitPPC/bin:$PATH";

            build ac_cv_prog_wiiload= --host=powerpc-eabi --with-wii;
    fi

  - if [ "$config" = "devkitpsp" ]; then
            export DEVKITPRO="$HOME/devkitPro";
            export DEVKITPSP="$DEVKITPRO/devkitPSP";
            export PATH="$DEVKITPSP/bin:$PATH";

            build --host=psp;
    fi
