Skip to content

Commit

Permalink
Add Rust config files
Browse files Browse the repository at this point in the history
Two environment configuration files are added to be used in the edk2
repository for Rust development but to also serve as a configuration
reference for downstream repositories for the settings being used in
edk2.

A goal in these changes is support a standalone Rust build experience
using the command line where that build support can be leveraged
within the edk2 build system. A component for achieving this is the
"cargo-make task runner" that allows the "cargo make" command to be
used to directly build and test individual Rust packages with simple
commands. It also allows the details to be captured in a single
makefile.

These files have no impact on users not building Rust code.

- `Makefile.toml` - Defines the tasks and environment settings used
  to build and test code.

For more information:
https://github.com/sagiegurari/cargo-make?tab=readme-ov-file#usage

- `rustfmt.toml` - Defines Rust formatting options used.
  Automatically read by the `cargo fmt` command in the workspace.

For more information:
https://github.com/rust-lang/rustfmt

- `rust-toolchain.toml` - Defines the exact Rust toolchain supported
  in this repository. This ensures developers build with a toolchain
  that is used by all other developers and CI.

For more information:
https://rust-lang.github.io/rustup/overrides.html#the-toolchain-file

Co-authored-by: Joey Vagedes <[email protected]>
Signed-off-by: Michael Kubacki <[email protected]>
  • Loading branch information
makubacki and Javagedes committed Jun 19, 2024
1 parent d04b259 commit 86cdb69
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
52 changes: 52 additions & 0 deletions Makefile.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
[config]
default_to_workspace = false

[env]
CARGO_MAKE_EXTEND_WORKSPACE_MAKEFILE = true
RUSTC_BOOTSTRAP = 1
ARCH = "X64"
TARGET_TRIPLE = { source = "${ARCH}", mapping = { "X64" = "x86_64-unknown-uefi", "IA32" = "i686-unknown-uefi", "AARCH64" = "aarch64-unknown-uefi", "LOCAL" = "${CARGO_MAKE_RUST_TARGET_TRIPLE}" }, condition = { env_not_set = [ "TARGET_TRIPLE" ] } }
PACKAGE_TARGET = {value = "-p ${CARGO_MAKE_TASK_ARGS}",condition = { env_true = [ "CARGO_MAKE_TASK_ARGS", ] } }

BUILD_FLAGS = "--profile ${RUSTC_PROFILE} --target ${TARGET_TRIPLE} -Zbuild-std=core,compiler_builtins,alloc -Zbuild-std-features=compiler-builtins-mem -Zunstable-options --timings=html"
TEST_FLAGS = { value = "", condition = { env_not_set = ["TEST_FLAGS"] } }
COV_FLAGS = { value = "--out Html", condition = { env_not_set = ["COV_FLAGS"] } }

[env.development]
RUSTC_PROFILE = "dev"
RUSTC_TARGET = "debug"

[env.release]
RUSTC_PROFILE = "release"
RUSTC_TARGET = "release"

[tasks.build]
description = """Builds a single rust package.
Customizations:
-p [development|release]: Builds in debug or release. Default: development
-e ARCH=[IA32|X64|AARCH64|LOCAL]: Builds with specifed arch. Default: X64
Example:
`cargo make build RustModule`
`cargo make -p release build RustModule`
`cargo make -e ARCH=IA32 build RustLib`
"""
clear = true
command = "cargo"
args = ["build", "@@split(PACKAGE_TARGET, )", "@@split(BUILD_FLAGS, )"]

[tasks.test]
description = "Builds all rust tests in the workspace. Example `cargo make test`"
clear = true
command = "cargo"
args = ["test", "@@split(PACKAGE_TARGET, )", "@@split(TEST_FLAGS, )"]

[tasks.coverage]
disabled = true

[tasks.cov]
description = "Build and run all tests and calculate coverage."
clear = true
command = "cargo"
args = ["tarpaulin", "@@split(PACKAGE_TARGET, )", "@@split(COV_FLAGS, )", "--exclude-files", "**/tests/*", "--output-dir", "${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/target"]
6 changes: 6 additions & 0 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[toolchain]
channel = "1.76.0"

[tool]
cargo-make = "0.37.9"
cargo-tarpaulin = "0.27.3"
21 changes: 21 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# rustfmt (and cargo fmt) will automatically pick up this config when run in the workspace.

# Note that some items are included here set to their default values. This is to explicitly specify settings for
# more common options.

# Keep these options sorted in ascending order to ease lookup against rustfmt documentation.

edition = "2021" # This would normally be picked up from Cargo.toml if not specified here
force_explicit_abi = true # Always print the ABI for extern items (e.g. extern {... will become extern "C" {...)
hard_tabs = false # Always uses spaces for indentation and alignment
max_width = 120 # The maximum width of each line
merge_derives = false # Do not merge derives into a single line (leave to author discretion).
imports_granularity = "Crate" # Merge imports from a single crate into separate statements.
newline_style = "Windows" # Always use Windows line endings '\r\n'
reorder_impl_items = false # Do not force where type and const before macros and methods in impl blocks.
reorder_imports = true # Do reorder import and extern crate statements alphabetically for readability.
reorder_modules = true # Do reorder mod declarations alphabetically for readability.
tab_spaces = 2 # Use 2 spaces for indentation (Rust default is 4).
unstable_features = false # Do not use unstable rustfmt features.
use_small_heuristics = "Max" # Set all granular width settings to the same as max_width (do not use heuristics)
wrap_comments = false # Leave comment formatting to author's discretion

0 comments on commit 86cdb69

Please sign in to comment.