[edk2-devel] [PATCH] BaseTools:Add the FeatureFlagExpression usage to the InfBuildData

Fan, ZhijuX posted 1 patch 4 years, 10 months ago
Failed in applying to current master (apply log)
There is a newer version of this series
BaseTools/Source/Python/Common/Expression.py       |  2 +-
BaseTools/Source/Python/Common/GlobalData.py       |  1 +
BaseTools/Source/Python/Workspace/InfBuildData.py  | 69 ++++++++++++++++++++--
.../Source/Python/Workspace/WorkspaceCommon.py     | 10 +++-
4 files changed, 73 insertions(+), 9 deletions(-)
[edk2-devel] [PATCH] BaseTools:Add the FeatureFlagExpression usage to the InfBuildData
Posted by Fan, ZhijuX 4 years, 10 months ago
BZ:https://bugzilla.tianocore.org/show_bug.cgi?id=1446

FeatureFlagExpression Support in LibraryClasses/Guids/Ppi/Protocols
section of INF file. The Pcd value in the expression is from INF or DEC
When a FeatureFlagExpression is present,if the expression evaluates
to TRUE,then the entry is valid. If the expression evaluates to FALSE, 
then the EDK II build tools must ignore the entry.

This patch is going to add this feature.

Cc: Bob Feng <bob.c.feng@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Signed-off-by: Zhiju.Fan <zhijux.fan@intel.com>
---
 BaseTools/Source/Python/Common/Expression.py       |  2 +-
 BaseTools/Source/Python/Common/GlobalData.py       |  1 +
 BaseTools/Source/Python/Workspace/InfBuildData.py  | 69 ++++++++++++++++++++--
 .../Source/Python/Workspace/WorkspaceCommon.py     | 10 +++-
 4 files changed, 73 insertions(+), 9 deletions(-)

diff --git a/BaseTools/Source/Python/Common/Expression.py b/BaseTools/Source/Python/Common/Expression.py
index 07ca039a9c..31bf0e4b6c 100644
--- a/BaseTools/Source/Python/Common/Expression.py
+++ b/BaseTools/Source/Python/Common/Expression.py
@@ -43,7 +43,7 @@ ERR_IN_OPERAND          = 'Macro after IN operator can only be: $(FAMILY), $(ARC
 __ValidString = re.compile(r'[_a-zA-Z][_0-9a-zA-Z]*$')
 _ReLabel = re.compile('LABEL\((\w+)\)')
 _ReOffset = re.compile('OFFSET_OF\((\w+)\)')
-PcdPattern = re.compile(r'[_a-zA-Z][0-9A-Za-z_]*\.[_a-zA-Z][0-9A-Za-z_]*$')
+PcdPattern = re.compile(r'^[_a-zA-Z][0-9A-Za-z_]*\.[_a-zA-Z][0-9A-Za-z_]*$')
 
 ## SplitString
 #  Split string to list according double quote
diff --git a/BaseTools/Source/Python/Common/GlobalData.py b/BaseTools/Source/Python/Common/GlobalData.py
index 79f23c892d..1404e5d7ac 100644
--- a/BaseTools/Source/Python/Common/GlobalData.py
+++ b/BaseTools/Source/Python/Common/GlobalData.py
@@ -18,6 +18,7 @@ gGlobalDefines = {}
 gPlatformDefines = {}
 # PCD name and value pair for fixed at build and feature flag
 gPlatformPcds = {}
+gPlatformFinalPcds = {}
 # PCDs with type that are not fixed at build and feature flag
 gPlatformOtherPcds = {}
 gActivePlatform = None
diff --git a/BaseTools/Source/Python/Workspace/InfBuildData.py b/BaseTools/Source/Python/Workspace/InfBuildData.py
index e66b7c9832..4b4644b60e 100644
--- a/BaseTools/Source/Python/Workspace/InfBuildData.py
+++ b/BaseTools/Source/Python/Workspace/InfBuildData.py
@@ -14,6 +14,7 @@ from types import *
 from .MetaFileParser import *
 from collections import OrderedDict
 from Workspace.BuildClassObject import ModuleBuildClassObject, LibraryClassObject, PcdClassObject
+from Common.Expression import ValueExpressionEx, PcdPattern
 
 ## Get Protocol value from given packages
 #
@@ -561,6 +562,9 @@ class InfBuildData(ModuleBuildClassObject):
             Instance = Record[1]
             if Instance:
                 Instance = NormPath(Instance, self._Macros)
+                FeaturePcdExpression = self.CheckFeatureFlagPcd(Instance)
+                if not FeaturePcdExpression:
+                    continue
                 RetVal[Lib] = Instance
             else:
                 RetVal[Lib] = None
@@ -591,6 +595,10 @@ class InfBuildData(ModuleBuildClassObject):
         self._ProtocolComments = OrderedDict()
         RecordList = self._RawData[MODEL_EFI_PROTOCOL, self._Arch, self._Platform]
         for Record in RecordList:
+            if Record[1]:
+                FeaturePcdExpression = self.CheckFeatureFlagPcd(Record[1])
+                if not FeaturePcdExpression:
+                    continue
             CName = Record[0]
             Value = _ProtocolValue(CName, self.Packages, self.MetaFile.Path)
             if Value is None:
@@ -615,6 +623,10 @@ class InfBuildData(ModuleBuildClassObject):
         self._PpiComments = OrderedDict()
         RecordList = self._RawData[MODEL_EFI_PPI, self._Arch, self._Platform]
         for Record in RecordList:
+            if Record[1]:
+                FeaturePcdExpression = self.CheckFeatureFlagPcd(Record[1])
+                if not FeaturePcdExpression:
+                    continue
             CName = Record[0]
             Value = _PpiValue(CName, self.Packages, self.MetaFile.Path)
             if Value is None:
@@ -638,7 +650,12 @@ class InfBuildData(ModuleBuildClassObject):
         RetVal = OrderedDict()
         self._GuidComments = OrderedDict()
         RecordList = self._RawData[MODEL_EFI_GUID, self._Arch, self._Platform]
+        RetVal.update(self.GetGuidsUsedByPcd())
         for Record in RecordList:
+            if Record[1]:
+                FeaturePcdExpression = self.CheckFeatureFlagPcd(Record[1])
+                if not FeaturePcdExpression:
+                    continue
             CName = Record[0]
             Value = GuidValue(CName, self.Packages, self.MetaFile.Path)
             if Value is None:
@@ -825,6 +842,13 @@ class InfBuildData(ModuleBuildClassObject):
         self.Pcds
         return self._GuidsUsedByPcd
 
+    @cached_class_function
+    def GetGuidDict(self):
+        GuidDict = OrderedDict()
+        for Package in self.Packages:
+            GuidDict.update(Package.Guids)
+        return GuidDict
+
     ## Retrieve PCD for given type
     def _GetPcd(self, Type):
         Pcds = OrderedDict()
@@ -835,14 +859,13 @@ class InfBuildData(ModuleBuildClassObject):
             PcdDict[Arch, Platform, PcdCName, TokenSpaceGuid] = (Setting, LineNo)
             PcdList.append((PcdCName, TokenSpaceGuid))
             # get the guid value
-            if TokenSpaceGuid not in self.Guids:
+            if TokenSpaceGuid not in self._GuidsUsedByPcd:
                 Value = GuidValue(TokenSpaceGuid, self.Packages, self.MetaFile.Path)
                 if Value is None:
                     PackageList = "\n\t".join(str(P) for P in self.Packages)
                     EdkLogger.error('build', RESOURCE_NOT_AVAILABLE,
                                     "Value of Guid [%s] is not found under [Guids] section in" % TokenSpaceGuid,
                                     ExtraData=PackageList, File=self.MetaFile, Line=LineNo)
-                self.Guids[TokenSpaceGuid] = Value
                 self._GuidsUsedByPcd[TokenSpaceGuid] = Value
             CommentRecords = self._RawData[MODEL_META_DATA_COMMENT, self._Arch, self._Platform, Id]
             Comments = []
@@ -851,7 +874,6 @@ class InfBuildData(ModuleBuildClassObject):
             self._PcdComments[TokenSpaceGuid, PcdCName] = Comments
 
         # resolve PCD type, value, datum info, etc. by getting its definition from package
-        _GuidDict = self.Guids.copy()
         for PcdCName, TokenSpaceGuid in PcdList:
             PcdRealName = PcdCName
             Setting, LineNo = PcdDict[self._Arch, self.Platform, PcdCName, TokenSpaceGuid]
@@ -869,7 +891,7 @@ class InfBuildData(ModuleBuildClassObject):
                     '',
                     {},
                     False,
-                    self.Guids[TokenSpaceGuid]
+                    self.GetGuidDict()[TokenSpaceGuid]
                     )
             if Type == MODEL_PCD_PATCHABLE_IN_MODULE and ValueList[1]:
                 # Patch PCD: TokenSpace.PcdCName|Value|Offset
@@ -905,7 +927,6 @@ class InfBuildData(ModuleBuildClassObject):
                 #
                 #   TAB_PCDS_FIXED_AT_BUILD, TAB_PCDS_PATCHABLE_IN_MODULE, TAB_PCDS_FEATURE_FLAG, TAB_PCDS_DYNAMIC, TAB_PCDS_DYNAMIC_EX
                 #
-                _GuidDict.update(Package.Guids)
                 PcdType = self._PCD_TYPE_STRING_[Type]
                 if Type == MODEL_PCD_DYNAMIC:
                     Pcd.Pending = True
@@ -997,7 +1018,7 @@ class InfBuildData(ModuleBuildClassObject):
                         Pcd.DefaultValue = PcdInPackage.DefaultValue
                     else:
                         try:
-                            Pcd.DefaultValue = ValueExpressionEx(Pcd.DefaultValue, Pcd.DatumType, _GuidDict)(True)
+                            Pcd.DefaultValue = ValueExpressionEx(Pcd.DefaultValue, Pcd.DatumType, self.GetGuidDict())(True)
                         except BadExpression as Value:
                             EdkLogger.error('Parser', FORMAT_INVALID, 'PCD [%s.%s] Value "%s", %s' %(TokenSpaceGuid, PcdRealName, Pcd.DefaultValue, Value),
                                             File=self.MetaFile, Line=LineNo)
@@ -1020,3 +1041,39 @@ class InfBuildData(ModuleBuildClassObject):
         if (self.Binaries and not self.Sources) or GlobalData.gIgnoreSource:
             return True
         return False
+
+    def CheckFeatureFlagPcd(self,Instance):
+        Pcds = GlobalData.gPlatformFinalPcds[self.Arch].copy()
+        if PcdPattern.search(Instance):
+            PcdTuple = tuple(Instance.split('.')[::-1])
+            if PcdTuple in self.Pcds:
+                if not (self.Pcds[PcdTuple].Type == 'FeatureFlag' or self.Pcds[PcdTuple].Type == 'FixedAtBuild') and Instance not in Pcds:
+                    EdkLogger.error('build', FORMAT_INVALID,
+                                    "\nit must be defined in a [PcdsFeatureFlag] or [PcdsFixedAtBuild] section of Dsc or Dec file or [FeaturePcd] or [FixedPcd] of Inf file",
+                                    File=str(self), ExtraData=Instance)
+                Pcds[Instance] = self.Pcds[PcdTuple].DefaultValue
+            if Instance in Pcds:
+                if Pcds[Instance] == '0':
+                    return False
+                elif Pcds[Instance] == '1':
+                    return True
+            try:
+                Value = ValueExpression(Instance, Pcds)()
+                if Value == True:
+                    return True
+                return False
+            except:
+                EdkLogger.warn('build', FORMAT_INVALID,"The FeatureFlagExpression cannot be evaluated", File=str(self), ExtraData=Instance)
+                return False
+        else:
+            for Name, Guid in self.Pcds:
+                if self.Pcds[(Name, Guid)].Type == 'FeatureFlag' or self.Pcds[(Name, Guid)].Type == 'FixedAtBuild':
+                    Pcds['%s.%s' % (Guid, Name)] = self.Pcds[(Name, Guid)].DefaultValue
+            try:
+                Value = ValueExpression(Instance, Pcds)()
+                if Value == True:
+                    return True
+                return False
+            except:
+                EdkLogger.warn('build', FORMAT_INVALID, "The FeatureFlagExpression cannot be evaluated", File=str(self), ExtraData=Instance)
+                return False
diff --git a/BaseTools/Source/Python/Workspace/WorkspaceCommon.py b/BaseTools/Source/Python/Workspace/WorkspaceCommon.py
index 0cc83110ef..3ca142bc19 100644
--- a/BaseTools/Source/Python/Workspace/WorkspaceCommon.py
+++ b/BaseTools/Source/Python/Workspace/WorkspaceCommon.py
@@ -38,8 +38,9 @@ def GetPackageList(Platform, BuildDatabase, Arch, Target, Toolchain):
     for ModuleFile in Platform.Modules:
         Data = BuildDatabase[ModuleFile, Arch, Target, Toolchain]
         PkgSet.update(Data.Packages)
-        for Lib in GetLiabraryInstances(Data, Platform, BuildDatabase, Arch, Target, Toolchain):
-            PkgSet.update(Lib.Packages)
+    for ModuleFile in Platform.LibraryInstances:
+        Data = BuildDatabase[ModuleFile, Arch, Target, Toolchain]
+        PkgSet.update(Data.Packages)
     return list(PkgSet)
 
 ## Get all declared PCD from platform for specified arch, target and toolchain
@@ -71,6 +72,11 @@ def GetDeclaredPcd(Platform, BuildDatabase, Arch, Target, Toolchain, additionalP
                         break
             if (PcdCName, PcdTokenName) not in DecPcds:
                 DecPcds[PcdCName, PcdTokenName] = Pkg.Pcds[Pcd]
+    if not GlobalData.gPlatformFinalPcds.get(Arch):
+        GlobalData.gPlatformFinalPcds[Arch] = OrderedDict()
+    for Name,Guid in DecPcds:
+        if DecPcds[Name,Guid].Type == 'FeatureFlag' or DecPcds[Name,Guid].Type == 'FixedAtBuild':
+            GlobalData.gPlatformFinalPcds[Arch]['%s.%s'%(Guid,Name)]=DecPcds[Name,Guid].DefaultValue
     return DecPcds, GuidDict
 
 ## Get all dependent libraries for a module
-- 
2.14.1.windows.1


-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.

View/Reply Online (#40841): https://edk2.groups.io/g/devel/message/40841
Mute This Topic: https://groups.io/mt/31650344/1787277
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub  [importer@patchew.org]
-=-=-=-=-=-=-=-=-=-=-=-

Re: [edk2-devel] [PATCH] BaseTools:Add the FeatureFlagExpression usage to the InfBuildData
Posted by Laszlo Ersek 4 years, 10 months ago
On 05/17/19 02:57, Fan, ZhijuX wrote:
> BZ:https://bugzilla.tianocore.org/show_bug.cgi?id=1446
> 
> FeatureFlagExpression Support in LibraryClasses/Guids/Ppi/Protocols
> section of INF file. The Pcd value in the expression is from INF or DEC
> When a FeatureFlagExpression is present,if the expression evaluates
> to TRUE,then the entry is valid. If the expression evaluates to FALSE, 
> then the EDK II build tools must ignore the entry.
> 
> This patch is going to add this feature.
> 
> Cc: Bob Feng <bob.c.feng@intel.com>
> Cc: Liming Gao <liming.gao@intel.com>
> Signed-off-by: Zhiju.Fan <zhijux.fan@intel.com>
> ---
>  BaseTools/Source/Python/Common/Expression.py       |  2 +-
>  BaseTools/Source/Python/Common/GlobalData.py       |  1 +
>  BaseTools/Source/Python/Workspace/InfBuildData.py  | 69 ++++++++++++++++++++--
>  .../Source/Python/Workspace/WorkspaceCommon.py     | 10 +++-
>  4 files changed, 73 insertions(+), 9 deletions(-)

This feature has missed edk2-stable201905.

Please postpone the following BZ reference:

  https://bugzilla.tianocore.org/show_bug.cgi?id=1446

from

  https://github.com/tianocore/tianocore.github.io/wiki/EDK-II-Release-Planning#edk2-stable201905-tag-planning

to

  https://github.com/tianocore/tianocore.github.io/wiki/EDK-II-Release-Planning#edk2-stable201908-tag-planning

Thanks,
Laszlo

-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.

View/Reply Online (#41169): https://edk2.groups.io/g/devel/message/41169
Mute This Topic: https://groups.io/mt/31650344/1787277
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub  [importer@patchew.org]
-=-=-=-=-=-=-=-=-=-=-=-