blob: ff9d797b40223bf4098154d642ee1826b911d65b [file] [log] [blame]
Tamas Kenez25a99e92017-05-29 10:15:30 +02001#!/usr/bin/env python
2# Copyright (c) 2017, the R8 project authors. Please see the AUTHORS file
3# for details. All rights reserved. Use of this source code is governed by a
4# BSD-style license that can be found in the LICENSE file.
5
6from __future__ import print_function
7from glob import glob
8from itertools import chain
9from os import makedirs
10from os.path import exists, join, dirname
11from shutil import rmtree
12from string import Template, upper
13import os
14import re
15import sys
16
17import utils
18
19JCTFROOT = 'third_party/jctf'
20DESTINATION_DIR = 'build/generated/test/java/com/android/tools/r8/jctf'
21PACKAGE_PREFIX = 'com.google.jctf.test.lib.java.'
22RELATIVE_TESTDIR = 'LibTests/src/com/google/jctf/test/lib/java'
23TESTDIR = join(JCTFROOT, RELATIVE_TESTDIR)
24TEMPLATE = Template(
25"""// Copyright (c) 2017, the R8 project authors. Please see the AUTHORS file
26// for details. All rights reserved. Use of this source code is governed by a
27// BSD-style license that can be found in the LICENSE file.
28package com.android.tools.r8.jctf.${compilerUnderTest}.${relativePackage};
29
30import org.junit.Test;
31import com.android.tools.r8.R8RunArtTestsTest;
32
33/**
34 * Auto-generated test for the jctf test:
35 * ${name}
36 *
37 * DO NOT EDIT THIS FILE. EDIT THE HERE DOCUMENT TEMPLATE IN
38 * tools/create_jctf_tests.py INSTEAD!
39 */
40public class ${testClassName} extends R8RunArtTestsTest {
41
42 public ${testClassName}() {
43 super("${nameWithoutPackagePrefix}", DexTool.NONE);
44 }
45
46 @Test
47 public void run${testClassName}() throws Exception {
48 // For testing with other Art VMs than the default set the system property
49 // 'dex_vm' to the desired VM string (e.g. '4.4.4', see ToolHelper.DexVm)
50 runJctfTest(CompilerUnderTest.${compilerUnderTestEnum},
51 "$classFile",
52 "$name"
53 );
54 }
55}
56""")
57
58EXIT_FAILURE = 1
59RE_PACKAGE = re.compile('package\\s+(com[^\\s;]*)')
60
61def file_contains_string(filepath, search_string):
62 with open(filepath) as f:
63 return search_string in f.read()
64
65def read_package_from_java_file(filepath):
66 with open(filepath) as f:
67 for line in f:
68 m = RE_PACKAGE.search(line)
69 if m:
70 return m.groups()[0]
71 raise IOError("Can't find package statement in java file: " + filepath)
72
73
Tamas Kenez9a4e53d2017-06-01 11:00:40 +020074def generate_test(class_name, compiler_under_test, compiler_under_test_enum,
75 relative_package):
Tamas Kenez25a99e92017-05-29 10:15:30 +020076 filename = join(DESTINATION_DIR, compiler_under_test,
77 relative_package.replace('.', '/'), class_name + '.java')
78 utils.makedirs_if_needed(dirname(filename))
79
80 full_class_name = '{}{}.{}'.format(PACKAGE_PREFIX, relative_package,
81 class_name)
82 contents = TEMPLATE.substitute(
83 compilerUnderTest = compiler_under_test,
84 relativePackage = relative_package,
85 name = full_class_name,
86 testClassName = class_name,
Tamas Kenez9a4e53d2017-06-01 11:00:40 +020087 compilerUnderTestEnum = compiler_under_test_enum,
Tamas Kenez25a99e92017-05-29 10:15:30 +020088 classFile = full_class_name.replace('.', '/') + '.class',
89 nameWithoutPackagePrefix = '{}.{}'.format(relative_package, class_name))
90
91 with open(filename, 'w') as f:
92 f.write(contents)
93
94def Main():
95 if not exists(JCTFROOT):
96 print('JCTF test package not found in {}'.format(JCTFROOT),
97 file = sys.stderr)
98 return EXIT_FAILURE
99
100 for tool in ['d8', 'r8']:
101 p = join(DESTINATION_DIR, tool)
102 if exists(p):
103 rmtree(p)
104 makedirs(p)
105
106 java_files = (chain.from_iterable(glob(join(x[0], '*.java'))
107 for x in os.walk(TESTDIR)))
108
109 dot_java_dot = '.java.'
110
111 for f in java_files:
112 if not file_contains_string(f, '@Test'):
113 continue
114
115 class_name = os.path.splitext(os.path.basename(f))[0]
116 assert class_name.find('-') < 0
117
118 package = read_package_from_java_file(f)
119
120 idx = package.find(dot_java_dot)
121 assert idx >= 0
122 relative_package = package[idx + len(dot_java_dot):]
123
Tamas Kenez070843c2017-06-02 11:52:59 +0200124 generate_test(class_name, 'd8', 'R8_AFTER_D8', relative_package)
Tamas Kenez9a4e53d2017-06-01 11:00:40 +0200125 generate_test(class_name, 'r8', 'R8', relative_package)
Tamas Kenez25a99e92017-05-29 10:15:30 +0200126
127
128if __name__ == '__main__':
129 sys.exit(Main())