# arm-none-eabi-gdb -i=mi -ex "target remote localhost:3333" giac.elf
# disable quickjs/flto to have a smaller executable in gdb and fast compilation
# Makefile for nspire
# for CX II activate --use-lcd-blit true, for CX HW-W do not activate
# ?? try --mthumb-interwork -mthumb
# adjust path to your install and
# make sure you have done
# /bin/cp config.h.nspire config.h
# before compiling with make -f Makefile.nspire
GCC = nspire-gcc
CXX = nspire-g++
LD = nspire-ld
FLTO = -flto
#FLTO =
# with -DKHICAS check that kdisplay.o kadd.o k_csdk.o are in GIACOBJS
#GCCFLAGS = -g -Os -marm -I/Users/parisse/Ndless/ndless-sdk/toolchain/install/include  -fno-strict-aliasing -DHAVE_CONFIG_H -I. -I..  -DIN_GIAC -DTIMEOUT -DNSPIRE_NEWLIB  -DNO_PHYSICAL_CONSTANTS -DNO_STDEXCEPT -DSTATIC_BUILTIN_LEXER_FUNCTIONS -DGIAC_BINARY_ARCHIVE -DNO_UNARY_FUNCTION_COMPOSE -DTIMEOUT -fno-exceptions # -DGIAC_GENERIC_CONSTANTS -ffunction-sections -fdata-sections
GCCFLAGS = -g -Os -marm  -I/Users/parisse/Ndless-r2015/ndless-sdk/toolchain/install/include  -fno-strict-aliasing -DHAVE_CONFIG_H -I. -I..   -DIN_GIAC -DTIMEOUT -DNSPIRE_NEWLIB -DKHICAS -DNO_PHYSICAL_CONSTANTS -DSTATIC_BUILTIN_LEXER_FUNCTIONS -DGIAC_BINARY_ARCHIVE -DNO_UNARY_FUNCTION_COMPOSE -DTIMEOUT -DNO_STDEXCEPT -fno-exceptions -DMICROPY_LIB -D_GNU_SOURCE -DCONFIG_VERSION=\"2020-11-08\" -DCONFIG_BIGNUM  -DQUICKJS  $(FLTO) # -DBF2GMP # -DNSPIRE_LED # -DGIAC_GENERIC_CONSTANTS -ffunction-sections -fdata-sections
LDFLAGS = -g -Wl,--nspireio,--gc-sections -L/Users/parisse/Ndless-r2015/ndless-sdk/toolchain/install/lib $(FLTO) -lmicropy -lmpfi -lmpfr -lgmp  # 
#GCCFLAGS =  -marm -I~/Ndless/ndless-sdk/include -Os -fno-strict-aliasing -DGIAC_GENERIC_CONSTANTS -DHAVE_CONFIG_H -I. -I..  -DIN_GIAC -DNO_PHYSICAL_CONSTANTS -DHAVE_CONFIG_H -DNO_STDEXCEPT -DSTATIC_BUILTIN_LEXER_FUNCTIONS -DGIAC_BINARY_ARCHIVE -DNO_UNARY_FUNCTION_COMPOSE -DTIMEOUT -DNO_TEMPLATE_MULTGCD -DUSE_GMP_REPLACEMENTS -DNSPIRE_NEWLIB
#LDFLAGS = -Wl,--nspireio,--gc-sections -ltommath
GENZEHN = genzehn

EXE = luagiac.luax.tns

OBJS = sym2poly.o gausspol.o threaded.o maple.o ti89.o mathml.o moyal.o misc.o permu.o desolve.o input_parser.o symbolic.o index.o modpoly.o modfactor.o ezgcd.o derive.o solve.o intg.o intgab.o risch.o lin.o series.o subst.o vecteur.o sparse.o csturm.o tex.o global.o ifactor.o alg_ext.o gauss.o isom.o help.o plot.o plot3d.o rpn.o prog.o pari.o cocoa.o unary.o usual.o identificateur.o gen.o input_lexer.o tinymt32.o first.o quater.o kdisplay.o k_csdk.o kadd.o quickjs.o libregexp.o libunicode.o cutils.o quickjs-libc.o libbf.o qjscalcjs.o caseval.o graphic.o tijs.o qjsgiac.o # sha256.o
# sha256 is already available in micropython (struct SHA256_CTX renamed as CRYAL_SHA256_CTX)
DISTDIR = .
vpath %.tns $(DISTDIR)

all: $(EXE) khicaslua.tns khicas.tns khicas.zip

.cc.o:
	$(CXX) $(GCCFLAGS) -c $<

.c.o:
	$(GCC) $(GCCFLAGS) -c $<

tijs.o: tijs.js
	arm-none-eabi-objcopy --input binary --output arm-none-eabi --binary-architecture arm $< $@

qjscalcjs.o: qjscalcjs.js
	arm-none-eabi-objcopy --input binary --output arm-none-eabi --binary-architecture arm $< $@

luagiac.o: luagiac.c
	$(GCC) -Os -Wall -W -marm -c $<

missing.o: missing.c
	$(GCC) -Os -Wall -W -marm -c $<

luagiac.luax.elf: $(OBJS)  luagiac.o luabridge.o 
	$(LD) $^ -o $(DISTDIR)/$@ $(LDFLAGS)

luagiac.luax.tns.zehn: luagiac.luax.elf
	$(GENZEHN) --compress --input $(DISTDIR)/$^ --output $(DISTDIR)/$@ --name "luagiac"  --uses-lcd-blit true # --240x320-support true
#	$(GENZEHN)  --input $(DISTDIR)/$^ --output $(DISTDIR)/$@ --name "luagiac" --uses-lcd-blit true

luagiac.luax.tns: luagiac.luax.tns.zehn
	make-prg $(DISTDIR)/$^ $(DISTDIR)/$@
	genzehn --info --input luagiac.luax.tns  
	/bin/cp luagiac.luax.tns khicas.tns  /shared/ndless/ti/ndless
	echo "Run buildsha ndless/* to update sha256 fingerprint keys"

clean:
	rm -f *.o luagiac.luax.* 

khicaslua.tns: khicas.lua
	luna khicas.lua khicaslua.tns
	/bin/cp khicaslua.tns /shared/ndless/ti/ndless

ptt.tns: ptt.o
	nspire-ld -o ptt.elf ptt.o $(LDFLAGS) 
	$(GENZEHN) --compress --input ptt.elf --output ptt.tns.zehn --name "giac"  --uses-lcd-blit true #--240x320-support  true
# without compression
#	$(GENZEHN) --input giac.elf --output khicas.tns.zehn --name "giac"  --uses-lcd-blit true
	make-prg ptt.tns.zehn ptt.tns
	genzehn --info --input ptt.tns
	/bin/cp ptt.tns /shared/ndless/ti/ndless

# run make twice, def/undef FIREBIRDEMU in k_csdk.c and copy to khicas.tns/khicasemu.tns
khicas.tns:	$(OBJS) giacnspire.o #missing.o
	nspire-ld -o giac.elf giacnspire.o $(OBJS) $(LDFLAGS) 
	$(GENZEHN) --compress --input giac.elf --output khicas.tns.zehn --name "giac"  --uses-lcd-blit true #--240x320-support  true
# without compression
#	$(GENZEHN) --input giac.elf --output khicas.tns.zehn --name "giac"  --uses-lcd-blit true --240x320-support true
	make-prg khicas.tns.zehn khicas.tns
	genzehn --info --input khicas.tns
	/bin/cp khicas.tns /shared/ndless/ti/ndless/khicas.tns
	/bin/cp luagiac.luax.tns khicas.tns  /shared/ndless/ti/ndless
	firebird-send khicas.tns /ndless
	echo "Run buildsha ndless/* to update sha256 fingerprint keys"
# Warning: if the size of khicas.tns is too large, debugging does not work
# the limit should be changed in  Ndless/ndless/src/resources/ndless.h
# recompile (make clean && make) and send ndless to the emulator
# firebird-send calcbin/ndless_installer_4.5.0.tns /ndless
# firebird-send calcbin/ndless_resources.tns /ndless


libgiac.a:   $(OBJS) 
	arm-none-eabi-ar rcs libgiac.a $(OBJS) 
	arm-none-eabi-ranlib libgiac.a

khicas.zip: luagiac.luax.tns khicas.tns 
	zip khicas.zip luagiac.luax.tns khicas.tns khicas.lua
