stages:
    - build-libvlc
    - build-unity-plugin

variables:
    VLC_UWP_LLVM_IMAGE: registry.videolan.org/vlc-debian-llvm-ucrt:20240617101054
    VLC_WIN_LLVM_IMAGE: registry.videolan.org/vlc-debian-llvm-msvcrt:20240617101054
    VLC_DEBIAN_IMAGE: registry.videolan.org/vlc-debian-unstable:20240402131352
    VLC_ANDROID_IMAGE: registry.videolan.org/vlc-debian-android:20240522152555

.variables-trial: &variables-trial
    ARGS_TRIAL: -Dwatermark=true

.base-template:
    tags:
        - docker
        - amd64
    stage: build-libvlc
    artifacts:
        paths:
            - nightlies/*
        expire_in: 2 weeks

.default_rules: &default_rules
  rules:
    - if: '$CI_PIPELINE_SOURCE == "schedule" || $CI_PIPELINE_SOURCE == "web"'
      when: always
    - if: '$CI_PIPELINE_SOURCE == "push" || $CI_PIPELINE_SOURCE == "merge_request_event"'
      changes:
        - "!Assets/VLCUnity/**/*"

# ANDROID

.variables-android-armv7: &variables-android-armv7
        ANDROID_ABI: armeabi-v7a

.variables-android-armv8: &variables-android-armv8
        ANDROID_ABI: arm64-v8a

.variables-android-x86: &variables-android-x86
        ANDROID_ABI: x86

.variables-android-x86_64: &variables-android-x86_64
        ANDROID_ABI: x86_64

.libvlc-android-common:
    extends: .base-template
    image:
        name: $VLC_ANDROID_IMAGE
    script: |
        git clone https://code.videolan.org/videolan/vlc-android && cd vlc-android
        git clone https://code.videolan.org/videolan/libvlcjni && cd libvlcjni
        git clone https://code.videolan.org/videolan/vlc/ --depth=1
        git apply ../../patches/no-libvlcjni-build.patch
        cd ..
        ANDROID_HOME=/sdk/android-sdk-linux ./buildsystem/compile.sh -l -b -r -a ${ANDROID_ABI}
    artifacts:
        paths:
            - vlc-android/libvlcjni/libvlc/jni
            - vlc-android/libvlcjni/vlc/include
            - vlc-android/libvlcjni/libvlc/build/intermediates/aar_main_jar/release/classes.jar
    <<: *default_rules

.unity-android-common:
    extends: .base-template
    image:
        name: $VLC_ANDROID_IMAGE
    stage: build-unity-plugin
    script:
        - export PATH="$HOME/sandbox/bin:$PATH"
        - PKG_CONFIG_PATH=./vlc-android/libvlcjni/libvlc/jni/pkgconfig/${ANDROID_ABI} meson setup --cross-file=cross/android-${ANDROID_ABI}-ndk25.txt build_android_${ANDROID_ABI} ${ARGS_TRIAL} --buildtype release
        - ninja -C build_android_${ANDROID_ABI}
    artifacts:
        paths:
            - vlc-android/libvlcjni/libvlc/jni/libs/${ANDROID_ABI}/libvlc.so
            - vlc-android/libvlcjni/libvlc/jni/libs/${ANDROID_ABI}/libc++_shared.so
            - vlc-android/libvlcjni/libvlc/build/intermediates/aar_main_jar/release/classes.jar
            - build_android_${ANDROID_ABI}/PluginSource/libVLCUnityPlugin.so
    <<: *default_rules

# libvlc android

libvlc-android-armv7:
    extends: .libvlc-android-common
    variables: *variables-android-armv7

libvlc-android-armv8:
    extends: .libvlc-android-common
    variables: *variables-android-armv8

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

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

# unity android

unity-android-armv7:
    extends: .unity-android-common
    variables: *variables-android-armv7
    dependencies:
      - libvlc-android-armv7

unity-android-armv8:
    extends: .unity-android-common
    variables: *variables-android-armv8
    dependencies:
      - libvlc-android-armv8

unity-android-x86:
    extends: .unity-android-common
    variables: *variables-android-x86
    dependencies:
      - libvlc-android-x86

unity-android-x86_64:
    extends: .unity-android-common
    variables: *variables-android-x86_64
    dependencies:
      - libvlc-android-x86_64

unity-android-armv7-trial:
    extends: .unity-android-common
    variables:
      <<: [*variables-android-armv7, *variables-trial]
    dependencies:
      - libvlc-android-armv7

unity-android-armv8-trial:
    extends: .unity-android-common
    variables:
      <<: [*variables-android-armv8, *variables-trial]
    dependencies:
      - libvlc-android-armv8

# Windows

.variables-windows-classic: &variables-windows-classic
        PLATFORM: windows
        CROSSFILE: windows-x86_64.txt

.variables-windows-uwp: &variables-windows-uwp
        PLATFORM: uwp

.libvlc-win-common:
    tags:
        - docker
        - amd64
    stage: build-libvlc
    script: |
        git clone https://code.videolan.org/videolan/vlc/ && cd vlc
        git apply ../patches/disable-activex.patch
        if [[ $ARGS == *"-w"* ]]; then
            echo "Applying the UWP specific patch"
            git apply ../patches/uwp-plugins-path.patch
        fi
        extras/package/win32/build.sh $ARGS
    after_script:
        - mkdir nightlies
        - cd ./vlc/$BUILD_FOLDER
        - make package-win-strip
        - rm -rf vlc-4.0.0-dev/lua/http
    artifacts:
        paths:
            - vlc/${BUILD_FOLDER}/vlc-4.0.0-dev
            - vlc/${BUILD_FOLDER}/_win32
        expire_in: 2 weeks
    variables:
        WINE_SDK_PATH: /usr/include/wine/wine/windows/
    <<: *default_rules

.unity-win-common:
    image:
        name: $VLC_WIN_LLVM_IMAGE
    tags:
        - docker
        - amd64
    stage: build-unity-plugin
    script:
        - PKG_CONFIG_PATH=./vlc/${BUILD_FOLDER}/_win32/lib/pkgconfig meson setup build_${PLATFORM} --cross-file=cross/${CROSSFILE} ${ARGS_TRIAL} --buildtype release
        - ninja -C build_${PLATFORM}
        - mv build_${PLATFORM}/PluginSource/libVLCUnityPlugin-1.dll VLCUnityPlugin.dll
    artifacts:
        paths:
            - VLCUnityPlugin.dll
        expire_in: 2 weeks
    <<: *default_rules

libvlc-win-x86_64-debug:
    extends: .libvlc-win-common
    image:
        name: $VLC_WIN_LLVM_IMAGE
    variables:
        ARGS: -c -z -d -x -a x86_64 -g l
        ARTIFACT: vlc-4.0.0-dev-win64-debug.7z
        BUILD_FOLDER: win64

libvlc-win-x86_64:
    extends: .libvlc-win-common
    image:
        name: $VLC_WIN_LLVM_IMAGE
    variables:
        ARGS: -c -z -r -x -a x86_64 -i nope -g l
        ARTIFACT: vlc-4.0.0-dev-win64.7z
        BUILD_FOLDER: win64

libvlc-uwp-x64:
    extends: .libvlc-win-common
    image:
        name: $VLC_UWP_LLVM_IMAGE
    variables:
        ARGS: -w -u -c -z -r -x -a x86_64 -i nope -S 0x0A000007 -g l
        ARTIFACT: vlc-4.0.0-dev-win64.7z
        BUILD_FOLDER: win64-uwp

libvlc-uwp-arm64:
    extends: .libvlc-win-common
    image:
        name: $VLC_UWP_LLVM_IMAGE
    variables:
        ARGS: -w -u -c -z -r -x -a aarch64 -i nope -S 0x0A000007 -g l
        ARTIFACT: vlc-4.0.0-dev-win64.7z
        BUILD_FOLDER: winarm64-uwp

libvlc-uwp-x64-debug:
    extends: .libvlc-win-common
    image:
        name: $VLC_UWP_LLVM_IMAGE
    variables:
        ARGS: -w -u -c -z -d -x -a x86_64 -S 0x0A000007 -g l
        ARTIFACT: vlc-4.0.0-dev-win64-debug.7z
        BUILD_FOLDER: win64-uwp

unity-plugin-windows:
    extends: .unity-win-common
    dependencies:
      - libvlc-win-x86_64
    variables: 
        <<: [*variables-windows-classic]
        BUILD_FOLDER: win64

unity-plugin-windows-trial:
    extends: .unity-win-common
    dependencies:
      - libvlc-win-x86_64
    variables:
        <<: [*variables-trial, *variables-windows-classic]
        BUILD_FOLDER: win64

unity-plugin-uwp-x64:
    extends: .unity-win-common
    dependencies:
      - libvlc-uwp-x64
    variables: 
        <<: [*variables-windows-uwp]
        BUILD_FOLDER: win64-uwp
        CROSSFILE: uwp-x86_64.txt

unity-plugin-uwp-x64-trial:
    extends: .unity-win-common
    dependencies:
      - libvlc-uwp-x64
    variables:
        <<: [*variables-trial, *variables-windows-uwp]
        BUILD_FOLDER: win64-uwp
        CROSSFILE: uwp-x86_64.txt

unity-plugin-uwp-arm64:
    extends: .unity-win-common
    dependencies:
      - libvlc-uwp-arm64
    variables: 
        <<: [*variables-windows-uwp]
        BUILD_FOLDER: winarm64-uwp
        CROSSFILE: uwp-arm64.txt

unity-plugin-uwp-arm64-trial:
    extends: .unity-win-common
    dependencies:
      - libvlc-uwp-arm64
    variables: 
        <<: [*variables-trial, *variables-windows-uwp]
        BUILD_FOLDER: winarm64-uwp
        CROSSFILE: uwp-arm64.txt

# libvlc mac

.variables-macos-x64: &variables-macos-x64
        ARCH: x86_64
        VLC_PATH: /Users/videolanci/sandbox/bin

.variables-macos-aarch64: &variables-macos-aarch64
        ARCH: aarch64
        VLC_PATH: /Users/videolanci/sandbox/bin

.libvlc-macos-common:
    stage: build-libvlc
    script:
        - git clone https://code.videolan.org/videolan/vlc && cd vlc   
        - mkdir build && cd build
        - ../extras/package/macosx/build.sh ${ARGS} -a ${ARCH}
        # remove sym files
        - rm -rf macos-install/lib/libvlc.dylib
        - rm -rf macos-install/lib/libvlccore.dylib
        - mv macos-install/lib/libvlc.12.dylib macos-install/lib/libvlc.dylib
        - mv macos-install/lib/libvlccore.9.dylib macos-install/lib/libvlccore.dylib
    artifacts:
        paths:
            - vlc/build/macos-install/include/*
            - vlc/build/macos-install/libexec/*
            - vlc/build/macos-install/lib/pkgconfig/*
            - vlc/build/macos-install/lib/vlc/**/*.dylib
            - vlc/build/macos-install/lib/libvlc.dylib
            - vlc/build/macos-install/lib/libvlccore.dylib
        expire_in: 2 weeks
    variables:
        ARGS: -i z -c -g l
    <<: *default_rules

libvlc-macos-x86_64:
    extends: .libvlc-macos-common
    variables: *variables-macos-x64
    tags:
        - monterey
        - amd64

libvlc-macos-aarch64:
    extends: .libvlc-macos-common
    variables: *variables-macos-aarch64
    tags:
        - monterey
        - macos-m1

# unity plugin mac

.unity-plugin-macos-common:
    stage: build-unity-plugin
    script:
        # patch libvlc.pc to build the plugin
        - cd vlc/build/macos-install
        - sed -i '' "1s|.*|prefix=$(pwd | sed 's#/#\\\/#g')|" lib/pkgconfig/libvlc.pc
        - export PATH="$HOME/sandbox/bin:$PATH"
        - cd ../../..
        - PKG_CONFIG_PATH=./vlc/build/macos-install/lib/pkgconfig meson setup build_${ARCH} ${ARGS_TRIAL} --buildtype release
        - ninja -C build_${ARCH}
        - mv "build_${ARCH}/PluginSource/libVLCUnityPlugin.1.dylib" "build_${ARCH}/PluginSource/libVLCUnityPlugin.dylib"
        - install_name_tool -add_rpath @loader_path/ build_${ARCH}/PluginSource/libVLCUnityPlugin.dylib
    artifacts:
        paths:
            - "build_${ARCH}/PluginSource/libVLCUnityPlugin.dylib"
        expire_in: 2 weeks
    <<: *default_rules

unity-plugin-macos-x86_64:
    extends: .unity-plugin-macos-common
    dependencies:
        - libvlc-macos-x86_64
    variables: *variables-macos-x64
    tags:
        - monterey
        - amd64

unity-plugin-macos-x86_64-trial:
    extends: .unity-plugin-macos-common
    dependencies:
        - libvlc-macos-x86_64
    variables:
      <<: [*variables-macos-x64, *variables-trial]
    tags:
        - monterey
        - amd64

unity-plugin-macos-aarch64:
    extends: .unity-plugin-macos-common
    dependencies:
        - libvlc-macos-aarch64
    variables: *variables-macos-aarch64
    tags:
        - monterey
        - macos-m1

unity-plugin-macos-aarch64-trial:
    extends: .unity-plugin-macos-common
    dependencies:
        - libvlc-macos-aarch64
    variables:
      <<: [*variables-macos-aarch64, *variables-trial]
    tags:
        - monterey
        - macos-m1

# libvlc iOS

.variables-ios-x64: &variables-ios-x64
        ARCH: x86_64
        SDKNAME: iphonesimulator
        VLC_PATH: /Users/videolanci/sandbox/bin
        SHORT_ARCH: x86_64

.variables-ios-arm64: &variables-ios-arm64
        ARCH: aarch64
        SDKNAME: iphoneos
        VLC_PATH: /Users/videolanci/sandbox/bin
        SHORT_ARCH: arm64

.libvlc-ios-common:
    stage: build-libvlc
    tags:
        - monterey
    script:
        - git clone https://code.videolan.org/videolan/vlc && cd vlc
        # get latest live555 patches
        - curl -O https://code.videolan.org/videolan/VLCKit/-/raw/master/libvlc/patches/0001-Revert-contrib-live555-update-to-2022.07.14.patch
        - curl -O https://code.videolan.org/videolan/VLCKit/-/raw/master/libvlc/patches/0002-contrib-use-live555-version-that-is-compatible-with-.patch
        - git apply 0001-Revert-contrib-live555-update-to-2022.07.14.patch && git apply 0002-contrib-use-live555-version-that-is-compatible-with-.patch
        - mkdir build && cd build
        - ../extras/package/apple/build.sh --sdk=$SDKNAME --arch=$ARCH -j8 --enable-shared
        - cd ../..
    artifacts:
        paths:
            - vlc/build/vlc-$SDKNAME-$SHORT_ARCH/lib/*
            - vlc/build/vlc-$SDKNAME-$SHORT_ARCH/include/*
        expire_in: 2 weeks
    <<: *default_rules

libvlc-ios-x86_64:
    extends: .libvlc-ios-common
    variables: *variables-ios-x64

libvlc-ios-arm64:
    extends: .libvlc-ios-common
    variables: *variables-ios-arm64

# unity plugin iOS

.unity-plugin-ios-common:
    stage: build-unity-plugin
    tags:
        - monterey
    script:
        # patch libvlc.pc to build the plugin
        - cd vlc/build/vlc-$SDKNAME-$SHORT_ARCH
        - sed -i '' "1s|.*|prefix=$(pwd | sed 's#/#\\\/#g')|" lib/pkgconfig/libvlc.pc
        - cd ../../..
        - export PATH="$HOME/sandbox/bin:$PATH"
        - PKG_CONFIG_PATH=./vlc/build/vlc-$SDKNAME-$SHORT_ARCH/lib/pkgconfig meson setup --cross-file=cross/$SDKNAME.txt build_${SHORT_ARCH} ${ARGS_TRIAL} --buildtype release
        - ninja -C build_${SHORT_ARCH}
        - ./copy_ios.sh vlc/build/vlc-$SDKNAME-$SHORT_ARCH $SHORT_ARCH
    artifacts:
        paths:
            - Assets/VLCUnity/Plugins/iOS
        expire_in: 2 weeks
    <<: *default_rules

unity-plugin-ios-aarch64:
    extends: .unity-plugin-ios-common
    variables: *variables-ios-arm64
    dependencies:
        - libvlc-ios-arm64

unity-plugin-ios-aarch64-trial:
    extends: .unity-plugin-ios-common
    variables:
      <<: [*variables-ios-arm64, *variables-trial]
    dependencies:
        - libvlc-ios-arm64

unity-plugin-ios-x86_64:
    extends: .unity-plugin-ios-common
    variables: *variables-ios-x64
    dependencies:
        - libvlc-ios-x86_64
    
# LibVLCSharp

.libvlcsharp-unity:
    image:
        name: $VLC_DEBIAN_IMAGE
    tags:
        - docker
        - amd64
    stage: build-libvlc
    script:
        - wget https://download.visualstudio.microsoft.com/download/pr/db901b0a-3144-4d07-b8ab-6e7a43e7a791/4d9d1b39b879ad969c6c0ceb6d052381/dotnet-sdk-8.0.401-linux-x64.tar.gz
        - mkdir -p $HOME/dotnet && tar zxf dotnet-sdk-8.0.401-linux-x64.tar.gz -C $HOME/dotnet
        - export DOTNET_ROOT=$HOME/dotnet
        - export PATH=$PATH:$HOME/dotnet
        - mkdir tmp && cd tmp && git clone https://code.videolan.org/videolan/LibVLCSharp lvs
        - cd lvs
        - git checkout -f master
        - dotnet build src/LibVLCSharp/LibVLCSharp.csproj ${ARGS} -c Release
    after_script:
        - mkdir nightlies
        - cp -r tmp/lvs/src/LibVLCSharp/bin/Release/netstandard2.0 nightlies
    artifacts:
        paths:
            - nightlies/*
        expire_in: 2 weeks

libvlcsharp-all:
    extends: .libvlcsharp-unity
    variables:
        ARGS: /p:UNITY=true

libvlcsharp-ios:
    extends: .libvlcsharp-unity
    variables:
        ARGS: /p:UNITY_IOS=true