Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / mojo / tools / mopy / config.py
blob4b17c2dee83a2e35e28c70bb0dce74ee7d4ed601
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
3 # found in the LICENSE file.
6 import ast
7 import os.path
8 import platform
9 import re
10 import sys
13 class Config(object):
14 '''A Config contains a dictionary that species a build configuration.'''
16 # Valid values for target_os:
17 OS_ANDROID = 'android'
18 OS_CHROMEOS = 'chromeos'
19 OS_LINUX = 'linux'
20 OS_MAC = 'mac'
21 OS_WINDOWS = 'windows'
23 # Valid values for target_cpu:
24 ARCH_X86 = 'x86'
25 ARCH_X64 = 'x64'
26 ARCH_ARM = 'arm'
28 def __init__(self, build_dir=None, target_os=None, target_cpu=None,
29 is_debug=None, is_verbose=None, apk_name='MojoRunner.apk'):
30 '''Function arguments take precedence over GN args and default values.'''
31 assert target_os in (None, Config.OS_ANDROID, Config.OS_CHROMEOS,
32 Config.OS_LINUX, Config.OS_MAC, Config.OS_WINDOWS)
33 assert target_cpu in (None, Config.ARCH_X86, Config.ARCH_X64,
34 Config.ARCH_ARM)
35 assert is_debug in (None, True, False)
36 assert is_verbose in (None, True, False)
38 self.values = {
39 'build_dir': build_dir,
40 'target_os': self.GetHostOS(),
41 'target_cpu': self.GetHostCPU(),
42 'is_debug': True,
43 'is_verbose': True,
44 'dcheck_always_on': False,
45 'is_asan': False,
46 'apk_name': apk_name,
49 self._ParseGNArgs()
50 if target_os is not None:
51 self.values['target_os'] = target_os
52 if target_cpu is not None:
53 self.values['target_cpu'] = target_cpu
54 if is_debug is not None:
55 self.values['is_debug'] = is_debug
56 if is_verbose is not None:
57 self.values['is_verbose'] = is_verbose
59 @staticmethod
60 def GetHostOS():
61 if sys.platform == 'linux2':
62 return Config.OS_LINUX
63 if sys.platform == 'darwin':
64 return Config.OS_MAC
65 if sys.platform == 'win32':
66 return Config.OS_WINDOWS
67 raise NotImplementedError('Unsupported host OS')
69 @staticmethod
70 def GetHostCPU():
71 # Derived from //native_client/pynacl/platform.py
72 machine = platform.machine()
73 if machine in ('x86', 'x86-32', 'x86_32', 'x8632', 'i386', 'i686', 'ia32',
74 '32'):
75 return Config.ARCH_X86
76 if machine in ('x86-64', 'amd64', 'AMD64', 'x86_64', 'x8664', '64'):
77 return Config.ARCH_X64
78 if machine.startswith('arm'):
79 return Config.ARCH_ARM
80 raise Exception('Cannot identify CPU arch: %s' % machine)
82 def _ParseGNArgs(self):
83 '''Parse the gn config file from the build directory, if it exists.'''
84 TRANSLATIONS = { 'true': 'True', 'false': 'False', }
85 if self.values['build_dir'] is None:
86 return
87 gn_file = os.path.join(self.values['build_dir'], 'args.gn')
88 if not os.path.isfile(gn_file):
89 return
91 with open(gn_file, 'r') as f:
92 for line in f:
93 line = re.sub('\s*#.*', '', line)
94 result = re.match('^\s*(\w+)\s*=\s*(.*)\s*$', line)
95 if result:
96 key = result.group(1)
97 value = result.group(2)
98 self.values[key] = ast.literal_eval(TRANSLATIONS.get(value, value))
100 # Getters for standard fields ------------------------------------------------
102 @property
103 def build_dir(self):
104 '''Build directory path.'''
105 return self.values['build_dir']
107 @property
108 def target_os(self):
109 '''OS of the build/test target.'''
110 return self.values['target_os']
112 @property
113 def target_cpu(self):
114 '''CPU arch of the build/test target.'''
115 return self.values['target_cpu']
117 @property
118 def is_debug(self):
119 '''Is Debug build?'''
120 return self.values['is_debug']
122 @property
123 def is_verbose(self):
124 '''Should print additional logging information?'''
125 return self.values['is_verbose']
127 @property
128 def dcheck_always_on(self):
129 '''DCHECK and MOJO_DCHECK are fatal even in release builds'''
130 return self.values['dcheck_always_on']
132 @property
133 def is_asan(self):
134 '''Is ASAN build?'''
135 return self.values['is_asan']
137 @property
138 def apk_name(self):
139 '''Name of the APK file to run'''
140 return self.values['apk_name']