stages:
    - build

default:
    before_script:
        - set -x
        - export VLC_CONTRIB_SHA="$(extras/ci/get-contrib-sha.sh)"
        - export VLC_PREBUILT_CONTRIBS_URL="https://artifacts.videolan.org/vlc/${CI_JOB_NAME##nightly-}/vlc-contrib-${TRIPLET}-${VLC_CONTRIB_SHA}.tar.bz2"
        - if ! extras/ci/check-url.sh "$VLC_PREBUILT_CONTRIBS_URL"; then unset VLC_PREBUILT_CONTRIBS_URL; fi
    after_script:
        - export VLC_CONTRIB_SHA="$(extras/ci/get-contrib-sha.sh)"
        - mv contrib/vlc-contrib-*.tar.bz2 contrib/vlc-contrib-${TRIPLET}-${VLC_CONTRIB_SHA}.tar.bz2 2>/dev/null || true
        - cd contrib && find . -maxdepth 1 -type f -name "vlc-contrib-*-${VLC_CONTRIB_SHA}.tar.bz2" -exec sh -c 'tar tf "$0" > /dev/null && shasum -a 512 "$0" | tee SHA512SUM' "{}" \;
    interruptible: true
    # Most jobs use docker, so we can default to those tags.
    # macOS base template will overwrite this
    tags:
        - docker
        - amd64

variables:
    VLC_WIN32_IMAGE: registry.videolan.org/vlc-debian-win32:20201106141924
    VLC_WIN64_IMAGE: registry.videolan.org/vlc-debian-win64:20201106143728
    VLC_WIN_LLVM_IMAGE: registry.videolan.org/vlc-debian-llvm-mingw:20211020094514
    VLC_UWP_LLVM_IMAGE: registry.videolan.org/vlc-debian-llvm-uwp:20211020111246
    VLC_DEBIAN_IMAGE: registry.videolan.org/vlc-debian-unstable:20210803114245
    VLC_ANDROID_IMAGE: registry.videolan.org/vlc-debian-android:20210730131708
    VLC_SNAP_IMAGE: registry.videolan.org/vlc-ubuntu-focal:20211020115724
    VLC_RASPBIAN_IMAGE: registry.videolan.org/vlc-ubuntu-raspberry:20211006142322
    VLC_WASM_EMSCRIPTEN: registry.videolan.org/vlc-debian-wasm-emscripten:20210915101305

.variables-debian: &variables-debian
        HOST_ARCH: x86_64
        TRIPLET: $HOST_ARCH-linux-gnu

.variables-snap: &variables-snap
        HOST_ARCH: x86_64
        TRIPLET: $HOST_ARCH-linux-gnu

.variables-raspbian-arm: &variables-raspbian-arm
        HOST_ARCH: arm
        TRIPLET: $HOST_ARCH-linux-gnueabihf

.variables-win32: &variables-win32
        SHORTARCH: win32
        HOST_ARCH: i686
        TRIPLET: $HOST_ARCH-w64-mingw32

.variables-win64: &variables-win64
        SHORTARCH: win64
        HOST_ARCH: x86_64
        TRIPLET: $HOST_ARCH-w64-mingw32

.variables-uwp-arm64: &variables-uwp-arm64
        SHORTARCH: arm64
        HOST_ARCH: aarch64
        TRIPLET: $HOST_ARCH-w64-mingw32

.variables-macos-x86_64: &variables-macos-x86_64
        VLC_PATH: /Users/videolanci/sandbox/bin
        VLC_FORCE_KERNELVERSION: 19
        SHORTARCH: intel64
        HOST_ARCH: x86_64
        TRIPLET: $HOST_ARCH-apple-darwin$VLC_FORCE_KERNELVERSION

.variables-macos-arm64: &variables-macos-arm64
        VLC_PATH: /Users/videolanci/sandbox/bin
        VLC_FORCE_KERNELVERSION: 19
        SHORTARCH: arm64
        HOST_ARCH: aarch64
        TRIPLET: $HOST_ARCH-apple-darwin$VLC_FORCE_KERNELVERSION

.variables-ios-arm64: &variables-ios-arm64
        VLC_PATH: /Users/videolanci/sandbox/bin
        HOST_ARCH: arm64
        TRIPLET: $HOST_ARCH-apple-iphoneos
        SDKNAME: iphoneos

.variables-ios-armv7: &variables-ios-armv7
        VLC_PATH: /Users/videolanci/sandbox/bin
        HOST_ARCH: armv7
        TRIPLET: $HOST_ARCH-apple-iphoneos
        SDKNAME: iphoneos

.variables-ios-simulator-x86_64: &variables-ios-simulator-x86_64
        VLC_PATH: /Users/videolanci/sandbox/bin
        HOST_ARCH: x86_64
        TRIPLET: $HOST_ARCH-apple-iphonesimulator
        SDKNAME: iphonesimulator

.variables-tvos-arm64: &variables-tvos-arm64
        VLC_PATH: /Users/videolanci/sandbox/bin
        HOST_ARCH: arm64
        TRIPLET: $HOST_ARCH-apple-appletvos
        SDKNAME: appletvos

.variables-android-arm: &variables-android-arm
        ANDROID_ARCH: arm
        TRIPLET: arm-linux-androideabi

.variables-android-arm64: &variables-android-arm64
        ANDROID_ARCH: arm64
        TRIPLET: aarch64-linux-android

.variables-android-x86: &variables-android-x86
        ANDROID_ARCH: x86
        TRIPLET: i686-linux-android

.variables-android-x86_64: &variables-android-x86_64
        ANDROID_ARCH: x86_64
        TRIPLET: x86_64-linux-android

.variables-emscripten-wasm32: &variables-emscripten-wasm32
        HOST_ARCH: wasm32
        TRIPLET: $HOST_ARCH-unknown-emscripten

# Common rules
.base-template:
    stage: build
    only:
        refs:
            - merge_requests
            - master@videolan/vlc
    except:
        - schedules
    artifacts:
        paths:
            - contrib/vlc-contrib-${TRIPLET}-*.tar.bz2
            - contrib/SHA512SUM

#
# Windows
#
.win-common:
    extends: .base-template
    script: |
        if [ "${CI_JOB_NAME:0:8}" = "nightly-" ]; then
            NIGHTLY_EXTRA_BUILD_FLAGS="-i n -l"
        fi
        if [ -n "$VLC_PREBUILT_CONTRIBS_URL" ]; then
            echo "Building using prebuilt contribs at $VLC_PREBUILT_CONTRIBS_URL"
            extras/package/win32/build.sh -p -a $HOST_ARCH $NIGHTLY_EXTRA_BUILD_FLAGS $LIBVLC_EXTRA_BUILD_FLAGS $UWP_EXTRA_BUILD_FLAGS
        else
            extras/package/win32/build.sh -c -a $HOST_ARCH $NIGHTLY_EXTRA_BUILD_FLAGS $LIBVLC_EXTRA_BUILD_FLAGS $UWP_EXTRA_BUILD_FLAGS
        fi

win32:
    extends: .win-common
    image:
        name: $VLC_WIN32_IMAGE
    variables: *variables-win32

win64:
    extends: .win-common
    image:
        name: $VLC_WIN64_IMAGE
    variables: *variables-win64

win64-llvm:
    extends: .win-common
    image:
        name: $VLC_WIN_LLVM_IMAGE
    variables: *variables-win64

win64-ucrt-llvm:
    extends: .win-common
    image:
        name: $VLC_UWP_LLVM_IMAGE
    variables:
        <<: *variables-win64
        TRIPLET: $HOST_ARCH-ucrt-w64-mingw32
        UWP_EXTRA_BUILD_FLAGS: -u -x -S 0x0A000006

uwp64-libvlc-llvm:
    extends: .win-common
    image:
        name: $VLC_UWP_LLVM_IMAGE
    variables:
        <<: *variables-win64
        LIBVLC_EXTRA_BUILD_FLAGS: -z
        UWP_EXTRA_BUILD_FLAGS: -u -w -x

uwp-arm64-libvlc-llvm:
    extends: .win-common
    image:
        name: $VLC_UWP_LLVM_IMAGE
    variables:
        <<: *variables-uwp-arm64
        LIBVLC_EXTRA_BUILD_FLAGS: -z
        UWP_EXTRA_BUILD_FLAGS: -u -w -x

.nightly-win-common:
    extends: .win-common
    only:
        - schedules
    except:
    after_script:
        - mkdir nightlies
        - for ext in 7z zip; do mv ${SHORTARCH}/vlc-*-dev-*-debug.${ext} nightlies/$(basename ${SHORTARCH}/vlc-*-dev-*-debug.${ext} | sed "s/\.${ext}/-${CI_COMMIT_SHORT_SHA}\.${ext}/"); done
        - for ext in exe msi 7z zip; do mv ${SHORTARCH}/vlc-*-dev-*.${ext} nightlies/$(basename ${SHORTARCH}/vlc-*-dev-*.${ext} | sed "s/\.${ext}/-${CI_COMMIT_SHORT_SHA}\.${ext}/"); done
        - cd nightlies && find . -maxdepth 1 -type f -not -name SHA512SUM | xargs shasum -a 512 | tee SHA512SUM
    artifacts:
        paths:
            - nightlies/*

nightly-win32:
    extends: .nightly-win-common
    image:
        name: $VLC_WIN32_IMAGE
    variables: *variables-win32

nightly-win64:
    extends: .nightly-win-common
    image:
        name: $VLC_WIN64_IMAGE
    variables: *variables-win64

nightly-win64-llvm:
    extends: .nightly-win-common
    image:
        name: $VLC_WIN_LLVM_IMAGE
    variables: *variables-win64

nightly-win64-ucrt-llvm:
    extends: .nightly-win-common
    image:
        name: $VLC_UWP_LLVM_IMAGE
    variables:
        <<: *variables-win64
        TRIPLET: $HOST_ARCH-ucrt-w64-mingw32
        UWP_EXTRA_BUILD_FLAGS: -u -x -S 0x0A000006

#
# Debian
#
debian:
    extends: .base-template
    image:
        name: $VLC_DEBIAN_IMAGE
    script: |
        export NCPU=$(getconf _NPROCESSORS_ONLN)

        # Build tools
        ( cd extras/tools && ./bootstrap && make -j$NCPU --output-sync=recurse )
        export PATH="$(pwd)/extras/tools/build/bin:$PATH"

        # Build contribs
        mkdir -p contrib/contrib-$TRIPLET && cd contrib/contrib-$TRIPLET
        ../bootstrap
        if [ -n "$VLC_PREBUILT_CONTRIBS_URL" ]; then
            echo "Building using prebuilt contribs at $VLC_PREBUILT_CONTRIBS_URL"
            make prebuilt PREBUILT_URL="${VLC_PREBUILT_CONTRIBS_URL}"
            make -j$NCPU --output-sync=recurse .protoc
        else
            make list
            make -j$NCPU --output-sync=recurse fetch
            make -j$NCPU --output-sync=recurse
            make package
        fi
        cd ../../

        # Build VLC
        ./bootstrap
        ./configure
        make -j$NCPU

        # Run tests
        VLC_TEST_TIMEOUT=60 sh -x ./test/make_check_wrapper.sh -j4
        if [ "${CI_JOB_NAME:0:8}" = "nightly-" ]; then
            export XZ_OPT="-T 0"
            make -j$NCPU distcheck
        else
          # Don't run distcheck but dist as it is already tested via
          # make_check_wrapper.sh
          make -j$NCPU dist
        fi
    variables: *variables-debian

nightly-debian:
    extends: debian
    only:
        - schedules
    except:
    after_script:
    artifacts:

#
# Snap builds
#
.snap-common:
    extends: .base-template
    image:
        name: $VLC_SNAP_IMAGE
    script: |
            cd extras/package/snap
            make -f package.mak snap
    after_script:
        - export VLC_CONTRIB_SHA="$(extras/ci/get-contrib-sha.sh)"
        - mv extras/package/snap/parts/vlc/build/contrib/vlc-contrib-*.tar.bz2 contrib/vlc-contrib-${TRIPLET}-${VLC_CONTRIB_SHA}.tar.bz2 2>/dev/null || true
    variables: *variables-snap

snap:
    extends: .snap-common

nightly-snap:
    extends: .snap-common
    only:
        - schedules
    except:
    after_script:
        - if [ "$CI_PROJECT_NAMESPACE" = "videolan" ]; then /bin/true; else exit 0; fi
        - mkdir nightlies
        - mv extras/package/snap/vlc_*.snap nightlies/
        - echo $SNAP_LOGIN | base64 --decode | snapcraft login --with -
        - snapcraft push nightlies/vlc_*.snap --release edge
        - snapcraft logout
        - cd nightlies && find . -maxdepth 1 -type f -not -name SHA512SUM | xargs shasum -a 512 | tee SHA512SUM
    artifacts:
        paths:
           - nightlies/*

#
# Raspbian
#
raspbian-arm:
    extends: .base-template
    image:
        name: $VLC_RASPBIAN_IMAGE
    script: |
        if [ -n "$VLC_PREBUILT_CONTRIBS_URL" ]; then
            echo "Building using prebuilt contribs at $VLC_PREBUILT_CONTRIBS_URL"
            extras/package/raspberry/build.sh -p -x -a $HOST_ARCH
        else
            extras/package/raspberry/build.sh -c -x -a $HOST_ARCH
        fi
    variables: *variables-raspbian-arm

#
# macOS
#
.ccache-common-apple:
  variables:
    CCACHE_BASEDIR: ${CI_PROJECT_DIR}
    CCACHE_DIR: ${CI_PROJECT_DIR}/ccache
    CCACHE_COMPILERCHECK: content
    CCACHE_CPP2: 'true'
    VLC_CCACHE_BINS_PATH: '/Users/videolanci/sandbox/bin/ccache/bin'
  before_script:
    - !reference [default, before_script]
    - |
      if [ -x "${VLC_CCACHE_BINS_PATH}/ccache" ]; then
        ${VLC_CCACHE_BINS_PATH}/ccache --zero-stats
        ${VLC_CCACHE_BINS_PATH}/ccache --show-stats
      else
        echo "No ccache present, disabling caching"
        unset VLC_CCACHE_BINS_PATH
      fi
  after_script:
    - !reference [default, after_script]
    - |
      if [ -x "${VLC_CCACHE_BINS_PATH}/ccache" ]; then
        ${VLC_CCACHE_BINS_PATH}/ccache --show-stats
      fi
  cache:
    key: ${CI_JOB_NAME}
    paths:
      - ccache

.macos-common:
    extends:
        - .base-template
        - .ccache-common-apple
    tags:
        - amd64
        - catalina
    script: |
        if [ "${CI_JOB_NAME:0:8}" = "nightly-" ]; then
            NIGHTLY_EXTRA_BUILD_FLAGS="-i n"
        fi
        if [ -n "$VLC_PREBUILT_CONTRIBS_URL" ]; then
            echo "Building using prebuilt contribs at $VLC_PREBUILT_CONTRIBS_URL"
        else
            EXTRA_BUILD_FLAGS="-c -p"
        fi
        mkdir build && cd build
        ../extras/package/macosx/build.sh $EXTRA_BUILD_FLAGS $NIGHTLY_EXTRA_BUILD_FLAGS -a $HOST_ARCH

macos-x86_64:
    extends: .macos-common
    variables: *variables-macos-x86_64

macos-arm64:
    extends: .macos-common
    variables: *variables-macos-arm64

.nightly-macos-common:
    extends: .macos-common
    only:
        - schedules
    except:
    after_script:
        - mkdir nightlies
        - mv build/vlc-*.dmg nightlies/$(basename build/vlc-*.dmg | sed "s/\.dmg/-${SHORTARCH}-${CI_COMMIT_SHORT_SHA}\.dmg/")
        - mv build/vlc-*.tar.gz nightlies/$(basename build/vlc-*.tar.gz | sed "s/\.tar\.gz/-${SHORTARCH}-${CI_COMMIT_SHORT_SHA}\.tar\.gz/")
        - cd nightlies && find . -maxdepth 1 -type f -not -name SHA512SUM | xargs shasum -a 512 | tee SHA512SUM
    artifacts:
        paths:
            - nightlies/*

nightly-macos-x86_64:
    extends: .nightly-macos-common
    variables: *variables-macos-x86_64

nightly-macos-arm64:
    extends: .nightly-macos-common
    variables: *variables-macos-arm64

#
# iOS
#
.apple-os-common:
    extends: .base-template
    tags:
        - amd64
        - catalina
    script: |
        mkdir build && cd build
        if [ -n "$VLC_PREBUILT_CONTRIBS_URL" ]; then
            ../extras/package/apple/build.sh --sdk=$SDKNAME --arch=$HOST_ARCH \
                --with-prebuilt-contribs
        else
            ../extras/package/apple/build.sh --sdk=$SDKNAME --arch=$HOST_ARCH --package-contribs
        fi

ios-arm64:
    extends: .apple-os-common
    variables: *variables-ios-arm64

ios-armv7:
    extends: .apple-os-common
    variables: *variables-ios-armv7

ios-simulator-x86_64:
    extends: .apple-os-common
    variables: *variables-ios-simulator-x86_64

tvos-arm64:
    extends: .apple-os-common
    variables: *variables-tvos-arm64

#
# Android
#
.android-common:
    extends: .base-template
    image:
        name: $VLC_ANDROID_IMAGE
    script: |
        wget https://code.videolan.org/videolan/vlc-android/raw/fd4d934ad31d37f3eeaa834214ca16eb0d4ecb6a/buildsystem/compile-libvlc.sh
        if [ -n "$VLC_PREBUILT_CONTRIBS_URL" ]; then
            /bin/sh ./compile-libvlc.sh -a $ANDROID_ARCH --with-prebuilt-contribs
        else
            /bin/sh ./compile-libvlc.sh -a $ANDROID_ARCH --package-contribs
        fi

android-arm:
    extends: .android-common
    variables: *variables-android-arm

android-arm64:
    extends: .android-common
    variables: *variables-android-arm64

android-x86:
    extends: .android-common
    variables: *variables-android-x86

android-x86_64:
    extends: .android-common
    variables: *variables-android-x86_64

#
# Webassembly
#

wasm32-emscripten-build :
    extends: .base-template
    variables: *variables-emscripten-wasm32
    image:
        name: $VLC_WASM_EMSCRIPTEN
    script:
        - source $EMSCRIPTEN_SDK/emsdk_env.sh
        - |
            if [ -n "$VLC_PREBUILT_CONTRIBS_URL" ]; then
                ./extras/package/wasm-emscripten/build.sh --with-prebuilt-contribs
            else
                ./extras/package/wasm-emscripten/build.sh --gen-contrib-archive
            fi

#
# VLC Documentation
#
pages:
    extends: .base-template
    image:
        name: $VLC_DEBIAN_IMAGE
    script:
        - ./bootstrap
        - ./configure
        - make doc
        - mv doc/doxygen/html public
    artifacts:
        paths:
            - public
    only:
        - schedules
    except:
