From 92c9cd2979ae4e145b7b6851fc8cd7024f7a3051 Mon Sep 17 00:00:00 2001 From: "Aaron Pop (from Dev Box)" Date: Tue, 15 Oct 2024 15:46:14 -0700 Subject: [PATCH 1/2] Updated _TokenizeConditional to better deal with != in dsc files --- edk2toollib/uefi/edk2/parsers/base_parser.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/edk2toollib/uefi/edk2/parsers/base_parser.py b/edk2toollib/uefi/edk2/parsers/base_parser.py index 45199c65..ac792b8f 100644 --- a/edk2toollib/uefi/edk2/parsers/base_parser.py +++ b/edk2toollib/uefi/edk2/parsers/base_parser.py @@ -546,12 +546,14 @@ def _TokenizeConditional(cls: "BaseParser", text: str) -> str: mode = 0 tokens = [] for character in text: - if character == '"' and len(token) == 0: + if character == '"' and len(token) == 0 and mode != QUOTE_MODE: mode = QUOTE_MODE elif character == '"' and mode == QUOTE_MODE: if len(token) > 0: tokens.append(f'"{token}"') token = "" + else: + tokens.append('""') mode = TEXT_MODE elif character == "$" and len(token) == 0: token += character From d9d389bdab962854d69ba4f3599af29f4fb3b5bf Mon Sep 17 00:00:00 2001 From: "Aaron Pop (from Dev Box)" Date: Thu, 17 Oct 2024 14:54:41 -0700 Subject: [PATCH 2/2] Added unit test for defines --- tests.unit/parsers/test_dsc_parser.py | 35 +++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/tests.unit/parsers/test_dsc_parser.py b/tests.unit/parsers/test_dsc_parser.py index 226c3b8d..b367bb2a 100644 --- a/tests.unit/parsers/test_dsc_parser.py +++ b/tests.unit/parsers/test_dsc_parser.py @@ -10,6 +10,7 @@ import unittest import tempfile import os +import textwrap from edk2toollib.uefi.edk2.parsers.dsc_parser import DscParser from edk2toollib.uefi.edk2.path_utilities import Edk2Path @@ -155,3 +156,37 @@ def test_dsc_include_relative_path(self): self.assertEqual(parser.LocalVars["INCLUDED"], "TRUE") # make sure we got the defines finally: os.chdir(cwd) + + def test_dsc_define_statements(self): + """This test some dsc define statements""" + SAMPLE_DSC_FILE1 = textwrap.dedent("""\ + [Defines] + PLATFORM_NAME = SomePlatformPkg + PLATFORM_GUID = aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa + PLATFORM_VERSION = 0.1 + DSC_SPECIFICATION = 0x00010005 + OUTPUT_DIRECTORY = Build/$(PLATFORM_NAME) + + DEFINE FLAG1 = + + !if FLAG1 != "" + FLAG2 = "hi" + !endif + + !if $(FLAG2) == "hi" + [Components.IA32] + FakePath/FakePath2/FakeInf.inf + !endif + """) + workspace = tempfile.mkdtemp() + + file1_name = "file1.dsc" + file1_path = os.path.join(workspace, file1_name) + TestDscParserIncludes.write_to_file(file1_path, SAMPLE_DSC_FILE1) + try: + parser = DscParser() + parser.SetEdk2Path(Edk2Path(workspace, [])) + parser.ParseFile(file1_path) + finally: + os.remove(file1_path) + assert any("FakePath/FakePath2/FakeInf.inf" in value for value in parser.Components)