gen_amalgamated: Fix duplicate headers in amalgamated perfetto.h

The amalgamated perfetto.h contained multiple copies some of headers
(e.g., protozero/message.h) because the amalgamator didn't consistently
keep track of which headers had already been written to the output.

This patch corrects the problem by always using the full instead of
relative path when recording processed headers.

We also need to set a default include path based on the
//gn:default_config target since "gn desc" doesn't expand configs and
some header-only targets don't specify any non-default include dirs.

This patch removes about 9000 lines from the generated perfetto.h and
20000 lines from perfetto.cc.

Bug: b:290306165
Test: grep INCLUDE_PERFETTO_PROTOZERO_MESSAGE_H_ sdk/perfetto.h => 1 copy
Change-Id: I6eef37e9858bf162ea60999a4795ffc121bc9f27
diff --git a/tools/gen_amalgamated b/tools/gen_amalgamated
index fac7f60..2fe68a6 100755
--- a/tools/gen_amalgamated
+++ b/tools/gen_amalgamated
@@ -77,6 +77,11 @@
 # Includes which will be removed from the generated source.
 includes_to_remove = r'^(gtest).*$'
 
+# From //gn:default_config (since "gn desc" doesn't describe configs).
+default_includes = [
+  'include',
+]
+
 default_cflags = [
     # Since we're expanding header files into the generated source file, some
     # constant may remain unused.
@@ -323,7 +328,7 @@
       self._add_header(target_name, header)
 
   def _get_include_dirs(self, target_name):
-    include_dirs = set()
+    include_dirs = set(default_includes)
     for target_name in self._iterate_target_and_deps(target_name):
       target = self.desc[target_name]
       if 'include_dirs' in target:
@@ -333,17 +338,17 @@
 
   def _add_source_included_header(self, include_dirs, allowed_files,
                                   header_name):
-    if header_name in self._processed_headers:
-      return
-    if header_name in self._processed_source_headers:
-      return
-    self._processed_source_headers.add(header_name)
     for include_dir in include_dirs:
       rel_path = os.path.join(include_dir, header_name)
       full_path = os.path.join(gn_utils.repo_root(), rel_path)
       if os.path.exists(full_path):
         if not rel_path in allowed_files:
           return
+        if full_path in self._processed_headers:
+          return
+        if full_path in self._processed_source_headers:
+          return
+        self._processed_source_headers.add(full_path)
         with open(full_path) as f:
           self.source.append('// %s begin header: %s' %
                              (tool_name, normalize_path(full_path)))
@@ -373,15 +378,15 @@
                 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))
+        raise Error('Failed adding source %s: %s' % (source_name, e))
 
   def _add_header_included_header(self, include_dirs, header_name):
-    if header_name in self._processed_headers:
-      return
-    self._processed_headers.add(header_name)
     for include_dir in include_dirs:
       full_path = os.path.join(gn_utils.repo_root(), include_dir, header_name)
       if os.path.exists(full_path):
+        if full_path in self._processed_headers:
+          return
+        self._processed_headers.add(full_path)
         with open(full_path) as f:
           self.header.append('// %s begin header: %s' %
                              (tool_name, normalize_path(full_path)))
@@ -393,11 +398,11 @@
     raise Error('Header file %s not found. %s' % (header_name, msg))
 
   def _add_header(self, target_name, header_name):
-    if header_name in self._processed_headers:
-      return
-    self._processed_headers.add(header_name)
     include_dirs = self._get_include_dirs(target_name)
     full_path = os.path.join(gn_utils.repo_root(), header_name)
+    if full_path in self._processed_headers:
+      return
+    self._processed_headers.add(full_path)
     if not os.path.exists(full_path):
       if self._compute_deps_only:
         return
@@ -408,7 +413,7 @@
       try:
         self.header.extend(self._process_header_includes(include_dirs, f))
       except Error as e:
-        raise Error('Failed adding header %s: %s' % (header_name, e.message))
+        raise Error('Failed adding header %s: %s' % (header_name, e))
 
   def _patch_source(self, source_name, lines):
     result = []