amalgamator: normalize paths, make output stable

The amalgamator has a minor bug when emitting the
// begin header comments:

1. Leaks the full path (including the username) of the repo.
   e.g.: https://android-review.googlesource.com/c/platform/external/perfetto/+/1210943/1/sdk/perfetto.h

2. Leaks the name of the output folder, which changes all the times:
   e.g., https://android-review.googlesource.com/c/platform/external/perfetto/+/1210943/1/sdk/perfetto.cc

This fixes it

Change-Id: I007c0e67e9a115487811e30790e3287f3dcf1e59
diff --git a/tools/gen_amalgamated b/tools/gen_amalgamated
index ea6e254..79a572d 100755
--- a/tools/gen_amalgamated
+++ b/tools/gen_amalgamated
@@ -117,6 +117,7 @@
 # ----------------------------------------------------------------------------
 
 tool_name = os.path.basename(__file__)
+project_root = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
 preamble = """// Copyright (C) 2019 The Android Open Source Project
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
@@ -143,6 +144,12 @@
   return [item for item in items if re.match(whitelist, item)]
 
 
+def normalize_path(path):
+  path = os.path.relpath(path, project_root)
+  path = re.sub(r'^out/[^/]+/', '', path)
+  return path
+
+
 class Error(Exception):
   pass
 
@@ -332,7 +339,8 @@
         if not rel_path in allowed_files:
           return
         with open(full_path) as f:
-          self.source.append('// %s begin header: %s' % (tool_name, rel_path))
+          self.source.append(
+              '// %s begin header: %s' % (tool_name, normalize_path(full_path)))
           self.source.extend(
               self._process_source_includes(include_dirs, allowed_files, f))
         return
@@ -351,12 +359,13 @@
     if not os.path.exists(full_path):
       raise Error('Source file %s not found' % source_name)
     with open(full_path) as f:
-      self.source.append('// %s begin source: %s' % (tool_name, source_name))
+      self.source.append(
+          '// %s begin source: %s' % (tool_name, normalize_path(full_path)))
       try:
         self.source.extend(
             self._patch_source(
-                source_name,
-                self._process_source_includes(include_dirs, deps, f)))
+                source_name, self._process_source_includes(
+                    include_dirs, deps, f)))
       except Error as e:
         raise Error('Failed adding source %s: %s' % (source_name, e.message))
 
@@ -368,7 +377,8 @@
       full_path = os.path.join(gn_utils.repo_root(), include_dir, header_name)
       if os.path.exists(full_path):
         with open(full_path) as f:
-          self.header.append('// %s begin header: %s' % (tool_name, full_path))
+          self.header.append(
+              '// %s begin header: %s' % (tool_name, normalize_path(full_path)))
           self.header.extend(self._process_header_includes(include_dirs, f))
         return
     if self._compute_deps_only:
@@ -387,7 +397,8 @@
         return
       raise Error('Header file %s not found' % header_name)
     with open(full_path) as f:
-      self.header.append('// %s begin header: %s' % (tool_name, header_name))
+      self.header.append(
+          '// %s begin header: %s' % (tool_name, normalize_path(full_path)))
       try:
         self.header.extend(self._process_header_includes(include_dirs, f))
       except Error as e:
@@ -507,8 +518,8 @@
 Example build command:
 
 %s
-""" % (header_file, source_file, ' '.join(self.cflags), ' '.join(
-        self.ldflags), ' '.join(self.libs), ' '.join(build_cmd))
+""" % (header_file, source_file, ' '.join(self.cflags), ' '.join(self.ldflags),
+       ' '.join(self.libs), ' '.join(build_cmd))
 
   def get_build_command(self, output_prefix):
     """Returns an example command line for building the output source."""