summaryrefslogtreecommitdiffstats
path: root/site_scons/site_tools/chromium_builders.py
blob: 80f89edda030a80e11ec6f8514c0b01c33ecf3fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""
Tool module for adding, to a construction environment, Chromium-specific
wrappers around SCons builders.  This gives us a central place for any
customization we need to make to the different things we build.
"""

import sys

from SCons.Script import *

import SCons.Node
import SCons.Util

class Null(object):
  def __new__(cls, *args, **kwargs):
    if '_inst' not in vars(cls):
      cls._inst = super(type, cls).__new__(cls, *args, **kwargs)
    return cls._inst
  def __init__(self, *args, **kwargs): pass
  def __call__(self, *args, **kwargs): return self
  def __repr__(self): return "Null()"
  def __nonzero__(self): return False
  def __getattr__(self, name): return self
  def __setattr__(self, name, val): return self
  def __delattr__(self, name): return self
  def __getitem__(self, name): return self

class FileList(object):
  def __init__(self, entries=None):
    if isinstance(entries, FileList):
      entries = entries.entries
    self.entries = entries or []
  def __getitem__(self, i):
    return self.entries[i]
  def __setitem__(self, i, item):
    self.entries[i] = item
  def __delitem__(self, i):
    del self.entries[i]
  def __add__(self, other):
    if isinstance(other, FileList):
      return self.__class__(self.entries + other.entries)
    elif isinstance(other, type(self.entries)):
      return self.__class__(self.entries + other)
    else:
      return self.__class__(self.entries + list(other))
  def __radd__(self, other):
    if isinstance(other, FileList):
      return self.__class__(other.entries + self.entries)
    elif isinstance(other, type(self.entries)):
      return self.__class__(other + self.entries)
    else:
      return self.__class__(list(other) + self.entries)
  def __iadd__(self, other):
    if isinstance(other, FileList):
      self.entries += other.entries
    elif isinstance(other, type(self.entries)):
      self.entries += other
    else:
      self.entries += list(other)
    return self
  def append(self, item):
    return self.entries.append(item)
  def extend(self, item):
    return self.entries.extend(item)
  def index(self, item, *args):
    return self.entries.index(item, *args)
  def remove(self, item):
    return self.entries.remove(item)

def FileListWalk(top, topdown=True, onerror=None):
  """
  """
  try:
    entries = top.entries
  except AttributeError, err:
    if onerror is not None:
      onerror(err)
    return

  dirs, nondirs = [], []
  for entry in entries:
    if hasattr(entry, 'entries'):
      dirs.append(entry)
    else:
      nondirs.append(entry)

  if topdown:
    yield top, dirs, nondirs
  for entry in dirs:
    for x in FileListWalk(entry, topdown, onerror):
      yield x
  if not topdown:
    yield top, dirs, nondirs

class ChromeFileList(FileList):
  def Append(self, *args):
    for element in args:
      self.append(element)
  def Extend(self, *args):
    for element in args:
      self.extend(element)
  def Remove(self, *args):
    for top, lists, nonlists in FileListWalk(self, topdown=False):
      for element in args:
        try:
          top.remove(element)
        except ValueError:
          pass
  def Replace(self, old, new):
    for top, lists, nonlists in FileListWalk(self, topdown=False):
      try:
        i = top.index(old)
      except ValueError:
        pass
      else:
        if SCons.Util.is_List(new):
          top[i:i+1] = new
        else:
          top[i] = new


def FilterOut(self, **kw):
  """Removes values from existing construction variables in an Environment.

  The values to remove should be a list.  For example:

  self.FilterOut(CPPDEFINES=['REMOVE_ME', 'ME_TOO'])

  Args:
    self: Environment to alter.
    kw: (Any other named arguments are values to remove).
  """

  kw = SCons.Environment.copy_non_reserved_keywords(kw)
  for key, val in kw.items():
    envval = self.get(key, None)
    if envval is None:
      # No existing variable in the environment, so nothing to delete.
      continue

    for vremove in val:
      # Use while not if, so we can handle duplicates.
      while vremove in envval:
        envval.remove(vremove)

    self[key] = envval

    # TODO(sgk): SCons.Environment.Append() has much more logic to deal
    # with various types of values.  We should handle all those cases in here
    # too.  (If variable is a dict, etc.)


import __builtin__
__builtin__.ChromeFileList = ChromeFileList

non_compilable_suffixes = {
    'LINUX' : set([
        '.bdic',
        '.css',
        '.dat',
        '.gperf',
        '.h',
        '.html',
        '.hxx',
        '.idl',
        '.js',
        '.rc',
    ]),
    'WINDOWS' : set([
        '.h',
        '.dat',
        '.idl',
    ]),
}

def compilable(env, file):
  base, ext = os.path.splitext(str(file))
  if ext in non_compilable_suffixes[env['TARGET_PLATFORM']]:
    return False
  return True

def compilable_files(env, sources):
  if not hasattr(sources, 'entries'):
    return [x for x in sources if compilable(env, x)]
  result = []
  for top, folders, nonfolders in FileListWalk(sources):
    result.extend([x for x in nonfolders if compilable(env, x)])
  return result

def ChromeProgram(env, target, source, *args, **kw):
  source = compilable_files(env, source)
  result = env.Program('$TOP_BUILDDIR/' + str(target), source, *args, **kw)
  if env.get('INCREMENTAL'):
    env.Precious(result)
  return result

def ChromeTestProgram(env, target, source, *args, **kw):
  source = compilable_files(env, source)
  result = env.Program('$TOP_BUILDDIR/' + str(target), source, *args, **kw)
  if env.get('INCREMENTAL'):
    env.Precious(*result)
  return result

def ChromeLibrary(env, target, source, *args, **kw):
  source = compilable_files(env, source)
  result = env.Library('$LIB_DIR/' + str(target), source, *args, **kw)
  return result

def ChromeLoadableModule(env, target, source, *args, **kw):
  source = compilable_files(env, source)
  if env.get('_GYP'):
    result = env.LoadableModule(target, source, *args, **kw)
  else:
    kw['COMPONENT_STATIC'] = True
    result = env.LoadableModule(target, source, *args, **kw)
  return result

def ChromeStaticLibrary(env, target, source, *args, **kw):
  source = compilable_files(env, source)
  if env.get('_GYP'):
    result = env.StaticLibrary('$LIB_DIR/' + str(target), source, *args, **kw)
  else:
    kw['COMPONENT_STATIC'] = True
    result = env.ComponentLibrary(target, source, *args, **kw)
  return result

def ChromeSharedLibrary(env, target, source, *args, **kw):
  source = compilable_files(env, source)
  if env.get('_GYP'):
    result = env.SharedLibrary('$LIB_DIR/' + str(target), source, *args, **kw)
  else:
    kw['COMPONENT_STATIC'] = False
    result = [env.ComponentLibrary(target, source, *args, **kw)[0]]
  if env.get('INCREMENTAL'):
    env.Precious(result)
  return result

def ChromeObject(env, *args, **kw):
  if env.get('_GYP'):
    result = env.Object(target, source, *args, **kw)
  else:
    result = env.ComponentObject(*args, **kw)
  return result

def generate(env):
  env.AddMethod(ChromeProgram)
  env.AddMethod(ChromeTestProgram)
  env.AddMethod(ChromeLibrary)
  env.AddMethod(ChromeLoadableModule)
  env.AddMethod(ChromeStaticLibrary)
  env.AddMethod(ChromeSharedLibrary)
  env.AddMethod(ChromeObject)

  env.AddMethod(FilterOut)

  # Add the grit tool to the base environment because we use this a lot.
  sys.path.append(env.Dir('$SRC_DIR/tools/grit').abspath)
  env.Tool('scons', toolpath=[env.Dir('$SRC_DIR/tools/grit/grit')])

  # Add the repack python script tool that we use in multiple places.
  sys.path.append(env.Dir('$SRC_DIR/tools/data_pack').abspath)
  env.Tool('scons', toolpath=[env.Dir('$SRC_DIR/tools/data_pack/')])

def exists(env):
  return True