# Makefile for uClibc
#
# Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
#
# Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
#
# The routines included in this math library are derived from the
# math library for Apple's MacOS X/Darwin math library, which was
# itself swiped from FreeBSD.  The original copyright information
# is as follows:
#
#     Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
#
#     Developed at SunPro, a Sun Microsystems, Inc. business.
#     Permission to use, copy, modify, and distribute this
#     software is freely granted, provided that this notice
#     is preserved.
#
# It has been ported to work with uClibc and generally behave
# by Erik Andersen <andersen@codepoet.org>
#

CFLAGS-libm := -DNOT_IN_libc -DIS_IN_libm $(SSP_ALL_CFLAGS)
CFLAGS-libm += -D_IEEE_LIBM

LDFLAGS-libm.so := $(LDFLAGS)

LIBS-libm.so := $(LIBS)

libm_FULL_NAME := libm-$(VERSION).so

libm_DIR:=$(top_srcdir)libm
libm_OUT:=$(top_builddir)libm

# Fix builds for powerpc as there are different cores in this 
# section now.`
ifeq ($(TARGET_ARCH),powerpc)
ifeq ($(CONFIG_E500),y)
libm_ARCH_DIR:=$(libm_DIR)/$(TARGET_ARCH)/e500
libm_ARCH_OUT:=$(libm_OUT)/$(TARGET_ARCH)/e500
else
libm_ARCH_DIR:=$(libm_DIR)/$(TARGET_ARCH)/classic
libm_ARCH_OUT:=$(libm_OUT)/$(TARGET_ARCH)/classic
endif
else
libm_ARCH_DIR:=$(libm_DIR)/$(TARGET_ARCH)
libm_ARCH_OUT:=$(libm_OUT)/$(TARGET_ARCH)
endif

libm_ARCH_fpu_DIR:=$(libm_ARCH_DIR)/fpu
libm_ARCH_fpu_OUT:=$(libm_ARCH_OUT)/fpu

ifeq ($(UCLIBC_HAS_FPU),y)
ifeq ($(DO_C99_MATH),y)
-include $(libm_ARCH_DIR)/Makefile.arch
endif
endif

FL_MSRC := float_wrappers.c

ifeq ($(DO_C99_MATH),y)
libm_CSRC := \
	e_acos.c e_acosh.c e_asin.c e_atan2.c e_atanh.c e_cosh.c \
	e_exp.c e_fmod.c e_gamma.c e_gamma_r.c e_hypot.c e_j0.c \
	e_j1.c e_jn.c e_lgamma.c e_lgamma_r.c e_log.c e_log10.c \
	e_pow.c e_remainder.c e_rem_pio2.c e_scalb.c e_sinh.c \
	e_sqrt.c k_cos.c k_rem_pio2.c k_sin.c k_standard.c k_tan.c \
	s_asinh.c s_atan.c s_cbrt.c s_ceil.c s_copysign.c s_cos.c \
	s_erf.c s_expm1.c s_fabs.c s_finite.c s_floor.c s_frexp.c \
	s_ilogb.c s_ldexp.c s_lib_version.c s_lrint.c s_lround.c s_llround.c \
	s_log1p.c s_logb.c s_matherr.c s_modf.c s_nextafter.c s_round.c \
	s_rint.c s_scalbn.c s_signgam.c s_significand.c s_sin.c s_tan.c \
	s_tanh.c s_trunc.c w_acos.c w_acosh.c w_asin.c w_atan2.c w_atanh.c \
	w_cabs.c w_cosh.c w_drem.c w_exp.c w_fmod.c w_gamma.c w_gamma_r.c \
	w_hypot.c w_j0.c w_j1.c w_jn.c w_lgamma.c w_lgamma_r.c \
	w_log.c w_log10.c w_pow.c w_remainder.c w_scalb.c w_sinh.c \
	w_sqrt.c fpmacros.c nan.c carg.c s_llrint.c
FL_MOBJ := \
	acosf.o acoshf.o asinf.o asinhf.o atan2f.o atanf.o atanhf.o cbrtf.o \
	ceilf.o copysignf.o cosf.o coshf.o erfcf.o erff.o exp2f.o expf.o \
	expm1f.o fabsf.o fdimf.o floorf.o fmaf.o fmaxf.o fminf.o fmodf.o \
	frexpf.o hypotf.o ilogbf.o ldexpf.o lgammaf.o llroundf.o log10f.o \
	log1pf.o log2f.o logbf.o logf.o lrintf.o lroundf.o modff.o nearbyintf.o \
	nextafterf.o powf.o remainderf.o remquof.o rintf.o roundf.o \
	scalblnf.o scalbnf.o sinf.o sinhf.o sqrtf.o tanf.o tanhf.o \
	tgammaf.o truncf.o cargf.o llrintf.o
else
# This list of math functions was taken from POSIX/IEEE 1003.1b-1993
libm_CSRC := \
	w_acos.c w_asin.c s_atan.c w_atan2.c s_ceil.c s_cos.c \
	w_cosh.c w_exp.c s_fabs.c s_floor.c w_fmod.c s_frexp.c \
	s_ldexp.c w_log.c w_log10.c s_modf.c w_pow.c s_sin.c \
	w_sinh.c w_sqrt.c s_tan.c s_tanh.c \
	s_expm1.c s_scalbn.c s_copysign.c e_acos.c e_asin.c e_atan2.c \
	k_cos.c e_cosh.c e_exp.c e_fmod.c e_log.c e_log10.c e_pow.c \
	k_sin.c e_sinh.c e_sqrt.c k_tan.c e_rem_pio2.c k_rem_pio2.c \
	s_finite.c
# We'll add sqrtf to avoid problems with libstdc++
FL_MOBJ := sqrtf.o
endif

# assume that arch specific versions are provided as single sources/objects
ifeq ($(UCLIBC_HAS_FPU),y)
ifeq ($(DO_C99_MATH),y)
ifneq ($(strip $(libm_ARCH_OBJS)),)
ifeq ($(TARGET_ARCH),powerpc)
ifeq ($(CONFIG_E500),y)
CFLAGS-libm/$(TARGET_ARCH)/e500/ := $(CFLAGS-libm)
else
CFLAGS-libm/$(TARGET_ARCH)/classic/ := $(CFLAGS-libm)
endif
else
CFLAGS-libm/$(TARGET_ARCH)/ := $(CFLAGS-libm)
endif

# remove generic sources, if arch specific version is present
ifneq ($(strip $(libm_ARCH_SRC)),)
libm_CSRC := $(filter-out $(notdir $(libm_ARCH_SRC)),$(libm_CSRC))
endif

# remove generic objects built from multi-sources, if arch specific version is present
FL_MOBJ := $(filter-out $(notdir $(libm_ARCH_OBJS)),$(FL_MOBJ))

# we also try to remove % if s_% is in arch specific subdir
FL_MOBJ := $(filter-out $(patsubst s_%.o,%.o,$(notdir $(libm_ARCH_OBJS))),$(FL_MOBJ))
endif
endif
endif

libm_SRC := $(patsubst %.c,$(libm_DIR)/%.c,$(libm_CSRC))
libm_OBJ := $(patsubst $(libm_DIR)/%.c,$(libm_OUT)/%.o,$(libm_SRC))

libm_MSRC := $(libm_DIR)/$(FL_MSRC)
libm_MOBJ := $(patsubst %.o,$(libm_OUT)/%.o,$(FL_MOBJ))

ifneq ($(DOMULTI),n)
CFLAGS-libm += $(patsubst %,-DL_%,$(subst .o,,$(notdir $(libm_MOBJ))))
endif

libm_OBJS := $(libm_OBJ) $(libm_MOBJ)

ifeq ($(DOPIC),y)
libm-a-y += $(libm_OBJS:.o=.os)
else
libm-a-y += $(libm_OBJS)
endif
libm-so-y += $(libm_OBJS:.o=.os)

lib-a-$(UCLIBC_HAS_FLOATS) += $(top_builddir)lib/libm.a
lib-so-$(UCLIBC_HAS_FLOATS) += $(top_builddir)lib/libm.so
objclean-y += libm_clean

ifeq ($(DOMULTI),n)
ifeq ($(DOPIC),y)
$(top_builddir)lib/libm.so: $(top_builddir)lib/libm.a $(libc.depend)
else
$(top_builddir)lib/libm.so: $(libm_OUT)/libm_so.a $(libc.depend)
endif
	$(call link.so,$(libm_FULL_NAME),$(MAJOR_VERSION))
else
$(top_builddir)lib/libm.so: $(libm_OUT)/libm.oS | $(libc.depend)
	$(call linkm.so,$(libm_FULL_NAME),$(MAJOR_VERSION))
endif

$(libm_OUT)/libm_so.a: $(libm-so-y)
	$(Q)$(RM) $@
	$(do_ar)

$(libm_OUT)/libm.oS: $(libm_SRC) $(libm_MSRC) $(libm_ARCH_SRC)
	$(Q)$(RM) $@
	$(compile-m)

$(top_builddir)lib/libm.a: $(libm-a-y)
	$(Q)$(INSTALL) -d $(dir $@)
	$(Q)$(RM) $@
	$(do_ar)

$(libm_MOBJ): $(libm_MSRC)
	$(compile.m)

$(libm_MOBJ:.o=.os): $(libm_MSRC)
	$(compile.m)

libm_clean:
	$(RM) $(libm_OUT)/{,*/,*/*/}*.{o,os,oS,a}