srcdir_ = @srcdir@
ifeq ($(src_dir),)
srcdir:=$(shell (cd $(srcdir_); pwd))
else
srcdir:=$(src_dir)
endif
VPATH = $(srcdir)	#: This colon should fool config.status into not
			#: deleting VPATH when srcdir = .
cpu=$(word 1, $(subst ., ,$(CPU-FPU)))
ifeq ($(cpu),5475)
	CPU=-mcpu=$(cpu)
else
	CPU=-m$(cpu)
endif
FLAVOUR=$(addprefix -m,$(wordlist 2,100, $(subst ., ,$(CPU-FPU))))
CC = @CC@
CFLAGS = -pipe -O2 -fomit-frame-pointer $(CPU) $(FLAVOUR) -Werror
prefix = @prefix@
exec_prefix = @exec_prefix@
libdir = $(prefix)/lib
incdir = $(prefix)/include
RANLIB = @RANLIB@
AR = @AR@
ac_aux_dir=@ac_aux_dir@
MKINSTALLDIRS = $(ac_aux_dir)/mkinstalldirs

#
# There are two options in making libm at fdlibm compile time:
# 	_IEEE_LIBM 	--- IEEE libm; smaller, and somewhat faster
#	_MULTI_LIBM	--- Support multi-standard at runtime by 
#			    imposing wrapper functions defined in 
#			    fdlibm.h:
#				_IEEE_MODE 	-- IEEE
#				_XOPEN_MODE 	-- X/OPEN
#				_POSIX_MODE 	-- POSIX/ANSI
#				_SVID3_MODE 	-- SVID
#
# Here is how to set up CFLAGS to create the desired libm at 
# compile time:
#
# 	DEFS = -D_IEEE_LIBM	... IEEE libm (recommended)
#	DEFS = -D_SVID3_MODE	... Multi-standard supported
#					    libm with SVID as the 
#					    default standard
#	DEFS = -D_XOPEN_MODE	... Multi-standard supported
#					    libm with XOPEN as the 
#					    default standard
#	DEFS = -D_POSIX_MODE	... Multi-standard supported
#					    libm with POSIX as the 
#					    default standard
#	DEFS = 			... Multi-standard supported
#					    libm with IEEE as the 
#					    default standard
# 
# NOTE: if scalb's second arguement is an int, then one must
# define _SCALB_INT in DEFS. The default prototype of scalb
# is double scalb(double, double)
#

# Multi-standard supported libm with POSIX as the default standard
DEFS=-D_IEEE_LIBM -D_REENTRANT

# Pick the CPU/FPU combinations you want
# The compiler will prefix -mCPU and -mFPU 
# so 68000.soft-float becomes -m68000 -msoft-float
CPU-FPU-TYPES = \
		68000.soft-float \
		68020-60.68881 \
		5475.hard-float \
		68000.soft-float.short \
		68020-60.68881.short \
		5475.hard-float.short

BUILD_FAST=$(shell if $(CC) -mfastcall -E - < /dev/null >/dev/null 2>&1; then echo Y; else echo N; fi)
ifeq ($(BUILD_FAST), Y)
CPU-FPU-TYPES += \
		68000.soft-float.fastcall \
		68020-60.68881.fastcall \
		5475.hard-float.fastcall \
		68000.soft-float.short.fastcall \
		68020-60.68881.short.fastcall \
		5475.hard-float.short.fastcall
endif

INCFILES = fdlibm.h k_stand.h
SRC = 	$(srcdir)/k_standard.c \
	$(srcdir)/k_rem_pio2.c $(srcdir)/k_rem_pio2f.c $(srcdir)/k_rem_pio2l.c \
	$(srcdir)/k_cos.c $(srcdir)/k_cosf.c $(srcdir)/k_cosl.c \
	$(srcdir)/k_sin.c $(srcdir)/k_sinf.c $(srcdir)/k_sinl.c \
	$(srcdir)/k_tan.c $(srcdir)/k_tanf.c $(srcdir)/k_tanl.c \
	$(srcdir)/e_acos.c $(srcdir)/e_acosf.c $(srcdir)/e_acosl.c \
	$(srcdir)/e_acosh.c $(srcdir)/e_acoshf.c $(srcdir)/e_acoshl.c \
	$(srcdir)/e_asin.c $(srcdir)/e_asinf.c $(srcdir)/e_asinl.c \
	$(srcdir)/e_atan2.c $(srcdir)/e_atan2f.c $(srcdir)/e_atan2l.c \
	$(srcdir)/e_atanh.c $(srcdir)/e_atanhf.c $(srcdir)/e_atanhl.c \
	$(srcdir)/e_cosh.c $(srcdir)/e_coshf.c $(srcdir)/e_coshl.c \
	$(srcdir)/e_exp.c $(srcdir)/e_expf.c $(srcdir)/e_expl.c \
	$(srcdir)/e_exp2.c $(srcdir)/e_exp2f.c $(srcdir)/e_exp2l.c \
	$(srcdir)/e_exp10.c $(srcdir)/e_exp10f.c $(srcdir)/e_exp10l.c \
	$(srcdir)/e_fmod.c $(srcdir)/e_fmodf.c $(srcdir)/e_fmodl.c \
	$(srcdir)/e_hypot.c $(srcdir)/e_hypotf.c $(srcdir)/e_hypotl.c \
	$(srcdir)/e_j0.c $(srcdir)/e_j0f.c $(srcdir)/e_j0l.c \
	$(srcdir)/e_j1.c $(srcdir)/e_j1f.c $(srcdir)/e_j1l.c \
	$(srcdir)/e_jn.c $(srcdir)/e_jnf.c $(srcdir)/e_jnl.c \
	$(srcdir)/e_y0.c $(srcdir)/e_y0f.c $(srcdir)/e_y0l.c \
	$(srcdir)/e_y1.c $(srcdir)/e_y1f.c $(srcdir)/e_y1l.c \
	$(srcdir)/e_yn.c $(srcdir)/e_ynf.c $(srcdir)/e_ynl.c \
	$(srcdir)/s_pzero.c $(srcdir)/s_pzerof.c $(srcdir)/s_pzerol.c \
	$(srcdir)/s_pone.c $(srcdir)/s_pone.c $(srcdir)/s_ponel.c \
	$(srcdir)/e_log.c $(srcdir)/e_logf.c $(srcdir)/e_logl.c \
	$(srcdir)/e_log2.c $(srcdir)/e_log2f.c $(srcdir)/e_log2l.c \
	$(srcdir)/e_log10.c $(srcdir)/e_log10f.c $(srcdir)/e_log10l.c \
	$(srcdir)/e_pow.c $(srcdir)/e_powf.c $(srcdir)/e_powl.c \
	$(srcdir)/e_powi.c $(srcdir)/e_powif.c $(srcdir)/e_powil.c \
	$(srcdir)/e_rem_pio2.c $(srcdir)/e_rem_pio2f.c $(srcdir)/e_rem_pio2l.c \
	$(srcdir)/e_remainder.c $(srcdir)/e_remainderf.c $(srcdir)/e_remainderl.c \
	$(srcdir)/e_scalb.c $(srcdir)/e_scalbf.c $(srcdir)/e_scalbl.c \
	$(srcdir)/e_sinh.c $(srcdir)/e_sinhf.c $(srcdir)/e_sinhl.c \
	$(srcdir)/e_sqrt.c $(srcdir)/e_sqrtf.c $(srcdir)/e_sqrtl.c \
	$(srcdir)/s_asinh.c $(srcdir)/s_asinhf.c $(srcdir)/s_asinhl.c \
	$(srcdir)/s_atan.c $(srcdir)/s_atanf.c $(srcdir)/s_atanl.c \
	$(srcdir)/s_cbrt.c $(srcdir)/s_cbrtf.c $(srcdir)/s_cbrtl.c \
	$(srcdir)/s_ceil.c $(srcdir)/s_ceilf.c $(srcdir)/s_ceill.c \
	$(srcdir)/s_copysign.c $(srcdir)/s_copysignf.c $(srcdir)/s_copysignl.c \
	$(srcdir)/s_cos.c $(srcdir)/s_cosf.c $(srcdir)/s_cosl.c \
	$(srcdir)/s_erf.c $(srcdir)/s_erff.c $(srcdir)/s_erfl.c \
	$(srcdir)/s_erfc.c $(srcdir)/s_erfcf.c $(srcdir)/s_erfcl.c \
	$(srcdir)/s_expm1.c $(srcdir)/s_expm1f.c $(srcdir)/s_expm1l.c \
	$(srcdir)/s_fabs.c $(srcdir)/s_fabsf.c $(srcdir)/s_fabsl.c \
	$(srcdir)/s_finite.c $(srcdir)/s_finitef.c $(srcdir)/s_finitel.c \
	$(srcdir)/s_floor.c $(srcdir)/s_floorf.c $(srcdir)/s_floorl.c \
	$(srcdir)/s_frexp.c $(srcdir)/s_frexpf.c $(srcdir)/s_frexpl.c \
	$(srcdir)/e_ilogb.c $(srcdir)/e_ilogbf.c $(srcdir)/e_ilogbl.c \
	$(srcdir)/s_isnan.c $(srcdir)/s_isnanf.c $(srcdir)/s_isnanl.c \
	$(srcdir)/s_isinf.c $(srcdir)/s_isinff.c $(srcdir)/s_isinfl.c \
	$(srcdir)/s_ldexp.c $(srcdir)/s_ldexpf.c $(srcdir)/s_ldexpl.c \
	$(srcdir)/s_lib_version.c \
	$(srcdir)/s_log1p.c $(srcdir)/s_log1pf.c $(srcdir)/s_log1pl.c \
	$(srcdir)/s_logb.c $(srcdir)/s_logbf.c $(srcdir)/s_logbl.c \
	$(srcdir)/s_matherr.c \
	$(srcdir)/s_modf.c $(srcdir)/s_modff.c $(srcdir)/s_modfl.c \
	$(srcdir)/s_remquo.c $(srcdir)/s_remquof.c $(srcdir)/s_remquol.c \
	$(srcdir)/s_nextafter.c $(srcdir)/s_nextafterf.c $(srcdir)/s_nextafterl.c \
	$(srcdir)/s_nexttoward.c $(srcdir)/s_nexttowardf.c \
	$(srcdir)/s_rint.c $(srcdir)/s_rintf.c $(srcdir)/s_rintl.c \
	$(srcdir)/s_lrint.c $(srcdir)/s_lrintf.c $(srcdir)/s_lrintl.c \
	$(srcdir)/s_llrint.c $(srcdir)/s_llrintf.c $(srcdir)/s_llrintl.c \
	$(srcdir)/s_scalbn.c $(srcdir)/s_scalbnf.c $(srcdir)/s_scalbnl.c \
	$(srcdir)/s_scalbln.c $(srcdir)/s_scalblnf.c $(srcdir)/s_scalblnl.c \
	$(srcdir)/s_signgam.c \
	$(srcdir)/s_significand.c $(srcdir)/s_significandf.c $(srcdir)/s_significandl.c \
	$(srcdir)/s_sin.c $(srcdir)/s_sinf.c $(srcdir)/s_sinl.c \
	$(srcdir)/s_sincos.c $(srcdir)/s_sincosf.c $(srcdir)/s_sincosl.c \
	$(srcdir)/s_tan.c $(srcdir)/s_tanf.c $(srcdir)/s_tanl.c \
	$(srcdir)/s_tanh.c $(srcdir)/s_tanhf.c $(srcdir)/s_tanhl.c \
	$(srcdir)/s_fmin.c $(srcdir)/s_fminf.c $(srcdir)/s_fminl.c \
	$(srcdir)/s_fmax.c $(srcdir)/s_fmaxf.c $(srcdir)/s_fmaxl.c \
	$(srcdir)/s_fdim.c $(srcdir)/s_fdimf.c $(srcdir)/s_fdiml.c \
	$(srcdir)/s_fpclassify.c $(srcdir)/s_fpclassifyf.c $(srcdir)/s_fpclassifyl.c \
	$(srcdir)/s_round.c $(srcdir)/s_roundf.c $(srcdir)/s_roundl.c \
	$(srcdir)/s_lround.c $(srcdir)/s_lroundf.c $(srcdir)/s_lroundl.c \
	$(srcdir)/s_llround.c $(srcdir)/s_llroundf.c $(srcdir)/s_llroundl.c \
	$(srcdir)/s_trunc.c $(srcdir)/s_truncf.c $(srcdir)/s_truncl.c \
	$(srcdir)/s_issig.c $(srcdir)/s_issigf.c $(srcdir)/s_issigl.c \
	$(srcdir)/s_nearbyint.c $(srcdir)/s_nearbyintf.c $(srcdir)/s_nearbyintl.c \
	$(srcdir)/t_sincosl.c \
	\
	$(srcdir)/e_lgamma.c $(srcdir)/e_lgammaf.c $(srcdir)/e_lgammal.c \
	$(srcdir)/e_lgamma_r.c $(srcdir)/e_lgammaf_r.c $(srcdir)/e_lgammal_r.c \
	$(srcdir)/e_tgamma_r.c $(srcdir)/e_tgammaf_r.c $(srcdir)/e_tgammal_r.c \
	$(srcdir)/w_gamma.c $(srcdir)/w_gammaf.c $(srcdir)/w_gammal.c \
	$(srcdir)/w_gamma_r.c $(srcdir)/w_gammaf_r.c $(srcdir)/w_gammal_r.c \
	$(srcdir)/w_lgamma.c $(srcdir)/w_lgammaf.c $(srcdir)/w_lgammal.c \
	$(srcdir)/w_lgamma_r.c $(srcdir)/w_lgammaf_r.c $(srcdir)/w_lgammal_r.c \
	$(srcdir)/w_tgamma.c $(srcdir)/w_tgammaf.c $(srcdir)/w_tgammal.c \
	\
	$(srcdir)/fpu_ctrl.c \
	$(srcdir)/fesetround.c \
	$(srcdir)/fegetround.c \
	$(srcdir)/fesetenv.c \
	$(srcdir)/fegetenv.c \
	$(srcdir)/feholdexcept.c \
	$(srcdir)/feupdateenv.c \
	$(srcdir)/feclearexcept.c \
	$(srcdir)/fedisableexcept.c \
	$(srcdir)/feenableexcept.c \
	$(srcdir)/fegetexcept.c \
	$(srcdir)/fegetexceptflag.c \
	$(srcdir)/feraiseexcept.c \
	$(srcdir)/fesetexceptflag.c \
	$(srcdir)/fetestexcept.c \
	\
	$(srcdir)/truncxfdf2.c \
	\
	$(empty)

OBJ = 	$(addsuffix .o, $(basename $(notdir $(SRC))))

.c.o:
	$(CC) -c $(DEFS) -I$(srcdir)/include $(CFLAGS) $< $(OUTPUT_OPTION)

all:
	for i in $(CPU-FPU-TYPES); do \
	  if [ ! -d $$i ] ; then \
	    mkdir $$i; \
	  else true; fi; \
	  (cd $$i; $(MAKE) -f ../Makefile CPU-FPU=$$i src_dir=$(srcdir) $(LIB)); \
	done

LIB = libm.a

$(LIB): $(OBJ) 
	$(AR) cru $(LIB) $(OBJ)
	$(RANLIB) $(LIB)

install: all install-headers
	@for i in $(CPU-FPU-TYPES); do \
	  if [ -d $$i ] ; then \
            cpu=`echo $$i | cut -d . -f 1`; \
            if test $$cpu = 5475; then cpu=-mcpu=$$cpu; else cpu=-m$$cpu; fi; \
            flavour=`echo "$$i" | sed -e 's/\./ -m/g' -e 's/[^ ]* //'`; \
            multilibdir=`$(CC) $$cpu $$flavour -print-multi-directory`; \
	    $(MKINSTALLDIRS) $(DESTDIR)$(libdir)/$$multilibdir; \
	    echo cp $$i/libm.a $(DESTDIR)$(libdir)/$$multilibdir/libm.a; \
	    cp $$i/libm.a $(DESTDIR)$(libdir)/$$multilibdir/libm.a; \
	  fi; \
        done

install-headers:
	mkdir -p $(DESTDIR)$(incdir)/bits/m68k
	for i in math.h fenv.h fpu_control.h; do \
		cp -a $(srcdir)/include/$$i $(DESTDIR)$(incdir) || exit 1; \
	done
	for i in fenv.h fenvinline.h huge_val.h inf.h math-68881.h math-cffpu.h mathcall.h mathdef.h nan.h fpu_control.h; do \
		cp -a $(srcdir)/include/bits/$$i $(DESTDIR)$(incdir)/bits || exit 1; \
	done
	for i in fenv.h fpu_control.h; do \
		cp -a $(srcdir)/include/bits/m68k/$$i $(DESTDIR)$(incdir)/bits/m68k || exit 1; \
	done

Makefile: $(srcdir)/Makefile.in config.status
	./config.status

check: all
	$(MAKE) -C tests $@

clean:
	$(MAKE) -C tests $@
	rm -rf a.out $(CPU-FPU-TYPES)

distclean: clean
	$(MAKE) -C tests $@
	rm -f config.cache config.status config.log Makefile
	rm -rf autom4te.cache

$(OBJ): $(srcdir)/include/fdlibm.h
