############################################################################
# libs/libxx/libcxxabi.defs
#
# 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.
#
###########################################################################

LIBCXXABI_VERSION=$(patsubst "%",%,$(CONFIG_LIBCXXABI_VERSION))

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

libcxxabi: libcxxabi-$(LIBCXXABI_VERSION).src.tar.xz
	$(Q) tar -xf libcxxabi-$(LIBCXXABI_VERSION).src.tar.xz
	$(Q) mv libcxxabi-$(LIBCXXABI_VERSION).src libcxxabi
	$(Q) touch $@
endif

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

context:: $(TOPDIR)/include/libcxxabi

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

ifeq ($(CONFIG_LIBCXXABI), y)
CXXFLAGS += ${DEFINE_PREFIX}LIBCXX_BUILDING_LIBCXXABI
CXXFLAGS += -I $(TOPDIR)/libs/libxx/libcxxabi/include
endif

# C++ABI files
CPPSRCS += cxa_aux_runtime.cpp cxa_default_handlers.cpp cxa_demangle.cpp cxa_exception_storage.cpp
CPPSRCS += cxa_guard.cpp cxa_handlers.cpp cxa_thread_atexit.cpp cxa_vector.cpp cxa_virtual.cpp
# C++ STL files
CPPSRCS += stdlib_exception.cpp stdlib_new_delete.cpp stdlib_stdexcept.cpp stdlib_typeinfo.cpp
# Internal files
CPPSRCS += abort_message.cpp fallback_malloc.cpp private_typeinfo.cpp

ifeq ($(CONFIG_CXX_EXCEPTION), y)
CXXFLAGS += ${DEFINE_PREFIX}LIBCXXABI_ENABLE_EXCEPTIONS
CPPSRCS += cxa_exception.cpp cxa_personality.cpp
else
CPPSRCS += cxa_noexception.cpp
endif

# RTTI is required for building the libcxxabi library
CXXFLAGS += -frtti

DEPPATH += --dep-path libcxxabi/src
VPATH += libcxxabi/src
