Re: Pending arrangements

Ken Manheimer (ken.manheimer@nist.gov)
Thu, 29 Sep 94 22:30:20 EDT

[Whoops, originally neglected to cc the list on this.]

Regarding dynamic-load python object modules - the provisions really
are very straightforward, and the results are powerful.

> My problem still remains. I have to recompile python every time I add a
> a new binary module? If not, how does it find the proper .so file to
> search for the compiled code. There is also the problem of adding the
> module name and function to the inittab.

Once you've created a dynamic object (eg, a .so object under sun
unix), you simply put it on the load-path, as you would any .py or
.pyc library-code file. Python will snarf up the dynamic load, as it
would a .py or .pyc, when you do an 'import' of the module!!

Just about the only effective difference is that you can't do a
'reload()' of the dynamic module once you've changed it, as you could
with the python-code based modules. You have to start a new python
session, and do a new 'import' to incorporate a new version of an
object module.

Dynamic-load modules really are extremely cool, and i find them to be
a blessing for development of C extensions. I'm the kind of
programmer that has to try things out incrementally, as i'm doing
development, particularly when i'm working with a substantial,
unfamiliar system like python. With dynamic loading, i can make
changes to the code, do a make to recompile the dyanmic object,
restart python, and within fifteen seconds or half a minute, test the
changes.

In case anyone's interested, i created the following makefile to make
the logistics of producing the dynamic objects easier - no (personal)
memory required. And very little effort - once the makefile is
configure for your installation, you don't even need to name the
modules you want it to compile.

If the module doesn't require special compile or load files, you need
only put your module in the same dir as the makefile, and do a make!
Even if they do require special flags, you need only create a suitable
variable for the flags, and then the module it completely taken care
of...

I know it works under solaris 1 and 2 (sunos4 and 5), and i made it so
it could be used with gnu make and gnu cc, as well, so it should be
easy to port to other platforms that support dynamic loading. (Will
require that you tailor the load statement for your platforms' dynamic
load mechanisms.) If anyone does experiment with this "minimal-
intervention" dynamic-load makefile on other platforms, i'd be
interested to hear what you encounter, with an eye towards making this
a multi-platform mechanism...

Ken
ken.manheimer@nist.gov, 301 975-3539

# Makefile to do general-coverage creation of dynamic-load libraries
# from python C-module sources.

# $Id: Makefile,v 1.4 1994/09/07 22:13:20 root Exp root $
# Created by Ken Manheimer, Jul-1994. ken.manheimer@nist.gov, 301 975-3539

# To configure for your site, select the appropriate SOURCES and macro
# def and assign the right path to the prefix macro.

ARCH= sun4
prefix= /depot/sundry
DESTLIB= $(prefix)/lib/python/$(ARCH)

# To configure for a new module:
# - put the module in the current directory
# - if it doesn't require any special compile or load options, that's it.
# - if it does require special compile or load options, create a macro
# composed of the (full) module name, sans suffix, plus 'CFLAGS' or
# 'LDFLAGS', depending on the compile phase in question.
metalbasemoduleCFLAGS= -I$(prefix)/include/mbase51 -DNO_TIMEB -DNO_USHORT -DNO_ENCRYPT
metalbasemoduleLDFLAGS= -L/depot/sundry/plat/lib -lmb
cursesmoduleCFLAGS= -I/usr/5include
cursesmoduleLDFLAGS= -L/usr/5lib -lcurses -ltermcap

### For Sun Make; tested in v 1.0, under both SunOS 4.1.3 and SunOS 5.3:
#SOURCES:sh= echo *.c
### For Gnu Make; works at least for v 3.59:
SOURCES= $(wildcard *.c)

OBJS= $(SOURCES:.c=.so)

CC= gcc
OPT= -g -O
DEFS= -DHAVE_CONFIG_H
INCLDIR= $(prefix)/include/python
CFLAGS= $(OPT) -I$(INCLDIR) -I.. $(DEFS)
LD= ld

all: $(OBJS)

%.o: %.c
$(CC) -c $(CFLAGS) $(CPPFLAGS) $($*CFLAGS) -o $@ $<

%.so: %.o
$(LD) $(LDFLAGS) -o $@ $< $($*LDFLAGS) $(LOADLIBES)

PHONY: echo # For testing derivation of $(OBJS).
echo:
@echo "(Set SOURCES def if you don't see a '.so' for each '.c' between the brackets)"
@echo :$(OBJS):

PHONY : install
install: $(OBJS)
@ls $(OBJS) | cpio -pvm $(DESTLIB) 2>&1 | grep -v " newer "

PHONY : clean
clean:
rm -f *.o *.so