#!/usr/bin/make -f
#
# You just got a Makefile to build (read: cross-compile) the openssl library
# for windows32 using mingw32 on a linux machine.
#
# You may have to change some of the variable to point to the correct path of
# the corresponding binary.
#
# If you are running Debian GNU/Linux you should be happy with the preset
# paths. (You'll need the packages: mingw32 mingw32-binutils)
#
# To use this Makefile, just place (or link) it in(to) the top level directory
# of the openssl suite an run
# make -f openssl-Makefile-mingw32
#
# Maximilian Wilhelm <max@rfc2324.org>
#  -- Sun, 29 Jul 2007 05:58:07 +0200
#

CP = cp
RM = rm

CC = /usr/bin/i586-mingw32msvc-gcc
RANLIB = /usr/i586-mingw32msvc/bin/ranlib
ASM = /usr/i586-mingw32msvc/bin/as
MKLIB = /usr/i586-mingw32msvc/bin/ar r

DLLWRAP = /usr/bin/i586-mingw32msvc-dllwrap
PERL = /usr/bin/perl

# The unix version of the shipped Makefile
MINGW32A_MAK = ../mingw32a.mak

#
# Configure things
doit:
	perl Configure mingw $1 $2 $3 $4 $5 $6 $7 $8

#
# Create x86 assemble
	@echo Generating x86 for GNU assember

	@echo Bignum
	@( cd crypto/bn/asm && $(PERL) bn-586.pl gaswin > bn-win32.s && $(PERL) co-586.pl gaswin > co-win32.s )
	
	@echo DES
	@( cd crypto/des/asm && $(PERL) des-586.pl gaswin > d-win32.s && $(PERL) crypt586.pl gaswin > y-win32.s )
	
	@echo Blowfish
	@( cd crypto/bf/asm && $(PERL) bf-586.pl gaswin > b-win32.s )

	@echo CAST5
	@( cd crypto/cast/asm && $(PERL) cast-586.pl gaswin > c-win32.s )
	
	@echo RC4
	@( cd crypto/rc4/asm && $(PERL) rc4-586.pl gaswin > r4-win32.s )
	
	@echo MD5
	@( cd crypto/md5/asm && $(PERL) md5-586.pl gaswin > m5-win32.s )
	
	@echo SHA1
	@(cd crypto/sha/asm && $(PERL) sha1-586.pl gaswin > s1-win32.s )
	
	@echo RIPEMD160
	@(cd crypto/ripemd/asm && $(PERL) rmd-586.pl gaswin > rm-win32.s )
	
	@echo RC5/32
	@( cd crypto/rc5/asm && $(PERL) rc5-586.pl gaswin > r5-win32.s )

	@echo CPUID
	@( cd crypto && $(PERL) x86cpuid.pl gaswin > cpu-win32.s )


#
# Generate the makefile
	@echo Generating makefile
	$(PERL) util/mkfiles.pl >MINFO
	$(PERL) util/mk1mf.pl gaswin Mingw32 >ms/mingw32a.mak


#
# Generate the DLL definitions
	@echo Generating DLL definition files
	$(PERL) util/mkdef.pl 32 libeay >ms/libeay32.def
	$(PERL) util/mkdef.pl 32 ssleay >ms/ssleay32.def


#
# Build the library
	@echo Building the libraries

	#
	# Make the generated makefile useable on a linux system
	# Just a little bit of $(PERL) regex magic...
	
	# Convert paths to uinx ones
	$(PERL) -p -i -e 's|\\|/|g;  s|/$$|\\|;' ms/mingw32a.mak

	# Set paths to programms correctly
	$(PERL) -p -i -e 's/^CP=.*/CP=$(CP)/;   s/^RM=.*/RM=$(RM)/; \
		s|^CC=.*|CC=$(CC)|;  s|^RANLIB=.*|RANLIB=$(RANLIB)|; \
		s|^MKLIB=.*|MKLIB=$(MKLIB)|;  s|^ASM=.*|ASM=$(ASM)|;' \
		ms/mingw32a.mak
	
	# Remove damn 'if exist' statements.
	$(PERL) -p -i -e 's/if exist \$$\(O_SSL\) \$$\(RM\) \$$\(O_SSL\)/\$$(RM) -f \$$(O_SSL)/; \
		s/if exist \$$\(O_CRYPTO\) \$$\(RM\) \$$\(O_CRYPTO\)/\$$(RM) -f \$$(O_CRYPTO)/;' \
		ms/mingw32a.mak

	# Now use the pimped Makefile
	make -f ms/mingw32a.mak

#
# Finish things
	@echo Generating the DLLs and input libraries
	$(DLLWRAP) --dllname libeay32.dll --output-lib out/libeay32.a --def ms/libeay32.def out/libcrypto.a -lwsock32 -lgdi32
	$(DLLWRAP) --dllname libssl32.dll --output-lib out/libssl32.a --def ms/ssleay32.def out/libssl.a out/libeay32.a
