1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
|
## @file
# Unit tests for DSC parser component scope handling.
#
# Copyright (c) Microsoft Corporation. All rights reserved.
#
# SPDX-License-Identifier: BSD-2-Clause-Patent
#
##
import os
import tempfile
import unittest
from Common import GlobalData
from CommonDataClass.DataClass import MODEL_EFI_LIBRARY_CLASS
from CommonDataClass.DataClass import MODEL_FILE_DSC
from CommonDataClass.DataClass import MODEL_META_DATA_COMPONENT
from CommonDataClass.DataClass import MODEL_PCD_FIXED_AT_BUILD
from Common.Misc import PathClass
from Workspace.MetaFileParser import DscParser
from Workspace.MetaFileParser import MetaFileParser
from Workspace.MetaFileTable import MetaFileStorage
from Workspace.WorkspaceDatabase import WorkspaceDatabase
class DscParserComponentScopeTests(unittest.TestCase):
def setUp(self):
self._saved_workspace = GlobalData.gWorkspace
self._saved_global_defines = GlobalData.gGlobalDefines
self._saved_platform_defines = GlobalData.gPlatformDefines
self._saved_command_line_defines = GlobalData.gCommandLineDefines
self._saved_edk_global = GlobalData.gEdkGlobal
self._saved_platform_pcds = GlobalData.gPlatformPcds
self._saved_platform_other_pcds = GlobalData.gPlatformOtherPcds
self._saved_meta_files = MetaFileParser.MetaFiles.copy()
self._saved_storage_cache = MetaFileStorage._ObjectCache.copy()
self._saved_included_files = DscParser.IncludedFiles.copy()
self._temporary_directory = tempfile.TemporaryDirectory()
workspace = self._temporary_directory.name
GlobalData.gWorkspace = workspace
GlobalData.gGlobalDefines = {"WORKSPACE": workspace}
GlobalData.gPlatformDefines = {}
GlobalData.gCommandLineDefines = {}
GlobalData.gEdkGlobal = {}
GlobalData.gPlatformPcds = {}
GlobalData.gPlatformOtherPcds = {}
def _write_file(self, relative_path, content):
path = os.path.join(self._temporary_directory.name, relative_path)
directory = os.path.dirname(path)
if directory:
os.makedirs(directory, exist_ok=True)
with open(path, "w", newline="\n") as output_file:
output_file.write(content)
return path
def _create_parser(
self,
platform_name,
platform_content,
included_files,
active_architecture="IA32",
):
for relative_path, content in included_files.items():
self._write_file(relative_path, content)
platform_path = self._write_file(platform_name, platform_content)
path = PathClass(platform_path)
database = WorkspaceDatabase()
table = MetaFileStorage(database, path, MODEL_FILE_DSC)
return DscParser(path, MODEL_FILE_DSC, active_architecture, table)
@staticmethod
def _platform_content(
include_path,
define_architecture=True,
macro_architecture="IA32",
):
architecture_definition = (
f" DEFINE PEI_ARCH = {macro_architecture}\n"
if define_architecture
else ""
)
return (
"[Defines]\n"
" PLATFORM_NAME = ParserTest\n"
" PLATFORM_GUID = 11111111-2222-3333-4444-555555555555\n"
" PLATFORM_VERSION = 0.1\n"
" DSC_SPECIFICATION = 0x00010005\n"
" OUTPUT_DIRECTORY = Build/ParserTest\n"
" SUPPORTED_ARCHITECTURES = IA32|X64\n"
" BUILD_TARGETS = DEBUG\n"
" SKUID_IDENTIFIER = DEFAULT\n"
f"{architecture_definition}"
"\n"
f"!include {include_path}\n"
)
@staticmethod
def _component_content(section):
return (
f"[{section}]\n"
" TestPkg/TestPeim.inf {\n"
" <LibraryClasses>\n"
" TestLib|TestPkg/TestLib.inf\n"
" <PcdsFixedAtBuild>\n"
" gTestPkgTokenSpaceGuid.PcdTest|TRUE\n"
" }\n"
)
def _assert_component_private_records(self, parser, architecture):
components = parser[MODEL_META_DATA_COMPONENT, architecture]
self.assertEqual(1, len(components))
self.assertEqual(architecture, components[0][3])
component_id = components[0][6]
library_classes = parser[
MODEL_EFI_LIBRARY_CLASS, architecture, None, component_id
]
self.assertEqual(1, len(library_classes))
self.assertEqual(architecture, library_classes[0][3])
fixed_pcds = parser[
MODEL_PCD_FIXED_AT_BUILD, architecture, None, component_id
]
self.assertEqual(1, len(fixed_pcds))
self.assertEqual(architecture, fixed_pcds[0][3])
self.assertEqual(
[],
parser[MODEL_EFI_LIBRARY_CLASS, architecture],
)
self.assertEqual(
[],
parser[MODEL_PCD_FIXED_AT_BUILD, architecture],
)
def tearDown(self):
MetaFileParser.MetaFiles.clear()
MetaFileParser.MetaFiles.update(self._saved_meta_files)
MetaFileStorage._ObjectCache.clear()
MetaFileStorage._ObjectCache.update(self._saved_storage_cache)
DscParser.IncludedFiles.clear()
DscParser.IncludedFiles.update(self._saved_included_files)
GlobalData.gWorkspace = self._saved_workspace
GlobalData.gGlobalDefines = self._saved_global_defines
GlobalData.gPlatformDefines = self._saved_platform_defines
GlobalData.gCommandLineDefines = self._saved_command_line_defines
GlobalData.gEdkGlobal = self._saved_edk_global
GlobalData.gPlatformPcds = self._saved_platform_pcds
GlobalData.gPlatformOtherPcds = self._saved_platform_other_pcds
self._temporary_directory.cleanup()
def test_private_records_use_expanded_component_architecture(self):
parser = self._create_parser(
"Platform.dsc",
self._platform_content("Child.inc.dsc"),
{
"Child.inc.dsc": self._component_content(
"Components.$(PEI_ARCH)"
)
},
)
self._assert_component_private_records(parser, "IA32")
def test_macro_architecture_is_independent_of_active_architecture(self):
parser = self._create_parser(
"DifferentArch.dsc",
self._platform_content(
"DifferentArch.inc.dsc",
macro_architecture="X64",
),
{
"DifferentArch.inc.dsc": self._component_content(
"Components.$(PEI_ARCH)"
)
},
active_architecture="IA32",
)
self._assert_component_private_records(parser, "X64")
self.assertEqual(
[],
parser[MODEL_META_DATA_COMPONENT, "IA32"],
)
def test_aarch64_private_records_use_expanded_component_architecture(self):
platform_content = self._platform_content(
"Aarch64.inc.dsc",
macro_architecture="AARCH64",
).replace(
"SUPPORTED_ARCHITECTURES = IA32|X64",
"SUPPORTED_ARCHITECTURES = IA32|X64|AARCH64",
)
parser = self._create_parser(
"Aarch64.dsc",
platform_content,
{
"Aarch64.inc.dsc": self._component_content(
"Components.$(PEI_ARCH)"
)
},
active_architecture="AARCH64",
)
self._assert_component_private_records(parser, "AARCH64")
def test_literal_component_architecture_is_unchanged(self):
parser = self._create_parser(
"Literal.dsc",
self._platform_content("Literal.inc.dsc", False),
{
"Literal.inc.dsc": self._component_content(
"Components.IA32"
)
},
)
self._assert_component_private_records(parser, "IA32")
def test_unresolved_component_architecture_macro_is_unchanged(self):
parser = self._create_parser(
"Unresolved.dsc",
self._platform_content("Unresolved.inc.dsc", False),
{
"Unresolved.inc.dsc": self._component_content(
"Components.$(UNKNOWN_ARCH)"
)
},
)
self._assert_component_private_records(parser, "$(UNKNOWN_ARCH)")
def test_nested_include_private_records_use_expanded_architecture(self):
parser = self._create_parser(
"Nested.dsc",
self._platform_content("Intermediate.inc.dsc"),
{
"Intermediate.inc.dsc": "!include Leaf.inc.dsc\n",
"Leaf.inc.dsc": self._component_content(
"Components.$(PEI_ARCH)"
),
},
)
self._assert_component_private_records(parser, "IA32")
def test_top_level_include_scope_is_not_expanded(self):
parser = self._create_parser(
"TopLevelInclude.dsc",
self._platform_content("TopLevelInclude.inc.dsc"),
{
"TopLevelInclude.inc.dsc": (
"[LibraryClasses.$(PEI_ARCH)]\n"
" TestLib|TestPkg/TestLib.inf\n"
)
},
)
library_classes = parser[
MODEL_EFI_LIBRARY_CLASS,
"$(PEI_ARCH)",
]
self.assertEqual(1, len(library_classes))
self.assertEqual("$(PEI_ARCH)", library_classes[0][3])
self.assertEqual(
[],
parser[MODEL_EFI_LIBRARY_CLASS, "IA32"],
)
def test_reprocessing_does_not_reuse_component_owner_mapping(self):
GlobalData.gCommandLineDefines["ENABLE_INCLUDE"] = "FALSE"
platform_content = self._platform_content("Reprocess.inc.dsc").replace(
"!include Reprocess.inc.dsc\n",
(
"!if $(ENABLE_INCLUDE) == TRUE\n"
"!include Reprocess.inc.dsc\n"
"!endif\n"
),
)
parser = self._create_parser(
"Reprocess.dsc",
platform_content,
{
"Reprocess.inc.dsc": (
"[LibraryClasses.$(PEI_ARCH)]\n"
" TestLib|TestPkg/TestLib.inf\n"
)
},
)
self.assertEqual(
[],
parser[MODEL_EFI_LIBRARY_CLASS, "$(PEI_ARCH)"],
)
GlobalData.gCommandLineDefines["ENABLE_INCLUDE"] = "TRUE"
parser.DoPostProcess()
library_classes = parser[
MODEL_EFI_LIBRARY_CLASS,
"$(PEI_ARCH)",
]
self.assertEqual(1, len(library_classes))
self.assertEqual("$(PEI_ARCH)", library_classes[0][3])
self.assertEqual(
[],
parser[MODEL_EFI_LIBRARY_CLASS, "IA32"],
)
def test_private_subsection_include_keeps_expanded_architecture(self):
parser = self._create_parser(
"SubsectionInclude.dsc",
self._platform_content("SubsectionInclude.inc.dsc"),
{
"SubsectionInclude.inc.dsc": (
"[Components.$(PEI_ARCH), Components.X64]\n"
" TestPkg/TestPeim.inf {\n"
" <LibraryClasses>\n"
" !include PrivateLibrary.inc.dsc\n"
" <PcdsFixedAtBuild>\n"
" gTestPkgTokenSpaceGuid.PcdTest|TRUE\n"
" }\n"
),
"PrivateLibrary.inc.dsc": (
"TestLib|TestPkg/TestLib.inf\n"
),
},
)
self._assert_component_private_records(parser, "IA32")
self._assert_component_private_records(parser, "X64")
def test_multi_architecture_private_records_keep_matching_owners(self):
parser = self._create_parser(
"MultiArch.dsc",
self._platform_content("MultiArch.inc.dsc"),
{
"MultiArch.inc.dsc": self._component_content(
"Components.$(PEI_ARCH), Components.X64"
)
},
)
self._assert_component_private_records(parser, "IA32")
self._assert_component_private_records(parser, "X64")
def test_repeated_macro_sections_keep_private_records_with_their_owner(self):
parser = self._create_parser(
"RepeatedSections.dsc",
self._platform_content("RepeatedSections.inc.dsc"),
{
"RepeatedSections.inc.dsc": (
"[Components.$(PEI_ARCH)]\n"
" TestPkg/FirstPeim.inf\n"
"\n"
+ self._component_content("Components.$(PEI_ARCH)")
)
},
)
components = parser[MODEL_META_DATA_COMPONENT, "IA32"]
self.assertEqual(
["TestPkg/FirstPeim.inf", "TestPkg/TestPeim.inf"],
[component[0] for component in components],
)
first_component_id = components[0][6]
private_component_id = components[1][6]
self.assertEqual(
[],
parser[
MODEL_EFI_LIBRARY_CLASS,
"IA32",
None,
first_component_id,
],
)
self.assertEqual(
1,
len(
parser[
MODEL_EFI_LIBRARY_CLASS,
"IA32",
None,
private_component_id,
]
),
)
self.assertEqual(
1,
len(
parser[
MODEL_PCD_FIXED_AT_BUILD,
"IA32",
None,
private_component_id,
]
),
)
def TheTestSuite():
return unittest.defaultTestLoader.loadTestsFromTestCase(
DscParserComponentScopeTests
)
if __name__ == "__main__":
unittest.main()
|