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 | |
Tamas Kenez | 8896537 | 2017-08-11 16:20:34 +0200 | [diff] [blame] | 19 | JCTFROOT = join(utils.REPO_ROOT, 'third_party', 'jctf') |
| 20 | DESTINATION_DIR = join(utils.REPO_ROOT, 'build', 'generated', 'test', 'java', |
| 21 | 'com', 'android', 'tools', 'r8', 'jctf') |
Tamas Kenez | 25a99e9 | 2017-05-29 10:15:30 +0200 | [diff] [blame] | 22 | PACKAGE_PREFIX = 'com.google.jctf.test.lib.java.' |
Tamas Kenez | 8896537 | 2017-08-11 16:20:34 +0200 | [diff] [blame] | 23 | RELATIVE_TESTDIR = join('LibTests', 'src', 'com', 'google', 'jctf', 'test', |
| 24 | 'lib', 'java') |
Tamas Kenez | 25a99e9 | 2017-05-29 10:15:30 +0200 | [diff] [blame] | 25 | TESTDIR = join(JCTFROOT, RELATIVE_TESTDIR) |
| 26 | TEMPLATE = 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. |
| 30 | package com.android.tools.r8.jctf.${compilerUnderTest}.${relativePackage}; |
| 31 | |
| 32 | import org.junit.Test; |
| 33 | import 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 | */ |
| 42 | public 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 | |
| 60 | EXIT_FAILURE = 1 |
| 61 | RE_PACKAGE = re.compile('package\\s+(com[^\\s;]*)') |
| 62 | |
Tamas Kenez | 8896537 | 2017-08-11 16:20:34 +0200 | [diff] [blame] | 63 | def fix_long_path(p): |
| 64 | if os.name == 'nt': |
| 65 | p = ('\\\\?\\' + p).decode('utf-8') |
| 66 | return p |
| 67 | |
Tamas Kenez | 25a99e9 | 2017-05-29 10:15:30 +0200 | [diff] [blame] | 68 | def file_contains_string(filepath, search_string): |
Tamas Kenez | 8896537 | 2017-08-11 16:20:34 +0200 | [diff] [blame] | 69 | with open(fix_long_path(filepath)) as f: |
Tamas Kenez | 25a99e9 | 2017-05-29 10:15:30 +0200 | [diff] [blame] | 70 | return search_string in f.read() |
| 71 | |
| 72 | def read_package_from_java_file(filepath): |
Tamas Kenez | 8896537 | 2017-08-11 16:20:34 +0200 | [diff] [blame] | 73 | with open(fix_long_path(filepath)) as f: |
Tamas Kenez | 25a99e9 | 2017-05-29 10:15:30 +0200 | [diff] [blame] | 74 | 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 Kenez | 9a4e53d | 2017-06-01 11:00:40 +0200 | [diff] [blame] | 81 | def generate_test(class_name, compiler_under_test, compiler_under_test_enum, |
| 82 | relative_package): |
Tamas Kenez | 25a99e9 | 2017-05-29 10:15:30 +0200 | [diff] [blame] | 83 | filename = join(DESTINATION_DIR, compiler_under_test, |
Tamas Kenez | 8896537 | 2017-08-11 16:20:34 +0200 | [diff] [blame] | 84 | relative_package.replace('.', os.sep), class_name + '.java') |
Tamas Kenez | 25a99e9 | 2017-05-29 10:15:30 +0200 | [diff] [blame] | 85 | 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 Kenez | 9a4e53d | 2017-06-01 11:00:40 +0200 | [diff] [blame] | 94 | compilerUnderTestEnum = compiler_under_test_enum, |
Tamas Kenez | 25a99e9 | 2017-05-29 10:15:30 +0200 | [diff] [blame] | 95 | classFile = full_class_name.replace('.', '/') + '.class', |
| 96 | nameWithoutPackagePrefix = '{}.{}'.format(relative_package, class_name)) |
| 97 | |
Tamas Kenez | 8896537 | 2017-08-11 16:20:34 +0200 | [diff] [blame] | 98 | with open(fix_long_path(filename), 'w') as f: |
Tamas Kenez | 25a99e9 | 2017-05-29 10:15:30 +0200 | [diff] [blame] | 99 | f.write(contents) |
| 100 | |
| 101 | def 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 Kenez | 8896537 | 2017-08-11 16:20:34 +0200 | [diff] [blame] | 108 | p = fix_long_path(join(DESTINATION_DIR, tool)) |
Tamas Kenez | 25a99e9 | 2017-05-29 10:15:30 +0200 | [diff] [blame] | 109 | 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 Kenez | 070843c | 2017-06-02 11:52:59 +0200 | [diff] [blame] | 131 | generate_test(class_name, 'd8', 'R8_AFTER_D8', relative_package) |
Tamas Kenez | 9a4e53d | 2017-06-01 11:00:40 +0200 | [diff] [blame] | 132 | generate_test(class_name, 'r8', 'R8', relative_package) |
Tamas Kenez | 458fee5 | 2018-09-28 10:38:39 +0200 | [diff] [blame] | 133 | generate_test(class_name, 'r8cf', 'R8CF', relative_package) |
Tamas Kenez | 25a99e9 | 2017-05-29 10:15:30 +0200 | [diff] [blame] | 134 | |
| 135 | |
| 136 | if __name__ == '__main__': |
| 137 | sys.exit(Main()) |