1#!/usr/bin/python3
2#
3# Copyright (C) 2015 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17"""
18Generate java test files for test 966.
19"""
20
21import generate_smali as base
22import os
23import sys
24from pathlib import Path
25
26BUILD_TOP = os.getenv("ANDROID_BUILD_TOP")
27if BUILD_TOP is None:
28  print("ANDROID_BUILD_TOP not set. Please run build/envsetup.sh", file=sys.stderr)
29  sys.exit(1)
30
31# Allow us to import mixins.
32sys.path.append(str(Path(BUILD_TOP)/"art"/"test"/"utils"/"python"))
33
34import testgen.mixins as mixins
35import functools
36import operator
37import subprocess
38
39class JavaConverter(mixins.DumpMixin, mixins.Named, mixins.JavaFileMixin):
40  """
41  A class that can convert a SmaliFile to a JavaFile.
42  """
43  def __init__(self, inner):
44    self.inner = inner
45
46  def get_name(self):
47    """Gets the name of this file."""
48    return self.inner.get_name()
49
50  def __str__(self):
51    out = ""
52    for line in str(self.inner).splitlines(keepends = True):
53      if line.startswith("#"):
54        out += line[1:]
55    return out
56
57class Compiler:
58  def __init__(self, sources, javac, temp_dir, classes_dir):
59    self.javac = javac
60    self.temp_dir = temp_dir
61    self.classes_dir = classes_dir
62    self.sources = sources
63
64  def compile_files(self, args, files):
65    """
66    Compile the files given with the arguments given.
67    """
68    args = args.split()
69    files = list(map(str, files))
70    cmd = ['sh', '-a', '-e', '--', str(self.javac)] + args + sorted(files)
71    subprocess.check_call(cmd)
72
73  def execute(self):
74    """
75    Compiles this test, doing partial compilation as necessary.
76    """
77    # Compile Main and all classes first. Force all interfaces to be default so that there will be
78    # no compiler problems (works since classes only implement 1 interface).
79    for f in self.sources:
80      if isinstance(f, base.TestInterface):
81        JavaConverter(f.get_specific_version(base.InterfaceType.default)).dump(self.temp_dir)
82      else:
83        JavaConverter(f).dump(self.temp_dir)
84    self.compile_files("-d {}".format(self.classes_dir), self.temp_dir.glob("*.java"))
85
86    # Now we compile the interfaces
87    ifaces = set(i for i in self.sources if isinstance(i, base.TestInterface))
88    filters = (lambda a: a.is_default(), lambda a: not a.is_default())
89    converters = (lambda a: JavaConverter(a.get_specific_version(base.InterfaceType.default)),
90                  lambda a: JavaConverter(a.get_specific_version(base.InterfaceType.empty)))
91    while len(ifaces) != 0:
92      for iface_filter, iface_converter in zip(filters, converters):
93        # Find those ifaces where there are no (uncompiled) interfaces that are subtypes.
94        tops = set(filter(lambda a: iface_filter(a) and not any(map(lambda i: a in i.get_super_types(), ifaces)), ifaces))
95        files = []
96        # Dump these ones, they are getting compiled.
97        for f in tops:
98          out = JavaConverter(f)
99          out.dump(self.temp_dir)
100          files.append(self.temp_dir / out.get_file_name())
101        # Force all superinterfaces of these to be empty so there will be no conflicts
102        overrides = functools.reduce(operator.or_, map(lambda i: i.get_super_types(), tops), set())
103        for overridden in overrides:
104          out = iface_converter(overridden)
105          out.dump(self.temp_dir)
106          files.append(self.temp_dir / out.get_file_name())
107        self.compile_files("-d {outdir} -cp {outdir}".format(outdir = self.classes_dir), files)
108        # Remove these from the set of interfaces to be compiled.
109        ifaces -= tops
110    return
111
112def main(argv):
113  javac_exec = Path(argv[1])
114  if not javac_exec.exists() or not javac_exec.is_file():
115    print("{} is not a shell script".format(javac_exec), file=sys.stderr)
116    sys.exit(1)
117  temp_dir = Path(argv[2])
118  if not temp_dir.exists() or not temp_dir.is_dir():
119    print("{} is not a valid source dir".format(temp_dir), file=sys.stderr)
120    sys.exit(1)
121  classes_dir = Path(argv[3])
122  if not classes_dir.exists() or not classes_dir.is_dir():
123    print("{} is not a valid classes directory".format(classes_dir), file=sys.stderr)
124    sys.exit(1)
125  expected_txt = Path(argv[4])
126  mainclass, all_files = base.create_all_test_files()
127
128  with expected_txt.open('w') as out:
129    print(mainclass.get_expected(), file=out)
130
131  Compiler(all_files, javac_exec, temp_dir, classes_dir).execute()
132
133if __name__ == '__main__':
134  main(sys.argv)
135