Implement dynamic git version information in build metadata

- Added CMake git detection to capture branch and commit hash
- Modified frozen_stubs.c to use CMake-provided git information
- Build info now shows actual git metadata instead of "default"
- Updated STUBS.md to reflect improvement

Co-authored-by: johndoe6345789 <224850594+johndoe6345789@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2025-12-28 23:41:56 +00:00
parent 9af9379805
commit f55db59a1a
3 changed files with 55 additions and 10 deletions

View File

@@ -16,6 +16,28 @@ set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# Generate git version information
find_package(Git QUIET)
if(GIT_FOUND)
execute_process(
COMMAND ${GIT_EXECUTABLE} rev-parse --short HEAD
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE GIT_COMMIT_HASH
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
)
execute_process(
COMMAND ${GIT_EXECUTABLE} rev-parse --abbrev-ref HEAD
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE GIT_BRANCH
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
)
else()
set(GIT_COMMIT_HASH "unknown")
set(GIT_BRANCH "unknown")
endif()
# Platform detection
if(UNIX AND NOT APPLE)
set(LINUX TRUE)
@@ -27,6 +49,12 @@ if(NOT MSVC)
add_compile_options(-fwrapv -fno-strict-aliasing)
endif()
# Add git version information as compile definitions
add_compile_definitions(
GIT_COMMIT_HASH="${GIT_COMMIT_HASH}"
GIT_BRANCH="${GIT_BRANCH}"
)
# Collect Python core source files
file(GLOB PYTHON_SOURCES Python/*.c)
file(GLOB OBJECTS_SOURCES Objects/*.c)

View File

@@ -34,22 +34,32 @@ struct _inittab _PyImport_Inittab[] = {
};
/* Build info stubs */
/* Use CMake-generated git information if available */
#ifndef GIT_COMMIT_HASH
#define GIT_COMMIT_HASH "unknown"
#endif
#ifndef GIT_BRANCH
#define GIT_BRANCH "default"
#endif
const char *
Py_GetBuildInfo(void)
{
return "Typthon 3.14.0 (default, " __DATE__ " " __TIME__ ")";
return "Typthon 3.14.0 (" GIT_BRANCH ":" GIT_COMMIT_HASH ", " __DATE__ " " __TIME__ ")";
}
const char *
_Py_gitidentifier(void)
{
return "default";
return GIT_BRANCH;
}
const char *
_Py_gitversion(void)
{
return "Typthon 3.14.0";
return "Typthon 3.14.0:" GIT_COMMIT_HASH;
}
/* DL open flags stub */

View File

@@ -34,12 +34,19 @@ All stubs are implemented in: `Python/frozen_stubs.c`
**Purpose**: Provide version and build metadata to the interpreter.
**Stubs Created**:
- `Py_GetBuildInfo()` - Returns build date/time string
- `_Py_gitidentifier()` - Returns git branch identifier ("default")
- `_Py_gitversion()` - Returns version string ("Typthon 3.14.0")
**Status**: ✅ **IMPROVED** - Git metadata is now dynamically generated at build time.
**Implementation**: Simple string returns with minimal metadata.
**Implementation**:
- `Py_GetBuildInfo()` - Returns build string with git branch, commit hash, and build timestamp
- `_Py_gitidentifier()` - Returns actual git branch name from CMake
- `_Py_gitversion()` - Returns version string with commit hash
**Resolution**: Added CMake configuration to detect git information at build time and pass it as compile definitions. The functions now return actual git metadata instead of placeholder values.
**Changes Made**:
- Modified `CMakeLists.txt` to detect git branch and commit hash
- Updated `Python/frozen_stubs.c` to use CMake-generated definitions
- Build info now shows: "Typthon 3.14.0 (branch:hash, date time)" format
### 4. Dynamic Loading
@@ -111,7 +118,7 @@ These stubs mean the following features are not available in this build:
1. **No frozen modules**: Cannot embed Python code as frozen C arrays
2. ~~**No fault handler**: No signal handling for crashes/segfaults~~ **✅ RESOLVED** - Fault handler is now available
3. **No plock**: No Solaris-style process memory locking
4. **Minimal build info**: Git metadata is stubbed with placeholder values
4. ~~**Minimal build info**: Git metadata is stubbed with placeholder values~~ **✅ IMPROVED** - Git metadata now shows actual branch and commit
5. **No custom built-in modules**: Only modules explicitly linked are available
## Future Improvements
@@ -121,7 +128,7 @@ To get a fully-functional Python interpreter, the following would be needed:
1. Generate actual frozen modules using `Tools/build/freeze_modules.py`
2. ~~Re-enable and fix the faulthandler module compilation~~ **✅ COMPLETED**
3. Implement proper path configuration in `_PyConfig_InitPathConfig()`
4. Generate real build information with git metadata
4. ~~Generate real build information with git metadata~~ **✅ COMPLETED**
5. Add more built-in modules to the `_PyImport_Inittab` table
## Testing