blob: b6221d18bada4d62fd77ca206d6c85191e4fd1d2 [file] [log] [blame]
Zhizhou Yange5986902017-08-10 17:37:53 -07001# 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):
Zhizhou Yang62362922017-08-30 16:04:36 -070016 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 Yange5986902017-08-10 17:37:53 -070029
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):
Zhizhou Yang62362922017-08-30 16:04:36 -070034 # 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 Yange5986902017-08-10 17:37:53 -070040
Zhizhou Yang62362922017-08-30 16:04:36 -070041 if not cflags:
42 cflags = ''
43 else:
44 cflags = '\"' + cflags + '\",'
45 if not ldflags:
46 ldflags = ''
47 else:
48 ldflags = '\"' + ldflags + '\",'
Zhizhou Yange5986902017-08-10 17:37:53 -070049
Zhizhou Yang62362922017-08-30 16:04:36 -070050 # 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 Yange5986902017-08-10 17:37:53 -070055
Zhizhou Yang62362922017-08-30 16:04:36 -070056 # 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 Yange5986902017-08-10 17:37:53 -070063
Zhizhou Yang62362922017-08-30 16:04:36 -070064 with open('modified.diff', 'w') as f:
65 f.write(output)
Zhizhou Yange5986902017-08-10 17:37:53 -070066
67
68def apply_patches(bench):
Zhizhou Yang62362922017-08-30 16:04:36 -070069 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 Yange5986902017-08-10 17:37:53 -070077
78
79def replace_flags_in_dir(bench, cflags, ldflags):
Zhizhou Yang62362922017-08-30 16:04:36 -070080 bench_mk = os.path.join(config.android_home, config.bench_dict[bench],
81 'Android.mk')
Zhizhou Yange5986902017-08-10 17:37:53 -070082
Zhizhou Yang62362922017-08-30 16:04:36 -070083 if not cflags:
84 cflags = ''
85 if not ldflags:
86 ldflags = ''
Zhizhou Yange5986902017-08-10 17:37:53 -070087
Zhizhou Yang62362922017-08-30 16:04:36 -070088 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 Yange5986902017-08-10 17:37:53 -070096
97
98def add_flags_Panorama(cflags, ldflags):
Zhizhou Yang62362922017-08-30 16:04:36 -070099 backup_file('Panorama', 'mk')
100 replace_flags_in_dir('Panorama', cflags, ldflags)
Zhizhou Yange5986902017-08-10 17:37:53 -0700101
102
103def add_flags_Synthmark(cflags, ldflags):
Zhizhou Yang62362922017-08-30 16:04:36 -0700104 backup_file('Synthmark', 'mk')
105 replace_flags_in_dir('Synthmark', cflags, ldflags)
Zhizhou Yange5986902017-08-10 17:37:53 -0700106
107
108def add_flags_Skia(cflags, ldflags):
Zhizhou Yang62362922017-08-30 16:04:36 -0700109 backup_file('Skia', 'bp')
110 replace_flags('Skia', config.android_type, 'bp', cflags, ldflags)
111 apply_patches('Skia')
Zhizhou Yange5986902017-08-10 17:37:53 -0700112
113
114def add_flags_Binder(cflags, ldflags):
Zhizhou Yang62362922017-08-30 16:04:36 -0700115 backup_file('Binder', 'bp')
116 replace_flags('Binder', config.android_type, 'bp', cflags, ldflags)
117 apply_patches('Binder')
Zhizhou Yange5986902017-08-10 17:37:53 -0700118
119
120def add_flags_Hwui(cflags, ldflags):
Zhizhou Yang62362922017-08-30 16:04:36 -0700121 backup_file('Hwui', 'bp')
122 replace_flags('Hwui', config.android_type, 'bp', cflags, ldflags)
123 apply_patches('Hwui')
Zhizhou Yange5986902017-08-10 17:37:53 -0700124
125
126def add_flags_Dex2oat(cflags, ldflags):
Zhizhou Yang62362922017-08-30 16:04:36 -0700127 backup_file('Dex2oat', 'bp')
128 replace_flags('Dex2oat', config.android_type, 'bp', cflags, ldflags)
129 apply_patches('Dex2oat')