Skip to content

Commit 3b179ca

Browse files
committed
generate_getbuildinfo.py now runs git commands on Windows
Check also that the string doesn't contain embedded null character.
1 parent e53aa5d commit 3b179ca

3 files changed

Lines changed: 30 additions & 16 deletions

File tree

‎Lib/test/test_tools/test_getbuildinfo.py‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ def test_functional(self):
164164
argv = [
165165
'program',
166166
'--output', filename,
167-
'--platform', 'SET_PLATFORM',
167+
# Inject a quote to check that it's escaped
168+
'--platform', 'SET"PLATFORM',
168169
'--git-version', 'VERSION',
169170
'--git-tag', 'TAG',
170171
'--git-branch', 'BRANCH',
@@ -180,10 +181,10 @@ def test_functional(self):
180181

181182
with open(filename) as fp:
182183
output = fp.read()
183-
expected = textwrap.dedent('''
184+
expected = textwrap.dedent(r'''
184185
// Header file auto-generated by Tools/build/generate_getbuildinfo.py
185186
186-
#define PLATFORM "SET_PLATFORM"
187+
#define PLATFORM "SET\"PLATFORM"
187188
#define COMPILER "[COMPILER]"
188189
#define GIT_VERSION "VERSION"
189190
#define GIT_IDENTIFIER "TAG"

‎PCbuild/_freeze_module.vcxproj‎

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -467,31 +467,25 @@
467467
Condition="'@(_UpdatedGetPath)' != ''" Importance="high" />
468468
</Target>
469469
<Target Name="_GetBuildInfoHeader" BeforeTargets="PrepareForBuild"
470-
Inputs="" Outputs="Modules\getbuildinfo.h"
470+
Inputs="Tools\build\generate_getbuildinfo.py"
471+
Outputs="Modules\getbuildinfo.h"
471472
DependsOnTargets="FindPythonForBuild">
472473
<PropertyGroup>
473474
<GIT Condition="$(GIT) == ''">git</GIT>
474475
<_GIT>$(GIT)</_GIT>
475-
<_GIT Condition="$(GIT.Contains(` `))">"$(GIT)"</_GIT>
476476
</PropertyGroup>
477-
<Message Text="Getting build info from $(_GIT)" Importance="high" />
477+
<Message Text="Getting compiler version string" Importance="high" />
478478
<MakeDir Directories="$(IntDir)" Condition="!Exists($(IntDir))" />
479-
<Exec Command="$(_GIT) name-rev --name-only HEAD &gt; &quot;$(IntDir)gitbranch.txt&quot;" ContinueOnError="true" />
480-
<Exec Command="$(_GIT) rev-parse --short HEAD &gt; &quot;$(IntDir)gitversion.txt&quot;" ContinueOnError="true" />
481-
<Exec Command="$(_GIT) describe --all --always --dirty &gt; &quot;$(IntDir)gittag.txt&quot;" ContinueOnError="true" />
482479
<Exec Command='"$(GetCompilerExe)" &gt; &quot;$(IntDir)compiler.txt&quot;' ContinueOnError="true" />
483480
<PropertyGroup>
484-
<GitBranch Condition="Exists('$(IntDir)gitbranch.txt')">$([System.IO.File]::ReadAllText('$(IntDir)gitbranch.txt').Trim())</GitBranch>
485-
<GitVersion Condition="Exists('$(IntDir)gitversion.txt')">$([System.IO.File]::ReadAllText('$(IntDir)gitversion.txt').Trim())</GitVersion>
486-
<GitTag Condition="Exists('$(IntDir)gittag.txt')">$([System.IO.File]::ReadAllText('$(IntDir)gittag.txt').Trim())</GitTag>
487481
<Compiler Condition="Exists('$(IntDir)compiler.txt')">$([System.IO.File]::ReadAllText('$(IntDir)compiler.txt').Trim())</Compiler>
488482

489483
<FreeThreading Condition="$(DisableGil) == 'true'">1</FreeThreading>
490484
<FreeThreading Condition="$(DisableGil) != 'true'">0</FreeThreading>
491485
</PropertyGroup>
492486

493487
<Message Text="Run generate_getbuildinfo.py" Importance="high" />
494-
<Exec Command='$(PythonForBuild) Tools/build/generate_getbuildinfo.py --platform=win32 --git-version="$(GitVersion)" --git-tag="$(GitTag)" --git-branch="$(GitBranch)" --compiler="$(Compiler)" --free-threading=$(FreeThreading)'
488+
<Exec Command='$(PythonForBuild) Tools\build\generate_getbuildinfo.py --platform=win32 --git-program="$(_GIT)" --compiler="$(Compiler)" --free-threading=$(FreeThreading)'
495489
WorkingDirectory="$(PySourcePath)" />
496490
</Target>
497491
<Target Name="_RebuildFrozen" AfterTargets="AfterBuild" Condition="$(Configuration) != 'PGUpdate'">

‎Tools/build/generate_getbuildinfo.py‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,10 @@ def parse_args():
121121
parser = argparse.ArgumentParser()
122122
parser.add_argument('-o', '--output', type=str)
123123
parser.add_argument('--platform', type=str, required=True)
124-
parser.add_argument('--git-version', type=str, required=True)
125-
parser.add_argument('--git-tag', type=str, required=True)
126-
parser.add_argument('--git-branch', type=str, required=True)
124+
parser.add_argument('--git-program', type=str)
125+
parser.add_argument('--git-version', type=str)
126+
parser.add_argument('--git-tag', type=str)
127+
parser.add_argument('--git-branch', type=str)
127128
parser.add_argument('--compiler', type=str)
128129
parser.add_argument('--free-threading', type=int)
129130
return parser.parse_args()
@@ -228,11 +229,25 @@ def main():
228229
args = parse_args()
229230
output_filename = args.output
230231
platform = args.platform
232+
git_program = args.git_program
231233
git_version = args.git_version
232234
git_tag = args.git_tag
233235
git_branch = args.git_branch
234236
free_threading = args.free_threading
235237

238+
if git_program:
239+
git_dir = os.path.join(SRC_DIR, '.git')
240+
git_program = [git_program, '--git-dir', git_dir]
241+
if git_version is None:
242+
cmd = [*git_program, 'rev-parse', '--short', 'HEAD']
243+
git_version = run_command(cmd, check=False) or ''
244+
if git_branch is None:
245+
cmd = [*git_program, 'describe', '--all', '--always', '--dirty']
246+
git_branch = run_command(cmd, check=False) or ''
247+
if git_tag is None:
248+
cmd = [*git_program, 'name-rev', '--name-only', 'HEAD']
249+
git_tag = run_command(cmd, check=False) or ''
250+
236251
if not output_filename:
237252
# Write to the current directory
238253
output_filename = os.path.join('Modules', 'getbuildinfo.h')
@@ -258,6 +273,10 @@ def main():
258273
new_filename = output_filename + ".new"
259274
with open(new_filename, "w", encoding="utf8") as fp:
260275
def write_macro(name, value):
276+
# Escape quote
277+
value = value.replace('"', '\\"')
278+
if '\0' in value:
279+
raise ValueError(f'embedded null character: {value!r}')
261280
print(f'#define {name} "{value}"', file=fp)
262281

263282
print(f'// Header file auto-generated by {SCRIPT_FULLNAME}', file=fp)

0 commit comments

Comments
 (0)