blob: 28dde995152faee9c527ddcfd452be23f964ce85 [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
Tamas Kenez88965372017-08-11 16:20:34 +020019JCTFROOT = join(utils.REPO_ROOT, 'third_party', 'jctf')
20DESTINATION_DIR = join(utils.REPO_ROOT, 'build', 'generated', 'test', 'java',
21 'com', 'android', 'tools', 'r8', 'jctf')
Tamas Kenez25a99e92017-05-29 10:15:30 +020022PACKAGE_PREFIX = 'com.google.jctf.test.lib.java.'
Tamas Kenez88965372017-08-11 16:20:34 +020023RELATIVE_TESTDIR = join('LibTests', 'src', 'com', 'google', 'jctf', 'test',
24 'lib', 'java')
Tamas Kenez25a99e92017-05-29 10:15:30 +020025TESTDIR = join(JCTFROOT, RELATIVE_TESTDIR)
26TEMPLATE = Template(
27"""// Copyright (c) 2017, the R8 project authors. Please see the AUTHORS file
28// for details. All rights reserved. Use of this source code is governed by a
29// BSD-style license that can be found in the LICENSE file.
30package com.android.tools.r8.jctf.${compilerUnderTest}.${relativePackage};
31
32import org.junit.Test;
33import com.android.tools.r8.R8RunArtTestsTest;
34
35/**
36 * Auto-generated test for the jctf test:
37 * ${name}
38 *
39 * DO NOT EDIT THIS FILE. EDIT THE HERE DOCUMENT TEMPLATE IN
40 * tools/create_jctf_tests.py INSTEAD!
41 */
42public class ${testClassName} extends R8RunArtTestsTest {
43
44 public ${testClassName}() {
45 super("${nameWithoutPackagePrefix}", DexTool.NONE);
46 }
47
48 @Test
49 public void run${testClassName}() throws Exception {
50 // For testing with other Art VMs than the default set the system property
51 // 'dex_vm' to the desired VM string (e.g. '4.4.4', see ToolHelper.DexVm)
52 runJctfTest(CompilerUnderTest.${compilerUnderTestEnum},
53 "$classFile",
54 "$name"
55 );
56 }
57}
58""")
59
60EXIT_FAILURE = 1
61RE_PACKAGE = re.compile('package\\s+(com[^\\s;]*)')
62
Tamas Kenez88965372017-08-11 16:20:34 +020063def fix_long_path(p):
64 if os.name == 'nt':
65 p = ('\\\\?\\' + p).decode('utf-8')
66 return p
67
Tamas Kenez25a99e92017-05-29 10:15:30 +020068def file_contains_string(filepath, search_string):
Tamas Kenez88965372017-08-11 16:20:34 +020069 with open(fix_long_path(filepath)) as f:
Tamas Kenez25a99e92017-05-29 10:15:30 +020070 return search_string in f.read()
71
72def read_package_from_java_file(filepath):
Tamas Kenez88965372017-08-11 16:20:34 +020073 with open(fix_long_path(filepath)) as f:
Tamas Kenez25a99e92017-05-29 10:15:30 +020074 for line in f:
75 m = RE_PACKAGE.search(line)
76 if m:
77 return m.groups()[0]
78 raise IOError("Can't find package statement in java file: " + filepath)
79
80
Tamas Kenez9a4e53d2017-06-01 11:00:40 +020081def generate_test(class_name, compiler_under_test, compiler_under_test_enum,
82 relative_package):
Tamas Kenez25a99e92017-05-29 10:15:30 +020083 filename = join(DESTINATION_DIR, compiler_under_test,
Tamas Kenez88965372017-08-11 16:20:34 +020084 relative_package.replace('.', os.sep), class_name + '.java')
Tamas Kenez25a99e92017-05-29 10:15:30 +020085 utils.makedirs_if_needed(dirname(filename))
86
87 full_class_name = '{}{}.{}'.format(PACKAGE_PREFIX, relative_package,
88 class_name)
89 contents = TEMPLATE.substitute(
90 compilerUnderTest = compiler_under_test,
91 relativePackage = relative_package,
92 name = full_class_name,
93 testClassName = class_name,
Tamas Kenez9a4e53d2017-06-01 11:00:40 +020094 compilerUnderTestEnum = compiler_under_test_enum,
Tamas Kenez25a99e92017-05-29 10:15:30 +020095 classFile = full_class_name.replace('.', '/') + '.class',
96 nameWithoutPackagePrefix = '{}.{}'.format(relative_package, class_name))
97
Tamas Kenez88965372017-08-11 16:20:34 +020098 with open(fix_long_path(filename), 'w') as f:
Tamas Kenez25a99e92017-05-29 10:15:30 +020099 f.write(contents)
100
101def Main():
102 if not exists(JCTFROOT):
103 print('JCTF test package not found in {}'.format(JCTFROOT),
104 file = sys.stderr)
105 return EXIT_FAILURE
106
107 for tool in ['d8', 'r8']:
Tamas Kenez88965372017-08-11 16:20:34 +0200108 p = fix_long_path(join(DESTINATION_DIR, tool))
Tamas Kenez25a99e92017-05-29 10:15:30 +0200109 if exists(p):
110 rmtree(p)
111 makedirs(p)
112
113 java_files = (chain.from_iterable(glob(join(x[0], '*.java'))
114 for x in os.walk(TESTDIR)))
115
116 dot_java_dot = '.java.'
117
118 for f in java_files:
119 if not file_contains_string(f, '@Test'):
120 continue
121
122 class_name = os.path.splitext(os.path.basename(f))[0]
123 assert class_name.find('-') < 0
124
125 package = read_package_from_java_file(f)
126
127 idx = package.find(dot_java_dot)
128 assert idx >= 0
129 relative_package = package[idx + len(dot_java_dot):]
130
Tamas Kenez070843c2017-06-02 11:52:59 +0200131 generate_test(class_name, 'd8', 'R8_AFTER_D8', relative_package)
Tamas Kenez9a4e53d2017-06-01 11:00:40 +0200132 generate_test(class_name, 'r8', 'R8', relative_package)
Tamas Kenez458fee52018-09-28 10:38:39 +0200133 generate_test(class_name, 'r8cf', 'R8CF', relative_package)
Tamas Kenez25a99e92017-05-29 10:15:30 +0200134
135
136if __name__ == '__main__':
137 sys.exit(Main())