blob: 773783291f25397eea5a97c6a8e984a71743b13f [file] [log] [blame]
sivachandra@chromium.org54da9682013-08-21 11:44:58 +09001# Copyright 2013 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.
4
5
6import functools
7import logging
8import os
9import shlex
10import sys
11
12
13def memoize(default=None):
14 """This decorator caches the return value of a parameterless pure function"""
15 def memoizer(func):
16 val = []
17 @functools.wraps(func)
18 def inner():
19 if not val:
20 ret = func()
21 val.append(ret if ret is not None else default)
22 if logging.getLogger().isEnabledFor(logging.INFO):
23 print '%s -> %r' % (func.__name__, val[0])
24 return val[0]
25 return inner
26 return memoizer
27
28
29@memoize()
30def IsWindows():
31 return sys.platform in ['win32', 'cygwin']
32
33
34@memoize()
35def IsLinux():
r.c.ladan@gmail.com8fa51fa2014-02-11 12:54:20 +090036 return sys.platform.startswith(('linux', 'freebsd'))
sivachandra@chromium.org54da9682013-08-21 11:44:58 +090037
38
39@memoize()
40def IsMac():
41 return sys.platform == 'darwin'
42
43
44@memoize()
45def gyp_defines():
46 """Parses and returns GYP_DEFINES env var as a dictionary."""
47 return dict(arg.split('=', 1)
48 for arg in shlex.split(os.environ.get('GYP_DEFINES', '')))
49
50@memoize()
51def gyp_msvs_version():
52 return os.environ.get('GYP_MSVS_VERSION', '')
53
54@memoize()
55def distributor():
56 """
57 Returns a string which is the distributed build engine in use (if any).
58 Possible values: 'goma', 'ib', ''
59 """
60 if 'goma' in gyp_defines():
61 return 'goma'
62 elif IsWindows():
63 if 'CHROME_HEADLESS' in os.environ:
64 return 'ib' # use (win and !goma and headless) as approximation of ib
65
66
67@memoize()
68def platform():
69 """
70 Returns a string representing the platform this build is targetted for.
71 Possible values: 'win', 'mac', 'linux', 'ios', 'android'
72 """
73 if 'OS' in gyp_defines():
74 if 'android' in gyp_defines()['OS']:
75 return 'android'
76 else:
77 return gyp_defines()['OS']
78 elif IsWindows():
79 return 'win'
80 elif IsLinux():
81 return 'linux'
82 else:
83 return 'mac'
84
85
86@memoize()
87def builder():
88 """
89 Returns a string representing the build engine (not compiler) to use.
90 Possible values: 'make', 'ninja', 'xcode', 'msvs', 'scons'
91 """
92 if 'GYP_GENERATORS' in os.environ:
93 # for simplicity, only support the first explicit generator
94 generator = os.environ['GYP_GENERATORS'].split(',')[0]
95 if generator.endswith('-android'):
96 return generator.split('-')[0]
97 elif generator.endswith('-ninja'):
98 return 'ninja'
99 else:
100 return generator
101 else:
102 if platform() == 'android':
103 # Good enough for now? Do any android bots use make?
104 return 'ninja'
105 elif platform() == 'ios':
106 return 'xcode'
107 elif IsWindows():
scottmg@chromium.orgc4862052014-01-09 14:08:41 +0900108 return 'ninja'
sivachandra@chromium.org54da9682013-08-21 11:44:58 +0900109 elif IsLinux():
110 return 'ninja'
111 elif IsMac():
thakis@chromium.orgcc961c22013-10-30 09:04:06 +0900112 return 'ninja'
sivachandra@chromium.org54da9682013-08-21 11:44:58 +0900113 else:
114 assert False, 'Don\'t know what builder we\'re using!'