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

import("//build/config/android/config.gni")
import("//build/config/c++/c++.gni")
import("//build/config/cast.gni")
import("//build/config/chromeos/ui_mode.gni")
import("//build/config/compiler/compiler.gni")
import("//build/config/cronet/config.gni")
import("//build/config/dcheck_always_on.gni")
import("//build/config/logging.gni")
import("//build/config/sanitizers/sanitizers.gni")

partition_alloc_enable_arc_config = "//build/config/compiler:enable_arc"

# partition_alloc is performance critical and it should generally be optimized
# for speed so that pdfium developers aren't slowed down, even in Debug mode.
partition_alloc_remove_configs =
    [ "//build/config/compiler:default_optimization" ]
partition_alloc_add_configs = [ "//build/config/compiler:optimize_speed" ]

# Sanitizers replace the allocator, don't use our own allocator.
_is_using_sanitizers = is_asan || is_hwasan || is_lsan || is_tsan || is_msan

# - Component build support is disabled on all platforms except Linux. It is
#   known to cause issues on some (e.g. Windows with shims, Android with
#   non-universal symbol wrapping), and has not been validated on others.
# - Windows: debug CRT is not compatible, see below.
_disable_partition_alloc_everywhere =
    (!is_linux && is_component_build) || (is_win && is_debug)

# Under Windows debug build, the allocator shim is not compatible with CRT.
# For all other platforms & configurations, the shim is required, to replace
#     the default system allocators, e.g. with Partition Alloc.

# Additionally:
# - The allocator shim does not work with macOS with use_custom_libcxx=false.
# - The allocator shim isn't working standalone on Windows at the moment.
#   See crbug.com/pdfium/2068.
if ((is_linux || is_chromeos || is_android || is_apple ||
     (is_win && !is_component_build && !is_debug)) && !_is_using_sanitizers &&
    (!is_mac || use_custom_libcxx) && !is_win) {
  _use_shim = true
} else {
  _use_shim = false
}

if (_use_shim && !_disable_partition_alloc_everywhere) {
  _default_allocator = "partition"
} else {
  _default_allocator = "none"
}

# See base/allocator/partition_allocator/external_builds.md
use_allocator_shim_default = _use_shim
use_partition_alloc_as_malloc_default = _default_allocator == "partition"

_is_brp_supported = (is_win || is_android || is_linux || is_mac ||
                     is_chromeos) && use_partition_alloc_as_malloc_default

enable_backup_ref_ptr_support_default = _is_brp_supported

enable_mte_checked_ptr_support_default = false
put_ref_count_in_previous_slot_default = true
enable_backup_ref_ptr_slow_checks_default = false
enable_dangling_raw_ptr_checks_default = false
raw_ptr_zero_on_construct_default = true
raw_ptr_zero_on_move_default = true
raw_ptr_zero_on_destruct_default = false

# Allow embedders to opt-out of C++20 build which is set as default.
# Set to true to use C++20 by default.
assert_cpp20_default = true
