Tuesday, November 9, 2010

Building Python in MSVC2010 with static runtime.

This guide will show an incomplete way how to build CPython (3.1.2, but I think it will work for another versions too) in MSVC2010 with static runtime (/MT option).

  1. Download a source, http://www.python.org/ftp/python/3.1.2/Python-3.1.2.tar.bz2 . Unpack it.
  2. Open .\Python-3.1.2\PCbuild\pcbuild.sln , MSVS will convert it to VC10 version. (You should have latest version of MSVC2010 or an installed hotfix for project converting).
  3. Open configuration manager and create new configuration "ReleaseStatic" based on "Release" configuration. Select all projects, open project properties and set Runtime = "Multi-Threaded (/MT)" in Code Generation settings. You can also adjust another properties like LTCG, etc.
Now, if you will press "Build All" some will successfully built, but pythoncore (pythonXX.dll) won't.  There will be link errors because of shared runtime (although we set static runtime). It's because of pythoncore pre-link event, which does
cl.exe ... -MD ..\Modules\getbuildinfo.c -Fogetbuildinfo.o -I..\Include -I..\PC

  1. Open .\Python-3.1.2\PCbuild\make_buildinfo.c  and apply the following patch:
    @@ -61,7 +61,10 @@
             fprintf(stderr, "make_buildinfo $(ConfigurationName)\n");
             return EXIT_FAILURE;
         }
    -    if (strcmp(argv[1], "Release") == 0) {
    +    if (strcmp(argv[1], "ReleaseStatic") == 0) {
    +        strcat_s(command, CMD_SIZE, "-MT ");
    +    }
    +    else if (strcmp(argv[1], "Release") == 0) {
             strcat_s(command, CMD_SIZE, "-MD ");
         }
         else if (strcmp(argv[1], "Debug") == 0) {
     
    Then pythoncore configuration properties, build events, pre-link event, and write there
    "$(SolutionDir)make_buildinfo.exe" ReleaseStatic 
To be edited and maybe continued.

No comments:

Post a Comment