name: build

on:
  push:
    branches:
    - '**'
  workflow_dispatch:
    branches:
    - '**'
  pull_request:
    branches:
    - master

jobs:
  rust-n-go:
    runs-on: ${{ matrix.os }}

    strategy:
      matrix:
        os: [ ubuntu-latest, windows-latest, macos-latest ]

    steps:
    - uses: actions/checkout@v2

    - uses: actions/cache@v2
      with:
        path: |
          ~/.cargo/registry
          **/Cargo.lock
          **/bindings/rust/target
        key: ${{ runner.os }}-cargo-21
        restore-keys: ${{ runner.os }}-cargo-20

    - name: Environment
      shell: bash
      run: |
        lscpu 2>/dev/null && echo --- || true
        env | sort

    - name: Rust
      shell: bash
      run: |
        rustc --version --verbose
        cd bindings/rust
        if [ "$GITHUB_EVENT_NAME" != "pull_request" ]; then
            cargo update
        fi
        cargo test --release
        cargo test --release --features=no-threads
        [ `uname -s` = "Linux" ] && ./publish.sh --dry-run
        cargo clean -p blst
        cargo clean -p blst --release
        rm -rf target/.rustc_info.json
        rm -rf target/package
        rm -rf target/{debug,release}/incremental
        rm -rf ~/.cargo/registry/src
        rm -rf ~/.cargo/registry/index/*/.cache

    - name: Go
      shell: bash
      run: |
        go version
        if ! (grep -q -e '^flags.*\badx\b' /proc/cpuinfo) 2>/dev/null; then
            export CGO_CFLAGS="-O -D__BLST_PORTABLE__"
        fi
        cd bindings/go
        go test -test.v

  misc-ubuntu-latest:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2

    - uses: actions/cache@v2
      with:
        path: ~/swig
        key: ${{ runner.os }}-swig-github

    - uses: actions/setup-java@v1
      with:
        java-version: 11

    - uses: actions/setup-node@v1
      with:
        node-version: '12.x'

    - name: Environment
      run: |
        lscpu
        echo ---
        env | sort

    - name: Python
      run: if [ -x bindings/python/run.me ]; then bindings/python/run.me; fi

    - name: Java
      run: if [ -x bindings/java/run.me ]; then bindings/java/run.me; fi

    - name: Node.js
      run: |
        node_js=bindings/node.js
        if [ -x $node_js/run.me ]; then
            if [ ! -x ~/swig/bin/swig ]; then
              ( git clone https://github.com/swig/swig;
                cd swig;
                ./autogen.sh;
                ./configure --prefix=$HOME/swig;
                make;
                make install;
                (cd ~/swig/share/swig && ln -s `ls` current)
              )
            fi
            env PATH=~/swig/bin:$PATH SWIG_LIB=~/swig/share/swig/current \
                $node_js/run.me
        fi
    - name: node-gyp
      run: |
        node_js=bindings/node.js
        if [ -f $node_js/binding.gyp -a -f $node_js/blst_wrap.cpp ]; then
            npm install --global node-gyp || true
            if which node-gyp > /dev/null 2>&1; then
              ( export PATH=~/swig/bin:$PATH SWIG_LIB=~/swig/share/swig/current;
                cd $node_js;
                node-gyp configure;
                node-gyp build;
                env NODE_PATH=build/Release: node runnable.js;
              )
            fi
        fi
    - name: TypeScript
      run: |
        node_js=bindings/node.js
        if [ -f $node_js/blst.hpp.ts -a -f $node_js/blst.node ]; then
            npm install --global typescript || true
            if which tsc > /dev/null 2>&1; then
              ( cd $node_js;
                npm install @types/node
                tsc runnable.ts;
                env NODE_PATH=.: node runnable.js;
              )
           fi
        fi
