gn 17.0 KB
Newer Older
1
#!/usr/bin/env python
M
Michael Goderbauer 已提交
2
# Copyright 2013 The Flutter Authors. All rights reserved.
3 4 5 6 7 8 9 10
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import argparse
import subprocess
import sys
import os

11
SRC_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
12

13
def get_out_dir(args):
14 15 16 17 18
    if args.target_os is not None:
        target_dir = [args.target_os]
    else:
        target_dir = ['host']

19
    runtime_mode = args.runtime_mode
20

21
    target_dir.append(args.runtime_mode)
22 23

    if args.simulator:
24
        target_dir.append('sim')
25

26 27
    if args.unoptimized:
        target_dir.append('unopt')
28

29 30 31
    if args.target_os != 'ios' and args.interpreter:
        target_dir.append('interpreter')

32
    if args.android_cpu != 'arm':
33
        target_dir.append(args.android_cpu)
34

35 36 37
    if args.ios_cpu != 'arm64':
        target_dir.append(args.ios_cpu)

38 39 40
    if args.linux_cpu is not None:
      target_dir.append(args.linux_cpu)

41 42 43
    if args.windows_cpu != 'x64':
      target_dir.append(args.windows_cpu)

44 45 46
    if args.target_os == 'fuchsia' and args.fuchsia_cpu is not None:
      target_dir.append(args.fuchsia_cpu)

47 48 49
    if args.enable_vulkan:
        target_dir.append('vulkan')

50
    return os.path.join(args.out_dir, 'out', '_'.join(target_dir))
51 52 53 54

def to_command_line(gn_args):
    def merge(key, value):
        if type(value) is bool:
55 56
            return '%s=%s' % (key, 'true' if value else 'false')
        return '%s="%s"' % (key, value)
57 58
    return [merge(x, y) for x, y in gn_args.iteritems()]

59 60 61 62 63 64 65 66
def cpu_for_target_arch(arch):
  if arch in ['ia32', 'arm', 'armv6', 'armv5te', 'mips',
              'simarm', 'simarmv6', 'simarmv5te', 'simmips', 'simdbc',
              'armsimdbc']:
    return 'x86'
  if arch in ['x64', 'arm64', 'simarm64', 'simdbc64', 'armsimdbc64']:
    return 'x64'

67
def to_gn_args(args):
68 69 70 71
    if args.simulator:
        if args.target_os == 'android':
            raise Exception('--simulator is not supported on Android')

72 73 74
    if args.target_os != 'android' and args.enable_vulkan:
      raise Exception('--enable-vulkan is only supported on Android')

75 76
    runtime_mode = args.runtime_mode

77 78
    gn_args = {}

D
Dan Field 已提交
79 80 81
    if args.bitcode:
      if args.target_os != 'ios':
        raise Exception('Bitcode is only supported for iOS')
D
Dan Field 已提交
82
      if runtime_mode != 'release':
83
        gn_args['bitcode_marker'] = True
D
Dan Field 已提交
84 85 86

    gn_args['enable_bitcode'] = args.bitcode

87
    # Skia GN args.
88
    gn_args['skia_enable_flutter_defines'] = True # Enable Flutter API guards in Skia.
89
    gn_args['skia_use_dng_sdk'] = False    # RAW image handling.
90
    gn_args['skia_use_sfntly'] = False     # PDF handling dependency.
91
    gn_args['skia_enable_pdf'] = False     # PDF handling.
92
    gn_args['skia_use_x11'] = False        # Never add the X11 dependency (only takes effect on Linux).
93
    gn_args['skia_use_wuffs'] = True
94
    gn_args['skia_use_expat'] = args.target_os == 'android'
95 96
    gn_args['skia_use_fontconfig'] = args.enable_fontconfig
    gn_args['flutter_use_fontconfig'] = args.enable_fontconfig
97
    gn_args['flutter_enable_skshaper'] = args.enable_skshaper
98 99
    if args.enable_skshaper:
      gn_args['skia_use_icu'] = True
100
    gn_args['is_official_build'] = True    # Disable Skia test utilities.
101
    gn_args['dart_component_kind'] = 'static_library' # Always link Dart in statically.
102
    gn_args['is_debug'] = args.unoptimized
103
    gn_args['android_full_debug'] = args.target_os == 'android' and args.unoptimized
D
Dan Field 已提交
104
    if args.clang is None:
105
      gn_args['is_clang'] = True
D
Dan Field 已提交
106 107
    else:
      gn_args['is_clang'] = args.clang
108

109 110 111 112 113 114
    if args.target_os == 'android' or args.target_os == 'ios':
      gn_args['skia_gl_standard'] = 'gles'
    else:
      # We explicitly don't want to pick GL because we run GLES tests using SwiftShader.
      gn_args['skia_gl_standard'] = ''

115 116 117
    if not sys.platform.startswith(('cygwin', 'win')):
      gn_args['use_clang_static_analyzer'] = args.clang_static_analyzer

118
    gn_args['embedder_for_target'] = args.embedder_for_target
119

120 121
    gn_args['enable_coverage'] = args.coverage

122 123 124
    if args.operator_new_alignment is not None:
      gn_args['operator_new_alignment'] = args.operator_new_alignment

125 126 127 128 129
    enable_lto = args.lto
    if args.unoptimized:
      # There is no point in enabling LTO in unoptimized builds.
      enable_lto = False

130
    if not sys.platform.startswith('win'):
131 132
      # The GN arg is not available in the windows toolchain.
      gn_args['enable_lto'] = enable_lto
133

134
    if args.target_os == 'android':
135
        gn_args['target_os'] = 'android'
136
    elif args.target_os == 'ios':
137
        gn_args['target_os'] = 'ios'
138
        gn_args['use_ios_simulator'] = args.simulator
139 140 141
    elif args.target_os == 'fuchsia':
        gn_args['target_os'] = 'fuchsia'
        gn_args['flutter_enable_legacy_fuchsia_embedder'] = args.fuchsia_legacy
142 143
    elif args.target_os is not None:
        gn_args['target_os'] = args.target_os
144

145 146
    gn_args['dart_lib_export_symbols'] = False

147
    if runtime_mode == 'debug':
148
        gn_args['dart_runtime_mode'] = 'develop'
149 150
    elif runtime_mode == 'jit_release':
        gn_args['dart_runtime_mode'] = 'release';
151
    else:
152
        gn_args['dart_runtime_mode'] = runtime_mode
153

154 155 156
    if args.dart_debug:
        gn_args['dart_debug'] = True

157 158 159 160
    if args.full_dart_debug:
      gn_args['dart_debug'] = True
      gn_args['dart_debug_optimization_level'] = '0'

161 162 163 164 165 166
    if args.target_os == 'android':
        gn_args['target_cpu'] = args.android_cpu
    elif args.target_os == 'ios':
        if args.simulator:
            gn_args['target_cpu'] = 'x64'
        else:
167
            gn_args['target_cpu'] = args.ios_cpu
168 169
    elif args.target_os == 'linux':
      gn_args['target_cpu'] = args.linux_cpu
170 171
    elif args.target_os == 'fuchsia':
      gn_args['target_cpu'] = args.fuchsia_cpu
172 173
    elif args.target_os == 'win':
      gn_args['target_cpu'] = args.windows_cpu
174 175 176
    else:
        # Building host artifacts
        gn_args['target_cpu'] = 'x64'
177

178 179 180 181
    # DBC is not supported anymore.
    if args.interpreter:
      raise Exception('--interpreter is no longer needed on any supported platform.')
    gn_args['dart_target_arch'] = gn_args['target_cpu']
182

183
    if sys.platform.startswith(('cygwin', 'win')) and args.target_os != 'win':
184 185
      if 'target_cpu' in gn_args:
        gn_args['target_cpu'] = cpu_for_target_arch(gn_args['target_cpu'])
186

187 188 189 190 191
    if args.target_os is None:
      if sys.platform.startswith(('cygwin', 'win')):
        gn_args['dart_use_fallback_root_certificates'] = True


192 193
    gn_args['dart_custom_version_for_pub'] = 'flutter'

194 195 196
    # Make sure host_cpu matches the bit width of target_cpu on x86.
    if gn_args['target_cpu'] == 'x86':
      gn_args['host_cpu'] = 'x86'
197

198
    gn_args['flutter_runtime_mode'] = runtime_mode
199

G
George Kulakowski 已提交
200 201
    if args.target_sysroot:
      gn_args['target_sysroot'] = args.target_sysroot
202 203 204 205 206 207 208
      gn_args['custom_sysroot'] = args.target_sysroot

    if args.target_toolchain:
      gn_args['custom_toolchain'] = args.target_toolchain

    if args.target_triple:
      gn_args['custom_target_triple'] = args.target_triple
G
George Kulakowski 已提交
209

E
Eric Seidel 已提交
210 211
    goma_dir = os.environ.get('GOMA_DIR')
    goma_home_dir = os.path.join(os.getenv('HOME', ''), 'goma')
212 213 214 215 216

    # GOMA has a different default (home) path on gWindows.
    if not os.path.exists(goma_home_dir) and sys.platform.startswith(('cygwin', 'win')):
      goma_home_dir = os.path.join('c:\\', 'src', 'goma', 'goma-win64')

E
Eric Seidel 已提交
217 218 219 220 221 222 223 224 225 226
    if args.goma and goma_dir:
      gn_args['use_goma'] = True
      gn_args['goma_dir'] = goma_dir
    elif args.goma and os.path.exists(goma_home_dir):
      gn_args['use_goma'] = True
      gn_args['goma_dir'] = goma_home_dir
    else:
      gn_args['use_goma'] = False
      gn_args['goma_dir'] = None

227 228
    # Enable Metal on non-simulator iOS builds.
    if args.target_os == 'ios' and not args.simulator:
229 230
      gn_args['skia_use_metal'] = True
      gn_args['shell_enable_metal'] = True
231 232 233 234
      # Bitcode enabled builds using the current version of the toolchain leak
      # C++ symbols decorated with the availability attribute. Disable these
      # attributes in release modes till the toolchain is updated.
      gn_args['skia_enable_api_available_macro'] = args.runtime_mode != "release"
235

236 237 238 239 240 241
    if args.enable_vulkan:
      # Enable vulkan in the Flutter shell.
      gn_args['shell_enable_vulkan'] = True
      # Configure Skia for Vulkan support.
      gn_args['skia_use_vulkan'] = True

242 243 244 245 246
    # The buildroot currently isn't set up to support Vulkan in the
    # Windows ANGLE build, so disable it regardless of enable_vulkan's value.
    if sys.platform.startswith(('cygwin', 'win')):
       gn_args['angle_enable_vulkan'] = False

247 248
    # We should not need a special case for x86, but this seems to introduce text relocations
    # even with -fPIC everywhere.
249
    # gn_args['enable_profiling'] = args.runtime_mode != 'release' and args.android_cpu != 'x86'
250

251 252 253
    if args.arm_float_abi:
      gn_args['arm_float_abi'] = args.arm_float_abi

254 255
    # Whether to build trained Dart SDK snapshots of dart2js and dartdevc,
    # including the web sdk kernel and source files.
256 257 258
    if args.target_os is None:
      # dart_platform_sdk is not declared for Android targets.
      gn_args['dart_platform_sdk'] = not args.full_dart_sdk
259
    gn_args['full_dart_sdk'] = args.full_dart_sdk
260

261 262 263 264 265 266
    # Desktop embeddings can have more dependencies than the engine library,
    # which can be problematic in some build environments (e.g., building on
    # Linux will bring in pkg-config dependencies at generation time). These
    # flags allow preventing those those targets from being part of the build
    # tree.
    gn_args['enable_desktop_embeddings'] = not args.disable_desktop_embeddings
267 268
    if args.build_glfw_shell is not None:
      gn_args['build_glfw_shell'] = args.build_glfw_shell
269

270 271
    gn_args['stripped_symbols'] = args.stripped

272 273 274 275 276 277 278 279 280 281 282 283
    if args.msan:
      gn_args['is_msan'] = True

    if args.asan:
      gn_args['is_asan'] = True

    if args.tsan:
      gn_args['is_tsan'] = True

    if args.lsan:
      gn_args['is_lsan'] = True

284 285 286
    if args.ubsan:
      gn_args['is_ubsan'] = True

287 288 289 290 291 292
    if args.enable_vulkan_validation_layers:
      if args.target_os is not 'fuchsia':
        print('Vulkan validation layers are currently only supported on Fuchsia targets.')
        sys.exit(1)
      gn_args['enable_vulkan_validation_layers'] = True

293 294
    return gn_args

295
def parse_args(args):
E
Eric Seidel 已提交
296
  args = args[1:]
297
  parser = argparse.ArgumentParser(description='A script run` gn gen`.')
E
Eric Seidel 已提交
298

299
  parser.add_argument('--unoptimized', default=False, action='store_true')
300

301
  parser.add_argument('--runtime-mode', type=str, choices=['debug', 'profile', 'release', 'jit_release'], default='debug')
302
  parser.add_argument('--interpreter', default=False, action='store_true')
303
  parser.add_argument('--dart-debug', default=False, action='store_true', help='Enables assertions in the Dart VM. ' +
K
KyleWong 已提交
304
      'Does not affect optimization levels. If you need to disable optimizations in Dart, use --full-dart-debug')
305 306
  parser.add_argument('--full-dart-debug', default=False, action='store_true', help='Implies --dart-debug ' +
      'and also disables optimizations in the Dart VM making it easier to step through VM code in the debugger.')
E
Eric Seidel 已提交
307

308
  parser.add_argument('--target-os', type=str, choices=['android', 'ios', 'linux', 'fuchsia', 'win'])
309
  parser.add_argument('--android', dest='target_os', action='store_const', const='android')
310
  parser.add_argument('--android-cpu', type=str, choices=['arm', 'x64', 'x86', 'arm64'], default='arm')
311
  parser.add_argument('--ios', dest='target_os', action='store_const', const='ios')
312
  parser.add_argument('--ios-cpu', type=str, choices=['arm', 'arm64'], default='arm64')
E
Eric Seidel 已提交
313
  parser.add_argument('--simulator', action='store_true', default=False)
314
  parser.add_argument('--fuchsia', dest='target_os', action='store_const', const='fuchsia')
315 316 317
  parser.add_argument('--fuchsia-legacy', default=True, action='store_true')
  parser.add_argument('--no-fuchsia-legacy', dest='fuchsia_legacy', action='store_false')

318
  parser.add_argument('--linux-cpu', type=str, choices=['x64', 'x86', 'arm64', 'arm'])
319
  parser.add_argument('--fuchsia-cpu', type=str, choices=['x64', 'arm64'], default = 'x64')
320
  parser.add_argument('--windows-cpu', type=str, choices=['x64', 'arm64'], default = 'x64')
321
  parser.add_argument('--arm-float-abi', type=str, choices=['hard', 'soft', 'softfp'])
E
Eric Seidel 已提交
322 323 324 325

  parser.add_argument('--goma', default=True, action='store_true')
  parser.add_argument('--no-goma', dest='goma', action='store_false')

326 327 328
  parser.add_argument('--lto', default=True, action='store_true')
  parser.add_argument('--no-lto', dest='lto', action='store_false')

D
Dan Field 已提交
329 330
  parser.add_argument('--clang', action='store_const', const=True)
  parser.add_argument('--no-clang', dest='clang', action='store_const', const=False)
A
Adam Barth 已提交
331

332 333 334
  parser.add_argument('--clang-static-analyzer', default=False, action='store_true')
  parser.add_argument('--no-clang-static-analyzer', dest='clang_static_analyzer', action='store_false')

G
George Kulakowski 已提交
335
  parser.add_argument('--target-sysroot', type=str)
336 337
  parser.add_argument('--target-toolchain', type=str)
  parser.add_argument('--target-triple', type=str)
338
  parser.add_argument('--operator-new-alignment', dest='operator_new_alignment', type=str, default=None)
G
George Kulakowski 已提交
339

340
  parser.add_argument('--enable-vulkan', action='store_true', default=False)
341

342
  parser.add_argument('--enable-fontconfig', action='store_true', default=False)
343
  parser.add_argument('--enable-skshaper', action='store_true', default=False)
344
  parser.add_argument('--enable-vulkan-validation-layers', action='store_true', default=False)
345

346 347
  parser.add_argument('--embedder-for-target', dest='embedder_for_target', action='store_true', default=False)

348 349
  parser.add_argument('--coverage', default=False, action='store_true')

350 351
  parser.add_argument('--out-dir', default='', type=str)

352 353 354
  parser.add_argument('--full-dart-sdk', default=False, action='store_true',
                      help='include trained dart2js and dartdevc snapshots. Enable only on steps that create an SDK')
  parser.add_argument('--no-full-dart-sdk', dest='full_dart_sdk', action='store_false')
355

356 357 358 359
  parser.add_argument('--ide', default='', type=str,
                      help='The IDE files to generate using GN. Use `gn gen help` and look for the --ide flag to' +
                      ' see supported IDEs. If this flag is not specified, a platform specific default is selected.')

360 361
  parser.add_argument('--disable-desktop-embeddings', default=False, action='store_true',
                      help='Do not include desktop embeddings in the build.')
362 363 364 365
  parser.add_argument('--build-glfw-shell', action='store_const', const=True,
                      help='Build the GLFW shell on supported platforms where it is not built by default.')
  parser.add_argument('--no-build-glfw-shell', dest='build_glfw_shell', action='store_const', const=False,
                      help='Do not build the GLFW shell on platforms where it is built by default.')
366

D
Dan Field 已提交
367
  parser.add_argument('--bitcode', default=False, action='store_true',
368
                      help='Enable bitcode for iOS targets. On debug runtime modes, this will be a marker only.')
D
Dan Field 已提交
369

370 371 372 373
  parser.add_argument('--stripped', default=True, action='store_true',
                      help='Strip debug symbols from the output. This defaults to true and has no effect on iOS.')
  parser.add_argument('--no-stripped', dest='stripped', action='store_false')

374 375 376 377 378
  # Sanitizers.
  parser.add_argument('--asan', default=False, action='store_true')
  parser.add_argument('--lsan', default=False, action='store_true')
  parser.add_argument('--msan', default=False, action='store_true')
  parser.add_argument('--tsan', default=False, action='store_true')
379
  parser.add_argument('--ubsan', default=False, action='store_true')
380

381
  return parser.parse_args(args)
382

383 384
def main(argv):
  args = parse_args(argv)
385

D
Dan Field 已提交
386
  exe = '.exe' if sys.platform.startswith(('cygwin', 'win')) else ''
387 388

  command = [
D
Dan Field 已提交
389
    '%s/flutter/third_party/gn/gn%s' % (SRC_ROOT, exe),
390
    'gen',
391
    '--check',
392
  ]
393

394 395 396 397
  if args.ide != '':
    command.append('--ide=%s' % args.ide)
  elif sys.platform == 'darwin':
    # On the Mac, generate an Xcode project by default.
398
    command.append('--ide=xcode')
399 400
  elif sys.platform.startswith('win'):
    # On Windows, generate a Visual Studio project.
401 402
    command.append('--ide=vs')

403 404 405 406
  gn_args = to_command_line(to_gn_args(args))
  out_dir = get_out_dir(args)
  command.append(out_dir)
  command.append('--args=%s' % ' '.join(gn_args))
407
  print "Generating GN files in: %s" % out_dir
408 409 410 411 412
  try:
    gn_call_result = subprocess.call(command, cwd=SRC_ROOT)
  except subprocess.CalledProcessError as exc:
    print "Failed to generate gn files: ", exc.returncode, exc.output
    sys.exit(1)
413 414 415 416

  if gn_call_result == 0:
    # Generate/Replace the compile commands database in out.
    compile_cmd_gen_cmd = [
417
      'ninja',
418 419 420 421 422
      '-C',
      out_dir,
      '-t',
      'compdb',
      'cc',
423 424 425 426
      'cxx',
      'objc',
      'objcxx',
      'asm',
427 428
    ]

K
Kaushik Iska 已提交
429 430 431 432 433
    try:
      contents = subprocess.check_output(compile_cmd_gen_cmd, cwd=SRC_ROOT)
    except subprocess.CalledProcessError as exc:
      print "Failed to run ninja: ", exc.returncode, exc.output
      sys.exit(1)
434 435 436 437 438
    compile_commands = open('%s/out/compile_commands.json' % SRC_ROOT, 'w+')
    compile_commands.write(contents)
    compile_commands.close()

  return gn_call_result
439 440

if __name__ == '__main__':
441
    sys.exit(main(sys.argv))