Skip to content

Commit

Permalink
BaseTools/Plugin: Add tool exclusion to RustEnvironmentCheck (#602)
Browse files Browse the repository at this point in the history
## Description

Allows a build wrapper to set an environment variable (chosen as the
input method for its simplicity) to exclude a list of tools.

For example, a GitHub workflow that only runs CodeQL for C code,
might need cargo installed for Rust compilation as part of the
package build, but might not need cargo tarpaulin for code coverage.
The GitHub workflow can set the environment variable on the build
step to opt out of verifying those tools.

This is not intended to be used often as most local developers and
build environments are expected to have the base set of tools needed
to build and test the code when the plugins scope is present.

- [ ] Impacts functionality?
- **Functionality** - Does the change ultimately impact how firmware
functions?
- Examples: Add a new library, publish a new PPI, update an algorithm,
...
- [ ] Impacts security?
- **Security** - Does the change have a direct security impact on an
application,
    flow, or firmware?
  - Examples: Crypto algorithm change, buffer overflow fix, parameter
    validation improvement, ...
- [ ] Breaking change?
- **Breaking change** - Will anyone consuming this change experience a
break
    in build or boot behavior?
- Examples: Add a new library class, move a module to a different repo,
call
    a function in a new library class in a pre-existing module, ...
- [ ] Includes tests?
  - **Tests** - Does the change include any explicit test code?
  - Examples: Unit tests, integration tests, robot tests, ...
- [x] Includes documentation?
- **Documentation** - Does the change contain explicit documentation
additions
    outside direct code modifications (and comments)?
- Examples: Update readme file, add feature readme file, link to
documentation
    on an a separate Web page, ...

## How This Was Tested

- Verified locally against a Rust enabled workspace
- Verified in a Mu Plus CodeQL workflow that does not have `cargo fmt`
  and `cargo tarpaulin` installed by adding the following to the build
  step (`RUST_ENV_CHECK_TOOL_EXCLUSIONS`):

```yaml
    - name: CI Build
      env:
        RUST_ENV_CHECK_TOOL_EXCLUSIONS: "cargo fmt, cargo tarpaulin"
        STUART_CODEQL_PATH: ${{ steps.cache_key_gen.outputs.codeql_cli_ext_dep_dir }}
      run: stuart_ci_build -c .pytool/CISettings.py -t DEBUG -p ${{ matrix.package }} -a ${{ matrix.archs }} TOOL_CHAIN_TAG=${{ matrix.tool_chain_tag }} --codeql
```

## Integration Instructions

Add `RUST_ENV_CHECK_TOOL_EXCLUSIONS` as a shell variable if any tools
need to be excluded from the Rust environment check. The variable
contains
a comma separated list of tool names.

Signed-off-by: Michael Kubacki <michael.kubacki@microsoft.com>
  • Loading branch information
makubacki authored and kenlautner committed Dec 18, 2023
1 parent 87d8333 commit 1daaa47
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion BaseTools/Plugin/RustEnvironmentCheck/RustEnvironmentCheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,21 @@
# fail much later during firmware code compilation when Rust tools are invoked
# with messages that are ambiguous or difficult to find.
#
# Note:
# - The entire plugin is enabled/disabled by scope.
# - Individual tools can be opted out by setting the environment variable
# `RUST_ENV_CHECK_TOOL_EXCLUSIONS` with a comma separated list of the tools
# to exclude. For example, "rustup, cargo tarpaulin" would not require that
# those tools be installed.
#
# Copyright (c) Microsoft Corporation.
#
# SPDX-License-Identifier: BSD-2-Clause-Patent
##
import logging
import re
from collections import namedtuple
from edk2toolext.environment import shell_environment
from edk2toolext.environment.plugintypes.uefi_build_plugin import IUefiBuildPlugin
from edk2toolext.environment.uefi_build import UefiBuilder
from edk2toollib.utility_functions import RunCmd
Expand Down Expand Up @@ -142,9 +150,15 @@ def verify_workspace_rust_toolchain_is_installed() -> RustToolChainInfo:
),
}

excluded_tools_in_shell = shell_environment.GetEnvironment().get_shell_var(
"RUST_ENV_CHECK_TOOL_EXCLUSIONS")
excluded_tools = ([t.strip() for t in
excluded_tools_in_shell.split(",")] if
excluded_tools_in_shell else [])

errors = 0
for tool_name, tool_info in tools.items():
if not verify_cmd(*tool_info.presence_cmd):
if tool_name not in excluded_tools and not verify_cmd(*tool_info.presence_cmd):
logging.error(
f"Rust Environment Failure: {tool_name} is not installed "
"or not on the system path.\n\n"
Expand Down

0 comments on commit 1daaa47

Please sign in to comment.