# Requires the BETA setting to use Ubunty 14.04 Trusty

# It appears that the current directory is reset at the start of each `-` line,
# so for example to do an out-of-source build, you have to chain operations
# after `cd`: `cd build && cmake .. `.

machine:
  environment:
    PATH: "~/$CIRCLE_PROJECT_REPONAME/ldc2-1.0.0-linux-x86_64/bin:$PATH"
dependencies:
  cache_directories:
    - ldc2-1.0.0-linux-x86_64
    #- clang+llvm-3.8.0-x86_64-linux-gnu-ubuntu-14.04
  pre:
    # LLVM's official APT repo:
    - sudo add-apt-repository -y 'deb http://apt.llvm.org/trusty/ llvm-toolchain-trusty main'
    - wget -O - http://apt.llvm.org/llvm-snapshot.gpg.key|sudo apt-key add -
    - sudo apt-get update

    # Download and cache a prebuilt LLVM package:
    #- if [[ ! -e clang+llvm-3.8.0-x86_64-linux-gnu-ubuntu-14.04 ]]; then wget http://llvm.org/releases/3.8.0/clang+llvm-3.8.0-x86_64-linux-gnu-ubuntu-14.04.tar.xz && xzcat clang+llvm-3.8.0-x86_64-linux-gnu-ubuntu-14.04.tar.xz | tar -xvf - ; fi

    # Download and cache a prebuilt LDC package:
    - if [[ ! -e ldc2-1.0.0-linux-x86_64/bin/ldc2 ]]; then wget https://github.com/ldc-developers/ldc/releases/download/v1.0.0/ldc2-1.0.0-linux-x86_64.tar.xz && xzcat ldc2-1.0.0-linux-x86_64.tar.xz | tar -xvf - ; fi

  override:
    - sudo apt-get remove clang llvm
    - sudo apt-get install libconfig++8-dev libedit-dev
    - sudo apt-get install llvm-4.0 llvm-4.0-dev clang-4.0
    - pip install --user lit
  post:
    - sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-4.0 99
    - sudo update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-4.0 99
    #- sudo update-alternatives --install /usr/bin/clang clang ~/$CIRCLE_PROJECT_REPONAME/clang+llvm-3.8.0-x86_64-linux-gnu-ubuntu-14.04/bin/clang 99
    #- sudo update-alternatives --install /usr/bin/clang++ clang++ ~/$CIRCLE_PROJECT_REPONAME/clang+llvm-3.8.0-x86_64-linux-gnu-ubuntu-14.04/bin/clang++ 99
    - gcc --version
    - clang --version
    - ldc2 -version
    - cmake --version
    - python -c "import lit; lit.main();" --version | head -n 1
    - gdb --version

checkout:
  post:
    - git submodule sync
    - git submodule update --init

test:
  pre:
    # Build first with a released LDC
    - mkdir build
    - cd build && export CC=clang && export CXX=clang++ && cmake .. && make -j3 && bin/ldc2 -version || exit 1

    # Now build LDC with itself and use this second build for further testing
    - CC=clang CXX=clang++ cmake -DD_COMPILER="build/bin/ldmd2" .
    - make -j3
    - bin/ldc2 -version || exit 1

  override:
    - make -j2 phobos2-ldc-unittest-debug phobos2-ldc-unittest
    - make -j3 druntime-ldc-unittest-debug druntime-ldc-unittest
    - CC="" DMD_TESTSUITE_MAKE_ARGS=-j3 ctest --verbose -R "dmd-testsuite"
    - ctest --output-on-failure -V -R "lit-tests"
    - ctest -j3 --output-on-failure -E "dmd-testsuite|lit-tests"

# To add more value the test results could be collected, see https://circleci.com/docs/test-metadata
# A way how to convert the ctest output is described here:
# http://stackoverflow.com/questions/6329215/how-to-get-ctest-results-in-hudson-jenkins

# The tests could be run in 4 parallel containers. The syntax is shown below.
# The containers were started but only the tests from container 0 were run.
# Containers 1-3 also started the tests but stop with the message
# "No tests were found!!!"
#   
#    - ? |
#        echo $CIRCLE_NODE_INDEX
#        case $CIRCLE_NODE_INDEX in
#          0)
#            MAKEOPTS=-j8 ctest -j4 --verbose -R "build-phobos2-ldc-unittest"
#            ;;
#          1)
#            MAKEOPTS=-j8 ctest -j4 --verbose -R "build-druntime-ldc-unittest"
#            ;;
#          2)
#            CC="" MAKEOPTS=-j8 ctest --verbose -R "dmd-testsuite"
#            ;;
#          3)
#            MAKEOPTS=-j8 ctest --verbose -R "-test-runner"
#            ;;
#        esac
#      :
#        parallel: true
#    - ctest -j8 --output-on-failure -E "testsuite"

