#!/usr/bin/env python
# Copyright (C) 2018 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os
import re
import sys

COMMON_PROTOS = (
  'protos/perfetto/common/android_log_constants.proto',
  'protos/perfetto/common/sys_stats_counters.proto',
)

CONFIG_PROTOS = (
  'protos/perfetto/config/android/android_log_config.proto',
  'protos/perfetto/config/chrome/chrome_config.proto',
  'protos/perfetto/config/data_source_config.proto',
  'protos/perfetto/config/ftrace/ftrace_config.proto',
  'protos/perfetto/config/inode_file/inode_file_config.proto',
  'protos/perfetto/config/power/android_power_config.proto',
  'protos/perfetto/config/process_stats/process_stats_config.proto',
  'protos/perfetto/config/sys_stats/sys_stats_config.proto',
  'protos/perfetto/config/test_config.proto',
  'protos/perfetto/config/trace_config.proto',
  'protos/perfetto/config/profiling/heapprofd_config.proto',
  'protos/perfetto/config/android/packages_list_config.proto',
)

MERGED_CONFIG_PROTO = 'protos/perfetto/config/perfetto_config.proto'

TRACE_PROTOS = (
  'protos/perfetto/common/trace_stats.proto', # only referenced by trace protos
  'protos/perfetto/trace/android/android_log.proto',
  'protos/perfetto/trace/android/packages_list.proto',
  'protos/perfetto/trace/clock_snapshot.proto',
  'protos/perfetto/trace/filesystem/inode_file_map.proto',
  'protos/perfetto/trace/ftrace/binder.proto',
  'protos/perfetto/trace/ftrace/block.proto',
  'protos/perfetto/trace/ftrace/clk.proto',
  'protos/perfetto/trace/ftrace/ext4.proto',
  'protos/perfetto/trace/ftrace/f2fs.proto',
  'protos/perfetto/trace/ftrace/filemap.proto',
  'protos/perfetto/trace/ftrace/ftrace.proto',
  'protos/perfetto/trace/ftrace/ftrace_event.proto',
  'protos/perfetto/trace/ftrace/ftrace_event_bundle.proto',
  'protos/perfetto/trace/ftrace/ftrace_stats.proto',
  'protos/perfetto/trace/ftrace/generic.proto',
  'protos/perfetto/trace/ftrace/kmem.proto',
  'protos/perfetto/trace/ftrace/lowmemorykiller.proto',
  'protos/perfetto/trace/ftrace/mm_event.proto',
  'protos/perfetto/trace/ftrace/power.proto',
  'protos/perfetto/trace/ftrace/raw_syscalls.proto',
  'protos/perfetto/trace/ftrace/sched.proto',
  'protos/perfetto/trace/ftrace/signal.proto',
  'protos/perfetto/trace/ftrace/systrace.proto',
  'protos/perfetto/trace/ftrace/task.proto',
  'protos/perfetto/trace/ftrace/vmscan.proto',
  'protos/perfetto/trace/interned_data/interned_data.proto',
  'protos/perfetto/trace/power/battery_counters.proto',
  'protos/perfetto/trace/power/power_rails.proto',
  'protos/perfetto/trace/profiling/profile_packet.proto',
  'protos/perfetto/trace/ps/process_stats.proto',
  'protos/perfetto/trace/ps/process_tree.proto',
  'protos/perfetto/trace/sys_stats/sys_stats.proto',
  'protos/perfetto/trace/system_info.proto',
  'protos/perfetto/trace/trace.proto',
  'protos/perfetto/trace/trace_packet.proto',
  'protos/perfetto/trace/track_event/debug_annotation.proto',
  'protos/perfetto/trace/track_event/process_descriptor.proto',
  'protos/perfetto/trace/track_event/task_execution.proto',
  'protos/perfetto/trace/track_event/thread_descriptor.proto',
  'protos/perfetto/trace/track_event/track_event.proto',
  'protos/perfetto/trace/trigger.proto',
)

MERGED_TRACE_PROTO = 'protos/perfetto/trace/perfetto_trace.proto'

REPLACEMENT_HEADER = '''
// AUTOGENERATED - DO NOT EDIT
// ---------------------------
// This file has been generated by
// AOSP://external/perfetto/%s
// merging the perfetto config protos.
// This fused proto is intended to be copied in:
//  - Android tree, for statsd.
//  - Google internal repos.

syntax = "proto2";

package perfetto.protos;
'''

def merge_protos(proto_paths, output_path):
  root_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
  merged_content = ''
  for proto in proto_paths:
    path = os.path.join(root_dir, proto)
    with open(path) as f:
      content = f.read()

    # Remove header
    header = re.match(r'\/(\*|\/)(?:.|\s)*?package .*;\n', content)
    header = header.group(0)
    content = content[len(header):]
    if merged_content == '':
      merged_content += REPLACEMENT_HEADER.lstrip() % __file__
    content = re.sub(r'^import.*?\n\n?', '', content, flags=re.MULTILINE)
    merged_content += '\n// Begin of %s\n' % proto
    merged_content += content
    merged_content += '\n// End of %s\n' % proto

  definitions_re = r'^ *(?:message|enum) ([A-Z][A-Za-z0-9].*) {'
  definitions = re.finditer(definitions_re, merged_content, re.MULTILINE)
  types = set((match.group(1) for match in definitions))

  uses_re = r'^( +)(?:repeated)?(?:optional)?\s'\
            r'?([A-Z]\w+.*)\s+[a-z]\w+\s*=\s*(\d+);'
  uses = re.finditer(uses_re, merged_content, re.MULTILINE)
  substitutions = []
  for use in uses:
    everything = use.group(0)
    indentation = use.group(1)
    used_type = use.group(2)
    field_number = use.group(3)
    if used_type not in types:
      replacement = '{}// removed field with id {}'.format(
          indentation, field_number)
      substitutions.append((everything, replacement))

  for before, after in substitutions:
    merged_content = merged_content.replace(before, after)

  out_path = os.path.join(root_dir, output_path)

  prev_content = None
  if os.path.exists(out_path):
    with open(out_path, 'rb') as fprev:
      prev_content = fprev.read()

  if prev_content == merged_content:
    return True

  if '--check-only' in sys.argv:
    return False

  print('Updating {}'.format(output_path))
  with open(out_path, 'wb') as fout:
    fout.write(merged_content)
  return True

def main():
  config_result = merge_protos(COMMON_PROTOS + CONFIG_PROTOS,
                               MERGED_CONFIG_PROTO)
  trace_result = merge_protos(COMMON_PROTOS + TRACE_PROTOS + CONFIG_PROTOS,
                              MERGED_TRACE_PROTO)
  return 0 if config_result and trace_result else 1

if __name__ == '__main__':
  sys.exit(main())