############################################################################
# libs/libxx/libcxx/Make.defs
#
# SPDX-License-Identifier: Apache-2.0
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.  The
# ASF licenses this file to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance with the
# License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
# License for the specific language governing permissions and limitations
# under the License.
#
###########################################################################

LIBCXX_VERSION=$(patsubst "%",%,$(CONFIG_LIBCXX_VERSION))

# Download and unpack tarball if no git repo found
ifeq ($(wildcard libcxx/libcxx/.git),)
libcxx-$(LIBCXX_VERSION).src.tar.xz:
	$(call DOWNLOAD,https://github.com/llvm/llvm-project/releases/download/llvmorg-$(LIBCXX_VERSION),$@)

libcxx/libcxx: libcxx-$(LIBCXX_VERSION).src.tar.xz
	$(Q) tar -xf $< \
	         --exclude libcxx-$(LIBCXX_VERSION).src/test/std/pstl
	$(Q) mv libcxx-$(LIBCXX_VERSION).src $@
	$(Q) patch -p0 < libcxx/0001_fix_stdatomic_h_miss_typedef.patch -d libcxx
	$(Q) patch -p2 < libcxx/mbstate_t.patch -d libcxx
	$(Q) patch -p0 < libcxx/0001-libcxx-remove-mach-time-h.patch -d libcxx
	$(Q) patch -p0 < libcxx/0001-libcxx-fix-ld-errors.patch -d libcxx
	$(Q) patch -p0 < libcxx/0001-Fix-build-error-about-__GLIBC__.patch -d libcxx
	$(Q) patch -p0 < libcxx/0001-libc-Fix-failures-with-GCC-14-92663.patch -d libcxx

endif

$(TOPDIR)/include/libcxx: libcxx/libcxx
	$(Q) $(DIRLINK) $(CURDIR)/$</include $(TOPDIR)/include/libcxx
	$(Q) cp $(CURDIR)/__config_site $(TOPDIR)/include/libcxx/__config_site

context:: $(TOPDIR)/include/libcxx

distclean::
	$(Q) $(DIRUNLINK) $(TOPDIR)/include/libcxx
ifeq ($(wildcard libcxx/libcxx/.git),)
	$(Q) $(DELFILE) libcxx-$(LIBCXX_VERSION).src.tar.xz
	$(call DELDIR, libcxx/libcxx)
endif

CXXFLAGS += ${DEFINE_PREFIX}_LIBCPP_BUILDING_LIBRARY
CXXFLAGS += ${INCDIR_PREFIX}$(CURDIR)/libcxx/libcxx/src

ifeq ($(CONFIG_LIBSUPCXX_TOOLCHAIN),y)
CXXFLAGS += ${DEFINE_PREFIX}__GLIBCXX__
endif

ifeq ($(CONFIG_LIBSUPCXX),y)
CXXFLAGS += ${DEFINE_PREFIX}__GLIBCXX__
endif

CXXFLAGS += -Wno-shadow -Wno-sign-compare -Wno-cpp
CXXFLAGS += -Wno-attributes -Wno-deprecated-declarations

ifeq ($(shell expr "$(GCCVER)" \>= 12), 1)
  CXXFLAGS += -Wno-maybe-uninitialized -Wno-alloc-size-larger-than
endif

CPPSRCS += $(wildcard libcxx/libcxx/src/*.cpp)
CPPSRCS += $(wildcard libcxx/libcxx/src/experimental/*.cpp)
CPPSRCS += $(wildcard libcxx/libcxx/src/filesystem/*.cpp)
CPPSRCS += $(wildcard libcxx/libcxx/src/ryu/*.cpp)

ifeq ($(CONFIG_CXX_LOCALIZATION),)
  LOCALE_CPPSRCS := libcxx/libcxx/src/ios.cpp
  LOCALE_CPPSRCS += libcxx/libcxx/src/ios.instantiations.cpp
  LOCALE_CPPSRCS += libcxx/libcxx/src/iostream.cpp
  LOCALE_CPPSRCS += libcxx/libcxx/src/locale.cpp
  LOCALE_CPPSRCS += libcxx/libcxx/src/regex.cpp
  LOCALE_CPPSRCS += libcxx/libcxx/src/strstream.cpp
  CPPSRCS := $(filter-out $(LOCALE_CPPSRCS), $(CPPSRCS))
endif
