# pssh ARM makefile

all: armc.ro


OPTIMIZATION = -Os

WARNINGS = -W -Wall -Wmissing-declarations -Wmissing-prototypes -Wpointer-arith -Wno-multichar

# libc and libgcc (must be either ARM with thumb-interwork, or Thumb)
LIBC = ../external-libs/libc.a
LIBGCC = ../external-libs/libgcc.a

# Peal
PEAL = ../../peal
PEAL_SRCS = util/pealstub.c
PEAL_FILES = util/pealstub.c util/pealstub.h
util/pealstub.c: $(PEAL)/arm/pealstub.c
	cp $(PEAL)/arm/pealstub.c util/pealstub.c
util/pealstub.h: $(PEAL)/arm/pealstub.h
	cp $(PEAL)/arm/pealstub.h util/pealstub.h

# OpenSSL
BN_SRCS = bn/bn_armcalls.c bn/bn_add.c bn/bn_asm.c bn/bn_ctx.c bn/bn_div.c bn/bn_exp.c bn/bn_gcd.c bn/bn_lib.c bn/bn_mod.c bn/bn_mont.c bn/bn_mul.c bn/bn_print.c bn/bn_rand.c bn/bn_recp.c bn/bn_shift.c bn/bn_sqr.c bn/bn_word.c
AES_SRCS = aes/aes_armcalls.c aes/aes_cbc.c aes/aes_core.c aes/aes_tables.c

OPENSSL_SRCS = $(addprefix openssl/, \
	$(BN_SRCS) $(AES_SRCS) \
)

# Non-OpenSSL crypto (sha2, Fortuna)
FORTUNA_SRCS = fortuna/fortuna.c fortuna/fortuna_acc.c fortuna/fortuna_gen.c
SHA2_SRCS = sha2/sha2.c

# crypto (OpenSSL and otherwise)
CRYPTO_SRCS = $(addprefix crypto/, \
	rand.c rand_armcalls.c $(FORTUNA_SRCS) $(OPENSSL_SRCS) $(SHA2_SRCS) \
)

# OpenSSH
OPENSSH_SRCS = $(addprefix openssh/, \
	openbsd-compat/bsd-snprintf.c \
)

# ssh (OpenSSH and putty and custom)
SSH_SRCS = $(addprefix ssh/, \
	ssh.c sshzlib.c sshzlib_armcalls.c $(OPENSSH_SRCS) \
)

# util
UTIL_SRCS = $(addprefix util/, \
	PalmOS.c stdlib.c xmalloc.c \
)

# vt100 (PuTTY)
VT100_SRCS = $(addprefix vt100/, \
	tree234.c wcwidth.c misc.c \
	palm/palmprint.c palm/palmucs.c palm/palmwindow.c \
	vt100.c vt100_armcalls.c terminal.c \
)

# form stuff
FORM_SRCS = $(addprefix forms/, \
	form_armcalls.c \
)


# Palm data
DATA_SRCS = data/prefs.c

# Everything ARM
C_SRCS = $(VT100_SRCS) $(DATA_SRCS) $(UTIL_SRCS) $(CRYPTO_SRCS) $(FORM_SRCS) $(SSH_SRCS) $(PEAL_SRCS)
ASM_SRCS = crypto/rand_time.S
SRCS = $(C_SRCS) $(ASM_SRCS)

OBJECTS = $(addprefix build/,$(C_SRCS:.c=.o) $(ASM_SRCS:.S=.o))

OBJDIRS = $(addprefix build/,$(sort $(dir $(SRCS) )))

armc.ro: pssh-arm-prelink export-list
	$(PEAL)/postlink/peal-postlink -K export-list -s 1000 -v -o armc.ro pssh-arm-prelink
	$(PEAL)/postlink/peal-postlink -K export-list -v -o armc.unsplit pssh-arm-prelink

$(OBJDIRS): 
	mkdir -p $@

pssh-arm-prelink: $(PEAL_FILES) $(OBJDIRS) $(OBJECTS) $(LIBC) $(LIBGCC)
	arm-palmos-gcc -mthumb -nostartfiles -nostdlib  -Wl,--split-by-file=64000 -Wl,--emit-relocs $(OBJECTS) $(LIBC) $(LIBGCC) -o pssh-arm-prelink


define compile
	arm-palmos-gcc -fPIC -mthumb -msingle-pic-base $(OPTIMIZATION) $(WARNINGS) \
	   -I. -Iutil -I/usr/lib/gcc-lib/arm-palmos/3.3.1/include/ $(addprefix -I,$1) \
	   -c $2 -o $3
endef



build/crypto/sha2/%.o  : crypto/sha2/%.c
	$(call compile, crypto crypto/sha2,  $<, $@ )

build/crypto/fortuna/%.o  : crypto/fortuna/%.c
	$(call compile, crypto crypto/fortuna,  $<, $@ )

build/crypto/rand_time.o : crypto/rand_time.S
	$(call compile, crypto, $<, $@)

build/crypto/rand.o : crypto/rand.c
	$(call compile, crypto, $<, $@)

build/crypto/%.o : crypto/%.c
	$(call compile, crypto, $<, $@)


build/ssh/openssh/openbsd-compat/%.o  : ssh/openssh/openbsd-compat/%.c
	$(call compile, ssh ssh/openssh ssh/openssh/openbsd-compat,  $<, $@ )

build/ssh/openssh/%.o  : ssh/openssh/%.c
	$(call compile, ssh ssh/openssh crypto,  $<, $@ )

build/ssh/%.o  : ssh/%.c
	$(call compile, ssh crypto vt100,  $<, $@ )


build/data/%.o  : data/%.c
	$(call compile, data,  $<, $@ )


build/util/pealstub.o  : util/pealstub.c
	$(call compile, ,  $<, $@ -mcallee-super-interworking)

build/util/%.o  : util/%.c
	$(call compile, ,  $<, $@ )


build/vt100/%.o  : vt100/%.c
	$(call compile, vt100 vt100/palm,  $<, $@ )


build/%.o  : %.c
	$(call compile, ,  $<, $@ )


clean: 
	rm -rf build
	rm -f $(PEAL_FILES)
	rm -f *~ *.grc  *.s *.i *.bin *.o *.ro *.prc pssh-arm pssh-arm-prelink armc.unsplit
	rm -f $(addsuffix *~,$(sort $(dir $(SRCS))))
	rm -f $(addsuffix .gdb_history,$(sort $(dir $(SRCS))))

.PHONY=all clean
.SUFFIXES=
