Creating a custom python debug library for mingw

From FIFE development wiki
Jump to: navigation, search


The problem

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 solution

The only way to address the problem is create a (lib)python27_d.a library yourself. This guide describes how to tackle that task.

Required software

Steps

Step #1: build Python from source

  1. Unpack the downloaded Python 2.7 source package
  2. 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!
  3. 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

  1. 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.
  2. 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.
  3. 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: Command.pngpexports.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.
  4. Now it's time to use dlltool from the mingw suite to create an .a library with the help of the generated .def file: Command.pngdlltool.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

  1. Copy libpython27_d.a from the mingw\bin directory into your <FIFE>\build\win32\static_libs\mingw\python27 directory
  2. 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.