language: shell
sudo: required
dist: xenial
services:
  - docker
addons:
  apt:
    packages:
      - gdb

git:
  depth: 2
  submodules: false

matrix:
  fast_finish: true
  include:
    # Images used in testing PR and try-build should be run first.
    - env: IMAGE=x86_64-gnu-llvm-6.0 RUST_BACKTRACE=1
      if: type = pull_request OR branch = auto

    - env: IMAGE=dist-x86_64-linux DEPLOY=1
      if: branch = try OR branch = auto

    # "alternate" deployments, these are "nightlies" but have LLVM assertions
    # turned on, they're deployed to a different location primarily for
    # additional testing.
    - env: IMAGE=dist-x86_64-linux DEPLOY_ALT=1 CI_JOB_NAME=dist-x86_64-linux-alt
      if: branch = try OR branch = auto

    - env: >
        RUST_CHECK_TARGET=dist
        RUST_CONFIGURE_ARGS="--enable-extended --enable-profiler --enable-lldb --set rust.jemalloc"
        SRC=.
        DEPLOY_ALT=1
        RUSTC_RETRY_LINKER_ON_SEGFAULT=1
        MACOSX_DEPLOYMENT_TARGET=10.7
        NO_LLVM_ASSERTIONS=1
        NO_DEBUG_ASSERTIONS=1
        CI_JOB_NAME=dist-x86_64-apple-alt
      os: osx
      osx_image: xcode9.3-moar
      if: branch = auto

    # macOS builders. These are placed near the beginning because they are very
    # slow to run.

    # OSX builders running tests, these run the full test suite.
    # NO_DEBUG_ASSERTIONS=1 to make them go faster, but also do have some
    # runners that run `//ignore-debug` tests.
    #
    # Note that the compiler is compiled to target 10.8 here because the Xcode
    # version that we're using, 8.2, cannot compile LLVM for OSX 10.7.
    - env: >
        RUST_CHECK_TARGET=check
        RUST_CONFIGURE_ARGS="--build=x86_64-apple-darwin --enable-sanitizers --enable-profiler --set rust.jemalloc"
        SRC=.
        RUSTC_RETRY_LINKER_ON_SEGFAULT=1
        MACOSX_DEPLOYMENT_TARGET=10.8
        MACOSX_STD_DEPLOYMENT_TARGET=10.7
        NO_LLVM_ASSERTIONS=1
        NO_DEBUG_ASSERTIONS=1
        CI_JOB_NAME=x86_64-apple
      os: osx
      osx_image: xcode9.3-moar
      if: branch = auto

    - env: >
        RUST_CHECK_TARGET=check
        RUST_CONFIGURE_ARGS="--build=i686-apple-darwin --set rust.jemalloc"
        SRC=.
        RUSTC_RETRY_LINKER_ON_SEGFAULT=1
        MACOSX_DEPLOYMENT_TARGET=10.8
        MACOSX_STD_DEPLOYMENT_TARGET=10.7
        NO_LLVM_ASSERTIONS=1
        NO_DEBUG_ASSERTIONS=1
        CI_JOB_NAME=i686-apple
      os: osx
      osx_image: xcode9.3-moar
      if: branch = auto

    # OSX builders producing releases. These do not run the full test suite and
    # just produce a bunch of artifacts.
    #
    # Note that these are running in the `xcode7` image instead of the
    # `xcode8.2` image as above. That's because we want to build releases for
    # OSX 10.7 and `xcode7` is the latest Xcode able to compile LLVM for 10.7.
    - env: >
        RUST_CHECK_TARGET=dist
        RUST_CONFIGURE_ARGS="--build=i686-apple-darwin --enable-full-tools --enable-profiler --enable-lldb --set rust.jemalloc"
        SRC=.
        DEPLOY=1
        RUSTC_RETRY_LINKER_ON_SEGFAULT=1
        MACOSX_DEPLOYMENT_TARGET=10.7
        NO_LLVM_ASSERTIONS=1
        NO_DEBUG_ASSERTIONS=1
        DIST_REQUIRE_ALL_TOOLS=1
        CI_JOB_NAME=dist-i686-apple
      os: osx
      osx_image: xcode9.3-moar
      if: branch = auto

    - env: >
        RUST_CHECK_TARGET=dist
        RUST_CONFIGURE_ARGS="--target=aarch64-apple-ios,armv7-apple-ios,armv7s-apple-ios,i386-apple-ios,x86_64-apple-ios --enable-full-tools --enable-sanitizers --enable-profiler --enable-lldb --set rust.jemalloc"
        SRC=.
        DEPLOY=1
        RUSTC_RETRY_LINKER_ON_SEGFAULT=1
        MACOSX_DEPLOYMENT_TARGET=10.7
        NO_LLVM_ASSERTIONS=1
        NO_DEBUG_ASSERTIONS=1
        DIST_REQUIRE_ALL_TOOLS=1
        CI_JOB_NAME=dist-x86_64-apple
      os: osx
      osx_image: xcode9.3-moar
      if: branch = auto

    # Linux builders, remaining docker images
    - env: IMAGE=arm-android
      if: branch = auto
    - env: IMAGE=armhf-gnu
      if: branch = auto
    - env: IMAGE=dist-various-1 DEPLOY=1
      if: branch = auto
    - env: IMAGE=dist-various-2 DEPLOY=1
      if: branch = auto
    - env: IMAGE=dist-aarch64-linux DEPLOY=1
      if: branch = auto
    - env: IMAGE=dist-android DEPLOY=1
      if: branch = auto
    - env: IMAGE=dist-arm-linux DEPLOY=1
      if: branch = auto
    - env: IMAGE=dist-armhf-linux DEPLOY=1
      if: branch = auto
    - env: IMAGE=dist-armv7-linux DEPLOY=1
      if: branch = auto
    - env: IMAGE=dist-i586-gnu-i586-i686-musl DEPLOY=1
      if: branch = auto
    - env: IMAGE=dist-i686-freebsd DEPLOY=1
      if: branch = auto
    - env: IMAGE=dist-i686-linux DEPLOY=1
      if: branch = auto
    - env: IMAGE=dist-mips-linux DEPLOY=1
      if: branch = auto
    - env: IMAGE=dist-mips64-linux DEPLOY=1
      if: branch = auto
    - env: IMAGE=dist-mips64el-linux DEPLOY=1
      if: branch = auto
    - env: IMAGE=dist-mipsel-linux DEPLOY=1
      if: branch = auto
    - env: IMAGE=dist-powerpc-linux DEPLOY=1
      if: branch = auto
    - env: IMAGE=dist-powerpc64-linux DEPLOY=1
      if: branch = auto
    - env: IMAGE=dist-powerpc64le-linux DEPLOY=1
      if: branch = auto
    - env: IMAGE=dist-s390x-linux DEPLOY=1
      if: branch = auto
    - env: IMAGE=dist-x86_64-freebsd DEPLOY=1
      if: branch = auto
    - env: IMAGE=dist-x86_64-musl DEPLOY=1
      if: branch = auto
    - env: IMAGE=dist-x86_64-netbsd DEPLOY=1
      if: branch = auto
    - env: IMAGE=asmjs
      if: branch = auto
    - env: IMAGE=i686-gnu
      if: branch = auto
    - env: IMAGE=i686-gnu-nopt
      if: branch = auto
    - env: IMAGE=wasm32-unknown
      if: branch = auto
    - env: IMAGE=x86_64-gnu
      if: branch = auto
    - env: IMAGE=x86_64-gnu-full-bootstrap
      if: branch = auto
    - env: IMAGE=x86_64-gnu-aux
      if: branch = auto
    - env: IMAGE=x86_64-gnu-tools
      if: branch = auto OR (type = pull_request AND commit_message =~ /(?i:^update.*\b(rls|rustfmt|clippy|miri|cargo)\b)/)
    - env: IMAGE=x86_64-gnu-debug
      if: branch = auto
    - env: IMAGE=x86_64-gnu-nopt
      if: branch = auto
    - env: IMAGE=x86_64-gnu-distcheck
      if: branch = auto
    - env: IMAGE=mingw-check
      if: type = pull_request OR branch = auto

    - stage: publish toolstate
      if: branch = master AND type = push
      before_install: []
      install: []
      sudo: false
      script:
        MESSAGE_FILE=$(mktemp -t msg.XXXXXX);
        . src/ci/docker/x86_64-gnu-tools/repo.sh;
        commit_toolstate_change "$MESSAGE_FILE" "$TRAVIS_BUILD_DIR/src/tools/publish_toolstate.py" "$(git rev-parse HEAD)" "$(git log --format=%s -n1 HEAD)" "$MESSAGE_FILE" "$TOOLSTATE_REPO_ACCESS_TOKEN";

before_install:
  # We'll use the AWS cli to download/upload cached docker layers as well as
  # push our deployments, so download that here.
  - pip install --user awscli; export PATH=$PATH:$HOME/.local/bin:$HOME/Library/Python/2.7/bin/
  - mkdir -p $HOME/rustsrc
  # FIXME(#46924): these two commands are required to enable IPv6,
  # they shouldn't exist, please revert once more official solutions appeared.
  # see https://github.com/travis-ci/travis-ci/issues/8891#issuecomment-353403729
  - if [ "$TRAVIS_OS_NAME" = linux ]; then
      echo '{"ipv6":true,"fixed-cidr-v6":"fd9a:8454:6789:13f7::/64"}' | sudo tee /etc/docker/daemon.json;
      sudo service docker restart;
    fi

install:
  - case "$TRAVIS_OS_NAME" in
        linux)
          travis_retry curl -fo $HOME/stamp https://s3-us-west-1.amazonaws.com/rust-lang-ci2/rust-ci-mirror/2017-03-17-stamp-x86_64-unknown-linux-musl &&
            chmod +x $HOME/stamp &&
            export PATH=$PATH:$HOME
          ;;
        osx)
          if [[ "$RUST_CHECK_TARGET" == dist ]]; then
            travis_retry brew update &&
            travis_retry brew install xz &&
            travis_retry brew install swig;
          fi &&
          travis_retry curl -fo /usr/local/bin/sccache https://s3-us-west-1.amazonaws.com/rust-lang-ci2/rust-ci-mirror/2018-04-02-sccache-x86_64-apple-darwin &&
            chmod +x /usr/local/bin/sccache &&
          travis_retry curl -fo /usr/local/bin/stamp https://s3-us-west-1.amazonaws.com/rust-lang-ci2/rust-ci-mirror/2017-03-17-stamp-x86_64-apple-darwin &&
            chmod +x /usr/local/bin/stamp &&
          travis_retry curl -f http://releases.llvm.org/7.0.0/clang+llvm-7.0.0-x86_64-apple-darwin.tar.xz | tar xJf - &&
            export CC=`pwd`/clang+llvm-7.0.0-x86_64-apple-darwin/bin/clang &&
            export CXX=`pwd`/clang+llvm-7.0.0-x86_64-apple-darwin/bin/clang++ &&
            export AR=ar
          ;;
    esac

before_script:
  - >
      echo "#### Disk usage before running script:";
      df -h;
      du . | sort -nr | head -n100
  - >
      RUN_SCRIPT="src/ci/init_repo.sh . $HOME/rustsrc";
      if [ "$TRAVIS_OS_NAME" = "osx" ]; then
          export RUN_SCRIPT="$RUN_SCRIPT && src/ci/run.sh";
      else
          export RUN_SCRIPT="$RUN_SCRIPT && src/ci/docker/run.sh $IMAGE";
          # Enable core dump on Linux.
          sudo sh -c 'echo "/checkout/obj/cores/core.%p.%E" > /proc/sys/kernel/core_pattern';
      fi
  - >
      if [ "$IMAGE" = mingw-check ]; then
        # verify the publish_toolstate script works.
        git clone --depth=1 https://github.com/rust-lang-nursery/rust-toolstate.git;
        cd rust-toolstate;
        python2.7 "$TRAVIS_BUILD_DIR/src/tools/publish_toolstate.py" "$(git rev-parse HEAD)" "$(git log --format=%s -n1 HEAD)" "" "";
        cd ..;
        rm -rf rust-toolstate;
      fi

# Log time information from this machine and an external machine for insight into possible
# clock drift. Timezones don't matter since relative deltas give all the necessary info.
script:
  - >
      date && (curl -fs --head https://google.com | grep ^Date: | sed 's/Date: //g' || true)
  - stamp sh -x -c "$RUN_SCRIPT"
  - >
      date && (curl -fs --head https://google.com | grep ^Date: | sed 's/Date: //g' || true)

after_success:
  - >
      echo "#### Build successful; Disk usage after running script:";
      df -h;
      du . | sort -nr | head -n100
  - >
      if [ "$DEPLOY$DEPLOY_ALT" == "1" ]; then
        mkdir -p deploy/$TRAVIS_COMMIT;
        if [ "$TRAVIS_OS_NAME" == "osx" ]; then
            rm -rf build/dist/doc &&
            cp -r build/dist/* deploy/$TRAVIS_COMMIT;
        else
            rm -rf obj/build/dist/doc &&
            cp -r obj/build/dist/* deploy/$TRAVIS_COMMIT;
        fi;
        ls -la deploy/$TRAVIS_COMMIT;
        deploy_dir=rustc-builds;
        if [ "$DEPLOY_ALT" == "1" ]; then
            deploy_dir=rustc-builds-alt;
        fi;
        travis_retry aws s3 cp --no-progress --recursive --acl public-read ./deploy s3://rust-lang-ci2/$deploy_dir
      fi

after_failure:
  - >
      echo "#### Build failed; Disk usage after running script:";
      df -h;
      du . | sort -nr | head -n100

  # Random attempt at debugging currently. Just poking around in here to see if
  # anything shows up.

  # Dump backtrace for macOS
  - ls -lat $HOME/Library/Logs/DiagnosticReports/
  - find $HOME/Library/Logs/DiagnosticReports
      -type f
      -name '*.crash'
      -not -name '*.stage2-*.crash'
      -not -name 'com.apple.CoreSimulator.CoreSimulatorService-*.crash'
      -exec printf travis_fold":start:crashlog\n\033[31;1m%s\033[0m\n" {} \;
      -exec head -750 {} \;
      -exec echo travis_fold":"end:crashlog \; || true

  # Dump backtrace for Linux
  - ln -s . checkout &&
    for CORE in obj/cores/core.*; do
      EXE=$(echo $CORE | sed 's|obj/cores/core\.[0-9]*\.!checkout!\(.*\)|\1|;y|!|/|');
      if [ -f "$EXE" ]; then
        printf travis_fold":start:crashlog\n\033[31;1m%s\033[0m\n" "$CORE";
        gdb --batch -q -c "$CORE" "$EXE"
          -iex 'set auto-load off'
          -iex 'dir src/'
          -iex 'set sysroot .'
          -ex bt
          -ex q;
        echo travis_fold":"end:crashlog;
      fi;
    done || true

  # see #50887
  - cat ./obj/build/x86_64-unknown-linux-gnu/native/asan/build/lib/asan/clang_rt.asan-dynamic-i386.vers || true

  # attempt to debug anything killed by the oom killer on linux, just to see if
  # it happened
  - dmesg | grep -i kill

notifications:
  email: false