# Copyright © SixtyFPS GmbH <info@slint.dev>
# SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-1.1 OR LicenseRef-Slint-commercial

name: Build slint-viewer or -lsp binary

on:
    workflow_dispatch:
        inputs:
            program:
                type: choice
                description: binary to build
                options:
                    - viewer
                    - lsp
            features:
                type: string
                description: features to enable for build
                # Keep in sync with features in nightly_snapshot.yaml, cpp_package.yaml, and api/node/Cargo.toml
                default: "backend-linuxkms-noseat,backend-winit,renderer-femtovg,renderer-skia,renderer-software"
            codesign:
                type: boolean
                description: Sign binaries on macOS (false for manual builds)
                default: false

    workflow_call:
        inputs:
            program:
                type: string
                description: binary to build
            features:
                type: string
                description: features to enable for build
                # Keep in sync with features in nightly_snapshot.yaml, cpp_package.yaml, and api/node/Cargo.toml
                default: "backend-linuxkms-noseat,backend-winit,renderer-femtovg,renderer-skia,renderer-software"
            codesign:
                type: boolean
                description: Sign binaries on macOS
                default: true
        secrets:
            certificate:
                description: "certificate secret"
                required: false
            certificate_password:
                description: "certificate password"
                required: false
            keychain_password:
                description: "keychain password to use"
                required: false
            developer_id:
                description: "developer id to use"
                required: false

env:
    MACOSX_DEPLOYMENT_TARGET: "11.0"

jobs:
    build_windows:
        runs-on: windows-2022
        steps:
            - uses: actions/checkout@v4
            - uses: ./.github/actions/setup-rust
              with:
                  target: x86_64-pc-windows-msvc
            - name: Install Qt
              uses: jurplel/install-qt-action@v3
              with:
                  version: 6.5.1
                  cache: true
            - uses: baptiste0928/cargo-install@v3
              with:
                  crate: cargo-about
            - name: Build
              run: cargo build --verbose --no-default-features --features ${{ github.event.inputs.features || inputs.features }} --release -p slint-${{ github.event.inputs.program || inputs.program }}
            - name: Create artifact directory
              run: |
                  mkdir pkg
                  cd pkg
                  mkdir slint-${{ github.event.inputs.program || inputs.program }}
                  cd slint-${{ github.event.inputs.program || inputs.program }}
                  cp ..\..\target/release/slint-${{ github.event.inputs.program || inputs.program }}.exe ./
                  cd ..
                  cd ..
                  cd tools\${{ github.event.inputs.program || inputs.program }}
                  bash -x ../../scripts/prepare_binary_package.sh ..\..\pkg\slint-${{ github.event.inputs.program || inputs.program }}

            - name: Upload artifact
              uses: actions/upload-artifact@v4
              with:
                  name: slint-${{ github.event.inputs.program || inputs.program }}-windows
                  path: |
                      pkg

    build_linux:
        runs-on: ubuntu-20.04
        steps:
            - uses: actions/checkout@v4
            - uses: ./.github/actions/install-linux-dependencies
              with:
                  old-ubuntu: true
            - uses: ./.github/actions/setup-rust
              with:
                  target: x86_64-unknown-linux-gnu
            - name: Install Qt
              uses: jurplel/install-qt-action@v3
              with:
                  version: 5.15.2
                  cache: true
            - uses: baptiste0928/cargo-install@v3
              with:
                  crate: cargo-about
            - name: Build
              run: cargo build --verbose --no-default-features --features backend-qt,${{ github.event.inputs.features || inputs.features }} --release -p slint-${{ github.event.inputs.program || inputs.program }}
            - name: Create artifact directory
              run: |
                  mkdir -p slint-${{ github.event.inputs.program || inputs.program }}
                  cp target/release/slint-${{ github.event.inputs.program || inputs.program }} slint-${{ github.event.inputs.program || inputs.program }}/
                  cd tools/${{ github.event.inputs.program || inputs.program }}
                  ../../scripts/prepare_binary_package.sh ../../slint-${{ github.event.inputs.program || inputs.program }}
            - name: Tar artifacts to preserve permissions
              run: tar czvf slint-${{ github.event.inputs.program || inputs.program }}-linux.tar.gz slint-${{ github.event.inputs.program || inputs.program }}
            - name: Upload artifact
              uses: actions/upload-artifact@v4
              with:
                  name: slint-${{ github.event.inputs.program || inputs.program }}-linux
                  path: slint-${{ github.event.inputs.program || inputs.program }}-linux.tar.gz

    build_macos:
        runs-on: macos-latest
        steps:
            - uses: actions/checkout@v4
            - uses: ./.github/actions/setup-rust
              with:
                  target: x86_64-apple-darwin
            - uses: ./.github/actions/setup-rust
              with:
                  target: aarch64-apple-darwin
            - uses: baptiste0928/cargo-install@v3
              with:
                  crate: cargo-about
            - name: Build x86_64
              run: cargo build --verbose --target x86_64-apple-darwin --no-default-features --features ${{ github.event.inputs.features || inputs.features }} --release -p slint-${{ github.event.inputs.program || inputs.program }}
            - name: Build aarch64
              run: cargo build --verbose --target aarch64-apple-darwin --no-default-features --features ${{ github.event.inputs.features || inputs.features }} --release -p slint-${{ github.event.inputs.program || inputs.program }}
            - name: Create artifact directory
              run: |
                  mkdir -p slint-${{ github.event.inputs.program || inputs.program }}
                  cd slint-${{ github.event.inputs.program || inputs.program }}
                  lipo -create -output ./slint-${{ github.event.inputs.program || inputs.program }} ../target/x86_64-apple-darwin/release/slint-${{ github.event.inputs.program || inputs.program }} ../target/aarch64-apple-darwin/release/slint-${{ github.event.inputs.program || inputs.program }}
                  install_name_tool -add_rpath @executable_path/. ./slint-${{ github.event.inputs.program || inputs.program }}
                  cd ..
                  cd tools/${{ github.event.inputs.program || inputs.program }}
                  ../../scripts/prepare_binary_package.sh ../../slint-${{ github.event.inputs.program || inputs.program }}
            - uses: ./.github/actions/codesign
              if: ${{ github.event.inputs.codesign == 'true' }}
              with:
                  binary: slint-${{ github.event.inputs.program || inputs.program }}/slint-${{ github.event.inputs.program || inputs.program }}
                  certificate: ${{ github.event.inputs.certificate }}
                  certificate_password: ${{ github.event.inputs.certificate_password }}
                  keychain_password: ${{ github.event.inputs.keychain_password }}
                  developer_id: ${{ github.event.inputs.developer_id }}
            - name: Tar artifacts to preserve permissions
              run: tar czvf slint-${{ github.event.inputs.program || inputs.program }}-macos.tar.gz slint-${{ github.event.inputs.program || inputs.program }}
            - name: Upload artifact
              uses: actions/upload-artifact@v4
              with:
                  name: slint-${{ github.event.inputs.program || inputs.program }}-macos
                  path: slint-${{ github.event.inputs.program || inputs.program }}-macos.tar.gz
