language: cpp

# OS config, depends on actual 'os' in build matrix
dist: bionic

stages:
  - build

env:
  global:
    - SCONS_CACHE=$HOME/.scons_cache/$TRAVIS_BRANCH
    - SCONS_CACHE_LIMIT=1024
    - OPTIONS="debug_symbols=no verbose=yes progress=no"
    - secure: "uch9QszCgsl1qVbuzY41P7S2hWL2IiNFV4SbAYRCdi0oJ9MIu+pVyrQdpf3+jG4rH6j4Rffl+sN17Zz4dIDDioFL1JwqyCqyCyswR8uACC0Rr8gr4Mi3+HIRbv+2s2P4cIQq41JM8FJe84k9jLEMGCGh69w+ibCWoWs74CokYVA="

cache:
  directories:
    - $SCONS_CACHE

matrix:
  include:
    - name: Static checks (clang-format) + Documentation checks
      stage: build
      env: STATIC_CHECKS=yes
      os: linux
      compiler: gcc
      addons:
        apt:
          packages:
            - clang-format-8

    - name: Linux editor (debug, GCC 9, with Mono)
      stage: build
      env: PLATFORM=linuxbsd TOOLS=yes TARGET=debug CACHE_NAME=${PLATFORM}-tools-mono-gcc-9 MATRIX_EVAL="CC=gcc-9 && CXX=g++-9" EXTRA_ARGS="module_mono_enabled=yes mono_glue=no warnings=extra werror=yes"
      os: linux
      compiler: gcc-9
      addons:
        apt:
          sources:
            - sourceline: "deb https://download.mono-project.com/repo/ubuntu stable-bionic main"
              key_url: "https://raw.githubusercontent.com/travis-ci/apt-source-safelist/master/keys/mono.asc"
            - sourceline: "ppa:ubuntu-toolchain-r/test"
          packages:
            - &gcc9_deps [gcc-9, g++-9]
            - &linux_deps [libasound2-dev, libgl1-mesa-dev, libglu1-mesa-dev, libx11-dev, libxcursor-dev, libxi-dev, libxinerama-dev, libxrandr-dev]
            - &linux_mono_deps [mono-devel, msbuild, nuget]

    - name: Linux export template (release, Clang 7)
      stage: build
      env: PLATFORM=linuxbsd TOOLS=no TARGET=release CACHE_NAME=${PLATFORM}-clang EXTRA_ARGS="warnings=extra werror=yes"
      os: linux
      compiler: clang
      addons:
        apt:
          packages:
            - *linux_deps

    - name: Android export template (release_debug, Clang)
      stage: build
      env: PLATFORM=android TOOLS=no TARGET=release_debug CACHE_NAME=${PLATFORM}-clang EXTRA_ARGS="warnings=extra werror=yes"
      os: linux
      compiler: clang
      addons:
        apt:
          packages:
            - openjdk-8-jdk

    - name: macOS editor (debug, Clang)
      stage: build
      env: PLATFORM=osx TOOLS=yes TARGET=debug CACHE_NAME=${PLATFORM}-tools-clang EXTRA_ARGS="warnings=extra werror=yes"
      os: osx
      osx_image: xcode11.5
      compiler: clang
      addons:
        homebrew:
          packages:
            - scons
          update: true

# TODO: iOS MoltenVK support

#    - name: iOS export template (debug, Clang)
#      stage: build
#      env: PLATFORM=iphone TOOLS=no TARGET=debug CACHE_NAME=${PLATFORM}-clang
#      os: osx
#      osx_image: xcode11.5
#      compiler: clang
#      addons:
#        homebrew:
#          packages:
#            - scons

    - name: Linux export template (release_debug, GCC 7, without 3D support)
      stage: build
      env: PLATFORM=linuxbsd TOOLS=no TARGET=release_debug CACHE_NAME=${PLATFORM}-gcc-7 EXTRA_ARGS="disable_3d=yes"
      os: linux
      compiler: gcc
      addons:
        apt:
          packages:
            - *linux_deps

    - name: JavaScript export template (release, emscripten latest)
      stage: build
      env: PLATFORM=javascript TOOLS=no TARGET=release CACHE_NAME=${PLATFORM}-emcc-latest EXTRA_ARGS="use_closure_compiler=yes"
      os: linux
      compiler: clang
      addons:
        apt:
          packages:
            - *linux_deps

before_install:
  - eval "${MATRIX_EVAL}"

install:
  - if [ "$TRAVIS_OS_NAME" = "linux" ]; then
      pyenv global 3.8 system;
      pip3 install --user scons;
    fi
  - scons --version
  - if [ "$TRAVIS_OS_NAME" = "linux" ] && [ "$PLATFORM" = "android" ]; then
      export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64;
      export PATH=/usr/lib/jvm/java-8-openjdk-amd64/jre/bin:${PATH};
      java -version;
      misc/travis/android-tools-linux.sh;
    fi
  - if [ "$PLATFORM" = "javascript" ]; then
      git clone --depth 1 "https://github.com/emscripten-core/emsdk.git";
      ./emsdk/emsdk install latest;
      ./emsdk/emsdk activate --no-embedded latest;
    fi
  - if [ "$STATIC_CHECKS" = "yes" ]; then
      unset SCONS_CACHE;
      pip3 install --user black pygments;
    fi

before_script:
  - if [ "$PLATFORM" = "android" ]; then
      export ANDROID_HOME=$TRAVIS_BUILD_DIR/godot-dev/build-tools/android-sdk;
      export ANDROID_NDK_ROOT=$TRAVIS_BUILD_DIR/godot-dev/build-tools/android-ndk;
    fi

script:
  - if [ "$STATIC_CHECKS" = "yes" ]; then
      sh ./misc/travis/clang-format.sh &&
      sh ./misc/travis/black-format.sh &&
      doc/tools/makerst.py --dry-run doc/classes modules;
    else
      scons -j2 CC=$CC CXX=$CXX platform=$PLATFORM tools=$TOOLS target=$TARGET $OPTIONS $EXTRA_ARGS &&
      if [ "$TEST_PROJECT" = "yes" ]; then
        git clone --depth 1 "https://github.com/godotengine/godot-tests.git";
        sed -i "s:custom_template/release=\"\":custom_template/release=\"$(readlink -e bin/godot_server.linuxbsd.opt.tools.64)\":" godot-tests/tests/project_export/export_presets.cfg;
        godot-tests/tests/project_export/test_project.sh "bin/godot_server.linuxbsd.opt.tools.64";
      fi
    fi