1616# Get PY_VERSION from Include/patchlevel.h
1717PY_VERSION_REGEX = re .compile (r'^#define PY_VERSION +"(.*)"$' , re .MULTILINE )
1818
19- # Get HOSTRUNNER from Makefile
20- HOSTRUNNER_REGEX = re .compile (r'^HOSTRUNNER *= *(.*)$' , re .MULTILINE )
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' )
2125
2226# Py_GIL_DISABLED from pyconfig.h
2327Py_GIL_DISABLED_DEF_REGEX = re .compile (r'^#define Py_GIL_DISABLED 1$' , re .MULTILINE )
2428Py_GIL_DISABLED_UNDEF_REGEX = re .compile (r'^/\* #undef Py_GIL_DISABLED \*/$' , re .MULTILINE )
2529
2630
31+ def exit_error (msg ):
32+ print (msg )
33+ print ("cwd: {os.getcwd()}" )
34+ sys .exit (1 )
35+
36+
2737def parse_file (variable_name , filename , regex ):
2838 with open (filename , encoding = 'utf8' ) as fp :
2939 code = fp .read ()
3040 match = regex .search (code )
3141 if not match :
32- print (f"ERROR: Unable to locate { variable_name } in { filename } " )
33- sys .exit (1 )
42+ exit_error (f"ERROR: Unable to locate { variable_name } in { filename } " )
3443 return match .group (1 )
3544
3645
@@ -46,6 +55,13 @@ def get_host_runner():
4655 return HOSTRUNNER .rstrip ()
4756
4857
58+ def get_makefile_cc ():
59+ # Look in the current working directory
60+ makefile = 'Makefile'
61+ CC = parse_file ('CC' , makefile , CC_REGEX )
62+ return CC .rstrip ()
63+
64+
4965def get_gil_disable ():
5066 # Look in the current working directory
5167 pyconfig_h = 'pyconfig.h'
@@ -57,8 +73,7 @@ def get_gil_disable():
5773 if Py_GIL_DISABLED_UNDEF_REGEX .search (code ):
5874 return False
5975
60- print (f"ERROR: Unable to locate Py_GIL_DISABLED in { pyconfig_h } " )
61- sys .exit (1 )
76+ exit_error (f"ERROR: Unable to locate Py_GIL_DISABLED in { pyconfig_h } " )
6277
6378
6479def get_date_time ():
@@ -102,7 +117,9 @@ def parse_args():
102117 return parser .parse_args ()
103118
104119
105- def run_command (cmd ):
120+ def run_command (cmd , * , check = True ):
121+ cmd_str = shlex .join (cmd )
122+ print (f"+ { cmd_str } " )
106123 try :
107124 proc = subprocess .run (cmd , stdout = subprocess .PIPE , text = True )
108125 except OSError as exc :
@@ -115,11 +132,38 @@ def run_command(cmd):
115132 error = None
116133
117134 if error :
118- print (f"Command { shlex .join (cmd )} failed { error } " )
119- sys .exit (1 )
135+ msg = f"Command { cmd_str } failed { error } "
136+ if check :
137+ exit_error (msg )
138+ else :
139+ print (msg )
140+ return None
141+
120142 return proc .stdout .rstrip ()
121143
122144
145+ def get_compiler ():
146+ # Run _getcompiler program
147+ getcompiler = os .path .join ('Programs' , '_getcompiler' )
148+ HOSTRUNNER = get_host_runner ()
149+ if HOSTRUNNER :
150+ # Cross-compilation
151+ runner = shlex .split (HOSTRUNNER )[0 ]
152+ compiler = run_command ([runner , getcompiler ], check = False )
153+ else :
154+ compiler = run_command ([getcompiler ], check = False )
155+
156+ if compiler :
157+ return compiler
158+
159+ # Running _getcompiler failed, run directly the compiler (--version)
160+ cc = get_makefile_cc ()
161+ cc = shlex .split (cc )
162+ output = run_command ([* cc , '--version' ])
163+ output = output .splitlines ()[0 ]
164+ return f'[{ output } ]'
165+
166+
123167def main ():
124168 # Force the C locale to format date as English
125169 locale .setlocale (locale .LC_ALL , 'C' )
@@ -134,19 +178,13 @@ def main():
134178 gil_enabled = args .gil_enabled
135179
136180 if not output_filename :
137- # In the current directory
181+ # Write to the current directory
138182 output_filename = os .path .join ('Modules' , 'getbuildinfo.h' )
139183 if not platform :
140184 platform = "unknown"
141185
142186 if not compiler :
143- getcompiler = os .path .join ('Programs' , '_getcompiler' )
144- HOSTRUNNER = get_host_runner ()
145- if HOSTRUNNER :
146- runner = shlex .split (HOSTRUNNER )[0 ]
147- compiler = run_command ([runner , getcompiler ])
148- else :
149- compiler = run_command ([getcompiler ])
187+ compiler = get_compiler ()
150188
151189 build_date , build_time = get_date_time ()
152190 build_info , git_id = get_build_info (git_tag , git_branch , git_version , build_date , build_time )
0 commit comments