blob: bf06e184dc432599c4f63d23bc72a79f97e3153b [file] [log] [blame]
Tamas Berghammer2e169022015-04-02 11:07:55 +00001""" This module contains functions used by the test cases to hide the
2architecture and/or the platform dependent nature of the tests. """
3
Zachary Turnerc1b7cd72015-11-05 19:22:28 +00004from __future__ import absolute_import
5
6# System modules
Zachary Turner5d3b3c72016-02-18 18:50:02 +00007import itertools
Zachary Turner62d3a652016-02-03 19:12:30 +00008import re
9import subprocess
Zachary Turner7a5382d2016-02-04 18:03:01 +000010import sys
Zachary Turnerc1b7cd72015-11-05 19:22:28 +000011
12# Third-party modules
Zachary Turner5d3b3c72016-02-18 18:50:02 +000013import six
Zachary Turner62d3a652016-02-03 19:12:30 +000014from six.moves.urllib import parse as urlparse
Zachary Turnerc1b7cd72015-11-05 19:22:28 +000015
16# LLDB modules
Zachary Turner62d3a652016-02-03 19:12:30 +000017from . import configuration
18import use_lldb_suite
19import lldb
Sagar Thakur9bf15282015-11-19 11:01:21 +000020
Tamas Berghammer2e169022015-04-02 11:07:55 +000021def check_first_register_readable(test_case):
Sagar Thakur9bf15282015-11-19 11:01:21 +000022 arch = test_case.getArchitecture()
23
24 if arch in ['x86_64', 'i386']:
Tamas Berghammer2e169022015-04-02 11:07:55 +000025 test_case.expect("register read eax", substrs = ['eax = 0x'])
Sagar Thakur9bf15282015-11-19 11:01:21 +000026 elif arch in ['arm']:
Tamas Berghammer3215d042015-04-17 09:37:06 +000027 test_case.expect("register read r0", substrs = ['r0 = 0x'])
Sagar Thakur9bf15282015-11-19 11:01:21 +000028 elif arch in ['aarch64']:
Tamas Berghammer2e169022015-04-02 11:07:55 +000029 test_case.expect("register read x0", substrs = ['x0 = 0x'])
Sagar Thakur9bf15282015-11-19 11:01:21 +000030 elif re.match("mips",arch):
31 test_case.expect("register read zero", substrs = ['zero = 0x'])
Tamas Berghammer2e169022015-04-02 11:07:55 +000032 else:
33 # TODO: Add check for other architectures
34 test_case.fail("Unsupported architecture for test case (arch: %s)" % test_case.getArchitecture())
Zachary Turner62d3a652016-02-03 19:12:30 +000035
36def _run_adb_command(cmd, device_id):
37 device_id_args = []
38 if device_id:
39 device_id_args = ["-s", device_id]
40 full_cmd = ["adb"] + device_id_args + cmd
Zachary Turner3abbf6f2016-02-03 22:53:18 +000041 p = subprocess.Popen(full_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Zachary Turner62d3a652016-02-03 19:12:30 +000042 stdout, stderr = p.communicate()
43 return p.returncode, stdout, stderr
44
45def _target_is_android():
46 if not hasattr(_target_is_android, 'result'):
47 triple = lldb.DBG.GetSelectedPlatform().GetTriple()
48 match = re.match(".*-.*-.*-android", triple)
49 _target_is_android.result = match is not None
50 return _target_is_android.result
51
52def android_device_api():
53 if not hasattr(android_device_api, 'result'):
54 assert configuration.lldb_platform_url is not None
55 device_id = None
56 parsed_url = urlparse.urlparse(configuration.lldb_platform_url)
57 host_name = parsed_url.netloc.split(":")[0]
58 if host_name != 'localhost':
59 device_id = host_name
60 if device_id.startswith('[') and device_id.endswith(']'):
61 device_id = device_id[1:-1]
62 retcode, stdout, stderr = _run_adb_command(
63 ["shell", "getprop", "ro.build.version.sdk"], device_id)
64 if retcode == 0:
65 android_device_api.result = int(stdout)
66 else:
67 raise LookupError(
68 ">>> Unable to determine the API level of the Android device.\n"
69 ">>> stdout:\n%s\n"
70 ">>> stderr:\n%s\n" % (stdout, stderr))
71 return android_device_api.result
72
73def match_android_device(device_arch, valid_archs=None, valid_api_levels=None):
74 if not _target_is_android():
75 return False
76 if valid_archs is not None and device_arch not in valid_archs:
77 return False
78 if valid_api_levels is not None and android_device_api() not in valid_api_levels:
79 return False
80
81 return True
82
83def finalize_build_dictionary(dictionary):
84 if _target_is_android():
85 if dictionary is None:
86 dictionary = {}
87 dictionary["OS"] = "Android"
88 if android_device_api() >= 16:
89 dictionary["PIE"] = 1
90 return dictionary
Zachary Turner7a5382d2016-02-04 18:03:01 +000091
92def getHostPlatform():
93 """Returns the host platform running the test suite."""
94 # Attempts to return a platform name matching a target Triple platform.
95 if sys.platform.startswith('linux'):
96 return 'linux'
97 elif sys.platform.startswith('win32'):
98 return 'windows'
99 elif sys.platform.startswith('darwin'):
100 return 'darwin'
101 elif sys.platform.startswith('freebsd'):
102 return 'freebsd'
103 elif sys.platform.startswith('netbsd'):
104 return 'netbsd'
105 else:
106 return sys.platform
Zachary Turner9a1a2942016-02-04 23:04:17 +0000107
108
109def getDarwinOSTriples():
110 return ['darwin', 'macosx', 'ios']
111
112def getPlatform():
113 """Returns the target platform which the tests are running on."""
114 platform = lldb.DBG.GetSelectedPlatform().GetTriple().split('-')[2]
115 if platform.startswith('freebsd'):
116 platform = 'freebsd'
117 elif platform.startswith('netbsd'):
118 platform = 'netbsd'
119 return platform
120
121def platformIsDarwin():
122 """Returns true if the OS triple for the selected platform is any valid apple OS"""
123 return getPlatform() in getDarwinOSTriples()
124
125class _PlatformContext(object):
126 """Value object class which contains platform-specific options."""
127
128 def __init__(self, shlib_environment_var, shlib_prefix, shlib_extension):
129 self.shlib_environment_var = shlib_environment_var
130 self.shlib_prefix = shlib_prefix
131 self.shlib_extension = shlib_extension
132
133def createPlatformContext():
134 if platformIsDarwin():
135 return _PlatformContext('DYLD_LIBRARY_PATH', 'lib', 'dylib')
136 elif getPlatform() in ("freebsd", "linux", "netbsd"):
137 return _PlatformContext('LD_LIBRARY_PATH', 'lib', 'so')
138 else:
139 return None