TOP=..

include $(TOP)/configure/CONFIG


CROSS_COMPILER_TARGET_ARCHS=$(BPM_CROSS_ARCHS)
#----------------------------------------
#  ADD MACRO DEFINITIONS AFTER THIS LINE
#=============================
FFTW_VERS=fftw-3.2.2
FFTW_SRCDIR=../$(FFTW_VERS)

LIBNAM=$(LIB_PREFIX)fftw3f$(LIB_SUFFIX)
LIBTGT=box/.libs/$(LIBNAM)

LIB_INSTALLS+=$(LIBTGT)
INC=fftw3.h

TARGETS=box/Makefile box/env.log $(LIBTGT)

# ugly hacks to map EPICS cpu/os names to gnu/configure 
# !!! THIS IS NOT VERY SOPHISTICATED !!!
#
GNU_CPU_RTEMS=$(RTEMS_CPU)
GNU_HOST_RTEMS=rtems
GNU_CPU_Linux=$(ARCH_CLASS)
GNU_HOST_Linux=linux

CONFIG_ARG_RTEMS_powerpc=--enable-altivec --with-gcc-arch=7450
# RTEMS gcc defines __PPC__ but fftw3 wants __powerpc__
CPPFLGS_powerpc=-D__powerpc__=1

CONFIG_ARG_RTEMS_i386=$(CONFIG_ARG_SIMD) --with-gcc-arch=pentium

CONFIG_ARG_ppc=$(CONFIG_ARG_powerpc)

CONFIG_ARG_Linux_x86='--enable-sse' --with-gcc-arch=pentium
#CONFIG_ARG_Linux_x86+=CC='gcc -m32'
CC_OPT_x86=' -m32'

CONFIG_ARG_x86_64=$(CONFIG_ARG_x86)

CONFIG_ARG_SIMD=$(shell $(COMPILE.c) -E -P ../simdtst.h)

# Crude hack for RTEMS; fftw3 configure has no option
# for disabling the programs in 'test' and 'tools' so
# we must build them even if we only want the library.
# Due to RTEMS specialities the binaries only link
# w/o error if we enforce linking 'main'...
CONFIG_ARG_RTEMS=LDFLAGS=-Wl,-u,main

GNU_CPU=$(GNU_CPU_$(OS_CLASS))
GNU_HOST=$(GNU_CPU)-$(GNU_HOST_$(OS_CLASS))

FFTW_CONFIG_ARGS= --enable-shared --enable-single --host=$(GNU_HOST) --disable-fortran --with-our-malloc16 $(CONFIG_ARG_$(GNU_CPU)) $(CONFIG_ARG_$(OS_CLASS))
FFTW_CONFIG_ARGS+=$(CONFIG_ARG_$(OS_CLASS)_$(GNU_CPU))

# FFTW's i386 stack align hack (kernel/ifftw.h) actually broke
# things for me (RTEMS).
# Remove by passing CPPFLAGS
CPPFLGS_RTEMS_i386=-DWITH_ALIGNED_STACK\(x\)={x}

FFTW_CONFIG_ARGS+= CPPFLAGS='$(CPPFLGS_$(GNU_CPU)) $(CPPFLGS_$(OS_CLASS)) $(CPPFLGS_$(OS_CLASS)_$(GNU_CPU))'
FFTW_CONFIG_ARGS+= CC='$(CC)'$(CC_OPT_$(OS_CLASS))$(CC_OPT_$(GNU_CPU))$(CC_OPT_$(OS_CLASS)_$(GNU_CPU))


#==================================================
# Build an IOC support library

include $(TOP)/configure/RULES
#----------------------------------------
#  ADD RULES AFTER THIS LINE
#
$(FFTW_SRCDIR):
	$(error "FFTW3 sources not found; unpack $(FFTW_VERS) in srcdir")

box/Makefile: $(FFTW_SRCDIR)
	if ! [ -d box ] ; then mkdir box ; fi
	cd box ; ../$(FFTW_SRCDIR)/configure $(FFTW_CONFIG_ARGS)

box/env.log:
	if ! [ -d box ] ; then mkdir box ; fi
	echo "CC: $(CC)" >> $@; echo "CFLAGS: $(CFLAGS)" >> $@; printenv >>$@

$(LIBTGT): gnu_check box/Makefile
	$(MAKE) -C box all

../O.Common/fftw3.h: $(FFTW_SRCDIR)/api/fftw3.h
	ln -s $^ $@

gnu_check:
ifdef T_A
	@if [ "$(GNU_HOST)"x = x ] ; then \
		echo "Error: unable to determine GNU_HOST for OS_CLASS: $(OS_CLASS), T_A: $(T_A); please add proper definitions to Makefile" ; exit 1; \
	fi
	@if [ "$(GNU_CPU)"x = x ] ; then \
		echo "Error: unable to determine GNU_CPU for OS_CLASS: $(OS_CLASS), T_A: $(T_A); please add proper definitions to Makefile" ; exit 1; \
	fi
endif

.PHONY: gnu_check

simd_check:
	echo $(CONFIG_ARG_SIMD)

.PHONY: simd_check
