#!/usr/bin/env python
# Copyright (C) 2017 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.

import os
import subprocess
import sys

ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
PUB_CORE_H = 'include/perfetto/tracing/core'
CORE_H   = 'include/perfetto/ext/tracing/core'
CORE_CPP = 'src/tracing/core'

PROTOS = (
    # Classes that are exposed as part of the public API surface.
    ('perfetto/common/data_source_descriptor.proto', PUB_CORE_H, CORE_CPP),
    ('perfetto/common/tracing_service_state.proto', PUB_CORE_H, CORE_CPP),
    ('perfetto/config/chrome/chrome_config.proto', PUB_CORE_H, CORE_CPP),
    ('perfetto/config/data_source_config.proto', PUB_CORE_H, CORE_CPP),
    ('perfetto/config/test_config.proto', PUB_CORE_H, CORE_CPP),
    ('perfetto/config/trace_config.proto', PUB_CORE_H, CORE_CPP),

    # Classes that are exposes only in the /ext/ (unstable) API surface.
    ('perfetto/common/commit_data_request.proto', CORE_H, CORE_CPP),
    ('perfetto/common/observable_events.proto', CORE_H, CORE_CPP),
    ('perfetto/common/trace_stats.proto', CORE_H, CORE_CPP),

    # Generate ftrace cpp/h files into the src/traced/probes/ directory.
    ('perfetto/config/ftrace/ftrace_config.proto',
        'src/traced/probes/ftrace', 'src/traced/probes/ftrace'),

    # Generate profiling cpp/h files into the src/profiling/memory/ directory.
    ('perfetto/config/profiling/heapprofd_config.proto',
        'src/profiling/memory', 'src/profiling/memory'),
)

def run(cmd):
  print('\nRunning ' + ' '.join(cmd))
  subprocess.check_call(cmd)


def main():
  if not os.path.exists('.gn'):
    print('This script must be executed from the perfetto root directory')
    return 1
  if len(sys.argv) < 2:
    print('Usage: %s out/xxx' % sys.argv[0])
    return 1
  out_dir = sys.argv[1]
  arch = 'mac' if sys.platform == 'darwin' else 'linux64'
  clang_format_path = os.path.join(
      ROOT_DIR, 'buildtools', arch, 'clang-format')
  clang_format = [clang_format_path, '-i', '--sort-includes']
  ninja = os.path.join(ROOT_DIR, 'tools', 'ninja')
  run([ninja, '-C', out_dir, 'proto_to_cpp'])
  tool = os.path.join(out_dir, 'proto_to_cpp')
  assert(os.path.exists(tool))
  for args in PROTOS:
    proto, header_dir, cpp_dir = args
    include_dir = header_dir.replace('include/', '')
    run([tool, proto] + [header_dir, cpp_dir, include_dir])
    fname = os.path.basename(proto).replace('.proto', '')
    run(clang_format + [os.path.join(header_dir, fname + '.h')])
    run(clang_format + [os.path.join(cpp_dir, fname + '.cc')])


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