on:
  push:
    tags:
    - 'v*'

name: Build Release Binaries

jobs:
  linux-x86-64:
    name: Linux x86-64
    runs-on: ubuntu-20.04
    steps:
    - name: Checkout sources
      uses: actions/checkout@v2
      with:
        submodules: recursive
    - run: git clone --depth 1 --branch solana-rustc/13.0-2021-08-08 https://github.com/solana-labs/llvm-project.git
    - name: Install Ninja
      uses: llvm/actions/install-ninja@main
    - run: cmake -G Ninja -DLLVM_ENABLE_ASSERTIONS=On -DLLVM_ENABLE_TERMINFO=Off
        -DLLVM_ENABLE_LIBXML2=Off -DLLVM_ENABLE_ZLIB=Off
        -DLLVM_ENABLE_PROJECTS='clang;lld'
        -DLLVM_TARGETS_TO_BUILD='WebAssembly;BPF'
        -DCMAKE_BUILD_TYPE=MinSizeRel -DCMAKE_INSTALL_PREFIX=../llvm13.0 llvm
      working-directory: ./llvm-project/
    - run: cmake --build . --target install
      working-directory: ./llvm-project/
    - run: tar Jcf ./llvm13.0-linux-x86-64.tar.xz ./llvm13.0/
    - name: Upload llvm
      uses: svenstaro/upload-release-action@v2
      with:
        repo_token: ${{ secrets.GITHUB_TOKEN }}
        file: llvm13.0-linux-x86-64.tar.xz
        asset_name: llvm13.0-linux-x86-64.tar.xz
        tag: ${{ github.ref }}
    - name: Add LLVM to Path
      run: echo "$(pwd)/llvm13.0/bin" >> $GITHUB_PATH
    - name: Rust stable
      run: rustup default 1.63.0
    - name: Build
      run: cargo build --verbose --release
    - name: Run tests
      run: cargo test --workspace --verbose --release
    - name: Upload binary
      uses: svenstaro/upload-release-action@v2
      with:
        repo_token: ${{ secrets.GITHUB_TOKEN }}
        file: target/release/solang
        asset_name: solang-linux-x86-64
        tag: ${{ github.ref }}

  linux-arm64:
    name: Linux arm64
    runs-on: linux-arm64
    if: ${{ github.repository_owner == 'hyperledger' }}
    steps:
    - name: Checkout sources
      uses: actions/checkout@v2
      with:
        submodules: recursive
    - run: git clone --depth 1 --branch solana-rustc/13.0-2021-08-08 https://github.com/solana-labs/llvm-project.git
    - run: cmake -G Ninja -DLLVM_ENABLE_ASSERTIONS=On -DLLVM_ENABLE_TERMINFO=Off
        -DLLVM_ENABLE_LIBXML2=Off -DLLVM_ENABLE_ZLIB=Off
        -DLLVM_ENABLE_PROJECTS='clang;lld'
        -DLLVM_TARGETS_TO_BUILD='WebAssembly;BPF'
        -DCMAKE_BUILD_TYPE=MinSizeRel -DCMAKE_INSTALL_PREFIX=../llvm13.0 llvm
      working-directory: ./llvm-project/
    - run: cmake --build . --target install
      working-directory: ./llvm-project/
      # compression takes a long time on linux-arm64, use multithreaded compression
    - run: tar cf ./llvm13.0-linux-arm64.tar.xz -I 'xz -T 0' ./llvm13.0/
    - name: Upload llvm
      uses: svenstaro/upload-release-action@v2
      with:
        repo_token: ${{ secrets.GITHUB_TOKEN }}
        file: llvm13.0-linux-arm64.tar.xz
        asset_name: llvm13.0-linux-arm64.tar.xz
        tag: ${{ github.ref }}
    - name: Add LLVM to Path
      run: echo "$(pwd)/llvm13.0/bin" >> $GITHUB_PATH
    - name: Rust stable
      run: rustup default 1.63.0
    - name: Build
      run: cargo build --verbose --release
    - name: Run tests
      run: cargo test --workspace --verbose --release
    - name: Upload binary
      uses: svenstaro/upload-release-action@v2
      with:
        repo_token: ${{ secrets.GITHUB_TOKEN }}
        file: target/release/solang
        asset_name: solang-linux-arm64
        tag: ${{ github.ref }}

  windows:
    name: Windows
    runs-on: windows-latest
    steps:
    - name: Checkout sources
      uses: actions/checkout@v2
      with:
        submodules: recursive
    - uses: actions-rs/toolchain@v1
      with:
        profile: minimal
        default: true
        toolchain: 1.63.0
    - run: git clone --depth 1 --branch solana-rustc/13.0-2021-08-08 https://github.com/solana-labs/llvm-project.git
      # We may not have enough space to compile llvm, see https://github.com/actions/virtual-environments/issues/326
      working-directory: C:\
    - name: Setup Windows
      uses: llvm/actions/setup-windows@main
      with:
        arch: amd64
    - name: Install Ninja
      uses: llvm/actions/install-ninja@main
    - run: cmake -G Ninja -DLLVM_ENABLE_ASSERTIONS=On -DLLVM_ENABLE_TERMINFO=Off
        -DLLVM_ENABLE_LIBXML2=Off -DLLVM_ENABLE_ZLIB=Off
        '-DLLVM_ENABLE_PROJECTS=clang;lld'
        -DCMAKE_BUILD_TYPE=MinSizeRel -DCMAKE_INSTALL_PREFIX=C:/llvm13.0 llvm
      working-directory: C:\llvm-project
    - run: cmake --build . --target install
      working-directory: C:\llvm-project
    - run: Compress-Archive -Path C:\llvm13.0 -DestinationPath C:\llvm13.0-win.zip
    - name: Upload llvm
      uses: svenstaro/upload-release-action@v2
      with:
        repo_token: ${{ secrets.GITHUB_TOKEN }}
        file: C:\llvm13.0-win.zip
        asset_name: llvm13.0-win.zip
        tag: ${{ github.ref }}
    - name: Add LLVM to Path
      run: echo "c:\llvm13.0\bin" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8
    - name: Build
      run: cargo build --release --verbose
    - name: Run tests
      run: cargo test --workspace --release --verbose
    - name: Upload binary
      uses: svenstaro/upload-release-action@v2
      with:
        repo_token: ${{ secrets.GITHUB_TOKEN }}
        file: target/release/solang.exe
        asset_name: solang.exe
        tag: ${{ github.ref }}

  mac-arm:
    name: Mac Arm
    runs-on: [self-hosted, macOS, ARM64, MacStadium]
    if: ${{ github.repository_owner == 'hyperledger' }}
    steps:
    - name: Checkout sources
      uses: actions/checkout@v3
      with:
        submodules: recursive
    - uses: actions-rs/toolchain@v1
      with:
          toolchain: 1.63.0
          default: true
          profile: minimal
    - run: git clone --depth 1 --branch solana-rustc/13.0-2021-08-08 https://github.com/solana-labs/llvm-project.git
    - name: Install Ninja
      uses: llvm/actions/install-ninja@main
    - run: cmake -G Ninja -DLLVM_ENABLE_ASSERTIONS=On -DLLVM_ENABLE_TERMINFO=Off
        -DLLVM_ENABLE_LIBXML2=Off -DLLVM_ENABLE_ZLIB=Off
        -DLLVM_ENABLE_PROJECTS='clang;lld'
        -DLLVM_TARGETS_TO_BUILD='WebAssembly;BPF'
        -DCMAKE_BUILD_TYPE=MinSizeRel -DCMAKE_INSTALL_PREFIX=../llvm13.0 llvm
      working-directory: ./llvm-project/
    - run: cmake --build . --target install
      working-directory: ./llvm-project/
    - run: tar Jcf ./llvm13.0-mac-arm.tar.xz ./llvm13.0/
    - name: Upload llvm
      uses: svenstaro/upload-release-action@v2
      with:
        repo_token: ${{ secrets.GITHUB_TOKEN }}
        file: llvm13.0-mac-arm.tar.xz
        asset_name: llvm13.0-mac-arm.tar.xz
        tag: ${{ github.ref }}
    - name: Add LLVM to Path
      run: echo "$(pwd)/llvm13.0/bin" >> $GITHUB_PATH
    - name: Build
      run: cargo build --release --verbose
    - name: Run tests
      run: cargo test --workspace --release --verbose
    - name: Upload binary
      uses: svenstaro/upload-release-action@v2
      with:
        repo_token: ${{ secrets.GITHUB_TOKEN }}
        file: target/release/solang
        asset_name: solang-mac-arm
        tag: ${{ github.ref }}

  mac-intel:
    name: Mac Intel
    # The Hyperledger self-hosted intel macs have the label macos-latest
    # and run macos 12. We don't want to build llvm on macos 12, because
    # then it can't be used on macos 11.
    runs-on: macos-11
    steps:
    - name: Checkout sources
      uses: actions/checkout@v3
      with:
        submodules: recursive
    - uses: actions-rs/toolchain@v1
      with:
          toolchain: 1.63.0
          default: true
          profile: minimal
    - run: git clone --depth 1 --branch solana-rustc/13.0-2021-08-08 https://github.com/solana-labs/llvm-project.git
    - name: Install Ninja
      uses: llvm/actions/install-ninja@main
    - run: cmake -G Ninja -DLLVM_ENABLE_ASSERTIONS=On -DLLVM_ENABLE_TERMINFO=Off
        -DLLVM_ENABLE_LIBXML2=Off -DLLVM_ENABLE_ZLIB=Off
        -DLLVM_ENABLE_PROJECTS='clang;lld'
        -DLLVM_TARGETS_TO_BUILD='WebAssembly;BPF'
        -DCMAKE_BUILD_TYPE=MinSizeRel -DCMAKE_INSTALL_PREFIX=../llvm13.0 llvm
      working-directory: ./llvm-project/
    - run: cmake --build . --target install
      working-directory: ./llvm-project/
    - run: tar Jcf ./llvm13.0-mac-intel.tar.xz ./llvm13.0/
    - name: Upload llvm
      uses: svenstaro/upload-release-action@v2
      with:
        repo_token: ${{ secrets.GITHUB_TOKEN }}
        file: llvm13.0-mac-intel.tar.xz
        asset_name: llvm13.0-mac-intel.tar.xz
        tag: ${{ github.ref }}
    - name: Add LLVM to Path
      run: echo "$(pwd)/llvm13.0/bin" >> $GITHUB_PATH
    - name: Build
      run: cargo build --release --verbose
    - name: Run tests
      run: cargo test --workspace --release --verbose
    - name: Upload binary
      uses: svenstaro/upload-release-action@v2
      with:
        repo_token: ${{ secrets.GITHUB_TOKEN }}
        file: target/release/solang
        asset_name: solang-mac-intel
        tag: ${{ github.ref }}

  mac-universal:
    name: Mac Universal Binary
    runs-on: macos-latest
    needs: [mac-arm, mac-intel]
    steps:
    - run: |
        curl -L --output solang-mac-intel https://github.com/hyperledger/solang/releases/download/${GITHUB_REF/refs\/tags\//}/solang-mac-intel
        curl -L --output solang-mac-arm https://github.com/hyperledger/solang/releases/download/${GITHUB_REF/refs\/tags\//}/solang-mac-arm
        lipo -create -output solang-mac solang-mac-intel solang-mac-arm
    - name: Upload binary
      uses: svenstaro/upload-release-action@v2
      with:
        repo_token: ${{ secrets.GITHUB_TOKEN }}
        file: solang-mac
        asset_name: solang-mac
        tag: ${{ github.ref }}

  container-multiarch:
    name: Multiarch Container Image
    runs-on: linux-arm64
    if: ${{ github.repository_owner == 'hyperledger' }}
    steps:
    - name: Checkout sources
      uses: actions/checkout@v3
    - run: |
        echo ${{ secrets.GITHUB_TOKEN }} | docker login ghcr.io -u $GITHUB_ACTOR --password-stdin
        docker buildx build . --push \
          -t ghcr.io/${GITHUB_REPOSITORY}:${GITHUB_REF/refs\/tags\//} \
          --platform linux/arm64,linux/amd64 \
          --label org.opencontainers.image.description="Solidity Compiler for Solana and Substrate version $(git describe --tags)"
