aboutsummaryrefslogtreecommitdiff
path: root/scripts/generate_test_files.py
blob: cdb10dbf95657e2fa0e21e64fd6f15d6d407e6de (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import json
import os
import re
from xml.dom import minidom
from xml.etree import ElementTree


SCRIPT_DIR = os.path.abspath(os.path.dirname(__file__))
TEST_JSON = 'tests.json'
TEST_JSON_PATH = os.path.join(SCRIPT_DIR, TEST_JSON)


def write_one_cc_test(test_details, f):
  stringified_sources = map(lambda s: f'"{s}"', test_details['srcs'])
  stringified_data = map(lambda s: f'"{s}"', test_details.get('data', []))
  stringified_cflags = map(lambda s: f'"{s}"', test_details.get('cflags', []))

  default = "ocl-test-defaults"
  if test_details.get('image_type', False):
    default = "ocl-test-image-defaults"

  rtti = test_details.get('rtti', False)

  cc_test_string = """
cc_test {{
    name: "{}",
    srcs: [ {} ],
    data: [ {} ],
    cflags: [ {} ],
    defaults: [ "{}" ],
    rtti: {},
    gtest: false
}}

""".format(test_details['binary_name'],
           ", ".join(stringified_sources),
           ", ".join(stringified_data),
           ", ".join(stringified_cflags),
           default,
           (str(rtti)).lower())

  empty_field_regex = re.compile("^\s*\w+: \[\s*\],?$")
  cc_test_string = '\n'.join([line for line in cc_test_string.split('\n')
                                   if not empty_field_regex.match(line)])
  f.write(cc_test_string)


def generate_android_bp():
  android_bp_head_path = os.path.join(SCRIPT_DIR, 'android_bp_head')
  android_bp_tail_path = os.path.join(SCRIPT_DIR, 'android_bp_tail')

  with open('Android.bp', 'w') as android_bp:
    with open(android_bp_head_path, 'r') as android_bp_head:
      android_bp.write(android_bp_head.read())

    with open(TEST_JSON_PATH) as f:
      tests = json.load(f)
    for test in tests:
      write_one_cc_test(test, android_bp)

    with open(android_bp_tail_path, 'r') as android_bp_tail:
      android_bp.write(android_bp_tail.read())


def create_subelement_with_attribs(element, tag, attribs):
  subelement = ElementTree.SubElement(element, tag)

  for key, value in attribs.items():
    subelement.attrib[key] = value

  return subelement


def generate_push_file_rules(configuration):
  create_subelement_with_attribs(configuration, 'target_preparer',
      { 'class': "com.android.tradefed.targetprep.RootTargetPreparer" })
  file_pusher = create_subelement_with_attribs(configuration, 'target_preparer',
      { 'class': "com.android.compatibility.common.tradefed.targetprep.FilePusher" })
  create_subelement_with_attribs(file_pusher, 'option',
      { 'name': "cleanup", 'value': "true" })
  create_subelement_with_attribs(file_pusher, 'option',
      { 'name': "append-bitness", 'value': "true" })

  with open(TEST_JSON_PATH, "r") as f:
    tests = json.load(f)

  for test in tests:
    if test.get('manual_only', False):
      continue

    create_subelement_with_attribs(file_pusher, 'option',
        {
          'name': "push-file",
          'key': test['binary_name'],
          'value': "/data/nativetest64/unrestricted/{}".format(test['binary_name'])
        })


def generate_test_rules(configuration):
  with open(TEST_JSON_PATH, "r") as f:
    tests = json.load(f)

  for test in tests:
    if test.get('manual_only', False):
      continue

    test_rule = create_subelement_with_attribs(configuration, 'test',
        { 'class': "com.android.tradefed.testtype.python.PythonBinaryHostTest" })

    create_subelement_with_attribs(test_rule, 'option',
        { 'name': "par-file-name", 'value': "opencl_cts" })
    create_subelement_with_attribs(test_rule, 'option',
        { 'name': "inject-android-serial", 'value': "true" })
    create_subelement_with_attribs(test_rule, 'option',
        { 'name': "test-timeout", 'value': test.get('timeout', "30m") })
    create_subelement_with_attribs(test_rule, 'option',
        { 'name': "python-options", 'value': test["test_name"] })
    create_subelement_with_attribs(test_rule, 'option',
        { 'name': "python-options",
          'value': "/data/nativetest64/unrestricted/{}".format(test['binary_name']) })

    for arg in test.get('arguments', []):
      create_subelement_with_attribs(test_rule, 'option',
          { 'name': "python-options", 'value': arg })


def generate_test_xml():
  configuration = ElementTree.Element('configuration')
  configuration.attrib['description'] = "Config to run OpenCL CTS"

  logcat = ElementTree.SubElement(configuration, 'option')
  logcat.attrib['name'] = "logcat-on-failure"
  logcat.attrib['value'] = "false"

  generate_push_file_rules(configuration)
  generate_test_rules(configuration)

  stringified_configuration = ElementTree.tostring(configuration, 'utf-8')
  reparsed_configuration = minidom.parseString(stringified_configuration)
  with open('test_opencl_cts.xml', 'w') as f:
    f.write(reparsed_configuration.toprettyxml(indent=" "*4))


def main():
  generate_android_bp()
  generate_test_xml()

  print("Don't forget to move -")
  print("    Android.bp -> {ANDROID_ROOT}/external/OpenCL-CTS/Android.bp")
  print("    test_opencl_cts.xml -> {ANDROID_ROOT}/external/OpenCL-CTS/scripts/test_opencl_cts.xml")


if __name__ == '__main__':
  main()