image: $CI_SERVER_HOST:5050/libretro-infrastructure/libretro-build-amd64-ubuntu:latest

stages:
  - build-prepare
  - build
  - prepare-for-static-cores
  - trigger-static-cores

build-retroarch-windows-x64:
  stage: build
  before_script:
    - export NUMPROC=$(($(nproc)/3))
  artifacts:
    paths:
    - retroarch.exe
    expire_in: 1 month
  dependencies: []
  script:
    - "./configure --host=x86_64-w64-mingw32"
    - "make -j$NUMPROC"
    - "mv retroarch retroarch.exe"

build-retroarch-linux-x64:
  stage: build
  before_script:
    - export NUMPROC=$(($(nproc)/3))
  artifacts:
    paths:
    - retroarch
    expire_in: 1 month
  dependencies: []
  script:
    - "./configure"
    - "make -j$NUMPROC"

build-retroarch-dingux-mips32:
  image: $CI_SERVER_HOST:5050/libretro-infrastructure/libretro-build-dingux:latest
  stage: build
  before_script:
    - export NUMPROC=$(($(nproc)/3))
  artifacts:
    paths:
    - retroarch_rg350.opk
    expire_in: 1 month
  dependencies: []
  script:
    - "make -j$NUMPROC -f Makefile.rg350"

build-static-retroarch-libnx-aarch64:
  image: $CI_SERVER_HOST:5050/libretro-infrastructure/libretro-build-libnx-devkitpro:latest
  stage: prepare-for-static-cores
  before_script:
    - export NUMPROC=$(($(nproc)/3))
  artifacts:
    paths:
    -  retroarch-precompiled/
    expire_in: 1 day
  dependencies: []
  needs:
    # Static dummy builds without a core so its a good check if it properly builds
    - build-static-retroarch-dummy-libnx-aarch64
  script:
    # Allow failure since we don't have a core
    - "make -f Makefile.libnx -j$NUMPROC ||:"
    - "mkdir .retroarch-precompiled"
    - "cp -r ./* .retroarch-precompiled/"
    - "mv .retroarch-precompiled/ retroarch-precompiled/"
    
build-static-retroarch-dummy-libnx-aarch64:
  image: $CI_SERVER_HOST:5050/libretro-infrastructure/libretro-build-libnx-devkitpro:latest
  stage: build
  before_script:
    - export NUMPROC=$(($(nproc)/3))
  artifacts:
    paths:
    -  retroarch_switch.nro
    -  retroarch_switch.elf
    expire_in: 1 month
  dependencies: []
  script:
    - "make -f Makefile.libnx -j$NUMPROC HAVE_STATIC_DUMMY=1"

build-static-retroarch-vita:
  image: $CI_SERVER_HOST:5050/libretro-infrastructure/libretro-build-vita:latest
  stage: prepare-for-static-cores
  before_script:
    - export NUMPROC=$(($(nproc)/3))
  artifacts:
    paths:
    -  retroarch-precompiled/
    expire_in: 1 day
  dependencies: []
  script:
    # Allow failure since we don't have a core
    - "make -f Makefile.vita -j$NUMPROC ||:"
    - "mkdir .retroarch-precompiled"
    - "cp -r ./* .retroarch-precompiled/"
    - "mv .retroarch-precompiled/ retroarch-precompiled/"

build-static-retroarch-ps2:
  image: $CI_SERVER_HOST:5050/libretro-infrastructure/libretro-build-ps2:latest
  stage: prepare-for-static-cores
  artifacts:
    paths:
    -  retroarch-precompiled/
    expire_in: 1 day
  dependencies: []
  script:
    # Allow failure since we don't have a core
    # PS2 makefile for now doesn't allow -jX so this is why is hardcoded to -j1
    - "make -f Makefile.ps2 -j1 ||:"
    - "mkdir .retroarch-precompiled"
    - "cp -r ./* .retroarch-precompiled/"
    - "mv .retroarch-precompiled/ retroarch-precompiled/"

build-static-retroarch-psp:
  image: $CI_SERVER_HOST:5050/libretro-infrastructure/libretro-build-psp:latest
  stage: prepare-for-static-cores
  before_script:
    - export NUMPROC=$(($(nproc)/3))
  artifacts:
    paths:
    -  retroarch-precompiled/
    expire_in: 1 day
  dependencies: []
  script:
    # We need to boostrap before compile
    - "(cd bootstrap/psp1/kernel_functions_prx/ && make && cd -)"
    # Allow failure since we don't have a core
    - "make -f Makefile.psp1 -j$NUMPROC ||:"
    - "mkdir .retroarch-precompiled"
    - "cp -r ./* .retroarch-precompiled/"
    - "mv .retroarch-precompiled/ retroarch-precompiled/"

build-static-retroarch-ctr:
  image: $CI_SERVER_HOST:5050/libretro-infrastructure/libretro-build-devkitpro:latest
  stage: prepare-for-static-cores
  before_script:
    - export NUMPROC=$(($(nproc)/3))
  artifacts:
    paths:
    -  retroarch-precompiled/
    expire_in: 1 day
  dependencies: []
  needs:
    # Dummy build requires no core
    - build-static-retroarch-dummy-ctr
  script:
    # Allow failure since we don't have a core
    - "make -f Makefile.ctr -j$NUMPROC USE_CTRULIB_2=1 ||:"
    - "mkdir .retroarch-precompiled"
    - "cp -r ./* .retroarch-precompiled/"
    - "mv .retroarch-precompiled/ retroarch-precompiled/"

build-static-retroarch-dummy-ctr:
  image: $CI_SERVER_HOST:5050/libretro-infrastructure/libretro-build-devkitpro:latest
  stage: build
  before_script:
    - export NUMPROC=$(($(nproc)/3))
  artifacts:
    paths:
    -  retroarch_3ds.cia
    -  RetroArch.3dsx
    -  RetroArch.smdh
    expire_in: 1 month
  dependencies: []
  script:
    - "make -f Makefile.ctr.salamander -j$NUMPROC USE_CTRULIB_2=1"
    - "mv -f retroarch_3ds_salamander.cia retroarch_3ds.cia"
    - "mv -f retroarch_3ds_salamander.3dsx RetroArch.3dsx"
    - "mv -f retroarch_3ds_salamander.smdh RetroArch.smdh"

build-static-retroarch-wii:
  image: $CI_SERVER_HOST:5050/libretro-infrastructure/libretro-build-devkitpro:latest
  stage: prepare-for-static-cores
  before_script:
    - export NUMPROC=$(($(nproc)/3))
  artifacts:
    paths:
    -  retroarch-precompiled/
    expire_in: 1 day
  dependencies: []
  needs:
    # Dummy build requires no core
    - build-static-retroarch-dummy-wii
  script:
    # Allow failure since we don't have a core
    - "make -f Makefile.wii -j$NUMPROC EXTERNAL_LIBOGC=1 GX_PTHREAD_LEGACY=0 ||:"
    - "mkdir .retroarch-precompiled"
    - "cp -r ./* .retroarch-precompiled/"
    - "mv .retroarch-precompiled/ retroarch-precompiled/"

build-static-retroarch-dummy-wii:
  image: $CI_SERVER_HOST:5050/libretro-infrastructure/libretro-build-devkitpro:latest
  stage: build
  before_script:
    - export NUMPROC=$(($(nproc)/3))
  artifacts:
    paths:
    -  boot.dol
    expire_in: 1 month
  dependencies: []
  script:
    - "make -f Makefile.wii.salamander -j$NUMPROC EXTERNAL_LIBOGC=1 GX_PTHREAD_LEGACY=0"
    - "mv -f retroarch-salamander_wii.dol boot.dol"

build-static-retroarch-ngc:
  image: $CI_SERVER_HOST:5050/libretro-infrastructure/libretro-build-devkitpro:latest
  stage: prepare-for-static-cores
  before_script:
    - export NUMPROC=$(($(nproc)/3))
  artifacts:
    paths:
    -  retroarch-precompiled/
    expire_in: 1 day
  dependencies: []
  script:
    # Allow failure since we don't have a core
    - "make -f Makefile.ngc -j$NUMPROC EXTERNAL_LIBOGC=1 GX_PTHREAD_LEGACY=0 ||:"
    - "mkdir .retroarch-precompiled"
    - "cp -r ./* .retroarch-precompiled/"
    - "mv .retroarch-precompiled/ retroarch-precompiled/"

trigger_static-cores:
  stage: trigger-static-cores
  needs:
  - build-static-retroarch-libnx-aarch64
  - build-static-retroarch-vita
  - build-static-retroarch-ps2
  - build-static-retroarch-psp
  - build-static-retroarch-ctr
  - build-static-retroarch-wii
  - build-static-retroarch-ngc
  dependencies: []
  script:
    # Dummy for now
    - /bin/true