[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(): |
krytarowski | ec291e214 | 2016-08-09 19:36:24 | [diff] [blame] | 36 | return sys.platform.startswith(('linux', 'freebsd', 'netbsd', 'openbsd')) |
[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 | |
thakis | 08e9878 | 2016-07-30 00:35:24 | [diff] [blame] | 50 | |
[email protected] | ec069f7 | 2013-08-21 02:44:58 | [diff] [blame] | 51 | @memoize() |
oetuaho | 6bad0d4 | 2014-11-03 09:09:53 | [diff] [blame] | 52 | def gyp_generator_flags(): |
| 53 | """Parses and returns GYP_GENERATOR_FLAGS env var as a dictionary.""" |
| 54 | return dict(arg.split('=', 1) |
| 55 | for arg in shlex.split(os.environ.get('GYP_GENERATOR_FLAGS', ''))) |
| 56 | |
thakis | 08e9878 | 2016-07-30 00:35:24 | [diff] [blame] | 57 | |
oetuaho | 6bad0d4 | 2014-11-03 09:09:53 | [diff] [blame] | 58 | @memoize() |
[email protected] | ec069f7 | 2013-08-21 02:44:58 | [diff] [blame] | 59 | def gyp_msvs_version(): |
| 60 | return os.environ.get('GYP_MSVS_VERSION', '') |
| 61 | |
thakis | 08e9878 | 2016-07-30 00:35:24 | [diff] [blame] | 62 | |
[email protected] | ec069f7 | 2013-08-21 02:44:58 | [diff] [blame] | 63 | @memoize() |
| 64 | def distributor(): |
| 65 | """ |
| 66 | Returns a string which is the distributed build engine in use (if any). |
thakis | 08e9878 | 2016-07-30 00:35:24 | [diff] [blame] | 67 | Possible values: 'goma', None |
[email protected] | ec069f7 | 2013-08-21 02:44:58 | [diff] [blame] | 68 | """ |
| 69 | if 'goma' in gyp_defines(): |
| 70 | return 'goma' |
[email protected] | ec069f7 | 2013-08-21 02:44:58 | [diff] [blame] | 71 | |
| 72 | |
| 73 | @memoize() |
| 74 | def platform(): |
| 75 | """ |
Julien Brianceau | 96dfe4d8 | 2017-08-01 09:03:13 | [diff] [blame] | 76 | Returns a string representing the platform this build is targeted for. |
[email protected] | ec069f7 | 2013-08-21 02:44:58 | [diff] [blame] | 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' |