name: NQC CI

on:
  workflow_dispatch:
  schedule:
    # Following POSIX cron syntax, run every Monday morning at 5:30 AM UTC
    #   https://pubs.opengroup.org/onlinepubs/9699919799/utilities/crontab.html#tag_20_25_07
    - cron:   '30 5 * * 1'
  push:
    # branches: [ master ]
  pull_request:
    # branches: [ master ]

env:
  UBUNTU_BUILD_DEPENDS:        make flex bison binutils gcc g++ emscripten pandoc  stow
  UBUNTU_CROSS_BUILD_DEPENDS:  binutils-h8300-hms

  CYGWIN_BUILD_DEPENDS:        make flex bison binutils gcc-core gcc-g++  tree stow
  CYGWIN_CROSS_BUILD_DEPENDS:  h8300-hitachi-coff-toolchain

jobs:
  build_with_cross_compiler:
    name:   Build ${{ matrix.with_cross && 'with' || 'without' }} H8/300 Cross-Assembler on ${{ matrix.os }}-${{ matrix.platform }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast:  false
      matrix:
        os:            [ "ubuntu-latest", "windows-latest" ]
        with_cross:    [ true, false ]
        platform:      [ "x86_64", "x86" ]
        exclude:
          - os:            ubuntu-latest
            platform:      x86

    steps:
    - name: Get the latest from version control
      uses: actions/checkout@v4

    - name: Setup Ubuntu build environment
      if:   contains(matrix.os, 'ubuntu')
      run:  sudo apt-get --assume-yes update && sudo apt-get --assume-yes install $UBUNTU_BUILD_DEPENDS $(if ${{ matrix.with_cross }} ; then echo "$UBUNTU_CROSS_BUILD_DEPENDS"; fi)

    - name: Setup Cygwin build environment
      if:   runner.os == 'Windows'
      uses: cygwin/cygwin-install-action@v5
      with:
        platform: ${{ matrix.platform }}
        packages: ${{ env.CYGWIN_BUILD_DEPENDS }} $(if ${{ matrix.with_cross }} ; then echo "$CYGWIN_CROSS_BUILD_DEPENDS"; fi)

    - name: Execute the build
      run:  make DEFAULT_SERIAL_NAME='"/dev/ttyS0"' DEFAULT_USB_NAME='"/dev/usb/legousbtower0"' DEFAULT_PORT_NAME='"usb"'
      # Cygwin action docs – https://github.com/marketplace/actions/install-cygwin-action
    - name: Archive native build artifacts
      uses: actions/upload-artifact@v4
      with:
        name: nqc-native-${{ matrix.with_cross && 'with' || 'without' }}-cross-assembler-on-${{ matrix.os }}-${{ matrix.platform }}
        path: build/bin/
    - name: Archive JavaScript-WebAssembly build artifacts
      if:   runner.os != 'Windows'
      uses: actions/upload-artifact@v4
      with:
        name: nqc-JS-WebAssembly-${{ matrix.with_cross && 'with' || 'without' }}-cross-assembler-on-${{ matrix.os }}-${{ matrix.platform }}
        path: build/js-wasm/
    - name: Install the new build to the Stow location
      run:  make install
    - name: Show the directory tree of installed contents
      run:  tree --dirsfirst "/opt/stow/nqc"
