Generate `Modules/getbuildinfo.h` when building Python to get build information statically

Hi,

In short, please test my PR gh-158951 if you cross-compile Python or build Python in a specific way. The PR changes Python build system to run a new generate_getbuildinfo.py script which generates getbuildinfo.h file.


Recently, I modified Python to no longer use a static buffer of 300 bytes, but allocate memory on the heap to create sys.version string in Py_GetVersion(). Previously, the compiler and build information (Git branch/tag/verison, date and time) could be truncated in sys.version. I expected all users to call Py_GetVersion(), but you cannot call functions on a crashed process for example.

Itamar reported that this change breaks debugger and crash reporters which read directly the version static buffer. My change truncates this buffer to 20 bytes, since it should no longer be used. I didn’t expect the debugger/crash reporter case.

I worked on a PR to change how build information are collected and formatted. I created PR gh-158951 which adds a Python script Tools/build/generate_getbuildinfo.py to generate a clean Modules/getbuildinfo.h header file. Example:

// Header file auto-generated by Tools/build/generate_getbuildinfo.py

#define PLATFORM "linux"
#define COMPILER "[GCC 16.2.1 20260819 (Red Hat 16.2.1-2)]"
#define GIT_VERSION "8431d21905b"
#define GIT_IDENTIFIER "heads/getbuildinfo"
#define BUILD_INFO "heads/getbuildinfo:8431d21905b, Oct  8 2026, 17:52:28"
#define VERSION "3.16.0a0 (heads/getbuildinfo:8431d21905b, Oct  8 2026, 17:52:28) [GCC 16.2.1 20260819 (Red Hat 16.2.1-2)]"

I tested my change on all GitHub Actions and buildbots: it works on all cases tested by these CIs.


On Android and iOS, the new Programs/_getcompiler program cannot be run since it’s built for a different CPU. In this case, $CC --version is run to get the compiler version. In other cases, the existing code using COMPILER and _Py_COMPILER (on Windows) macros is stil used; the only change is that a new Programs/_getcompiler is used to get the compiler string.

The generate_getbuildinfo.py collects data from:

  • Its command line: Makefile and PCbuild/_freeze_module.vcxproj pass some information, like the platform, the Git version/tag/branch on Unix, or Free Threading flag on Windows.
  • Get CC and HOSTRUNNER from Makefile.
  • Run Programs/_getcompiler (using HOSTRUNNER if specified) to get the compiler string.
  • Get Py_GIL_DISABLED macro from pyconfig.h.
  • Get Py_VERSION string from Include/patchlevel.h.
  • Use SOURCE_DATE_EPOCH environment variable to support reproducible build.

I see multiple advantages to add the generate_getbuildinfo.py script and getbuildinfo.h header:

  • The Clang version string is normalized (truncated).
  • Add support for SOURCE_DATE_EPOCH environment variable for reproducible build.
  • Py_GetBuildInfo() and Py_GetVersion() no longer have to format the string at the first call: it’s now computed statically during Python build.
  • Later, it will be easiler to modify the code to allow overridding the compiler string for reproducible build: see PR gh-144124.

To me, it seems cleaner to have a Python generate_getbuildinfo.py script to process, cleanup, and validate all build information. Since it’s written in Python, it’s also simpler to main, and I added unit tests!


Ok, now comes the question of the Clang version string. Currently, the script truncates long versions, like stripping long URLs. Examples:

  • Replace Android (13691557, +pgo, +bolt, +lto, +mlgo, based on r522817d) clang version 18.0.4 (https://android.googlesource.com/toolchain/llvm-project d8003a456d14a3deb8054cdaa529ffbf02d9b262) with Android Clang 18.0.4.
  • Replace Clang 18.0.4 (https://android.googlesource.com/toolchain/llvm-project d8003a456d14a3deb8054cdaa529ffbf02d9b262) with Clang 18.0.4.
  • Replace Clang 24.0.0git (https:/github.com/llvm/llvm-project a06d9165905ce89b5ffef2bbb84c886d60a9b8bf) with Clang 24.0.0git.
  • Replace Apple clang version 21.0.0 (clang-2100.1.1.101) with Apple Clang 21.0.0.
  • Replace Clang 21.0.0 (clang-2100.1.1.101) with Clang 21.0.0.

I chose to keep Android or Apple “vendor” prefix (ex Android Clang 18.0.4). I’m not sure if it’s useful.

The compiler string is displayed every time you start Python REPL, it’s the first line:

$ python3.15
Python 3.15.0rc2 (3.15.0~rc2-1.fc44.x86_64, Sep  3 2026, 00:00:00) [GCC 16.2.1 20260819 (Red Hat 16.2.1-2)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> sys.version
'3.15.0rc2 (3.15.0~rc2-1.fc44.x86_64, Sep  3 2026, 00:00:00) [GCC 16.2.1 20260819 (Red Hat 16.2.1-2)]'

Only the Clang version is normalized/truncated. For other C compilers, like GCC and MSC, the compiler version is left unchanged. For example, GCC 16.2.1 20260819 (Red Hat 16.2.1-2) is not replaced with GCC 16.2.1. Should we normalize other compiler strings?

Victor