############################################################################
# libs/libm/newlib/Make.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.
#
############################################################################

ifeq ($(CONFIG_ARCH_CHIP_ESP32),y)
  NEWLIB_VERSION=esp-4.1.0_20230425
  NEWLIB_BASENAME=newlib-esp32
  NEWLIB_URL_BASE=https://github.com/espressif/newlib-esp32/archive
  NEWLIB_TARBALL=$(NEWLIB_VERSION).tar.gz
else
  NEWLIB_VERSION=4.3.0.20230120
  NEWLIB_BASENAME=newlib
  NEWLIB_URL_BASE=https://sourceware.org/pub/newlib
  NEWLIB_TARBALL=$(NEWLIB_BASENAME)-$(NEWLIB_VERSION).tar.gz
endif

ifeq ($(wildcard newlib/newlib/.git),)
$(NEWLIB_BASENAME)-$(NEWLIB_VERSION).tar.gz:
	$(call DOWNLOAD,$(NEWLIB_URL_BASE),$(NEWLIB_TARBALL),$(NEWLIB_BASENAME)-$(NEWLIB_VERSION).tar.gz)

newlib/newlib: $(NEWLIB_BASENAME)-$(NEWLIB_VERSION).tar.gz
	$(Q) tar -xf $(NEWLIB_BASENAME)-$(NEWLIB_VERSION).tar.gz
	$(Q) mv $(NEWLIB_BASENAME)-$(NEWLIB_VERSION) newlib/newlib
	$(Q) patch -p0 < newlib/0001-newlib-libm-fix-__RCSID-build-error.patch
	$(Q) patch -p0 < newlib/0002-newlib-libm-remove-include-reent.h.patch
	$(Q) patch -p0 < newlib/0003-newlib-fix-compilation-for-x86.patch
	$(Q) touch $@
endif

distclean::
	$(Q) $(DIRUNLINK) $(TOPDIR)/include/newlib
ifeq ($(wildcard newlib/newlib/.git),)
	$(call DELFILE, $(NEWLIB_BASENAME)-$(NEWLIB_VERSION).tar.gz)
	$(call DELDIR, newlib/newlib)
endif

$(TOPDIR)/include/newlib: newlib/newlib
	$(Q) $(DIRLINK) $(CURDIR)/newlib/include $@

context:: $(TOPDIR)/include/newlib

CSRCS += $(wildcard newlib/newlib/newlib/libm/common/*.c)
CSRCS += $(wildcard newlib/newlib/newlib/libm/complex/*.c)

VPATH += :newlib/newlib/newlib/libm/common
VPATH += :newlib/newlib/newlib/libm/complex

ifeq ($(CONFIG_ARCH_ARM),y)
  ARCH = arm
else ifeq ($(CONFIG_ARCH_ARM64),y)
  ARCH = aarch64
else ifeq ($(CONFIG_ARCH_RISCV),y)
  ARCH = riscv
else ifeq ($(CONFIG_ARCH_X86),y)
  ARCH = i386
else ifeq ($(CONFIG_ARCH_X86_64),y)
  ARCH = x86_64
else ifeq ($(CONFIG_ARCH_SPARC),y)
  ARCH = sparc
else ifeq ($(CONFIG_ARCH_MIPS),y)
  ARCH = mips
else
  ARCH = $(CONFIG_ARCH)
endif

CSRCS += $(wildcard newlib/newlib/newlib/libm/machine/$(ARCH)/*.c)
VPATH += :newlib/newlib/newlib/libm/machine/$(ARCH)

ifeq ($(CONFIG_ARCH_X86_64),y)
CSRCS += $(wildcard newlib/newlib/newlib/libm/fenv/*.c)
VPATH += :newlib/newlib/newlib/libm/fenv

CFLAGS += ${INCDIR_PREFIX}newlib/newlib/newlib/libc/machine/shared_x86/sys
endif

# aggresive optimisation can replace occurrences of sinl() and cosl() with
# sincosl(), but sincosl() is missing in newlib which causes error. So let's
# use custom implementation here.

CSRCS += newlib/sincosl.c
VPATH += :newlib

ifeq ($(CONFIG_LIBM_NEWLIB_HW_FP),y)
  CSRCS += $(wildcard newlib/newlib/newlib/libm/mathfp/*.c)
  VPATH += :newlib/newlib/newlib/libm/mathfp
  CFLAGS += -Wno-dangling-else
  CFLAGS += -Wno-endif-labels
  CFLAGS += -Wno-implicit-function-declaration
  CFLAGS += -Wno-missing-braces
  CFLAGS += -Wno-shadow
  CFLAGS += -Wno-strict-prototypes
  CFLAGS += -Wno-unused-const-variable
else
  CSRCS += $(wildcard newlib/newlib/newlib/libm/math/*.c)
  VPATH += :newlib/newlib/newlib/libm/math
endif

CSRCS := $(shell echo $(notdir $(CSRCS) | tr " " "\n" | sort | uniq))

CFLAGS += -Wno-undef -Wno-unused-but-set-variable -Wno-unused-const-variable -Wno-maybe-uninitialized
CFLAGS += ${INCDIR_PREFIX}newlib/newlib/newlib/libm/common
CFLAGS += -D__int32_t=int32_t -D__uint32_t=uint32_t -D_REENT=0 -D_REENT_THREAD_LOCAL=1
