variables:
  DEBIAN_FRONTEND: noninteractive

stages:
  - build
  - unit_test
  - deploy

#-------------------------------------------------------------------------------
# Windows
#-------------------------------------------------------------------------------

build windows/x64:
  stage: build
  script:
    - CMD.EXE /C ci\build-windows64.bat
  artifacts:
    when: always
    paths:
      - build
    expire_in: 3 days
  tags:
    - libicsneo-win-x64

unit_test windows/x64:
  stage: unit_test
  script:
    - build\libicsneo-unit-tests.exe
  dependencies:
    - build windows/x64
  needs:
    - build windows/x64
  tags:
    - libicsneo-win-x64
  timeout: 5m

build windows/x86:
  stage: build
  script:
    - CMD.EXE /C ci\build-windows32.bat
  artifacts:
    when: always
    paths:
      - build
    expire_in: 3 days
  tags:
    - libicsneo-win-x64

unit_test windows/x86:
  stage: unit_test
  script:
    - build\libicsneo-unit-tests.exe
  dependencies:
    - build windows/x86
  needs:
    - build windows/x86
  tags:
    - libicsneo-win-x64
  timeout: 5m

#-------------------------------------------------------------------------------
# Ubuntu
#-------------------------------------------------------------------------------

.build_linux_ubuntu_gcc: &build_linux_ubuntu_gcc
  stage: build
  script:
    - apt update -y
    - apt upgrade -y
    - apt install -y g++ ninja-build cmake libpcap-dev git
    - sh ci/build-posix.sh
  artifacts:
    when: always
    paths:
      - build
    expire_in: 3 days
  tags:
    - linux-build

.test_linux_ubuntu_gcc: &test_linux_ubuntu_gcc
  stage: unit_test
  script:
    - apt update -y
    - apt upgrade -y
    - apt install -y libpcap-dev
    - build/libicsneo-unit-tests
  tags:
    - linux-build
  timeout: 5m

.build_linux_ubuntu_clang: &build_linux_ubuntu_clang
  stage: build
  script:
    - apt update -y
    - apt upgrade -y
    - apt install -y clang lld ninja-build cmake libpcap-dev git
    - CC=clang CXX=clang++ LDFLAGS=-fuse-ld=lld sh ci/build-posix.sh
  artifacts:
    when: always
    paths:
      - build
    expire_in: 3 days
  tags:
    - linux-build

.test_linux_ubuntu_clang: &test_linux_ubuntu_clang
  stage: unit_test
  script:
    - apt update -y
    - apt upgrade -y
    - apt install -y libpcap-dev
    - build/libicsneo-unit-tests
  tags:
    - linux-build
  timeout: 5m

build linux/ubuntu/2204/amd64/gcc:
  <<: *build_linux_ubuntu_gcc
  image: ubuntu:22.04

unit_test linux/ubuntu/2204/amd64/gcc:
  <<: *test_linux_ubuntu_gcc
  image: ubuntu:22.04
  dependencies:
    - build linux/ubuntu/2204/amd64/gcc
  needs:
    - build linux/ubuntu/2204/amd64/gcc

build linux/ubuntu/2204/amd64/clang:
  <<: *build_linux_ubuntu_clang
  image: ubuntu:22.04

unit_test linux/ubuntu/2204/amd64/clang:
  <<: *test_linux_ubuntu_clang
  image: ubuntu:22.04
  dependencies:
    - build linux/ubuntu/2204/amd64/clang
  needs:
    - build linux/ubuntu/2204/amd64/clang

build linux/ubuntu/2404/amd64/gcc:
  <<: *build_linux_ubuntu_gcc
  image: ubuntu:24.04

unit_test linux/ubuntu/2404/amd64/gcc:
  <<: *test_linux_ubuntu_gcc
  image: ubuntu:24.04
  dependencies:
    - build linux/ubuntu/2404/amd64/gcc
  needs:
    - build linux/ubuntu/2404/amd64/gcc

build linux/ubuntu/2404/amd64/clang:
  <<: *build_linux_ubuntu_clang
  image: ubuntu:24.04

unit_test linux/ubuntu/2404/amd64/clang:
  <<: *test_linux_ubuntu_clang
  image: ubuntu:24.04
  dependencies:
    - build linux/ubuntu/2404/amd64/clang
  needs:
    - build linux/ubuntu/2404/amd64/clang

#-------------------------------------------------------------------------------
# Fedora
#-------------------------------------------------------------------------------

.build_linux_fedora_gcc: &build_linux_fedora_gcc
  stage: build
  cache:
    paths:
      - /var/cache/dnf
  script:
    - echo max_parallel_downloads=10 >>/etc/dnf/dnf.conf
    - echo fastestmirror=True >>/etc/dnf/dnf.conf
    - dnf upgrade -y
    - dnf install -y g++ libpcap-devel cmake ninja-build git
    - sh ci/build-posix.sh
  artifacts:
    when: always
    paths:
      - build
    expire_in: 3 days
  tags:
    - linux-build

.test_linux_fedora_gcc: &test_linux_fedora_gcc
  stage: unit_test
  cache:
    paths:
      - /var/cache/dnf
  script:
    - echo max_parallel_downloads=10 >>/etc/dnf/dnf.conf
    - echo fastestmirror=True >>/etc/dnf/dnf.conf
    - dnf upgrade -y
    - dnf install -y libpcap-devel
    - build/libicsneo-unit-tests
  tags:
    - linux-build
  timeout: 5m

.build_linux_fedora_clang: &build_linux_fedora_clang
  stage: build
  cache:
    paths:
      - /var/cache/dnf
  script:
    - echo max_parallel_downloads=10 >>/etc/dnf/dnf.conf
    - echo fastestmirror=True >>/etc/dnf/dnf.conf
    - dnf upgrade -y
    - dnf install -y clang lld libpcap-devel cmake ninja-build git
    - CC=clang CXX=clang++ LDFLAGS=-fuse-ld=lld sh ci/build-posix.sh
  artifacts:
    when: always
    paths:
      - build
    expire_in: 3 days
  tags:
    - linux-build

.test_linux_fedora_clang: &test_linux_fedora_clang
  stage: unit_test
  cache:
    paths:
      - /var/cache/dnf
  script:
    - echo max_parallel_downloads=10 >>/etc/dnf/dnf.conf
    - echo fastestmirror=True >>/etc/dnf/dnf.conf
    - dnf upgrade -y
    - dnf install -y libpcap-devel
    - build/libicsneo-unit-tests
  tags:
    - linux-build
  timeout: 5m

build linux/fedora/41/amd64/gcc:
  <<: *build_linux_fedora_gcc
  image: fedora:41

unit_test linux/fedora/41/amd64/gcc:
  <<: *test_linux_fedora_gcc
  image: fedora:41
  dependencies:
    - build linux/fedora/41/amd64/gcc
  needs:
    - build linux/fedora/41/amd64/gcc

build linux/fedora/41/amd64/clang:
  <<: *build_linux_fedora_clang
  image: fedora:41

unit_test linux/fedora/41/amd64/clang:
  <<: *test_linux_fedora_clang
  image: fedora:41
  dependencies:
    - build linux/fedora/41/amd64/clang
  needs:
    - build linux/fedora/41/amd64/clang

build linux/fedora/42/amd64/gcc:
  <<: *build_linux_fedora_gcc
  image: fedora:42

unit_test linux/fedora/42/amd64/gcc:
  <<: *test_linux_fedora_gcc
  image: fedora:42
  dependencies:
    - build linux/fedora/42/amd64/gcc
  needs:
    - build linux/fedora/42/amd64/gcc

build linux/fedora/42/amd64/clang:
  <<: *build_linux_fedora_clang
  image: fedora:42

unit_test linux/fedora/42/amd64/clang:
  <<: *test_linux_fedora_clang
  image: fedora:42
  dependencies:
    - build linux/fedora/42/amd64/clang
  needs:
    - build linux/fedora/42/amd64/clang

#-------------------------------------------------------------------------------
# Python Module
#-------------------------------------------------------------------------------

build python/linux/amd64:
  stage: build
  tags:
    - linux-build
  image: python:3.13
  services:
    - name: docker:dind
      entrypoint: ["env", "-u", "DOCKER_HOST"]
      command: ["dockerd-entrypoint.sh"]
  variables:
    CIBW_BEFORE_ALL: yum install -y flex && sh ci/bootstrap-libpcap.sh
    CIBW_BUILD: "*manylinux*" # no musl
    CIBW_ARCHS: x86_64
    DOCKER_HOST: unix:///var/run/docker.sock
    DOCKER_DRIVER: overlay2
    DOCKER_TLS_CERTDIR: ""
    CIBW_ENVIRONMENT: CMAKE_PREFIX_PATH=/project/libpcap/install
  script:
    - curl -sSL https://get.docker.com/ | sh
    - sh ci/build-wheel-posix.sh
  artifacts:
    paths:
      - wheelhouse

build python/linux/arm64:
  stage: build
  tags:
    - arm64-linux-build
  variables:
    CIBW_BEFORE_ALL: yum install -y flex && sh ci/bootstrap-libpcap.sh
    CIBW_BUILD: "*manylinux*" # no musl
    CIBW_ARCHS: aarch64
    CIBW_ENVIRONMENT: CMAKE_PREFIX_PATH=/project/libpcap/install
  script:
    - sh ci/build-wheel-posix.sh
  artifacts:
    paths:
      - wheelhouse

build python/macos:
  stage: build
  tags:
    - macos-arm64
  variables:
    CIBW_BEFORE_ALL: sh ci/bootstrap-libpcap.sh
    CIBW_ARCHS: arm64
    CIBW_ENVIRONMENT: CMAKE_PREFIX_PATH=$CI_PROJECT_DIR/libpcap/install
    MACOSX_DEPLOYMENT_TARGET: 10.14
  script:
    - sh ci/build-wheel-posix.sh
  artifacts:
    paths:
      - wheelhouse

build python/windows:
  stage: build
  tags:
    - libicsneo-win-x64
  variables:
    CIBW_ARCHS: AMD64
    CIBW_ENVIRONMENT: CMAKE_GENERATOR=Ninja
  script:
    - cmd /c ci\build-wheel-windows.bat
  artifacts:
    paths:
      - wheelhouse

deploy python/pypi:
  stage: deploy
  variables:
    TWINE_USERNAME: __token__
    TWINE_PASSWORD: $PYPI_TOKEN
  tags:
    - linux-build
  image: python:3.13
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
  script:
    - python3 -m pip install -U twine
    - twine upload wheelhouse/*
  dependencies:
    - build python/linux/amd64
    - build python/linux/arm64
    - build python/macos
    - build python/windows
  needs:
    - build python/linux/amd64
    - build python/linux/arm64
    - build python/macos
    - build python/windows

push github:
  stage: deploy
  tags:
    - linux-build
  image: alpine:latest
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
      when: delayed
      start_in: 10 minutes
  dependencies:
    - deploy python/pypi
  needs:
    - deploy python/pypi
  script:
    - apk add git
    - git push https://$LIBICSNEO_GITHUB_USERNAME:$LIBICSNEO_GITHUB_TOKEN@github.com/intrepidcs/libicsneo.git HEAD:master
