Move some android platform functions to lldbplatformutil.

My eventual goal is to move all of the test decorators to their
own module such as `decorators.py`.  But some of the decorators
use existing functions in `lldbtest.py` and conceptually the
functions are probably more appropriately placed in lldbplatformutil.
Moreover, lldbtest.py is a huge file with a ton of random utility
functions scattered around, so this patch also workds toward the
goal of reducing the footprint of this one module to a more
reasonable size.

So this patch moves some of them over to lldbplatformutil with the
eventual goal of moving decorators over to their own module.

Reviewed By: Tamas Berghammer, Pavel Labath
Differential Revision: http://reviews.llvm.org/D16830

llvm-svn: 259680
diff --git a/lldb/packages/Python/lldbsuite/test/lldbplatformutil.py b/lldb/packages/Python/lldbsuite/test/lldbplatformutil.py
index f51c016..ff49d7d 100644
--- a/lldb/packages/Python/lldbsuite/test/lldbplatformutil.py
+++ b/lldb/packages/Python/lldbsuite/test/lldbplatformutil.py
@@ -4,12 +4,16 @@
 from __future__ import absolute_import
 
 # System modules
+import re
+import subprocess
 
 # Third-party modules
+from six.moves.urllib import parse as urlparse
 
 # LLDB modules
-
-import re
+from . import configuration
+import use_lldb_suite
+import lldb
 
 def check_first_register_readable(test_case):
     arch = test_case.getArchitecture()
@@ -25,3 +29,59 @@
     else:
         # TODO: Add check for other architectures
         test_case.fail("Unsupported architecture for test case (arch: %s)" % test_case.getArchitecture())
+
+def _run_adb_command(cmd, device_id):
+    device_id_args = []
+    if device_id:
+        device_id_args = ["-s", device_id]
+    full_cmd = ["adb"] + device_id_args + cmd
+    p = subprocess.Popen(full_cmd, stdout=PIPE, stderr=PIPE)
+    stdout, stderr = p.communicate()
+    return p.returncode, stdout, stderr
+
+def _target_is_android():
+    if not hasattr(_target_is_android, 'result'):
+        triple = lldb.DBG.GetSelectedPlatform().GetTriple()
+        match = re.match(".*-.*-.*-android", triple)
+        _target_is_android.result = match is not None
+    return _target_is_android.result
+
+def android_device_api():
+    if not hasattr(android_device_api, 'result'):
+        assert configuration.lldb_platform_url is not None
+        device_id = None
+        parsed_url = urlparse.urlparse(configuration.lldb_platform_url)
+        host_name = parsed_url.netloc.split(":")[0]
+        if host_name != 'localhost':
+            device_id = host_name
+            if device_id.startswith('[') and device_id.endswith(']'):
+                device_id = device_id[1:-1]
+        retcode, stdout, stderr = _run_adb_command(
+            ["shell", "getprop", "ro.build.version.sdk"], device_id)
+        if retcode == 0:
+            android_device_api.result = int(stdout)
+        else:
+            raise LookupError(
+                ">>> Unable to determine the API level of the Android device.\n"
+                ">>> stdout:\n%s\n"
+                ">>> stderr:\n%s\n" % (stdout, stderr))
+    return android_device_api.result
+
+def match_android_device(device_arch, valid_archs=None, valid_api_levels=None):
+    if not _target_is_android():
+        return False
+    if valid_archs is not None and device_arch not in valid_archs:
+        return False
+    if valid_api_levels is not None and android_device_api() not in valid_api_levels:
+        return False
+
+    return True
+
+def finalize_build_dictionary(dictionary):
+    if _target_is_android():
+        if dictionary is None:
+            dictionary = {}
+        dictionary["OS"] = "Android"
+        if android_device_api() >= 16:
+            dictionary["PIE"] = 1
+    return dictionary