[Pythonmac-SIG] Building PPC Mac extension modules - newbie help!

Jack Jansen Jack.Jansen@oratrix.com
Tue, 11 Mar 2003 22:46:23 +0100


On dinsdag, maa 11, 2003, at 19:18 Europe/Amsterdam, Andy Robinson 
wrote:

> The docs (Mac/Demos/building.html) tell me I don't need to
> rebuild Python first, and to look at "xx.prj".  The nearest
> match I can find to this is Mac/Build/ which contains xx.mcp
> and its kin.  Is this the same thing?

Yes, it's the same thing. .prj was the old name, I'll change the 
documentation.
That file was *horribly* outdated, I'm sorry for that. I don't have 
time to do
a real rewrite, but I'll update it a bit.

> If so, CodeWarrior converts this template to version 8 and complains
> about lack of GUSI.  So, do I need to download all the components and
> rebuild Python first to build extensions?

Yes, in your case I think you need GUSI. Download it via 
<http://www.cwi.nl/~jack/macpython.html>,
you need a rather special GUSI to build Python.
See :Mac:Demo:building.html for how to build it and where to put it.

> Second stage is to build an embedded example.  There seems to
> be an example in the source, which I'll tackle after step 1.
>
> Third stage is to make a compact distribution.  I'm looking
> for a way to do something like freeze or BuildApplication,
> but making a -library- not an -application-.  Can anyone
> give hints on how this is possible?

See :Mac:Demo:freezing.html. This isn't a recipe for what you want to 
do,
but it does contain most of the interesting information.

Bascially, you want to end up with the Python PYC resources (the stuff 
that is
normally in .pyc files) in the resource fork of your library, and you 
want the
library to be on sys.path. This latter is a bit of MacPython-magic: if 
there is a
file on sys.path (as opposed to a directory) we start looking for PYC 
resources
in that file.

Here's a quick and dirty idea that might work:
1. Run BuildApplication on your main Python script. This creates a 
useless application
(because you want a shared library), but you can now easily grab all 
the 'PYC ' resources
from this application, and put them in a resource file (let's call it 
andy.rsrc). If you
don't want to manually fiddle the BuildApplication output it should be 
reasonably simple
to adapt BuildApplication so it creates this .rsrc file for you in 
stead of a fullblown
application.
2. Create a project for your shared library. I'd say start with the 
PythonCore project,
and add your own stuff and andy.rsrc. See embed.html for how to embed 
on the mac (the
initialization entry point is different).
3. Now you only need to get your shared library in sys.path. It's a bit 
tricky to get at
the filename for a shared library form within that shared library, 
basically you ned to define
an initialization entrypoint routine and in that routine stow away the 
FSSpec for your
library. You can then pick this up later, turn it into a filename and 
put it on sys.path.
For an example of something similar (although this code only wants to 
open the resource fork
of the shared lib) look in _tkinter.c, routines init_tkinter_shlib 
(which is used as the shared
lib init routine) and mac_addlibresources.

If anything doesn't work as you expect (or as I say here:-) don't 
hesitate to contact me.
This should all be possibly, but i don't know whether it has actually 
ever been done by anyone.
--
- Jack Jansen        <Jack.Jansen@oratrix.com>        
http://www.cwi.nl/~jack -
- If I can't dance I don't want to be part of your revolution -- Emma 
Goldman -