import("//libcxx/config.gni")
import("//llvm/utils/gn/build/write_cmake_config.gni")

libcxx_generated_include_dir = "$root_build_dir/include/c++/v1"

# This is a bit weird. For now, we assume that __config_site is identical
# in all toolchains, and only copy it (and all other libcxx headers)
# to 'include' in the root build dir, so that it's the same for all toolchains.
# Maybe we want to make this per-toolchain eventually (and then use root_out_dir
# in libcxx_generated_include_dir) -- e.g. for cross-builds that for example
# use for-linux-configured libc++ for the host build but for-windows-configured
# libc++ for the target build.
if (current_toolchain == default_toolchain) {
  write_cmake_config("write_config_site") {
    input = "__config_site.in"
    output = "$libcxx_generated_include_dir/__config_site"

    values = [
      "_LIBCPP_ABI_FORCE_ITANIUM=",
      "_LIBCPP_ABI_FORCE_MICROSOFT=",
      "_LIBCPP_EXTRA_SITE_DEFINES=",
      "_LIBCPP_HIDE_FROM_ABI_PER_TU_BY_DEFAULT=",
      "_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY=",
      "_LIBCPP_HAS_NO_INCOMPLETE_FORMAT=",
      "_LIBCPP_HAS_NO_INCOMPLETE_RANGES=",
      "_LIBCPP_HAS_NO_THREADS=",
      "_LIBCPP_HAS_NO_MONOTONIC_CLOCK=",
      "_LIBCPP_HAS_MUSL_LIBC=",
      "_LIBCPP_HAS_THREAD_API_PTHREAD=",
      "_LIBCPP_HAS_THREAD_API_EXTERNAL=",
      "_LIBCPP_HAS_THREAD_API_WIN32=",
      "_LIBCPP_HAS_THREAD_LIBRARY_EXTERNAL=",
      "_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS=",
      "_LIBCPP_HAS_NO_VENDOR_AVAILABILITY_ANNOTATIONS=1",
      "_LIBCPP_NO_VCRUNTIME=",
      "_LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION=",
      "_LIBCPP_HAS_PARALLEL_ALGORITHMS=",
      "_LIBCPP_HAS_NO_RANDOM_DEVICE=",
      "_LIBCPP_HAS_NO_LOCALIZATION=",
      "_LIBCPP_HAS_NO_WIDE_CHARACTERS=",
      "_LIBCPP_ABI_DEFINES=",
    ]
    if (libcxx_abi_version != 1) {
      values += [ "_LIBCPP_ABI_VERSION=$libcxx_abi_version" ]
    } else {
      values += [ "_LIBCPP_ABI_VERSION=" ]
    }
    if (libcxx_abi_namespace != "") {
      values += [ "_LIBCPP_ABI_NAMESPACE=$libcxx_abi_namespace" ]
    } else {
      values += [ "_LIBCPP_ABI_NAMESPACE=" ]
    }
    if (libcxx_abi_unstable) {
      values += [ "_LIBCPP_ABI_UNSTABLE=1" ]
    } else {
      values += [ "_LIBCPP_ABI_UNSTABLE=" ]
    }
  }

  copy("copy_headers") {
    sources = [
      "__algorithm/adjacent_find.h",
      "__algorithm/all_of.h",
      "__algorithm/any_of.h",
      "__algorithm/binary_search.h",
      "__algorithm/clamp.h",
      "__algorithm/comp.h",
      "__algorithm/comp_ref_type.h",
      "__algorithm/copy.h",
      "__algorithm/copy_backward.h",
      "__algorithm/copy_if.h",
      "__algorithm/copy_n.h",
      "__algorithm/count.h",
      "__algorithm/count_if.h",
      "__algorithm/equal.h",
      "__algorithm/equal_range.h",
      "__algorithm/fill.h",
      "__algorithm/fill_n.h",
      "__algorithm/find.h",
      "__algorithm/find_end.h",
      "__algorithm/find_first_of.h",
      "__algorithm/find_if.h",
      "__algorithm/find_if_not.h",
      "__algorithm/for_each.h",
      "__algorithm/for_each_n.h",
      "__algorithm/generate.h",
      "__algorithm/generate_n.h",
      "__algorithm/half_positive.h",
      "__algorithm/includes.h",
      "__algorithm/inplace_merge.h",
      "__algorithm/is_heap.h",
      "__algorithm/is_heap_until.h",
      "__algorithm/is_partitioned.h",
      "__algorithm/is_permutation.h",
      "__algorithm/is_sorted.h",
      "__algorithm/is_sorted_until.h",
      "__algorithm/iter_swap.h",
      "__algorithm/lexicographical_compare.h",
      "__algorithm/lower_bound.h",
      "__algorithm/make_heap.h",
      "__algorithm/max.h",
      "__algorithm/max_element.h",
      "__algorithm/merge.h",
      "__algorithm/min.h",
      "__algorithm/min_element.h",
      "__algorithm/minmax.h",
      "__algorithm/minmax_element.h",
      "__algorithm/mismatch.h",
      "__algorithm/move.h",
      "__algorithm/move_backward.h",
      "__algorithm/next_permutation.h",
      "__algorithm/none_of.h",
      "__algorithm/nth_element.h",
      "__algorithm/partial_sort.h",
      "__algorithm/partial_sort_copy.h",
      "__algorithm/partition.h",
      "__algorithm/partition_copy.h",
      "__algorithm/partition_point.h",
      "__algorithm/pop_heap.h",
      "__algorithm/prev_permutation.h",
      "__algorithm/push_heap.h",
      "__algorithm/remove.h",
      "__algorithm/remove_copy.h",
      "__algorithm/remove_copy_if.h",
      "__algorithm/remove_if.h",
      "__algorithm/replace.h",
      "__algorithm/replace_copy.h",
      "__algorithm/replace_copy_if.h",
      "__algorithm/replace_if.h",
      "__algorithm/reverse.h",
      "__algorithm/reverse_copy.h",
      "__algorithm/rotate.h",
      "__algorithm/rotate_copy.h",
      "__algorithm/sample.h",
      "__algorithm/search.h",
      "__algorithm/search_n.h",
      "__algorithm/set_difference.h",
      "__algorithm/set_intersection.h",
      "__algorithm/set_symmetric_difference.h",
      "__algorithm/set_union.h",
      "__algorithm/shift_left.h",
      "__algorithm/shift_right.h",
      "__algorithm/shuffle.h",
      "__algorithm/sift_down.h",
      "__algorithm/sort.h",
      "__algorithm/sort_heap.h",
      "__algorithm/stable_partition.h",
      "__algorithm/stable_sort.h",
      "__algorithm/swap_ranges.h",
      "__algorithm/transform.h",
      "__algorithm/unique.h",
      "__algorithm/unique_copy.h",
      "__algorithm/unwrap_iter.h",
      "__algorithm/upper_bound.h",
      "__availability",
      "__bit/bit_cast.h",
      "__bit/byteswap.h",
      "__bit_reference",
      "__bits",
      "__bsd_locale_defaults.h",
      "__bsd_locale_fallbacks.h",
      "__charconv/chars_format.h",
      "__charconv/from_chars_result.h",
      "__charconv/to_chars_result.h",
      "__compare/common_comparison_category.h",
      "__compare/compare_three_way.h",
      "__compare/compare_three_way_result.h",
      "__compare/is_eq.h",
      "__compare/ordering.h",
      "__compare/partial_order.h",
      "__compare/strong_order.h",
      "__compare/synth_three_way.h",
      "__compare/three_way_comparable.h",
      "__compare/weak_order.h",
      "__concepts/arithmetic.h",
      "__concepts/assignable.h",
      "__concepts/boolean_testable.h",
      "__concepts/class_or_enum.h",
      "__concepts/common_reference_with.h",
      "__concepts/common_with.h",
      "__concepts/constructible.h",
      "__concepts/convertible_to.h",
      "__concepts/copyable.h",
      "__concepts/derived_from.h",
      "__concepts/destructible.h",
      "__concepts/different_from.h",
      "__concepts/equality_comparable.h",
      "__concepts/invocable.h",
      "__concepts/movable.h",
      "__concepts/predicate.h",
      "__concepts/regular.h",
      "__concepts/relation.h",
      "__concepts/same_as.h",
      "__concepts/semiregular.h",
      "__concepts/swappable.h",
      "__concepts/totally_ordered.h",
      "__config",
      "__coroutine/coroutine_handle.h",
      "__coroutine/coroutine_traits.h",
      "__coroutine/noop_coroutine_handle.h",
      "__coroutine/trivial_awaitables.h",
      "__debug",
      "__errc",
      "__filesystem/copy_options.h",
      "__filesystem/directory_entry.h",
      "__filesystem/directory_iterator.h",
      "__filesystem/directory_options.h",
      "__filesystem/file_status.h",
      "__filesystem/file_time_type.h",
      "__filesystem/file_type.h",
      "__filesystem/filesystem_error.h",
      "__filesystem/operations.h",
      "__filesystem/path.h",
      "__filesystem/path_iterator.h",
      "__filesystem/perm_options.h",
      "__filesystem/perms.h",
      "__filesystem/recursive_directory_iterator.h",
      "__filesystem/space_info.h",
      "__filesystem/u8path.h",
      "__format/format_arg.h",
      "__format/format_args.h",
      "__format/format_context.h",
      "__format/format_error.h",
      "__format/format_fwd.h",
      "__format/format_parse_context.h",
      "__format/format_string.h",
      "__format/format_to_n_result.h",
      "__format/formatter.h",
      "__format/formatter_bool.h",
      "__format/formatter_char.h",
      "__format/formatter_integer.h",
      "__format/formatter_integral.h",
      "__format/formatter_string.h",
      "__format/parser_std_format_spec.h",
      "__function_like.h",
      "__functional/binary_function.h",
      "__functional/binary_negate.h",
      "__functional/bind.h",
      "__functional/bind_back.h",
      "__functional/bind_front.h",
      "__functional/binder1st.h",
      "__functional/binder2nd.h",
      "__functional/compose.h",
      "__functional/default_searcher.h",
      "__functional/function.h",
      "__functional/hash.h",
      "__functional/identity.h",
      "__functional/invoke.h",
      "__functional/is_transparent.h",
      "__functional/mem_fn.h",
      "__functional/mem_fun_ref.h",
      "__functional/not_fn.h",
      "__functional/operations.h",
      "__functional/perfect_forward.h",
      "__functional/pointer_to_binary_function.h",
      "__functional/pointer_to_unary_function.h",
      "__functional/ranges_operations.h",
      "__functional/reference_wrapper.h",
      "__functional/unary_function.h",
      "__functional/unary_negate.h",
      "__functional/unwrap_ref.h",
      "__functional/weak_result_type.h",
      "__functional_base",
      "__hash_table",
      "__iterator/access.h",
      "__iterator/advance.h",
      "__iterator/back_insert_iterator.h",
      "__iterator/common_iterator.h",
      "__iterator/concepts.h",
      "__iterator/counted_iterator.h",
      "__iterator/data.h",
      "__iterator/default_sentinel.h",
      "__iterator/distance.h",
      "__iterator/empty.h",
      "__iterator/erase_if_container.h",
      "__iterator/front_insert_iterator.h",
      "__iterator/incrementable_traits.h",
      "__iterator/insert_iterator.h",
      "__iterator/istream_iterator.h",
      "__iterator/istreambuf_iterator.h",
      "__iterator/iter_move.h",
      "__iterator/iter_swap.h",
      "__iterator/iterator.h",
      "__iterator/iterator_traits.h",
      "__iterator/move_iterator.h",
      "__iterator/next.h",
      "__iterator/ostream_iterator.h",
      "__iterator/ostreambuf_iterator.h",
      "__iterator/prev.h",
      "__iterator/projected.h",
      "__iterator/readable_traits.h",
      "__iterator/reverse_access.h",
      "__iterator/reverse_iterator.h",
      "__iterator/size.h",
      "__iterator/unreachable_sentinel.h",
      "__iterator/wrap_iter.h",
      "__libcpp_version",
      "__locale",
      "__mbstate_t.h",
      "__memory/addressof.h",
      "__memory/allocation_guard.h",
      "__memory/allocator.h",
      "__memory/allocator_arg_t.h",
      "__memory/allocator_traits.h",
      "__memory/auto_ptr.h",
      "__memory/compressed_pair.h",
      "__memory/concepts.h",
      "__memory/construct_at.h",
      "__memory/pointer_traits.h",
      "__memory/ranges_uninitialized_algorithms.h",
      "__memory/raw_storage_iterator.h",
      "__memory/shared_ptr.h",
      "__memory/temporary_buffer.h",
      "__memory/uninitialized_algorithms.h",
      "__memory/unique_ptr.h",
      "__memory/uses_allocator.h",
      "__memory/voidify.h",
      "__mutex_base",
      "__node_handle",
      "__nullptr",
      "__numeric/accumulate.h",
      "__numeric/adjacent_difference.h",
      "__numeric/exclusive_scan.h",
      "__numeric/gcd_lcm.h",
      "__numeric/inclusive_scan.h",
      "__numeric/inner_product.h",
      "__numeric/iota.h",
      "__numeric/midpoint.h",
      "__numeric/partial_sum.h",
      "__numeric/reduce.h",
      "__numeric/transform_exclusive_scan.h",
      "__numeric/transform_inclusive_scan.h",
      "__numeric/transform_reduce.h",
      "__random/bernoulli_distribution.h",
      "__random/binomial_distribution.h",
      "__random/cauchy_distribution.h",
      "__random/chi_squared_distribution.h",
      "__random/clamp_to_integral.h",
      "__random/default_random_engine.h",
      "__random/discard_block_engine.h",
      "__random/discrete_distribution.h",
      "__random/exponential_distribution.h",
      "__random/extreme_value_distribution.h",
      "__random/fisher_f_distribution.h",
      "__random/gamma_distribution.h",
      "__random/generate_canonical.h",
      "__random/geometric_distribution.h",
      "__random/independent_bits_engine.h",
      "__random/is_seed_sequence.h",
      "__random/knuth_b.h",
      "__random/linear_congruential_engine.h",
      "__random/log2.h",
      "__random/lognormal_distribution.h",
      "__random/mersenne_twister_engine.h",
      "__random/negative_binomial_distribution.h",
      "__random/normal_distribution.h",
      "__random/piecewise_constant_distribution.h",
      "__random/piecewise_linear_distribution.h",
      "__random/poisson_distribution.h",
      "__random/random_device.h",
      "__random/ranlux.h",
      "__random/seed_seq.h",
      "__random/shuffle_order_engine.h",
      "__random/student_t_distribution.h",
      "__random/subtract_with_carry_engine.h",
      "__random/uniform_int_distribution.h",
      "__random/uniform_random_bit_generator.h",
      "__random/uniform_real_distribution.h",
      "__random/weibull_distribution.h",
      "__ranges/access.h",
      "__ranges/all.h",
      "__ranges/common_view.h",
      "__ranges/concepts.h",
      "__ranges/copyable_box.h",
      "__ranges/counted.h",
      "__ranges/dangling.h",
      "__ranges/data.h",
      "__ranges/drop_view.h",
      "__ranges/empty.h",
      "__ranges/empty_view.h",
      "__ranges/enable_borrowed_range.h",
      "__ranges/enable_view.h",
      "__ranges/iota_view.h",
      "__ranges/join_view.h",
      "__ranges/non_propagating_cache.h",
      "__ranges/range_adaptor.h",
      "__ranges/ref_view.h",
      "__ranges/reverse_view.h",
      "__ranges/single_view.h",
      "__ranges/size.h",
      "__ranges/subrange.h",
      "__ranges/take_view.h",
      "__ranges/transform_view.h",
      "__ranges/view_interface.h",
      "__split_buffer",
      "__std_stream",
      "__string",
      "__support/android/locale_bionic.h",
      "__support/fuchsia/xlocale.h",
      "__support/ibm/gettod_zos.h",
      "__support/ibm/limits.h",
      "__support/ibm/locale_mgmt_zos.h",
      "__support/ibm/nanosleep.h",
      "__support/ibm/support.h",
      "__support/ibm/xlocale.h",
      "__support/musl/xlocale.h",
      "__support/newlib/xlocale.h",
      "__support/openbsd/xlocale.h",
      "__support/solaris/floatingpoint.h",
      "__support/solaris/wchar.h",
      "__support/solaris/xlocale.h",
      "__support/win32/limits_msvc_win32.h",
      "__support/win32/locale_win32.h",
      "__support/xlocale/__nop_locale_mgmt.h",
      "__support/xlocale/__posix_l_fallback.h",
      "__support/xlocale/__strtonum_fallback.h",
      "__thread/poll_with_backoff.h",
      "__threading_support",
      "__tree",
      "__tuple",
      "__undef_macros",
      "__utility/as_const.h",
      "__utility/auto_cast.h",
      "__utility/cmp.h",
      "__utility/declval.h",
      "__utility/exchange.h",
      "__utility/forward.h",
      "__utility/in_place.h",
      "__utility/integer_sequence.h",
      "__utility/move.h",
      "__utility/pair.h",
      "__utility/piecewise_construct.h",
      "__utility/priority_tag.h",
      "__utility/rel_ops.h",
      "__utility/swap.h",
      "__utility/to_underlying.h",
      "__utility/transaction.h",
      "__variant/monostate.h",
      "algorithm",
      "any",
      "array",
      "atomic",
      "barrier",
      "bit",
      "bitset",
      "cassert",
      "ccomplex",
      "cctype",
      "cerrno",
      "cfenv",
      "cfloat",
      "charconv",
      "chrono",
      "cinttypes",
      "ciso646",
      "climits",
      "clocale",
      "cmath",
      "codecvt",
      "compare",
      "complex",
      "complex.h",
      "concepts",
      "condition_variable",
      "csetjmp",
      "csignal",
      "cstdarg",
      "cstdbool",
      "cstddef",
      "cstdint",
      "cstdio",
      "cstdlib",
      "cstring",
      "ctgmath",
      "ctime",
      "ctype.h",
      "cwchar",
      "cwctype",
      "deque",
      "errno.h",
      "exception",
      "execution",
      "experimental/__config",
      "experimental/__memory",
      "experimental/algorithm",
      "experimental/coroutine",
      "experimental/deque",
      "experimental/filesystem",
      "experimental/forward_list",
      "experimental/functional",
      "experimental/iterator",
      "experimental/list",
      "experimental/map",
      "experimental/memory_resource",
      "experimental/propagate_const",
      "experimental/regex",
      "experimental/set",
      "experimental/simd",
      "experimental/string",
      "experimental/type_traits",
      "experimental/unordered_map",
      "experimental/unordered_set",
      "experimental/utility",
      "experimental/vector",
      "ext/__hash",
      "ext/hash_map",
      "ext/hash_set",
      "fenv.h",
      "filesystem",
      "float.h",
      "format",
      "forward_list",
      "fstream",
      "functional",
      "future",
      "initializer_list",
      "inttypes.h",
      "iomanip",
      "ios",
      "iosfwd",
      "iostream",
      "istream",
      "iterator",
      "latch",
      "limits",
      "limits.h",
      "list",
      "locale",
      "locale.h",
      "map",
      "math.h",
      "memory",
      "module.modulemap",
      "mutex",
      "new",
      "numbers",
      "numeric",
      "optional",
      "ostream",
      "queue",
      "random",
      "ratio",
      "regex",
      "scoped_allocator",
      "semaphore",
      "set",
      "setjmp.h",
      "shared_mutex",
      "span",
      "sstream",
      "stack",
      "stdbool.h",
      "stddef.h",
      "stdexcept",
      "stdint.h",
      "stdio.h",
      "stdlib.h",
      "streambuf",
      "string",
      "string.h",
      "string_view",
      "strstream",
      "system_error",
      "tgmath.h",
      "thread",
      "tuple",
      "type_traits",
      "typeindex",
      "typeinfo",
      "unordered_map",
      "unordered_set",
      "utility",
      "valarray",
      "variant",
      "vector",
      "version",
      "wchar.h",
      "wctype.h",
    ]
    deps = [ ":write_config_site" ]
    if (target_os != "mac" && target_os != "win") {
      # libcxx/cmake/Modules/HandleLibCXXABI.cmake sets
      # LIBCXX_CXX_ABI_HEADER_TARGET if the libcxx abi library either of
      # "libstdc++", "libsupc++", "libcxxabi", "libcxxrt", but not if it's "none",
      # "default", or "vcruntime". So on Windows, these don't get copied due to
      # LIBCXX_CXX_ABI_HEADER_TARGET not being set.
      # On macOS, libcxx/CMakeLists.txt sets LIBCXX_CXX_ABI_SYSTEM to 1, which
      # causes an empty header list to be passed to setup_abi_lib, so these
      # don't get copied on macOS due to that.
      deps += [ "//libcxxabi/include" ]
    }
    outputs = [ "$root_build_dir/include/c++/v1/{{source_target_relative}}" ]
  }
}

config("include_config") {
  include_dirs = [ libcxx_generated_include_dir ]
}

group("include") {
  deps = [ ":copy_headers($default_toolchain)" ]
  public_configs = [ ":include_config" ]
}
