on:
  push:
    branches:
      - master
      - edge
      - 'topic/**'
  pull_request:
name: Build binaries
env:
  AWS_DEFAULT_REGION: eu-west-1
jobs:
  build:
    name: Build and deploy
    strategy:
      fail-fast: false
      matrix: # Build debug and production
        os: [ubuntu-latest, macos-latest, windows-latest]
    runs-on: ${{ matrix.os }}
    steps:
      - name: Setup Python
        uses: actions/setup-python@v4
        with:
          python-version: '3.10'
      - name: Set git autocrlf
        shell: bash
        run: git config --global core.autocrlf input
      - name: Install iconv and gmp (Windows only)
        if: ${{ runner.os == 'Windows' }}
        uses: msys2/setup-msys2@v2
        with:
          path-type: inherit
          update: true
          install: >-
            mingw64/mingw-w64-x86_64-libiconv
            mingw64/mingw-w64-x86_64-gmp
            mingw64/mingw-w64-x86_64-diffutils
      - name: Get gnatdoc
        uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - name: Get VSS
        uses: actions/checkout@v3
        with:
          repository: AdaCore/VSS
          path: subprojects/VSS
      - name: Get Markdown
        uses: actions/checkout@v3
        with:
          repository: AdaCore/markdown
          path: subprojects/markdown
      - name: Get Langkit
        uses: actions/checkout@v3
        with:
          repository: AdaCore/langkit
          path: subprojects/langkit
      - name: Get gpr-unit-provider
        uses: actions/checkout@v3
        with:
          repository: AdaCore/gpr-unit-provider
          path: subprojects/gpr-unit-provider
      - uses: actions/cache@v3
        with:
          path: ./cached_gnat
          key: ${{ runner.os }}-alire-2021
          restore-keys: ${{ runner.os }}-alire-2021
      - name: Get GNAT toolchain with alire
        uses: alire-project/setup-alire@v2
        with:
          toolchain: gnat_native^12 gprbuild^22
          toolchain_dir: ./cached_gnat
      - name: Build (Windows)
        if: ${{ runner.os == 'Windows' }}
        shell: msys2 {0}
        env:
          AWS_ACCESS_KEY_ID: ${{secrets.GHA_CACHE_ACCESS_KEY_ID}}
          AWS_SECRET_ACCESS_KEY: ${{secrets.GHA_CACHE_SECRET}}
        run: |
          # This is to avoid locking .sh on win that prevents its updating
          cp .github/workflows/build-binaries.sh .github/workflows/build-binaries.sh_
          .github/workflows/build-binaries.sh_ debug ${{ runner.os }}
      - name: Build (non-Windows)
        env:
          AWS_ACCESS_KEY_ID: ${{secrets.GHA_CACHE_ACCESS_KEY_ID}}
          AWS_SECRET_ACCESS_KEY: ${{secrets.GHA_CACHE_SECRET}}
        if: ${{ runner.os != 'Windows' }}
        run: .github/workflows/build-binaries.sh debug ${{ runner.os }}
      - name: AWS S3 upload
        if: ${{ github.ref == 'refs/heads/master' }}
        env:
          AWS_ACCESS_KEY_ID: ${{secrets.GHA_CACHE_ACCESS_KEY_ID}}
          AWS_SECRET_ACCESS_KEY: ${{secrets.GHA_CACHE_SECRET}}
          AWS_DEFAULT_REGION: eu-west-1
        run: |
            aws s3 cp . s3://adacore-gha-tray-eu-west-1/gnatdoc/ --recursive --exclude "*" --include "*.tar.gz" --sse=AES256
            aws s3 ls s3://adacore-gha-tray-eu-west-1/gnatdoc/
