summaryrefslogtreecommitdiffstats
path: root/tools/auto_bisect/builder.py
blob: 97b01f7d82f71533b76f34756a9839d4c5f1ea43 (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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# Copyright 2014 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.

"""Classes and functions for building Chrome.

This includes functions for running commands to build, as well as
specific rules about which targets to build.
"""

import os
import subprocess
import sys

import bisect_utils

ORIGINAL_ENV = {}


class Builder(object):
  """Subclasses of the Builder class are used by the bisect script to build
  relevant targets.
  """
  def __init__(self, opts):
    """Performs setup for building with target build system.

    Args:
      opts: Options parsed from command line.

    Raises:
      RuntimeError: Some condition necessary for building was not met.
    """
    if bisect_utils.IsWindowsHost():
      if not opts.build_preference:
        opts.build_preference = 'msvs'

      if opts.build_preference == 'msvs':
        if not os.getenv('VS100COMNTOOLS'):
          raise RuntimeError(
              'Path to visual studio could not be determined.')
      else:
        # Need to re-escape goma dir, see crbug.com/394990.
        if opts.goma_dir:
          opts.goma_dir = opts.goma_dir.encode('string_escape')
        SetBuildSystemDefault(opts.build_preference, opts.use_goma,
                              opts.goma_dir, opts.target_arch)
    else:
      if not opts.build_preference:
        if 'ninja' in os.getenv('GYP_GENERATORS', default=''):
          opts.build_preference = 'ninja'
        else:
          opts.build_preference = 'make'

      SetBuildSystemDefault(opts.build_preference, opts.use_goma, opts.goma_dir)

    if not SetupPlatformBuildEnvironment(opts):
      raise RuntimeError('Failed to set platform environment.')

  @staticmethod
  def FromOpts(opts):
    """Constructs and returns a Builder object.

    Args:
      opts: Options parsed from the command-line.
    """
    builder = None
    if opts.target_platform == 'android':
      builder = AndroidBuilder(opts)
    elif opts.target_platform == 'android-chrome':
      builder = AndroidChromeBuilder(opts)
    else:
      builder = DesktopBuilder(opts)
    return builder

  def Build(self, depot, opts):
    """Runs a command to build Chrome."""
    raise NotImplementedError()


def GetBuildOutputDirectory(opts, src_dir=None):
  """Returns the path to the build directory, relative to the checkout root.

  Assumes that the current working directory is the checkout root.

  Args:
    opts: Command-line options.
    src_dir: Path to chromium/src directory.

  Returns:
    A path to the directory to use as build output directory.

  Raises:
    NotImplementedError: The platform according to sys.platform is unexpected.
  """
  src_dir = src_dir or 'src'
  if opts.build_preference == 'ninja' or bisect_utils.IsLinuxHost():
    return os.path.join(src_dir, 'out')
  if bisect_utils.IsMacHost():
    return os.path.join(src_dir, 'xcodebuild')
  if bisect_utils.IsWindowsHost():
    return os.path.join(src_dir, 'build')
  raise NotImplementedError('Unexpected platform %s' % sys.platform)


class DesktopBuilder(Builder):
  """DesktopBuilder is used to build Chromium on Linux, Mac, or Windows."""

  def __init__(self, opts):
    super(DesktopBuilder, self).__init__(opts)

  def Build(self, depot, opts):
    """Builds chromium_builder_perf target using options passed into the script.

    Args:
      depot: Name of current depot being bisected.
      opts: The options parsed from the command line.

    Returns:
      True if build was successful.
    """
    targets = ['chromium_builder_perf']

    threads = None
    if opts.use_goma:
      threads = opts.goma_threads

    build_success = False
    if opts.build_preference == 'make':
      build_success = BuildWithMake(threads, targets, opts.target_build_type)
    elif opts.build_preference == 'ninja':
      build_success = BuildWithNinja(threads, targets, opts.target_build_type)
    elif opts.build_preference == 'msvs':
      assert bisect_utils.IsWindowsHost(), 'msvs is only supported on Windows.'
      build_success = BuildWithVisualStudio(targets, opts.target_build_type)
    else:
      assert False, 'No build system defined.'
    return build_success


class AndroidBuilder(Builder):
  """AndroidBuilder is used to build on android."""

  def __init__(self, opts):
    super(AndroidBuilder, self).__init__(opts)

  # TODO(qyearsley): Make this a class method and verify that it works with
  # a unit test.
  # TODO (prasadv): Remove android-chrome-shell target once we confirm there are
  # no pending bisect jobs with this in command.
  # pylint: disable=R0201
  def _GetTargets(self):
    """Returns a list of build targets."""
    return [
        'chrome_public_apk',
        'chrome_shell_apk',
        'cc_perftests_apk',
        'android_tools'
    ]

  def Build(self, depot, opts):
    """Builds the android content shell and other necessary tools.

    Args:
        depot: Current depot being bisected.
        opts: The options parsed from the command line.

    Returns:
        True if build was successful.
    """
    threads = None
    if opts.use_goma:
      threads = opts.goma_threads

    build_success = False
    if opts.build_preference == 'ninja':
      build_success = BuildWithNinja(
          threads, self._GetTargets(), opts.target_build_type)
    else:
      assert False, 'No build system defined.'

    return build_success


class AndroidChromeBuilder(AndroidBuilder):
  """AndroidChromeBuilder is used to build "android-chrome".

  This is slightly different from AndroidBuilder.
  """

  def __init__(self, opts):
    super(AndroidChromeBuilder, self).__init__(opts)

  # TODO(qyearsley): Make this a class method and verify that it works with
  # a unit test.
  # pylint: disable=R0201
  def _GetTargets(self):
    """Returns a list of build targets."""
    return AndroidBuilder._GetTargets(self) + ['chrome_apk']


def SetBuildSystemDefault(build_system, use_goma, goma_dir, target_arch='ia32'):
  """Sets up any environment variables needed to build with the specified build
  system.

  Args:
    build_system: A string specifying build system. Currently only 'ninja' or
        'make' are supported.
    use_goma: Determines whether to GOMA for compile.
    goma_dir: GOMA directory path.
    target_arch: The target build architecture, ia32 or x64. Default is ia32.
  """
  if build_system == 'ninja':
    gyp_var = os.getenv('GYP_GENERATORS', default='')

    if not gyp_var or not 'ninja' in gyp_var:
      if gyp_var:
        os.environ['GYP_GENERATORS'] = gyp_var + ',ninja'
      else:
        os.environ['GYP_GENERATORS'] = 'ninja'

      if bisect_utils.IsWindowsHost():
        os.environ['GYP_DEFINES'] = 'component=shared_library '\
            'incremental_chrome_dll=1 disable_nacl=1 fastbuild=1 '\
            'chromium_win_pch=0'

  elif build_system == 'make':
    os.environ['GYP_GENERATORS'] = 'make'
  else:
    raise RuntimeError('%s build not supported.' % build_system)

  if use_goma:
    os.environ['GYP_DEFINES'] = '%s %s' % (os.getenv('GYP_DEFINES', default=''),
                                           'use_goma=1')
    if goma_dir:
      os.environ['GYP_DEFINES'] += ' gomadir=%s' % goma_dir

  # Produce 64 bit chromium binaries when target architecure is set to x64.
  if target_arch == 'x64':
    os.environ['GYP_DEFINES'] += ' target_arch=%s' % target_arch

def SetupPlatformBuildEnvironment(opts):
  """Performs any platform-specific setup.

  Args:
    opts: The options parsed from the command line through parse_args().

  Returns:
    True if successful.
  """
  if 'android' in opts.target_platform:
    CopyAndSaveOriginalEnvironmentVars()
    return SetupAndroidBuildEnvironment(opts)
  return True


def BuildWithMake(threads, targets, build_type='Release'):
  """Runs a make command with the given targets.

  Args:
    threads: The number of threads to use. None means unspecified/unlimited.
    targets: List of make targets.
    build_type: Release or Debug.

  Returns:
    True if the command had a 0 exit code, False otherwise.
  """
  cmd = ['make', 'BUILDTYPE=%s' % build_type]
  if threads:
    cmd.append('-j%d' % threads)
  cmd += targets
  return_code = bisect_utils.RunProcess(cmd)
  return not return_code


def BuildWithNinja(threads, targets, build_type='Release'):
  """Runs a ninja command with the given targets."""
  cmd = ['ninja', '-C', os.path.join('out', build_type)]
  if threads:
    cmd.append('-j%d' % threads)
  cmd += targets
  return_code = bisect_utils.RunProcess(cmd)
  return not return_code


def BuildWithVisualStudio(targets, build_type='Release'):
  """Runs a command to build the given targets with Visual Studio."""
  path_to_devenv = os.path.abspath(
      os.path.join(os.environ['VS100COMNTOOLS'], '..', 'IDE', 'devenv.com'))
  path_to_sln = os.path.join(os.getcwd(), 'chrome', 'chrome.sln')
  cmd = [path_to_devenv, '/build', build_type, path_to_sln]
  for t in targets:
    cmd.extend(['/Project', t])
  return_code = bisect_utils.RunProcess(cmd)
  return not return_code


def CopyAndSaveOriginalEnvironmentVars():
  """Makes a copy of the current environment variables.

  Before making a copy of the environment variables and setting a global
  variable, this function unsets a certain set of environment variables.
  """
  # TODO: Waiting on crbug.com/255689, will remove this after.
  vars_to_remove = [
      'CHROME_SRC',
      'CHROMIUM_GYP_FILE',
      'GYP_DEFINES',
      'GYP_GENERATORS',
      'GYP_GENERATOR_FLAGS',
      'OBJCOPY',
  ]
  for key in os.environ:
    if 'ANDROID' in key:
      vars_to_remove.append(key)
  for key in vars_to_remove:
    if os.environ.has_key(key):
      del os.environ[key]

  global ORIGINAL_ENV
  ORIGINAL_ENV = os.environ.copy()


def SetupAndroidBuildEnvironment(opts, path_to_src=None):
  """Sets up the android build environment.

  Args:
    opts: The options parsed from the command line through parse_args().
    path_to_src: Path to the src checkout.

  Returns:
    True if successful.
  """
  # Revert the environment variables back to default before setting them up
  # with envsetup.sh.
  env_vars = os.environ.copy()
  for k, _ in env_vars.iteritems():
    del os.environ[k]
  for k, v in ORIGINAL_ENV.iteritems():
    os.environ[k] = v

  envsetup_path = os.path.join('build', 'android', 'envsetup.sh')
  proc = subprocess.Popen(['bash', '-c', 'source %s && env' % envsetup_path],
                          stdout=subprocess.PIPE,
                          stderr=subprocess.PIPE,
                          cwd=path_to_src)
  out, _ = proc.communicate()

  for line in out.splitlines():
    k, _, v = line.partition('=')
    os.environ[k] = v

  # envsetup.sh no longer sets OS=android in GYP_DEFINES environment variable.
  # (See http://crrev.com/170273005). So, we set this variable explicitly here
  # in order to build Chrome on Android.
  if 'GYP_DEFINES' not in os.environ:
    os.environ['GYP_DEFINES'] = 'OS=android'
  else:
    os.environ['GYP_DEFINES'] += ' OS=android'

  if opts.use_goma:
    os.environ['GYP_DEFINES'] += ' use_goma=1'
  return not proc.returncode