|
| 1 | +# @file stuart_codeql.py |
| 2 | +# |
| 3 | +# Exports functions commonly needed for Stuart-based platforms to easily |
| 4 | +# enable CodeQL in their platform build. |
| 5 | +# |
| 6 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 7 | +# SPDX-License-Identifier: BSD-2-Clause-Patent |
| 8 | +## |
| 9 | + |
| 10 | +from edk2toolext.environment.uefi_build import UefiBuilder |
| 11 | +from edk2toollib.utility_functions import GetHostInfo |
| 12 | +from argparse import ArgumentParser, Namespace |
| 13 | +from typing import Tuple |
| 14 | + |
| 15 | + |
| 16 | +def add_command_line_option(parser: ArgumentParser) -> None: |
| 17 | + """Adds the CodeQL command to the platform command line options. |
| 18 | +
|
| 19 | + Args: |
| 20 | + parser (ArgumentParser): The argument parser used in this build. |
| 21 | +
|
| 22 | + """ |
| 23 | + parser.add_argument( |
| 24 | + '--codeql', |
| 25 | + dest='codeql', |
| 26 | + action='store_true', |
| 27 | + default=False, |
| 28 | + help="Optional - Produces CodeQL results from the build. See " |
| 29 | + "BaseTools/Plugin/CodeQL/Readme.md for more info.") |
| 30 | + |
| 31 | + |
| 32 | +def get_scopes(codeql_enabled: bool) -> Tuple[str]: |
| 33 | + """Returns the active CodeQL scopes for this build. |
| 34 | +
|
| 35 | + Args: |
| 36 | + codeql_enabled (bool): Whether CodeQL is enabled. |
| 37 | +
|
| 38 | + Returns: |
| 39 | + Tuple[str]: A tuple of strings containing scopes that enable the |
| 40 | + CodeQL plugin. |
| 41 | + """ |
| 42 | + active_scopes = () |
| 43 | + |
| 44 | + if codeql_enabled: |
| 45 | + if GetHostInfo().os == "Linux": |
| 46 | + active_scopes += ("codeql-linux-ext-dep",) |
| 47 | + else: |
| 48 | + active_scopes += ("codeql-windows-ext-dep",) |
| 49 | + active_scopes += ("codeql-build", "codeql-analyze") |
| 50 | + |
| 51 | + return active_scopes |
| 52 | + |
| 53 | + |
| 54 | +def is_codeql_enabled_on_command_line(args: Namespace) -> bool: |
| 55 | + """Returns whether CodeQL was enabled on the command line. |
| 56 | +
|
| 57 | + Args: |
| 58 | + args (Namespace): Object holding a string representation of command |
| 59 | + line arguments. |
| 60 | +
|
| 61 | + Returns: |
| 62 | + bool: True if CodeQL is enabled on the command line. Otherwise, false. |
| 63 | + """ |
| 64 | + return args.codeql |
| 65 | + |
| 66 | + |
| 67 | +def set_audit_only_mode(uefi_builder: UefiBuilder) -> None: |
| 68 | + """Configures the CodeQL plugin to run in audit only mode. |
| 69 | +
|
| 70 | + Args: |
| 71 | + uefi_builder (UefiBuilder): The UefiBuilder object for this platform |
| 72 | + build. |
| 73 | +
|
| 74 | + """ |
| 75 | + |
| 76 | + uefi_builder.env.SetValue( |
| 77 | + "STUART_CODEQL_AUDIT_ONLY", |
| 78 | + "true", |
| 79 | + "Platform Defined") |
0 commit comments