1# Copyright 2014 the V8 project 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():
36  return sys.platform.startswith(('linux', 'freebsd'))
37
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
51@memoize()
52def 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
57
58@memoize()
59def gyp_msvs_version():
60  return os.environ.get('GYP_MSVS_VERSION', '')
61
62
63@memoize()
64def distributor():
65  """
66  Returns a string which is the distributed build engine in use (if any).
67  Possible values: 'goma', 'ib', ''
68  """
69  if 'goma' in gyp_defines():
70    return 'goma'
71  elif IsWindows():
72    if 'CHROME_HEADLESS' in os.environ:
73      return 'ib' # use (win and !goma and headless) as approximation of ib
74
75
76@memoize()
77def platform():
78  """
79  Returns a string representing the platform this build is targetted for.
80  Possible values: 'win', 'mac', 'linux', 'ios', 'android'
81  """
82  if 'OS' in gyp_defines():
83    if 'android' in gyp_defines()['OS']:
84      return 'android'
85    else:
86      return gyp_defines()['OS']
87  elif IsWindows():
88    return 'win'
89  elif IsLinux():
90    return 'linux'
91  else:
92    return 'mac'
93
94
95@memoize()
96def builder():
97  """
98  Returns a string representing the build engine (not compiler) to use.
99  Possible values: 'make', 'ninja', 'xcode', 'msvs', 'scons'
100  """
101  if 'GYP_GENERATORS' in os.environ:
102    # for simplicity, only support the first explicit generator
103    generator = os.environ['GYP_GENERATORS'].split(',')[0]
104    if generator.endswith('-android'):
105      return generator.split('-')[0]
106    elif generator.endswith('-ninja'):
107      return 'ninja'
108    else:
109      return generator
110  else:
111    if platform() == 'android':
112      # Good enough for now? Do any android bots use make?
113      return 'make'
114    elif platform() == 'ios':
115      return 'xcode'
116    elif IsWindows():
117      return 'msvs'
118    elif IsLinux():
119      return 'make'
120    elif IsMac():
121      return 'xcode'
122    else:
123      assert False, 'Don\'t know what builder we\'re using!'
124