#****************************************************************************
#
# Portions of this software Copyright (c) 2005-2014 Broadcom Corporation 
#
#*****************************************************************************
#   Filename:      makefile
#   Author:        Sean Dunlap
#   Creation Date: May 2nd, 2005
#
#**************************************************************************
#   Description:
#
#   eCos makefile for openSSH library
#
#**************************************************************************

ARCH          = mipsisa32-elf
CROSS_COMPILE = $(ARCH)
CPU           = MIPS32

BFC_DIR       = ../../../rbb_cm_src/Bfc
# jb-gcc5.3 - update directory to point to current build tree
#             instead of /projects/bfc/ecos20/bcm33xx/bcm33xx_ipv6_install/
#CONFIG_DIR    = $(ECOS_CONFIG_ROOT)/$(ECOS_CONFIG_DIR)
CONFIG_DIR    = $(BFC_DIR)/LibSupport/eCos/bcm33xx_ipv6_install

# jb-gcc5.3 -  dynamically figure out the GNU include directory
#              remove reference to $(ECOS_DIR)
#GNU_INC       = $(ECOS_DIR)/gnutools/$(ARCH)/$(ARCH)
# Find the directory where the Gnu compiler is installed.
GNU_INC := $(dir $(shell which $(ARCH)-gcc))

# Get rid of the "/bin/", and add the architecture directory.  This is where
# all the include files will be found.
GNU_INC := $(subst /bin/,,$(GNU_INC))/$(ARCH)

CC            = $(ARCH)-gcc 

EXTRA_DEFINE = \
        -DCPU=$(CPU) \
        -DMIPSEB \
        -DOS_ECOS=1

EXTRA_INCLUDE += \
        -I. \
        -I$(CONFIG_DIR)/include \
        -I$(GNU_INC)/include/backward \
        -I$(GNU_INC)/include \
        -I$(GNU_INC)/include/$(ARCH) \
        -I$(BFC_DIR)/LibSupport/OpenSSL/include \
        -I../zlib-1.2.11 \
        -I../ecos \
        -I./openbsd-compat \
        -I$(BFC_DIR)/IpHelpers/Https/ssl_lib/include
#--------------------------------------------------------------------------
# Compiler flags:
#        -EB            - big-endian
#        -mips32        - MIPS32 CPU
#        -G 0           - no "small global" area
#        -O1            - some, but not all, optimizations
#        -msoft-float   - use soft floating point
#        -Wall          - all warnings
#        -c             - compile to .o
#        jb-gcc5.3 : removed fno-for-scope, valid for C++ only
#        -fno-for-scope - make variables declared in "for" visible outside loop
#--------------------------------------------------------------------------
CFLAGS  = \
        -EB \
        -mips32 \
        -G 0 \
        -Os \
        -ffunction-sections \
        -msoft-float \
        -Wall \
        $(EXTRA_INCLUDE) \
        $(EXTRA_DEFINE) \
        -c

#--------------------------------------------------------------------------
# Debug Compiler flags: To debug a given .c file
#        -E -dM        - #list all macro defines
#        -msoft-float   - use soft floating point
#        -Wall          - all warnings
#--------------------------------------------------------------------------
DFLAGS = \
         -E -H -dM


# additional cflags specific to our openSSH configuration
CFLAGS += -D NO_GZIP


COMPILE = $(CC) $(CFLAGS)
ARCHIVE = ar cru $@

LIBRARY_PATH = $(BFC_DIR)/LibSupport/OpenSSH
PROJECTLIB   = $(LIBRARY_PATH)/libOpenSSH.a 


########################################################################
# It shouldn't be necessary to change anything below this point.
########################################################################

#//======================================================================
#// OpenSSH files
#//======================================================================
LIBSSH_OBJS  = authfile.o bufaux.o buffer.o \
	canohost.o cipher.o \
	compat.o crc32.o fatal.o \
	match.o nchan.o packet.o \
	log.o ttymodes.o xmalloc.o \
	atomicio.o key.o dispatch.o kex.o mac.o misc.o \
	monitor_fdpass.o ssh-dss.o dh.o kexdh.o \
	kexgex.o kexdhc.o kexgexc.o msg.o  \
	entropy.o channels.o \
	ssh-rsa.o sshbuf.o sshbuf-getput-basic.o \
	opacket.o sshkey.o ssherr.o openbsd-compat/bsd-misc.o \
	openbsd-compat/getopt_long.o openbsd-compat/strsep.o openbsd-compat/reallocarray.o \
	openbsd-compat/recallocarray.o sshbuf-misc.o umac.o \
        openbsd-compat/bsd-asprintf.o hmac.o digest-openssl.o \
	digest-libc.o openbsd-compat/md5.o openbsd-compat/sha1.o sshbuf-getput-crypto.o \
	ed25519.o ge25519.o fe25519.o sc25519.o ssh-ecdsa.o hash.o blocks.o verify.o openbsd-compat/bcrypt_pbkdf.o \
	ssh-ed25519.o openbsd-compat/blowfish.c openbsd-compat/timingsafe_bcmp.o openbsd-compat/base64.o bufbn.o \
        openbsd-compat/xcrypt.o cipher-ctr.o kexc25519s.o umac128.o kexc25519.o openbsd-compat/arc4random.o \
	smult_curve25519_ref.o openbsd-compat/strtonum.o cipher-chachapoly.o chacha.o groupaccess.o kexecdhs.o \
	poly1305.o openbsd-compat/getgrouplist.o loginrec.o platform.o  openbsd-compat/setenv.o authfd.o \
	openbsd-compat/bsd-waitpid.o openbsd-compat/setproctitle.o addrmatch.o  openbsd-compat/vis.o auth2-pubkey.o \
        kexecdh.o openbsd-compat/openssl-compat.c hostfile.o platform-misc.o openbsd-compat/fake-rfc2553.o \
	openbsd-compat/fmt_scaled.o auth2-hostbased.o auth-rhosts.o sshpty.o

SSHKEYGENOBJS = krl.o moduli.c bitmap.o readpass.o openbsd-compat/readpassphrase.o \
	uuencode.o utf8.o dns.o uidswap.o ssh-keygen.o

SSHDOBJS = sshd.o  auth-passwd.o \
	sshlogin.o servconf.o serverloop.o \
	auth.o auth2.o auth-options.o session.o \
	auth2-none.o auth2-passwd.o \
	monitor.o monitor_wrap.o kexdhs.o kexgexs.o \
	md5crypt.o \
	auth2-chall.o auth2-kbdint.o 

ECOSOBJS = ../ecos/openssh_to_ecos.o ../ecos/portfree.o

#// Source Files
#//======================================================================
SOURCE_C=$(LIBSSH_OBJS) $(SSHDOBJS) $(SSHKEYGENOBJS) $(ECOSOBJS)

#//======================================================================
#// Generic rule for generating dependencies files
#//======================================================================
%.d : %.c
	$(COMPILE_DEPENDS) -M $< > $(@F)

#//======================================================================
#// Generic rule for generating *.o object files
#//======================================================================
%.o: %.c
	$(COMPILE) $< -o $@

#//======================================================================
%.dir:
	$(MAKE) -C $*

#//======================================================================
%.cln:
	$(MAKE) -C $* clean

#//======================================================================
#// Internal definitions
#//======================================================================
allcfiles      = $(SOURCE_C)
c_objects      = $(allcfiles:.c=.o)

all_objects   := $(c_objects)

#//======================================================================
#// Build targets
#//======================================================================
target : project

project : $(all_objects) $(PROJECTLIB)

$(PROJECTLIB): $(all_objects)
	$(ARCHIVE) $?

all :
	@$(MAKE) clean
	@$(MAKE) target

clean : $(c_subdirs)
	rm -f *.o
	rm -f ../ecos/*.o
	rm -f openbsd-compat/*.o
	rm -f $(PROJECTLIB)

debug :
#	$(CC) $(CFLAGS) $(DFLAGS) openbsd-compat/readpassphrase.c
	$(CC) $(CFLAGS) $(DFLAGS) ../ecos/openssh_to_ecos.c
#	$(CC) $(CFLAGS) $(DFLAGS) openbsd-compat/getopt_long.c

dumplib :
	nm -C ../../../rbb_cm_src/Bfc/LibSupport/OpenSSH/libOpenSSH.a

# special case target for umac128
umac128.o: umac.c
	$(COMPILE) -o umac128.o -c umac.c \
	    -DUMAC_OUTPUT_LEN=16 -Dumac_new=umac128_new \
	    -Dumac_update=umac128_update -Dumac_final=umac128_final \
	    -Dumac_delete=umac128_delete -Dumac_ctx=umac128_ctx
