]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Source/Python/AutoGen/GenC.py
New EdkShell binaries.
[mirror_edk2.git] / BaseTools / Source / Python / AutoGen / GenC.py
CommitLineData
30fdf114
LG
1## @file
2# Routines for generating AutoGen.h and AutoGen.c
3#
e8a47801 4# Copyright (c) 2007 - 2013, Intel Corporation. All rights reserved.<BR>
40d841f6 5# This program and the accompanying materials
30fdf114
LG
6# are licensed and made available under the terms and conditions of the BSD License
7# which accompanies this distribution. The full text of the license may be found at
8# http://opensource.org/licenses/bsd-license.php
9#
10# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12#
13
14## Import Modules
15#
16import string
17
18from Common import EdkLogger
19
20from Common.BuildToolError import *
21from Common.DataType import *
22from Common.Misc import *
23from Common.String import StringToArray
24from StrGather import *
e8a47801 25from GenPcdDb import CreatePcdDatabaseCode
30fdf114
LG
26
27## PCD type string
28gItemTypeStringDatabase = {
29 TAB_PCDS_FEATURE_FLAG : 'FixedAtBuild',
30 TAB_PCDS_FIXED_AT_BUILD : 'FixedAtBuild',
31 TAB_PCDS_PATCHABLE_IN_MODULE: 'BinaryPatch',
32 TAB_PCDS_DYNAMIC : '',
33 TAB_PCDS_DYNAMIC_DEFAULT : '',
34 TAB_PCDS_DYNAMIC_VPD : '',
35 TAB_PCDS_DYNAMIC_HII : '',
36 TAB_PCDS_DYNAMIC_EX : '',
37 TAB_PCDS_DYNAMIC_EX_DEFAULT : '',
38 TAB_PCDS_DYNAMIC_EX_VPD : '',
39 TAB_PCDS_DYNAMIC_EX_HII : '',
40}
41
42## Dynamic PCD types
43gDynamicPcd = [TAB_PCDS_DYNAMIC, TAB_PCDS_DYNAMIC_DEFAULT, TAB_PCDS_DYNAMIC_VPD, TAB_PCDS_DYNAMIC_HII]
44
45## Dynamic-ex PCD types
46gDynamicExPcd = [TAB_PCDS_DYNAMIC_EX, TAB_PCDS_DYNAMIC_EX_DEFAULT, TAB_PCDS_DYNAMIC_EX_VPD, TAB_PCDS_DYNAMIC_EX_HII]
47
48## Datum size
49gDatumSizeStringDatabase = {'UINT8':'8','UINT16':'16','UINT32':'32','UINT64':'64','BOOLEAN':'BOOLEAN','VOID*':'8'}
50gDatumSizeStringDatabaseH = {'UINT8':'8','UINT16':'16','UINT32':'32','UINT64':'64','BOOLEAN':'BOOL','VOID*':'PTR'}
51gDatumSizeStringDatabaseLib = {'UINT8':'8','UINT16':'16','UINT32':'32','UINT64':'64','BOOLEAN':'Bool','VOID*':'Ptr'}
52
30fdf114
LG
53## AutoGen File Header Templates
54gAutoGenHeaderString = TemplateString("""\
55/**
56 DO NOT EDIT
57 FILE auto-generated
58 Module name:
59 ${FileName}
60 Abstract: Auto-generated ${FileName} for building module or library.
61**/
62""")
63
64gAutoGenHPrologueString = TemplateString("""
65#ifndef _${File}_${Guid}
66#define _${File}_${Guid}
67
2bcc713e
LG
68""")
69
d0acc87a 70gAutoGenHCppPrologueString = """\
d40b2ee6
LG
71#ifdef __cplusplus
72extern "C" {
73#endif
74
2bcc713e 75"""
30fdf114
LG
76
77gAutoGenHEpilogueString = """
d40b2ee6
LG
78
79#ifdef __cplusplus
80}
81#endif
82
30fdf114
LG
83#endif
84"""
85
86## PEI Core Entry Point Templates
87gPeiCoreEntryPointPrototype = TemplateString("""
88${BEGIN}
89VOID
90EFIAPI
91${Function} (
92 IN CONST EFI_SEC_PEI_HAND_OFF *SecCoreData,
93 IN CONST EFI_PEI_PPI_DESCRIPTOR *PpiList,
94 IN VOID *Context
95 );
96${END}
97""")
98
99gPeiCoreEntryPointString = TemplateString("""
100${BEGIN}
101VOID
102EFIAPI
103ProcessModuleEntryPointList (
104 IN CONST EFI_SEC_PEI_HAND_OFF *SecCoreData,
105 IN CONST EFI_PEI_PPI_DESCRIPTOR *PpiList,
106 IN VOID *Context
107 )
108
109{
110 ${Function} (SecCoreData, PpiList, Context);
111}
112${END}
113""")
114
115
116## DXE Core Entry Point Templates
117gDxeCoreEntryPointPrototype = TemplateString("""
118${BEGIN}
119VOID
120EFIAPI
121${Function} (
122 IN VOID *HobStart
123 );
124${END}
125""")
126
127gDxeCoreEntryPointString = TemplateString("""
128${BEGIN}
129VOID
130EFIAPI
131ProcessModuleEntryPointList (
132 IN VOID *HobStart
133 )
134
135{
136 ${Function} (HobStart);
137}
138${END}
139""")
140
141## PEIM Entry Point Templates
142gPeimEntryPointPrototype = TemplateString("""
143${BEGIN}
144EFI_STATUS
145EFIAPI
146${Function} (
147 IN EFI_PEI_FILE_HANDLE FileHandle,
148 IN CONST EFI_PEI_SERVICES **PeiServices
149 );
150${END}
151""")
152
30fdf114
LG
153gPeimEntryPointString = [
154TemplateString("""
155GLOBAL_REMOVE_IF_UNREFERENCED const UINT32 _gPeimRevision = ${PiSpecVersion};
156
157EFI_STATUS
158EFIAPI
159ProcessModuleEntryPointList (
160 IN EFI_PEI_FILE_HANDLE FileHandle,
161 IN CONST EFI_PEI_SERVICES **PeiServices
162 )
163
164{
165 return EFI_SUCCESS;
166}
167"""),
168TemplateString("""
169GLOBAL_REMOVE_IF_UNREFERENCED const UINT32 _gPeimRevision = ${PiSpecVersion};
170${BEGIN}
171EFI_STATUS
172EFIAPI
173ProcessModuleEntryPointList (
174 IN EFI_PEI_FILE_HANDLE FileHandle,
175 IN CONST EFI_PEI_SERVICES **PeiServices
176 )
177
178{
179 return ${Function} (FileHandle, PeiServices);
180}
181${END}
182"""),
183TemplateString("""
184GLOBAL_REMOVE_IF_UNREFERENCED const UINT32 _gPeimRevision = ${PiSpecVersion};
185
186EFI_STATUS
187EFIAPI
188ProcessModuleEntryPointList (
189 IN EFI_PEI_FILE_HANDLE FileHandle,
190 IN CONST EFI_PEI_SERVICES **PeiServices
191 )
192
193{
194 EFI_STATUS Status;
195 EFI_STATUS CombinedStatus;
196
197 CombinedStatus = EFI_LOAD_ERROR;
198${BEGIN}
199 Status = ${Function} (FileHandle, PeiServices);
200 if (!EFI_ERROR (Status) || EFI_ERROR (CombinedStatus)) {
201 CombinedStatus = Status;
202 }
203${END}
204 return CombinedStatus;
205}
206""")
207]
208
b303ea72
LG
209## SMM_CORE Entry Point Templates
210gSmmCoreEntryPointPrototype = TemplateString("""
211${BEGIN}
212EFI_STATUS
213EFIAPI
214${Function} (
215 IN EFI_HANDLE ImageHandle,
216 IN EFI_SYSTEM_TABLE *SystemTable
217 );
218${END}
219""")
220
221gSmmCoreEntryPointString = TemplateString("""
222${BEGIN}
52302d4d 223const UINT32 _gUefiDriverRevision = ${UefiSpecVersion};
b303ea72
LG
224const UINT32 _gDxeRevision = ${PiSpecVersion};
225
226EFI_STATUS
227EFIAPI
228ProcessModuleEntryPointList (
229 IN EFI_HANDLE ImageHandle,
230 IN EFI_SYSTEM_TABLE *SystemTable
231 )
232{
233 return ${Function} (ImageHandle, SystemTable);
234}
235${END}
236""")
237
30fdf114
LG
238## DXE SMM Entry Point Templates
239gDxeSmmEntryPointPrototype = TemplateString("""
240${BEGIN}
241EFI_STATUS
242EFIAPI
243${Function} (
244 IN EFI_HANDLE ImageHandle,
245 IN EFI_SYSTEM_TABLE *SystemTable
246 );
247${END}
248""")
249
250gDxeSmmEntryPointString = [
251TemplateString("""
52302d4d 252const UINT32 _gUefiDriverRevision = ${UefiSpecVersion};
30fdf114
LG
253const UINT32 _gDxeRevision = ${PiSpecVersion};
254
255EFI_STATUS
256EFIAPI
257ProcessModuleEntryPointList (
258 IN EFI_HANDLE ImageHandle,
259 IN EFI_SYSTEM_TABLE *SystemTable
260 )
261
262{
263 return EFI_SUCCESS;
264}
265"""),
266TemplateString("""
52302d4d 267const UINT32 _gUefiDriverRevision = ${UefiSpecVersion};
30fdf114
LG
268const UINT32 _gDxeRevision = ${PiSpecVersion};
269
270static BASE_LIBRARY_JUMP_BUFFER mJumpContext;
52302d4d 271static EFI_STATUS mDriverEntryPointStatus;
30fdf114
LG
272
273VOID
274EFIAPI
275ExitDriver (
276 IN EFI_STATUS Status
277 )
278{
279 if (!EFI_ERROR (Status) || EFI_ERROR (mDriverEntryPointStatus)) {
280 mDriverEntryPointStatus = Status;
281 }
282 LongJump (&mJumpContext, (UINTN)-1);
283 ASSERT (FALSE);
284}
285
286EFI_STATUS
287EFIAPI
288ProcessModuleEntryPointList (
289 IN EFI_HANDLE ImageHandle,
290 IN EFI_SYSTEM_TABLE *SystemTable
291 )
30fdf114 292{
52302d4d
LG
293 mDriverEntryPointStatus = EFI_LOAD_ERROR;
294
30fdf114
LG
295${BEGIN}
296 if (SetJump (&mJumpContext) == 0) {
297 ExitDriver (${Function} (ImageHandle, SystemTable));
298 ASSERT (FALSE);
299 }
300${END}
301
302 return mDriverEntryPointStatus;
303}
304""")
305]
306
307## UEFI Driver Entry Point Templates
308gUefiDriverEntryPointPrototype = TemplateString("""
309${BEGIN}
310EFI_STATUS
311EFIAPI
312${Function} (
313 IN EFI_HANDLE ImageHandle,
314 IN EFI_SYSTEM_TABLE *SystemTable
315 );
316${END}
317""")
318
319gUefiDriverEntryPointString = [
320TemplateString("""
52302d4d 321const UINT32 _gUefiDriverRevision = ${UefiSpecVersion};
30fdf114
LG
322const UINT32 _gDxeRevision = ${PiSpecVersion};
323
324EFI_STATUS
325EFIAPI
326ProcessModuleEntryPointList (
327 IN EFI_HANDLE ImageHandle,
328 IN EFI_SYSTEM_TABLE *SystemTable
329 )
330{
331 return EFI_SUCCESS;
332}
333"""),
334TemplateString("""
52302d4d 335const UINT32 _gUefiDriverRevision = ${UefiSpecVersion};
30fdf114
LG
336const UINT32 _gDxeRevision = ${PiSpecVersion};
337
338${BEGIN}
339EFI_STATUS
340EFIAPI
341ProcessModuleEntryPointList (
342 IN EFI_HANDLE ImageHandle,
343 IN EFI_SYSTEM_TABLE *SystemTable
344 )
345
346{
347 return ${Function} (ImageHandle, SystemTable);
348}
349${END}
350VOID
351EFIAPI
352ExitDriver (
353 IN EFI_STATUS Status
354 )
355{
356 if (EFI_ERROR (Status)) {
357 ProcessLibraryDestructorList (gImageHandle, gST);
358 }
359 gBS->Exit (gImageHandle, Status, 0, NULL);
360}
361"""),
362TemplateString("""
52302d4d 363const UINT32 _gUefiDriverRevision = ${UefiSpecVersion};
30fdf114
LG
364const UINT32 _gDxeRevision = ${PiSpecVersion};
365
52302d4d
LG
366static BASE_LIBRARY_JUMP_BUFFER mJumpContext;
367static EFI_STATUS mDriverEntryPointStatus;
368
30fdf114
LG
369EFI_STATUS
370EFIAPI
371ProcessModuleEntryPointList (
372 IN EFI_HANDLE ImageHandle,
373 IN EFI_SYSTEM_TABLE *SystemTable
374 )
30fdf114 375{
52302d4d 376 mDriverEntryPointStatus = EFI_LOAD_ERROR;
30fdf114
LG
377 ${BEGIN}
378 if (SetJump (&mJumpContext) == 0) {
379 ExitDriver (${Function} (ImageHandle, SystemTable));
380 ASSERT (FALSE);
381 }
382 ${END}
383 return mDriverEntryPointStatus;
384}
385
30fdf114
LG
386VOID
387EFIAPI
388ExitDriver (
389 IN EFI_STATUS Status
390 )
391{
392 if (!EFI_ERROR (Status) || EFI_ERROR (mDriverEntryPointStatus)) {
393 mDriverEntryPointStatus = Status;
394 }
395 LongJump (&mJumpContext, (UINTN)-1);
396 ASSERT (FALSE);
397}
398""")
399]
400
401
402## UEFI Application Entry Point Templates
403gUefiApplicationEntryPointPrototype = TemplateString("""
404${BEGIN}
405EFI_STATUS
406EFIAPI
407${Function} (
408 IN EFI_HANDLE ImageHandle,
409 IN EFI_SYSTEM_TABLE *SystemTable
410 );
411${END}
412""")
413
414gUefiApplicationEntryPointString = [
415TemplateString("""
52302d4d 416const UINT32 _gUefiDriverRevision = ${UefiSpecVersion};
30fdf114
LG
417
418EFI_STATUS
419EFIAPI
420ProcessModuleEntryPointList (
421 IN EFI_HANDLE ImageHandle,
422 IN EFI_SYSTEM_TABLE *SystemTable
423 )
424{
425 return EFI_SUCCESS;
426}
427"""),
428TemplateString("""
52302d4d 429const UINT32 _gUefiDriverRevision = ${UefiSpecVersion};
30fdf114
LG
430
431${BEGIN}
432EFI_STATUS
433EFIAPI
434ProcessModuleEntryPointList (
435 IN EFI_HANDLE ImageHandle,
436 IN EFI_SYSTEM_TABLE *SystemTable
437 )
438
439{
440 return ${Function} (ImageHandle, SystemTable);
441}
442${END}
443VOID
444EFIAPI
445ExitDriver (
446 IN EFI_STATUS Status
447 )
448{
449 if (EFI_ERROR (Status)) {
450 ProcessLibraryDestructorList (gImageHandle, gST);
451 }
452 gBS->Exit (gImageHandle, Status, 0, NULL);
453}
454"""),
455TemplateString("""
52302d4d 456const UINT32 _gUefiDriverRevision = ${UefiSpecVersion};
30fdf114
LG
457
458EFI_STATUS
459EFIAPI
460ProcessModuleEntryPointList (
461 IN EFI_HANDLE ImageHandle,
462 IN EFI_SYSTEM_TABLE *SystemTable
463 )
464
465{
466 ${BEGIN}
467 if (SetJump (&mJumpContext) == 0) {
468 ExitDriver (${Function} (ImageHandle, SystemTable));
469 ASSERT (FALSE);
470 }
471 ${END}
472 return mDriverEntryPointStatus;
473}
474
475static BASE_LIBRARY_JUMP_BUFFER mJumpContext;
476static EFI_STATUS mDriverEntryPointStatus = EFI_LOAD_ERROR;
477
478VOID
479EFIAPI
480ExitDriver (
481 IN EFI_STATUS Status
482 )
483{
484 if (!EFI_ERROR (Status) || EFI_ERROR (mDriverEntryPointStatus)) {
485 mDriverEntryPointStatus = Status;
486 }
487 LongJump (&mJumpContext, (UINTN)-1);
488 ASSERT (FALSE);
489}
490""")
491]
492
493## UEFI Unload Image Templates
494gUefiUnloadImagePrototype = TemplateString("""
495${BEGIN}
496EFI_STATUS
497EFIAPI
498${Function} (
499 IN EFI_HANDLE ImageHandle
500 );
501${END}
502""")
503
504gUefiUnloadImageString = [
505TemplateString("""
506GLOBAL_REMOVE_IF_UNREFERENCED const UINT8 _gDriverUnloadImageCount = ${Count};
507
508EFI_STATUS
509EFIAPI
510ProcessModuleUnloadList (
511 IN EFI_HANDLE ImageHandle
512 )
513{
514 return EFI_SUCCESS;
515}
516"""),
517TemplateString("""
518GLOBAL_REMOVE_IF_UNREFERENCED const UINT8 _gDriverUnloadImageCount = ${Count};
519
520${BEGIN}
521EFI_STATUS
522EFIAPI
523ProcessModuleUnloadList (
524 IN EFI_HANDLE ImageHandle
525 )
526{
527 return ${Function} (ImageHandle);
528}
529${END}
530"""),
531TemplateString("""
532GLOBAL_REMOVE_IF_UNREFERENCED const UINT8 _gDriverUnloadImageCount = ${Count};
533
534EFI_STATUS
535EFIAPI
536ProcessModuleUnloadList (
537 IN EFI_HANDLE ImageHandle
538 )
539{
540 EFI_STATUS Status;
541
542 Status = EFI_SUCCESS;
543${BEGIN}
544 if (EFI_ERROR (Status)) {
545 ${Function} (ImageHandle);
546 } else {
547 Status = ${Function} (ImageHandle);
548 }
549${END}
550 return Status;
551}
552""")
553]
554
555gLibraryStructorPrototype = {
556'BASE' : TemplateString("""${BEGIN}
557RETURN_STATUS
558EFIAPI
559${Function} (
560 VOID
561 );${END}
562"""),
563
564'PEI' : TemplateString("""${BEGIN}
565EFI_STATUS
566EFIAPI
567${Function} (
568 IN EFI_PEI_FILE_HANDLE FileHandle,
569 IN CONST EFI_PEI_SERVICES **PeiServices
570 );${END}
571"""),
572
573'DXE' : TemplateString("""${BEGIN}
574EFI_STATUS
575EFIAPI
576${Function} (
577 IN EFI_HANDLE ImageHandle,
578 IN EFI_SYSTEM_TABLE *SystemTable
579 );${END}
580"""),
581}
582
583gLibraryStructorCall = {
584'BASE' : TemplateString("""${BEGIN}
585 Status = ${Function} ();
586 ASSERT_EFI_ERROR (Status);${END}
587"""),
588
589'PEI' : TemplateString("""${BEGIN}
590 Status = ${Function} (FileHandle, PeiServices);
591 ASSERT_EFI_ERROR (Status);${END}
592"""),
593
594'DXE' : TemplateString("""${BEGIN}
595 Status = ${Function} (ImageHandle, SystemTable);
596 ASSERT_EFI_ERROR (Status);${END}
597"""),
598}
599
600## Library Constructor and Destructor Templates
601gLibraryString = {
602'BASE' : TemplateString("""
603${BEGIN}${FunctionPrototype}${END}
604
605VOID
606EFIAPI
607ProcessLibrary${Type}List (
608 VOID
609 )
610{
611${BEGIN} EFI_STATUS Status;
612${FunctionCall}${END}
613}
614"""),
615
616'PEI' : TemplateString("""
617${BEGIN}${FunctionPrototype}${END}
618
619VOID
620EFIAPI
621ProcessLibrary${Type}List (
622 IN EFI_PEI_FILE_HANDLE FileHandle,
623 IN CONST EFI_PEI_SERVICES **PeiServices
624 )
625{
626${BEGIN} EFI_STATUS Status;
627${FunctionCall}${END}
628}
629"""),
630
631'DXE' : TemplateString("""
632${BEGIN}${FunctionPrototype}${END}
633
634VOID
635EFIAPI
636ProcessLibrary${Type}List (
637 IN EFI_HANDLE ImageHandle,
638 IN EFI_SYSTEM_TABLE *SystemTable
639 )
640{
641${BEGIN} EFI_STATUS Status;
642${FunctionCall}${END}
643}
644"""),
645}
646
30fdf114
LG
647gBasicHeaderFile = "Base.h"
648
649gModuleTypeHeaderFile = {
650 "BASE" : [gBasicHeaderFile],
651 "SEC" : ["PiPei.h", "Library/DebugLib.h"],
652 "PEI_CORE" : ["PiPei.h", "Library/DebugLib.h", "Library/PeiCoreEntryPoint.h"],
653 "PEIM" : ["PiPei.h", "Library/DebugLib.h", "Library/PeimEntryPoint.h"],
654 "DXE_CORE" : ["PiDxe.h", "Library/DebugLib.h", "Library/DxeCoreEntryPoint.h"],
655 "DXE_DRIVER" : ["PiDxe.h", "Library/BaseLib.h", "Library/DebugLib.h", "Library/UefiBootServicesTableLib.h", "Library/UefiDriverEntryPoint.h"],
656 "DXE_SMM_DRIVER" : ["PiDxe.h", "Library/BaseLib.h", "Library/DebugLib.h", "Library/UefiBootServicesTableLib.h", "Library/UefiDriverEntryPoint.h"],
657 "DXE_RUNTIME_DRIVER": ["PiDxe.h", "Library/BaseLib.h", "Library/DebugLib.h", "Library/UefiBootServicesTableLib.h", "Library/UefiDriverEntryPoint.h"],
658 "DXE_SAL_DRIVER" : ["PiDxe.h", "Library/BaseLib.h", "Library/DebugLib.h", "Library/UefiBootServicesTableLib.h", "Library/UefiDriverEntryPoint.h"],
659 "UEFI_DRIVER" : ["Uefi.h", "Library/BaseLib.h", "Library/DebugLib.h", "Library/UefiBootServicesTableLib.h", "Library/UefiDriverEntryPoint.h"],
660 "UEFI_APPLICATION" : ["Uefi.h", "Library/BaseLib.h", "Library/DebugLib.h", "Library/UefiBootServicesTableLib.h", "Library/UefiApplicationEntryPoint.h"],
b303ea72 661 "SMM_CORE" : ["PiDxe.h", "Library/BaseLib.h", "Library/DebugLib.h", "Library/UefiDriverEntryPoint.h"],
30fdf114
LG
662 "USER_DEFINED" : [gBasicHeaderFile]
663}
664
e8a47801
LG
665## Autogen internal worker macro to define DynamicEx PCD name includes both the TokenSpaceGuidName
666# the TokenName and Guid comparison to avoid define name collisions.
667#
668# @param Info The ModuleAutoGen object
669# @param AutoGenH The TemplateString object for header file
670#
671#
672def DynExPcdTokenNumberMapping(Info, AutoGenH):
673 ExTokenCNameList = []
674 PcdExList = []
675 if Info.IsLibrary:
676 PcdList = Info.LibraryPcdList
677 else:
678 PcdList = Info.ModulePcdList
679 for Pcd in PcdList:
680 if Pcd.Type in gDynamicExPcd:
681 ExTokenCNameList.append(Pcd.TokenCName)
682 PcdExList.append(Pcd)
683 if len(ExTokenCNameList) == 0:
684 return
685 AutoGenH.Append('\n#define COMPAREGUID(Guid1, Guid2) (BOOLEAN)(*(CONST UINT64*)Guid1 == *(CONST UINT64*)Guid2 && *((CONST UINT64*)Guid1 + 1) == *((CONST UINT64*)Guid2 + 1))\n')
686 # AutoGen for each PCD listed in a [PcdEx] section of a Module/Lib INF file.
687 # Auto generate a macro for each TokenName that takes a Guid pointer as a parameter.
688 # Use the Guid pointer to see if it matches any of the token space GUIDs.
689 TokenCNameList = []
690 for TokenCName in ExTokenCNameList:
691 if TokenCName in TokenCNameList:
692 continue
693 Index = 0
694 Count = ExTokenCNameList.count(TokenCName)
695 for Pcd in PcdExList:
696 if Pcd.TokenCName == TokenCName:
697 Index = Index + 1
698 if Index == 1:
699 AutoGenH.Append('\n#define __PCD_%s_ADDR_CMP(GuidPtr) (' % (Pcd.TokenCName))
700 AutoGenH.Append('\\\n (GuidPtr == &%s) ? _PCD_TOKEN_%s_%s:'
701 % (Pcd.TokenSpaceGuidCName, Pcd.TokenSpaceGuidCName, Pcd.TokenCName))
702 else:
703 AutoGenH.Append('\\\n (GuidPtr == &%s) ? _PCD_TOKEN_%s_%s:'
704 % (Pcd.TokenSpaceGuidCName, Pcd.TokenSpaceGuidCName, Pcd.TokenCName))
705 if Index == Count:
706 AutoGenH.Append('0 \\\n )\n')
707 TokenCNameList.append(TokenCName)
708
709 TokenCNameList = []
710 for TokenCName in ExTokenCNameList:
711 if TokenCName in TokenCNameList:
712 continue
713 Index = 0
714 Count = ExTokenCNameList.count(TokenCName)
715 for Pcd in PcdExList:
716 if Pcd.Type in gDynamicExPcd and Pcd.TokenCName == TokenCName:
717 Index = Index + 1
718 if Index == 1:
719 AutoGenH.Append('\n#define __PCD_%s_VAL_CMP(GuidPtr) (' % (Pcd.TokenCName))
720 AutoGenH.Append('\\\n COMPAREGUID (GuidPtr, &%s) ? _PCD_TOKEN_%s_%s:'
721 % (Pcd.TokenSpaceGuidCName, Pcd.TokenSpaceGuidCName, Pcd.TokenCName))
722 else:
723 AutoGenH.Append('\\\n COMPAREGUID (GuidPtr, &%s) ? _PCD_TOKEN_%s_%s:'
724 % (Pcd.TokenSpaceGuidCName, Pcd.TokenSpaceGuidCName, Pcd.TokenCName))
725 if Index == Count:
726 AutoGenH.Append('0 \\\n )\n')
727 # Autogen internal worker macro to compare GUIDs. Guid1 is a pointer to a GUID.
728 # Guid2 is a C name for a GUID. Compare pointers first because optimizing compiler
729 # can do this at build time on CONST GUID pointers and optimize away call to COMPAREGUID().
730 # COMPAREGUID() will only be used if the Guid passed in is local to the module.
731 AutoGenH.Append('#define _PCD_TOKEN_EX_%s(GuidPtr) __PCD_%s_ADDR_CMP(GuidPtr) ? __PCD_%s_ADDR_CMP(GuidPtr) : __PCD_%s_VAL_CMP(GuidPtr) \n'
732 % (Pcd.TokenCName, Pcd.TokenCName, Pcd.TokenCName, Pcd.TokenCName))
733 TokenCNameList.append(TokenCName)
734
30fdf114
LG
735## Create code for module PCDs
736#
737# @param Info The ModuleAutoGen object
738# @param AutoGenC The TemplateString object for C code
739# @param AutoGenH The TemplateString object for header file
740# @param Pcd The PCD object
741#
742def CreateModulePcdCode(Info, AutoGenC, AutoGenH, Pcd):
743 TokenSpaceGuidValue = Pcd.TokenSpaceGuidValue #Info.GuidList[Pcd.TokenSpaceGuidCName]
744 PcdTokenNumber = Info.PlatformInfo.PcdTokenNumber
745 #
746 # Write PCDs
747 #
748 PcdTokenName = '_PCD_TOKEN_' + Pcd.TokenCName
749 if Pcd.Type in gDynamicExPcd:
750 TokenNumber = int(Pcd.TokenValue, 0)
e8a47801
LG
751 # Add TokenSpaceGuidValue value to PcdTokenName to discriminate the DynamicEx PCDs with
752 # different Guids but same TokenCName
753 PcdExTokenName = '_PCD_TOKEN_' + Pcd.TokenSpaceGuidCName + '_' + Pcd.TokenCName
754 AutoGenH.Append('\n#define %s %dU\n' % (PcdExTokenName, TokenNumber))
30fdf114
LG
755 else:
756 if (Pcd.TokenCName, Pcd.TokenSpaceGuidCName) not in PcdTokenNumber:
e8a47801
LG
757 # If one of the Source built modules listed in the DSC is not listed in FDF modules,
758 # and the INF lists a PCD can only use the PcdsDynamic access method (it is only
759 # listed in the DEC file that declares the PCD as PcdsDynamic), then build tool will
760 # report warning message notify the PI that they are attempting to build a module
761 # that must be included in a flash image in order to be functional. These Dynamic PCD
762 # will not be added into the Database unless it is used by other modules that are
763 # included in the FDF file.
764 # In this case, just assign an invalid token number to make it pass build.
765 if Pcd.Type in PCD_DYNAMIC_TYPE_LIST:
766 TokenNumber = 0
767 else:
768 EdkLogger.error("build", AUTOGEN_ERROR,
769 "No generated token number for %s.%s\n" % (Pcd.TokenSpaceGuidCName, Pcd.TokenCName),
770 ExtraData="[%s]" % str(Info))
771 else:
772 TokenNumber = PcdTokenNumber[Pcd.TokenCName, Pcd.TokenSpaceGuidCName]
773 AutoGenH.Append('\n#define %s %dU\n' % (PcdTokenName, TokenNumber))
30fdf114
LG
774
775 EdkLogger.debug(EdkLogger.DEBUG_3, "Creating code for " + Pcd.TokenCName + "." + Pcd.TokenSpaceGuidCName)
776 if Pcd.Type not in gItemTypeStringDatabase:
777 EdkLogger.error("build", AUTOGEN_ERROR,
778 "Unknown PCD type [%s] of PCD %s.%s" % (Pcd.Type, Pcd.TokenSpaceGuidCName, Pcd.TokenCName),
779 ExtraData="[%s]" % str(Info))
780 if Pcd.DatumType not in gDatumSizeStringDatabase:
781 EdkLogger.error("build", AUTOGEN_ERROR,
782 "Unknown datum type [%s] of PCD %s.%s" % (Pcd.DatumType, Pcd.TokenSpaceGuidCName, Pcd.TokenCName),
783 ExtraData="[%s]" % str(Info))
784
785 DatumSize = gDatumSizeStringDatabase[Pcd.DatumType]
786 DatumSizeLib = gDatumSizeStringDatabaseLib[Pcd.DatumType]
787 GetModeName = '_PCD_GET_MODE_' + gDatumSizeStringDatabaseH[Pcd.DatumType] + '_' + Pcd.TokenCName
788 SetModeName = '_PCD_SET_MODE_' + gDatumSizeStringDatabaseH[Pcd.DatumType] + '_' + Pcd.TokenCName
789
e8a47801 790 PcdExCNameList = []
30fdf114 791 if Pcd.Type in gDynamicExPcd:
e8a47801
LG
792 if Info.IsLibrary:
793 PcdList = Info.LibraryPcdList
30fdf114 794 else:
e8a47801
LG
795 PcdList = Info.ModulePcdList
796 for PcdModule in PcdList:
797 if PcdModule.Type in gDynamicExPcd:
798 PcdExCNameList.append(PcdModule.TokenCName)
799 # Be compatible with the current code which using PcdToken and PcdGet/Set for DynamicEx Pcd.
800 # If only PcdToken and PcdGet/Set used in all Pcds with different CName, it should succeed to build.
801 # If PcdToken and PcdGet/Set used in the Pcds with different Guids but same CName, it should failed to build.
802 if PcdExCNameList.count(Pcd.TokenCName) > 1:
803 AutoGenH.Append('// Disabled the macros, as PcdToken and PcdGet/Set are not allowed in the case that more than one DynamicEx Pcds are different Guids but same CName.\n')
804 AutoGenH.Append('// #define %s %s\n' % (PcdTokenName, PcdExTokenName))
805 AutoGenH.Append('// #define %s LibPcdGetEx%s(&%s, %s)\n' % (GetModeName, DatumSizeLib, Pcd.TokenSpaceGuidCName, PcdTokenName))
806 if Pcd.DatumType == 'VOID*':
807 AutoGenH.Append('// #define %s(SizeOfBuffer, Buffer) LibPcdSetEx%s(&%s, %s, (SizeOfBuffer), (Buffer))\n' % (SetModeName, DatumSizeLib, Pcd.TokenSpaceGuidCName, PcdTokenName))
808 else:
809 AutoGenH.Append('// #define %s(Value) LibPcdSetEx%s(&%s, %s, (Value))\n' % (SetModeName, DatumSizeLib, Pcd.TokenSpaceGuidCName, PcdTokenName))
810 else:
811 AutoGenH.Append('#define %s %s\n' % (PcdTokenName, PcdExTokenName))
812 AutoGenH.Append('#define %s LibPcdGetEx%s(&%s, %s)\n' % (GetModeName, DatumSizeLib, Pcd.TokenSpaceGuidCName, PcdTokenName))
813 if Pcd.DatumType == 'VOID*':
814 AutoGenH.Append('#define %s(SizeOfBuffer, Buffer) LibPcdSetEx%s(&%s, %s, (SizeOfBuffer), (Buffer))\n' % (SetModeName, DatumSizeLib, Pcd.TokenSpaceGuidCName, PcdTokenName))
815 else:
816 AutoGenH.Append('#define %s(Value) LibPcdSetEx%s(&%s, %s, (Value))\n' % (SetModeName, DatumSizeLib, Pcd.TokenSpaceGuidCName, PcdTokenName))
30fdf114
LG
817 elif Pcd.Type in gDynamicPcd:
818 AutoGenH.Append('#define %s LibPcdGet%s(%s)\n' % (GetModeName, DatumSizeLib, PcdTokenName))
819 if Pcd.DatumType == 'VOID*':
820 AutoGenH.Append('#define %s(SizeOfBuffer, Buffer) LibPcdSet%s(%s, (SizeOfBuffer), (Buffer))\n' %(SetModeName, DatumSizeLib, PcdTokenName))
821 else:
822 AutoGenH.Append('#define %s(Value) LibPcdSet%s(%s, (Value))\n' % (SetModeName, DatumSizeLib, PcdTokenName))
823 else:
824 PcdVariableName = '_gPcd_' + gItemTypeStringDatabase[Pcd.Type] + '_' + Pcd.TokenCName
825 Const = 'const'
826 if Pcd.Type == TAB_PCDS_PATCHABLE_IN_MODULE:
827 Const = ''
828 Type = ''
829 Array = ''
52302d4d 830 Value = Pcd.DefaultValue
30fdf114 831 Unicode = False
52302d4d 832 ValueNumber = 0
0d2711a6
LG
833
834 if Pcd.DatumType == 'BOOLEAN':
835 BoolValue = Value.upper()
d0acc87a 836 if BoolValue == 'TRUE' or BoolValue == '1':
d40b2ee6 837 Value = '1U'
d0acc87a 838 elif BoolValue == 'FALSE' or BoolValue == '0':
d40b2ee6 839 Value = '0U'
0d2711a6 840
52302d4d
LG
841 if Pcd.DatumType in ['UINT64', 'UINT32', 'UINT16', 'UINT8']:
842 try:
843 if Value.upper().startswith('0X'):
844 ValueNumber = int (Value, 16)
845 else:
846 ValueNumber = int (Value)
847 except:
848 EdkLogger.error("build", AUTOGEN_ERROR,
849 "PCD value is not valid dec or hex number for datum type [%s] of PCD %s.%s" % (Pcd.DatumType, Pcd.TokenSpaceGuidCName, Pcd.TokenCName),
850 ExtraData="[%s]" % str(Info))
851 if Pcd.DatumType == 'UINT64':
852 if ValueNumber < 0:
853 EdkLogger.error("build", AUTOGEN_ERROR,
854 "PCD can't be set to negative value for datum type [%s] of PCD %s.%s" % (Pcd.DatumType, Pcd.TokenSpaceGuidCName, Pcd.TokenCName),
855 ExtraData="[%s]" % str(Info))
856 elif ValueNumber >= 0x10000000000000000:
857 EdkLogger.error("build", AUTOGEN_ERROR,
858 "Too large PCD value for datum type [%s] of PCD %s.%s" % (Pcd.DatumType, Pcd.TokenSpaceGuidCName, Pcd.TokenCName),
859 ExtraData="[%s]" % str(Info))
860 if not Value.endswith('ULL'):
861 Value += 'ULL'
862 elif Pcd.DatumType == 'UINT32':
863 if ValueNumber < 0:
864 EdkLogger.error("build", AUTOGEN_ERROR,
865 "PCD can't be set to negative value for datum type [%s] of PCD %s.%s" % (Pcd.DatumType, Pcd.TokenSpaceGuidCName, Pcd.TokenCName),
866 ExtraData="[%s]" % str(Info))
867 elif ValueNumber >= 0x100000000:
868 EdkLogger.error("build", AUTOGEN_ERROR,
869 "Too large PCD value for datum type [%s] of PCD %s.%s" % (Pcd.DatumType, Pcd.TokenSpaceGuidCName, Pcd.TokenCName),
870 ExtraData="[%s]" % str(Info))
d40b2ee6
LG
871 if not Value.endswith('U'):
872 Value += 'U'
52302d4d
LG
873 elif Pcd.DatumType == 'UINT16':
874 if ValueNumber < 0:
875 EdkLogger.error("build", AUTOGEN_ERROR,
876 "PCD can't be set to negative value for datum type [%s] of PCD %s.%s" % (Pcd.DatumType, Pcd.TokenSpaceGuidCName, Pcd.TokenCName),
877 ExtraData="[%s]" % str(Info))
878 elif ValueNumber >= 0x10000:
879 EdkLogger.error("build", AUTOGEN_ERROR,
880 "Too large PCD value for datum type [%s] of PCD %s.%s" % (Pcd.DatumType, Pcd.TokenSpaceGuidCName, Pcd.TokenCName),
881 ExtraData="[%s]" % str(Info))
d40b2ee6
LG
882 if not Value.endswith('U'):
883 Value += 'U'
52302d4d
LG
884 elif Pcd.DatumType == 'UINT8':
885 if ValueNumber < 0:
886 EdkLogger.error("build", AUTOGEN_ERROR,
887 "PCD can't be set to negative value for datum type [%s] of PCD %s.%s" % (Pcd.DatumType, Pcd.TokenSpaceGuidCName, Pcd.TokenCName),
888 ExtraData="[%s]" % str(Info))
889 elif ValueNumber >= 0x100:
890 EdkLogger.error("build", AUTOGEN_ERROR,
891 "Too large PCD value for datum type [%s] of PCD %s.%s" % (Pcd.DatumType, Pcd.TokenSpaceGuidCName, Pcd.TokenCName),
892 ExtraData="[%s]" % str(Info))
d40b2ee6
LG
893 if not Value.endswith('U'):
894 Value += 'U'
30fdf114
LG
895 if Pcd.DatumType == 'VOID*':
896 if Pcd.MaxDatumSize == None or Pcd.MaxDatumSize == '':
897 EdkLogger.error("build", AUTOGEN_ERROR,
898 "Unknown [MaxDatumSize] of PCD [%s.%s]" % (Pcd.TokenSpaceGuidCName, Pcd.TokenCName),
899 ExtraData="[%s]" % str(Info))
900
901 ArraySize = int(Pcd.MaxDatumSize, 0)
902 if Value[0] == '{':
903 Type = '(VOID *)'
52302d4d 904 else:
30fdf114
LG
905 if Value[0] == 'L':
906 Unicode = True
907 Value = Value.lstrip('L') #.strip('"')
908 Value = eval(Value) # translate escape character
909 NewValue = '{'
910 for Index in range(0,len(Value)):
911 if Unicode:
52302d4d
LG
912 NewValue = NewValue + str(ord(Value[Index]) % 0x10000) + ', '
913 else:
30fdf114 914 NewValue = NewValue + str(ord(Value[Index]) % 0x100) + ', '
52302d4d
LG
915 if Unicode:
916 ArraySize = ArraySize / 2;
917
30fdf114 918 if ArraySize < (len(Value) + 1):
e56468c0 919 EdkLogger.error("build", AUTOGEN_ERROR,
920 "The maximum size of VOID* type PCD '%s.%s' is less than its actual size occupied." % (Pcd.TokenSpaceGuidCName, Pcd.TokenCName),
921 ExtraData="[%s]" % str(Info))
52302d4d 922 Value = NewValue + '0 }'
30fdf114
LG
923 Array = '[%d]' % ArraySize
924 #
925 # skip casting for fixed at build since it breaks ARM assembly.
926 # Long term we need PCD macros that work in assembly
927 #
928 elif Pcd.Type != TAB_PCDS_FIXED_AT_BUILD:
929 Value = "((%s)%s)" % (Pcd.DatumType, Value)
930
931 if Pcd.Type == TAB_PCDS_PATCHABLE_IN_MODULE:
932 PcdValueName = '_PCD_PATCHABLE_VALUE_' + Pcd.TokenCName
933 else:
934 PcdValueName = '_PCD_VALUE_' + Pcd.TokenCName
935
52302d4d
LG
936 if Pcd.DatumType == 'VOID*':
937 #
938 # For unicode, UINT16 array will be generated, so the alignment of unicode is guaranteed.
939 #
940 if Unicode:
941 AutoGenH.Append('#define _PCD_PATCHABLE_%s_SIZE %s\n' % (Pcd.TokenCName, Pcd.MaxDatumSize))
942 AutoGenH.Append('#define %s %s%s\n' %(PcdValueName, Type, PcdVariableName))
943 AutoGenC.Append('GLOBAL_REMOVE_IF_UNREFERENCED %s UINT16 %s%s = %s;\n' % (Const, PcdVariableName, Array, Value))
944 AutoGenH.Append('extern %s UINT16 %s%s;\n' %(Const, PcdVariableName, Array))
945 AutoGenH.Append('#define %s %s%s\n' %(GetModeName, Type, PcdVariableName))
30fdf114
LG
946 else:
947 AutoGenH.Append('#define _PCD_PATCHABLE_%s_SIZE %s\n' % (Pcd.TokenCName, Pcd.MaxDatumSize))
948 AutoGenH.Append('#define %s %s%s\n' %(PcdValueName, Type, PcdVariableName))
949 AutoGenC.Append('GLOBAL_REMOVE_IF_UNREFERENCED %s UINT8 %s%s = %s;\n' % (Const, PcdVariableName, Array, Value))
950 AutoGenH.Append('extern %s UINT8 %s%s;\n' %(Const, PcdVariableName, Array))
951 AutoGenH.Append('#define %s %s%s\n' %(GetModeName, Type, PcdVariableName))
952 elif Pcd.Type == TAB_PCDS_PATCHABLE_IN_MODULE:
953 AutoGenH.Append('#define %s %s\n' %(PcdValueName, Value))
52302d4d 954 AutoGenC.Append('volatile %s %s %s = %s;\n' %(Const, Pcd.DatumType, PcdVariableName, PcdValueName))
30fdf114
LG
955 AutoGenH.Append('extern volatile %s %s %s%s;\n' % (Const, Pcd.DatumType, PcdVariableName, Array))
956 AutoGenH.Append('#define %s %s%s\n' % (GetModeName, Type, PcdVariableName))
957 else:
958 AutoGenH.Append('#define %s %s\n' %(PcdValueName, Value))
959 AutoGenC.Append('GLOBAL_REMOVE_IF_UNREFERENCED %s %s %s = %s;\n' %(Const, Pcd.DatumType, PcdVariableName, PcdValueName))
960 AutoGenH.Append('extern %s %s %s%s;\n' % (Const, Pcd.DatumType, PcdVariableName, Array))
961 AutoGenH.Append('#define %s %s%s\n' % (GetModeName, Type, PcdVariableName))
962
963 if Pcd.Type == TAB_PCDS_PATCHABLE_IN_MODULE:
964 if Pcd.DatumType == 'VOID*':
965 AutoGenH.Append('#define %s(SizeOfBuffer, Buffer) LibPatchPcdSetPtr(_gPcd_BinaryPatch_%s, (UINTN)_PCD_PATCHABLE_%s_SIZE, (SizeOfBuffer), (Buffer))\n' % (SetModeName, Pcd.TokenCName, Pcd.TokenCName))
966 else:
967 AutoGenH.Append('#define %s(Value) (%s = (Value))\n' % (SetModeName, PcdVariableName))
968 else:
969 AutoGenH.Append('//#define %s ASSERT(FALSE) // It is not allowed to set value for a FIXED_AT_BUILD PCD\n' % SetModeName)
970
971## Create code for library module PCDs
972#
973# @param Info The ModuleAutoGen object
974# @param AutoGenC The TemplateString object for C code
975# @param AutoGenH The TemplateString object for header file
976# @param Pcd The PCD object
977#
978def CreateLibraryPcdCode(Info, AutoGenC, AutoGenH, Pcd):
979 PcdTokenNumber = Info.PlatformInfo.PcdTokenNumber
980 TokenSpaceGuidCName = Pcd.TokenSpaceGuidCName
981 TokenCName = Pcd.TokenCName
e8a47801
LG
982 PcdTokenName = '_PCD_TOKEN_' + TokenCName
983 #
984 # Write PCDs
985 #
da92f276
LG
986 if Pcd.Type in gDynamicExPcd:
987 TokenNumber = int(Pcd.TokenValue, 0)
e8a47801
LG
988 else:
989 if (Pcd.TokenCName, Pcd.TokenSpaceGuidCName) not in PcdTokenNumber:
990 # If one of the Source built modules listed in the DSC is not listed in FDF modules,
991 # and the INF lists a PCD can only use the PcdsDynamic access method (it is only
992 # listed in the DEC file that declares the PCD as PcdsDynamic), then build tool will
993 # report warning message notify the PI that they are attempting to build a module
994 # that must be included in a flash image in order to be functional. These Dynamic PCD
995 # will not be added into the Database unless it is used by other modules that are
996 # included in the FDF file.
997 # In this case, just assign an invalid token number to make it pass build.
998 if Pcd.Type in PCD_DYNAMIC_TYPE_LIST:
999 TokenNumber = 0
1000 else:
1001 EdkLogger.error("build", AUTOGEN_ERROR,
1002 "No generated token number for %s.%s\n" % (Pcd.TokenSpaceGuidCName, Pcd.TokenCName),
1003 ExtraData="[%s]" % str(Info))
1004 else:
1005 TokenNumber = PcdTokenNumber[Pcd.TokenCName, Pcd.TokenSpaceGuidCName]
da92f276 1006
30fdf114
LG
1007 if Pcd.Type not in gItemTypeStringDatabase:
1008 EdkLogger.error("build", AUTOGEN_ERROR,
1009 "Unknown PCD type [%s] of PCD %s.%s" % (Pcd.Type, Pcd.TokenSpaceGuidCName, Pcd.TokenCName),
1010 ExtraData="[%s]" % str(Info))
1011 if Pcd.DatumType not in gDatumSizeStringDatabase:
1012 EdkLogger.error("build", AUTOGEN_ERROR,
1013 "Unknown datum type [%s] of PCD %s.%s" % (Pcd.DatumType, Pcd.TokenSpaceGuidCName, Pcd.TokenCName),
1014 ExtraData="[%s]" % str(Info))
1015
1016 DatumType = Pcd.DatumType
1017 DatumSize = gDatumSizeStringDatabaseH[DatumType]
1018 DatumSizeLib= gDatumSizeStringDatabaseLib[DatumType]
1019 GetModeName = '_PCD_GET_MODE_' + DatumSize + '_' + TokenCName
1020 SetModeName = '_PCD_SET_MODE_' + DatumSize + '_' + TokenCName
1021
1022 Type = ''
1023 Array = ''
1024 if Pcd.DatumType == 'VOID*':
1025 Type = '(VOID *)'
1026 Array = '[]'
30fdf114 1027 PcdItemType = Pcd.Type
e8a47801 1028 PcdExCNameList = []
30fdf114 1029 if PcdItemType in gDynamicExPcd:
e8a47801
LG
1030 PcdExTokenName = '_PCD_TOKEN_' + TokenSpaceGuidCName + '_' + Pcd.TokenCName
1031 AutoGenH.Append('\n#define %s %dU\n' % (PcdExTokenName, TokenNumber))
1032
1033 if Info.IsLibrary:
1034 PcdList = Info.LibraryPcdList
1035 else:
1036 PcdList = Info.ModulePcdList
1037 for PcdModule in PcdList:
1038 if PcdModule.Type in gDynamicExPcd:
1039 PcdExCNameList.append(PcdModule.TokenCName)
1040 # Be compatible with the current code which using PcdGet/Set for DynamicEx Pcd.
1041 # If only PcdGet/Set used in all Pcds with different CName, it should succeed to build.
1042 # If PcdGet/Set used in the Pcds with different Guids but same CName, it should failed to build.
1043 if PcdExCNameList.count(Pcd.TokenCName) > 1:
1044 AutoGenH.Append('// Disabled the macros, as PcdToken and PcdGet/Set are not allowed in the case that more than one DynamicEx Pcds are different Guids but same CName.\n')
1045 AutoGenH.Append('// #define %s %s\n' % (PcdTokenName, PcdExTokenName))
1046 AutoGenH.Append('// #define %s LibPcdGetEx%s(&%s, %s)\n' % (GetModeName, DatumSizeLib, Pcd.TokenSpaceGuidCName, PcdTokenName))
1047 if Pcd.DatumType == 'VOID*':
1048 AutoGenH.Append('// #define %s(SizeOfBuffer, Buffer) LibPcdSetEx%s(&%s, %s, (SizeOfBuffer), (Buffer))\n' % (SetModeName, DatumSizeLib, Pcd.TokenSpaceGuidCName, PcdTokenName))
1049 else:
1050 AutoGenH.Append('// #define %s(Value) LibPcdSetEx%s(&%s, %s, (Value))\n' % (SetModeName, DatumSizeLib, Pcd.TokenSpaceGuidCName, PcdTokenName))
30fdf114 1051 else:
e8a47801
LG
1052 AutoGenH.Append('#define %s %s\n' % (PcdTokenName, PcdExTokenName))
1053 AutoGenH.Append('#define %s LibPcdGetEx%s(&%s, %s)\n' % (GetModeName, DatumSizeLib, Pcd.TokenSpaceGuidCName, PcdTokenName))
1054 if Pcd.DatumType == 'VOID*':
1055 AutoGenH.Append('#define %s(SizeOfBuffer, Buffer) LibPcdSetEx%s(&%s, %s, (SizeOfBuffer), (Buffer))\n' % (SetModeName, DatumSizeLib, Pcd.TokenSpaceGuidCName, PcdTokenName))
1056 else:
1057 AutoGenH.Append('#define %s(Value) LibPcdSetEx%s(&%s, %s, (Value))\n' % (SetModeName, DatumSizeLib, Pcd.TokenSpaceGuidCName, PcdTokenName))
1058 else:
1059 AutoGenH.Append('#define _PCD_TOKEN_%s %dU\n' % (TokenCName, TokenNumber))
30fdf114 1060 if PcdItemType in gDynamicPcd:
30fdf114
LG
1061 AutoGenH.Append('#define %s LibPcdGet%s(%s)\n' % (GetModeName, DatumSizeLib, PcdTokenName))
1062 if DatumType == 'VOID*':
1063 AutoGenH.Append('#define %s(SizeOfBuffer, Buffer) LibPcdSet%s(%s, (SizeOfBuffer), (Buffer))\n' %(SetModeName, DatumSizeLib, PcdTokenName))
1064 else:
1065 AutoGenH.Append('#define %s(Value) LibPcdSet%s(%s, (Value))\n' % (SetModeName, DatumSizeLib, PcdTokenName))
1066 if PcdItemType == TAB_PCDS_PATCHABLE_IN_MODULE:
1067 PcdVariableName = '_gPcd_' + gItemTypeStringDatabase[TAB_PCDS_PATCHABLE_IN_MODULE] + '_' + TokenCName
e8a47801 1068 AutoGenH.Append('extern volatile %s _gPcd_BinaryPatch_%s%s;\n' %(DatumType, TokenCName, Array) )
30fdf114
LG
1069 AutoGenH.Append('#define %s %s_gPcd_BinaryPatch_%s\n' %(GetModeName, Type, TokenCName))
1070 AutoGenH.Append('#define %s(Value) (%s = (Value))\n' % (SetModeName, PcdVariableName))
1071 if PcdItemType == TAB_PCDS_FIXED_AT_BUILD or PcdItemType == TAB_PCDS_FEATURE_FLAG:
1072 AutoGenH.Append('extern const %s _gPcd_FixedAtBuild_%s%s;\n' %(DatumType, TokenCName, Array))
30fdf114
LG
1073 AutoGenH.Append('#define %s %s_gPcd_FixedAtBuild_%s\n' %(GetModeName, Type, TokenCName))
1074 AutoGenH.Append('//#define %s ASSERT(FALSE) // It is not allowed to set value for a FIXED_AT_BUILD PCD\n' % SetModeName)
1075
30fdf114 1076
30fdf114
LG
1077
1078## Create code for library constructor
1079#
1080# @param Info The ModuleAutoGen object
1081# @param AutoGenC The TemplateString object for C code
1082# @param AutoGenH The TemplateString object for header file
1083#
1084def CreateLibraryConstructorCode(Info, AutoGenC, AutoGenH):
1085 #
1086 # Library Constructors
1087 #
1088 ConstructorPrototypeString = TemplateString()
1089 ConstructorCallingString = TemplateString()
1090 if Info.IsLibrary:
1091 DependentLibraryList = [Info.Module]
1092 else:
1093 DependentLibraryList = Info.DependentLibraryList
1094 for Lib in DependentLibraryList:
1095 if len(Lib.ConstructorList) <= 0:
1096 continue
1097 Dict = {'Function':Lib.ConstructorList}
1098 if Lib.ModuleType in ['BASE', 'SEC']:
1099 ConstructorPrototypeString.Append(gLibraryStructorPrototype['BASE'].Replace(Dict))
1100 ConstructorCallingString.Append(gLibraryStructorCall['BASE'].Replace(Dict))
1101 elif Lib.ModuleType in ['PEI_CORE','PEIM']:
1102 ConstructorPrototypeString.Append(gLibraryStructorPrototype['PEI'].Replace(Dict))
1103 ConstructorCallingString.Append(gLibraryStructorCall['PEI'].Replace(Dict))
1104 elif Lib.ModuleType in ['DXE_CORE','DXE_DRIVER','DXE_SMM_DRIVER','DXE_RUNTIME_DRIVER',
b303ea72 1105 'DXE_SAL_DRIVER','UEFI_DRIVER','UEFI_APPLICATION','SMM_CORE']:
30fdf114
LG
1106 ConstructorPrototypeString.Append(gLibraryStructorPrototype['DXE'].Replace(Dict))
1107 ConstructorCallingString.Append(gLibraryStructorCall['DXE'].Replace(Dict))
1108
1109 if str(ConstructorPrototypeString) == '':
1110 ConstructorPrototypeList = []
1111 else:
1112 ConstructorPrototypeList = [str(ConstructorPrototypeString)]
1113 if str(ConstructorCallingString) == '':
1114 ConstructorCallingList = []
1115 else:
1116 ConstructorCallingList = [str(ConstructorCallingString)]
1117
1118 Dict = {
1119 'Type' : 'Constructor',
1120 'FunctionPrototype' : ConstructorPrototypeList,
1121 'FunctionCall' : ConstructorCallingList
1122 }
1123 if Info.IsLibrary:
1124 AutoGenH.Append("${BEGIN}${FunctionPrototype}${END}", Dict)
1125 else:
1126 if Info.ModuleType in ['BASE', 'SEC']:
1127 AutoGenC.Append(gLibraryString['BASE'].Replace(Dict))
1128 elif Info.ModuleType in ['PEI_CORE','PEIM']:
1129 AutoGenC.Append(gLibraryString['PEI'].Replace(Dict))
1130 elif Info.ModuleType in ['DXE_CORE','DXE_DRIVER','DXE_SMM_DRIVER','DXE_RUNTIME_DRIVER',
b303ea72 1131 'DXE_SAL_DRIVER','UEFI_DRIVER','UEFI_APPLICATION','SMM_CORE']:
30fdf114
LG
1132 AutoGenC.Append(gLibraryString['DXE'].Replace(Dict))
1133
1134## Create code for library destructor
1135#
1136# @param Info The ModuleAutoGen object
1137# @param AutoGenC The TemplateString object for C code
1138# @param AutoGenH The TemplateString object for header file
1139#
1140def CreateLibraryDestructorCode(Info, AutoGenC, AutoGenH):
1141 #
1142 # Library Destructors
1143 #
1144 DestructorPrototypeString = TemplateString()
1145 DestructorCallingString = TemplateString()
1146 if Info.IsLibrary:
1147 DependentLibraryList = [Info.Module]
1148 else:
1149 DependentLibraryList = Info.DependentLibraryList
1150 for Index in range(len(DependentLibraryList)-1, -1, -1):
1151 Lib = DependentLibraryList[Index]
1152 if len(Lib.DestructorList) <= 0:
1153 continue
1154 Dict = {'Function':Lib.DestructorList}
1155 if Lib.ModuleType in ['BASE', 'SEC']:
1156 DestructorPrototypeString.Append(gLibraryStructorPrototype['BASE'].Replace(Dict))
1157 DestructorCallingString.Append(gLibraryStructorCall['BASE'].Replace(Dict))
1158 elif Lib.ModuleType in ['PEI_CORE','PEIM']:
1159 DestructorPrototypeString.Append(gLibraryStructorPrototype['PEI'].Replace(Dict))
1160 DestructorCallingString.Append(gLibraryStructorCall['PEI'].Replace(Dict))
1161 elif Lib.ModuleType in ['DXE_CORE','DXE_DRIVER','DXE_SMM_DRIVER','DXE_RUNTIME_DRIVER',
b303ea72 1162 'DXE_SAL_DRIVER','UEFI_DRIVER','UEFI_APPLICATION', 'SMM_CORE']:
30fdf114
LG
1163 DestructorPrototypeString.Append(gLibraryStructorPrototype['DXE'].Replace(Dict))
1164 DestructorCallingString.Append(gLibraryStructorCall['DXE'].Replace(Dict))
1165
1166 if str(DestructorPrototypeString) == '':
1167 DestructorPrototypeList = []
1168 else:
1169 DestructorPrototypeList = [str(DestructorPrototypeString)]
1170 if str(DestructorCallingString) == '':
1171 DestructorCallingList = []
1172 else:
1173 DestructorCallingList = [str(DestructorCallingString)]
1174
1175 Dict = {
1176 'Type' : 'Destructor',
1177 'FunctionPrototype' : DestructorPrototypeList,
1178 'FunctionCall' : DestructorCallingList
1179 }
1180 if Info.IsLibrary:
1181 AutoGenH.Append("${BEGIN}${FunctionPrototype}${END}", Dict)
1182 else:
1183 if Info.ModuleType in ['BASE', 'SEC']:
1184 AutoGenC.Append(gLibraryString['BASE'].Replace(Dict))
1185 elif Info.ModuleType in ['PEI_CORE','PEIM']:
1186 AutoGenC.Append(gLibraryString['PEI'].Replace(Dict))
1187 elif Info.ModuleType in ['DXE_CORE','DXE_DRIVER','DXE_SMM_DRIVER','DXE_RUNTIME_DRIVER',
b303ea72 1188 'DXE_SAL_DRIVER','UEFI_DRIVER','UEFI_APPLICATION','SMM_CORE']:
30fdf114
LG
1189 AutoGenC.Append(gLibraryString['DXE'].Replace(Dict))
1190
1191
1192## Create code for ModuleEntryPoint
1193#
1194# @param Info The ModuleAutoGen object
1195# @param AutoGenC The TemplateString object for C code
1196# @param AutoGenH The TemplateString object for header file
1197#
1198def CreateModuleEntryPointCode(Info, AutoGenC, AutoGenH):
1199 if Info.IsLibrary or Info.ModuleType in ['USER_DEFINED', 'SEC']:
1200 return
1201 #
1202 # Module Entry Points
1203 #
1204 NumEntryPoints = len(Info.Module.ModuleEntryPointList)
1205 if 'PI_SPECIFICATION_VERSION' in Info.Module.Specification:
1206 PiSpecVersion = Info.Module.Specification['PI_SPECIFICATION_VERSION']
1207 else:
da92f276 1208 PiSpecVersion = '0x00000000'
52302d4d
LG
1209 if 'UEFI_SPECIFICATION_VERSION' in Info.Module.Specification:
1210 UefiSpecVersion = Info.Module.Specification['UEFI_SPECIFICATION_VERSION']
30fdf114 1211 else:
da92f276 1212 UefiSpecVersion = '0x00000000'
30fdf114 1213 Dict = {
52302d4d 1214 'Function' : Info.Module.ModuleEntryPointList,
d0acc87a
LG
1215 'PiSpecVersion' : PiSpecVersion + 'U',
1216 'UefiSpecVersion': UefiSpecVersion + 'U'
30fdf114
LG
1217 }
1218
1219 if Info.ModuleType in ['PEI_CORE', 'DXE_CORE', 'SMM_CORE']:
da92f276
LG
1220 if Info.SourceFileList <> None and Info.SourceFileList <> []:
1221 if NumEntryPoints != 1:
1222 EdkLogger.error(
1223 "build",
1224 AUTOGEN_ERROR,
1225 '%s must have exactly one entry point' % Info.ModuleType,
1226 File=str(Info),
1227 ExtraData= ", ".join(Info.Module.ModuleEntryPointList)
1228 )
30fdf114
LG
1229 if Info.ModuleType == 'PEI_CORE':
1230 AutoGenC.Append(gPeiCoreEntryPointString.Replace(Dict))
1231 AutoGenH.Append(gPeiCoreEntryPointPrototype.Replace(Dict))
1232 elif Info.ModuleType == 'DXE_CORE':
1233 AutoGenC.Append(gDxeCoreEntryPointString.Replace(Dict))
1234 AutoGenH.Append(gDxeCoreEntryPointPrototype.Replace(Dict))
1235 elif Info.ModuleType == 'SMM_CORE':
1236 AutoGenC.Append(gSmmCoreEntryPointString.Replace(Dict))
b303ea72 1237 AutoGenH.Append(gSmmCoreEntryPointPrototype.Replace(Dict))
30fdf114
LG
1238 elif Info.ModuleType == 'PEIM':
1239 if NumEntryPoints < 2:
1240 AutoGenC.Append(gPeimEntryPointString[NumEntryPoints].Replace(Dict))
1241 else:
1242 AutoGenC.Append(gPeimEntryPointString[2].Replace(Dict))
1243 AutoGenH.Append(gPeimEntryPointPrototype.Replace(Dict))
b303ea72
LG
1244 elif Info.ModuleType in ['DXE_RUNTIME_DRIVER','DXE_DRIVER','DXE_SAL_DRIVER','UEFI_DRIVER']:
1245 if NumEntryPoints < 2:
1246 AutoGenC.Append(gUefiDriverEntryPointString[NumEntryPoints].Replace(Dict))
30fdf114 1247 else:
b303ea72
LG
1248 AutoGenC.Append(gUefiDriverEntryPointString[2].Replace(Dict))
1249 AutoGenH.Append(gUefiDriverEntryPointPrototype.Replace(Dict))
1250 elif Info.ModuleType == 'DXE_SMM_DRIVER':
1251 if NumEntryPoints == 0:
1252 AutoGenC.Append(gDxeSmmEntryPointString[0].Replace(Dict))
1253 else:
1254 AutoGenC.Append(gDxeSmmEntryPointString[1].Replace(Dict))
1255 AutoGenH.Append(gDxeSmmEntryPointPrototype.Replace(Dict))
30fdf114
LG
1256 elif Info.ModuleType == 'UEFI_APPLICATION':
1257 if NumEntryPoints < 2:
1258 AutoGenC.Append(gUefiApplicationEntryPointString[NumEntryPoints].Replace(Dict))
1259 else:
1260 AutoGenC.Append(gUefiApplicationEntryPointString[2].Replace(Dict))
1261 AutoGenH.Append(gUefiApplicationEntryPointPrototype.Replace(Dict))
1262
1263## Create code for ModuleUnloadImage
1264#
1265# @param Info The ModuleAutoGen object
1266# @param AutoGenC The TemplateString object for C code
1267# @param AutoGenH The TemplateString object for header file
1268#
1269def CreateModuleUnloadImageCode(Info, AutoGenC, AutoGenH):
1270 if Info.IsLibrary or Info.ModuleType in ['USER_DEFINED', 'SEC']:
1271 return
1272 #
1273 # Unload Image Handlers
1274 #
1275 NumUnloadImage = len(Info.Module.ModuleUnloadImageList)
d0acc87a 1276 Dict = {'Count':str(NumUnloadImage) + 'U', 'Function':Info.Module.ModuleUnloadImageList}
30fdf114
LG
1277 if NumUnloadImage < 2:
1278 AutoGenC.Append(gUefiUnloadImageString[NumUnloadImage].Replace(Dict))
1279 else:
1280 AutoGenC.Append(gUefiUnloadImageString[2].Replace(Dict))
1281 AutoGenH.Append(gUefiUnloadImagePrototype.Replace(Dict))
1282
1283## Create code for GUID
1284#
1285# @param Info The ModuleAutoGen object
1286# @param AutoGenC The TemplateString object for C code
1287# @param AutoGenH The TemplateString object for header file
1288#
1289def CreateGuidDefinitionCode(Info, AutoGenC, AutoGenH):
1290 if Info.IsLibrary:
1291 return
1292
1293 if Info.ModuleType in ["USER_DEFINED", "BASE"]:
1294 GuidType = "GUID"
1295 else:
1296 GuidType = "EFI_GUID"
1297
1298 if Info.GuidList:
1299 AutoGenC.Append("\n// Guids\n")
1300 #
1301 # GUIDs
1302 #
1303 for Key in Info.GuidList:
1304 AutoGenC.Append('GLOBAL_REMOVE_IF_UNREFERENCED %s %s = %s;\n' % (GuidType, Key, Info.GuidList[Key]))
1305
1306## Create code for protocol
1307#
1308# @param Info The ModuleAutoGen object
1309# @param AutoGenC The TemplateString object for C code
1310# @param AutoGenH The TemplateString object for header file
1311#
1312def CreateProtocolDefinitionCode(Info, AutoGenC, AutoGenH):
1313 if Info.IsLibrary:
1314 return
1315
1316 if Info.ModuleType in ["USER_DEFINED", "BASE"]:
1317 GuidType = "GUID"
1318 else:
1319 GuidType = "EFI_GUID"
1320
1321 if Info.ProtocolList:
1322 AutoGenC.Append("\n// Protocols\n")
1323 #
1324 # Protocol GUIDs
1325 #
1326 for Key in Info.ProtocolList:
1327 AutoGenC.Append('GLOBAL_REMOVE_IF_UNREFERENCED %s %s = %s;\n' % (GuidType, Key, Info.ProtocolList[Key]))
1328
1329## Create code for PPI
1330#
1331# @param Info The ModuleAutoGen object
1332# @param AutoGenC The TemplateString object for C code
1333# @param AutoGenH The TemplateString object for header file
1334#
1335def CreatePpiDefinitionCode(Info, AutoGenC, AutoGenH):
1336 if Info.IsLibrary:
1337 return
1338
1339 if Info.ModuleType in ["USER_DEFINED", "BASE"]:
1340 GuidType = "GUID"
1341 else:
1342 GuidType = "EFI_GUID"
1343
1344 if Info.PpiList:
1345 AutoGenC.Append("\n// PPIs\n")
1346 #
1347 # PPI GUIDs
1348 #
1349 for Key in Info.PpiList:
1350 AutoGenC.Append('GLOBAL_REMOVE_IF_UNREFERENCED %s %s = %s;\n' % (GuidType, Key, Info.PpiList[Key]))
1351
1352## Create code for PCD
1353#
1354# @param Info The ModuleAutoGen object
1355# @param AutoGenC The TemplateString object for C code
1356# @param AutoGenH The TemplateString object for header file
1357#
1358def CreatePcdCode(Info, AutoGenC, AutoGenH):
da92f276
LG
1359
1360 # Collect Token Space GUIDs used by DynamicEc PCDs
1361 TokenSpaceList = []
1362 for Pcd in Info.ModulePcdList:
e8a47801 1363 if Pcd.Type in gDynamicExPcd and Pcd.TokenSpaceGuidCName not in TokenSpaceList:
da92f276
LG
1364 TokenSpaceList += [Pcd.TokenSpaceGuidCName]
1365
1366 # Add extern declarations to AutoGen.h if one or more Token Space GUIDs were found
1367 if TokenSpaceList <> []:
1368 AutoGenH.Append("\n// Definition of PCD Token Space GUIDs used in this module\n\n")
1369 if Info.ModuleType in ["USER_DEFINED", "BASE"]:
1370 GuidType = "GUID"
1371 else:
1372 GuidType = "EFI_GUID"
1373 for Item in TokenSpaceList:
1374 AutoGenH.Append('extern %s %s;\n' % (GuidType, Item))
1375
30fdf114
LG
1376 if Info.IsLibrary:
1377 if Info.ModulePcdList:
1378 AutoGenH.Append("\n// PCD definitions\n")
1379 for Pcd in Info.ModulePcdList:
1380 CreateLibraryPcdCode(Info, AutoGenC, AutoGenH, Pcd)
e8a47801 1381 DynExPcdTokenNumberMapping (Info, AutoGenH)
30fdf114
LG
1382 else:
1383 if Info.ModulePcdList:
1384 AutoGenH.Append("\n// Definition of PCDs used in this module\n")
1385 AutoGenC.Append("\n// Definition of PCDs used in this module\n")
1386 for Pcd in Info.ModulePcdList:
1387 CreateModulePcdCode(Info, AutoGenC, AutoGenH, Pcd)
e8a47801 1388 DynExPcdTokenNumberMapping (Info, AutoGenH)
30fdf114
LG
1389 if Info.LibraryPcdList:
1390 AutoGenH.Append("\n// Definition of PCDs used in libraries is in AutoGen.c\n")
1391 AutoGenC.Append("\n// Definition of PCDs used in libraries\n")
1392 for Pcd in Info.LibraryPcdList:
1393 CreateModulePcdCode(Info, AutoGenC, AutoGenC, Pcd)
1394 CreatePcdDatabaseCode(Info, AutoGenC, AutoGenH)
1395
1396## Create code for unicode string definition
1397#
1398# @param Info The ModuleAutoGen object
1399# @param AutoGenC The TemplateString object for C code
1400# @param AutoGenH The TemplateString object for header file
b303ea72
LG
1401# @param UniGenCFlag UniString is generated into AutoGen C file when it is set to True
1402# @param UniGenBinBuffer Buffer to store uni string package data
30fdf114 1403#
b303ea72 1404def CreateUnicodeStringCode(Info, AutoGenC, AutoGenH, UniGenCFlag, UniGenBinBuffer):
30fdf114
LG
1405 WorkingDir = os.getcwd()
1406 os.chdir(Info.WorkspaceDir)
1407
1408 IncList = [Info.MetaFile.Dir]
1409 # Get all files under [Sources] section in inf file for EDK-II module
6780eef1 1410 EDK2Module = True
30fdf114
LG
1411 SrcList = [F for F in Info.SourceFileList]
1412 if Info.AutoGenVersion < 0x00010005:
6780eef1 1413 EDK2Module = False
30fdf114
LG
1414 # Get all files under the module directory for EDK-I module
1415 Cwd = os.getcwd()
1416 os.chdir(Info.MetaFile.Dir)
1417 for Root, Dirs, Files in os.walk("."):
1418 if 'CVS' in Dirs:
1419 Dirs.remove('CVS')
1420 if '.svn' in Dirs:
1421 Dirs.remove('.svn')
1422 for File in Files:
1423 File = PathClass(os.path.join(Root, File), Info.MetaFile.Dir)
1424 if File in SrcList:
1425 continue
1426 SrcList.append(File)
1427 os.chdir(Cwd)
1428
1429 if 'BUILD' in Info.BuildOption and Info.BuildOption['BUILD']['FLAGS'].find('-c') > -1:
1430 CompatibleMode = True
1431 else:
1432 CompatibleMode = False
1433
1434 #
6780eef1 1435 # -s is a temporary option dedicated for building .UNI files with ISO 639-2 language codes of EDK Shell in EDK2
30fdf114
LG
1436 #
1437 if 'BUILD' in Info.BuildOption and Info.BuildOption['BUILD']['FLAGS'].find('-s') > -1:
1438 if CompatibleMode:
1439 EdkLogger.error("build", AUTOGEN_ERROR,
1440 "-c and -s build options should be used exclusively",
1441 ExtraData="[%s]" % str(Info))
1442 ShellMode = True
1443 else:
1444 ShellMode = False
1445
6780eef1
LG
1446 #RFC4646 is only for EDKII modules and ISO639-2 for EDK modules
1447 if EDK2Module:
1448 FilterInfo = [EDK2Module] + [Info.PlatformInfo.Platform.RFCLanguages]
1449 else:
1450 FilterInfo = [EDK2Module] + [Info.PlatformInfo.Platform.ISOLanguages]
1451 Header, Code = GetStringFiles(Info.UnicodeFileList, SrcList, IncList, Info.IncludePathList, ['.uni', '.inf'], Info.Name, CompatibleMode, ShellMode, UniGenCFlag, UniGenBinBuffer, FilterInfo)
b303ea72
LG
1452 if CompatibleMode or UniGenCFlag:
1453 AutoGenC.Append("\n//\n//Unicode String Pack Definition\n//\n")
1454 AutoGenC.Append(Code)
1455 AutoGenC.Append("\n")
30fdf114
LG
1456 AutoGenH.Append("\n//\n//Unicode String ID\n//\n")
1457 AutoGenH.Append(Header)
b303ea72
LG
1458 if CompatibleMode or UniGenCFlag:
1459 AutoGenH.Append("\n#define STRING_ARRAY_NAME %sStrings\n" % Info.Name)
30fdf114
LG
1460 os.chdir(WorkingDir)
1461
1462## Create common code
1463#
1464# @param Info The ModuleAutoGen object
1465# @param AutoGenC The TemplateString object for C code
1466# @param AutoGenH The TemplateString object for header file
1467#
1468def CreateHeaderCode(Info, AutoGenC, AutoGenH):
1469 # file header
1470 AutoGenH.Append(gAutoGenHeaderString.Replace({'FileName':'AutoGen.h'}))
1471 # header file Prologue
1472 AutoGenH.Append(gAutoGenHPrologueString.Replace({'File':'AUTOGENH','Guid':Info.Guid.replace('-','_')}))
2bcc713e 1473 AutoGenH.Append(gAutoGenHCppPrologueString)
30fdf114 1474 if Info.AutoGenVersion >= 0x00010005:
30fdf114
LG
1475 # header files includes
1476 AutoGenH.Append("#include <%s>\n" % gBasicHeaderFile)
1477 if Info.ModuleType in gModuleTypeHeaderFile \
1478 and gModuleTypeHeaderFile[Info.ModuleType][0] != gBasicHeaderFile:
1479 AutoGenH.Append("#include <%s>\n" % gModuleTypeHeaderFile[Info.ModuleType][0])
b36d134f
LG
1480 #
1481 # if either PcdLib in [LibraryClasses] sections or there exist Pcd section, add PcdLib.h
1482 # As if modules only uses FixedPcd, then PcdLib is not needed in [LibraryClasses] section.
1483 #
1484 if 'PcdLib' in Info.Module.LibraryClasses or Info.Module.Pcds:
4234283c
LG
1485 AutoGenH.Append("#include <Library/PcdLib.h>\n")
1486
4afd3d04
LG
1487 AutoGenH.Append('\nextern GUID gEfiCallerIdGuid;')
1488 AutoGenH.Append('\nextern CHAR8 *gEfiCallerBaseName;\n\n')
30fdf114
LG
1489
1490 if Info.IsLibrary:
1491 return
1492
1493 AutoGenH.Append("#define EFI_CALLER_ID_GUID \\\n %s\n" % GuidStringToGuidStructureString(Info.Guid))
1494
1495 if Info.IsLibrary:
1496 return
1497 # C file header
1498 AutoGenC.Append(gAutoGenHeaderString.Replace({'FileName':'AutoGen.c'}))
1499 if Info.AutoGenVersion >= 0x00010005:
1500 # C file header files includes
1501 if Info.ModuleType in gModuleTypeHeaderFile:
1502 for Inc in gModuleTypeHeaderFile[Info.ModuleType]:
1503 AutoGenC.Append("#include <%s>\n" % Inc)
1504 else:
1505 AutoGenC.Append("#include <%s>\n" % gBasicHeaderFile)
1506
1507 #
1508 # Publish the CallerId Guid
1509 #
1510 AutoGenC.Append('\nGLOBAL_REMOVE_IF_UNREFERENCED GUID gEfiCallerIdGuid = %s;\n' % GuidStringToGuidStructureString(Info.Guid))
4afd3d04 1511 AutoGenC.Append('\nGLOBAL_REMOVE_IF_UNREFERENCED CHAR8 *gEfiCallerBaseName = "%s";\n' % Info.Name)
30fdf114
LG
1512
1513## Create common code for header file
1514#
1515# @param Info The ModuleAutoGen object
1516# @param AutoGenC The TemplateString object for C code
1517# @param AutoGenH The TemplateString object for header file
1518#
1519def CreateFooterCode(Info, AutoGenC, AutoGenH):
1520 AutoGenH.Append(gAutoGenHEpilogueString)
1521
1522## Create code for a module
1523#
1524# @param Info The ModuleAutoGen object
1525# @param AutoGenC The TemplateString object for C code
1526# @param AutoGenH The TemplateString object for header file
b303ea72
LG
1527# @param UniGenCFlag UniString is generated into AutoGen C file when it is set to True
1528# @param UniGenBinBuffer Buffer to store uni string package data
30fdf114 1529#
b303ea72 1530def CreateCode(Info, AutoGenC, AutoGenH, StringH, UniGenCFlag, UniGenBinBuffer):
30fdf114
LG
1531 CreateHeaderCode(Info, AutoGenC, AutoGenH)
1532
1533 if Info.AutoGenVersion >= 0x00010005:
1534 CreateGuidDefinitionCode(Info, AutoGenC, AutoGenH)
1535 CreateProtocolDefinitionCode(Info, AutoGenC, AutoGenH)
1536 CreatePpiDefinitionCode(Info, AutoGenC, AutoGenH)
1537 CreatePcdCode(Info, AutoGenC, AutoGenH)
1538 CreateLibraryConstructorCode(Info, AutoGenC, AutoGenH)
1539 CreateLibraryDestructorCode(Info, AutoGenC, AutoGenH)
1540 CreateModuleEntryPointCode(Info, AutoGenC, AutoGenH)
1541 CreateModuleUnloadImageCode(Info, AutoGenC, AutoGenH)
1542
1543 if Info.UnicodeFileList:
1544 FileName = "%sStrDefs.h" % Info.Name
1545 StringH.Append(gAutoGenHeaderString.Replace({'FileName':FileName}))
1546 StringH.Append(gAutoGenHPrologueString.Replace({'File':'STRDEFS', 'Guid':Info.Guid.replace('-','_')}))
b303ea72 1547 CreateUnicodeStringCode(Info, AutoGenC, StringH, UniGenCFlag, UniGenBinBuffer)
30fdf114
LG
1548 StringH.Append("\n#endif\n")
1549 AutoGenH.Append('#include "%s"\n' % FileName)
1550
1551 CreateFooterCode(Info, AutoGenC, AutoGenH)
1552
b36d134f 1553 # no generation of AutoGen.c for Edk modules without unicode file
30fdf114
LG
1554 if Info.AutoGenVersion < 0x00010005 and len(Info.UnicodeFileList) == 0:
1555 AutoGenC.String = ''
1556
1557## Create the code file
1558#
b303ea72
LG
1559# @param FilePath The path of code file
1560# @param Content The content of code file
1561# @param IsBinaryFile The flag indicating if the file is binary file or not
30fdf114
LG
1562#
1563# @retval True If file content is changed or file doesn't exist
1564# @retval False If the file exists and the content is not changed
1565#
b303ea72
LG
1566def Generate(FilePath, Content, IsBinaryFile):
1567 return SaveFileOnChange(FilePath, Content, IsBinaryFile)
30fdf114 1568