Skip to content

Commit

Permalink
BaseTools: Add Cargo Feature support for build (#682)
Browse files Browse the repository at this point in the history
## Description

Adds build support for setting cargo features via EDKII Feature Pcds.
Adds a new makefile variable, CARGO_FEATURES [a comma separated list]
that is generated per module based on any FeaturePcds in the INF that
are enabled. any build rule can consume this makefile variable.

Updates the build rules for building rust crates to consume this
variable.

Updates cargo make's config to pass the variable to the underlying
build command.

**WARNING: Makefile.toml will be updated via mu_devops
(microsoft/mu_devops#298

- [ ] 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, ...
- [x] 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, ...
- [ ] 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

Created a featurePCD and ensured that the rust feature is passed to
cargo. Ensured that when the featurepcd is set to true, the feature is
enabled in cargo. Ensured that when the featurepcd is set to false, the
feature is not enabled in cargo.

## Integration Instructions

Developers will need to complete the following:
1. Remove the temporary Conf file, so that it can be repopulated.
2. Update consuming repository's Makefile.toml from mu_devops.
  • Loading branch information
Javagedes authored Jan 19, 2024
1 parent cc6cf6f commit 113983d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
4 changes: 2 additions & 2 deletions BaseTools/Conf/build_rule.template
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@
$(OUTPUT_DIR)(+)$(MODULE_NAME)rust.lib

<Command>
$(CARGO) make $(CARGOMAKE_FLAGS) -e RUSTFLAGS="-C link-arg=/MAP:$(DEBUG_DIR)(+)$(MODULE_NAME).map $(RUST_FLAGS)" build $(MODULE_NAME)
$(CARGO) make $(CARGOMAKE_FLAGS) -e FEATURES=$(CARGO_FEATURES) -e RUSTFLAGS="-C link-arg=/MAP:$(DEBUG_DIR)(+)$(MODULE_NAME).map $(RUST_FLAGS)" build $(MODULE_NAME)
$(CP) $(DEBUG_DIR)(+)$(TARGET_TRIPLE)(+)$(RUST_TARGET)(+)*.a $(DEBUG_DIR)(+)$(MODULE_NAME)rust.lib
$(CP) $(DEBUG_DIR)(+)$(TARGET_TRIPLE)(+)$(RUST_TARGET)(+)*.a $(OUTPUT_DIR)(+)$(MODULE_NAME)rust.lib

Expand All @@ -294,7 +294,7 @@
$(OUTPUT_DIR)(+)$(MODULE_NAME).efi

<Command>
$(CARGO) make $(CARGOMAKE_FLAGS) -e RUSTFLAGS="-C link-arg=/MAP:$(DEBUG_DIR)(+)$(MODULE_NAME).map $(RUST_FLAGS)" build $(MODULE_NAME)
$(CARGO) make $(CARGOMAKE_FLAGS) -e FEATURES=$(CARGO_FEATURES) -e RUSTFLAGS="-C link-arg=/MAP:$(DEBUG_DIR)(+)$(MODULE_NAME).map $(RUST_FLAGS)" build $(MODULE_NAME)
"$(GENFW)" -e $(MODULE_TYPE) -o $(DEBUG_DIR)(+)$(MODULE_NAME).efi $(DEBUG_DIR)(+)$(TARGET_TRIPLE)(+)$(RUST_TARGET)(+)$(MODULE_NAME).efi $(GENFW_FLAGS)
$(CP) $(DEBUG_DIR)(+)$(MODULE_NAME).map $(OUTPUT_DIR)(+)$(MODULE_NAME).map
$(CP) $(DEBUG_DIR)(+)$(MODULE_NAME).efi $(OUTPUT_DIR)(+)$(MODULE_NAME).efi
Expand Down
14 changes: 14 additions & 0 deletions BaseTools/Source/Python/AutoGen/GenMake.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ class ModuleMakefile(BuildFile):
# MU_CHANGE [BEGIN] - Add Rust build support
CARGO_OUTPUT_DIR = ${cargo_module_output_directory}
CARGO_FEATURES = ${cargo_module_enabled_features}
# MU_CHANGE [END] - Add Rust build support
#
Expand Down Expand Up @@ -729,6 +730,7 @@ def _TemplateDict(self):
"module_output_directory" : MyAgo.OutputDir,
# MU_CHANGE [BEGIN] - Add Rust build support
"cargo_module_output_directory": MyAgo.OutputDir,
"cargo_module_enabled_features": self.GetModuleEnabledRustFeatures(),
# MU_CHANGE [END] - Add Rust build support
"module_debug_directory" : MyAgo.DebugDir,

Expand Down Expand Up @@ -1196,6 +1198,18 @@ def GetFileDependency(self, FileList, ForceInculeList, SearchPathList):
Dependency[F] = GetDependencyList(self._AutoGenObject, self.FileCache, F, ForceInculeList, SearchPathList)
return Dependency

# MU_CHANGE [BEGIN] - Add Rust build support
## Returns a comma delimited string of enabled cargo features for this module
#
# @retval str comma delimited string of cargo feature flags
#
def GetModuleEnabledRustFeatures(self):
features = ""
for pcd in self._AutoGenObject.ModulePcdList:
if pcd.Type == "FeatureFlag" and bool(pcd.DefaultValue):
features += f'{pcd.TokenCName},'
return features
# MU_CHANGE [END] - Add Rust build support

## CustomMakefile class
#
Expand Down

0 comments on commit 113983d

Please # to comment.