Skip to content

Commit 2c9bafb

Browse files
committed
Support old sysconfig versions
1 parent ebaadbc commit 2c9bafb

2 files changed

Lines changed: 57 additions & 35 deletions

File tree

‎Lib/test/test_tools/test_getbuildinfo.py‎

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -70,37 +70,37 @@ def test_get_build_info(self):
7070
def test_get_compiler(self):
7171
getcompiler = os.path.join('Programs', '_getcompiler')
7272
MOCK_COMPILER = 'MOCK_COMPILER'
73+
CC = 'gcc -std=c11'
7374

7475
for hostrunner in ('', 'node'):
75-
makefile_vars = {
76-
'HOSTRUNNER': hostrunner,
77-
'CC': 'gcc -std=c11',
78-
}
7976
for use_cc in (False, True):
80-
with mock.patch.object(generate_getbuildinfo,
81-
'get_makefile_vars',
82-
return_value=makefile_vars):
83-
with mock.patch.object(generate_getbuildinfo,
84-
'run_command') as mock_run_command:
85-
if use_cc:
86-
mock_run_command.side_effect = ('', MOCK_COMPILER)
87-
else:
88-
mock_run_command.return_value = MOCK_COMPILER
89-
compiler = generate_getbuildinfo._get_compiler()
90-
self.assertEqual(compiler, MOCK_COMPILER)
91-
if hostrunner:
92-
cmd = [hostrunner, getcompiler]
93-
else:
94-
cmd = [getcompiler]
95-
if use_cc:
96-
self.assertEqual(mock_run_command.call_args_list,
97-
[
98-
mock.call(cmd, check=False),
99-
mock.call(['gcc', '-std=c11', '--version'], check=False),
100-
]
101-
)
102-
else:
103-
mock_run_command.assert_called_once_with(cmd, check=False)
77+
with (mock.patch.object(generate_getbuildinfo,
78+
'get_hostrunner',
79+
return_value=hostrunner),
80+
mock.patch.object(generate_getbuildinfo,
81+
'get_makefile_cc',
82+
return_value=CC),
83+
mock.patch.object(generate_getbuildinfo,
84+
'run_command') as mock_run_command):
85+
if use_cc:
86+
mock_run_command.side_effect = ('', MOCK_COMPILER)
87+
else:
88+
mock_run_command.return_value = MOCK_COMPILER
89+
compiler = generate_getbuildinfo._get_compiler()
90+
self.assertEqual(compiler, MOCK_COMPILER)
91+
if hostrunner:
92+
cmd = [hostrunner, getcompiler]
93+
else:
94+
cmd = [getcompiler]
95+
if use_cc:
96+
self.assertEqual(mock_run_command.call_args_list,
97+
[
98+
mock.call(cmd, check=False),
99+
mock.call(['gcc', '-std=c11', '--version'], check=False),
100+
]
101+
)
102+
else:
103+
mock_run_command.assert_called_once_with(cmd, check=False)
104104

105105
def test_compact_compiler(self):
106106
get_compiler = generate_getbuildinfo.get_compiler

‎Tools/build/generate_getbuildinfo.py‎

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import shlex
88
import subprocess
99
import sys
10-
import sysconfig.__main__
10+
import sysconfig
1111
import time
1212

1313
SRC_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..'))
@@ -17,6 +17,13 @@
1717
# Get PY_VERSION from Include/patchlevel.h
1818
PY_VERSION_REGEX = re.compile(r'^#define PY_VERSION +"(.*)"$', re.MULTILINE)
1919

20+
def create_makefile_regex(name):
21+
return re.compile(fr'^{name}[ \t]*=[ \t]*(.*)$', re.MULTILINE)
22+
23+
# Get HOSTRUNNER and CC from Makefile
24+
HOSTRUNNER_REGEX = create_makefile_regex('HOSTRUNNER')
25+
CC_REGEX = create_makefile_regex('CC')
26+
2027
# Parse Clang version (truncated examples using '...'):
2128
# 'Clang 21.0.0 (clang-2100.1.1.101)'
2229
# 'clang version 18.0.4'
@@ -48,10 +55,27 @@ def get_py_version():
4855
return match.group(1)
4956

5057

51-
def get_makefile_vars():
58+
def parse_file(variable_name, filename, regex):
59+
with open(filename, encoding='utf8') as fp:
60+
code = fp.read()
61+
match = regex.search(code)
62+
if not match:
63+
exit_error(f"ERROR: Unable to locate {variable_name} in {filename}")
64+
return match.group(1)
65+
66+
67+
def get_hostrunner():
68+
# Look in the current working directory
69+
makefile = 'Makefile'
70+
HOSTRUNNER = parse_file('HOSTRUNNER', makefile, HOSTRUNNER_REGEX)
71+
return HOSTRUNNER.rstrip()
72+
73+
74+
def get_makefile_cc():
5275
# Look in the current working directory
5376
makefile = 'Makefile'
54-
return sysconfig.__main__._parse_makefile(makefile)
77+
CC = parse_file('CC', makefile, CC_REGEX)
78+
return CC.rstrip()
5579

5680

5781
def get_gil_disabled():
@@ -131,11 +155,9 @@ def run_command(cmd, *, check=True):
131155

132156

133157
def _get_compiler():
134-
makefile_vars = get_makefile_vars()
135-
136158
# Run _getcompiler program
137159
getcompiler = os.path.join('Programs', '_getcompiler')
138-
HOSTRUNNER = makefile_vars.get('HOSTRUNNER')
160+
HOSTRUNNER = get_hostrunner()
139161
if HOSTRUNNER:
140162
# Cross-compilation
141163
runner = shlex.split(HOSTRUNNER)[0]
@@ -146,7 +168,7 @@ def _get_compiler():
146168
return compiler
147169

148170
# Running _getcompiler failed, run the compiler with --version
149-
CC = makefile_vars.get('CC')
171+
CC = get_makefile_cc()
150172
if not CC:
151173
exit_error(f"ERROR: Unable to locate CC in Makefile")
152174

0 commit comments

Comments
 (0)