Zhizhou Yang | e598690 | 2017-08-10 17:37:53 -0700 | [diff] [blame] | 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 | |
| 6 | from __future__ import print_function |
| 7 | |
| 8 | import config |
| 9 | import os |
| 10 | import subprocess |
| 11 | |
| 12 | |
| 13 | # Find the makefile/blueprint based on the benchmark, and make a copy of |
| 14 | # it for restoring later. |
| 15 | def backup_file(bench, file_type): |
Zhizhou Yang | 6236292 | 2017-08-30 16:04:36 -0700 | [diff] [blame] | 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)) |
Zhizhou Yang | e598690 | 2017-08-10 17:37:53 -0700 | [diff] [blame] | 29 | |
| 30 | |
| 31 | # Insert lines to add LOCAL_CFLAGS/LOCAL_LDFLAGS to the benchmarks |
| 32 | # makefile/blueprint |
| 33 | def replace_flags(bench, android_type, file_type, cflags, ldflags): |
Zhizhou Yang | 6236292 | 2017-08-30 16:04:36 -0700 | [diff] [blame] | 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()) |
Zhizhou Yang | e598690 | 2017-08-10 17:37:53 -0700 | [diff] [blame] | 40 | |
Zhizhou Yang | 6236292 | 2017-08-30 16:04:36 -0700 | [diff] [blame] | 41 | if not cflags: |
| 42 | cflags = '' |
| 43 | else: |
| 44 | cflags = '\"' + cflags + '\",' |
| 45 | if not ldflags: |
| 46 | ldflags = '' |
| 47 | else: |
| 48 | ldflags = '\"' + ldflags + '\",' |
Zhizhou Yang | e598690 | 2017-08-10 17:37:53 -0700 | [diff] [blame] | 49 | |
Zhizhou Yang | 6236292 | 2017-08-30 16:04:36 -0700 | [diff] [blame] | 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' |
Zhizhou Yang | e598690 | 2017-08-10 17:37:53 -0700 | [diff] [blame] | 55 | |
Zhizhou Yang | 6236292 | 2017-08-30 16:04:36 -0700 | [diff] [blame] | 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 |
Zhizhou Yang | e598690 | 2017-08-10 17:37:53 -0700 | [diff] [blame] | 63 | |
Zhizhou Yang | 6236292 | 2017-08-30 16:04:36 -0700 | [diff] [blame] | 64 | with open('modified.diff', 'w') as f: |
| 65 | f.write(output) |
Zhizhou Yang | e598690 | 2017-08-10 17:37:53 -0700 | [diff] [blame] | 66 | |
| 67 | |
| 68 | def apply_patches(bench): |
Zhizhou Yang | 6236292 | 2017-08-30 16:04:36 -0700 | [diff] [blame] | 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) |
Zhizhou Yang | e598690 | 2017-08-10 17:37:53 -0700 | [diff] [blame] | 77 | |
| 78 | |
| 79 | def replace_flags_in_dir(bench, cflags, ldflags): |
Zhizhou Yang | 6236292 | 2017-08-30 16:04:36 -0700 | [diff] [blame] | 80 | bench_mk = os.path.join(config.android_home, config.bench_dict[bench], |
| 81 | 'Android.mk') |
Zhizhou Yang | e598690 | 2017-08-10 17:37:53 -0700 | [diff] [blame] | 82 | |
Zhizhou Yang | 6236292 | 2017-08-30 16:04:36 -0700 | [diff] [blame] | 83 | if not cflags: |
| 84 | cflags = '' |
| 85 | if not ldflags: |
| 86 | ldflags = '' |
Zhizhou Yang | e598690 | 2017-08-10 17:37:53 -0700 | [diff] [blame] | 87 | |
Zhizhou Yang | 6236292 | 2017-08-30 16:04:36 -0700 | [diff] [blame] | 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) |
Zhizhou Yang | e598690 | 2017-08-10 17:37:53 -0700 | [diff] [blame] | 96 | |
| 97 | |
| 98 | def add_flags_Panorama(cflags, ldflags): |
Zhizhou Yang | 6236292 | 2017-08-30 16:04:36 -0700 | [diff] [blame] | 99 | backup_file('Panorama', 'mk') |
| 100 | replace_flags_in_dir('Panorama', cflags, ldflags) |
Zhizhou Yang | e598690 | 2017-08-10 17:37:53 -0700 | [diff] [blame] | 101 | |
| 102 | |
| 103 | def add_flags_Synthmark(cflags, ldflags): |
Zhizhou Yang | 6236292 | 2017-08-30 16:04:36 -0700 | [diff] [blame] | 104 | backup_file('Synthmark', 'mk') |
| 105 | replace_flags_in_dir('Synthmark', cflags, ldflags) |
Zhizhou Yang | e598690 | 2017-08-10 17:37:53 -0700 | [diff] [blame] | 106 | |
| 107 | |
| 108 | def add_flags_Skia(cflags, ldflags): |
Zhizhou Yang | 6236292 | 2017-08-30 16:04:36 -0700 | [diff] [blame] | 109 | backup_file('Skia', 'bp') |
| 110 | replace_flags('Skia', config.android_type, 'bp', cflags, ldflags) |
| 111 | apply_patches('Skia') |
Zhizhou Yang | e598690 | 2017-08-10 17:37:53 -0700 | [diff] [blame] | 112 | |
| 113 | |
| 114 | def add_flags_Binder(cflags, ldflags): |
Zhizhou Yang | 6236292 | 2017-08-30 16:04:36 -0700 | [diff] [blame] | 115 | backup_file('Binder', 'bp') |
| 116 | replace_flags('Binder', config.android_type, 'bp', cflags, ldflags) |
| 117 | apply_patches('Binder') |
Zhizhou Yang | e598690 | 2017-08-10 17:37:53 -0700 | [diff] [blame] | 118 | |
| 119 | |
| 120 | def add_flags_Hwui(cflags, ldflags): |
Zhizhou Yang | 6236292 | 2017-08-30 16:04:36 -0700 | [diff] [blame] | 121 | backup_file('Hwui', 'bp') |
| 122 | replace_flags('Hwui', config.android_type, 'bp', cflags, ldflags) |
| 123 | apply_patches('Hwui') |
Zhizhou Yang | e598690 | 2017-08-10 17:37:53 -0700 | [diff] [blame] | 124 | |
| 125 | |
| 126 | def add_flags_Dex2oat(cflags, ldflags): |
Zhizhou Yang | 6236292 | 2017-08-30 16:04:36 -0700 | [diff] [blame] | 127 | backup_file('Dex2oat', 'bp') |
| 128 | replace_flags('Dex2oat', config.android_type, 'bp', cflags, ldflags) |
| 129 | apply_patches('Dex2oat') |