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, bench)) 28 29 30# Insert lines to add LOCAL_CFLAGS/LOCAL_LDFLAGS to the benchmarks 31# makefile/blueprint 32def replace_flags(bench, android_type, file_type, cflags, ldflags): 33 # Use format ["Flag1", "Flag2"] for bp file 34 if file_type == 'bp': 35 if cflags: 36 cflags = '\", \"'.join(cflags.split()) 37 if ldflags: 38 ldflags = '\", \"'.join(ldflags.split()) 39 40 if not cflags: 41 cflags = '' 42 else: 43 cflags = '\"' + cflags + '\",' 44 if not ldflags: 45 ldflags = '' 46 else: 47 ldflags = '\"' + ldflags + '\",' 48 49 # Two different diffs are used for aosp or internal android repo. 50 if android_type == 'aosp': 51 bench_diff = bench + '_flags_aosp.diff' 52 else: 53 bench_diff = bench + '_flags_internal.diff' 54 55 # Replace CFLAGS_FOR_BENCH_SUITE marker with proper cflags 56 output = '' 57 with open(bench_diff) as f: 58 for line in f: 59 line = line.replace('CFLAGS_FOR_BENCH_SUITE', cflags) 60 line = line.replace('LDFLAGS_FOR_BENCH_SUITE', ldflags) 61 output += line 62 63 with open('modified.diff', 'w') as f: 64 f.write(output) 65 66 67def apply_patches(bench): 68 bench_dir = os.path.join(config.android_home, config.bench_dict[bench]) 69 bench_diff = 'modified.diff' 70 flags_patch = os.path.join( 71 os.path.dirname(os.path.realpath(__file__)), bench_diff) 72 try: 73 subprocess.check_call(['git', '-C', bench_dir, 'apply', flags_patch]) 74 except subprocess.CalledProcessError: 75 raise OSError('Patch for adding flags for %s does not succeed.' % (bench)) 76 77 78def replace_flags_in_dir(bench, cflags, ldflags): 79 bench_mk = os.path.join(config.android_home, config.bench_dict[bench], 80 'Android.mk') 81 82 if not cflags: 83 cflags = '' 84 if not ldflags: 85 ldflags = '' 86 87 output = '' 88 with open(bench_mk) as f: 89 for line in f: 90 line = line.replace('$(CFLAGS_FOR_BENCH_SUITE)', cflags) 91 line = line.replace('$(LDFLAGS_FOR_BENCH_SUITE)', ldflags) 92 output += line 93 with open(bench_mk, 'w') as f: 94 f.write(output) 95 96 97def add_flags_Panorama(cflags, ldflags): 98 backup_file('Panorama', 'mk') 99 replace_flags_in_dir('Panorama', cflags, ldflags) 100 101 102def add_flags_Synthmark(cflags, ldflags): 103 backup_file('Synthmark', 'mk') 104 replace_flags_in_dir('Synthmark', cflags, ldflags) 105 106 107def add_flags_Skia(cflags, ldflags): 108 backup_file('Skia', 'bp') 109 replace_flags('Skia', config.android_type, 'bp', cflags, ldflags) 110 apply_patches('Skia') 111 112 113def add_flags_Binder(cflags, ldflags): 114 backup_file('Binder', 'bp') 115 replace_flags('Binder', config.android_type, 'bp', cflags, ldflags) 116 apply_patches('Binder') 117 118 119def add_flags_Hwui(cflags, ldflags): 120 backup_file('Hwui', 'bp') 121 replace_flags('Hwui', config.android_type, 'bp', cflags, ldflags) 122 apply_patches('Hwui') 123 124 125def add_flags_Dex2oat(cflags, ldflags): 126 backup_file('Dex2oat', 'bp') 127 replace_flags('Dex2oat', config.android_type, 'bp', cflags, ldflags) 128 apply_patches('Dex2oat') 129