version: 2.1
jobs:
  gcc:
    resource_class: large
    docker:
      - image: debian:testing-slim
        auth:
          username: $DOCKERHUB_USERNAME
          password: $DOCKERHUB_PASSWORD
    environment:
      CFLAGS: -Wextra -Werror -march=native
      CXXFLAGS: -Wextra -Werror -march=native
      CC: gcc
      CXX: g++
    steps:
      - run: apt-get update && apt-get install -y git ssh ninja-build pipx python3-setuptools python3-wheel gcovr gcc g++
      - checkout
      - run: cat /proc/cpuinfo /proc/meminfo
      - run: pipx install meson==0.55.0
      - run: /root/.local/bin/meson setup build -Db_coverage=true
      - run: ninja -C build -v -j 5
      - run: ninja -C build -v test

  clang:
    resource_class: large
    docker:
      - image: debian:testing-slim
        auth:
          username: $DOCKERHUB_USERNAME
          password: $DOCKERHUB_PASSWORD
    environment:
      CFLAGS: -Weverything -Werror -march=native
      CXXFLAGS: -Weverything -Werror -march=native
    steps:
      - run: apt-get update && apt-get install -y git ssh ninja-build pipx python3-setuptools python3-wheel gcovr clang
      - checkout
      - run: cat /proc/cpuinfo /proc/meminfo
      - run: pipx install meson==0.55.0
      - run: CFLAGS="-Wno-unsafe-buffer-usage" CXXFLAGS="-Wno-unsafe-buffer-usage" CC=clang CXX=clang++ /root/.local/bin/meson setup build -Db_coverage=true
      - run: ninja -C build -v -j 5
      - run: ninja -C build -v test

  loongson:
    resource_class: large
    docker:
      - image: debian:testing-slim
        auth:
          username: $DOCKERHUB_USERNAME
          password: $DOCKERHUB_PASSWORD
    environment:
      CFLAGS: -Wextra -Werror
      CXXFLAGS: -Wextra -Werror
    steps:
      - run: apt-get update && apt-get install -y git ssh ninja-build pipx python3-setuptools python3-wheel gcovr gcc-mips64el-linux-gnuabi64 g++-mips64el-linux-gnuabi64 qemu-user-static libc6-mips64el-cross ccache build-essential
      - checkout
      - run: cat /proc/cpuinfo /proc/meminfo
      - run:
          command: |
            ccache --set-config=max_size='500M'
            ccache --set-config=compression=true
            ccache -p
            ccache -z
      - run: pipx install meson==0.55.0
      - run: /root/.local/bin/meson setup build -Db_coverage=true --cross-file=docker/cross-files/loongson-gcc-ccache.cross
      - restore_cache:
          keys:
            - v1-loongson-ccache-{{ .Branch }}-
            - v1-loongson-ccache-
      - run: ninja -C build -v -j 5
      - run: /usr/bin/ccache -s
      - save_cache:
          key: v1-loongson-ccache-{{ .Branch }}-{{ .Revision }}
          paths: [ "/root/.cache/ccache" ]
      # - run: ninja -C build -v -j 3 test

  i686:
    resource_class: large
    docker:
      - image: debian:testing-slim
        auth:
          username: $DOCKERHUB_USERNAME
          password: $DOCKERHUB_PASSWORD
    steps:
      - run: apt-get update && apt-get install -y git ssh ninja-build pipx python3-setuptools python3-wheel gcovr {gcc,g++}-i686-linux-gnu libc6-i386-cross qemu-user-static ccache
      - checkout
      - run:
          name: System Information
          command: cat /proc/cpuinfo /proc/meminfo
      - run: dpkg --add-architecture i386
      - run: pipx install meson==0.55.0
      - run: /root/.local/bin/meson setup build -Db_coverage=true --cross-file docker/cross-files/i686-gcc-qemu.cross
      - restore_cache:
          keys:
            - v1-i686-ccache-{{ .Branch }}-
            - v1-i686-ccache-
      - run:
          command: |
            ccache --set-config=max_size='500M'
            ccache --set-config=compression=true
            ccache -p
            ccache -z
      - run: /root/.local/bin/meson compile -C build --verbose -j 5
      - run: /usr/bin/ccache -s
      - save_cache:
          key: v1-i686-ccache-{{ .Branch }}-{{ .Revision }}
          paths: [ "/root/.cache/ccache" ]
      - run: /root/.local/bin/meson test -C build --num-processes 5 --print-errorlogs

  i686-gcc11-O2:
    resource_class: large
    docker:
      - image: debian:testing-slim
        auth:
          username: $DOCKERHUB_USERNAME
          password: $DOCKERHUB_PASSWORD
    steps:
      - run: apt-get update && apt-get install -y git ssh ninja-build pipx python3-setuptools python3-wheel gcovr {gcc,g++}-11-i686-linux-gnu libc6-i386-cross qemu-user-static ccache build-essential
      - checkout
      - run:
          name: System Information
          command: cat /proc/cpuinfo /proc/meminfo
      - run: dpkg --add-architecture i386
      - run: pipx install meson==0.55.0
      - run: /root/.local/bin/meson setup build -Db_coverage=true --cross-file docker/cross-files/i686-gcc-11-qemu.cross
      - restore_cache:
          keys:
            - v1-i686-gcc11-O2-ccache-{{ .Branch }}-
            - v1-i686-gcc11-O2-ccache-
      - run:
          command: |
            ccache --set-config=max_size='500M'
            ccache --set-config=compression=true
            ccache -p
            ccache -z
      - run: /root/.local/bin/meson compile -C build --verbose -j 5
      - run: /usr/bin/ccache -s
      - save_cache:
          key: v1-i686-gcc11-O2-ccache-{{ .Branch }}-{{ .Revision }}
          paths: [ "/root/.cache/ccache" ]
      - run: /root/.local/bin/meson test -C build --num-processes 5 --print-errorlogs


workflows:
  version: 2
  test:
    jobs:
      - gcc:
          filters:
            branches:
              ignore:
              - master
              - /^ci/(?!circleci).*$/
      - clang:
          filters:
            branches:
              ignore:
              - master
              - /^ci/(?!circleci).*$/
      - loongson:
          filters:
            branches:
              ignore:
              - master
              - /^ci/(?!circleci).*$/
      # - i686:
      #     filters:
      #       branches:
      #         ignore:
      #         - master
      #         - /^ci/(?!circleci).*$/
      - i686-gcc11-O2:
          filters:
            branches:
              ignore:
              - master
              - /^ci/(?!circleci).*$/
