# CI script for building Wine

.wine-build:
  stage: build
  image: $CI_REGISTRY/wine/wine:debian-bookworm
  interruptible: true
  variables:
    GIT_DEPTH: 0
  cache:
    - paths:
        - ccache/
    - key:
        files:
          - configure.ac
      paths:
        - build32/config.cache
        - build64/config.cache
  before_script:
    - export BASEDIR="$PWD"
    - export CCACHE_BASEDIR="$BASEDIR"
    - export CCACHE_DIR="$BASEDIR/ccache"
    - export CCACHE_COMPILERCHECK=content
    - export PATH="/usr/lib/ccache:$PATH"
    - git config --global --add safe.directory $CI_PROJECT_DIR
    - git fetch --tags
    - mkdir -p build32 build64
    - rm -fr .git/rebase-merge  # in case a previous CI run failed in git rebase

build-linux:
  extends: .wine-build
  rules:
    - if: $CI_PIPELINE_SOURCE == 'merge_request_event'
  artifacts:
    expire_in: 1 day
    paths:
      - usr/local/
  script:
    - git rebase $CI_MERGE_REQUEST_DIFF_BASE_SHA --exec ./tools/gitlab/build-linux
    - git diff --name-only $CI_MERGE_REQUEST_DIFF_BASE_SHA | sed -re '/\/tests\//!d; s@/tests/.*@/tests/Makefile.in@' |
      (xargs -r ls 2>/dev/null || true) | xargs -r sed '/TESTDLL/!d; s@.dll@@; s@.*= *@@' >usr/local/share/wine/winetest.args

build-clang:
  extends: .wine-build
  rules:
    - if: $CI_PIPELINE_SOURCE == 'merge_request_event'
  script:
    - ./tools/gitlab/build-clang

build-mac:
  extends: .wine-build
  rules:
    - if: $CI_PIPELINE_SOURCE == 'merge_request_event'
  tags:
    - mac
  artifacts:
    when: on_failure
    paths:
      - build64/config.log
      - build32/config.log
  script:
    - export PATH="/usr/local/opt/ccache/libexec:$PATH"
    - git rebase $CI_MERGE_REQUEST_DIFF_BASE_SHA --exec ./tools/gitlab/build-mac

build-winetest:
  stage: build
  image: $CI_REGISTRY/wine/wine:debian-bookworm
  rules:
    - if: $CI_PIPELINE_SOURCE == 'merge_request_event'
  needs:
    - job: build-linux
  variables:
    GIT_STRATEGY: none
  artifacts:
    name: winetest
    paths:
      - winetest.args
      - winetest.exe
      - winetest64.exe
  script:
    - mv usr/local/share/wine/winetest.args winetest.args
    - mv usr/local/lib/wine/i386-windows/winetest.exe winetest.exe
    - mv usr/local/lib/wine/x86_64-windows/winetest.exe winetest64.exe

build-daily-linux:
  extends: .wine-build
  rules:
    - if: $CI_PIPELINE_SOURCE == 'trigger' && $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
  artifacts:
    expire_in: 1 day
    paths:
      - usr/local/
  script:
    - ./tools/gitlab/build-linux

build-daily-winetest:
  stage: build
  image: $CI_REGISTRY/wine/wine:debian-bookworm
  rules:
    - if: $CI_PIPELINE_SOURCE == 'trigger' && $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
  needs:
    - job: build-daily-linux
  variables:
    GIT_STRATEGY: none
  artifacts:
    name: winetest
    paths:
      - winetest.exe
      - winetest64.exe
  script:
    - mv usr/local/lib/wine/i386-windows/winetest.exe winetest.exe
    - mv usr/local/lib/wine/x86_64-windows/winetest.exe winetest64.exe
