gn2bp: separate parsing gn desc from retrieving Target object

To support parsing multiple gn desc files for different architectures,
the process of parsing and retrieving Targets needs to be split in two.

Test: //components/cronet/android:cronet
Change-Id: Ib94cbc18b9b73b17942a71289ef86f69d4e36c5f
diff --git a/tools/gn2bp/gen_android_bp b/tools/gn2bp/gen_android_bp
index 2ab61cd..ea7dbcd 100755
--- a/tools/gn2bp/gen_android_bp
+++ b/tools/gn2bp/gen_android_bp
@@ -1315,9 +1315,10 @@
   # Currently, multi tool chain is not supported for all targets and create_modules_from_target can
   # not reach to this target by following the dependency.
   # So it's required to specify explicitly.
-  create_modules_from_target(blueprint, gn,
-                             "//third_party/boringssl:boringssl_asm" +
-                             "(//build/toolchain/android:android_clang_x86)")
+  boringssl_gn_target_name = ('//third_party/boringssl:boringssl_asm' +
+                             '(//build/toolchain/android:android_clang_x86)')
+  gn.parse_gn_desc(boringssl_gn_target_name)
+  create_modules_from_target(blueprint, gn, boringssl_gn_target_name)
 
   create_java_module(blueprint, gn)
   update_jni_registration_module(blueprint, gn)
@@ -1381,7 +1382,12 @@
     desc = json.load(f)
 
   gn = gn_utils.GnParser(desc)
-  blueprint = create_blueprint_for_targets(gn, desc, args.targets or default_targets)
+  targets = args.targets or default_targets
+  for target in targets:
+    # TODO: pass desc to parse_gn_desc() to support parsing multiple desc files
+    # for different target architectures.
+    gn.parse_gn_desc(target)
+  blueprint = create_blueprint_for_targets(gn, desc, targets)
   project_root = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
   tool_name = os.path.relpath(os.path.abspath(__file__), project_root)
 
diff --git a/tools/gn2bp/gn_utils.py b/tools/gn2bp/gn_utils.py
index 118208c..23ac2c8 100644
--- a/tools/gn2bp/gn_utils.py
+++ b/tools/gn2bp/gn_utils.py
@@ -195,10 +195,16 @@
     # TODO: There are some other possible variations we might need to support.
     return target.type == 'group' and re.match('.*_java$', target.name)
 
-
   def get_target(self, gn_target_name):
     """Returns a Target object from the fully qualified GN target name.
 
+      get_target() requires that parse_gn_desc() has already been called.
+      """
+    return self.all_targets[gn_target_name]
+
+  def parse_gn_desc(self, gn_target_name):
+    """Parses a gn desc tree and resolves all target dependencies.
+
         It bubbles up variables from source_set dependencies as described in the
         class-level comments.
         """
@@ -277,7 +283,7 @@
 
     # Recurse in dependencies.
     for dep_name in desc.get('deps', []):
-      dep = self.get_target(dep_name)
+      dep = self.parse_gn_desc(dep_name)
       if dep.is_third_party_dep_:
         target.deps.add(dep_name)
       elif dep.type == 'proto_library':