Skip to content

gh-158948: Add auto-generated Module/getbuildinfo.h - #158951

Draft
vstinner wants to merge 18 commits into
python:mainfrom
vstinner:getbuildinfo
Draft

vstinner wants to merge 18 commits into
python:mainfrom
vstinner:getbuildinfo

Conversation

@vstinner

@vstinner vstinner commented Oct 7, 2026 •

Copy link
Copy Markdown
Member

Merge Python/getcompiler.c, Python/getcopyright.c, Python/getplatform.c and Python/getversion.c into
Modules/getbuildinfo.c.

Add Tools/build/generate_getbuildinfo.py to auto-generate a new Module/getbuildinfo.h header file which contains build information strings.

Merge Python/getcompiler.c, Python/getcopyright.c,
Python/getplatform.c and Python/getversion.c into
Modules/getbuildinfo.c.

Add Tools/build/generate_getbuildinfo.py to auto-generate a new
Module/getbuildinfo.h header file which contains build information
strings.
@vstinner

vstinner commented Oct 7, 2026

Copy link
Copy Markdown
Member Author

This PR is a draft. I didn't investigate Windows support yet. I didn't try cross-compilation, it may break.

@vstinner

vstinner commented Oct 7, 2026

Copy link
Copy Markdown
Member Author

This change may fix the regression described by @itamaro: #158608 (comment).

@vstinner

vstinner commented Oct 7, 2026

Copy link
Copy Markdown
Member Author

Tools/build/generate_getbuildinfo.py the SOURCE_DATE_EPOCH environment variable for reproducible build: https://reproducible-builds.org/specs/source-date-epoch/. If it's set, use it for the date and time.

@vstinner

vstinner commented Oct 7, 2026

Copy link
Copy Markdown
Member Author

Ok, I fixed the out-of-tree build (used by the Ubuntu CI for example).

It's only needed in the Python script. Python.h defines PY_VERSION
(via patchlevel.h).
@vstinner

vstinner commented Oct 7, 2026

Copy link
Copy Markdown
Member Author

Example of auto-generated Modules/getbuildinfo.h header file:

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

#define DATE "Oct  7 2026"
#define TIME "05:35:34"
#define COMPILER "[GCC 16.2.1 20260819 (Red Hat 16.2.1-2)]"

#define PLATFORM "linux"

#define GITBRANCH "getbuildinfo"
#define GITTAG "heads/getbuildinfo"
#define GITVERSION "22d1a6b7c1e"
#define GIT_IDENTIFIER "heads/getbuildinfo"

#define BUILDINFO "heads/getbuildinfo:22d1a6b7c1e, Oct  7 2026, 05:35:34"
#define GET_VERSION "3.16.0a0 (heads/getbuildinfo:22d1a6b7c1e, Oct  7 2026, 05:35:34) [GCC 16.2.1 20260819 (Red Hat 16.2.1-2)]"

@vstinner

vstinner commented Oct 7, 2026 •

Copy link
Copy Markdown
Member Author

Ah, the "WASI / build and test" job does cross-compile! Running the Programs/_getcompiler program to get the compiler version fails with Exec format error.

/home/runner/work/cpython/cpython/cross-build/aarch64-unknown-linux-gnu/python ../../Tools/build/generate_getbuildinfo.py Modules/getbuildinfo.h \
		"wasi" \
		`LC_ALL=C git --git-dir ../../.git rev-parse --short HEAD` \
		`LC_ALL=C git --git-dir ../../.git describe --all --always --dirty` \
		`LC_ALL=C git --git-dir ../../.git name-rev --name-only HEAD` \
		"`Programs/_getcompiler`"
(...)
/bin/sh: 1: Programs/_getcompiler: Exec format error

Later, running test.pythinfo fails on collect_platform() because sys.version lacks the compiler:

ERROR: collect_platform() failed
Traceback (most recent call last):
  File "/Lib/test/pythoninfo.py", line 1418, in collect_info
    collect_func(info_add)
    ~~~~~~~~~~~~^^^^^^^^^^
  File "/Lib/test/pythoninfo.py", line 185, in collect_platform
    platform.python_implementation())
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/Lib/platform.py", line 1199, in python_implementation
    return _sys_version()[0]
           ~~~~~~~~~~~~^^
  File "/Lib/platform.py", line 1159, in _sys_version
    raise ValueError(
        'failed to parse CPython sys.version: %s' %
        repr(sys_version))
ValueError: failed to parse CPython sys.version: '3.16.0a0 (remotes/pull/158951/merge-dirty:00e222b, Oct  7 2026, 03:37:58) '

@vstinner

vstinner commented Oct 7, 2026

Copy link
Copy Markdown
Member Author

With the help of @zware, I managed to update the PCbuild project, to build Programs_getcompiler.c and generate Modules\getbuildinfo.h on Windows.

@vstinner

vstinner commented Oct 7, 2026

Copy link
Copy Markdown
Member Author

Aha, test_freeze_simple_script() of test_tools fails on Ubuntu / build and test (ubuntu-26.04-arm) with:


LC_ALL=C sed -e 's,\$(\([A-Za-z0-9_]*\)),\$\{\1\},g' < Misc/python-config.sh >python-config
python3.14 /tmp/test_python_0l4z8_yx/tmpt900bnty/cpython/Tools/build/generate_getbuildinfo.py Modules/getbuildinfo.h \
		"linux" \
		`LC_ALL=C ` \
		`LC_ALL=C ` \
		`LC_ALL=C ` \
		"`Programs/_getcompiler || echo COMPILER`" \
		use_pyconfig
usage: generate_getbuildinfo.py OUTPUT PLATFORM GITVERSION GITTAG GITBRANCH COMPILER GIL_ENABLED
make[1]: Leaving directory '/tmp/test_python_0l4z8_yx/tmpt900bnty/python-build'

@vstinner

vstinner commented Oct 7, 2026

Copy link
Copy Markdown
Member Author

Aha, test_freeze_simple_script() of test_tools fails on Ubuntu / build and test (ubuntu-26.04-arm)

It's just missing quotes: I fixed it.

* Use argparse for command line parsing.
* Run _getcompiler if --compiler option is omitted.
* Rename getbuildinfo.h variables.
@vstinner

vstinner commented Oct 7, 2026

Copy link
Copy Markdown
Member Author

Cool! The change now builds successfully on all GitHub Action jobs! I checked sys.version:

  • Windows / Build and test (x64, tail-call): CPython 3.16.0a0 (remotes/pull/158951/merge:1c30427, Oct 7 2026, 22:57:35) [MSC v.1951 32 bit (Intel)]
  • macOS / build and test (macos-26): CPython 3.16.0a0 (remotes/pull/158951/merge:1c30427, Oct 7 2026, 22:58:53) [Clang 21.0.0 (clang-2100.1.1.101)]
  • macOS (free-threading) / build and test (macos-26): CPython 3.16.0a0 free-threading build (remotes/pull/158951/merge:1c30427, Oct 7 2026, 23:00:13) [Clang 21.0.0 (clang-2100.1.1.101)]
  • Ubuntu / build and test (ubuntu-26.04): CPython 3.16.0a0 (remotes/pull/158951/merge-dirty:1c30427, Oct 7 2026, 23:03:04) [GCC 15.2.0]
  • Ubuntu (free-threading) / build and test (ubuntu-26.04-arm): CPython 3.16.0a0 free-threading build (remotes/pull/158951/merge-dirty:1c30427, Oct 7 2026, 22:58:15) [GCC 15.2.0]
  • Android (x86_64): CPython 3.16.0a0 (remotes/pull/158951/merge-dirty:1c30427, Oct 7 2026, 23:00:19) [Android (13691557, +pgo, +bolt, +lto, +mlgo, based on r522817d) clang version 18.0.4 (https://android.googlesource.com/toolchain/llvm-project d8003a456d14a3deb8054cdaa529ffbf02d9b262)]
  • iOS: CPython 3.16.0a0 (remotes/pull/158951/merge-dirty:1c30427, Oct 7 2026, 23:01:08) [Apple clang version 21.0.0 (clang-2100.1.1.101)]
  • Emscripten / build and test: CPython 3.16.0a0 (remotes/pull/158951/merge-dirty:1c30427, Oct 7 2026, 22:53:20) [Clang 24.0.0git (https:/github.com/llvm/llvm-project a06d9165905ce89b5ffef2bbb84c886d60a9b8bf)]
  • WASI / build and test: CPython 3.16.0a0 (remotes/pull/158951/merge-dirty:1c30427, Oct 7 2026, 22:58:12) [GCC 15.2.0]
  • Cross build Linux: sys.version: 3.16.0a0 (remotes/pull/158951/merge:1c30427, Oct 7 2026, 22:59:42) [GCC 15.2.0]

Some Clang versions are quite verbose/long. I will update the PR to strip the URL.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant