Creating a custom python debug library for mingw
To debug FIFE debug builds with gdb, you'll need to link FIFE against a debug version of the Python library. While the official Python 2.7 distribution for win32 ships with a python27.a library for mingw, it lacks a debug version of it.
The only way to address the problem is create a (lib)python27_d.a library yourself. This guide describes how to tackle that task.
- MinGW: http://sourceforge.net/projects/tdm-gcc/files/TDM-GCC%20Installer/tdm-gcc-4.5.1.exe/download (just you'll need dlltool.exe in theory but it can't hurt to install the entire suite)
- MSVC 2005 SP1 (Express Edition will work as well but you'll install the platform SDK in this case!)
- pexports.exe: http://www.emmestech.com/software/pexports-0.43/pexports-0.43.zip (you'll just need pexports.exe from the bin directory in the zip file)
- Python 2.7 source package: http://www.python.org/ftp/python/2.7/Python-2.7.tar.bz2 (unpack with a tool that can handle bzip2 archives such as 7zip)
Step #1: build Python from source
- Unpack the downloaded Python 2.7 source package
- cd into the <python27src>\PC\VS8.0 directory and open the pcbuild.sln solution with MSVC 2005 SP1
- In theory building the pcbuild.sln solution from <python27src>\PCbuild directory with MSVC 2008 should work fine as well. Unfortunately the resulting python_d.exe binary is borked somehow (at least for me) and my Windows 7 system complains that it's no legit win32 binary. It works fine for me with MSVC 2005 SP1 and the solution file mentioned above so use MSVC 2005 OR procede with MSVC 2008 at your own risk!
- Build the entire solution in debug mode.
- Don't worry about some Python libraries that will fail to build. They're not essential for the task at hand; we'll basically just need python27_d.dll (and python_d.exe for running the debug build of FIFE later).
Step #2: turning the python_d.dll into a static library that mingw can handle
- We now have a python_d.dll that has been built with MSVC 2005, let's copy it into your mingw\bin directory for further processing. The python_d.dll should reside in the <python27src>\PC\VS8.0 directory, where the pcbuild.sln solution file that you used for building can be found as well.
- Turning the DLL into .a file that mingw understand is a two step process. At first, we'll need pexports.exe for that purpose so let's unpack it from the downloaded pexports zip file (pexports.exe resides in the bin subdirectory) and extract it into your mingw\bin directory.
- Now we'll export the symbols from the python27_d.dll into a definition file. To do so, open a win32 command line window in the mingw\bin directory and enter: pexports.exe python27_d.dll > libpython27_d.def
- There are FAQs how to use pexports for creating .def files and it turned out that the proposed solution is to use the sed tool to filter underscores from the .dll, as mingw and MSVC use different ways to prefix procedures in their libraries. However filtering with sed will NOT work (at least for me) in combination with python27_d.dll.
- Now it's time to use dlltool from the mingw suite to create an .a library with the help of the generated .def file: dlltool.exe --add-stdcall-underscore -d libpython27_d.def -l libpython27_d.a
- FAQs on the topic usually advise to use the --add-underscore (= -U) parameter instead of the --add-stdcall-underscore parameter in combination with dlltool. However using the --add-underscore parameter resulted in a borked libpython27_d.a file for me. You can actually successfully link FIFE against this library but FIFE will terminate if you try to run a demo. The only solution I found is to use the --add-stdcall-underscore parameter as described above.
Step #3: finishing touches
- Copy libpython27_d.a from the mingw\bin directory into your <FIFE>\build\win32\static_libs\mingw\python27 directory
- Now you can build FIFE in debug mode by moving into the <FIFE>\build\win32\build_environments\scons directory and running: build_engine_debug.bat
Notes / Issues
- The biggest drawback is that python_d.exe and python27_d.dll files will need the MSVC debugging dlls to be in place on your system, or won't be able to run debug builds of FIFE. Unfortunately Microsoft doesn't permit to redistribute these files so we can't simply offer a precompiled Python package at this point that already comes with all necessary files that are necessary to debug debug builds of FIFE in gdb.