#!/usr/bin/env vpython3
# Copyright 2013 The Flutter Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import argparse
import subprocess
import sys
import os
import platform

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


def get_out_dir(args):
  if args.target_os is not None:
    target_dir = [args.target_os]
  elif args.web:
    target_dir = ['wasm']
  else:
    target_dir = ['host']

  target_dir.append(args.runtime_mode)

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

  if args.unoptimized:
    target_dir.append('unopt')

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

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

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

  if args.mac_cpu != 'x64':
    target_dir.append(args.mac_cpu)

  if args.simulator_cpu != 'x64':
    target_dir.append(args.simulator_cpu)

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

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

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

  # This exists for backwards compatibility of tests that are being run
  # on LUCI. This can be removed in coordination with a LUCI change:
  # https://github.com/flutter/flutter/issues/76547
  if args.macos_enable_metal:
    target_dir.append('metal')

  if args.target_dir != '':
    target_dir = [args.target_dir]

  if args.darwin_extension_safe:
    target_dir.append('extension_safe')

  return os.path.join(args.out_dir, 'out', '_'.join(target_dir))


def to_command_line(gn_args):
  """Converts the arguments dictionary to a list of command-line arguments.

    Args:
      gn_args: GN arguments dictionary generated by to_gn_args().
    """

  def merge(key, value):
    if isinstance(value, bool):
      return '%s=%s' % (key, 'true' if value else 'false')
    if isinstance(value, int):
      return '%s=%d' % (key, value)
    return '%s="%s"' % (key, value)

  return [merge(x, y) for x, y in gn_args.items()]


def is_host_build(args):
  # If target_os == None, then this is a host build.
  if args.target_os is None:
    return True
  # For linux arm64 builds, we cross compile from x64 hosts, so the
  # target_os='linux' and linux-cpu='arm64'
  if args.target_os == 'linux' and args.linux_cpu == 'arm64':
    return True
  # The Mac and host targets are redundant. Again, necessary to disambiguate
  # during cross-compilation.
  if args.target_os == 'mac':
    return True
  return False


# Determines whether a prebuilt Dart SDK can be used instead of building one.
# We can use a prebuilt Dart SDK when:
# 1. It is a host build, a build targeting Fuchsia, or a build targeting desktop.
# 2. The prebuilt SDK exists under //flutter/prebuilts/$OS-$ARCH.
def can_use_prebuilt_dart(args):
  prebuilt = None
  # In a Fuchsia build, we can use a prebuilt Dart SDK for the host to build
  # platform agnostic artifacts (e.g. kernel snapshots), and a Dart SDK
  # targeting Fuchsia is not needed. So, it is safe to say that the prebuilt
  # Dart SDK in a Fuchsia build is the host prebuilt Dart SDK.
  if args.target_os is None or args.target_os == 'fuchsia':
    if sys.platform.startswith(('cygwin', 'win')):
      prebuilt = 'windows-x64'
    elif sys.platform == 'darwin':
      prebuilt = 'macos-x64'
    else:
      prebuilt = 'linux-x64'
  elif args.target_os == 'linux' and args.linux_cpu in ['x64', 'arm64']:
    prebuilt = 'linux-%s' % args.linux_cpu
  elif args.target_os == 'mac' and args.mac_cpu in ['x64', 'arm64']:
    prebuilt = 'macos-%s' % args.mac_cpu
  elif args.target_os == 'win' and args.windows_cpu in ['x64', 'arm64']:
    prebuilt = 'windows-%s' % args.windows_cpu

  prebuilts_dir = None
  if prebuilt is not None:
    prebuilts_dir = os.path.join(SRC_ROOT, 'flutter', 'prebuilts', prebuilt)
  return prebuilts_dir is not None and os.path.isdir(prebuilts_dir)


# Returns the host machine operating system.
def get_host_os():
  if sys.platform.startswith(('cygwin', 'win')):
    return 'win'
  if sys.platform == 'darwin':
    return 'mac'
  return 'linux'


# Runs true if the currently executing python interpreter is running under
# Rosetta. I.e., python3 is an x64 executable and we're on an arm64 Mac.
def is_rosetta():
  if platform.system() == 'Darwin':
    proc = subprocess.Popen(['sysctl', '-in', 'sysctl.proc_translated'],
                            stdout=subprocess.PIPE,
                            stderr=subprocess.STDOUT)
    output, _ = proc.communicate()
    return output.decode('utf-8').strip() == '1'
  return False


# Returns the host machine CPU architecture.
def get_host_cpu():
  # If gn itself is running under Rosetta on an arm64 Mac, platform.machine()
  # will return x86_64; instead return the underlying host architecture.
  if is_rosetta():
    return 'arm64'
  machine = platform.machine()
  if machine in ['aarch64', 'arm64', 'ARM64']:
    return 'arm64'
  if machine in ['x86_64', 'AMD64', 'x64']:
    return 'x64'
  if machine in ['i686', 'i386', 'x86']:
    return 'x86'
  raise Exception('Unknown CPU architecture: %s' % machine)


# Returns the target CPU architecture.
#
# For macOS host builds where --mac-cpu is specified, returns that value.
# For windows host builds where --windows-cpu is specified, returns that value.
# For all other host builds, assumes 'x64'.
def get_target_cpu(args):
  if args.target_os == 'android':
    return args.android_cpu
  if args.target_os == 'ios':
    if args.simulator:
      return args.simulator_cpu
    return args.ios_cpu
  if args.target_os == 'mac':
    return args.mac_cpu
  if args.target_os == 'linux':
    return args.linux_cpu
  if args.target_os == 'fuchsia':
    return args.fuchsia_cpu
  if args.target_os == 'wasm':
    return 'wasm'
  if args.target_os == 'win':
    return args.windows_cpu

  # Host build. Default to x64 unless overridden.
  if get_host_os() == 'mac' and args.mac_cpu:
    return args.mac_cpu
  if get_host_os() == 'win' and args.windows_cpu:
    return args.windows_cpu

  return 'x64'


def buildtools_dir():
  host_os = get_host_os()
  host_cpu = get_host_cpu()
  if host_os == 'win':
    host_os = 'windows'
  if host_os == 'mac' and host_cpu == 'arm64':
    host_cpu = 'x64'
  return '%s-%s' % (host_os, host_cpu)


def setup_goma(args):
  goma_gn_args = {}

  # args.goma has three states, True (--goma), False (--no-goma), and
  # None (default). In True mode, we force GOMA to be used (and fail
  # if we cannot enable it) unless the selected target definitely does
  # not support it (in which case we print a warning). In False mode,
  # we disable GOMA regardless. In None mode, we enable it if we can
  # autodetect a configuration, and otherwise disable it.

  # When running in CI, the recipes use their own goma install, and take
  # care of starting and stopping the compiler proxy.
  running_on_luci = os.environ.get('LUCI_CONTEXT') is not None

  # Prefer the goma fetched by gclient if it exists.
  cipd_goma_dir = os.path.join(SRC_ROOT, 'buildtools', buildtools_dir(), 'goma')

  # Next, if GOMA_DIR is set, use that install.
  goma_dir = os.environ.get('GOMA_DIR')

  # Finally, look for goma in the install location recommended in our
  # documentation.
  goma_home_dir = os.path.join(os.getenv('HOME', ''), 'goma')
  # 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')

  if args.target_os == 'wasm' or args.web:
    goma_gn_args['use_goma'] = False
    goma_gn_args['goma_dir'] = None
    if args.goma:
      print('Disabling GOMA for wasm builds, it is not supported yet.')
  elif args.goma is not False and not running_on_luci and os.path.exists(
      cipd_goma_dir):
    goma_gn_args['use_goma'] = True
    goma_gn_args['goma_dir'] = cipd_goma_dir
  elif args.goma is not False and goma_dir and os.path.exists(goma_dir):
    goma_gn_args['use_goma'] = True
    goma_gn_args['goma_dir'] = goma_dir
  elif args.goma is not False and os.path.exists(goma_home_dir):
    goma_gn_args['use_goma'] = True
    goma_gn_args['goma_dir'] = goma_home_dir
  elif args.goma:
    raise Exception(
        'GOMA was specified but was not found. Set the GOMA_DIR environment '
        'variable, install goma at $HOME/goma following the instructions at '
        'https://github.com/flutter/flutter/wiki/Compiling-the-engine, or '
        'run this script with the --no-goma flag to do a non-goma-enabled '
        'build.',
    )
  else:
    goma_gn_args['use_goma'] = False
    goma_gn_args['goma_dir'] = None

  if goma_gn_args['use_goma'] and sys.platform == 'darwin':
    if (not running_on_luci or args.xcode_symlinks or
        os.getenv('FLUTTER_GOMA_CREATE_XCODE_SYMLINKS', '0') == '1'):
      goma_gn_args['create_xcode_symlinks'] = True

  return goma_gn_args


def to_gn_args(args):
  if args.simulator:
    if args.target_os != 'ios':
      raise Exception('--simulator is only supported for iOS')

  runtime_mode = args.runtime_mode

  gn_args = {}

  gn_args['is_debug'] = args.unoptimized

  gn_args.update(setup_goma(args))

  # If building for WASM, set the GN args using 'to_gn_wasm_args' as most
  # of the Flutter SDK specific arguments are unused.
  if args.target_os == 'wasm' or args.web:
    to_gn_wasm_args(args, gn_args)
    return gn_args

  gn_args['full_dart_sdk'] = args.full_dart_sdk

  if args.enable_unittests:
    gn_args['enable_unittests'] = True
  if args.no_enable_unittests:
    gn_args['enable_unittests'] = False

  # Skia GN args.
  gn_args['skia_enable_flutter_defines'
         ] = True  # Enable Flutter API guards in Skia.
  gn_args['skia_use_dng_sdk'] = False  # RAW image handling.
  gn_args['skia_use_sfntly'] = False  # PDF handling dependency.
  gn_args['skia_enable_pdf'] = False  # PDF handling.
  gn_args['skia_use_x11'
         ] = False  # Never add the X11 dependency (only takes effect on Linux).
  gn_args['skia_use_wuffs'] = True
  gn_args['skia_use_expat'] = True
  gn_args['skia_use_fontconfig'] = args.enable_fontconfig
  gn_args['skia_use_icu'] = True
  gn_args['is_official_build'] = True  # Disable Skia test utilities.
  gn_args['android_full_debug'
         ] = args.target_os == 'android' and args.unoptimized
  if args.clang is None:
    gn_args['is_clang'] = True
  else:
    gn_args['is_clang'] = args.clang

  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'] = ''

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

  gn_args['enable_coverage'] = args.coverage

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

  enable_lto = args.lto
  if args.unoptimized:
    # There is no point in enabling LTO in unoptimized builds.
    enable_lto = False

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

  # Set OS, CPU arch for host or target build.
  if is_host_build(args):
    gn_args['host_os'] = get_host_os()
    gn_args['host_cpu'] = get_host_cpu()
    gn_args['target_os'] = gn_args['host_os']
    gn_args['target_cpu'] = get_target_cpu(args)
    gn_args['dart_target_arch'] = gn_args['target_cpu']
  else:
    gn_args['target_os'] = args.target_os
    gn_args['target_cpu'] = get_target_cpu(args)
    gn_args['dart_target_arch'] = gn_args['target_cpu']

  if not args.build_engine_artifacts:
    gn_args['flutter_build_engine_artifacts'] = False

  # We cannot cross-compile for 32 bit arm on a Windows host. We work around
  # this by leaving 'target_cpu' and 'dart_target_arch' set to 'arm' so that
  # Dart tools such as gen_snapshot that are built for the host will correctly
  # target arm, but we hardcode the 'current_cpu' to always be the host arch
  # so that the GN build doesn't go looking for a Windows arm toolchain, which
  # does not exist. Further, we set the 'host_cpu' so that it shares the
  # bitwidth of the 32-bit arm target.
  if sys.platform.startswith(
      ('cygwin', 'win')
  ) and args.target_os == 'android' and gn_args['target_cpu'] == 'arm':
    gn_args['host_cpu'] = 'x86'
    gn_args['current_cpu'] = 'x86'

  # When building binaries to run on a macOS host (like gen_snapshot), always
  # use the clang_x64 toolchain, which will run under Rosetta. This is done for
  # two reasons:
  #  1. goma currently only supports the clang_x64 toolchain.
  #  2. gen_snapshot cannot crossbuild from arm64 to x64. Its host architecture
  #     must be x64 to target x64.
  # TODO(cbracken): https://github.com/flutter/flutter/issues/103386
  if get_host_os() == 'mac' and not args.force_mac_arm64:
    gn_args['host_cpu'] = 'x64'

  if is_host_build(args) and gn_args['host_os'] == 'mac':
    # macOS unit tests include Vulkan headers which reference Metal types
    # introduced in macOS 10.14.
    gn_args['mac_sdk_min'] = '10.14'
    gn_args['mac_deployment_target'] = '10.14.0'

  if gn_args['target_os'] == 'ios':
    gn_args['use_ios_simulator'] = args.simulator
  elif get_host_os() == 'mac':
    gn_args['use_ios_simulator'] = False

  if args.dart_debug:
    gn_args['dart_debug'] = True

  if args.full_dart_debug:
    gn_args['dart_debug'] = True
    gn_args['dart_debug_optimization_level'] = '0'

  if args.dart_optimization_level:
    gn_args['dart_default_optimization_level'] = args.dart_optimization_level
  elif gn_args['target_os'] in ['android', 'ios']:
    gn_args['dart_default_optimization_level'] = '2'

  gn_args['flutter_use_fontconfig'] = args.enable_fontconfig
  gn_args['dart_component_kind'
         ] = 'static_library'  # Always link Dart in statically.
  gn_args['embedder_for_target'] = args.embedder_for_target
  gn_args['dart_lib_export_symbols'] = False
  gn_args['flutter_runtime_mode'] = runtime_mode
  gn_args['dart_version_git_info'] = not args.no_dart_version_git_info

  gn_args['dart_lib_export_symbols'] = False
  if runtime_mode == 'debug':
    gn_args['dart_runtime_mode'] = 'develop'
  elif runtime_mode == 'jit_release':
    gn_args['dart_runtime_mode'] = 'release'
  else:
    gn_args['dart_runtime_mode'] = runtime_mode

  # 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 targets from being part of the build tree.
  gn_args['enable_desktop_embeddings'] = not args.disable_desktop_embeddings

  # Determine whether backtace support should be compiled in.
  if args.backtrace:
    gn_args['enable_backtrace'] = (
        args.target_os in ['linux', 'mac', 'win'] or
        args.target_os == 'ios' and runtime_mode == 'debug'
    )
  else:
    gn_args['enable_backtrace'] = False

  # Overrides whether Boring SSL is compiled with system as. Only meaningful
  # on Android.
  gn_args['bssl_use_clang_integrated_as'] = True

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

  # DBC is not supported anymore.
  if args.interpreter:
    raise Exception(
        '--interpreter is no longer needed on any supported platform.'
    )

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

  if args.target_sysroot:
    gn_args['target_sysroot'] = args.target_sysroot
    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

  # Enable Metal on iOS builds.
  if args.target_os == 'ios':
    gn_args['shell_enable_gl'] = False
    gn_args['skia_use_gl'] = False
    gn_args['shell_enable_metal'] = True
    gn_args['skia_use_metal'] = True
    # 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'
  else:
    gn_args['skia_use_gl'] = args.target_os != 'fuchsia'

  if sys.platform == 'darwin' and args.target_os not in ['android', 'fuchsia']:
    # OpenGL is deprecated on macOS > 10.11.
    # This is not necessarily needed but enabling this until we have a way to
    # build a macOS metal only shell and a gl only shell.
    gn_args['allow_deprecated_api_calls'] = True
    gn_args['skia_use_metal'] = True
    gn_args['shell_enable_metal'] = True

  # Enable Vulkan on all platforms except for iOS. This is just
  # to save on mobile binary size, as there's no reason the Vulkan embedder
  # features can't work on these platforms.
  if gn_args['target_os'] not in ['ios', 'mac']:
    gn_args['skia_use_vulkan'] = True
    gn_args['skia_use_vma'] = False
    gn_args['shell_enable_vulkan'] = True
    # Disable VMA's use of std::shared_mutex in environments where the
    # standard library doesn't support it.
    if args.target_os == 'ios' or sys.platform.startswith(('cygwin', 'win')):
      gn_args['disable_vma_stl_shared_mutex'] = True

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

  # Make symbols visible in order to enable symbolization of unit test crash backtraces on Linux
  gn_args['disable_hidden_visibility'
         ] = args.target_os == 'linux' and args.unoptimized

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

  # If we have a prebuilt for the Dart SDK for the target architecture, then
  # use it instead of building a new one.
  if args.prebuilt_dart_sdk:
    if can_use_prebuilt_dart(args):
      print(
          'Using prebuilt Dart SDK binary. If you are editing Dart sources '
          'and wish to compile the Dart SDK, set `--no-prebuilt-dart-sdk`.'
      )
      gn_args['flutter_prebuilt_dart_sdk'] = True
      gn_args['dart_sdk_output'] = 'built-dart-sdk'
    elif is_host_build(args):
      print(
          'Tried to download prebuilt Dart SDK but an appropriate version '
          'could not be found!'
      )
      print(
          'Either retry by running '
          'flutter/tools/download_dart_sdk.py manually or compile from '
          'source by setting `--no-prebuilt-dart-sdk` flag to tools/gn'
      )
  elif is_host_build(args):
    # If we are building the dart sdk in-tree, exclude the wasm-opt target, as
    # it doesn't build properly with our gn configuration.
    gn_args['dart_include_wasm_opt'] = False

    # dart_platform_sdk is only defined for host builds, linux arm host builds
    # specify target_os=linux.
    # dart_platform_sdk=True means exclude web-related files, e.g. dart2js,
    # dartdevc, web SDK kernel and source files.
    gn_args['dart_platform_sdk'] = not args.full_dart_sdk

  if args.build_glfw_shell is not None:
    gn_args['build_glfw_shell'] = args.build_glfw_shell

  if args.build_embedder_examples is not None:
    gn_args['build_embedder_examples'] = args.build_embedder_examples

  gn_args['stripped_symbols'] = args.stripped

  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

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

  if args.fstack_protector:
    gn_args['use_fstack_protector'] = True

  enable_vulkan_validation = args.enable_vulkan_validation_layers

  # Enable Vulkan validation layer automatically on debug builds for arm64.
  if args.unoptimized and args.target_os == 'android' and args.android_cpu == 'arm64':
    enable_vulkan_validation = True

  if enable_vulkan_validation:
    gn_args['enable_vulkan_validation_layers'] = True
    if args.target_os == 'android':
      gn_args['android_api_level'] = 26

  # Enable pointer compression on 64-bit mobile targets. iOS is excluded due to
  # its inability to allocate address space without allocating memory.
  if args.target_os in ['android'] and gn_args['target_cpu'] in ['x64', 'arm64'
                                                                ]:
    gn_args['dart_use_compressed_pointers'] = True

  if args.fuchsia_target_api_level is not None:
    gn_args['fuchsia_target_api_level'] = args.fuchsia_target_api_level
  elif args.target_os == 'fuchsia':
    # Read the default target api level from a file so we can update it with a roller
    with open(os.path.join(os.path.dirname(__file__),
                           'fuchsia/target_api_level')) as file:
      gn_args['fuchsia_target_api_level'] = int(file.read().strip())

  # Flags for Dart features:
  if args.use_mallinfo2:
    gn_args['dart_use_mallinfo2'] = args.use_mallinfo2

  # Impeller flags.
  if args.enable_impeller_3d:
    gn_args['impeller_enable_3d'] = True

  if args.enable_impeller_vulkan:
    gn_args['impeller_enable_vulkan'] = True

  if args.enable_impeller_opengles:
    gn_args['impeller_enable_opengles'] = True

  if args.prebuilt_impellerc is not None:
    gn_args['impeller_use_prebuilt_impellerc'] = args.prebuilt_impellerc

  if args.malioc_path is not None:
    gn_args['impeller_malioc_path'] = args.malioc_path
  else:
    malioc_path = os.environ.get('MALIOC_PATH')
    if malioc_path:
      gn_args['impeller_malioc_path'] = malioc_path

  if args.use_glfw_swiftshader:
    if get_host_os() == 'mac':
      gn_args['glfw_vulkan_library'] = r'\"libvulkan.dylib\"'

  # ANGLE is exclusively used for:
  #  - Windows at runtime
  #  - Non-fuchsia host unit tests (is_host_build evaluates to false).
  # Setting these variables creates warnings otherwise.
  # If we add ANGLE usage on other platforms, include them here.
  # There is a special case for Android on Windows because there we _only_ build
  # gen_snapshot, but the build defines otherwise make it look like the build is
  # for a host Windows build and make GN think we will be building ANGLE.
  # Angle is not used on Mac hosts as there are no tests for the OpenGL backend.
  if (is_host_build(args) and
      gn_args['host_os'] != 'mac') or (args.target_os == 'android' and
                                       get_host_os() == 'win'):
    # Do not build unnecessary parts of the ANGLE tree.
    gn_args['angle_build_all'] = False
    gn_args['angle_has_astc_encoder'] = False
    # Force ANGLE context checks on Windows to prevent crashes.
    # TODO(loic-sharma): Remove this once ANGLE crashes have been fixed.
    # https://github.com/flutter/flutter/issues/114107
    if get_host_os() == 'win':
      gn_args['angle_force_context_check_every_call'] = True

    # ANGLE and SwiftShader share build flags to enable X11 and Wayland,
    # but we only need these enabled for SwiftShader.
    gn_args['angle_use_x11'] = False
    gn_args['angle_use_wayland'] = False

    # Requires RTTI. We may want to build this in debug modes, punting on that
    # for now.
    gn_args['angle_enable_vulkan_validation_layers'] = False
    gn_args['angle_vulkan_headers_dir'
           ] = '//third_party/vulkan-deps/vulkan-headers/src'
    gn_args['angle_vulkan_loader_dir'
           ] = '//third_party/vulkan-deps/vulkan-loader/src'
    gn_args['angle_vulkan_tools_dir'
           ] = '//third_party/vulkan-deps/vulkan-tools/src'

  if args.darwin_extension_safe:
    gn_args['darwin_extension_safe'] = True

  return gn_args


# When building for WASM, almost all GN args used in the Flutter SDK
# build are unused. This method is used instead.
def to_gn_wasm_args(args, gn_args):
  gn_args['is_official_build'] = True
  gn_args['skia_enable_flutter_defines'] = True
  gn_args['is_component_build'] = False
  gn_args['use_clang_static_analyzer'] = False
  gn_args['is_clang'] = True
  gn_args['target_os'] = 'wasm'
  gn_args['target_cpu'] = 'wasm'
  gn_args['wasm_use_dwarf'] = args.wasm_use_dwarf
  gn_args['skia_use_angle'] = False
  gn_args['skia_use_dng_sdk'] = False
  gn_args['skia_use_expat'] = False
  gn_args['skia_use_vulkan'] = False
  gn_args['skia_use_webgpu'] = False
  gn_args['skia_use_libheif'] = False
  gn_args['skia_use_libjpeg_turbo_encode'] = False
  gn_args['skia_use_no_jpeg_encode'] = True
  # TODO(yjbanov): https://github.com/flutter/flutter/issues/122759
  # Remove this and implement it through Canvas2d.
  gn_args['skia_use_libpng_encode'] = True
  gn_args['skia_use_libwebp_encode'] = False
  gn_args['skia_use_no_webp_encode'] = True
  gn_args['skia_use_lua'] = False
  gn_args['skia_use_wuffs'] = True
  gn_args['skia_use_zlib'] = True
  gn_args['skia_gl_standard'] = 'webgl'
  gn_args['skia_enable_ganesh'] = True
  gn_args['skia_enable_sksl_tracing'] = False
  gn_args['icu_use_data_file'] = False
  gn_args['skia_use_freetype'] = True
  gn_args['skia_use_harfbuzz'] = True
  gn_args['skia_use_fontconfig'] = False
  gn_args['skia_use_libheif'] = False
  gn_args['skia_enable_fontmgr_custom_directory'] = False
  gn_args['skia_enable_fontmgr_custom_embedded'] = True
  gn_args['skia_enable_fontmgr_custom_empty'] = True
  gn_args['skia_fontmgr_factory'
         ] = '//third_party/skia:fontmgr_custom_empty_factory'
  gn_args['skia_enable_skshaper'] = True
  gn_args['skia_enable_skparagraph'] = True
  gn_args['skia_canvaskit_force_tracing'] = False
  gn_args['skia_canvaskit_enable_skp_serialization'] = True
  gn_args['skia_canvaskit_enable_effects_deserialization'] = False
  gn_args['skia_canvaskit_enable_skottie'] = False
  gn_args['skia_canvaskit_include_viewer'] = False
  gn_args['skia_canvaskit_enable_pathops'] = True
  gn_args['skia_canvaskit_enable_rt_shader'] = True
  gn_args['skia_canvaskit_enable_matrix_helper'] = False
  gn_args['skia_canvaskit_enable_canvas_bindings'] = False
  gn_args['skia_canvaskit_enable_font'] = True
  gn_args['skia_canvaskit_enable_embedded_font'] = False
  gn_args['skia_canvaskit_enable_alias_font'] = True
  gn_args['skia_canvaskit_legacy_draw_vertices_blend_mode'] = False
  gn_args['skia_canvaskit_enable_debugger'] = False
  gn_args['skia_canvaskit_enable_paragraph'] = True
  gn_args['skia_canvaskit_enable_webgl'] = True
  gn_args['skia_canvaskit_enable_webgpu'] = False
  gn_args['skia_canvaskit_profile_build'] = args.runtime_mode == 'profile'
  gn_args['flutter_prebuilt_dart_sdk'] = True


def run_impeller_cmake(args):
  impeller_cmake_dir = os.path.join('third_party', 'impeller-cmake-example')
  if not os.path.isdir(os.path.join(SRC_ROOT, impeller_cmake_dir)):
    print(
        'The Impeller cmake example directory "{}" does not exist'
        .format(impeller_cmake_dir)
    )
    return 1
  goma_gn_args = setup_goma(args)
  goma_dir = goma_gn_args['goma_dir']
  cmake_cmd = [
      'python3',
      os.path.join(SRC_ROOT, 'flutter', 'ci', 'impeller_cmake_build_test.py'),
      '--path',
      impeller_cmake_dir,
      '--cmake',
  ]
  if goma_dir is not None:
    cmake_cmd = cmake_cmd + ['--goma-dir', goma_dir]
  if args.xcode_symlinks:
    cmake_cmd = cmake_cmd + ['--xcode-symlinks']
  try:
    cmake_call_result = subprocess.call(cmake_cmd, cwd=SRC_ROOT)
  except subprocess.CalledProcessError as exc:
    print('Failed to generate cmake files: ', exc.returncode, exc.output)
    return 1
  return cmake_call_result


def parse_args(args):
  args = args[1:]
  parser = argparse.ArgumentParser(description='A script to run `gn gen`.')

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

  parser.add_argument(
      '--enable-unittests',
      action='store_true',
      default=False,
      help='Force enable building unit test binaries.'
  )
  parser.add_argument(
      '--no-enable-unittests',
      default=False,
      action='store_true',
      help='Force disable building unit test binaries.'
  )

  parser.add_argument(
      '--runtime-mode',
      type=str,
      choices=['debug', 'profile', 'release', 'jit_release'],
      default='debug'
  )
  parser.add_argument('--interpreter', default=False, action='store_true')
  parser.add_argument(
      '--dart-debug',
      default=False,
      action='store_true',
      help='Enables assertions in the Dart VM. Does not affect optimization '
      'levels. If you need to disable optimizations in Dart, use '
      '--full-dart-debug'
  )
  parser.add_argument(
      '--no-dart-version-git-info',
      default=False,
      action='store_true',
      help='Set by default; if unset, turns off the dart SDK git hash check'
  )
  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.'
  )

  parser.add_argument(
      '--dart-optimization-level',
      type=str,
      help='The default optimization level for the Dart VM runtime.',
  )

  parser.add_argument(
      '--target-os',
      type=str,
      choices=['android', 'ios', 'mac', 'linux', 'fuchsia', 'wasm', 'win']
  )
  parser.add_argument(
      '--android', dest='target_os', action='store_const', const='android'
  )
  parser.add_argument(
      '--android-cpu',
      type=str,
      choices=['arm', 'x64', 'x86', 'arm64'],
      default='arm'
  )
  parser.add_argument(
      '--ios', dest='target_os', action='store_const', const='ios'
  )
  parser.add_argument(
      '--ios-cpu', type=str, choices=['arm', 'arm64'], default='arm64'
  )
  parser.add_argument(
      '--mac', dest='target_os', action='store_const', const='mac'
  )
  parser.add_argument(
      '--mac-cpu', type=str, choices=['x64', 'arm64'], default='x64'
  )
  parser.add_argument(
      '--force-mac-arm64',
      action='store_true',
      default=False,
      help='Force use of the clang_arm64 toolchain on an arm64 mac host when '
      'building host binaries.'
  )
  parser.add_argument('--simulator', action='store_true', default=False)
  parser.add_argument(
      '--linux', dest='target_os', action='store_const', const='linux'
  )
  parser.add_argument(
      '--fuchsia', dest='target_os', action='store_const', const='fuchsia'
  )
  parser.add_argument(
      '--wasm', dest='target_os', action='store_const', const='wasm'
  )
  parser.add_argument(
      '--wasm-use-dwarf',
      action='store_true',
      default=False,
      help='Embed dwarf debugging info in the output module instead of using '
      'sourcemap files.'
  )
  parser.add_argument('--web', action='store_true', default=False)
  parser.add_argument(
      '--windows', dest='target_os', action='store_const', const='win'
  )

  parser.add_argument(
      '--linux-cpu', type=str, choices=['x64', 'x86', 'arm64', 'arm']
  )
  parser.add_argument(
      '--fuchsia-cpu', type=str, choices=['x64', 'arm64'], default='x64'
  )
  parser.add_argument(
      '--windows-cpu', type=str, choices=['x64', 'arm64', 'x86'], default='x64'
  )
  parser.add_argument(
      '--simulator-cpu', type=str, choices=['x64', 'arm64'], default='x64'
  )
  parser.add_argument(
      '--arm-float-abi', type=str, choices=['hard', 'soft', 'softfp']
  )

  # Whether to compile in backtrace support.
  # Available for Windows and POSIX platforms whose libc includes execinfo.h.
  # MUSL doesn't include execinfo.h should be build with --no-backtrace.
  parser.add_argument(
      '--backtrace',
      default=True,
      action='store_true',
      help='Whether OS support exists for collecting backtraces.'
  )
  parser.add_argument('--no-backtrace', dest='backtrace', action='store_false')

  parser.add_argument(
      '--build-engine-artifacts',
      default=True,
      action='store_true',
      help='Build the host-side development artifacts.'
  )
  parser.add_argument(
      '--no-build-engine-artifacts',
      dest='build_engine_artifacts',
      action='store_false',
      help='Do not build the host-side development artifacts.'
  )

  parser.add_argument('--goma', default=None, action='store_true')
  parser.add_argument('--no-goma', dest='goma', action='store_false')
  parser.add_argument(
      '--xcode-symlinks',
      action='store_true',
      help='Set to true for builds targeting macOS or iOS when using goma. If '
      'set, symlinks to the Xcode provided sysroot and SDKs will be '
      'created in a generated folder, which will avoid potential backend '
      'errors in Fuchsia RBE. Instead of specifying the flag on each '
      'invocation the FLUTTER_GOMA_CREATE_XCODE_SYMLINKS environment '
      'variable may be set to 1 to achieve the same effect.'
  )
  parser.add_argument(
      '--no-xcode-symlinks',
      dest='xcode_symlinks',
      default=False,
      action='store_false'
  )
  parser.add_argument(
      '--depot-tools',
      default='~/depot_tools',
      type=str,
      help='Depot tools provides an alternative location for gomacc in ' +
      '/path/to/depot_tools/.cipd_bin'
  )

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

  parser.add_argument('--clang', action='store_const', const=True)
  parser.add_argument(
      '--no-clang', dest='clang', action='store_const', const=False
  )

  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'
  )

  parser.add_argument('--target-sysroot', type=str)
  parser.add_argument('--target-toolchain', type=str)
  parser.add_argument('--target-triple', type=str)
  parser.add_argument(
      '--operator-new-alignment',
      dest='operator_new_alignment',
      type=str,
      default=None
  )

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

  parser.add_argument('--enable-fontconfig', action='store_true', default=False)
  parser.add_argument(
      '--enable-vulkan-validation-layers', action='store_true', default=False
  )

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

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

  parser.add_argument(
      '--out-dir',
      default='',
      type=str,
      help='Root out directory. Target specific gn files will be generated in ${out-dir}/'
  )
  parser.add_argument(
      '--target-dir',
      default='',
      type=str,
      help='Use the specified name for target out directory. By default this tool determines one.'
  )

  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'
  )

  parser.add_argument(
      '--build-canvaskit',
      default=False,
      action='store_true',
      help='build canvaskit from source (DEPRECATED: use ninja targets to select what to build)'
  )

  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.'
  )

  parser.add_argument(
      '--allow-deprecated-api-calls',
      action='store_true',
      default=False,
      help='Turns off warnings about the usage of deprecated APIs.'
  )

  parser.add_argument(
      '--disable-desktop-embeddings',
      default=False,
      action='store_true',
      help='Do not include desktop embeddings in the build.'
  )
  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.'
  )
  parser.add_argument(
      '--build-embedder-examples',
      action='store_const',
      const=True,
      help='Build the example embedders using the Embedder API.'
  )
  parser.add_argument(
      '--no-build-embedder-examples',
      dest='build_embedder_examples',
      action='store_const',
      const=False,
      help='Do not build the example embedders using the Embedder API.'
  )

  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')

  parser.add_argument(
      '--prebuilt-dart-sdk',
      default=True,
      action='store_true',
      help='Whether to use a prebuilt Dart SDK instead of building one. This defaults to '
      + 'true and is enabled on CI.'
  )
  parser.add_argument(
      '--no-prebuilt-dart-sdk', dest='prebuilt_dart_sdk', action='store_false'
  )

  parser.add_argument(
      '--fuchsia-target-api-level', dest='fuchsia_target_api_level'
  )

  # Flags for Dart features.
  parser.add_argument(
      '--use-mallinfo2',
      dest='use_mallinfo2',
      default=False,
      action='store_true',
      help='Use mallinfo2 to collect malloc stats.'
  )

  # Impeller flags.
  parser.add_argument(
      '--enable-impeller-vulkan',
      default=False,
      action='store_true',
      help='Enable the Impeller Vulkan backend.'
  )

  parser.add_argument(
      '--enable-impeller-opengles',
      default=False,
      action='store_true',
      help='Enable the Impeller OpenGL ES backend.'
  )

  parser.add_argument(
      '--prebuilt-impellerc',
      default=None,
      type=str,
      help='Absolute path to a prebuilt impellerc. ' +
      'Do not use this outside of CI or with impellerc from a different engine version.'
  )

  parser.add_argument(
      '--enable-impeller-3d',
      default=False,
      action='store_true',
      help='Enables experimental 3d support.'
  )

  parser.add_argument(
      '--malioc-path', type=str, help='The path to the malioc tool.'
  )

  parser.add_argument(
      '--impeller-cmake-example',
      default=False,
      action='store_true',
      help='Do not run GN. Instead configure the Impeller cmake example build.',
  )

  # 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')
  parser.add_argument('--ubsan', default=False, action='store_true')

  parser.add_argument(
      '--fstack-protector',
      default=False,
      action='store_true',
      help='Whether the -fstack-protector flag should be passed unconditionally.'
  )

  parser.add_argument(
      '--trace-gn',
      default=False,
      action='store_true',
      help='Write a GN trace log (gn_trace.json) in the Chromium tracing '
      'format in the build directory.'
  )

  parser.add_argument(
      '--darwin-extension-safe',
      default=False,
      action='store_true',
      help='Whether the produced Flutter.framework is app extension safe. Only for iOS.'
  )

  # Verbose output.
  parser.add_argument('--verbose', default=False, action='store_true')

  parser.add_argument(
      '--gn-args',
      action='append',
      help='Additional gn args to be passed to gn. If you '
      'need to use this, it should probably be another switch '
      'in //flutter/tools/gn.',
  )

  parser.add_argument(
      '--use-glfw-swiftshader',
      default=False,
      action='store_true',
      help='Forces glfw to use swiftshader.',
  )

  return parser.parse_args(args)


def validate_args(args):
  valid = True
  if args.simulator:
    if args.mac_cpu != 'x64':
      print(
          'Specified a non-default mac-cpu for a simulator build. Did you mean '
          'to use `--simulator-cpu`?'
      )
      valid = False
    if args.ios_cpu != 'arm64':
      print(
          'Specified a non-default ios-cpu for a simulator build. Did you mean '
          'to use `--simulator-cpu`?'
      )
      valid = False

  if not valid:
    sys.exit(-1)


def main(argv):
  args = parse_args(argv)
  validate_args(args)

  if args.impeller_cmake_example:
    return run_impeller_cmake(args)

  exe = '.exe' if sys.platform.startswith(('cygwin', 'win')) else ''

  command = [
      '%s/flutter/third_party/gn/gn%s' % (SRC_ROOT, exe),
      'gen',
      '--check',
      '--export-compile-commands',
  ]

  if not args.web:
    if args.ide != '':
      command.append('--ide=%s' % args.ide)
    elif sys.platform == 'darwin':
      # On the Mac, generate an Xcode project by default.
      command.append('--ide=xcode')
      command.append('--xcode-project=flutter_engine')
      command.append('--xcode-build-system=new')
    elif sys.platform.startswith('win'):
      # On Windows, generate a Visual Studio project.
      command.append('--ide=vs')
      command.append('--ninja-executable=ninja')

  command.append('--export-compile-commands=default')

  gn_args = to_command_line(to_gn_args(args))
  gn_args.extend(args.gn_args or [])
  out_dir = get_out_dir(args)
  command.append(out_dir)
  command.append('--args=%s' % ' '.join(gn_args))

  if args.trace_gn:
    command.append('--tracelog=%s/gn_trace.json' % out_dir)

  if args.verbose:
    command.append('-v')

  print('Generating GN files in: %s' % out_dir)
  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)

  return gn_call_result


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