1# Copyright 2017, The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import os
16import re
17import tempfile
18import subprocess
19
20_env = dict(os.environ)
21
22def _getEnvBoolean(var, default):
23  val = _env.get(var)
24  if val:
25    if val == "True" or val == "true":
26      return True
27    if val == "False" or val == "false":
28      return False
29  return default
30
31_DUMP_MANY_VARS_LIST = ['HOST_2ND_ARCH_PREFIX',
32                        'TARGET_2ND_ARCH',
33                        'TARGET_ARCH',
34                        'HOST_PREFER_32_BIT',
35                        'HOST_OUT_EXECUTABLES']
36_DUMP_MANY_VARS = None  # To be set to a dictionary with above list being the keys,
37                        # and the build variable being the value.
38def _dump_many_vars(var_name):
39  """
40  Reach into the Android build system to dump many build vars simultaneously.
41  Since the make system is so slow, we want to avoid calling into build frequently.
42  """
43  global _DUMP_MANY_VARS
44  global _DUMP_MANY_VARS_LIST
45
46  # Look up var from cache.
47  if _DUMP_MANY_VARS:
48    return _DUMP_MANY_VARS[var_name]
49
50  all_vars=" ".join(_DUMP_MANY_VARS_LIST)
51
52  # The command is taken from build/envsetup.sh to fetch build variables.
53  command = ("CALLED_FROM_SETUP=true "  # Enable the 'dump-many-vars' make target.
54             "BUILD_SYSTEM=build/core " # Set up lookup path for make includes.
55             "make --no-print-directory -C \"%s\" -f build/core/config.mk "
56             "dump-many-vars DUMP_MANY_VARS=\"%s\"") % (ANDROID_BUILD_TOP, all_vars)
57
58  config = subprocess.Popen(command,
59                            stdout=subprocess.PIPE,
60                            universal_newlines=True,
61                            shell=True).communicate()[0] # read until EOF, select stdin
62  # Prints out something like:
63  # TARGET_ARCH='arm64'
64  # HOST_ARCH='x86_64'
65  _DUMP_MANY_VARS = {}
66  for line in config.split("\n"):
67    # Split out "$key='$value'" via regex.
68    match = re.search("([^=]+)='([^']*)", line)
69    if not match:
70      continue
71    key = match.group(1)
72    value = match.group(2)
73    _DUMP_MANY_VARS[key] = value
74
75  return _DUMP_MANY_VARS[var_name]
76
77def _get_build_var(var_name):
78  return _dump_many_vars(var_name)
79
80def get_env(key):
81  return _env.get(key)
82
83def _get_android_build_top():
84  path_to_top = _env.get('ANDROID_BUILD_TOP')
85  if not path_to_top:
86    # nothing set. try to guess it based on the relative path of this env.py file.
87    this_file_path = os.path.realpath(__file__)
88    path_to_top = os.path.join(os.path.dirname(this_file_path), '../../../')
89    path_to_top = os.path.realpath(path_to_top)
90
91  if not os.path.exists(os.path.join(path_to_top, 'build/envsetup.sh')):
92    raise AssertionError("env.py must be located inside an android source tree")
93
94  return path_to_top
95
96ANDROID_BUILD_TOP = _get_android_build_top()
97
98# Directory used for temporary test files on the host.
99ART_HOST_TEST_DIR = tempfile.mkdtemp(prefix = 'test-art-')
100
101# Keep going after encountering a test failure?
102ART_TEST_KEEP_GOING = _getEnvBoolean('ART_TEST_KEEP_GOING', True)
103
104# Do you want all tests, even those that are time consuming?
105ART_TEST_FULL = _getEnvBoolean('ART_TEST_FULL', False)
106
107# Do you want interpreter tests run?
108ART_TEST_INTERPRETER = _getEnvBoolean('ART_TEST_INTERPRETER', ART_TEST_FULL)
109ART_TEST_INTERPRETER_ACCESS_CHECKS = _getEnvBoolean('ART_TEST_INTERPRETER_ACCESS_CHECKS',
110                                                   ART_TEST_FULL)
111
112# Do you want JIT tests run?
113ART_TEST_JIT = _getEnvBoolean('ART_TEST_JIT', ART_TEST_FULL)
114
115# Do you want optimizing compiler tests run?
116ART_TEST_OPTIMIZING = _getEnvBoolean('ART_TEST_OPTIMIZING', ART_TEST_FULL)
117
118# Do you want to test the optimizing compiler with graph coloring register allocation?
119ART_TEST_OPTIMIZING_GRAPH_COLOR = _getEnvBoolean('ART_TEST_OPTIMIZING_GRAPH_COLOR', ART_TEST_FULL)
120
121# Do you want to do run-tests with profiles?
122ART_TEST_SPEED_PROFILE = _getEnvBoolean('ART_TEST_SPEED_PROFILE', ART_TEST_FULL)
123
124# Do we want to test PIC-compiled tests ("apps")?
125ART_TEST_PIC_TEST = _getEnvBoolean('ART_TEST_PIC_TEST', ART_TEST_FULL)
126# Do you want tracing tests run?
127ART_TEST_TRACE = _getEnvBoolean('ART_TEST_TRACE', ART_TEST_FULL)
128
129# Do you want tracing tests (streaming mode) run?
130ART_TEST_TRACE_STREAM = _getEnvBoolean('ART_TEST_TRACE_STREAM', ART_TEST_FULL)
131
132# Do you want tests with GC verification enabled run?
133ART_TEST_GC_VERIFY = _getEnvBoolean('ART_TEST_GC_VERIFY', ART_TEST_FULL)
134
135# Do you want tests with the GC stress mode enabled run?
136ART_TEST_GC_STRESS = _getEnvBoolean('ART_TEST_GC_STRESS', ART_TEST_FULL)
137
138# Do you want tests with the JNI forcecopy mode enabled run?
139ART_TEST_JNI_FORCECOPY = _getEnvBoolean('ART_TEST_JNI_FORCECOPY', ART_TEST_FULL)
140
141# Do you want run-tests with relocation disabled run?
142ART_TEST_RUN_TEST_RELOCATE = _getEnvBoolean('ART_TEST_RUN_TEST_RELOCATE', ART_TEST_FULL)
143
144# Do you want run-tests with prebuilding?
145ART_TEST_RUN_TEST_PREBUILD = _getEnvBoolean('ART_TEST_RUN_TEST_PREBUILD', ART_TEST_FULL)
146
147# Do you want run-tests with no prebuilding enabled run?
148ART_TEST_RUN_TEST_NO_PREBUILD = _getEnvBoolean('ART_TEST_RUN_TEST_NO_PREBUILD', ART_TEST_FULL)
149
150# Do you want run-tests with a pregenerated core.art?
151ART_TEST_RUN_TEST_IMAGE = _getEnvBoolean('ART_TEST_RUN_TEST_IMAGE', ART_TEST_FULL)
152
153# Do you want run-tests without a pregenerated core.art?
154ART_TEST_RUN_TEST_NO_IMAGE = _getEnvBoolean('ART_TEST_RUN_TEST_NO_IMAGE', ART_TEST_FULL)
155
156# Do you want run-tests with relocation enabled but patchoat failing?
157ART_TEST_RUN_TEST_RELOCATE_NO_PATCHOAT = _getEnvBoolean('ART_TEST_RUN_TEST_RELOCATE_NO_PATCHOAT',
158                                                       ART_TEST_FULL)
159
160# Do you want run-tests without a dex2oat?
161ART_TEST_RUN_TEST_NO_DEX2OAT = _getEnvBoolean('ART_TEST_RUN_TEST_NO_DEX2OAT', ART_TEST_FULL)
162
163# Do you want run-tests with libartd.so?
164ART_TEST_RUN_TEST_DEBUG = _getEnvBoolean('ART_TEST_RUN_TEST_DEBUG', ART_TEST_FULL)
165
166# Do you want run-tests with libart.so?
167ART_TEST_RUN_TEST_NDEBUG = _getEnvBoolean('ART_TEST_RUN_TEST_NDEBUG', ART_TEST_FULL)
168
169# Do you want failed tests to have their artifacts cleaned up?
170ART_TEST_RUN_TEST_ALWAYS_CLEAN = _getEnvBoolean('ART_TEST_RUN_TEST_ALWAYS_CLEAN', True)
171
172# Do you want run-tests with the --debuggable flag
173ART_TEST_RUN_TEST_DEBUGGABLE = _getEnvBoolean('ART_TEST_RUN_TEST_DEBUGGABLE', ART_TEST_FULL)
174
175# Do you want to test multi-part boot-image functionality?
176ART_TEST_RUN_TEST_MULTI_IMAGE = _getEnvBoolean('ART_TEST_RUN_TEST_MULTI_IMAGE', ART_TEST_FULL)
177
178ART_TEST_DEBUG_GC = _getEnvBoolean('ART_TEST_DEBUG_GC', False)
179
180ART_TEST_BISECTION = _getEnvBoolean('ART_TEST_BISECTION', False)
181
182DEX2OAT_HOST_INSTRUCTION_SET_FEATURES = _env.get('DEX2OAT_HOST_INSTRUCTION_SET_FEATURES')
183
184# Do you want run-tests with the host/target's second arch?
185ART_TEST_RUN_TEST_2ND_ARCH = _getEnvBoolean('ART_TEST_RUN_TEST_2ND_ARCH', True)
186
187HOST_2ND_ARCH_PREFIX = _get_build_var('HOST_2ND_ARCH_PREFIX')
188HOST_2ND_ARCH_PREFIX_DEX2OAT_HOST_INSTRUCTION_SET_FEATURES = _env.get(
189  HOST_2ND_ARCH_PREFIX + 'DEX2OAT_HOST_INSTRUCTION_SET_FEATURES')
190
191ART_TEST_ANDROID_ROOT = _env.get('ART_TEST_ANDROID_ROOT')
192
193ART_TEST_WITH_STRACE = _getEnvBoolean('ART_TEST_DEBUG_GC', False)
194
195EXTRA_DISABLED_TESTS = set(_env.get("ART_TEST_RUN_TEST_SKIP", "").split())
196
197ART_TEST_RUN_TEST_BUILD = _getEnvBoolean('ART_TEST_RUN_TEST_BUILD', False)
198
199TARGET_2ND_ARCH = _get_build_var('TARGET_2ND_ARCH')
200TARGET_ARCH = _get_build_var('TARGET_ARCH')
201
202# Note: ART_2ND_PHONY_TEST_TARGET_SUFFIX is 2ND_ART_PHONY_TEST_TARGET_SUFFIX in .mk files
203# Note: ART_2ND_PHONY_TEST_HOST_SUFFIX is 2ND_ART_PHONY_HOST_TARGET_SUFFIX in .mk files
204# Python does not let us have variable names starting with a digit, so it has differ.
205
206ART_TEST_RUN_TEST_JVMTI_STRESS = _getEnvBoolean('ART_TEST_RUN_TEST_JVMTI_STRESS', ART_TEST_FULL)
207
208if TARGET_2ND_ARCH:
209  if "64" in TARGET_ARCH:
210    ART_PHONY_TEST_TARGET_SUFFIX = "64"
211    ART_2ND_PHONY_TEST_TARGET_SUFFIX = "32"
212  else:
213    ART_PHONY_TEST_TARGET_SUFFIX = "32"
214    ART_2ND_PHONY_TEST_TARGET_SUFFIX = ""
215else:
216  if "64" in TARGET_ARCH:
217    ART_PHONY_TEST_TARGET_SUFFIX = "64"
218    ART_2ND_PHONY_TEST_TARGET_SUFFIX = ""
219  else:
220    ART_PHONY_TEST_TARGET_SUFFIX = "32"
221    ART_2ND_PHONY_TEST_TARGET_SUFFIX = ""
222
223HOST_PREFER_32_BIT = _get_build_var('HOST_PREFER_32_BIT')
224if HOST_PREFER_32_BIT == "true":
225  ART_PHONY_TEST_HOST_SUFFIX = "32"
226  ART_2ND_PHONY_TEST_HOST_SUFFIX = ""
227else:
228  ART_PHONY_TEST_HOST_SUFFIX = "64"
229  ART_2ND_PHONY_TEST_HOST_SUFFIX = "32"
230
231HOST_OUT_EXECUTABLES = os.path.join(ANDROID_BUILD_TOP,
232                                    _get_build_var("HOST_OUT_EXECUTABLES"))
233os.environ['JACK'] = HOST_OUT_EXECUTABLES + '/jack'
234os.environ['DX'] = HOST_OUT_EXECUTABLES + '/dx'
235os.environ['SMALI'] = HOST_OUT_EXECUTABLES + '/smali'
236os.environ['JASMIN'] = HOST_OUT_EXECUTABLES + '/jasmin'
237os.environ['DXMERGER'] = HOST_OUT_EXECUTABLES + '/dexmerger'
238