Tamas Kenez | 25a99e9 | 2017-05-29 10:15:30 +0200 | [diff] [blame] | 1 | #!/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 | |
| 6 | from __future__ import print_function |
| 7 | from glob import glob |
| 8 | from itertools import chain |
| 9 | from os import makedirs |
| 10 | from os.path import exists, join, dirname |
| 11 | from shutil import rmtree |
| 12 | from string import Template, upper |
| 13 | import os |
| 14 | import re |
| 15 | import sys |
| 16 | |
| 17 | import utils |
| 18 | |
| 19 | JCTFROOT = 'third_party/jctf' |
| 20 | DESTINATION_DIR = 'build/generated/test/java/com/android/tools/r8/jctf' |
| 21 | PACKAGE_PREFIX = 'com.google.jctf.test.lib.java.' |
| 22 | RELATIVE_TESTDIR = 'LibTests/src/com/google/jctf/test/lib/java' |
| 23 | TESTDIR = join(JCTFROOT, RELATIVE_TESTDIR) |
| 24 | TEMPLATE = 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. |
| 28 | package com.android.tools.r8.jctf.${compilerUnderTest}.${relativePackage}; |
| 29 | |
| 30 | import org.junit.Test; |
| 31 | import 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 | */ |
| 40 | public 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 | |
| 58 | EXIT_FAILURE = 1 |
| 59 | RE_PACKAGE = re.compile('package\\s+(com[^\\s;]*)') |
| 60 | |
| 61 | def file_contains_string(filepath, search_string): |
| 62 | with open(filepath) as f: |
| 63 | return search_string in f.read() |
| 64 | |
| 65 | def 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 Kenez | 9a4e53d | 2017-06-01 11:00:40 +0200 | [diff] [blame] | 74 | def generate_test(class_name, compiler_under_test, compiler_under_test_enum, |
| 75 | relative_package): |
Tamas Kenez | 25a99e9 | 2017-05-29 10:15:30 +0200 | [diff] [blame] | 76 | 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 Kenez | 9a4e53d | 2017-06-01 11:00:40 +0200 | [diff] [blame] | 87 | compilerUnderTestEnum = compiler_under_test_enum, |
Tamas Kenez | 25a99e9 | 2017-05-29 10:15:30 +0200 | [diff] [blame] | 88 | 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 | |
| 94 | def 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 Kenez | 070843c | 2017-06-02 11:52:59 +0200 | [diff] [blame] | 124 | generate_test(class_name, 'd8', 'R8_AFTER_D8', relative_package) |
Tamas Kenez | 9a4e53d | 2017-06-01 11:00:40 +0200 | [diff] [blame] | 125 | generate_test(class_name, 'r8', 'R8', relative_package) |
Tamas Kenez | 25a99e9 | 2017-05-29 10:15:30 +0200 | [diff] [blame] | 126 | |
| 127 | |
| 128 | if __name__ == '__main__': |
| 129 | sys.exit(Main()) |