# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

name: Prepare Rust Builder
description: "Prepare Rust Build Environment"
runs:
  using: "composite"
  steps:
    - name: Cache Cargo
      uses: actions/cache@27d5ce7f107fe9357f9df03efb73ab90386fccae  # v5.0.5
      with:
        # these represent dependencies downloaded by cargo
        # and thus do not depend on the OS, arch nor rust version.
        #
        # source https://github.com/actions/cache/blob/main/examples.md#rust---cargo
        path: |
          /usr/local/cargo/bin/
          /usr/local/cargo/registry/index/
          /usr/local/cargo/registry/cache/
          /usr/local/cargo/git/db/
        key: cargo-cache3-${{ hashFiles('**/Cargo.toml') }}
        restore-keys: cargo-cache3-
    - name: Setup Rust toolchain
      shell: bash
      run: rustup install
    - name: Generate lockfile
      shell: bash
      run: cargo fetch
    - name: Install Build Dependencies
      shell: bash
      run: |
        apt-get update
        apt-get install -y protobuf-compiler
    - name: Disable debuginfo generation
      # Disable full debug symbol generation to speed up CI build and keep memory down
      # "1" means line tables only, which is useful for panic tracebacks.
      shell: bash
      run: echo "RUSTFLAGS=-C debuginfo=1" >> $GITHUB_ENV
    - name: Enable backtraces
      shell: bash
      run: echo "RUST_BACKTRACE=1" >> $GITHUB_ENV
    - name: Disable incremental compilation
      shell: bash
      run: echo CARGO_INCREMENTAL=0 >> $GITHUB_ENV
    - name: Fixup git permissions
      # https://github.com/actions/checkout/issues/766
      shell: bash
      run: git config --global --add safe.directory "$GITHUB_WORKSPACE"
