# Copyright 2017 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import("//build/config/clang/clang.gni")
import("//build/config/compiler/compiler.gni")
import("//build/config/fuchsia/config.gni")
import("//build/config/fuchsia/generate_runner_scripts.gni")
import("//build/config/fuchsia/gn_configs.gni")

assert(is_fuchsia)
assert(!is_posix, "Fuchsia is not POSIX.")

config("compiler") {
  configs = [ "//third_party/fuchsia-gn-sdk/src/config:compiler" ]

  # TODO(crbug.com/40513182): The stack defaults to 256k on Fuchsia (see
  # https://fuchsia.googlesource.com/zircon/+/master/system/private/zircon/stack.h#9),
  # but on other platforms it's much higher, so a variety of code assumes more
  # will be available. Raise to 8M which matches e.g. macOS.
  ldflags = [ "-Wl,-z,stack-size=0x800000" ]

  if (optimize_for_size && use_lld) {
    # Enable LLD's balanced partitioning for ordering function sections.
    # The flag has a significant reduction on binary size (roughly 5MB on 50MB
    # compressed webengine.far), but also slows down the runtime performance
    # (~5.35% to javascript benchrmarks on PGO enabled builds, or 12% on size
    # optimized builds). So we only enable it for size optimized builds to avoid
    # unnecessary performance losses on non-storage-constrained devices.
    # It's worth noting that this flag has a much smaller impact on browsing
    # performance which is more important for WebEngine. So trading some
    # performance for binary size savings is acceptable.
    ldflags += [ "-Wl,--bp-compression-sort=function" ]
  }

  if (is_web_engine) {
    defines = [ "IS_WEB_ENGINE" ]
  }
}

# Files required to run on Fuchsia on isolated swarming clients.
group("deployment_resources") {
  data = [
    "//build/fuchsia/",
    "//build/util/lib/",
    "//third_party/fuchsia-sdk/sdk/.build-id/",
    "//third_party/fuchsia-sdk/sdk/meta/manifest.json",
    "//third_party/fuchsia-sdk/sdk/tools/${test_host_cpu}/",
  ]

  if (fuchsia_additional_boot_images == []) {
    data += [ "${boot_image_root}" ]
  } else {
    # We never need to use both qemu images and device images.
    foreach(fuchsia_additional_boot_image, fuchsia_additional_boot_images) {
      data += [ "${fuchsia_additional_boot_image}/" ]
    }
  }

  if (test_isolate_uses_emulator) {
    # This assumes we are testing on QEMU with (U)EFI emulation only
    # if host_cpu == target_cpu, and also that these tests are executed
    # on x64 and arm64 only.
    if (test_host_cpu == "arm64") {
      data += [
        "//third_party/fuchsia-sdk/sdk/tools/qemu_uefi_internal/arm64",
        "//third_party/fuchsia-sdk/sdk/tools/qemu_uefi_internal/arm64-meta.json",
        "//third_party/qemu-${host_os}-${test_host_cpu}/",
      ]
    } else {
      data += [
        "//third_party/fuchsia-sdk/sdk/tools/qemu_uefi_internal/x64",
        "//third_party/fuchsia-sdk/sdk/tools/qemu_uefi_internal/x64-meta.json",
      ]
    }
  }
}

# Copy the loader to place it at the expected path in the final package.
copy("sysroot_asan_libs") {
  sources = [ "${fuchsia_arch_root}/sysroot/dist/lib/asan/ld.so.1" ]
  outputs = [ "${root_out_dir}/lib/asan/{{source_file_part}}" ]
}

# Copy the loader to place it at the expected path in the final package.
copy("sysroot_asan_runtime_libs") {
  sources = [ "$clang_base_path/lib/clang/$clang_version/lib/x86_64-unknown-fuchsia/libclang_rt.asan.so" ]
  outputs = [ "${root_out_dir}/lib/{{source_file_part}}" ]
}

# This adds the runtime deps for Fuchsia ASAN builds.
group("asan_runtime_library") {
  data_deps = [
    ":sysroot_asan_libs",
    ":sysroot_asan_runtime_libs",
  ]
}

# rustc gives the linker (clang++) "-pie" directives. clang++ complains on
# Fuchsia that these don't make any sense. On Fuchsia alone, for Rust-linked
# targets only, disable these warnings.
config("rustc_no_pie_warning") {
  ldflags = [ "-Wno-unused-command-line-argument" ]
}
