gn2bp: add code that collects common sources
Once sources will be added to a Target.Arch, common sources need to be
collected from the arch-dependent object. This is done via finalize()
method, which is called once on every Target. Finalize collects the
intersection of all sources and subtracts it from the arch dependent
sources.
Test: //components/cronet/android:cronet
Change-Id: If2d0d04ff6c8df0efb541a322bcf2febcf79bb55
diff --git a/tools/gn2bp/gn_utils.py b/tools/gn2bp/gn_utils.py
index f56e41f..86f9f74 100644
--- a/tools/gn2bp/gn_utils.py
+++ b/tools/gn2bp/gn_utils.py
@@ -150,6 +150,9 @@
# placeholder target once we hit //gn:protoc or similar.
self.is_third_party_dep_ = False
+ # TODO: come up with a better way to only run this once.
+ # is_finalized tracks whether finalize() was called on this target.
+ self.is_finalized = False
self.arch = dict()
def __lt__(self, other):
@@ -173,6 +176,27 @@
'libs', 'proto_paths'):
self.__dict__[key].update(other.__dict__.get(key, []))
+ def finalize(self):
+ """Move common properties out of arch-dependent subobjects to Target object.
+
+ TODO: find a better name for this function.
+ """
+ if self.is_finalized:
+ return
+ self.is_finalized = True
+
+ # TODO: temporary hack until sources are collected on a per arch basis.
+ if self.sources:
+ return
+
+ # Target contains the intersection of arch-dependent properties
+ self.sources = set.intersection(*[arch.sources for arch in self.arch.values()])
+
+ # Deduplicate arch-dependent properties
+ for arch in self.arch.keys():
+ self.arch[arch].sources -= self.sources
+
+
def __init__(self):
self.all_targets = {}
self.linker_units = {} # Executables, shared or static libraries.
@@ -222,6 +246,10 @@
get_target() requires that parse_gn_desc() has already been called.
"""
+ # Run this every time as parse_gn_desc can be called at any time.
+ for target in self.all_targets.values():
+ target.finalize()
+
return self.all_targets[label_without_toolchain(gn_target_name)]
def parse_gn_desc(self, gn_desc, gn_target_name):