#
# This file defines the tasks and platforms for WiredTiger in the
# MongoDB continuous integration system (see https://mci.mongodb.com).
#

functions:
  "fetch source" :
    command: git.get_project
    params:
      directory: wiredtiger
  "fetch binaries" : &fetch_binaries
    - command: s3.get
      params:
        aws_key: ${aws_key}
        aws_secret: ${aws_secret}
        remote_file: wiredtiger/${build_variant}/${revision}/binaries/${build_id}.tgz
        bucket: build_external
        extract_to: wiredtiger

pre:
  - command: shell.exec
    params:
      script: |
        rm -rf "wiredtiger"
post:
  - command: archive.targz_pack
    params:
      target: "wiredtiger.tgz"
      source_dir: "wiredtiger"
      include:
        - "./**"
  - command: s3.put
    params:
      aws_secret: ${aws_secret}
      aws_key: ${aws_key}
      local_file: wiredtiger.tgz
      bucket: build_external
      permissions: public-read
      content_type: application/tar
      display_name: Artifacts
      remote_file: wiredtiger/${build_variant}/${revision}/artifacts/${build_id}.tgz
  - command: shell.exec
    params:
      script: |
        rm -rf "wiredtiger"

tasks:
## Base compile task on posix flavours
  - name: compile
    depends_on: []
    commands:
      - func: "fetch source"
      - command: git.apply_patch
        params:
          directory: wiredtiger
      - command: shell.exec
        params:
          working_dir: "wiredtiger"
          script: |
            set -o errexit
            set -o verbose
            if [ "Windows_NT" = "$OS" ]; then
              scons.bat --enable-python=c:\\swigwin-3.0.2\\swig.exe --enable-diagnostic ${smp_command|}
            else
              ./build_posix/reconf
              ${configure_env_vars|} ./configure --enable-diagnostic --enable-python --enable-zlib --enable-strict
              ${make_command|make} ${smp_command|} 2>&1

              # On macOS, change the binary location with install_name_tool since DYLD_LIBRARY_PATH
              # appears not to work for dynamic modules loaded by python. For wt, the libtool generated
              # script has the wrong path for running on test machines.
              if [ "$(uname -s)" == "Darwin" ]; then
                WT_VERSION=$(m4 build_posix/aclocal/version.m4)
                install_name_tool -change /usr/local/lib/libwiredtiger-$WT_VERSION.dylib $(pwd)/.libs/libwiredtiger-$WT_VERSION.dylib lang/python/_wiredtiger*.so
                install_name_tool -change /usr/local/lib/libwiredtiger-$WT_VERSION.dylib $(pwd)/.libs/libwiredtiger-$WT_VERSION.dylib .libs/wt
              fi

              ${test_env_vars|} TESTUTIL_ENABLE_LONG_TESTS=1 ${make_command|make} VERBOSE=1 check 2>&1
            fi
      - command: archive.targz_pack
        params:
          target: "wiredtiger.tgz"
          source_dir: "wiredtiger"
          include:
             - "./**"
      - command: s3.put
        params:
          aws_secret: ${aws_secret}
          aws_key: ${aws_key}
          local_file: wiredtiger.tgz
          bucket: build_external
          permissions: public-read
          content_type: application/tar
          display_name: Binaries
          remote_file: wiredtiger/${build_variant}/${revision}/binaries/${build_id}.tgz

  - name: unit-test
    depends_on:
    - name: compile
    commands:
      - func: "fetch binaries"
      - command: shell.exec
        params:
          working_dir: "wiredtiger"
          script: |
            set -o errexit
            set -o verbose

            # Avoid /usr/bin/python, at least on macOS: with System Integrity
            # Protection enabled, it ignores DYLD_LIBRARY_PATH and hence
            # doesn't find the WiredTiger library in the local tree.
            ${test_env_vars|} python ./test/suite/run.py -v 2 ${smp_command|} 2>&1

  - name: compile-windows-alt
    depends_on:
    - name: compile
    commands:
      - func: "fetch binaries"
      - command: shell.exec
        params:
          working_dir: "wiredtiger"
          script: |
            set -o errexit
            set -o verbose

            scons.bat ${smp_command|} "CFLAGS=/Gv /wd4090 /wd4996 /we4047 /we4024 /TC /we4100 /we4133" wiredtiger.dll libwiredtiger.lib

  - name: fops
    depends_on:
    - name: compile
    commands:
      - func: "fetch binaries"
      - command: shell.exec
        params:
          working_dir: "wiredtiger"
          script: |
            set -o errexit
            set -o verbose
            if [ "Windows_NT" = "$OS" ]; then
              cmd.exe /c t_fops.exe
            else
              ./test/fops/t
            fi

  - name: format
    depends_on:
    - name: compile
    commands:
      - func: "fetch binaries"
      - command: shell.exec
        params:
          working_dir: "wiredtiger"
          script: |
            set -o errexit
            set -o verbose
            # format assumes we run it from the format directory
            cmd.exe /c "cd test\\format && ..\\..\\t_format.exe reverse=0 encryption=none logging_compression=none runs=20"

buildvariants:
- name: ubuntu1404
  display_name: Ubuntu 14.04
  run_on:
  - ubuntu1404-test
  expansions:
    # It's ugly, but we need the absolute path here, not the relative
    test_env_vars: LD_LIBRARY_PATH=`pwd`/.libs
    smp_command: -j $(grep -c ^processor /proc/cpuinfo)
    configure_env_vars: CC=/opt/mongodbtoolchain/bin/gcc CXX=/opt/mongodbtoolchain/bin/g++
  tasks:
    - name: compile
    - name: unit-test
    - name: fops

- name: windows-64
  display_name: Windows 64-bit
  run_on:
  - windows-64-vs2013-test
  tasks:
    - name: compile
    - name: compile-windows-alt
    - name: unit-test
    #- name: format  - Enable when we have a solution for hangs and crashses
    - name: fops

- name: macos-1012
  display_name: OS X 10.12
  run_on:
  - macos-1012
  expansions:
    smp_command: -j $(sysctl -n hw.logicalcpu)
    configure_env_vars: PATH=/opt/mongodbtoolchain/v2/bin:$PATH
    make_command: PATH=/opt/mongodbtoolchain/v2/bin:$PATH ARCHFLAGS=-Wno-error=unused-command-line-argument-hard-error-in-future make
    test_env_vars: PATH=/opt/mongodbtoolchain/v2/bin:$PATH DYLD_LIBRARY_PATH=`pwd`/.libs
  tasks:
    - name: compile
    - name: unit-test
    - name: fops

