version: 2

references:
  container_config: &container_config
    docker:
      - image: "debian:stretch"
        working_directory: /root/build

  restore_repo: &restore_repo
    restore_cache:
      keys:
        - source-repo-{{ .Branch }}-{{ .Revision }}
        - source-repo-{{ .Branch }}
        - source-repo

  cmake_configure: &cmake_configure
    run:
      name: Creating Build Files
      command: |
        mkdir build
        cd build

        CXXFLAGS="-m64 -mtune=generic -mfpmath=sse -msse -msse2 -pipe -Wno-unknown-pragmas"
        CFLAGS="-m64 -mtune=generic -mfpmath=sse -msse -msse2 -pipe -Wno-unknown-pragmas"

        if [[ "$CC" =~ ^clang.*$ ]]; then
            CXXFLAGS="$CXXFLAGS -stdlib=libc++"
        fi

        eval $CC --version

        export CXXFLAGS
        export CFLAGS
        CMAKE="cmake -G Ninja -DFSO_FATAL_WARNINGS=ON $CMAKE_OPTIONS"
        eval $CMAKE -DCMAKE_BUILD_TYPE=$CONFIGURATION -DFSO_BUILD_TESTS=ON -DFSO_BUILD_INCLUDED_LIBS=ON ..

  compile: &compile
    run:
      name: Compiling Source code
      command: |
        cd build
        ninja -k 20 all

  run_tests: &run_tests
    run:
      name: Run unit tests
      command: |
        ./build/bin/unittests --gtest_shuffle

  compile_steps: &compile_steps
    steps:
      - *restore_repo
      - *cmake_configure
      - *compile
      - *run_tests
jobs:
  checkout_code:
    <<: *container_config
    steps:
      - run:
          name: Installing Git
          command: 'apt-get update && apt-get -yq install git'
      - *restore_repo
      - checkout
      - run:
          name: Updating submodules
          command: 'git submodule update --init --recursive'
      - save_cache:
          key: source-repo-{{ .Branch }}-{{ .Revision }}
          paths:
            - .

  build_gcc_lowest_debug:
    docker:
      - image: asarium/fso_compile:gcc-lowest
        working_directory: /root/build
    environment:
      CC: gcc
      CXX: g++
      CONFIGURATION: Debug
    <<: *compile_steps

  build_gcc_lowest_release:
    docker:
      - image: asarium/fso_compile:gcc-lowest
        working_directory: /root/build
    environment:
      CC: gcc
      CXX: g++
      CONFIGURATION: Release
    <<: *compile_steps

  build_gcc_latest_debug:
    docker:
      - image: asarium/fso_compile:gcc-latest
        working_directory: /root/build
    environment:
      CC: gcc
      CXX: g++
      CONFIGURATION: Debug
    <<: *compile_steps

  build_gcc_latest_release:
    docker:
      - image: asarium/fso_compile:gcc-latest
        working_directory: /root/build
    environment:
      CC: gcc
      CXX: g++
      CONFIGURATION: Release
    <<: *compile_steps

  build_clang_latest_debug:
    docker:
      - image: asarium/fso_compile:clang-latest
        working_directory: /root/build
    environment:
      CC: clang-8
      CXX: clang++-8
      CONFIGURATION: Debug
    <<: *compile_steps

  build_clang_latest_release:
    docker:
      - image: asarium/fso_compile:clang-latest
        working_directory: /root/build
    environment:
      CC: clang-8
      CXX: clang++-8
      CONFIGURATION: Release
    <<: *compile_steps

  build_clang_lowest_debug:
    docker:
      - image: asarium/fso_compile:clang-lowest
        working_directory: /root/build
    environment:
      CC: clang
      CXX: clang++
      CONFIGURATION: Debug
    <<: *compile_steps

  build_clang_lowest_release:
    docker:
      - image: asarium/fso_compile:clang-lowest
        working_directory: /root/build
    environment:
      CC: clang
      CXX: clang++
      CONFIGURATION: Release
    <<: *compile_steps

workflows:
  version: 2

  build_test:
    jobs:
      - checkout_code
      - build_gcc_lowest_debug:
          requires:
            - checkout_code
      - build_gcc_lowest_release:
          requires:
            - checkout_code
      - build_gcc_latest_debug:
          requires:
            - checkout_code
      - build_gcc_latest_release:
          requires:
            - checkout_code
      - build_clang_latest_debug:
          requires:
            - checkout_code
      - build_clang_latest_release:
          requires:
            - checkout_code
      - build_clang_lowest_debug:
          requires:
            - checkout_code
      - build_clang_lowest_release:
          requires:
            - checkout_code