1# Copyright 2017 The Chromium OS Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4"""Helper functions to put user defined flags to mk/bp files""" 5 6from __future__ import print_function 7 8import config 9import os 10import subprocess 11 12 13# Find the makefile/blueprint based on the benchmark, and make a copy of 14# it for restoring later. 15def backup_file(bench, file_type): 16 mk_file = os.path.join(config.android_home, config.bench_dict[bench], 17 'Android.' + file_type) 18 try: 19 # Make a copy of the makefile/blueprint so that we can recover it after 20 # building the benchmark 21 subprocess.check_call([ 22 'cp', mk_file, 23 os.path.join(config.android_home, config.bench_dict[bench], 24 'tmp_makefile') 25 ]) 26 except subprocess.CalledProcessError(): 27 raise OSError('Cannot backup Android.%s file for %s' % (file_type, 28 bench)) 29 30 31# Insert lines to add LOCAL_CFLAGS/LOCAL_LDFLAGS to the benchmarks 32# makefile/blueprint 33def replace_flags(bench, android_type, file_type, cflags, ldflags): 34 # Use format ["Flag1", "Flag2"] for bp file 35 if file_type == 'bp': 36 if cflags: 37 cflags = '\", \"'.join(cflags.split()) 38 if ldflags: 39 ldflags = '\", \"'.join(ldflags.split()) 40 41 if not cflags: 42 cflags = '' 43 else: 44 cflags = '\"' + cflags + '\",' 45 if not ldflags: 46 ldflags = '' 47 else: 48 ldflags = '\"' + ldflags + '\",' 49 50 # Two different diffs are used for aosp or internal android repo. 51 if android_type == 'aosp': 52 bench_diff = bench + '_flags_aosp.diff' 53 else: 54 bench_diff = bench + '_flags_internal.diff' 55 56 # Replace CFLAGS_FOR_BENCH_SUITE marker with proper cflags 57 output = '' 58 with open(bench_diff) as f: 59 for line in f: 60 line = line.replace('CFLAGS_FOR_BENCH_SUITE', cflags) 61 line = line.replace('LDFLAGS_FOR_BENCH_SUITE', ldflags) 62 output += line 63 64 with open('modified.diff', 'w') as f: 65 f.write(output) 66 67 68def apply_patches(bench): 69 bench_dir = os.path.join(config.android_home, config.bench_dict[bench]) 70 bench_diff = 'modified.diff' 71 flags_patch = os.path.join( 72 os.path.dirname(os.path.realpath(__file__)), bench_diff) 73 try: 74 subprocess.check_call(['git', '-C', bench_dir, 'apply', flags_patch]) 75 except subprocess.CalledProcessError: 76 raise OSError('Patch for adding flags for %s does not succeed.' % bench) 77 78 79def replace_flags_in_dir(bench, cflags, ldflags): 80 bench_mk = os.path.join(config.android_home, config.bench_dict[bench], 81 'Android.mk') 82 83 if not cflags: 84 cflags = '' 85 if not ldflags: 86 ldflags = '' 87 88 output = '' 89 with open(bench_mk) as f: 90 for line in f: 91 line = line.replace('$(CFLAGS_FOR_BENCH_SUITE)', cflags) 92 line = line.replace('$(LDFLAGS_FOR_BENCH_SUITE)', ldflags) 93 output += line 94 with open(bench_mk, 'w') as f: 95 f.write(output) 96 97 98def add_flags_Panorama(cflags, ldflags): 99 backup_file('Panorama', 'mk') 100 replace_flags_in_dir('Panorama', cflags, ldflags) 101 102 103def add_flags_Synthmark(cflags, ldflags): 104 backup_file('Synthmark', 'mk') 105 replace_flags_in_dir('Synthmark', cflags, ldflags) 106 107 108def add_flags_Skia(cflags, ldflags): 109 backup_file('Skia', 'bp') 110 replace_flags('Skia', config.android_type, 'bp', cflags, ldflags) 111 apply_patches('Skia') 112 113 114def add_flags_Binder(cflags, ldflags): 115 backup_file('Binder', 'bp') 116 replace_flags('Binder', config.android_type, 'bp', cflags, ldflags) 117 apply_patches('Binder') 118 119 120def add_flags_Hwui(cflags, ldflags): 121 backup_file('Hwui', 'bp') 122 replace_flags('Hwui', config.android_type, 'bp', cflags, ldflags) 123 apply_patches('Hwui') 124 125 126def add_flags_Dex2oat(cflags, ldflags): 127 backup_file('Dex2oat', 'bp') 128 replace_flags('Dex2oat', config.android_type, 'bp', cflags, ldflags) 129 apply_patches('Dex2oat') 130