blob: 9cc75efefe273943e2e991b84b17f3a498d08e31 [file] [log] [blame]
[email protected]ec069f72013-08-21 02:44:581# 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():
krytarowskiec291e2142016-08-09 19:36:2436 return sys.platform.startswith(('linux', 'freebsd', 'netbsd', 'openbsd'))
[email protected]ec069f72013-08-21 02:44:5837
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
thakis08e98782016-07-30 00:35:2450
[email protected]ec069f72013-08-21 02:44:5851@memoize()
oetuaho6bad0d42014-11-03 09:09:5352def 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
thakis08e98782016-07-30 00:35:2457
oetuaho6bad0d42014-11-03 09:09:5358@memoize()
[email protected]ec069f72013-08-21 02:44:5859def gyp_msvs_version():
60 return os.environ.get('GYP_MSVS_VERSION', '')
61
thakis08e98782016-07-30 00:35:2462
[email protected]ec069f72013-08-21 02:44:5863@memoize()
64def distributor():
65 """
66 Returns a string which is the distributed build engine in use (if any).
thakis08e98782016-07-30 00:35:2467 Possible values: 'goma', None
[email protected]ec069f72013-08-21 02:44:5868 """
69 if 'goma' in gyp_defines():
70 return 'goma'
[email protected]ec069f72013-08-21 02:44:5871
72
73@memoize()
74def platform():
75 """
Julien Brianceau96dfe4d82017-08-01 09:03:1376 Returns a string representing the platform this build is targeted for.
[email protected]ec069f72013-08-21 02:44:5877 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'