aboutsummaryrefslogtreecommitdiff
path: root/android_bench_suite/set_flags.py
blob: a243c7cfa39a7af6eab8783d26e1699e946b41e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# Copyright 2017 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Helper functions to put user defined flags to mk/bp files"""

from __future__ import print_function

import config
import os
import subprocess


# Find the makefile/blueprint based on the benchmark, and make a copy of
# it for restoring later.
def backup_file(bench, file_type):
  mk_file = os.path.join(config.android_home, config.bench_dict[bench],
                         'Android.' + file_type)
  try:
    # Make a copy of the makefile/blueprint so that we can recover it after
    # building the benchmark
    subprocess.check_call([
        'cp', mk_file,
        os.path.join(config.android_home, config.bench_dict[bench],
                     'tmp_makefile')
    ])
  except subprocess.CalledProcessError():
    raise OSError('Cannot backup Android.%s file for %s' % (file_type, bench))


# Insert lines to add LOCAL_CFLAGS/LOCAL_LDFLAGS to the benchmarks
# makefile/blueprint
def replace_flags(bench, android_type, file_type, cflags, ldflags):
  # Use format ["Flag1", "Flag2"] for bp file
  if file_type == 'bp':
    if cflags:
      cflags = '\", \"'.join(cflags.split())
    if ldflags:
      ldflags = '\", \"'.join(ldflags.split())

  if not cflags:
    cflags = ''
  else:
    cflags = '\"' + cflags + '\",'
  if not ldflags:
    ldflags = ''
  else:
    ldflags = '\"' + ldflags + '\",'

  # Two different diffs are used for aosp or internal android repo.
  if android_type == 'aosp':
    bench_diff = bench + '_flags_aosp.diff'
  else:
    bench_diff = bench + '_flags_internal.diff'

  # Replace CFLAGS_FOR_BENCH_SUITE marker with proper cflags
  output = ''
  with open(bench_diff) as f:
    for line in f:
      line = line.replace('CFLAGS_FOR_BENCH_SUITE', cflags)
      line = line.replace('LDFLAGS_FOR_BENCH_SUITE', ldflags)
      output += line

  with open('modified.diff', 'w') as f:
    f.write(output)


def apply_patches(bench):
  bench_dir = os.path.join(config.android_home, config.bench_dict[bench])
  bench_diff = 'modified.diff'
  flags_patch = os.path.join(
      os.path.dirname(os.path.realpath(__file__)), bench_diff)
  try:
    subprocess.check_call(['git', '-C', bench_dir, 'apply', flags_patch])
  except subprocess.CalledProcessError:
    raise OSError('Patch for adding flags for %s does not succeed.' % (bench))


def replace_flags_in_dir(bench, cflags, ldflags):
  bench_mk = os.path.join(config.android_home, config.bench_dict[bench],
                          'Android.mk')

  if not cflags:
    cflags = ''
  if not ldflags:
    ldflags = ''

  output = ''
  with open(bench_mk) as f:
    for line in f:
      line = line.replace('$(CFLAGS_FOR_BENCH_SUITE)', cflags)
      line = line.replace('$(LDFLAGS_FOR_BENCH_SUITE)', ldflags)
      output += line
  with open(bench_mk, 'w') as f:
    f.write(output)


def add_flags_Panorama(cflags, ldflags):
  backup_file('Panorama', 'mk')
  replace_flags_in_dir('Panorama', cflags, ldflags)


def add_flags_Synthmark(cflags, ldflags):
  backup_file('Synthmark', 'mk')
  replace_flags_in_dir('Synthmark', cflags, ldflags)


def add_flags_Skia(cflags, ldflags):
  backup_file('Skia', 'bp')
  replace_flags('Skia', config.android_type, 'bp', cflags, ldflags)
  apply_patches('Skia')


def add_flags_Binder(cflags, ldflags):
  backup_file('Binder', 'bp')
  replace_flags('Binder', config.android_type, 'bp', cflags, ldflags)
  apply_patches('Binder')


def add_flags_Hwui(cflags, ldflags):
  backup_file('Hwui', 'bp')
  replace_flags('Hwui', config.android_type, 'bp', cflags, ldflags)
  apply_patches('Hwui')


def add_flags_Dex2oat(cflags, ldflags):
  backup_file('Dex2oat', 'bp')
  replace_flags('Dex2oat', config.android_type, 'bp', cflags, ldflags)
  apply_patches('Dex2oat')