Skip to content

Commit 7cdcf1b

Browse files
committed
Fix build errors on macOS and Windows
1 parent dee6775 commit 7cdcf1b

4 files changed

Lines changed: 127 additions & 85 deletions

File tree

Makefile.pre.in

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ LDLIBRARYDIR= @LDLIBRARYDIR@
290290
INSTSONAME= @INSTSONAME@
291291
LIBRARY_DEPS= @LIBRARY_DEPS@
292292
LINK_PYTHON_DEPS=@LINK_PYTHON_DEPS@
293+
JIT_OBJS= @JIT_SHIM_O@
293294
PY_ENABLE_SHARED= @PY_ENABLE_SHARED@
294295
STATIC_LIBPYTHON= @STATIC_LIBPYTHON@
295296

@@ -3204,8 +3205,10 @@ Python/emscripten_trampoline_inner.wasm: $(srcdir)/Python/emscripten_trampoline_
32043205
Python/emscripten_trampoline_wasm.c: Python/emscripten_trampoline_inner.wasm
32053206
$(PYTHON_FOR_REGEN) $(srcdir)/Platforms/emscripten/prepare_external_wasm.py $< $@ getWasmTrampolineModule
32063207

3207-
JIT_OBJS= @JIT_SHIM_O@
3208-
JIT_TARGETS= jit_stencils.h @JIT_STENCILS_H@ $(JIT_OBJS)
3208+
JIT_SHIM_BUILD_OBJS= @JIT_SHIM_BUILD_O@
3209+
JIT_BUILD_TARGETS= jit_stencils.h @JIT_STENCILS_H@ $(JIT_SHIM_BUILD_OBJS)
3210+
JIT_TARGETS= $(JIT_BUILD_TARGETS) $(filter-out $(JIT_SHIM_BUILD_OBJS),$(JIT_OBJS))
3211+
JIT_GENERATED_STAMP= .jit-stamp
32093212

32103213
JIT_DEPS = \
32113214
$(srcdir)/Tools/jit/*.c \
@@ -3214,15 +3217,25 @@ JIT_DEPS = \
32143217
$(srcdir)/Python/executor_cases.c.h \
32153218
pyconfig.h
32163219

3217-
$(JIT_TARGETS): $(JIT_DEPS)
3220+
$(JIT_GENERATED_STAMP): $(JIT_DEPS)
32183221
@REGEN_JIT_COMMAND@
3222+
@touch $@
3223+
3224+
$(JIT_BUILD_TARGETS): $(JIT_GENERATED_STAMP)
3225+
@if test ! -f "$@"; then \
3226+
rm -f $(JIT_GENERATED_STAMP); \
3227+
$(MAKE) $(JIT_GENERATED_STAMP); \
3228+
test -f "$@"; \
3229+
fi
3230+
3231+
jit_shim-universal2-apple-darwin.o: jit_shim-aarch64-apple-darwin.o jit_shim-x86_64-apple-darwin.o
3232+
lipo -create -output $@ jit_shim-aarch64-apple-darwin.o jit_shim-x86_64-apple-darwin.o
32193233

32203234
Python/jit.o: $(srcdir)/Python/jit.c @JIT_STENCILS_H@
32213235
$(CC) -c $(PY_CORE_CFLAGS) -o $@ $<
32223236

32233237
.PHONY: regen-jit
3224-
regen-jit:
3225-
@REGEN_JIT_COMMAND@
3238+
regen-jit: $(JIT_TARGETS)
32263239

32273240
# Some make's put the object file in the current directory
32283241
.c.o:
@@ -3346,7 +3359,7 @@ clean-profile: clean-retain-profile clean-bolt
33463359
# gh-141808: The JIT stencils are deliberately kept in clean-profile
33473360
.PHONY: clean-jit-stencils
33483361
clean-jit-stencils:
3349-
-rm -f $(JIT_TARGETS) jit_stencils*.h jit_shim*.o
3362+
-rm -f $(JIT_TARGETS) $(JIT_GENERATED_STAMP) jit_stencils*.h jit_shim*.o
33503363

33513364
.PHONY: clean
33523365
clean: clean-profile clean-jit-stencils

Tools/jit/_targets.py

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ class _Target(typing.Generic[_S, _R]):
5757
known_symbols: dict[str, int] = dataclasses.field(default_factory=dict)
5858
pyconfig_dir: pathlib.Path = pathlib.Path.cwd().resolve()
5959

60+
def _compile_args(self) -> list[str]:
61+
return list(self.args)
62+
63+
def _shim_compile_args(self) -> list[str]:
64+
return []
65+
6066
def _get_nop(self) -> bytes:
6167
if re.fullmatch(r"aarch64-.*", self.triple):
6268
nop = b"\x1f\x20\x03\xd5"
@@ -139,7 +145,7 @@ def _handle_relocation(
139145
) -> _stencils.Hole:
140146
raise NotImplementedError(type(self))
141147

142-
def _common_clang_args(self, opname: str, tempdir: pathlib.Path) -> list[str]:
148+
def _base_clang_args(self, opname: str, tempdir: pathlib.Path) -> list[str]:
143149
return [
144150
f"--target={self.triple}",
145151
"-DPy_BUILD_CORE_MODULE",
@@ -179,20 +185,22 @@ async def _build_stencil_group(
179185
) -> _stencils.StencilGroup:
180186
s = tempdir / f"{opname}.s"
181187
o = tempdir / f"{opname}.o"
182-
args_s = self._common_clang_args(opname, tempdir)
188+
args_s = self._base_clang_args(opname, tempdir)
183189
args_s += [
184190
"-S",
185-
# This debug info isn't necessary, and bloats out the JIT'ed code.
186-
# We *may* be able to re-enable this, process it, and JIT it for a
187-
# nicer debugging experience... but that needs a lot more research:
191+
# Stencils do not need unwind info, and the optimizer does not
192+
# preserve .cfi_* directives correctly. On Darwin,
193+
# -fno-asynchronous-unwind-tables alone still leaves synchronous
194+
# unwind directives in the assembly, so disable both forms here.
195+
"-fno-unwind-tables",
188196
"-fno-asynchronous-unwind-tables",
189197
"-o",
190198
f"{s}",
191199
f"{c}",
192200
]
193201
if self.frame_pointers:
194202
args_s += ["-Xclang", "-mframe-pointer=reserved"]
195-
args_s += self.args
203+
args_s += self._compile_args()
196204
# Allow user-provided CFLAGS to override any defaults
197205
args_s += shlex.split(self.cflags)
198206
await _llvm.run(
@@ -222,13 +230,8 @@ async def _build_stencil_group(
222230
async def _build_shim_object(self, output: pathlib.Path) -> None:
223231
with tempfile.TemporaryDirectory() as tempdir:
224232
work = pathlib.Path(tempdir).resolve()
225-
args_o = self._common_clang_args("shim", work)
226-
if self.triple.endswith("-windows-msvc"):
227-
# The linked shim is part of pythoncore, not a shared extension.
228-
# On Windows, Py_BUILD_CORE_MODULE makes public APIs import from
229-
# pythonXY.lib, which creates a self-dependency when linking
230-
# pythoncore.dll. Build the shim with builtin/core semantics.
231-
args_o += ["-UPy_BUILD_CORE_MODULE", "-DPy_BUILD_CORE_BUILTIN"]
233+
args_o = self._base_clang_args("shim", work)
234+
args_o += self._shim_compile_args()
232235
args_o += [
233236
"-c",
234237
# The linked shim is a real function in the final binary, so
@@ -237,7 +240,7 @@ async def _build_shim_object(self, output: pathlib.Path) -> None:
237240
]
238241
if self.frame_pointers:
239242
args_o += ["-Xclang", "-mframe-pointer=all"]
240-
args_o += self.args
243+
args_o += self._compile_args()
241244
args_o += shlex.split(self.cflags)
242245
args_o += ["-o", f"{output}", f"{TOOLS_JIT / 'shim.c'}"]
243246
await _llvm.run(
@@ -328,6 +331,13 @@ def build(
328331
class _COFF(
329332
_Target[_schema.COFFSection, _schema.COFFRelocation]
330333
): # pylint: disable = too-few-public-methods
334+
def _shim_compile_args(self) -> list[str]:
335+
# The linked shim is part of pythoncore, not a shared extension.
336+
# On Windows, Py_BUILD_CORE_MODULE makes public APIs import from
337+
# pythonXY.lib, which creates a self-dependency when linking
338+
# pythoncore.dll. Build the shim with builtin/core semantics.
339+
return ["-UPy_BUILD_CORE_MODULE", "-DPy_BUILD_CORE_BUILTIN"]
340+
331341
def _handle_section(
332342
self, section: _schema.COFFSection, group: _stencils.StencilGroup
333343
) -> None:
@@ -428,6 +438,10 @@ class _COFF64(_COFF):
428438
symbol_prefix = ""
429439
re_global = re.compile(r'\s*\.def\s+(?P<label>[\w."$?@]+);')
430440

441+
def _compile_args(self) -> list[str]:
442+
runtime = "-fms-runtime-lib=dll_dbg" if self.debug else "-fms-runtime-lib=dll"
443+
return [runtime, *self.args]
444+
431445

432446
class _ELF(
433447
_Target[_schema.ELFSection, _schema.ELFRelocation]
@@ -639,9 +653,8 @@ def get_target(host: str) -> _COFF32 | _COFF64 | _ELF | _MachO:
639653
elif re.fullmatch(r"aarch64-pc-windows-msvc", host):
640654
host = "aarch64-pc-windows-msvc"
641655
condition = "defined(_M_ARM64)"
642-
args = ["-fms-runtime-lib=dll"]
643656
optimizer = _optimizers.OptimizerAArch64
644-
target = _COFF64(host, condition, args=args, optimizer=optimizer)
657+
target = _COFF64(host, condition, optimizer=optimizer)
645658
elif re.fullmatch(r"aarch64-.*-linux-gnu", host):
646659
host = "aarch64-unknown-linux-gnu"
647660
condition = "defined(__aarch64__) && defined(__linux__)"
@@ -668,9 +681,8 @@ def get_target(host: str) -> _COFF32 | _COFF64 | _ELF | _MachO:
668681
elif re.fullmatch(r"x86_64-pc-windows-msvc", host):
669682
host = "x86_64-pc-windows-msvc"
670683
condition = "defined(_M_X64)"
671-
args = ["-fms-runtime-lib=dll"]
672684
optimizer = _optimizers.OptimizerX86
673-
target = _COFF64(host, condition, args=args, optimizer=optimizer)
685+
target = _COFF64(host, condition, optimizer=optimizer)
674686
elif re.fullmatch(r"x86_64-.*-linux-gnu", host):
675687
host = "x86_64-unknown-linux-gnu"
676688
condition = "defined(__x86_64__) && defined(__linux__)"

configure

Lines changed: 39 additions & 31 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

configure.ac

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8385,42 +8385,51 @@ PY_STDLIB_MOD([xxlimited_35], [test "$TEST_MODULES" = yes], [test "$ac_cv_func_d
83858385
# Determine JIT stencils header files based on target platform
83868386
JIT_STENCILS_H=""
83878387
JIT_SHIM_O=""
8388+
JIT_SHIM_BUILD_O=""
83888389
AS_VAR_IF([jit_flags],
83898390
[],
83908391
[],
8391-
[case "$host" in
8392-
aarch64-apple-darwin*)
8393-
JIT_STENCILS_H="jit_stencils-aarch64-apple-darwin.h"
8394-
JIT_SHIM_O="jit_shim-aarch64-apple-darwin.o"
8395-
;;
8396-
x86_64-apple-darwin*)
8397-
JIT_STENCILS_H="jit_stencils-x86_64-apple-darwin.h"
8398-
JIT_SHIM_O="jit_shim-x86_64-apple-darwin.o"
8399-
;;
8400-
aarch64-pc-windows-msvc)
8401-
JIT_STENCILS_H="jit_stencils-aarch64-pc-windows-msvc.h"
8402-
JIT_SHIM_O="jit_shim-aarch64-pc-windows-msvc.o"
8403-
;;
8404-
i686-pc-windows-msvc)
8405-
JIT_STENCILS_H="jit_stencils-i686-pc-windows-msvc.h"
8406-
JIT_SHIM_O="jit_shim-i686-pc-windows-msvc.o"
8407-
;;
8408-
x86_64-pc-windows-msvc)
8409-
JIT_STENCILS_H="jit_stencils-x86_64-pc-windows-msvc.h"
8410-
JIT_SHIM_O="jit_shim-x86_64-pc-windows-msvc.o"
8411-
;;
8412-
aarch64-*-linux-gnu)
8413-
JIT_STENCILS_H="jit_stencils-aarch64-unknown-linux-gnu.h"
8414-
JIT_SHIM_O="jit_shim-aarch64-unknown-linux-gnu.o"
8415-
;;
8416-
x86_64-*-linux-gnu)
8417-
JIT_STENCILS_H="jit_stencils-x86_64-unknown-linux-gnu.h"
8418-
JIT_SHIM_O="jit_shim-x86_64-unknown-linux-gnu.o"
8419-
;;
8420-
esac])
8392+
[if test "${enable_universalsdk}" && test "$UNIVERSAL_ARCHS" = "universal2"; then
8393+
JIT_STENCILS_H="jit_stencils-aarch64-apple-darwin.h jit_stencils-x86_64-apple-darwin.h"
8394+
JIT_SHIM_O="jit_shim-universal2-apple-darwin.o"
8395+
JIT_SHIM_BUILD_O="jit_shim-aarch64-apple-darwin.o jit_shim-x86_64-apple-darwin.o"
8396+
else
8397+
case "$host" in
8398+
aarch64-apple-darwin*)
8399+
JIT_STENCILS_H="jit_stencils-aarch64-apple-darwin.h"
8400+
JIT_SHIM_O="jit_shim-aarch64-apple-darwin.o"
8401+
;;
8402+
x86_64-apple-darwin*)
8403+
JIT_STENCILS_H="jit_stencils-x86_64-apple-darwin.h"
8404+
JIT_SHIM_O="jit_shim-x86_64-apple-darwin.o"
8405+
;;
8406+
aarch64-pc-windows-msvc)
8407+
JIT_STENCILS_H="jit_stencils-aarch64-pc-windows-msvc.h"
8408+
JIT_SHIM_O="jit_shim-aarch64-pc-windows-msvc.o"
8409+
;;
8410+
i686-pc-windows-msvc)
8411+
JIT_STENCILS_H="jit_stencils-i686-pc-windows-msvc.h"
8412+
JIT_SHIM_O="jit_shim-i686-pc-windows-msvc.o"
8413+
;;
8414+
x86_64-pc-windows-msvc)
8415+
JIT_STENCILS_H="jit_stencils-x86_64-pc-windows-msvc.h"
8416+
JIT_SHIM_O="jit_shim-x86_64-pc-windows-msvc.o"
8417+
;;
8418+
aarch64-*-linux-gnu)
8419+
JIT_STENCILS_H="jit_stencils-aarch64-unknown-linux-gnu.h"
8420+
JIT_SHIM_O="jit_shim-aarch64-unknown-linux-gnu.o"
8421+
;;
8422+
x86_64-*-linux-gnu)
8423+
JIT_STENCILS_H="jit_stencils-x86_64-unknown-linux-gnu.h"
8424+
JIT_SHIM_O="jit_shim-x86_64-unknown-linux-gnu.o"
8425+
;;
8426+
esac
8427+
JIT_SHIM_BUILD_O="$JIT_SHIM_O"
8428+
fi])
84218429

84228430
AC_SUBST([JIT_STENCILS_H])
84238431
AC_SUBST([JIT_SHIM_O])
8432+
AC_SUBST([JIT_SHIM_BUILD_O])
84248433

84258434
# substitute multiline block, must come after last PY_STDLIB_MOD()
84268435
AC_SUBST([MODULE_BLOCK])

0 commit comments

Comments
 (0)