99import re
1010import shutil
1111import sys
12+ import sysconfig
1213import tempfile
1314import typing
1415
2728CPYTHON = TOOLS .parent
2829PYTHON_EXECUTOR_CASES_C_H = CPYTHON / "Python" / "executor_cases.c.h"
2930TOOLS_JIT_TEMPLATE_C = TOOLS_JIT / "template.c"
31+ SUPPORTED_TRIPLES = {
32+ "aarch64-apple-darwin" ,
33+ "aarch64-unknown-linux-gnu" ,
34+ "i686-pc-windows-msvc" ,
35+ "x86_64-apple-darwin" ,
36+ "x86_64-pc-windows-msvc" ,
37+ "x86_64-pc-linux-gnu" ,
38+ }
3039
3140_S = typing .TypeVar ("_S" , _schema .COFFSection , _schema .ELFSection , _schema .MachOSection )
3241_R = typing .TypeVar (
@@ -45,6 +54,7 @@ class _Target(typing.Generic[_S, _R]):
4554 debug : bool = False
4655 verbose : bool = False
4756 known_symbols : dict [str , int ] = dataclasses .field (default_factory = dict )
57+ stencil_name : str = ""
4858
4959 def _compute_digest (self , out : pathlib .Path ) -> str :
5060 hasher = hashlib .sha256 ()
@@ -190,27 +200,49 @@ def build(self, out: pathlib.Path, *, force: bool = False) -> None:
190200 digest = f"{ self ._compute_digest (out )} \n "
191201 jit_stencils = out / "jit_stencils.h"
192202 jit_stencils_digest = out / "jit_stencils.h.digest"
203+ hosted_stencil = TOOLS_JIT_STENCILS / f"{ self .stencil_name } .h"
193204
194- if not force and jit_stencils_digest .exists () and jit_stencils .exists ():
205+ if (
206+ not force
207+ and jit_stencils_digest .exists ()
208+ and jit_stencils .exists ()
209+ and hosted_stencil .exists ()
210+ ):
195211 if jit_stencils_digest .read_text () == digest :
196212 return
197213
198- # We need to ignore the version number for Darwin targets
199- target = self .triple
200- darwin_index = target .find ("-darwin" )
201- if darwin_index != - 1 :
202- target = self .triple [: darwin_index + len ("-darwin" )]
203-
204- jit_stencils = TOOLS_JIT_STENCILS / f"{ target } .h"
205214 stencil_groups = asyncio .run (self ._build_stencils ())
206- jit_stencils_new = TOOLS_JIT_STENCILS / f" { target } .h.new"
215+ jit_stencils_new = out / "jit_stencils .h.new"
207216 try :
208217 with jit_stencils_new .open ("w" , newline = "\n " ) as file :
209218 for line in _writer .dump (stencil_groups , self .known_symbols ):
210219 file .write (f"{ line } \n " )
211220 try :
212221 jit_stencils_new .replace (jit_stencils )
213- shutil .copy (jit_stencils , out / "jit_stencils.h" )
222+
223+ if "windows" in self .triple :
224+ JIT_ARGS = {
225+ "--experimental-jit"
226+ } # TODO: Need to figure out the right flags here for Windows
227+ copy_stencils = True
228+
229+ else :
230+ JIT_ARGS = {
231+ "--enable-experimental-jit" ,
232+ "--with-lto" ,
233+ "--enable-optimizations" ,
234+ }
235+ makefile = out / "Makefile"
236+ config_args = re .search (
237+ r"CONFIG_ARGS\s*=\s*'(.*)'" , makefile .read_text ()
238+ ).group (1 )
239+ copy_stencils = config_args and all (
240+ arg in JIT_ARGS for arg in config_args .split ()
241+ )
242+
243+ copy_stencils = copy_stencils and self .triple in SUPPORTED_TRIPLES
244+ if copy_stencils :
245+ shutil .copy (jit_stencils , hosted_stencil )
214246 except FileNotFoundError :
215247 # another process probably already moved the file
216248 if not jit_stencils .is_file ():
@@ -495,9 +527,12 @@ def get_target(host: str) -> _COFF | _ELF | _MachO:
495527 """Build a _Target for the given host "triple" and options."""
496528 target : _COFF | _ELF | _MachO
497529 if re .fullmatch (r"aarch64-apple-darwin.*" , host ):
498- target = _MachO (host , alignment = 8 , prefix = "_" )
530+ target = _MachO (
531+ host , alignment = 8 , prefix = "_" , stencil_name = "aarch64-apple-darwin"
532+ )
499533 elif re .fullmatch (r"aarch64-pc-windows-msvc" , host ):
500534 args = ["-fms-runtime-lib=dll" ]
535+ # stencil_name is omitted since aarch64-pc-windows-msvc is Tier 3
501536 target = _COFF (host , alignment = 8 , args = args )
502537 elif re .fullmatch (r"aarch64-.*-linux-gnu" , host ):
503538 args = [
@@ -506,22 +541,24 @@ def get_target(host: str) -> _COFF | _ELF | _MachO:
506541 # was required to disable them.
507542 "-mno-outline-atomics" ,
508543 ]
509- target = _ELF (host , alignment = 8 , args = args )
544+ target = _ELF (
545+ host , alignment = 8 , args = args , stencil_name = "aarch64-unknown-linux-gnu"
546+ )
510547 elif re .fullmatch (r"i686-pc-windows-msvc" , host ):
511548 args = [
512549 "-DPy_NO_ENABLE_SHARED" ,
513550 # __attribute__((preserve_none)) is not supported
514551 "-Wno-ignored-attributes" ,
515552 ]
516- target = _COFF (host , args = args , prefix = "_" )
553+ target = _COFF (host , args = args , prefix = "_" , stencil_name = "i686-pc-windows-msvc" )
517554 elif re .fullmatch (r"x86_64-apple-darwin.*" , host ):
518- target = _MachO (host , prefix = "_" )
555+ target = _MachO (host , prefix = "_" , stencil_name = "x86_64-apple-darwin" )
519556 elif re .fullmatch (r"x86_64-pc-windows-msvc" , host ):
520557 args = ["-fms-runtime-lib=dll" ]
521- target = _COFF (host , args = args )
558+ target = _COFF (host , args = args , stencil_name = "x86_64-pc-windows-msvc" )
522559 elif re .fullmatch (r"x86_64-.*-linux-gnu" , host ):
523560 args = ["-fpic" ]
524- target = _ELF (host , args = args )
561+ target = _ELF (host , args = args , stencil_name = "x86_64-pc-linux-gnu" )
525562 else :
526563 raise ValueError (host )
527564 return target
0 commit comments