include:
  - component: gitlab.gnome.org/GNOME/citemplates/release-service@master
    inputs:
      dist-job-name: "build-release-tarball"              # <1.>
      tarball-artifact-path: "${TARBALL_ARTIFACT_PATH}"   # <2.>

variables:
  # Clone test-images
  GIT_SUBMODULE_STRATEGY: recursive
  RUST_BACKTRACE: "full"
  TARBALL_ARTIFACT_PATH: "builddir/meson-dist/${CI_PROJECT_NAME}-${CI_COMMIT_TAG}.tar.xz"


.install_dependencies:
  before_script:
    - apt update
    - apt install -y git curl meson pkg-config gobject-introspection gi-docgen valac libgirepository1.0-dev python3-gi build-essential liblcms2-dev gettext clang mold bubblewrap libseccomp-dev libfontconfig-dev
    - apt-get install -y libheif-dev libheif-plugin-aomenc libheif-plugin-x265 libgtk-4-dev libcairo2-dev librsvg2-2 libjxl-dev
  artifacts:
    when: always
    paths:
      - tests/failures
      - builddir/meson-logs

build-release-tarball:
  stage: build
  image: rust:1.92-trixie
  extends: .install_dependencies
  script:
    - meson setup builddir
    - meson install -C builddir
    - apt-get install -y gawk
    - ./build-aux/publish-crates-io.py
    - meson dist -C builddir
  artifacts:
    name: "${CI_JOB_NAME}-${CI_COMMIT_REF_NAME}"
    when: always
    paths:
      - "${TARBALL_ARTIFACT_PATH}"
  rules:
    - if: $CI_COMMIT_TAG && $CI_COMMIT_REF_PROTECTED

test-x86_64:
  image: rust:1.92-trixie
  extends: .install_dependencies
  interruptible: true
  script:
    - export LD_LIBRARY_PATH=/usr/local/lib/x86_64-linux-gnu/
    # Skip isolated install for tests to test usual installation scenario
    - meson setup -Dprofile=dev -Dpython_tests=true -Dtest_skip_install=true -Dcapi_docs=true --prefix=/usr builddir
    - meson install -C builddir
    - meson test -vC builddir
    - mv builddir/libglycin/libglycin-2 libglycin-docs
    - mv builddir/libglycin/libglycin-gtk4-2 libglycin-gtk4-docs
  artifacts:
    paths:
      - libglycin-docs
      - libglycin-gtk4-docs

test-i386:
  # Use hash to force i386, lookup "MANIFEST DIGEST" here <https://hub.docker.com/r/i386/rust/tags>
  image: rust@sha256:e92287d0a1ba6cfa8ec85b6b660bfd4d6b4dc47e211b511c6cfcf277f330adc0
  extends: .install_dependencies
  interruptible: true
  # As long as 32-bit CI is so unstable
  allow_failure: true
  script:
    - export LD_LIBRARY_PATH=/usr/local/lib/i386-linux-gnu/
    - apt-get install -y ninja-build
    - meson setup -Dprofile=dev  -Dpython_tests=true -Dtest_skip_install=true -Dtest_skip_ext=avif --prefix=/usr builddir
    - meson install -C builddir
    - meson test -vC builddir

test-aarch64:
  image: rust:1.92-trixie
  tags:
    - asan-aarch64
  extends: .install_dependencies
  interruptible: true
  script:
    - export LD_LIBRARY_PATH=/usr/local/lib/aarch64-linux-gnu/
    - meson setup -Dprofile=dev -Dpython_tests=true -Dtest_skip_install=true --prefix=/usr builddir
    - meson install -C builddir
    - meson test -vC builddir

test-fedora43-loaders:
  image: 'registry.fedoraproject.org/fedora:43'
  interruptible: true
  script:
    - dnf install --assumeyes glycin-loaders cargo glib2-devel gtk4-devel lcms2-devel libseccomp-devel fontconfig-devel
    - GLYCIN_TEST_SKIP_EXT=exr cargo test -p tests -- --nocapture --test-threads 1 \
      color color_exif_orientation color_iccp_pro gray_iccp icon exif fonts editing_crop_too_large_value editing_rotation_90

cargo-semver-checks:
  image: rust
  extends: .install_dependencies
  interruptible: true
  variables:
    GIT_DEPTH: 1000
  script:
    # We need libglycin to be properly installed for pkgconf to work in libglycin-gtk4
    - meson setup -Dprofile=dev -Dglycin-loaders=false -Dglycin-thumbnailer=false --prefix=/usr builddir
    - meson install -C builddir
    - cargo install cargo-semver-checks --locked
    - cargo semver-checks -p glycin -p glycin-utils
    # Use the commit where we changed to defining all functions as unsafe
    - cargo semver-checks -p libglycin -p libglycin-gtk4 --baseline-rev 97b80be3

libglycin-rebind:
  image: rust:1.92-trixie
  extends: .install_dependencies
  interruptible: true
  variables:
    GIT_SUBMODULE_STRATEGY: recursive
    RUSTDOCFLAGS: "--cfg docsrs"
  script:
    - git submodule update --checkout
    - meson setup -Dprofile=dev -Dglycin-loaders=false -Dglycin-thumbnailer=false -Dtest_skip_install=true -Dvapi=false --prefix=/usr builddir
    - meson install -C builddir
    - cargo build --package libglycin-gtk4-rebind --package libglycin-rebind
    # dependencies for the docs
    - curl --proto '=https' --tlsv1.2 -sSf -o libglycin-rebind/gir-rustdoc.py
      https://gitlab.gnome.org/World/Rust/gir-rustdoc/-/raw/main/gir-rustdoc.py
    - apt update
    - apt install -y python3-requests
    # generate the docs
    - cd libglycin-rebind
    - cargo install rustdoc-stripper
    - ./gir/generator.py --embed-docs --no-fmt
    - rustup default nightly
    - eval $(./gir-rustdoc.py --branch=main pre-docs)
    - cd ../  # move back to the root
    - cargo doc --all-features --no-deps -p libglycin-gtk4-rebind -p libglycin-rebind -p libglycin-gtk4-rebind-sys -p libglycin-rebind-sys -p glycin -p glycin-common -p glycin-common
    - mkdir public
    - mv target/doc public/rust
  artifacts:
    paths:
      - public

cargo-fmt:
  image: rust
  interruptible: true
  script:
    - rustup component add rustfmt
    - cargo fmt --check

cargo-deny:
  image: rust
  interruptible: true
  script:
    - cargo install cargo-deny --locked
    - cargo deny --target x86_64-unknown-linux-gnu check

cargo-vet:
  image: "rust"
  allow_failure: true
  interruptible: true
  script:
    - cargo install cargo-vet
    - CARGO_BUILD_TARGET=x86_64-unknown-linux-gnu cargo vet


website:
  image: rust
  extends: .install_dependencies
  interruptible: true
  script:
    - ./docs/website/build.bash
  artifacts:
    paths:
      - public

pages:
  stage: deploy
  dependencies:
    - website
    - test-x86_64
  script:
    - mv libglycin-docs public/libglycin
    - mv libglycin-gtk4-docs public/libglycin-gtk4
  artifacts:
    paths:
      - public
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
