Skip to content

Commit 912e169

Browse files
committed
gh-111650: Generate pyconfig.h on Windows
Prior to this change, the Py_NOGIL macro was not defined when building C API extensions with the `--disable-gil` build on Windows. `Py_NOGIL` was only defined as a pre-processor definition in pyproject.props, but that is not used by C-API extensions. This instead generates the `pyconfig.h` header on Windows as part of the build process. For now, `Py_NOGIL` is the only macro that may be conditionally defined in the generated file.
1 parent 974847b commit 912e169

File tree

4 files changed

+62
-1
lines changed

4 files changed

+62
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ Programs/_testembed
9292
PC/python_nt*.h
9393
PC/pythonnt_rc*.h
9494
Modules/python.exp
95+
PC/pyconfig.h
9596
PC/*/*.exp
9697
PC/*/*.lib
9798
PC/*/*.bsc

PC/pyconfig.h renamed to PC/pyconfig.h.in

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef Py_CONFIG_H
22
#define Py_CONFIG_H
33

4-
/* pyconfig.h. NOT Generated automatically by configure.
4+
/* pyconfig.h.in.
55
66
This is a manually maintained version used for the Watcom,
77
Borland and Microsoft Visual C++ compilers. It is a
@@ -710,6 +710,9 @@ Py_NO_ENABLE_SHARED to find out. Also support MS_NO_COREDLL for b/w compat */
710710
/* Define to 1 if you have the `erfc' function. */
711711
#define HAVE_ERFC 1
712712

713+
/* Define if you want to disable the GIL */
714+
#undef Py_NOGIL
715+
713716
// netdb.h functions (provided by winsock.h)
714717
#define HAVE_GETHOSTNAME 1
715718
#define HAVE_GETHOSTBYADDR 1

PCbuild/generate_pyconfig.ps1

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#
2+
# Generates pyconfig.h from PC\pyconfig.h.in
3+
#
4+
5+
param (
6+
[string[]]$define,
7+
[string]$File
8+
)
9+
10+
$definedValues = @{}
11+
12+
foreach ($arg in $define) {
13+
$parts = $arg -split '='
14+
15+
if ($parts.Count -eq 1) {
16+
$key = $parts[0]
17+
$definedValues[$key] = ""
18+
} elseif ($parts.Count -eq 2) {
19+
$key = $parts[0]
20+
$value = $parts[1]
21+
$definedValues[$key] = $value
22+
} else {
23+
Write-Host "Invalid argument: $arg"
24+
exit 1
25+
}
26+
}
27+
28+
$cpythonRoot = Split-Path $PSScriptRoot -Parent
29+
$pyconfigPath = Join-Path $cpythonRoot "PC\pyconfig.h.in"
30+
31+
$header = "/* pyconfig.h. Generated from PC\pyconfig.h.in by generate_pyconfig.ps1. */"
32+
33+
$lines = Get-Content -Path $pyconfigPath
34+
$lines = @($header) + $lines
35+
36+
foreach ($i in 0..($lines.Length - 1)) {
37+
if ($lines[$i] -match "^#undef (\w+)$") {
38+
$key = $Matches[1]
39+
if ($definedValues.ContainsKey($key)) {
40+
$value = $definedValues[$key]
41+
$lines[$i] = "#define $key $value".TrimEnd()
42+
} else {
43+
$lines[$i] = "/* #undef $key */"
44+
}
45+
}
46+
}
47+
48+
$ParentDir = Split-Path -Path $File -Parent
49+
New-Item -ItemType Directory -Force -Path $ParentDir | Out-Null
50+
Set-Content -Path $File -Value $lines

PCbuild/pcbuild.proj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props"/>
44
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props"/>
55
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets"/>
6+
<Import Project="python.props" />
67
<PropertyGroup Label="Globals">
78
<ProjectGuid>{CC9B93A2-439D-4058-9D29-6DCF43774405}</ProjectGuid>
89
<Platform Condition="'$(Platform)' == ''">Win32</Platform>
@@ -14,6 +15,7 @@
1415
<IncludeSSL Condition="'$(IncludeSSL)' == ''">true</IncludeSSL>
1516
<IncludeTkinter Condition="'$(IncludeTkinter)' == ''">true</IncludeTkinter>
1617
<IncludeUwp Condition="'$(IncludeUwp)' == ''">false</IncludeUwp>
18+
<PyConfigArgs Condition="'$(DisableGil)' == 'true'">Py_NOGIL=1,$(PyConfigArgs)</PyConfigArgs>
1719
</PropertyGroup>
1820

1921
<ItemDefinitionGroup>
@@ -94,6 +96,11 @@
9496
<Projects2 Include="venvlauncher.vcxproj;venvwlauncher.vcxproj" />
9597
</ItemGroup>
9698

99+
<Target Name="PreBuild" BeforeTargets="Build">
100+
<!-- Stick a _PLACEHOLDER=1 after $(PyConfigArgs) to handle both trailing commas and empty $(PyConfigArgs) -->
101+
<Exec Command="powershell.exe $(PySourcePath)PCbuild\generate_pyconfig.ps1 -File $(PySourcePath)PC\pyconfig.h -define $(PyConfigArgs)_PLACEHOLDER=1" />
102+
</Target>
103+
97104
<Target Name="Build">
98105
<MSBuild Projects="@(FreezeProjects)"
99106
Properties="Configuration=%(Configuration);Platform=%(Platform);%(Properties)"

0 commit comments

Comments
 (0)