gn 9.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
#!/usr/bin/env python
# Copyright 2015 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.

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 19
    if args.target_os is not None:
        target_dir = [args.target_os]
    else:
        target_dir = ['host']

    target_dir.append(args.runtime_mode)
20 21

    if args.simulator:
22
        target_dir.append('sim')
23

24 25
    if args.unoptimized:
        target_dir.append('unopt')
26

27
    if args.android_cpu != 'arm':
28
        target_dir.append(args.android_cpu)
29

30 31 32
    if args.ios_cpu != 'arm64':
        target_dir.append(args.ios_cpu)

33 34 35
    if args.enable_vulkan:
        target_dir.append('vulkan')

36
    return os.path.join('out', '_'.join(target_dir))
37 38 39 40

def to_command_line(gn_args):
    def merge(key, value):
        if type(value) is bool:
41 42
            return '%s=%s' % (key, 'true' if value else 'false')
        return '%s="%s"' % (key, value)
43 44
    return [merge(x, y) for x, y in gn_args.iteritems()]

45 46 47 48 49 50 51 52
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'

53
def to_gn_args(args):
54 55 56 57
    if args.simulator:
        if args.target_os == 'android':
            raise Exception('--simulator is not supported on Android')
        elif args.target_os == 'ios':
58 59
            if args.runtime_mode != 'debug':
                raise Exception('iOS simulator only supports the debug runtime mode')
60

61 62 63
    if args.target_os != 'android' and args.enable_vulkan:
      raise Exception('--enable-vulkan is only supported on Android')

64 65
    gn_args = {}

66
    # Skia GN args.
67
    gn_args['skia_enable_flutter_defines'] = True # Enable Flutter API guards in Skia.
68 69 70 71
    gn_args['skia_use_dng_sdk'] = False    # RAW image handling.
    gn_args['skia_use_sfntly'] = False     # PDF handling.
    gn_args['skia_use_libwebp'] = False    # Needs third_party/libwebp.
    gn_args['skia_use_fontconfig'] = False # Use the custom font manager instead.
72
    gn_args['is_official_build'] = True    # Disable Skia test utilities.
73

74
    gn_args['is_debug'] = args.unoptimized
75
    gn_args['android_full_debug'] = args.target_os == 'android' and args.unoptimized
76
    gn_args['is_clang'] = not sys.platform.startswith(('cygwin', 'win'))
77

78 79 80 81 82
    enable_lto = args.lto
    if args.unoptimized:
      # There is no point in enabling LTO in unoptimized builds.
      enable_lto = False

83
    if args.target_os != 'win':
84 85
      # The GN arg is not available in the windows toolchain.
      gn_args['enable_lto'] = enable_lto
86

87
    aot = args.runtime_mode != 'debug'
88
    if args.target_os == 'android':
89
        gn_args['target_os'] = 'android'
90
    elif args.target_os == 'ios':
91
        gn_args['target_os'] = 'ios'
92
        gn_args['use_ios_simulator'] = args.simulator
93
        if not args.simulator:
94
          aot = True
95
    else:
96
      aot = False
97

98 99 100 101 102
    if args.runtime_mode == 'debug':
        gn_args['dart_runtime_mode'] = 'develop'
    else:
        gn_args['dart_runtime_mode'] = args.runtime_mode

103 104 105 106 107 108
    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:
109
            gn_args['target_cpu'] = args.ios_cpu
110 111 112
    else:
        # Building host artifacts
        gn_args['target_cpu'] = 'x64'
113

114 115 116 117
    # 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.
    use_dbc = args.target_os == 'ios' and not args.simulator and args.runtime_mode == 'debug'
118 119
    if use_dbc:
      gn_args['dart_target_arch'] = 'dbc'
120 121 122 123 124 125 126 127 128
    else:
      gn_args['dart_target_arch'] = gn_args['target_cpu']

    # No cross-compilation on Windows (for now).
    if sys.platform.startswith(('cygwin', 'win')):
      if 'target_os' in gn_args:
        gn_args['target_os'] = 'win'
      if 'target_cpu' in gn_args:
        gn_args['target_cpu'] = cpu_for_target_arch(gn_args['target_cpu'])
129

130
    # Modify host_toolchain into dart_host_toolchain so it matches word size of target_cpu
131 132
    target_is_32_bit = gn_args['target_cpu'] == 'arm' or gn_args['target_cpu'] == 'x86'
    if target_is_32_bit:
133 134 135 136 137 138
      if sys.platform.startswith('linux'):
        gn_args['dart_host_toolchain'] = "//build/toolchain/linux:clang_x86"
      elif sys.platform.startswith('darwin'):
        gn_args['dart_host_toolchain'] = "//build/toolchain/mac:clang_i386"
      elif sys.platform.startswith('win'):
        gn_args['dart_host_toolchain'] = "//build/toolchain/win:x86"
139 140 141 142 143 144 145
    else:
      if sys.platform.startswith('linux'):
        gn_args['dart_host_toolchain'] = "//build/toolchain/linux:clang_x64"
      elif sys.platform.startswith('darwin'):
        gn_args['dart_host_toolchain'] = "//build/toolchain/mac:clang_x64"
      elif sys.platform.startswith('win'):
        gn_args['dart_host_toolchain'] = "//build/toolchain/win:x64"
146

147
    gn_args['flutter_runtime_mode'] = args.runtime_mode
148
    gn_args['flutter_aot'] = aot
149

G
George Kulakowski 已提交
150 151 152 153 154 155
    if args.target_sysroot:
      gn_args['target_sysroot'] = args.target_sysroot

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

E
Eric Seidel 已提交
156 157 158 159 160 161 162 163 164 165 166 167
    goma_dir = os.environ.get('GOMA_DIR')
    goma_home_dir = os.path.join(os.getenv('HOME', ''), 'goma')
    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

168 169 170 171 172
    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
173
      gn_args['skia_vulkan_headers'] = "//third_party/vulkan/src"
174

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

179 180
    return gn_args

181
def parse_args(args):
E
Eric Seidel 已提交
182
  args = args[1:]
183
  parser = argparse.ArgumentParser(description='A script run` gn gen`.')
E
Eric Seidel 已提交
184

185
  parser.add_argument('--unoptimized', default=False, action='store_true')
186

187
  parser.add_argument('--runtime-mode', type=str, choices=['debug', 'profile', 'release'], default='debug')
E
Eric Seidel 已提交
188

189
  parser.add_argument('--target-os', type=str, choices=['android', 'ios'])
190
  parser.add_argument('--android', dest='target_os', action='store_const', const='android')
191
  parser.add_argument('--android-cpu', type=str, choices=['arm', 'x64', 'x86', 'arm64'], default='arm')
192
  parser.add_argument('--ios', dest='target_os', action='store_const', const='ios')
193
  parser.add_argument('--ios-cpu', type=str, choices=['arm', 'arm64'], default='arm64')
E
Eric Seidel 已提交
194
  parser.add_argument('--simulator', action='store_true', default=False)
E
Eric Seidel 已提交
195 196 197 198

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

199 200 201
  parser.add_argument('--lto', default=True, action='store_true')
  parser.add_argument('--no-lto', dest='lto', action='store_false')

A
Adam Barth 已提交
202 203 204
  parser.add_argument('--clang', default=True, action='store_true')
  parser.add_argument('--no-clang', dest='clang', action='store_false')

G
George Kulakowski 已提交
205 206 207
  parser.add_argument('--target-sysroot', type=str)
  parser.add_argument('--toolchain-prefix', type=str)

208
  parser.add_argument('--use-glfw', action='store_true', default=False)
209
  parser.add_argument('--enable-vulkan', action='store_true', default=False)
210

211
  return parser.parse_args(args)
212

213 214
def main(argv):
  args = parse_args(argv)
215 216 217 218 219 220 221 222 223 224 225 226 227

  if sys.platform.startswith(('cygwin', 'win')):
    subdir = 'win'
  elif sys.platform == 'darwin':
    subdir = 'mac'
  elif sys.platform.startswith('linux'):
     subdir = 'linux64'
  else:
    raise Error('Unknown platform: ' + sys.platform)

  command = [
    '%s/buildtools/%s/gn' % (SRC_ROOT, subdir),
    'gen',
228
    '--check',
229
  ]
230 231 232 233 234

  if sys.platform == 'darwin':
    # On the Mac, also generate Xcode projects for ease of editing.
    command.append('--ide=xcode')

235 236
  gn_args = to_command_line(to_gn_args(args))
  out_dir = get_out_dir(args)
E
Eric Seidel 已提交
237
  print "gn gen --check in %s" % out_dir
238 239
  command.append(out_dir)
  command.append('--args=%s' % ' '.join(gn_args))
240 241 242 243 244
  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 = [
245
      'ninja',
246 247 248 249 250
      '-C',
      out_dir,
      '-t',
      'compdb',
      'cc',
251 252 253 254
      'cxx',
      'objc',
      'objcxx',
      'asm',
255 256 257 258 259 260 261 262
    ]

    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
263 264

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