| OLD | NEW |
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 """Build/test configurations, which are just dictionaries. This | |
| 6 "defines" the schema and provides some wrappers.""" | |
| 7 | 5 |
| 8 | 6 import ast |
| 7 import os.path |
| 9 import platform | 8 import platform |
| 9 import re |
| 10 import sys | 10 import sys |
| 11 | 11 |
| 12 | 12 |
| 13 class Config(object): | 13 class Config(object): |
| 14 """A Config is basically just a wrapper around a dictionary that species a | 14 """A Config contains a dictionary that species a build configuration.""" |
| 15 build/test configuration. The dictionary is accessible through the values | |
| 16 member.""" | |
| 17 | 15 |
| 18 # Valid values for target_os (None is also valid): | 16 # Valid values for target_os: |
| 19 OS_ANDROID = "android" | 17 OS_ANDROID = "android" |
| 20 OS_CHROMEOS = "chromeos" | 18 OS_CHROMEOS = "chromeos" |
| 21 OS_LINUX = "linux" | 19 OS_LINUX = "linux" |
| 22 OS_MAC = "mac" | 20 OS_MAC = "mac" |
| 23 OS_WINDOWS = "windows" | 21 OS_WINDOWS = "windows" |
| 24 | 22 |
| 25 # Valid values for target_cpu (None is also valid): | 23 # Valid values for target_cpu: |
| 26 ARCH_X86 = "x86" | 24 ARCH_X86 = "x86" |
| 27 ARCH_X64 = "x64" | 25 ARCH_X64 = "x64" |
| 28 ARCH_ARM = "arm" | 26 ARCH_ARM = "arm" |
| 29 | 27 |
| 30 # Valid values for sanitizer (None is also valid): | 28 def __init__(self, build_dir=None, target_os=None, target_cpu=None, |
| 31 SANITIZER_ASAN = "asan" | 29 is_debug=None, apk_name="MojoRunner.apk"): |
| 32 | 30 """Function arguments take precedence over GN args and default values.""" |
| 33 # Standard values for test types (test types are arbitrary strings; other | |
| 34 # values are allowed). | |
| 35 TEST_TYPE_DEFAULT = "default" | |
| 36 TEST_TYPE_UNIT = "unit" | |
| 37 TEST_TYPE_PERF = "perf" | |
| 38 TEST_TYPE_INTEGRATION = "integration" | |
| 39 | |
| 40 def __init__(self, target_os=None, target_cpu=None, is_debug=True, | |
| 41 is_clang=None, sanitizer=None, dcheck_always_on=False, | |
| 42 apk_name="MojoRunner.apk", **kwargs): | |
| 43 """Constructs a Config with key-value pairs specified via keyword arguments. | |
| 44 If target_os is not specified, it will be set to the host OS.""" | |
| 45 | |
| 46 assert target_os in (None, Config.OS_ANDROID, Config.OS_CHROMEOS, | 31 assert target_os in (None, Config.OS_ANDROID, Config.OS_CHROMEOS, |
| 47 Config.OS_LINUX, Config.OS_MAC, Config.OS_WINDOWS) | 32 Config.OS_LINUX, Config.OS_MAC, Config.OS_WINDOWS) |
| 48 assert target_cpu in (None, Config.ARCH_X86, Config.ARCH_X64, | 33 assert target_cpu in (None, Config.ARCH_X86, Config.ARCH_X64, |
| 49 Config.ARCH_ARM) | 34 Config.ARCH_ARM) |
| 50 assert isinstance(is_debug, bool) | 35 assert is_debug in (None, True, False) |
| 51 assert is_clang is None or isinstance(is_clang, bool) | |
| 52 assert sanitizer in (None, Config.SANITIZER_ASAN) | |
| 53 if "test_types" in kwargs: | |
| 54 assert isinstance(kwargs["test_types"], list) | |
| 55 | 36 |
| 56 self.values = {} | 37 self.values = { |
| 57 self.values["target_os"] = (self.GetHostOS() if target_os is None else | 38 "build_dir": build_dir, |
| 58 target_os) | 39 "target_os": self.GetHostOS(), |
| 40 "target_cpu": self.GetHostCPU(), |
| 41 "is_debug": True, |
| 42 "dcheck_always_on": False, |
| 43 "is_asan": False, |
| 44 "apk_name": apk_name, |
| 45 } |
| 59 | 46 |
| 60 if target_cpu is None: | 47 self._ParseGNArgs() |
| 61 if target_os == Config.OS_ANDROID: | 48 if target_os is not None: |
| 62 target_cpu = Config.ARCH_ARM | 49 self.values["target_os"] = target_os |
| 63 else: | 50 if target_cpu is not None: |
| 64 target_cpu = self.GetHostCPUArch() | 51 self.values["target_cpu"] = target_cpu |
| 65 self.values["target_cpu"] = target_cpu | 52 if is_debug is not None: |
| 66 | 53 self.values["is_debug"] = is_debug |
| 67 self.values["is_debug"] = is_debug | |
| 68 self.values["is_clang"] = is_clang | |
| 69 self.values["sanitizer"] = sanitizer | |
| 70 self.values["dcheck_always_on"] = dcheck_always_on | |
| 71 self.values["apk_name"] = apk_name | |
| 72 | |
| 73 self.values.update(kwargs) | |
| 74 | 54 |
| 75 @staticmethod | 55 @staticmethod |
| 76 def GetHostOS(): | 56 def GetHostOS(): |
| 77 if sys.platform == "linux2": | 57 if sys.platform == "linux2": |
| 78 return Config.OS_LINUX | 58 return Config.OS_LINUX |
| 79 if sys.platform == "darwin": | 59 if sys.platform == "darwin": |
| 80 return Config.OS_MAC | 60 return Config.OS_MAC |
| 81 if sys.platform == "win32": | 61 if sys.platform == "win32": |
| 82 return Config.OS_WINDOWS | 62 return Config.OS_WINDOWS |
| 83 raise NotImplementedError("Unsupported host OS") | 63 raise NotImplementedError("Unsupported host OS") |
| 84 | 64 |
| 85 @staticmethod | 65 @staticmethod |
| 86 def GetHostCPUArch(): | 66 def GetHostCPU(): |
| 87 # Derived from //native_client/pynacl/platform.py | 67 # Derived from //native_client/pynacl/platform.py |
| 88 machine = platform.machine() | 68 machine = platform.machine() |
| 89 if machine in ("x86", "x86-32", "x86_32", "x8632", "i386", "i686", "ia32", | 69 if machine in ("x86", "x86-32", "x86_32", "x8632", "i386", "i686", "ia32", |
| 90 "32"): | 70 "32"): |
| 91 return Config.ARCH_X86 | 71 return Config.ARCH_X86 |
| 92 if machine in ("x86-64", "amd64", "x86_64", "x8664", "64"): | 72 if machine in ("x86-64", "amd64", "x86_64", "x8664", "64"): |
| 93 return Config.ARCH_X64 | 73 return Config.ARCH_X64 |
| 94 if machine.startswith("arm"): | 74 if machine.startswith("arm"): |
| 95 return Config.ARCH_ARM | 75 return Config.ARCH_ARM |
| 96 raise Exception("Cannot identify CPU arch: %s" % machine) | 76 raise Exception("Cannot identify CPU arch: %s" % machine) |
| 97 | 77 |
| 78 def _ParseGNArgs(self): |
| 79 """Parse the gn config file from the build directory, if it exists.""" |
| 80 TRANSLATIONS = { "true": "True", "false": "False", } |
| 81 if self.values["build_dir"] is None: |
| 82 return |
| 83 gn_file = os.path.join(self.values["build_dir"], "args.gn") |
| 84 if not os.path.isfile(gn_file): |
| 85 return |
| 86 |
| 87 with open(gn_file, "r") as f: |
| 88 for line in f: |
| 89 line = re.sub("\s*#.*", "", line) |
| 90 result = re.match("^\s*(\w+)\s*=\s*(.*)\s*$", line) |
| 91 if result: |
| 92 key = result.group(1) |
| 93 value = result.group(2) |
| 94 self.values[key] = ast.literal_eval(TRANSLATIONS.get(value, value)) |
| 95 |
| 98 # Getters for standard fields ------------------------------------------------ | 96 # Getters for standard fields ------------------------------------------------ |
| 99 | 97 |
| 100 @property | 98 @property |
| 99 def build_dir(self): |
| 100 """Build directory path.""" |
| 101 return self.values["build_dir"] |
| 102 |
| 103 @property |
| 101 def target_os(self): | 104 def target_os(self): |
| 102 """OS of the build/test target.""" | 105 """OS of the build/test target.""" |
| 103 return self.values["target_os"] | 106 return self.values["target_os"] |
| 104 | 107 |
| 105 @property | 108 @property |
| 106 def target_cpu(self): | 109 def target_cpu(self): |
| 107 """CPU arch of the build/test target.""" | 110 """CPU arch of the build/test target.""" |
| 108 return self.values["target_cpu"] | 111 return self.values["target_cpu"] |
| 109 | 112 |
| 110 @property | 113 @property |
| 111 def is_debug(self): | 114 def is_debug(self): |
| 112 """Is Debug build?""" | 115 """Is Debug build?""" |
| 113 return self.values["is_debug"] | 116 return self.values["is_debug"] |
| 114 | 117 |
| 115 @property | 118 @property |
| 116 def dcheck_always_on(self): | 119 def dcheck_always_on(self): |
| 117 """DCHECK and MOJO_DCHECK are fatal even in release builds""" | 120 """DCHECK and MOJO_DCHECK are fatal even in release builds""" |
| 118 return self.values["dcheck_always_on"] | 121 return self.values["dcheck_always_on"] |
| 119 | 122 |
| 120 @property | 123 @property |
| 124 def is_asan(self): |
| 125 """Is ASAN build?""" |
| 126 return self.values["is_asan"] |
| 127 |
| 128 @property |
| 121 def apk_name(self): | 129 def apk_name(self): |
| 122 """Name of the APK file to run""" | 130 """Name of the APK file to run""" |
| 123 return self.values["apk_name"] | 131 return self.values["apk_name"] |
| 124 | |
| 125 @property | |
| 126 def is_clang(self): | |
| 127 """Should use clang?""" | |
| 128 return self.values["is_clang"] | |
| 129 | |
| 130 @property | |
| 131 def sanitizer(self): | |
| 132 """Sanitizer to use, if any.""" | |
| 133 return self.values["sanitizer"] | |
| 134 | |
| 135 @property | |
| 136 def test_types(self): | |
| 137 """List of test types to run.""" | |
| 138 return self.values.get("test_types", [Config.TEST_TYPE_DEFAULT]) | |
| OLD | NEW |