Skip to content

BaseTools: Honor DSC conditional branches during post-process#12856

Open
kirkchoutw wants to merge 1 commit into
tianocore:masterfrom
kirkchoutw:feature/basetools-fix-conditional-macro-processing
Open

BaseTools: Honor DSC conditional branches during post-process#12856
kirkchoutw wants to merge 1 commit into
tianocore:masterfrom
kirkchoutw:feature/basetools-fix-conditional-macro-processing

Conversation

@kirkchoutw

@kirkchoutw kirkchoutw commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Description

When DSC conditional directives (!if/!ifdef/!ifndef/!else/!elseif) disable a branch, the data records inside that branch should not be processed during post-process. Previously, macro expansion, PCD processing, and "!include" evaluation could still be performed on records belonging to disabled branches, which could trigger build failures due to undefined macros or non-existent include files.

This PR introduces a centralized "_Enabled" gate in "DscParser._PostProcess()". Only conditional-directive records and section context records (section/subsection headers) are always processed, while all other records are skipped when the current branch is disabled. This preserves directive-stack correctness while preventing side effects from inactive
branches. "_Enabled" is also reset at the beginning of post-process to avoid stale state carried over from raw parsing.

The per-processor "if not self._Enabled: return" guards (in "_ProcessError", "__ProcessDefine", "__ProcessDirective"/include handling, and "__ProcessPcd") are removed in favor of the single gate, simplifying the logic.

  • Breaking change?
    • Breaking change - Does this PR cause a break in build or boot behavior?
    • Examples: Does it add a new library class or move a module to a different repo.
    • If checked, follow the Breaking Change and Release Process.
  • Impacts security?
    • Security - Does this PR have a direct security impact?
    • Examples: Crypto algorithm change or buffer overflow fix.
  • Includes tests?
    • Tests - Does this PR include any explicit test code?
    • Examples: Unit tests or integration tests.

How This Was Tested

Tested with the standard build command using a real package - MdePkg.
A disabled conditional branch containing an undefined macro was temporarily added to MdePkg/MdePkg.dsc :
[LibraryClasses]
SafeIntLib|MdePkg/Library/BaseSafeIntLib/BaseSafeIntLib.inf
!if 0
DisabledLib|$(THIS_MACRO_IS_NOT_DEFINED)/DisabledLib.inf
!endif

Build command :

build -p MdePkg/MdePkg.dsc -m MdePkg/Library/BaseLib/BaseLib.inf -a IA32 -t GCC -b NOOPT

Results:

  • With this fix: build completes successfully. The record inside the disabled !if 0 branch is skipped, so its undefined macro is never expanded.
  • Without this fix (parser reverted to the parent commit): build aborts during DSC parsing with "MdePkg.dsc(line): error 3000: THIS_MACRO_IS_NOT_DEFINED not defined", confirming the regression this change fixes.

The same module also builds cleanly with no disabled branch present, confirming active-branch parsing is unaffected.

Integration Instructions

N/A

When DSC conditional directives disable a branch, data records
inside the branch should not be processed. Previously macro
expansion, PCD processing and include evaluation could still be
performed on records from disabled branches during post-process,
which might trigger build failures for undefined macros or
non-existent include files.

Introduce a centralized _Enabled gate in DscParser._PostProcess()
so that only conditional directives and section context records
are processed while a branch is disabled. This preserves
directive-stack correctness while preventing side effects from
inactive branches.

Also reset _Enabled at the beginning of post-process to avoid
stale state from raw parsing.

Signed-off-by: Kirk Chou <kirk.chou@hpe.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant