[sdk][zircon] Move sysroot debug libraries to .build-id.

Test: manually verified that the libraries were in the correct location in SDK archives
Bug: DX-401
Change-Id: I8874adad24f82e2e831d9079770bb4a1d12d8d41
diff --git a/cpp/gen_sdk_prebuilt_meta_file.py b/cpp/gen_sdk_prebuilt_meta_file.py
index 697e057..cc9f47f 100755
--- a/cpp/gen_sdk_prebuilt_meta_file.py
+++ b/cpp/gen_sdk_prebuilt_meta_file.py
@@ -56,7 +56,7 @@
     # The path of the debug file in the SDK depends on its build id.
     debug_path = binaries.get_sdk_debug_path(args.lib_debug_file)
     with open(args.debug_mapping, 'w') as mappings_file:
-        mappings_file.write(debug_path + '=' + args.lib_debug_file)
+        mappings_file.write(debug_path + '=' + args.lib_debug_file + '\n')
 
     metadata = {
         'type': 'cc_prebuilt_library',
diff --git a/zircon/add_library_debug_data.py b/zircon/add_library_debug_data.py
index 794a86e..e6b1113 100755
--- a/zircon/add_library_debug_data.py
+++ b/zircon/add_library_debug_data.py
@@ -35,7 +35,7 @@
     # The path of the debug file in the SDK depends on its build id.
     debug_path = binaries.get_sdk_debug_path(args.lib_debug_file)
     with open(args.debug_mapping, 'w') as mappings_file:
-        mappings_file.write(debug_path + '=' + args.lib_debug_file)
+        mappings_file.write(debug_path + '=' + args.lib_debug_file + '\n')
 
     with open(args.base, 'r') as base_file:
         metadata = json.load(base_file)
diff --git a/zircon/add_sysroot_debug_data.py b/zircon/add_sysroot_debug_data.py
new file mode 100755
index 0000000..9a87918
--- /dev/null
+++ b/zircon/add_sysroot_debug_data.py
@@ -0,0 +1,55 @@
+#!/usr/bin/env python
+# Copyright 2018 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+import argparse
+import json
+import os
+import sys
+
+sys.path.append(os.path.join(
+    os.path.dirname(__file__),
+    os.pardir,
+    "cpp",
+))
+import binaries
+
+
+def main():
+    parser = argparse.ArgumentParser('Builds a metadata file')
+    parser.add_argument('--base',
+                        help='Path to the base metadata file',
+                        required=True)
+    parser.add_argument('--out',
+                        help='Path to the output file',
+                        required=True)
+    parser.add_argument('--lib-debug-file',
+                        help='Path to the source debug version of the library',
+                        action='append')
+    parser.add_argument('--debug-mapping',
+                        help='Path to the file where to write the file mapping for the debug library',
+                        required=True)
+    args = parser.parse_args()
+
+    debug_files = []
+
+    with open(args.debug_mapping, 'w') as mappings_file:
+        for debug_file in args.lib_debug_file:
+            debug_path = binaries.get_sdk_debug_path(debug_file)
+            mappings_file.write(debug_path + '=' + debug_file + '\n')
+            debug_files.append(debug_path)
+
+    with open(args.base, 'r') as base_file:
+        metadata = json.load(base_file)
+
+    metadata['versions'].values()[0]['debug_libs'] = debug_files
+
+    with open(args.out, 'w') as out_file:
+        json.dump(metadata, out_file, indent=2, sort_keys=True)
+
+    return 0
+
+
+if __name__ == '__main__':
+    sys.exit(main())
diff --git a/zircon/create_gn_rules.py b/zircon/create_gn_rules.py
index fff5387..64af35e 100755
--- a/zircon/create_gn_rules.py
+++ b/zircon/create_gn_rules.py
@@ -285,7 +285,7 @@
         self.headers = []
         self.link_libs = []
         self.dist_libs = []
-        self.debug_libs = []
+        self.debug_source_libs = []
 
 
 def generate_sysroot(package, context):
@@ -309,17 +309,19 @@
     # Lib.
     for name, path in package.get('lib', {}).iteritems():
         (file, _) = extract_file(name, path, context)
-        data.files[name] = '//%s' % file
-        data.sdk_files[name] = '//%s' % file
+        label = '//%s' % file
+        data.files[name] = label
         type_dir = os.path.dirname(name)
-        if type_dir == 'lib':
-            data.link_libs.append(name)
-        elif type_dir == 'dist/lib':
-            data.dist_libs.append(name)
-        elif type_dir == 'debug':
-            data.debug_libs.append(name)
+        if type_dir == 'debug':
+            data.debug_source_libs.append(label)
         else:
-            raise Exception('Unknown library type: ' + type_dir)
+            data.sdk_files[name] = label
+            if type_dir == 'lib':
+                data.link_libs.append(name)
+            elif type_dir == 'dist/lib':
+                data.dist_libs.append(name)
+            else:
+                raise Exception('Unknown library type: ' + type_dir)
 
     # Generate the build file.
     build_path = os.path.join(context.out_dir, 'sysroot', 'BUILD.gn')
diff --git a/zircon/sysroot.mako b/zircon/sysroot.mako
index 871ee95..39373cc 100644
--- a/zircon/sysroot.mako
+++ b/zircon/sysroot.mako
@@ -47,11 +47,6 @@
   % for lib in sorted(data.dist_libs):
   dist_libs += [ "$file_base/${lib}" ]
   % endfor
-
-  debug_libs = []
-  % for lib in sorted(data.debug_libs):
-  debug_libs += [ "$file_base/${lib}" ]
-  % endfor
 }
 metadata = {
   type = "sysroot"
@@ -67,6 +62,43 @@
   }
 }
 
+base_meta_file = "$target_gen_dir/sysroot.base_meta.json"
+write_file(base_meta_file, metadata, "json")
+augmented_meta_file = "$target_gen_dir/sysroot.full_meta.json"
+debug_mapping_file = "$target_gen_dir/sysroot.mapping.txt"
+
+action("sysroot_meta") {
+  script = "//build/zircon/add_sysroot_debug_data.py"
+
+  inputs = [
+    base_meta_file,
+    % for lib in sorted(data.debug_source_libs):
+    "${lib}",
+    % endfor
+  ]
+
+  outputs = [
+    augmented_meta_file,
+  ]
+
+  args = [
+    "--base",
+    rebase_path(base_meta_file),
+    "--out",
+    rebase_path(augmented_meta_file),
+    "--debug-mapping",
+    rebase_path(debug_mapping_file),
+    % for lib in sorted(data.debug_source_libs):
+    "--lib-debug-file",
+    rebase_path("${lib}"),
+    % endfor
+  ]
+
+  deps = [
+    ":sysroot",
+  ]
+}
+
 sdk_atom("sysroot_sdk") {
   id = "sdk://pkg/sysroot"
   category = "partner"
@@ -74,7 +106,7 @@
   meta = {
     dest = "pkg/sysroot/meta.json"
     schema = "sysroot"
-    value = metadata
+    source = augmented_meta_file
   }
 
   files = [
@@ -85,4 +117,11 @@
     },
     % endfor
   ]
+
+  file_list = debug_mapping_file
+
+  non_sdk_deps = [
+    ":sysroot",
+    ":sysroot_meta",
+  ]
 }