# Universal Makefile to build Python extensions.
#
# Instructions:
#
# 0) Build and install the basic Python 1.5 distribution.
#
# 1) Edit Makefile.pre.in (not any other file!!!) to change the
# definition of installdir (and possibly exec_installdir).  These
# should match the prefix and exec_prefix used when installing Python.
# Alternatively, you can specify a value for installdir on the
# "make -f Makefile.pre.in Makefile" command line (see below).	This
# is probably easier since it avoids editing Makefile.pre.in at all...
#
# 2) Copy Setup.in to Setup and edit Setup to change the selection of
# modules you want to use.  Read the comments in the core Python's
# Modules/Setup.in for more detailed instructions.
#
# 3) Run "make Makefile -f Makefile.pre.in".  This creates Makefile
# (producing Makefile.pre and sedscript as intermediate files) and
# config.c.  Values for installdir, srcdir, VPATH, LINKPATH, OPT,
# BASELIB and BASESETUP on the make command line will be incorporated
# in the final Makefile.
#
# 4) Run "make".  This creates a Python executable.  The set of
# built-in modules and features is a superset of that of the basic
# Python -- additions are taken from ./Setup.
#
# If you don't actually want to install the Python headers and
# binary library files, you can edit the "Fixed definitions" section
# below to point directly into the source and/or build tree.
#
# Note: Each time you edit Makefile.pre.in or Setup, you must run
# "make Makefile" before running "make".
#
# Hint: if you want to use VPATH, you can start in an empty
# subdirectory and say (e.g.):
# make -f ../Makefile.pre.in VERSION=1.5 srcdir=.. VPATH=.. Makefile


# === Variables that you just *have* to customize manually ===

# (You can also specify their values on the command line for
# "make -f Makefile.pre.in VERSION=1.5 Makefile".)

# The prefix used by "make inclinstall libainstall" of core python
installdir=	/usr

# The exec_prefix used by the same
exec_installdir=$(installdir)

# Source directory and VPATH in case you want to use VPATH.
# (You will have to edit these two lines yourself -- there is no
# automatic support as the Makefile is not generated by
# config.status.)
srcdir=		.
VPATH=		.

# === Variables that you may want to customize ===

# Add possible -L options here
LINKPATH=

# You may want to change this to -O
OPT=		-O -Wall

# Add more -I and -D options here
CFLAGS=		$(OPT) -I$(INCLUDEPY) -I$(LIBPL) $(DEFS)

# These two variables can be set in Setup to merge extensions.
# See example[23].
BASELIB=
BASESETUP=

# === Variables set by makesetup ===

MODOBJS=	_MODOBJS_
MODLIBS=	_MODLIBS_

# === Definitions added by makesetup ===

# === Variables from configure (through sedscript) ===

VERSION=	@VERSION@
CC=		@CC@
DEFS=		@DEFS@
LIBS=		@LIBS@
LIBM=		@LIBM@
LIBC=		@LIBC@
RANLIB=		@RANLIB@
MACHDEP=	@MACHDEP@
SO=		@SO@
LDSHARED=	@LDSHARED@
CCSHARED=	@CCSHARED@
LINKFORSHARED=	@LINKFORSHARED@

# Install prefix for architecture-independent files
prefix=		/usr/local

# Install prefix for architecture-dependent files
exec_prefix=	$(prefix)

# Portable install script (configure doesn't always guess right)
INSTALL=        $(LIBPL)/install-sh -c
INSTALL_PROGRAM=${INSTALL} -m 755
INSTALL_DATA=   ${INSTALL} -m 644
# Shared libraries must be installed with executable mode on some systems;
# rather than figuring out exactly which, we always give them executable mode.
# Also, making them read-only seems to be a good idea...
INSTALL_SHARED= ${INSTALL} -m 555

# === Fixed definitions ===

# Expanded directories
BINDIR=		$(exec_installdir)/bin
LIBDIR=		$(exec_prefix)/lib
MANDIR=		$(installdir)/man
INCLUDEDIR=	$(installdir)/include
SCRIPTDIR=	$(prefix)/lib

# Detailed destination directories
BINLIBDEST=	$(LIBDIR)/python$(VERSION)
LIBDEST=	$(SCRIPTDIR)/python$(VERSION)
INCLUDEPY=	$(INCLUDEDIR)/python$(VERSION)
LIBP=		$(exec_installdir)/lib/python$(VERSION)

LIBPL=		$(LIBP)/config

PYTHONLIBS=	$(LIBPL)/libModules.a \
		$(LIBPL)/libPython.a \
		$(LIBPL)/libObjects.a \
		$(LIBPL)/libParser.a

MAKESETUP=	$(LIBPL)/makesetup
MAKEFILE=	$(LIBPL)/Makefile
CONFIGC=	$(LIBPL)/config.c
CONFIGCIN=	$(LIBPL)/config.c.in
SETUP=		$(LIBPL)/Setup

SYSLIBS=	$(LIBM) $(LIBC)

ADDOBJS=	$(LIBPL)/main.o getpath.o config.o

# === Fixed rules ===

all:		sharedmods

sharedmods:	$(SHAREDMODS)

python:		$(ADDOBJS) lib.a $(PYTHONLIBS) Makefile $(BASELIB)
		$(CC) $(ADDOBJS) lib.a $(PYTHONLIBS) \
		 $(LINKPATH) $(BASELIB) $(MODLIBS) $(LIBS) $(SYSLIBS) -o python

lib.a:		$(MODOBJS)
		-rm -f lib.a
		ar cr lib.a $(MODOBJS)
		-$(RANLIB) lib.a || \
		echo "don't worry if ranlib fails -- probably SYSV or equiv"

# This runs makesetup *twice* to use the BASESETUP definition from Setup
config.c Makefile:	Makefile.pre Setup $(BASESETUP) $(MAKESETUP)
		$(MAKESETUP) \
		 -m Makefile.pre -c $(CONFIGCIN) Setup -n $(BASESETUP) $(SETUP)
		$(MAKE) -f Makefile do-it-again

do-it-again:
		$(MAKESETUP) \
		 -m Makefile.pre -c $(CONFIGCIN) Setup -n $(BASESETUP) $(SETUP)

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

getpath.o:	$(LIBPL)/getpath.c Makefile
		$(CC) $(CFLAGS) -DPYTHONPATH=\"$(PYTHONPATH)\" -c $(LIBPL)/getpath.c

# Setup is copied from Setup.in *only* if it doesn't yet exist
Setup:
		cp $(srcdir)/Setup.in Setup

Makefile.pre: Makefile.pre.in sedscript
		sed -f sedscript $(srcdir)/Makefile.pre.in >Makefile.pre

# Shortcuts to make the sed arguments on one line
P=prefix
E=exec_prefix
H=Generated automatically from Makefile.pre.in by sedscript.
sedscript:	$(MAKEFILE)
		sed -n \
		  -e '1s/.*/1i\\/p' \
		  -e '2s%.*%# $H%p' \
		  -e '/^VERSION=/s/^VERSION=[	]*\(.*\)/s%@VERSION[@]%\1%/p' \
		  -e '/^CC=/s/^CC=[	]*\(.*\)/s%@CC[@]%\1%/p' \
		  -e '/^DEFS=/s/^DEFS=[		]*\(.*\)/s%@DEFS[@]%\1%/p' \
		  -e '/^LIBS=/s/^LIBS=[		]*\(.*\)/s%@LIBS[@]%\1%/p' \
		  -e '/^LIBM=/s/^LIBM=[		]*\(.*\)/s%@LIBM[@]%\1%/p' \
		  -e '/^LIBC=/s/^LIBC=[		]*\(.*\)/s%@LIBC[@]%\1%/p' \
		  -e '/^RANLIB=/s/^RANLIB=[	]*\(.*\)/s%@RANLIB[@]%\1%/p' \
		  -e '/^MACHDEP=/s/^MACHDEP=[	]*\(.*\)/s%@MACHDEP[@]%\1%/p' \
		  -e '/^SO=/s/^SO=[	]*\(.*\)/s%@SO[@]%\1%/p' \
		  -e '/^LDSHARED=/s/^LDSHARED=[		]*\(.*\)/s%@LDSHARED[@]%\1%/p' \
		  -e '/^CCSHARED=/s/^CCSHARED=[		]*\(.*\)/s%@CCSHARED[@]%\1%/p' \
		  -e '/^LINKFORSHARED=/s/^LINKFORSHARED=[	]*\(.*\)/s%@LINKFORSHARED[@]%\1%/p' \
		  -e '/^$P=/s/^$P=\(.*\)/s%^$P=.*%$P=\1%/p' \
		  -e '/^$E=/s/^$E=\(.*\)/s%^$E=.*%$E=\1%/p' \
		  $(MAKEFILE) >sedscript
		( \
		  echo "/^installdir=/s%=.*%=	$(installdir)%"; \
		  echo "/^exec_installdir=/s%=.*%=$(exec_installdir)%"; \
		  echo "/^srcdir=/s%=.*%=		$(srcdir)%"; \
		  echo "/^VPATH=/s%=.*%=		$(VPATH)%"; \
		  echo "/^LINKPATH=/s%=.*%=	$(LINKPATH)%"; \
		  echo "/^OPT=/s%=.*%=		$(OPT)%"; \
		  echo "/^BASELIB=/s%=.*%=	$(BASELIB)%"; \
		  echo "/^BASESETUP=/s%=.*%=	$(BASESETUP)%"; \
		) >>sedscript

clean:
		-rm -f *.o *~

clobber:	clean
		-rm -f python lib.a sedscript config.c Makefile.pre

distclean:	clobber
		-rm -f Makefile Setup

CRYPTO_DIR=     $(LIBDEST)/site-packages/Crypto
HASHMODS=       MD2 MD4 MD5 SHA HAVAL
CIPHERMODS=     ARC2 ARC4 Blowfish CAST DES DES3 Diamond IDEA RC5 Sapphire 

links:  $(SHAREDMODS)
		# Make links in the Hash directory
	        -for i in $(HASHMODS) ; do \
	            ln -f $${i}module$(SO) ../Hash/$${i}module$(SO) ; \
		done
		
		# Make links in the Cipher directory
	        -for i in $(CIPHERMODS) ; do \
	            ln -f $${i}module$(SO) ../Cipher/$${i}module$(SO) ; \
		done

install:	$(SHAREDMODS)
	        # Create package directories
		@for i in $(CRYPTO_DIR) $(CRYPTO_DIR)/Cipher \
			  $(CRYPTO_DIR)/PublicKey $(CRYPTO_DIR)/Hash \
			  $(CRYPTO_DIR)/Protocol $(CRYPTO_DIR)/Util ; \
		do \
			if test ! -d $$i; then \
				echo "Creating directory $$i"; \
				mkdir $$i; \
				chmod 755 $$i; \
			else	true; \
			fi; \
		done

		$(INSTALL_DATA) ../_checkversion.py $(CRYPTO_DIR)

	        # Copy the __init__.py files
		-for i in "" Cipher Hash Protocol; do \
	            $(INSTALL_DATA) ../$$i/__init__.py $(CRYPTO_DIR)/$$i/__init__.py ; \
		done

		# Copy things into the Hash directory
	        -for i in $(HASHMODS) ; do \
	            $(INSTALL_SHARED) $${i}module$(SO) $(CRYPTO_DIR)/Hash/ ; \
		done
		
		# Copy things into the Cipher directory
	        -for i in $(CIPHERMODS) ; do \
	            $(INSTALL_SHARED) $${i}module$(SO) $(CRYPTO_DIR)/Cipher/ ; \
		done
		
		# Copy things into the PublicKey directory
	        -for i in ../PublicKey/*.py ; do \
	            $(INSTALL_DATA) $$i $(CRYPTO_DIR)/PublicKey/$$i ; \
		done
		
		# Copy things into the Util directory
	        -for i in ../Util/*.py ; do \
	            $(INSTALL_DATA) $$i $(CRYPTO_DIR)/Util/$$i ; \
		done

	        python -c 'from compileall import *;compile_dir("$(CRYPTO_DIR)")'
