---
name: Linux

on:
  pull_request:
    paths:
      - '**.c'
      - '**.h'
      - '**.sh'
      - .github/workflows/linux.yml
      - '**.am'
      - doc/**
      - configure.ac
  push:

permissions:
  contents: read  #  to fetch code (actions/checkout)


jobs:
  build:
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@v3
      - run: .github/setup-linux.sh
      - run: .github/build.sh dist
      - name: Upload test logs
        uses: actions/upload-artifact@v3
        if: failure()
        with:
          name: ubuntu-test-logs
          path: |
            tests/*.log
            src/tests/unittests/*.log
      - uses: actions/cache@v3
        id: cache-build
        with:
          path: ./*
          key: ${{ runner.os }}-${{ github.sha }}
      - name: Upload build artifacts
        uses: actions/upload-artifact@v3
        with:
          name: opensc-build
          path:
            opensc*.tar.gz

  valgrind:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - run: .github/setup-linux.sh debug
      - run: .github/build.sh valgrind
      - name: Upload test logs
        uses: actions/upload-artifact@v3
        if: failure()
        with:
          name: ubuntu-valgrind-logs
          path: |
            tests/*.log
            src/tests/unittests/*.log

  build-no-shared:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - run: .github/setup-linux.sh
      - run: .github/build.sh no-shared valgrind

  build-ix86:
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@v3
      - run: .github/setup-linux.sh ix86
      - run: .github/build.sh ix86

  build-mingw:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - run: .github/setup-linux.sh mingw
      - run: .github/build.sh mingw
      - name: Cache build artifacts
        uses: actions/upload-artifact@v3
        with:
          name: opensc-build-mingw
          path:
            win32/Output/OpenSC*.exe

  build-mingw32:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - run: .github/setup-linux.sh mingw32
      - run: .github/build.sh mingw32
      - name: Cache build artifacts
        uses: actions/upload-artifact@v3
        with:
          name: opensc-build-mingw32
          path:
            win32/Output/OpenSC*.exe

  build-piv-sm:
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@v3
      - run: .github/setup-linux.sh
      - run: .github/build.sh piv-sm dist
      - name: Upload test logs
        uses: actions/upload-artifact@v3
        if: failure()
        with:
          name: ubuntu-piv-sm-test-logs
          path: |
            tests/*.log
            src/tests/unittests/*.log
      - uses: actions/cache@v3
        id: cache-build
        with:
          path: ./*
          key: ${{ runner.os }}-piv-sm-${{ github.sha }}

  test-piv:
    runs-on: ubuntu-20.04
    needs: [build]
    steps:
      - uses: actions/checkout@v3
      - uses: actions/cache@v3
        id: cache-build
        with:
          path: ./*
          key: ${{ runner.os }}-${{ github.sha }}
      - run: .github/setup-linux.sh piv
      - run: .github/test-piv.sh
      - run: .github/test-piv.sh valgrind

  test-piv-sm:
    runs-on: ubuntu-20.04
    needs: [build-piv-sm]
    steps:
      - uses: actions/checkout@v3
      - uses: actions/cache@v3
        id: cache-build
        with:
          path: ./*
          key: ${{ runner.os }}-piv-sm-${{ github.sha }}
      - run: .github/setup-linux.sh piv
      - run: .github/test-piv.sh
      - run: .github/test-piv.sh valgrind

  test-isoapplet-v0:
    runs-on: ubuntu-20.04
    needs: [build]
    steps:
      - uses: actions/checkout@v3
      - uses: actions/cache@v3
        id: cache-build
        with:
          path: ./*
          key: ${{ runner.os }}-${{ github.sha }}
      - run: .github/setup-linux.sh isoapplet
      - run: .github/test-isoapplet.sh v0
      - run: .github/test-isoapplet.sh v0 valgrind

  test-isoapplet-v1:
    runs-on: ubuntu-20.04
    needs: [build]
    steps:
      - uses: actions/checkout@v3
      - uses: actions/cache@v3
        id: cache-build
        with:
          path: ./*
          key: ${{ runner.os }}-${{ github.sha }}
      - run: .github/setup-linux.sh isoapplet
      - run: .github/test-isoapplet.sh v1
      - run: .github/test-isoapplet.sh v1 valgrind

  test-gidsapplet:
    runs-on: ubuntu-20.04
    needs: [build]
    steps:
      - uses: actions/checkout@v3
      - uses: actions/cache@v3
        id: cache-build
        with:
          path: ./*
          key: ${{ runner.os }}-${{ github.sha }}
      - run: .github/setup-linux.sh gidsapplet
      - run: .github/test-gidsapplet.sh
      - run: .github/test-gidsapplet.sh valgrind

  test-openpgp:
    runs-on: ubuntu-20.04
    needs: [build]
    steps:
      - uses: actions/checkout@v3
      - uses: actions/cache@v3
        id: cache-build
        with:
          path: ./*
          key: ${{ runner.os }}-${{ github.sha }}
      - run: .github/setup-linux.sh openpgp
      - run: .github/test-openpgp.sh
      - run: .github/test-openpgp.sh valgrind

  build-clang-tidy:
    runs-on: ubuntu-20.04
    needs: [build]
    steps:
      - uses: actions/checkout@v3
      - uses: actions/cache@v3
        id: cache-build
        with:
          path: ./*
          key: ${{ runner.os }}-${{ github.sha }}
      - run: .github/setup-linux.sh clang-tidy
      - run: .github/build.sh

  test-cac:
    runs-on: ubuntu-20.04
    needs: [build]
    steps:
      - uses: actions/checkout@v3
      - uses: actions/cache@v3
        id: cache-build
        with:
          path: ./*
          key: ${{ runner.os }}-${{ github.sha }}
      - run: .github/setup-linux.sh cac
      - run: .github/test-cac.sh
      - run: .github/test-cac.sh valgrind

  test-oseid:
    runs-on: ubuntu-20.04
    needs: [build]
    steps:
      - uses: actions/checkout@v3
      - uses: actions/cache@v3
        id: cache-build
        with:
          path: ./*
          key: ${{ runner.os }}-${{ github.sha }}
      - run: .github/setup-linux.sh oseid
      - run: .github/test-oseid.sh
      - run: .github/test-oseid.sh valgrind

  ############################################
  ## Ubuntu 22.04 with OpenSSL 3.0 pipeline ##
  ############################################
  build-ubuntu-22:
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v3
      - run: .github/setup-linux.sh
      - run: .github/build.sh dist valgrind
      - uses: actions/upload-artifact@v3
        if: failure()
        with:
          name: ubuntu-22-test-logs
          path: |
            tests/*.log
            src/tests/unittests/*.log
      - uses: actions/cache@v3
        id: cache-build
        if: ${{ success() }}
        with:
          path: ./*
          key: ${{ runner.os }}-22-${{ github.sha }}
      - name: Upload artifacts
        uses: actions/upload-artifact@v3
        if: ${{ success() }}
        with:
          name: opensc-build
          path:
            opensc*.tar.gz

  build-ubuntu-22-piv-sm:
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v3
      - run: .github/setup-linux.sh
      - run: .github/build.sh piv-sm dist valgrind
      - uses: actions/upload-artifact@v3
        if: failure()
        with:
          name: ubuntu-22-piv-sm-test-logs
          path: |
            tests/*.log
            src/tests/unittests/*.log
      - uses: actions/cache@v3
        id: cache-build
        if: ${{ success() }}
        with:
          path: ./*
          key: ${{ runner.os }}-22-piv-sm-${{ github.sha }}

  test-piv-sm-ubuntu-22:
    runs-on: ubuntu-22.04
    needs: [build-ubuntu-22-piv-sm]
    steps:
      - uses: actions/checkout@v3
      - uses: actions/cache@v3
        id: cache-build
        with:
          path: ./*
          key: ${{ runner.os }}-22-piv-sm-${{ github.sha }}
      - run: .github/setup-linux.sh piv
      - run: .github/test-piv.sh
      - run: .github/test-piv.sh valgrind

  test-cac-ubuntu-22:
    runs-on: ubuntu-22.04
    needs: [build-ubuntu-22]
    steps:
      - uses: actions/checkout@v3
      - uses: actions/cache@v3
        id: cache-build
        with:
          path: ./*
          key: ${{ runner.os }}-22-${{ github.sha }}
      - run: .github/setup-linux.sh cac
      - run: .github/test-cac.sh
      - run: .github/test-cac.sh valgrind

  test-oseid-ubuntu-22:
    runs-on: ubuntu-22.04
    needs: [build-ubuntu-22]
    steps:
      - uses: actions/checkout@v3
      - uses: actions/cache@v3
        id: cache-build
        with:
          path: ./*
          key: ${{ runner.os }}-22-${{ github.sha }}
      - run: .github/setup-linux.sh oseid
      - run: .github/test-oseid.sh
      - run: .github/test-oseid.sh valgrind

  #######################
  ## LibreSSL pipeline ##
  #######################
  build-libressl:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - run: .github/setup-linux.sh libressl
      - run: .github/build.sh dist libressl valgrind
      - uses: actions/upload-artifact@v3
        if: failure()
        with:
          name: libressl-logs
          path: |
            config.log
            tests/*.log
            src/tests/unittests/*.log
      - uses: actions/cache@v3
        id: cache-build
        if: ${{ success() }}
        with:
          path: ./*
          key: ${{ runner.os }}-libressl-${{ github.sha }}

  test-cac-libressl:
    runs-on: ubuntu-latest
    needs: [build-libressl]
    steps:
      - uses: actions/checkout@v3
      - uses: actions/cache@v3
        id: cache-build
        with:
          path: ./*
          key: ${{ runner.os }}-libressl-${{ github.sha }}
      - run: .github/setup-linux.sh cac libressl
      - run: .github/test-cac.sh
      - run: .github/test-cac.sh valgrind

  test-oseid-libressl:
    runs-on: ubuntu-latest
    needs: [build-libressl]
    steps:
      - uses: actions/checkout@v3
      - uses: actions/cache@v3
        id: cache-build
        with:
          path: ./*
          key: ${{ runner.os }}-libressl-${{ github.sha }}
      - run: .github/setup-linux.sh oseid libressl
      - run: .github/test-oseid.sh
      - run: .github/test-oseid.sh valgrind

  ####################
  ## Push artifacts ##
  ####################
  push-artifacts:
    runs-on: ubuntu-latest
    needs: [build, build-mingw]
    steps:
      - uses: actions/checkout@v3
      - uses: actions/cache@v3
        id: cache-build
        with:
          path: ./*
          key: ${{ runner.os }}-${{ github.sha }}
      - name: Pull mingw build artifacts
        uses: actions/download-artifact@v3
        with:
          name: opensc-build-mingw
      - run: git config --global user.email "builds@github.com"
      - run: git config --global user.name "Github Actions";
      - run: .github/push_artifacts.sh "Github Actions ${GITHUB_REF}"
        if: ${{ github.repository == 'OpenSC/OpenSC' }}
