stages:
  - build
  - build_lib
  - test
  - test_lib
  - ltp
  - compat
  - release

variables:
  CARGO_HOME: $CI_PROJECT_DIR/.cargo
  LANDLOCK_CRATE_TEST_ABI: 7

.cache:
  cache:
    key: ${CI_COMMIT_REF_SLUG}-${HOST}
    paths:
      - .cargo
      - target
    policy: pull-push

.build:
  stage: build
  extends: .cache
  image: exherbo/syd-builder:${HOST}
  interruptible: true
  variables:
    - MAKEARGS: ""
  script:
    - zcat /proc/config.gz || true
    - sed -i -e '/^lto/s/=.*$/= "off"/' Cargo.toml
    - dev/install_libseccomp.sh -v main -i /usr/host || true
    - chown -R paludisbuild:paludisbuild .
    - su paludisbuild -c 'test -e ~/.cargo/env && . ~/.cargo/env; make ${MAKEARGS} release'
  rules:
    - if: $CI_COMMIT_TAG
      when: never
    - when: always

.build_lib:
  stage: build_lib
  extends: .cache
  image: exherbo/syd-builder:${HOST}
  interruptible: true
  script:
    - chown -R paludisbuild:paludisbuild .
    - su paludisbuild -c 'test -e ~/.cargo/env && . ~/.cargo/env; cd lib; make'
  rules:
    - if: $CI_COMMIT_TAG
      when: never
    - when: on_success

.test:
  stage: test
  extends: .cache
  image: exherbo/syd-builder:${HOST}
  interruptible: true
  script:
    - cave sync || true
    - cave resolve jq socat -zx || true
    - sed -i -e '/^lto/s/=.*$/= "off"/' Cargo.toml
    - dev/install_libseccomp.sh -v main -i /usr/host || true
    - chown -R paludisbuild:paludisbuild .
    - chmod -R go= .
    - su paludisbuild -c 'test -e ~/.cargo/env && . ~/.cargo/env; env LANDLOCK_CRATE_TEST_ABI=${LANDLOCK_CRATE_TEST_ABI} make distcheck'
  after_script:
    - rm -rf syd_test* || true
  rules:
    - if: $CI_COMMIT_TAG
      when: never
    - when: on_success

.test_lib:
  stage: test_lib
  extends: .cache
  image: exherbo/syd-builder:${HOST}
  interruptible: true
  script:
    - cave sync || true
    - cave resolve check yajl -zx || true
    - chown -R paludisbuild:paludisbuild .
    - chmod -R go= .
    - su paludisbuild -c 'test -e ~/.cargo/env && . ~/.cargo/env; export PATH=$PWD/target/$(rustc --print=host-tuple)/release:$PATH; cd lib; make check'
  rules:
    - if: $CI_COMMIT_TAG
      when: never
    - when: on_success

.ltp:
  stage: ltp
  extends: .cache
  image: exherbo/syd-builder:${HOST}
  interruptible: true
  script:
    - sed -i -e '/^lto/s/=.*$/= "off"/' Cargo.toml
    - dev/install_libseccomp.sh -v main -i /usr/host || true
    - chown -R paludisbuild:paludisbuild .
    - su paludisbuild -c 'test -e ~/.cargo/env && . ~/.cargo/env; env PATH="$PWD/target/$(rustc --print=host-tuple)/release:$PATH" ./dev/ltp-test.sh'
  rules:
    - if: $CI_COMMIT_TAG
      when: never
    - when: on_success

.compat:
  stage: compat
  extends: .cache
  image: exherbo/syd-builder:${HOST}
  interruptible: true
  script:
    - sed -i -e '/^lto/s/=.*$/= "off"/' Cargo.toml
    - dev/install_libseccomp.sh -v main -i /usr/host || true
    - chown -R paludisbuild:paludisbuild .
    - su paludisbuild -c 'test -e ~/.cargo/env && . ~/.cargo/env; env PATH="$PWD/target/$(rustc --print=host-tuple)/release:$PATH" ./dev/gnulib-test.sh 30'
  rules:
    - if: $CI_COMMIT_TAG
      when: never
    - when: on_success

.release:
  stage: release
  image: exherbo/syd-builder:${HOST}
  interruptible: true
  rules:
    - if: $CI_COMMIT_TAG =~ /^v.+/
  script:
    - dev/install_libseccomp.sh -v main -i /usr/host || true
    - test -e ~/.cargo/env && . ~/.cargo/env
    - test -e ~/.venv/bin/activate && . ~/.venv/bin/activate
    - rm -rf target
    - mkdir -p syd-${CI_COMMIT_TAG#v}-${HOST}/bin
    - cp -ax src/esyd.sh ./README.md ./ChangeLog.md ./COPYING syd-${CI_COMMIT_TAG#v}-${HOST}/
    - |
      LIBSECCOMP_LINK_TYPE=static \
      LIBSECCOMP_LIB_PATH=$(pkg-config --variable=libdir libseccomp || echo /usr/lib) \
      RUSTFLAGS="-g -Ctarget-feature=+crt-static" \
      cargo build --release --locked --features trusted --target=$(rustc --print=host-tuple) -j${RUNNER_BUILD_JOBS:-5}
    - target/$(rustc --print=host-tuple)/release/syd -V
    - target/$(rustc --print=host-tuple)/release/syd --check
    - |
      for bin in $(find target/$(rustc --print=host-tuple)/release -mindepth 1 -maxdepth 1 -type f -executable -name 'syd*'); do
        file -L "${bin}"
        size "${bin}"
        chmod 755 "${bin}"
        mv "${bin}" "syd-${CI_COMMIT_TAG#v}-${HOST}/bin/"
      done
    - tar --owner=0 --group=0 -cJvpf ./syd-${CI_COMMIT_TAG#v}-${HOST}.tar.xz syd-${CI_COMMIT_TAG#v}-${HOST}
    - sha512sum ./syd-${CI_COMMIT_TAG#v}-${HOST}.tar.xz > ./syd-${CI_COMMIT_TAG#v}-${HOST}.tar.xz.sha512sum
    - echo "${GPG_PRIVATE_KEY}"|base64 -d > private.gpg
    - gpg --import private.gpg
    - gpg --default-key F60FF478 --sign --armor --output ./syd-${CI_COMMIT_TAG#v}-${HOST}.tar.xz.sha512sum.asc --detach-sig ./syd-${CI_COMMIT_TAG#v}-${HOST}.tar.xz.sha512sum
    - s3cmd --host=${CELLAR_ADDON_HOST} --host-bucket=${CELLAR_ADDON_HOST} --access_key=${CELLAR_ADDON_KEY_ID} --secret_key=${CELLAR_ADDON_KEY_SECRET} -P -M put ./syd-${CI_COMMIT_TAG#v}-${HOST}.tar.xz ./syd-${CI_COMMIT_TAG#v}-${HOST}.tar.xz.sha512sum ./syd-${CI_COMMIT_TAG#v}-${HOST}.tar.xz.sha512sum.asc s3://${EXHERBO_CELLAR_HOSTNAME}/sydbox/

build:aarch64:
  tags:
    - aarch64
  variables:
    HOST: aarch64-unknown-linux-gnueabi
    MAKEARGS: "CARGOFEATS=oci,uring"
  extends: .build

build_lib:aarch64:
  tags:
    - aarch64
  needs: ['build:aarch64']
  variables:
    HOST: aarch64-unknown-linux-gnueabi
  extends: .build_lib

test:aarch64:
  tags:
    - aarch64
  needs: ['build:aarch64']
  variables:
    HOST: aarch64-unknown-linux-gnueabi
  extends: .test

test_lib:aarch64:
  tags:
    - aarch64
  needs: ['build:aarch64', 'build_lib:aarch64']
  variables:
    HOST: aarch64-unknown-linux-gnueabi
  extends: .test_lib

ltp:aarch64:
  tags:
    - aarch64
  needs: ['build:aarch64']
  variables:
    HOST: aarch64-unknown-linux-gnueabi
  extends: .ltp

compat:aarch64:
  tags:
    - aarch64
  needs: ['build:aarch64']
  variables:
    HOST: aarch64-unknown-linux-gnueabi
  extends: .compat

release:aarch64:
  tags:
    - aarch64
  extends: .release
  variables:
    HOST: aarch64-unknown-linux-gnueabi

build:armv7:
  tags:
    - armv7
  variables:
    HOST: armv7-unknown-linux-gnueabihf
  extends: .build

build_lib:armv7:
  tags:
    - armv7
  needs: ['build:armv7']
  variables:
    HOST: armv7-unknown-linux-gnueabihf
  extends: .build_lib

test:armv7:
  tags:
    - armv7
  needs: ['build:armv7']
  variables:
    HOST: armv7-unknown-linux-gnueabihf
  extends: .test

test_lib:armv7:
  tags:
    - armv7
  needs: ['build:armv7', 'build_lib:armv7']
  variables:
    HOST: armv7-unknown-linux-gnueabihf
  extends: .test_lib

ltp:armv7:
  tags:
    - armv7
  needs: ['build:armv7']
  variables:
    HOST: armv7-unknown-linux-gnueabihf
  extends: .ltp

compat:armv7:
  tags:
    - armv7
  needs: ['build:armv7']
  variables:
    HOST: armv7-unknown-linux-gnueabihf
  extends: .compat

release:armv7:
  tags:
    - armv7
  extends: .release
  variables:
    HOST: armv7-unknown-linux-gnueabihf

build:x86_64:
  tags:
    - x86_64
  variables:
    HOST: x86_64-pc-linux-gnu
    MAKEARGS: "CARGOFEATS=oci,uring"
  extends: .build

build_lib:x86_64:
  tags:
    - x86_64
  needs: ['build:x86_64']
  variables:
    HOST: x86_64-pc-linux-gnu
  extends: .build_lib

test:x86_64:
  tags:
    - x86_64
  needs: ['build:x86_64']
  variables:
    HOST: x86_64-pc-linux-gnu
  extends: .test

test_lib:x86_64:
  tags:
    - x86_64
  needs: ['build:x86_64', 'build_lib:x86_64']
  variables:
    HOST: x86_64-pc-linux-gnu
  extends: .test_lib

ltp:x86_64:
  tags:
    - x86_64
  needs: ['build:x86_64']
  variables:
    HOST: x86_64-pc-linux-gnu
  extends: .ltp

compat:x86_64:
  tags:
    - x86_64
  needs: ['build:x86_64']
  variables:
    HOST: x86_64-pc-linux-gnu
  extends: .compat

release:x86_64:
  tags:
    - x86_64
  extends: .release
  variables:
    HOST: x86_64-pc-linux-gnu

build:s390x:
  tags:
    - s390x
  variables:
    HOST: s390x-unknown-linux-gnu
    MAKEARGS: "CARGOFEATS=oci,uring"
  extends: .build

build_lib:s390x:
  tags:
    - s390x
  needs: ['build:s390x']
  variables:
    HOST: s390x-unknown-linux-gnu
  extends: .build_lib

test:s390x:
  tags:
    - s390x
  needs: ['build:s390x']
  variables:
    HOST: s390x-unknown-linux-gnu
  extends: .test

test_lib:s390x:
  tags:
    - s390x
  needs: ['build:s390x', 'build_lib:s390x']
  variables:
    HOST: s390x-unknown-linux-gnu
  extends: .test_lib

ltp:s390x:
  tags:
    - s390x
  needs: ['build:s390x']
  variables:
    HOST: s390x-unknown-linux-gnu
  extends: .ltp

compat:s390x:
  tags:
    - s390x
  needs: ['build:s390x']
  variables:
    HOST: s390x-unknown-linux-gnu
  extends: .compat

release:s390x:
  tags:
    - s390x
  extends: .release
  variables:
    HOST: s390x-unknown-linux-gnu
