77import shlex
88import subprocess
99import sys
10+ import sysconfig .__main__
1011import time
1112
1213SRC_DIR = os .path .abspath (os .path .join (os .path .dirname (__file__ ), '..' , '..' ))
1617# Get PY_VERSION from Include/patchlevel.h
1718PY_VERSION_REGEX = re .compile (r'^#define PY_VERSION +"(.*)"$' , re .MULTILINE )
1819
19- def create_makefile_regex (name ):
20- return re .compile (fr'^{ name } [ \t]*=[ \t]*(.*)$' , re .MULTILINE )
21-
22- # Get HOSTRUNNER and CC from Makefile
23- HOSTRUNNER_REGEX = create_makefile_regex ('HOSTRUNNER' )
24- CC_REGEX = create_makefile_regex ('CC' )
25-
26- # Py_GIL_DISABLED from pyconfig.h
27- Py_GIL_DISABLED_DEF_REGEX = re .compile (r'^#define Py_GIL_DISABLED 1$' , re .MULTILINE )
28- Py_GIL_DISABLED_UNDEF_REGEX = re .compile (r'^/\* #undef Py_GIL_DISABLED \*/$' , re .MULTILINE )
29-
3020
3121def exit_error (msg ):
3222 print (msg )
@@ -48,32 +38,18 @@ def get_py_version():
4838 return parse_file ('PY_VERSION ' , patchlevel_h , PY_VERSION_REGEX )
4939
5040
51- def get_host_runner ():
52- # Look in the current working directory
53- makefile = 'Makefile'
54- HOSTRUNNER = parse_file ('HOSTRUNNER' , makefile , HOSTRUNNER_REGEX )
55- return HOSTRUNNER .rstrip ()
56-
57-
58- def get_makefile_cc ():
41+ def get_makefile_vars ():
5942 # Look in the current working directory
6043 makefile = 'Makefile'
61- CC = parse_file ('CC' , makefile , CC_REGEX )
62- return CC .rstrip ()
44+ return sysconfig .__main__ ._parse_makefile (makefile )
6345
6446
6547def get_gil_disable ():
6648 # Look in the current working directory
6749 pyconfig_h = 'pyconfig.h'
68- with open (pyconfig_h , encoding = 'utf8' ) as fp :
69- code = fp .read ()
70-
71- if Py_GIL_DISABLED_DEF_REGEX .search (code ):
72- return True
73- if Py_GIL_DISABLED_UNDEF_REGEX .search (code ):
74- return False
75-
76- exit_error (f"ERROR: Unable to locate Py_GIL_DISABLED in { pyconfig_h } " )
50+ with open (pyconfig_h , encoding = "utf-8" ) as fp :
51+ config_vars = sysconfig .parse_config_h (fp )
52+ return bool (config_vars ['Py_GIL_DISABLED' ])
7753
7854
7955def get_date_time ():
@@ -91,7 +67,9 @@ def get_date_time():
9167 return (build_date , build_time )
9268
9369
94- def get_build_info (git_tag , git_branch , git_version , build_date , build_time ):
70+ def get_build_info (git_tag , git_branch , git_version ):
71+ build_date , build_time = get_date_time ()
72+
9573 if git_tag and git_tag != "undefined" :
9674 git_id = git_tag
9775 else :
@@ -113,7 +91,7 @@ def parse_args():
11391 parser .add_argument ('--git-tag' , type = str , required = True )
11492 parser .add_argument ('--git-branch' , type = str , required = True )
11593 parser .add_argument ('--compiler' , type = str )
116- parser .add_argument ('--gil-enabled ' , type = int )
94+ parser .add_argument ('--free-threading ' , type = int )
11795 return parser .parse_args ()
11896
11997
@@ -143,9 +121,11 @@ def run_command(cmd, *, check=True):
143121
144122
145123def get_compiler ():
124+ makefile_vars = get_makefile_vars ()
125+
146126 # Run _getcompiler program
147127 getcompiler = os .path .join ('Programs' , '_getcompiler' )
148- HOSTRUNNER = get_host_runner ( )
128+ HOSTRUNNER = makefile_vars . get ( 'HOSTRUNNER' )
149129 if HOSTRUNNER :
150130 # Cross-compilation
151131 runner = shlex .split (HOSTRUNNER )[0 ]
@@ -157,7 +137,10 @@ def get_compiler():
157137 return compiler
158138
159139 # Running _getcompiler failed, run directly the compiler (--version)
160- cc = get_makefile_cc ()
140+ CC = makefile_vars .get ('CC' )
141+ if not CC :
142+ exit_error (f"ERROR: Unable to locate CC in Makefile" )
143+
161144 cc = shlex .split (cc )
162145 output = run_command ([* cc , '--version' ])
163146 output = output .splitlines ()[0 ]
@@ -175,7 +158,7 @@ def main():
175158 git_tag = args .git_tag
176159 git_branch = args .git_branch
177160 compiler = args .compiler
178- gil_enabled = args .gil_enabled
161+ free_threading = args .free_threading
179162
180163 if not output_filename :
181164 # Write to the current directory
@@ -186,17 +169,15 @@ def main():
186169 if not compiler :
187170 compiler = get_compiler ()
188171
189- build_date , build_time = get_date_time ()
190- build_info , git_id = get_build_info (git_tag , git_branch , git_version , build_date , build_time )
172+ build_info , git_id = get_build_info (git_tag , git_branch , git_version )
191173
192174 # Get PY_VERSION macro from Include/patchlevel.h
193175 PY_VERSION = get_py_version ()
194- if gil_enabled is None :
176+ if free_threading is None :
195177 # Get Py_GIL_DISABLED macro from pyconfig.h (defined or undefined)
196- Py_GIL_DISABLED = get_gil_disable ()
197- gil_enabled = not Py_GIL_DISABLED
178+ free_threading = get_gil_disable ()
198179
199- if not gil_enabled :
180+ if free_threading :
200181 version = f"{ PY_VERSION } free-threading build ({ build_info } ) { compiler } "
201182 else :
202183 version = f"{ PY_VERSION } ({ build_info } ) { compiler } "
0 commit comments