gn 10.5 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
    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
    return os.path.join('out', '_'.join(target_dir))
47 48 49 50

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

55 56 57 58 59 60 61 62
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'

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

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

74 75 76 77
    runtime_mode = args.runtime_mode
    if args.dynamic and runtime_mode in ['profile', 'release']:
      runtime_mode = 'dynamic_' + runtime_mode

78 79
    gn_args = {}

80
    # Skia GN args.
81
    gn_args['skia_enable_flutter_defines'] = True # Enable Flutter API guards in Skia.
82
    gn_args['skia_use_dng_sdk'] = False    # RAW image handling.
83 84
    gn_args['skia_use_sfntly'] = False     # PDF handling depedency.
    gn_args['skia_enable_pdf'] = False     # PDF handling.
85
    gn_args['skia_use_x11'] = False        # Never add the X11 dependency (only takes effect on Linux).
86
    gn_args['skia_use_expat'] = args.target_os == 'android'
87
    gn_args['skia_use_fontconfig'] = False # Use the custom font manager instead.
88
    gn_args['is_official_build'] = True    # Disable Skia test utilities.
89

90
    gn_args['is_debug'] = args.unoptimized
91
    gn_args['android_full_debug'] = args.target_os == 'android' and args.unoptimized
92
    gn_args['is_clang'] = not sys.platform.startswith(('cygwin', 'win'))
93

94
    gn_args['embedder_for_target'] = args.embedder_for_target
95

96 97 98 99 100
    enable_lto = args.lto
    if args.unoptimized:
      # There is no point in enabling LTO in unoptimized builds.
      enable_lto = False

101
    if not sys.platform.startswith('win'):
102 103
      # The GN arg is not available in the windows toolchain.
      gn_args['enable_lto'] = enable_lto
104

105
    aot = not runtime_mode in ['debug', 'dynamic_profile', 'dynamic_release']
106
    if args.target_os == 'android':
107
        gn_args['target_os'] = 'android'
108
    elif args.target_os == 'ios':
109
        gn_args['target_os'] = 'ios'
110
        gn_args['use_ios_simulator'] = args.simulator
111
        if not args.simulator:
112
          aot = True
113
    else:
114
      aot = False
115

116 117
    gn_args['dart_lib_export_symbols'] = False

118
    if runtime_mode == 'debug':
119
        gn_args['dart_runtime_mode'] = 'develop'
120 121 122 123
    elif runtime_mode == 'dynamic_profile':
        gn_args['dart_runtime_mode'] = 'profile'
    elif runtime_mode == 'dynamic_release':
        gn_args['dart_runtime_mode'] = 'release'
124
    else:
125
        gn_args['dart_runtime_mode'] = runtime_mode
126

127 128 129
    if args.dart_debug:
        gn_args['dart_debug'] = True

130 131 132 133 134 135
    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:
136
            gn_args['target_cpu'] = args.ios_cpu
137 138
    elif args.target_os == 'linux':
      gn_args['target_cpu'] = args.linux_cpu
139 140 141
    else:
        # Building host artifacts
        gn_args['target_cpu'] = 'x64'
142

143 144 145
    # 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.
146 147 148 149 150 151 152
    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)
153 154
    if use_dbc:
      gn_args['dart_target_arch'] = 'dbc'
155 156 157 158 159 160 161 162 163
    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'])
164

165 166 167 168 169
    if args.target_os is None:
      if sys.platform.startswith(('cygwin', 'win')):
        gn_args['dart_use_fallback_root_certificates'] = True


170 171
    gn_args['dart_custom_version_for_pub'] = 'flutter'

172
    # Make sure host_cpu matches the bit width of target_cpu.
173 174
    target_is_32_bit = gn_args['target_cpu'] == 'arm' or gn_args['target_cpu'] == 'x86'
    if target_is_32_bit:
175
      gn_args["host_cpu"] = "x86"
176

177
    gn_args['flutter_runtime_mode'] = runtime_mode
178
    gn_args['flutter_aot'] = aot
179

G
George Kulakowski 已提交
180 181 182 183 184 185
    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 已提交
186 187 188 189 190 191 192 193 194 195 196 197
    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

198 199 200 201 202
    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
203
      gn_args['skia_vulkan_header'] = "flutter/vulkan/skia_vulkan_header.h"
204

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

209 210
    return gn_args

211
def parse_args(args):
E
Eric Seidel 已提交
212
  args = args[1:]
213
  parser = argparse.ArgumentParser(description='A script run` gn gen`.')
E
Eric Seidel 已提交
214

215
  parser.add_argument('--unoptimized', default=False, action='store_true')
216

217
  parser.add_argument('--runtime-mode', type=str, choices=['debug', 'profile', 'release'], default='debug')
218 219
  parser.add_argument('--dynamic', default=False, action='store_true')
  parser.add_argument('--interpreter', default=False, action='store_true')
220
  parser.add_argument('--dart-debug', default=False, action='store_true')
E
Eric Seidel 已提交
221

222
  parser.add_argument('--target-os', type=str, choices=['android', 'ios', 'linux'])
223
  parser.add_argument('--android', dest='target_os', action='store_const', const='android')
224
  parser.add_argument('--android-cpu', type=str, choices=['arm', 'x64', 'x86', 'arm64'], default='arm')
225
  parser.add_argument('--ios', dest='target_os', action='store_const', const='ios')
226
  parser.add_argument('--ios-cpu', type=str, choices=['arm', 'arm64'], default='arm64')
E
Eric Seidel 已提交
227
  parser.add_argument('--simulator', action='store_true', default=False)
228
  parser.add_argument('--linux-cpu', type=str, choices=['x64', 'x86', 'arm64'])
E
Eric Seidel 已提交
229 230 231 232

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

233 234 235
  parser.add_argument('--lto', default=True, action='store_true')
  parser.add_argument('--no-lto', dest='lto', action='store_false')

A
Adam Barth 已提交
236 237 238
  parser.add_argument('--clang', default=True, action='store_true')
  parser.add_argument('--no-clang', dest='clang', action='store_false')

G
George Kulakowski 已提交
239 240 241
  parser.add_argument('--target-sysroot', type=str)
  parser.add_argument('--toolchain-prefix', type=str)

242
  parser.add_argument('--enable-vulkan', action='store_true', default=False)
243

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

246
  return parser.parse_args(args)
247

248 249
def main(argv):
  args = parse_args(argv)
250 251 252 253

  if sys.platform.startswith(('cygwin', 'win')):
    subdir = 'win'
  elif sys.platform == 'darwin':
254
    subdir = 'mac-x64'
255
  elif sys.platform.startswith('linux'):
256
     subdir = 'linux-x64'
257 258 259 260 261 262
  else:
    raise Error('Unknown platform: ' + sys.platform)

  command = [
    '%s/buildtools/%s/gn' % (SRC_ROOT, subdir),
    'gen',
263
    '--check',
264
  ]
265 266 267 268 269

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

270 271 272 273
  if sys.platform.startswith('win'):
    # On Windows, also generate Visual Studio project for ease of editing.
    command.append('--ide=vs')

274 275
  gn_args = to_command_line(to_gn_args(args))
  out_dir = get_out_dir(args)
E
Eric Seidel 已提交
276
  print "gn gen --check in %s" % out_dir
277 278
  command.append(out_dir)
  command.append('--args=%s' % ' '.join(gn_args))
279 280 281 282 283
  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 = [
284
      'ninja',
285 286 287 288 289
      '-C',
      out_dir,
      '-t',
      'compdb',
      'cc',
290 291 292 293
      'cxx',
      'objc',
      'objcxx',
      'asm',
294 295 296 297 298 299 300 301
    ]

    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
302 303

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