[Observatory] Refactor observatory build to prepare for Fuchsia prebuilt

This splits create_archive.py into two steps. One that creates the .tar
and one that creates the .cc for the .tar. This way on Fuchsia, the
second step will take the prebuilt instead of building it.

Change-Id: I7f407f9e70db3135884eeeb61aa5aec3091f1fcc
Reviewed-on: https://dart-review.googlesource.com/56291
Reviewed-by: Ryan Macnak <rmacnak@google.com>
diff --git a/BUILD.gn b/BUILD.gn
index f4f9bec..d0cd1b5 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -139,6 +139,12 @@
   ]
 }
 
+group("observatory_archive") {
+  deps = [
+    "runtime/observatory:observatory_archive"
+  ]
+}
+
 # The rules below build a Fuchsia OS image that includes the Dart tree
 # under /system/test/dart.  To get this into the `user.bootfs` generated
 # by the Fuchsia build, add the GN build argument:
diff --git a/runtime/observatory/BUILD.gn b/runtime/observatory/BUILD.gn
index badb8cb..61ddc8f 100644
--- a/runtime/observatory/BUILD.gn
+++ b/runtime/observatory/BUILD.gn
@@ -43,12 +43,12 @@
            "--command",
            "build",
            rebase_path("web/main.dart"),
-           rebase_path("$root_gen_dir/observatory/web/main.dart.js"),
+           rebase_path("$target_gen_dir/observatory/web/main.dart.js"),
            rebase_path("dart2js.packages"),
          ]
 
   outputs = [
-    "$root_gen_dir/observatory/web/main.dart.js",
+    "$target_gen_dir/observatory/web/main.dart.js",
   ]
 }
 
@@ -59,32 +59,28 @@
 
   inputs = [
     script,
-    "$root_gen_dir/observatory/web/main.dart.js",
+    "$target_gen_dir/observatory/web/main.dart.js",
   ]
 
   args = build_args + [
            "--silent=True",
            "--directory",
-           rebase_path("$root_out_dir/observatory"),
+           rebase_path("$target_out_dir/observatory"),
            "--command",
            "deploy",
-           rebase_path("$root_out_dir/observatory/deployed"),
+           rebase_path("$target_out_dir/observatory/deployed"),
            rebase_path("web"),
            rebase_path("lib"),
-           rebase_path("$root_gen_dir/observatory/web/main.dart.js"),
+           rebase_path("$target_gen_dir/observatory/web/main.dart.js"),
            rebase_path("../../third_party/observatory_pub_packages/packages"),
          ]
 
   outputs = [
-    "$root_out_dir/observatory/deployed/web/main.dart.js",
+    "$target_out_dir/observatory/deployed/web/main.dart.js",
   ]
 }
 
 template("observatory_archive") {
-  assert(defined(invoker.inner_namespace),
-         "Need inner_namespace in $target_name")
-  assert(defined(invoker.outer_namespace),
-         "Need outer_namespace in $target_name")
   enable_compression = false
   if (defined(invoker.compress) && invoker.compress) {
     enable_compression = true
@@ -95,42 +91,97 @@
     ]
 
     inputs = [
-      "$root_out_dir/observatory/deployed/web/main.dart.js",
+      "$target_out_dir/observatory/deployed/web/main.dart.js",
     ]
 
-    inner_namespace = invoker.inner_namespace
-    outer_namespace = invoker.outer_namespace
     output_name = target_name
 
+    output = "$target_gen_dir/${output_name}.tar"
+    outputs = [
+      output,
+    ]
+
     script = "../tools/create_archive.py"
     args = [
-      "--output",
-      rebase_path("$root_gen_dir/observatory/${output_name}.cc"),
       "--tar_output",
-      rebase_path("$root_gen_dir/observatory/${output_name}.tar"),
-      "--outer_namespace",
-      outer_namespace,
-      "--inner_namespace",
-      inner_namespace,
-      "--name",
-      "observatory_assets_archive",
+      rebase_path(output),
       "--client_root",
-      rebase_path("$root_out_dir/observatory/deployed/web/"),
+      rebase_path("$target_out_dir/observatory/deployed/web/"),
     ]
     if (enable_compression) {
       args += [ "--compress" ]
     }
+  }
+}
 
+observatory_archive("compressed_observatory_archive") {
+  compress = true
+}
+
+observatory_archive("observatory_archive") {
+  compress = false
+}
+
+# Generates a .cc file containing the bytes of the observatory archive in a C
+# array.
+#
+# Parameters:
+#  inner_namespace (required):
+#    The inner C++ namespace that the C array lives in.
+#
+#  outer_namespace (required):
+#    The outer C++ namespace that the C array lives in.
+#
+#  archive_file (required):
+#    The path to the observatory archive.
+#
+template("observatory_archive_source") {
+  assert(defined(invoker.inner_namespace),
+         "Need inner_namespace in $target_name")
+  assert(defined(invoker.outer_namespace),
+         "Need outer_namespace in $target_name")
+  assert(defined(invoker.archive_file), "Need archive_file in $target_name")
+
+  action(target_name) {
+    forward_variables_from(invoker, [ "deps" ])
+
+    inputs = [
+      invoker.archive_file,
+    ]
+
+    output = "$target_gen_dir/${target_name}.cc"
     outputs = [
-      "$root_gen_dir/observatory/${output_name}.cc",
-      "$root_gen_dir/observatory/${output_name}.tar",
+      output,
+    ]
+
+    script = "../tools/create_archive.py"
+    args = [
+      "--tar_input",
+      rebase_path(invoker.archive_file),
+      "--output",
+      rebase_path(output),
+      "--outer_namespace",
+      invoker.outer_namespace,
+      "--inner_namespace",
+      invoker.inner_namespace,
+      "--name",
+      "observatory_assets_archive",
     ]
   }
 }
 
-observatory_archive("embedded_archive_observatory") {
+observatory_archive_source("embedded_archive_observatory") {
   outer_namespace = "dart"
   inner_namespace = "observatory"
+
+  # TODO(zra): In a Fuchsia build, use a prebuilt Observatory archive.
+  archive_target = ":observatory_archive"
+  deps = [
+    archive_target,
+  ]
+  archive_dir = get_label_info(archive_target, "target_gen_dir")
+  archive_name = get_label_info(archive_target, "name")
+  archive_file = "${archive_dir}/${archive_name}.tar"
 }
 
 source_set("embedded_observatory_archive") {
@@ -139,14 +190,22 @@
   ]
 
   sources = [
-    rebase_path("$root_gen_dir/observatory/embedded_archive_observatory.cc"),
+    rebase_path("$target_gen_dir/embedded_archive_observatory.cc"),
   ]
 }
 
-observatory_archive("standalone_archive_observatory") {
-  compress = true
+observatory_archive_source("standalone_archive_observatory") {
   outer_namespace = "dart"
   inner_namespace = "bin"
+
+  # TODO(zra): In a Fuchsia build, use a prebuilt Observatory archive.
+  archive_target = ":compressed_observatory_archive"
+  deps = [
+    archive_target,
+  ]
+  archive_dir = get_label_info(archive_target, "target_gen_dir")
+  archive_name = get_label_info(archive_target, "name")
+  archive_file = "${archive_dir}/${archive_name}.tar"
 }
 
 source_set("standalone_observatory_archive") {
@@ -155,6 +214,6 @@
   ]
 
   sources = [
-    rebase_path("$root_gen_dir/observatory/standalone_archive_observatory.cc"),
+    rebase_path("$target_gen_dir/standalone_archive_observatory.cc"),
   ]
 }
diff --git a/runtime/tools/create_archive.py b/runtime/tools/create_archive.py
index c1fc70d..2857db4 100755
--- a/runtime/tools/create_archive.py
+++ b/runtime/tools/create_archive.py
@@ -16,7 +16,7 @@
 import tempfile
 import gzip
 
-def makeArchive(tar_path, client_root, compress, files):
+def CreateTarArchive(tar_path, client_root, compress, files):
   mode_string = 'w'
   tar = tarfile.open(tar_path, mode=mode_string)
   for input_file_name in files:
@@ -42,7 +42,35 @@
       gz.write(uncompressed)
       gz.close()
 
-def writeCCFile(output_file,
+
+def MakeArchive(options):
+  if not options.client_root:
+    sys.stderr.write('--client_root not specified')
+    return -1
+
+  files = [ ]
+  for dirname, dirnames, filenames in os.walk(options.client_root):
+    # strip out all dot files.
+    filenames = [f for f in filenames if not f[0] == '.']
+    dirnames[:] = [d for d in dirnames if not d[0] == '.']
+    for f in filenames:
+      src_path = os.path.join(dirname, f)
+      if (os.path.isdir(src_path)):
+          continue
+      files.append(src_path)
+
+  # Ensure consistent file ordering for reproducible builds.
+  files.sort()
+
+  # Write out archive.
+  CreateTarArchive(options.tar_output,
+                   options.client_root,
+                   options.compress,
+                   files)
+  return 0
+
+
+def WriteCCFile(output_file,
                 outer_namespace,
                 inner_namespace,
                 name,
@@ -89,13 +117,41 @@
 
   open(output_file, 'w').write(cc_text)
 
-def main(args):
+
+def MakeCCFile(options):
+  if not options.output:
+    sys.stderr.write('--output not specified\n')
+    return -1
+  if not options.name:
+    sys.stderr.write('--name not specified\n')
+    return -1
+  if not options.tar_input:
+    sys.stderr.write('--tar_input not specified\n')
+    return -1
+
+  # Read it back in.
+  with open(options.tar_input, 'rb') as tar_file:
+    tar_archive = tar_file.read()
+
+  # Write CC file.
+  WriteCCFile(options.output,
+              options.outer_namespace,
+              options.inner_namespace,
+              options.name,
+              tar_archive)
+  return 0
+
+
+def Main(args):
   try:
     # Parse input.
     parser = OptionParser()
     parser.add_option("--output",
                       action="store", type="string",
                       help="output file name")
+    parser.add_option("--tar_input",\
+                      action="store", type="string",
+                      help="input tar archive")
     parser.add_option("--tar_output",
                       action="store", type="string",
                       help="tar output file name")
@@ -116,51 +172,11 @@
                       help="root directory client resources")
 
     (options, args) = parser.parse_args()
-    if not options.output:
-      sys.stderr.write('--output not specified\n')
-      return -1
-    if not options.tar_output:
-      sys.stderr.write('--tar_output not specified\n')
-      return -1
-    if not options.name:
-      sys.stderr.write('--name not specified\n')
-      return -1
-    if not options.client_root:
-      sys.stderr.write('--client_root not specified')
-      return -1
 
-    files = [ ]
-
-    for dirname, dirnames, filenames in os.walk(options.client_root):
-      # strip out all dot files.
-      filenames = [f for f in filenames if not f[0] == '.']
-      dirnames[:] = [d for d in dirnames if not d[0] == '.']
-      for f in filenames:
-        src_path = os.path.join(dirname, f)
-        if (os.path.isdir(src_path)):
-            continue
-        files.append(src_path)
-
-    # Ensure consistent file ordering for reproducible builds.
-    files.sort()
-
-    # Write out archive.
-    makeArchive(options.tar_output,
-                options.client_root,
-                options.compress,
-                files)
-
-    # Read it back in.
-    with open(options.tar_output, 'rb') as tar_file:
-      tar_archive = tar_file.read()
-
-    # Write CC file.
-    writeCCFile(options.output,
-                options.outer_namespace,
-                options.inner_namespace,
-                options.name,
-                tar_archive)
-    return 0
+    if options.tar_output:
+      return MakeArchive(options)
+    else:
+      return MakeCCFile(options)
 
   except Exception, inst:
     sys.stderr.write('create_resources.py exception\n')
@@ -168,5 +184,6 @@
     sys.stderr.write('\n')
     return -1
 
+
 if __name__ == '__main__':
-  sys.exit(main(sys.argv))
+  sys.exit(Main(sys.argv))