#!/usr/bin/make -f
#
# Twofish/i586 for Linux
#
# Copyright 2003, Fruhwirth Clemens <clemens@endorphin.org>
# This package is Public Domain.
#

KERNEL_SOURCE=/usr/src/linux-2.6.0-test2/

KEY := FULL_KEY
CFLAGS := -DUSE_ASM -DGetCodeSize -D_M_IX86 -D$(KEY) -Wall \
	-Wstrict-prototypes -Wno-trigraphs -O2 -fno-strict-aliasing \
	 -fno-common -pipe -mpreferred-stack-boundary=2 \
	-I$(KERNEL_SOURCE)/include/asm-i386/mach-default \
	-fomit-frame-pointer -iwithprefix include -DMODULE

OBJCOPY := objcopy
USE_PRECOMPILED := 1

all:	twofish-i586.ko

twofish2.o:	twofish2.c
	$(CC) $(CFLAGS) -o twofish2.o -c twofish2.c

tst2fish.o:	tst2fish.c
	$(CC) $(CFLAGS) -o tst2fish.o -c tst2fish.c

# OBJDUMP_FLAGS := --remove-leading-char
# does not work, using a lot of redefines instead.

ifeq ($(USE_PRECOMPILED),0)

2fish_86.o: 2fish_86.obj
	$(OBJCOPY) $(OBJDUMP_FLAGS) -I coff-i386 -O elf32-i386     \
		--redefine-syms 2fish_86.remap			   \
		2fish_86.obj 2fish_86.o
#		--redefine-sym _MDStab=MDStab                       \
#		--redefine-sym _P8x8=P8x8                           \
#		--redefine-sym _useAsm=useAsm                       \
#		--redefine-sym _get_cpu_type=get_cpu_type           \
#		--redefine-sym _TwofishAsmCodeSize=TwofishAsmCodeSize       \
#		--redefine-sym _blockDecrypt_86=blockDecrypt_86     \
#		--redefine-sym _blockEncrypt_86=blockEncrypt_86     \
#		--redefine-sym _reKey_86=reKey_86                   \

else

2fish_86.o: precompiled/2fish_86.o.uu
	uudecode -o 2fish_86.o precompiled/2fish_86.o.uu
endif

2fish_86.obj:	masm/ml.exe
	wine masm/ml /Fo2fish_86.obj /coff /c /Zm /Cp /D$(KEY) 2fish_86.asm


tst2fish: 2fish_86.o twofish2.o tst2fish.o
	$(CC) -o tst2fish twofish2.o tst2fish.o 2fish_86.o


twofish_glue.o: twofish_glue.c
	$(CC) -D__KERNEL__ -nostdinc -I$(KERNEL_SOURCE)/include $(CFLAGS) -DKBUILD_BASENAME=twofish_i586 -DKBUILD_MODNAME=twofish_i586 \
	-c -o twofish_glue.o twofish_glue.c

twofish_glue.mod.c: twofish_glue.o
	$(KERNEL_SOURCE)/scripts/modpost twofish_glue.o

twofish_glue.mod.o: twofish_glue.mod.c
	$(CC) -D__KERNEL__ -I$(KERNEL_SOURCE)/include -Wall \
	-Wstrict-prototypes -Wno-trigraphs -O2 -fno-strict-aliasing -fno-common \
	-pipe -mpreferred-stack-boundary=2 -I$(KERNEL_SOURCE)/include/asm-i386/mach-default \
	-fomit-frame-pointer -nostdinc -iwithprefix include -DMODULE   \
	-DKBUILD_BASENAME=twofish_i586 -DKBUILD_MODNAME=twofish_i586 -c -o \
	twofish_glue.mod.o twofish_glue.mod.c

twofish-i586.ko: 2fish_86.o twofish2.o twofish_glue.o twofish_glue.mod.o
	$(LD) -m elf_i386 -r -o twofish-i586.ko twofish_glue.o twofish_glue.mod.o twofish2.o 2fish_86.o

clean:
	rm -f 2fish_86.o 2fish_86.obj tst2fish.o twofish2.o tst2fish twofish_glue.o twofish_glue.mod.c twofish_glue.mod.o twofish-i586.ko


