gn 5.9 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
    return os.path.join('out', '_'.join(target_dir))
31 32 33 34

def to_command_line(gn_args):
    def merge(key, value):
        if type(value) is bool:
35 36
            return '%s=%s' % (key, 'true' if value else 'false')
        return '%s="%s"' % (key, value)
37 38 39
    return [merge(x, y) for x, y in gn_args.iteritems()]

def to_gn_args(args):
40 41 42 43
    if args.simulator:
        if args.target_os == 'android':
            raise Exception('--simulator is not supported on Android')
        elif args.target_os == 'ios':
44 45
            if args.runtime_mode != 'debug':
                raise Exception('iOS simulator only supports the debug runtime mode')
46

47 48
    gn_args = {}

49
    gn_args['is_debug'] = args.unoptimized
50
    gn_args['is_clang'] = True
51

52 53
    ios_target_cpu = 'arm64'

54
    aot = args.runtime_mode != 'debug'
55
    if args.target_os == 'android':
56
        gn_args['target_os'] = 'android'
57
    elif args.target_os == 'ios':
58 59
        gn_args['target_os'] = 'ios'
        gn_args['ios_deployment_target'] = '7.0'
60
        gn_args['use_ios_simulator'] = args.simulator
J
Jason Simmons 已提交
61
        if args.simulator:
62
          gn_args['use_libjpeg_turbo'] = False
J
Jason Simmons 已提交
63
        else:
64
          # Always use AOT on iOS devices until the interpreter stabilizes
65
          aot = True
66
    else:
67 68
      gn_args['use_system_harfbuzz'] = False
      aot = False
69

70 71 72 73 74
    if args.runtime_mode == 'debug':
        gn_args['dart_runtime_mode'] = 'develop'
    else:
        gn_args['dart_runtime_mode'] = args.runtime_mode

75 76 77 78 79 80 81
    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:
            gn_args['target_cpu'] = 'arm64'
E
Eric Seidel 已提交
82

83
    gn_args['flutter_aot'] = aot
84
    if aot:
85
        gn_args['dart_target_arch'] = gn_args['target_cpu']
86

87 88 89 90 91
    # 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'
    gn_args['dart_experimental_interpreter'] = use_dbc
92

93
    gn_args['flutter_runtime_mode'] = args.runtime_mode
94

G
George Kulakowski 已提交
95 96 97 98 99 100
    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 已提交
101 102 103 104 105 106 107 108 109 110 111 112
    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

113
    gn_args['use_glfw'] = args.use_glfw
114

115 116
    return gn_args

117
def parse_args(args):
E
Eric Seidel 已提交
118
  args = args[1:]
119
  parser = argparse.ArgumentParser(description='A script run` gn gen`.')
E
Eric Seidel 已提交
120

121
  parser.add_argument('--unoptimized', default=False, action='store_true')
122

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

125
  parser.add_argument('--target-os', type=str, choices=['android', 'ios'])
126
  parser.add_argument('--android', dest='target_os', action='store_const', const='android')
127
  parser.add_argument('--android-cpu', type=str, choices=['arm', 'x64', 'x86'], default='arm')
128
  parser.add_argument('--ios', dest='target_os', action='store_const', const='ios')
E
Eric Seidel 已提交
129
  parser.add_argument('--simulator', action='store_true', default=False)
E
Eric Seidel 已提交
130 131 132 133

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

A
Adam Barth 已提交
134 135 136
  parser.add_argument('--clang', default=True, action='store_true')
  parser.add_argument('--no-clang', dest='clang', action='store_false')

G
George Kulakowski 已提交
137 138 139
  parser.add_argument('--target-sysroot', type=str)
  parser.add_argument('--toolchain-prefix', type=str)

140
  parser.add_argument('--use-glfw', action='store_true', default=False)
141

142
  return parser.parse_args(args)
143

144 145
def main(argv):
  args = parse_args(argv)
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160

  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',
    '--check'
  ]
161 162
  gn_args = to_command_line(to_gn_args(args))
  out_dir = get_out_dir(args)
E
Eric Seidel 已提交
163
  print "gn gen --check in %s" % out_dir
164 165
  command.append(out_dir)
  command.append('--args=%s' % ' '.join(gn_args))
166 167 168 169 170 171 172 173 174 175 176
  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 = [
      '%s/buildtools/%s/ninja' % (SRC_ROOT, subdir),
      '-C',
      out_dir,
      '-t',
      'compdb',
      'cc',
177 178 179 180
      'cxx',
      'objc',
      'objcxx',
      'asm',
181 182 183 184 185 186 187 188
    ]

    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
189 190

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