wxGTK on Mac OSX
Preface
Building and installing a usable copy of wxWidgets/wxPython using the GTK toolkit while running on Macintosh OS X has been a project that I have attempted and failed at many times. It had been awhile since my last attempt so I decided to give it ago again using the wxPython 2.8.6.0 release sources and was finally able to successfully get everything to work so I can now run and test programs using wxMac, wxGTK, and wxMSW (with parallels) all at the same time on the same machine without needing to reboot.
Purpose
While trying to do this I was unable to find very much help or even very much evidence of others who have tried to accomplish this in my searches of the web. The purpose of this article serves a few points.
- To say it can be done (on an Intel Mac anyway)
- To show how it can be done
- Act as a reminder so I remember how to do it next time around
Eight Steps to Completion
Step 1 Installing Needed Tools:
There are some tools that need to be installed prior to working on the build dependencies so install the following items if they are not already on your machine.
Install Apple Developer Tools (Xcode 2.4.1 or later) http://developer.apple.com/tools/download/
Install X11 http://www.apple.com/support/downloads/x11update2006113.html
Install MacPorts (formerly DarwinPorts) http://www.macports.org/
Install Fink http://finkproject.org/
Step 2 Installing Dependencies:
In order to build wxGTK on Macintosh OS X there are a number of libraries that need to be installed prior to starting the build so this section will try its best to list out what needs to be installed and how to install it. These instructions will also assume you have installed the above tools at their default locations so adjust accordingly if you have installed them elsewhere.
Unfortunately before I started the build this time I had all of the dependencies already installed so this list may be incomplete as I am going to be going from memories about a year ago. So if I have missed anything please feel free to add it.
Setup build environment
- Open up Terminal.app
Copy you profile to a new file to use when building cp ~/.profile ~/GTK_BUILD_ENV
- Open ~/GTK_BUILD_ENV in your favorite command line editor
vim ~/GTK_BUILD_ENV or nano ~/GTK_BUILD_ENV and add the following lines to the bottom of the file in this order.
export CFLAGS=”-no-cpp-precomp” export CPPFLAGS=”$CFLAGS” export PATH=/opt/local/bin:/sw/bin:/usr/X11R6/bin:$PATH}}}
- Update the open shell's environment
source ~/GTK_BUILD_ENV
- Update Package Systems
{{{sudo port selfupdate fink selfupdate}}}
MacPorts and Fink How To
For most possibly all the dependencies first try to install them using MacPorts and if that fails try with fink. Personally I like fink more because it is usually more reliable most the time but currently MacPorts has much more up to date versions of most the software that is needed. The usage of both programs is very similar so just for quick reference here are the three basic commands for both programs that will be needed.
- Update package system:
MacPorts: sudo port selfupdate
- Fink: fink selfupdate
- Find a package:
MacPorts: sudo port list regex
- Fink: fink list regex
- Install a package:
MacPorts: sudo port install pkgname
- Fink: fink install pkgname
- Update package system:
Start installing the dependencies:
Next here is the list of things to install the package systems will take care of build order and dependencies for each of these packages so what order you choose to install them in doesn’t really matter just so long as they all get installed. The list will show the MacPorts command for each of the packages. I suggest doing each command one at a time so it is easy to find where a problem is when something goes wrong.
{{{sudo port install gtk2 sudo port install cairo sudo port install pango sudo port install atk sudo port install glib2 sudo port install libgnomeprint sudo port install python2.5}}}
- By installing the above packages the package systems should install most of the other needed libraries as well but if they didn’t you will find out later during the step where we try to configure and build wxWidgets.
Step 3 Readjusting the Build Environment
Now that the required libraries are installed the build environment needs to readjusted so that the configure scripts for wxWidgets can find them.
Open your profile vim ~/GTK_BUILD_ENV
Add the following new entry to the bottom of the file export LDFLAGS=”-L/opt/local/lib –L/sw/lib”
Modify the following previous entry to match what is shown here export CFLAGS=”-no-cpp-precomp –I/opt/local/include –I/opt/local/include/cairo –I/sw/include”
source ~/GTK_BUILD_ENV
Step 4 Build and Install wxWidgets
For this example I will be using the wxPython 2.8.6.0 sources (http://downloads.sourceforge.net/wxpython/wxPython-src-2.8.6.0.tar.bz2)
I suggest having the BUILD guide available for reference during this step as well (http://wxpython.org/BUILD.html)
Get setup and configure:
- Unpackage the source code
tar xf wxPython-src-2.8.6.0.tar.bz2
cd wxPython-src-2.8.6.0
mkdir gtkbuild && cd gtkbuild
- Configure wxWidgets to build wxGTK {{{../configure --prefix=/opt/wx/2.8 \
- --with-gtk \ --with-gnomeprint \ --without-opengl \ --enable-debug \ --enable-geometry \ --enable-graphics_ctx \ --enable-display \ --disable-debugreport \ --enable-monolithic \ --enable-unicode \ --with-libjpeg=builtin \ --with-libpng=builtin \ --with-libtiff=builtin \ --with-zlib=builtin}}}
If there were any errors that stopped the above command see what it complained about and install it using MacPorts or Fink.
- Make a small helper script as described in the BUILD guide
vim .make Add this text {{{make $* \
&& make -C contrib/src/gizmos $* \ && make -C contrib/src/stc $*}}}
chmod +x .make
Do the Build:
.make
NOTE: during some of the final linking I got a number of errors related to undefined symbols for cairo. This was easily fixed by issuing the following command in the shell that you are doing the build in (export LDFLAGS=”$LDFLAGS –lcairo”) and running .make again.
Install:
sudo .make install
Part 5 Adjust the Build Environment (Again)
vim ~/GTK_BUILD_ENV
Add the following variables to the end of the file:
{{{export DYLD_LIBRARY_PATH=/opt/wx/2.8/lib export LD_LIBRARY_PATH=/opt/wx/2.8/lib}}}
The CFLAGS need to be adjusted again, these values came from trial and error as it seems that the setup.py script has a hard time finding headers that are in subdirectories as they are when installed using MacPorts.
{{{export CFLAGS= "-no-cpp-precomp -I/opt/local/include -I/sw/include -I/opt/local/include/cairo -I/opt/local/include/gtk-2.0 -I/opt/local/include/glib-2.0 -I/opt/local/lib/glib-2.0/include -I/opt/local/include/pango-1.0 -I/opt/local/lib/gtk-2.0/include -I/opt/local/include/atk-1.0"
export PATH=/opt/wx/2.8/bin:/opt/local/bin:/sw/bin:/usr/X11R6/bin:$PATH}}}
source ~/GTK_BUILD_ENV
Step 6 Build wxPython:
First change directory to the wxPython directory this assumes you are still in the gtkbuild directory from earlier.
cd ../wxPython
When running the following command make sure that you are using the python executable that was installed by using Fink or MacPorts and not the one that is under (/usr/bin or /usr/local/bin).
This is the command that I had to use to it to build correctly on my machine, I had installed python with Fink so if you used MacPorts adjust accordingly.
/sw/bin/python2.5 setup.py build_ext –inplace WXPORT=gtk2 BUILD_GLCANVAS=0 BUILD_OGL=0 WX_CONFIG=/opt/wx/2.8/bin/wx-config
Step 7 Final Configuration:
In order to use the wxPython that was just built some more environment changes are needed so open ~/GTK_BUILD_ENV again and add the following variable to the end of the file where $WXDIR is the full path to the wxPython-src-2.8.6.0 directory.
export PYTHONPATH=$WXDIR/WxPython
Make sure to save the GTK_BUILD_ENV file for future use as you will need to source it in whenever you want to run the GTK version of wxPython that was built. Another note to take care is not to move or delete the wxPython-src-2.8.6.0 folder as wxPython was installed in there.
Step 8 How to use it:
- Start X11.app
In an xterm that is started from X11 setup your environment (source ~/GTK_BUILD_ENV)
Try it out any of your wxPython scripts and see how the work under GTK. For example lets use the demo app that's in wxPython-src-2.8.6.0/wxPython/demo. Just make sure to use the same python that you used in Part 6.
{{{cd wxPython-src-2.8.6.0/WxPython/demo /sw/bin/python2.5 demo.py}}}
Screenshots
- The wxPython Demo running under wxGTK using Apples X11 window manager
- The wxPython Demo running under wxGTK using the Enlightenment e16 window manager
- Screenshot with the same program running on all three platforms at the same time (Mac natively, GTK under X11, MSW using Parallels Coherence mode)
NOTES / COMMENTS:
- These instructions do not build the OpenGL context or OGL libraries. They may work but I have no need for them so I didn't bother with any troubles that may or may no have come from trying to build them.
TODO
- Possibly add instructions on how to configure gtk2 themes without installing Gnome
- Cleanup formatting