From e329ca4c26daba053730a0d1008de75bdb42826d Mon Sep 17 00:00:00 2001 From: prevter Date: Fri, 1 Aug 2025 14:42:32 +0300 Subject: [PATCH] Fix universal macOS binaries builds Trying to compile libwebp with CMAKE_OSX_ARCHITECTURES="x86_64;arm64" (also called as universal binary), will always fail due to SIMD intrinsics check. CMake passes `-arch x86_64 -arch arm64` into the compiler during `check_c_source_compiles`, which will always fail for any flag, because either x86_64 or arm64 won't have it. This also results in you not being able to compile the library due to errors like "SSE2 register return with SSE2 disabled". Change-Id: I32a41e4c64ce8c6a043083023a4018d512be124b --- cmake/cpu.cmake | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/cmake/cpu.cmake b/cmake/cpu.cmake index 3b0b2d37..ccb86907 100644 --- a/cmake/cpu.cmake +++ b/cmake/cpu.cmake @@ -18,11 +18,36 @@ function(webp_check_compiler_flag WEBP_SIMD_FLAG ENABLE_SIMD) unset(WEBP_HAVE_FLAG_${WEBP_SIMD_FLAG} CACHE) cmake_push_check_state() set(CMAKE_REQUIRED_INCLUDES ${CMAKE_CURRENT_SOURCE_DIR}) + + # If building a universal binary on macOS, we need to check if one of the + # architectures supports the SIMD flag. + set(OSX_CHECK "") + if(APPLE AND CMAKE_OSX_ARCHITECTURES) + list(LENGTH CMAKE_OSX_ARCHITECTURES ARCH_COUNT) + if(ARCH_COUNT EQUAL 2) + set(OSX_CHECK "defined(WEBP_CAN_HAVE_${WEBP_SIMD_FLAG}) &&") + endif() + endif() + check_c_source_compiles( " #include \"${CMAKE_CURRENT_LIST_DIR}/../src/dsp/dsp.h\" + #if defined(__APPLE__) + #if defined(__x86_64__) + #define WEBP_CAN_HAVE_SSE2 + #define WEBP_CAN_HAVE_SSE41 + #define WEBP_CAN_HAVE_AVX2 + #elif defined(__aarch64__) + #define WEBP_CAN_HAVE_NEON + #endif + // MIPS intrinsics are not supported on macOS, but we have to define them + // so that the check happens. + #define WEBP_CAN_HAVE_MIPS32 + #define WEBP_CAN_HAVE_MIPS_DSP_R2 + #define WEBP_CAN_HAVE_MSA + #endif int main(void) { - #if !defined(WEBP_USE_${WEBP_SIMD_FLAG}) + #if ${OSX_CHECK} !defined(WEBP_USE_${WEBP_SIMD_FLAG}) this is not valid code #endif return 0;