gn 14.8 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 20
    runtime_mode = args.runtime_mode
    if args.dynamic and runtime_mode in ['profile', 'release']:
21 22
        target_dir.append('dynamic')

23
    target_dir.append(args.runtime_mode)
24 25

    if args.simulator:
26
        target_dir.append('sim')
27

28 29
    if args.unoptimized:
        target_dir.append('unopt')
30

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

34
    if args.android_cpu != 'arm':
35
        target_dir.append(args.android_cpu)
36

37 38 39
    if args.ios_cpu != 'arm64':
        target_dir.append(args.ios_cpu)

40 41 42
    if args.linux_cpu is not None:
      target_dir.append(args.linux_cpu)

43 44 45
    if args.enable_vulkan:
        target_dir.append('vulkan')

46 47 48
    if args.enable_metal:
      target_dir.append('metal')

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

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

58 59 60 61 62 63 64 65
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'

66
def to_gn_args(args):
67 68 69 70
    if args.simulator:
        if args.target_os == 'android':
            raise Exception('--simulator is not supported on Android')
        elif args.target_os == 'ios':
71 72
            if args.runtime_mode != 'debug':
                raise Exception('iOS simulator only supports the debug runtime mode')
73

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

77 78 79
    if args.target_os != 'ios' and args.enable_metal:
      raise Exception('--enable-metal is only supported on iOS')

80 81 82 83
    runtime_mode = args.runtime_mode
    if args.dynamic and runtime_mode in ['profile', 'release']:
      runtime_mode = 'dynamic_' + runtime_mode

84 85
    gn_args = {}

86
    # Skia GN args.
87
    gn_args['skia_enable_flutter_defines'] = True # Enable Flutter API guards in Skia.
88
    gn_args['skia_use_dng_sdk'] = False    # RAW image handling.
89
    gn_args['skia_use_sfntly'] = False     # PDF handling dependency.
90
    gn_args['skia_enable_pdf'] = False     # PDF handling.
91
    gn_args['skia_use_x11'] = False        # Never add the X11 dependency (only takes effect on Linux).
92
    gn_args['skia_use_expat'] = args.target_os == 'android'
93 94
    gn_args['skia_use_fontconfig'] = args.enable_fontconfig
    gn_args['flutter_use_fontconfig'] = args.enable_fontconfig
95
    gn_args['is_official_build'] = True    # Disable Skia test utilities.
96
    gn_args['dart_component_kind'] = 'static_library' # Always link Dart in statically.
97
    gn_args['is_debug'] = args.unoptimized
98
    gn_args['android_full_debug'] = args.target_os == 'android' and args.unoptimized
99
    gn_args['is_clang'] = not sys.platform.startswith(('cygwin', 'win'))
100

101 102 103 104 105 106
    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'] = ''

107 108 109
    if not sys.platform.startswith(('cygwin', 'win')):
      gn_args['use_clang_static_analyzer'] = args.clang_static_analyzer

110
    gn_args['embedder_for_target'] = args.embedder_for_target
111

112 113
    gn_args['enable_coverage'] = args.coverage

114 115 116
    if args.operator_new_alignment is not None:
      gn_args['operator_new_alignment'] = args.operator_new_alignment

117 118 119 120 121
    enable_lto = args.lto
    if args.unoptimized:
      # There is no point in enabling LTO in unoptimized builds.
      enable_lto = False

122
    if not sys.platform.startswith('win'):
123 124
      # The GN arg is not available in the windows toolchain.
      gn_args['enable_lto'] = enable_lto
125

126
    if args.target_os == 'android':
127
        gn_args['target_os'] = 'android'
128
    elif args.target_os == 'ios':
129
        gn_args['target_os'] = 'ios'
130
        gn_args['use_ios_simulator'] = args.simulator
131 132
    elif args.target_os is not None:
        gn_args['target_os'] = args.target_os
133

134 135
    gn_args['dart_lib_export_symbols'] = False

136
    if runtime_mode == 'debug':
137
        gn_args['dart_runtime_mode'] = 'develop'
138 139 140 141
    elif runtime_mode == 'dynamic_profile':
        gn_args['dart_runtime_mode'] = 'profile'
    elif runtime_mode == 'dynamic_release':
        gn_args['dart_runtime_mode'] = 'release'
142
    else:
143
        gn_args['dart_runtime_mode'] = runtime_mode
144

145 146 147
    if args.dart_debug:
        gn_args['dart_debug'] = True

148 149 150 151
    if args.full_dart_debug:
      gn_args['dart_debug'] = True
      gn_args['dart_debug_optimization_level'] = '0'

152 153 154 155 156 157
    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:
158
            gn_args['target_cpu'] = args.ios_cpu
159 160
    elif args.target_os == 'linux':
      gn_args['target_cpu'] = args.linux_cpu
161 162 163
    else:
        # Building host artifacts
        gn_args['target_cpu'] = 'x64'
164

165 166 167
    # On iOS Devices, use the Dart bytecode interpreter so we don't incur
    # snapshotting and linking costs of the precompiler during development.
    # We can still use the JIT on the simulator though.
168 169 170 171 172 173 174
    can_use_dbc = runtime_mode in ['debug', 'dynamic_profile', 'dynamic_release']
    use_dbc = args.target_os == 'ios' and not args.simulator and can_use_dbc
    # Use dbc if it is requested on the command line and supported by the
    # requested runtime mode.
    if args.interpreter and not can_use_dbc:
      raise Exception('--interpreter not supported with --runtime-mode=' + runtime_mode)
    use_dbc = use_dbc or (args.interpreter and can_use_dbc)
175 176
    if use_dbc:
      gn_args['dart_target_arch'] = 'dbc'
177 178 179 180 181 182
    else:
      gn_args['dart_target_arch'] = gn_args['target_cpu']

    if sys.platform.startswith(('cygwin', 'win')):
      if 'target_cpu' in gn_args:
        gn_args['target_cpu'] = cpu_for_target_arch(gn_args['target_cpu'])
183

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


189 190
    gn_args['dart_custom_version_for_pub'] = 'flutter'

191
    # Make sure host_cpu matches the bit width of target_cpu.
192 193
    target_is_32_bit = gn_args['target_cpu'] == 'arm' or gn_args['target_cpu'] == 'x86'
    if target_is_32_bit:
194
      gn_args["host_cpu"] = "x86"
195

196
    gn_args['flutter_runtime_mode'] = runtime_mode
197

G
George Kulakowski 已提交
198 199
    if args.target_sysroot:
      gn_args['target_sysroot'] = args.target_sysroot
200 201 202 203 204 205 206
      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 已提交
207 208 209 210

    if args.toolchain_prefix:
      gn_args['toolchain_prefix'] = args.toolchain_prefix

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

    # 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 已提交
218 219 220 221 222 223 224 225 226 227
    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

228 229 230 231
    if args.enable_metal:
      gn_args['skia_use_metal'] = True
      gn_args['shell_enable_metal'] = True
      gn_args['ios_deployment_target'] = '11'
232
      gn_args['allow_deprecated_api_calls'] = True
233

234 235 236 237 238 239
    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

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

244 245 246
    if args.arm_float_abi:
      gn_args['arm_float_abi'] = args.arm_float_abi

247 248
    # Whether to build trained Dart SDK snapshots of dart2js and dartdevc,
    # including the web sdk kernel and source files.
249 250 251
    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
252
    gn_args['full_dart_sdk'] = args.full_dart_sdk
253

254 255
    if sys.platform == 'darwin':
      gn_args['mac_sdk_path'] = args.mac_sdk_path
256 257
      if args.build_glfw_shell:
        gn_args['build_glfw_shell'] = True
258 259
      if gn_args['mac_sdk_path'] == '':
        gn_args['mac_sdk_path'] = os.getenv('FLUTTER_MAC_SDK_PATH', '')
260

261 262
    return gn_args

263
def parse_args(args):
E
Eric Seidel 已提交
264
  args = args[1:]
265
  parser = argparse.ArgumentParser(description='A script run` gn gen`.')
E
Eric Seidel 已提交
266

267
  parser.add_argument('--unoptimized', default=False, action='store_true')
268

269
  parser.add_argument('--runtime-mode', type=str, choices=['debug', 'profile', 'release'], default='debug')
270 271
  parser.add_argument('--dynamic', default=False, action='store_true')
  parser.add_argument('--interpreter', default=False, action='store_true')
272
  parser.add_argument('--dart-debug', default=False, action='store_true', help='Enables assertsion in the Dart VM. ' +
K
KyleWong 已提交
273
      'Does not affect optimization levels. If you need to disable optimizations in Dart, use --full-dart-debug')
274 275
  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 已提交
276

277
  parser.add_argument('--target-os', type=str, choices=['android', 'ios', 'linux', 'fuchsia'])
278
  parser.add_argument('--android', dest='target_os', action='store_const', const='android')
279
  parser.add_argument('--android-cpu', type=str, choices=['arm', 'x64', 'x86', 'arm64'], default='arm')
280
  parser.add_argument('--ios', dest='target_os', action='store_const', const='ios')
281
  parser.add_argument('--ios-cpu', type=str, choices=['arm', 'arm64'], default='arm64')
E
Eric Seidel 已提交
282
  parser.add_argument('--simulator', action='store_true', default=False)
283
  parser.add_argument('--fuchsia', dest='target_os', action='store_const', const='fuchsia')
284 285
  parser.add_argument('--linux-cpu', type=str, choices=['x64', 'x86', 'arm64', 'arm'])
  parser.add_argument('--arm-float-abi', type=str, choices=['hard', 'soft', 'softfp'])
E
Eric Seidel 已提交
286 287 288 289

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

290 291 292
  parser.add_argument('--lto', default=True, action='store_true')
  parser.add_argument('--no-lto', dest='lto', action='store_false')

A
Adam Barth 已提交
293 294 295
  parser.add_argument('--clang', default=True, action='store_true')
  parser.add_argument('--no-clang', dest='clang', action='store_false')

296 297 298
  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 已提交
299
  parser.add_argument('--target-sysroot', type=str)
300 301
  parser.add_argument('--target-toolchain', type=str)
  parser.add_argument('--target-triple', type=str)
G
George Kulakowski 已提交
302
  parser.add_argument('--toolchain-prefix', type=str)
303
  parser.add_argument('--operator-new-alignment', dest='operator_new_alignment', type=str, default=None)
G
George Kulakowski 已提交
304

305
  parser.add_argument('--enable-vulkan', action='store_true', default=False)
306
  parser.add_argument('--enable-metal', action='store_true', default=False)
307

308 309
  parser.add_argument('--enable-fontconfig', action='store_true', default=False)

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

312 313
  parser.add_argument('--coverage', default=False, action='store_true')

314 315
  parser.add_argument('--out-dir', default='', type=str)

316 317 318
  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')
319

320
  parser.add_argument('--mac-sdk-path', default='', type=str,
321 322
                      help='On the mac, the SDK is inferred from the Xcode location unless this flag is specified. ' +
                      ' Setting the FLUTTER_MAC_SDK_PATH environment variable achieves the same effect.')
323

324 325 326 327
  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.')

328 329 330
  parser.add_argument('--build-glfw-shell', dest='build_glfw_shell', default=False, action='store_true',
                      help='Force building the GLFW shell on desktop platforms where it is not built by default.')

331
  return parser.parse_args(args)
332

333 334
def main(argv):
  args = parse_args(argv)
335 336 337 338

  if sys.platform.startswith(('cygwin', 'win')):
    subdir = 'win'
  elif sys.platform == 'darwin':
339
    subdir = 'mac-x64'
340
  elif sys.platform.startswith('linux'):
341
     subdir = 'linux-x64'
342 343 344 345 346 347
  else:
    raise Error('Unknown platform: ' + sys.platform)

  command = [
    '%s/buildtools/%s/gn' % (SRC_ROOT, subdir),
    'gen',
348
    '--check',
349
  ]
350

351 352 353 354
  if args.ide != '':
    command.append('--ide=%s' % args.ide)
  elif sys.platform == 'darwin':
    # On the Mac, generate an Xcode project by default.
355
    command.append('--ide=xcode')
356 357
  elif sys.platform.startswith('win'):
    # On Windows, generate a Visual Studio project.
358 359
    command.append('--ide=vs')

360 361 362 363
  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))
364
  print "Generating GN files in: %s" % out_dir
365 366 367 368 369
  gn_call_result = subprocess.call(command, cwd=SRC_ROOT)

  if gn_call_result == 0:
    # Generate/Replace the compile commands database in out.
    compile_cmd_gen_cmd = [
370
      'ninja',
371 372 373 374 375
      '-C',
      out_dir,
      '-t',
      'compdb',
      'cc',
376 377 378 379
      'cxx',
      'objc',
      'objcxx',
      'asm',
380 381 382 383 384 385 386 387
    ]

    contents = subprocess.check_output(compile_cmd_gen_cmd, cwd=SRC_ROOT)
    compile_commands = open('%s/out/compile_commands.json' % SRC_ROOT, 'w+')
    compile_commands.write(contents)
    compile_commands.close()

  return gn_call_result
388 389

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