diff --git a/Misc/NEWS.d/next/Build/2025-10-22-14-09-41.gh-issue-140421.2SCWXM.rst b/Misc/NEWS.d/next/Build/2025-10-22-14-09-41.gh-issue-140421.2SCWXM.rst new file mode 100644 index 00000000000000..f3b207492a8093 --- /dev/null +++ b/Misc/NEWS.d/next/Build/2025-10-22-14-09-41.gh-issue-140421.2SCWXM.rst @@ -0,0 +1 @@ +Fix compilation errors in the perf trampoline on older macOS versions. diff --git a/Python/perf_jit_trampoline.c b/Python/perf_jit_trampoline.c index 8732be973616d4..e1a717202bbf7a 100644 --- a/Python/perf_jit_trampoline.c +++ b/Python/perf_jit_trampoline.c @@ -80,6 +80,17 @@ # include // System call interface #endif +#if defined(__APPLE__) +#include +#if defined(MAC_OS_X_VERSION_MIN_REQUIRED) \ + && (!defined(MAC_OS_X_VERSION_10_12) || \ + MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_12) +#define PY_PERF_JIT_USE_MACH_TIME +#include +#include // mach_absolute_time +#endif +#endif + // ============================================================================= // CONSTANTS AND CONFIGURATION // ============================================================================= @@ -284,6 +295,12 @@ static const intptr_t nanoseconds_per_second = 1000000000; * Returns: Current monotonic time in nanoseconds since an arbitrary epoch */ static int64_t get_current_monotonic_ticks(void) { +#if defined(__APPLE__) && defined(PY_PERF_JIT_USE_MACH_TIME) + /* clock_gettime() is not available before macOS 10.12. + * mach_absolute_time() should always return a value in + * nanoseconds on x86. */ + return (int64_t)mach_absolute_time(); +#else struct timespec ts; if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) { Py_UNREACHABLE(); // Should never fail on supported systems @@ -295,6 +312,7 @@ static int64_t get_current_monotonic_ticks(void) { result *= nanoseconds_per_second; result += ts.tv_nsec; return result; +#endif } /* diff --git a/Python/perf_trampoline.c b/Python/perf_trampoline.c index 987e8d2a11a659..589049c735498a 100644 --- a/Python/perf_trampoline.c +++ b/Python/perf_trampoline.c @@ -145,6 +145,9 @@ any DWARF information available for them). #include // sysconf() #include // gettimeofday() +#if !defined(MAP_ANONYMOUS) && defined(MAP_ANON) +# define MAP_ANONYMOUS MAP_ANON +#endif #if defined(__arm__) || defined(__arm64__) || defined(__aarch64__) #define PY_HAVE_INVALIDATE_ICACHE diff --git a/configure b/configure index 95e231f406a9a3..659aa9a26c3130 100755 --- a/configure +++ b/configure @@ -13836,7 +13836,15 @@ case $PLATFORM_TRIPLET in #( aarch64-linux-gnu) : perf_trampoline=yes ;; #( darwin) : - perf_trampoline=yes ;; #( + case $host_cpu in #( + p*pc*) : + perf_trampoline=no ;; #( + *) : + perf_trampoline=yes + ;; #( + *) : + ;; +esac ;; #( *) : perf_trampoline=no ;; diff --git a/configure.ac b/configure.ac index 65c37579633754..18efb6a70ef60b 100644 --- a/configure.ac +++ b/configure.ac @@ -3704,7 +3704,10 @@ AC_MSG_CHECKING([perf trampoline]) AS_CASE([$PLATFORM_TRIPLET], [x86_64-linux-gnu], [perf_trampoline=yes], [aarch64-linux-gnu], [perf_trampoline=yes], - [darwin], [perf_trampoline=yes], + [darwin], AS_CASE([$host_cpu], + [p*pc*], [perf_trampoline=no], + [*], [perf_trampoline=yes] + ), [perf_trampoline=no] ) AC_MSG_RESULT([$perf_trampoline])