CC			= arm-none-eabi-gcc
LD			= arm-none-eabi-gcc
OBJCOPY		= arm-none-eabi-objcopy
LKR			= arm_lib.lkr
LTO			= -flto
WARNINGS	= -Wno-multichar -Wall -ffreestanding -Wno-strict-aliasing -Wno-parentheses -Wno-maybe-uninitialized -Wno-comment -Wno-misleading-indentation
COMMON		= $(LTO) -Os -g -ggdb3 -fpic $(WARNINGS) -ffixed-r9
CCFLAGS		= $(COMMON) -I. -ffunction-sections -fdata-sections -DUSE_TRAPS=0
LDFLAGS		= $(COMMON) -Wl,--gc-sections -Wl,-T $(LKR)

#arch
CRTGENARCH	= v7m
GCCARCH		= -march=armv7e-m -mcpu=cortex-m4 -mthumb
#CRTGENARCH	= v4
#GCCARCH		= -march=armv4t -marm


RSRC		= amdc0000
PRC			= MathLibArm.prc
SRCS		+= pace.S main.c libpalmos.c


#add SDK
CCFLAGS		+=  -I../../SDK
CCFLAGS		+=  -I../../SDK/Core
CCFLAGS		+=  -I../../SDK/Core/System
CCFLAGS		+=  -I../../SDK/Core/Hardware
CCFLAGS		+=  -I../../SDK/Core/UI
CCFLAGS		+=  -I../../SDK/Dynamic
CCFLAGS		+=  -I../../SDK/libraries

#add repalm
CCFLAGS		+=  -isystem ../dal
EXTRA_RSRCS	+= amdi0000.bin tver03e8.bin

#add math
SRCS		+= math/e_acos.c math/e_acosh.c math/e_asin.c math/e_atan2.c math/e_atanh.c math/e_cosh.c math/e_exp.c math/e_fmod.c math/e_hypot.c math/e_log.c math/e_log10.c math/e_pow.c math/e_remainder.c math/e_rem_pio2.c math/e_scalb.c math/e_sinh.c math/e_sqrt.c math/k_cos.c math/k_rem_pio2.c math/k_sin.c math/k_tan.c math/s_asinh.c math/s_atan.c math/s_cbrt.c math/s_ceil.c math/s_copysign.c math/s_cos.c math/s_expm1.c math/s_fabs.c math/s_finite.c math/s_floor.c math/s_frexp.c math/s_ilogb.c math/s_isinf.c math/s_isnan.c math/s_ldexp.c math/s_log1p.c math/s_log2.c math/s_logb.c math/s_modf.c math/s_nextafter.c math/s_rint.c math/s_round.c math/s_scalbn.c math/s_signbit.c math/s_significand.c math/s_sin.c math/s_sincos.c math/s_tan.c math/s_tanh.c math/s_trunc.c

#define funcs for ourselves
CCFLAGS		+= -DBUILDING_MATHLIB



#do not touch below
OBJS		= $(patsubst %.S,%.o,$(patsubst %.c,%.o,$(SRCS)))
INCS		= $(wildcard *.h)

.SECONDARY:

all: MathLibWithExports.prc MathLibNoExports.prc

%.prc: %.sbin $(EXTRA_RSRCS)
	../../tools/mkrom/mkprc MathLib libr MthL $(EXTRA_RSRCS) amdc0000=$< > $@

%.sbin: %.elf
	$(OBJCOPY) -I elf32-littlearm -O binary $< $@ -j.vec -j.text -j.rodata -j.data

%.elf: %.crt.S $(OBJS) $(LKR)
	$(LD) -o $@ $(LDFLAGS) $(GCCARCH) $(OBJS) $<

libpalmos.c: ../dal/libpalmos.c
	cp $< $@

%.o : %.c $(INCS) Makefile
	$(CC) $(CCFLAGS) $(GCCARCH) -c $< -o $@
	
%.o : %.S Makefile
	$(CC) $(CCFLAGS) $(GCCARCH) -c $< -o $@

pace.S: Makefile ../../tools/libtools/PACE.libdescr
	echo "#define _ASSEMBLY\n#include \"common.h\"\n#define LIB_SLOT_IN_GLOBALS 0x10" > pace.S
	../../tools/libtools/inclgen.sh $(CRTGENARCH) ../../tools/libtools/PACE.libdescr >> pace.S

%.crt.S: %.libdescr
	../../tools/libtools/crtgen.sh $(CRTGENARCH) $< >$@

clean:
	rm -f $(OBJS) *.sbin *.elf *.prc *.crt.S pace.S libpalmos.c


