Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / mojo / tools / mopy / config.py
blobcac46d4f6415c08d19308809890baa9e4af96f4c
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, 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)
37 self.values = {
38 'build_dir': build_dir,
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,
47 self._ParseGNArgs()
48 if target_os is not None:
49 self.values['target_os'] = target_os
50 if target_cpu is not None:
51 self.values['target_cpu'] = target_cpu
52 if is_debug is not None:
53 self.values['is_debug'] = is_debug
55 @staticmethod
56 def GetHostOS():
57 if sys.platform == 'linux2':
58 return Config.OS_LINUX
59 if sys.platform == 'darwin':
60 return Config.OS_MAC
61 if sys.platform == 'win32':
62 return Config.OS_WINDOWS
63 raise NotImplementedError('Unsupported host OS')
65 @staticmethod
66 def GetHostCPU():
67 # Derived from //native_client/pynacl/platform.py
68 machine = platform.machine()
69 if machine in ('x86', 'x86-32', 'x86_32', 'x8632', 'i386', 'i686', 'ia32',
70 '32'):
71 return Config.ARCH_X86
72 if machine in ('x86-64', 'amd64', 'AMD64', 'x86_64', 'x8664', '64'):
73 return Config.ARCH_X64
74 if machine.startswith('arm'):
75 return Config.ARCH_ARM
76 raise Exception('Cannot identify CPU arch: %s' % machine)
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
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))
96 # Getters for standard fields ------------------------------------------------
98 @property
99 def build_dir(self):
100 '''Build directory path.'''
101 return self.values['build_dir']
103 @property
104 def target_os(self):
105 '''OS of the build/test target.'''
106 return self.values['target_os']
108 @property
109 def target_cpu(self):
110 '''CPU arch of the build/test target.'''
111 return self.values['target_cpu']
113 @property
114 def is_debug(self):
115 '''Is Debug build?'''
116 return self.values['is_debug']
118 @property
119 def dcheck_always_on(self):
120 '''DCHECK and MOJO_DCHECK are fatal even in release builds'''
121 return self.values['dcheck_always_on']
123 @property
124 def is_asan(self):
125 '''Is ASAN build?'''
126 return self.values['is_asan']
128 @property
129 def apk_name(self):
130 '''Name of the APK file to run'''
131 return self.values['apk_name']