Home | History | Annotate | Download | only in tools
      1 #!/usr/bin/env python
      2 # Copyright (C) 2017 The Android Open Source Project
      3 #
      4 # Licensed under the Apache License, Version 2.0 (the "License");
      5 # you may not use this file except in compliance with the License.
      6 # You may obtain a copy of the License at
      7 #
      8 #      http://www.apache.org/licenses/LICENSE-2.0
      9 #
     10 # Unless required by applicable law or agreed to in writing, software
     11 # distributed under the License is distributed on an "AS IS" BASIS,
     12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13 # See the License for the specific language governing permissions and
     14 # limitations under the License.
     15 
     16 import os
     17 import subprocess
     18 import sys
     19 
     20 PROTOS = (
     21   'perfetto/common/android_log_constants.proto',
     22   'perfetto/common/commit_data_request.proto',
     23   'perfetto/common/observable_events.proto',
     24   'perfetto/common/sys_stats_counters.proto',
     25   'perfetto/common/trace_stats.proto',
     26   'perfetto/config/android/android_log_config.proto',
     27   'perfetto/config/chrome/chrome_config.proto',
     28   'perfetto/config/data_source_config.proto',
     29   'perfetto/config/data_source_descriptor.proto',
     30   'perfetto/config/ftrace/ftrace_config.proto',
     31   'perfetto/config/inode_file/inode_file_config.proto',
     32   'perfetto/config/power/android_power_config.proto',
     33   'perfetto/config/process_stats/process_stats_config.proto',
     34   'perfetto/config/profiling/heapprofd_config.proto',
     35   'perfetto/config/sys_stats/sys_stats_config.proto',
     36   'perfetto/config/test_config.proto',
     37   'perfetto/config/trace_config.proto',
     38   'perfetto/config/android/packages_list_config.proto',
     39 )
     40 
     41 HEADER_PATH = 'include/perfetto/tracing/core'
     42 CPP_PATH = 'src/tracing/core'
     43 INCLUDE_PATH = 'perfetto/tracing/core'
     44 ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
     45 
     46 def run(cmd):
     47   print('\nRunning ' + ' '.join(cmd))
     48   subprocess.check_call(cmd)
     49 
     50 
     51 def main():
     52   if not os.path.exists('.gn'):
     53     print('This script must be executed from the perfetto root directory')
     54     return 1
     55   if len(sys.argv) < 2:
     56     print('Usage: %s out/xxx' % sys.argv[0])
     57     return 1
     58   out_dir = sys.argv[1]
     59   arch = 'mac' if sys.platform == 'darwin' else 'linux64'
     60   clang_format_path = os.path.join(ROOT_DIR, 'buildtools', arch, 'clang-format')
     61   clang_format = [clang_format_path, '-i', '--sort-includes']
     62   tool = os.path.join(out_dir, 'proto_to_cpp')
     63   if not os.path.exists(tool):
     64     print('Could not find %s, run ninja -C %s proto_to_cpp' % (tool, out_dir))
     65   for proto in PROTOS:
     66     run([tool, proto] + [HEADER_PATH, CPP_PATH, INCLUDE_PATH])
     67     fname = os.path.basename(proto).replace('.proto', '')
     68     run(clang_format + [os.path.join(HEADER_PATH, fname + '.h')])
     69     run(clang_format + [os.path.join(CPP_PATH, fname + '.cc')])
     70 
     71 
     72 if __name__ == '__main__':
     73   sys.exit(main())
     74