######################################################################
# Build all embedding examples: "make -f Makefile.all";
# also builds extension modules shareables (cregister, environ);
# be sure to set the Python search path to find imported modules:
# setenv PYTHONPATH .:/opt/local/src/Python-1.4-embed/Python-1.4/Lib
######################################################################

Cc = /opt/SUNWspro/bin/cc
PY = /opt/local/src/Python-1.4-embed/Python-1.4

# libs and objects from Python build tree 
PLIBS = $(PY)/Modules/libModules.a \
        $(PY)/Python/libPython.a \
        $(PY)/Objects/libObjects.a \
        $(PY)/Parser/libParser.a

POBJS = $(PY)/Modules/config.o $(PY)/Modules/getpath.o

CLIBS  = -L/usr/lib -lsocket -lnsl -ldl -lm
CFLAGS = -c -g -I$(PY)/Include -I$(PY)/. -I$(APISRC)

# extended API files from "Programming Python" CD
APIMODS = pyembed0.o pyembed1.o pyembed2.o pyembed3.o pyembed4.o
APISRC  = ..

# targets
EXECS = main1 \
        objects1 objects1err objects2 \
        codestring1 codestring2 
CMODS = cregister.so environ.so

all: $(EXECS) $(CMODS)

# [main1: run simple strings]
main1: 
	$(MAKE) -f Makefile.main1

# [objects1: call objects]
objects1: objects1.o
	$(Cc) -g objects1.o $(POBJS) $(PLIBS) $(CLIBS) -o $@

objects1.o: objects1.c
	$(Cc) objects1.c $(CFLAGS)

# [objects1err: objects1 + error checking]
objects1err: objects1err.o
	$(Cc) -g objects1err.o $(POBJS) $(PLIBS) $(CLIBS) -o $@

objects1err.o: objects1err.c
	$(Cc) objects1err.c $(CFLAGS)

# [objects2: call objects via api]
objects2: objects2.o $(APIMODS)
	$(Cc) -g objects2.o $(APIMODS) $(POBJS) $(PLIBS) $(CLIBS) -o $@

objects2.o: objects2.c $(APISRC)/pyembed.h
	$(Cc) objects2.c $(CFLAGS)

# [codestring1: run strings]
codestring1: codestring1.o
	$(Cc) -g codestring1.o $(POBJS) $(PLIBS) $(CLIBS) -o $@

codestring1.o: codestring1.c
	$(Cc) codestring1.c $(CFLAGS)

# [codestring2: run strings via api]
codestring2: codestring2.o $(APIMODS)
	$(Cc) -g codestring2.o $(APIMODS) $(POBJS) $(PLIBS) $(CLIBS) -o $@

codestring2.o: codestring2.c $(APISRC)/pyembed.h
	$(Cc) codestring2.c $(CFLAGS)

# [extended api files: 0..4]
pyembed%.o: $(APISRC)/pyembed%.c $(APISRC)/pyembed.h
	$(Cc) $(APISRC)/pyembed$*.c $(CFLAGS)


###################################################################
# add the extension modules (cregister, environ) as dynamically 
# linked modules; when first imported, they are located in a dir 
# on $PYTHONPATH, and loaded into the process. 
#
# Note: the code here is specific to Sun Solaris (SunOS 5); it may
# need to be tweaked to produce a shareable object file on your 
# system.  Alternatively, these two modules can be bound in to 
# the 'python' executable statically (not dynamically), by adding
# lines to Modules/Setup like "cregister cregister.c", putting a
# link to the .c file in Modules (or the .o file), and remake'ing
# Python.  Static linking requires rebuilding Python itself, but
# is a more portable strategy.
###################################################################


cregister.so: cregister.c
	$(Cc) cregister.c $(CFLAGS) -DDEBIG -KPIC -o cregister.o
	ld -G cregister.o -o $@
	rm -f cregister.o

environ.so: environ.c
	$(Cc) environ.c $(CFLAGS) -KPIC -o environ.o
	ld -G environ.o -o $@
	rm -f environ.o

clean: 
	rm -f *.o *.pyc $(EXECS) $(CMODS)

