1#!/usr/bin/env python3 2# 3# Copyright 2019, The Android Open Source Project 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17import importlib 18import os 19import sys 20import tempfile 21from enum import Enum 22from typing import TextIO, List 23 24# local import 25DIR = os.path.abspath(os.path.dirname(__file__)) 26sys.path.append(os.path.dirname(DIR)) 27import lib.print_utils as print_utils 28 29# Type of compiler. 30class CompilerType(Enum): 31 HOST = 1 # iorap.cmd.compiler on host 32 DEVICE = 2 # adb shell iorap.cmd.compiler 33 RI = 3 # compiler.py 34 35def compile_perfetto_trace_ri( 36 argv: List[str], 37 compiler) -> TextIO: 38 print_utils.debug_print('Compile using RI compiler.') 39 compiler_trace_file = tempfile.NamedTemporaryFile() 40 argv.extend(['-o', compiler_trace_file.name]) 41 print_utils.debug_print(argv) 42 compiler.main([''] + argv) 43 return compiler_trace_file 44 45def compile_perfetto_trace_device(inodes_path: str, 46 package: str, 47 activity: str, 48 compiler) -> TextIO: 49 print_utils.debug_print('Compile using on-device compiler.') 50 compiler_trace_file = tempfile.NamedTemporaryFile() 51 compiler.main(inodes_path, package, activity, compiler_trace_file.name) 52 return compiler_trace_file 53 54def compile(compiler_type: CompilerType, 55 inodes_path: str, 56 ri_compiler_argv, 57 package: str, 58 activity: str) -> TextIO: 59 if compiler_type == CompilerType.RI: 60 compiler = importlib.import_module('iorap.compiler_ri') 61 compiler_trace_file = compile_perfetto_trace_ri(ri_compiler_argv, 62 compiler) 63 return compiler_trace_file 64 if compiler_type == CompilerType.DEVICE: 65 compiler = importlib.import_module('iorap.compiler_device') 66 compiler_trace_file = compile_perfetto_trace_device(inodes_path, 67 package, 68 activity, 69 compiler) 70 return compiler_trace_file 71 72 # Should not arrive here. 73 raise ValueError('Unknown compiler type') 74