{"id":29,"date":"2011-10-18T16:12:14","date_gmt":"2011-10-18T19:12:14","guid":{"rendered":"http:\/\/mdqinc.com\/blog\/?p=29"},"modified":"2012-12-10T09:51:34","modified_gmt":"2012-12-10T12:51:34","slug":"cross-compiling-python-for-windows-with-mingw32","status":"publish","type":"post","link":"https:\/\/mdqinc.com\/blog\/2011\/10\/cross-compiling-python-for-windows-with-mingw32\/","title":{"rendered":"Cross Compiling Python For Windows With Mingw32"},"content":{"rendered":"<p>If you are interested in cross compiling Python 2.7.2 for use in Windows using the Mingw32 cross compiling tools I&#8217;ll suggest that you first read my post on <a href=\"http:\/\/mdqinc.com\/blog\/2011\/09\/cross-compiling-python-for-android\/\"  target=\"_blank\">how to cross compile it for Android<\/a> as much of the instructions are the same.<\/p>\n<p>Again, this instructions are tested under Ubuntu 11.04 (Natty) 64 bits . <a href=\"https:\/\/bitbucket.org\/gabomdq\/ignifuga\/raw\/baf7883aa5d693352a9ca65b763f51e1407e311d\/tools\/patches\/python.mingw32.diff\"  target=\"_blank\">The patch<\/a> that you&#8217;ll need is based on the Android patch, and several other pieces from around the web (including several <a href=\"http:\/\/bugs.python.org\/issue3871\" target=\"_blank\">proposed patches<\/a> in the Python tracker).<\/p>\n<p>Instead of repeating the instructions all over again, I&#8217;ll just point out the differences in the process. Assuming you&#8217;ve already downloaded <a href=\"http:\/\/www.python.org\/getit\/releases\/2.7.2\/\" target=\"_blank\">Python&#8217;s 2.7.2 source<\/a>, <a href=\"https:\/\/bitbucket.org\/gabomdq\/ignifuga\/raw\/baf7883aa5d693352a9ca65b763f51e1407e311d\/tools\/patches\/python.mingw32.diff\"  target=\"_blank\">the patch<\/a>, created a host version of the Python interpreter and applied the patch for cross compilation, you&#8217;ll then need to install the Mingw32 toolset (if you haven&#8217;t already):<\/p>\n<pre class=\"brush:bash\">sudo apt-get install mingw32 mingw32-binutils mingw32-runtime<\/pre>\n<p>Prepare the environment:<\/p>\n<pre class=\"brush:bash\">export ARCH=\"win32\"\r\nexport CFLAGS=\"\"\r\nexport CXXFLAGS=\"\"\r\nexport CC=\"i586-mingw32msvc-gcc\"\r\nexport CXX=\"i586-mingw32msvc-g++\"\r\nexport AR=\"i586-mingw32msvc-ar\"\r\nexport RANLIB=\"i586-mingw32msvc-ranlib\"\r\nexport STRIP=\"i586-mingw32msvc-strip --strip-unneeded\"\r\nexport LD=\"i586-mingw32msvc-ld\"\r\nexport AS=\"i586-mingw32msvc-as\"\r\nexport NM=\"i586-mingw32msvc-nm\"\r\nexport DLLTOOL=\"i586-mingw32msvc-dlltool\"\r\nexport OBJDUMP=\"i586-mingw32msvc-objdump\"\r\nexport RESCOMP=\"i586-mingw32msvc-windres\"\r\nexport MAKE=\"make -k -j4 HOSTPYTHON=[PATH TO HOST PYTHON] HOSTPGEN=[PATH TO HOST PGEN]  CROSS_COMPILE=mingw32msvc CROSS_COMPILE_TARGET=yes\"\r\nexport EXTRALIBS=\"-lstdc++ -lgcc -lodbc32 -lwsock32 -lwinspool -lwinmm -lshell32 -lcomctl32 -lctl3d32 -lodbc32 -ladvapi32 -lopengl32 -lglu32 -lole32 -loleaut32 -luuid\"<\/pre>\n<p>Then configure the build:<\/p>\n<pre class=\"brush:bash\">.\/configure LDFLAGS=\"-Wl,--no-export-dynamic -static-libgcc -static $EXTRALIBS\" CFLAGS=\"-DMS_WIN32 -DMS_WINDOWS -DHAVE_USABLE_WCHAR_T\" CPPFLAGS=\"-static\" LINKFORSHARED=\" \" LIBOBJS=\"import_nt.o dl_nt.o getpathp.o\" THREADOBJ=\"Python\/thread.o\" DYNLOADFILE=\"dynload_win.o\" --disable-shared HOSTPYTHON=[PATH TO HOST PYTHON] HOSTPGEN=[PATH TO HOST PGEN] --host=i586-mingw32msvc --build=i686-pc-linux-gnu  --prefix=\"[WHERE YOU WANT TO PUT THE GENERATED PYTHON STUFF]\"<\/pre>\n<p>You may have noticed that there&#8217;s a few static compilation related switches in there. I think it&#8217;s possible to compile Python without them, but as I&#8217;m building a monolithic app with all the modules compiled in the same exe file, I haven&#8217;t really tested the shared library building part of the process. If you do test with shared libraries enabled, then feel free to leave feedback in the comments and I&#8217;ll update the article with whatever caveats there might be.<\/p>\n<p>Now comes the hacky stuff&#8230;not everything that should be fixed for the cross compilation to succeed is fixed in the patch, so we need to do some manual repairs after the config process is finished.<\/p>\n<pre class=\"brush:bash\">sed -i \"s|\\${LIBOBJDIR}fileblocks\\$U\\.o||g\" Makefile\r\n# Enable NT Threads\r\nsed -i \"s|.*NT_THREADS.*|#define NT_THREADS|g\" pyconfig.h\r\n# Disable PTY stuff that gets activated because of errors in the configure script\r\nsed -i \"s|.*HAVE_OPENPTY.*|#undef HAVE_OPENPTY|g\" pyconfig.h\r\nsed -i \"s|.*HAVE__GETPTY.*|#undef HAVE__GETPTY|g\" pyconfig.h\r\nsed -i \"s|.*HAVE_DEV_PTMX.*|#undef HAVE_DEV_PTMX|g\" pyconfig.h<\/pre>\n<p>Finally:<\/p>\n<pre class=\"brush:bash\">$MAKE<\/pre>\n<p>Hopefully you&#8217;ll get a static python binary (.exe) and also a static library that you can link against other files. The interactive console seems to work as well (if you copy the generated files to Windows and run python.exe)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you are interested in cross compiling Python 2.7.2 for use in Windows using the Mingw32 cross compiling tools I&#8217;ll suggest that you first read my post on how to cross compile it for Android as much of the instructions are the same. Again, this instructions are tested under Ubuntu 11.04 (Natty) 64 bits . [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[3,4],"tags":[],"_links":{"self":[{"href":"https:\/\/mdqinc.com\/blog\/wp-json\/wp\/v2\/posts\/29"}],"collection":[{"href":"https:\/\/mdqinc.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mdqinc.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mdqinc.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mdqinc.com\/blog\/wp-json\/wp\/v2\/comments?post=29"}],"version-history":[{"count":4,"href":"https:\/\/mdqinc.com\/blog\/wp-json\/wp\/v2\/posts\/29\/revisions"}],"predecessor-version":[{"id":69,"href":"https:\/\/mdqinc.com\/blog\/wp-json\/wp\/v2\/posts\/29\/revisions\/69"}],"wp:attachment":[{"href":"https:\/\/mdqinc.com\/blog\/wp-json\/wp\/v2\/media?parent=29"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mdqinc.com\/blog\/wp-json\/wp\/v2\/categories?post=29"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mdqinc.com\/blog\/wp-json\/wp\/v2\/tags?post=29"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}