[email protected] | ec069f7 | 2013-08-21 02:44:58 | [diff] [blame] | 1 | # 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 | |
| 6 | import functools |
| 7 | import logging |
| 8 | import os |
| 9 | import shlex |
| 10 | import sys |
| 11 | |
| 12 | |
| 13 | def 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() |
| 30 | def IsWindows(): |
| 31 | return sys.platform in ['win32', 'cygwin'] |
| 32 | |
| 33 | |
| 34 | @memoize() |
| 35 | def IsLinux(): |
[email protected] | d96b4b6 | 2014-02-11 03:54:20 | [diff] [blame] | 36 | return sys.platform.startswith(('linux', 'freebsd')) |
[email protected] | ec069f7 | 2013-08-21 02:44:58 | [diff] [blame] | 37 | |
| 38 | |
| 39 | @memoize() |
| 40 | def IsMac(): |
| 41 | return sys.platform == 'darwin' |
| 42 | |
| 43 | |
| 44 | @memoize() |
| 45 | def 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() |
oetuaho | 6bad0d4 | 2014-11-03 09:09:53 | [diff] [blame^] | 51 | def gyp_generator_flags(): |
| 52 | """Parses and returns GYP_GENERATOR_FLAGS env var as a dictionary.""" |
| 53 | return dict(arg.split('=', 1) |
| 54 | for arg in shlex.split(os.environ.get('GYP_GENERATOR_FLAGS', ''))) |
| 55 | |
| 56 | @memoize() |
[email protected] | ec069f7 | 2013-08-21 02:44:58 | [diff] [blame] | 57 | def gyp_msvs_version(): |
| 58 | return os.environ.get('GYP_MSVS_VERSION', '') |
| 59 | |
| 60 | @memoize() |
| 61 | def distributor(): |
| 62 | """ |
| 63 | Returns a string which is the distributed build engine in use (if any). |
| 64 | Possible values: 'goma', 'ib', '' |
| 65 | """ |
| 66 | if 'goma' in gyp_defines(): |
| 67 | return 'goma' |
| 68 | elif IsWindows(): |
| 69 | if 'CHROME_HEADLESS' in os.environ: |
| 70 | return 'ib' # use (win and !goma and headless) as approximation of ib |
| 71 | |
| 72 | |
| 73 | @memoize() |
| 74 | def platform(): |
| 75 | """ |
| 76 | Returns a string representing the platform this build is targetted for. |
| 77 | Possible values: 'win', 'mac', 'linux', 'ios', 'android' |
| 78 | """ |
| 79 | if 'OS' in gyp_defines(): |
| 80 | if 'android' in gyp_defines()['OS']: |
| 81 | return 'android' |
| 82 | else: |
| 83 | return gyp_defines()['OS'] |
| 84 | elif IsWindows(): |
| 85 | return 'win' |
| 86 | elif IsLinux(): |
| 87 | return 'linux' |
| 88 | else: |
| 89 | return 'mac' |
| 90 | |
| 91 | |
| 92 | @memoize() |
| 93 | def builder(): |
| 94 | """ |
| 95 | Returns a string representing the build engine (not compiler) to use. |
| 96 | Possible values: 'make', 'ninja', 'xcode', 'msvs', 'scons' |
| 97 | """ |
| 98 | if 'GYP_GENERATORS' in os.environ: |
| 99 | # for simplicity, only support the first explicit generator |
| 100 | generator = os.environ['GYP_GENERATORS'].split(',')[0] |
| 101 | if generator.endswith('-android'): |
| 102 | return generator.split('-')[0] |
| 103 | elif generator.endswith('-ninja'): |
| 104 | return 'ninja' |
| 105 | else: |
| 106 | return generator |
| 107 | else: |
| 108 | if platform() == 'android': |
| 109 | # Good enough for now? Do any android bots use make? |
| 110 | return 'ninja' |
| 111 | elif platform() == 'ios': |
| 112 | return 'xcode' |
| 113 | elif IsWindows(): |
[email protected] | 87518c3 | 2014-01-09 05:08:41 | [diff] [blame] | 114 | return 'ninja' |
[email protected] | ec069f7 | 2013-08-21 02:44:58 | [diff] [blame] | 115 | elif IsLinux(): |
| 116 | return 'ninja' |
| 117 | elif IsMac(): |
[email protected] | f93857b | 2013-10-30 00:04:06 | [diff] [blame] | 118 | return 'ninja' |
[email protected] | ec069f7 | 2013-08-21 02:44:58 | [diff] [blame] | 119 | else: |
| 120 | assert False, 'Don\'t know what builder we\'re using!' |