SDL: cmake: Allow build system to disable arm neon intrinsics

From fc4085b54efe805c4850164d7293a4625fee1dc5 Mon Sep 17 00:00:00 2001
From: Anonymous Maarten <[EMAIL REDACTED]>
Date: Sun, 26 Feb 2023 01:51:19 +0100
Subject: [PATCH] cmake: Allow build system to disable arm neon intrinsics

---
 CMakeLists.txt                                | 18 ++++++++++++++++--
 include/SDL3/SDL_intrin.h                     |  4 ++--
 include/build_config/SDL_build_config.h.cmake |  1 +
 src/SDL_internal.h                            |  3 +--
 src/audio/SDL_audiotypecvt.c                  |  4 ----
 src/video/SDL_stretch.c                       |  5 ++---
 6 files changed, 22 insertions(+), 13 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 739ccce23853..60bd7f645311 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -912,6 +912,7 @@ if(SDL_ASSEMBLY)
     if(SDL_ARMSIMD)
       cmake_push_check_state()
       set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -x assembler-with-cpp")
+      list(APPEND CMAKE_REQUIRED_LINK_OPTIONS -x none)
       check_c_source_compiles("
         .text
         .arch armv6
@@ -921,6 +922,8 @@ if(SDL_ASSEMBLY)
         #ifndef __ARM_EABI__
         #error EABI is required (to be sure that calling conventions are compatible)
         #endif
+        main:
+        .global main
         pld [r0]
         uqadd8 r0, r0, r0
       " ARMSIMD_FOUND)
@@ -929,8 +932,10 @@ if(SDL_ASSEMBLY)
       if(ARMSIMD_FOUND)
         set(HAVE_ARMSIMD TRUE)
         set(SDL_ARM_SIMD_BLITTERS 1)
+        enable_language(ASM)
         file(GLOB ARMSIMD_SOURCES ${SDL3_SOURCE_DIR}/src/video/arm/pixman-arm-simd*.S)
         list(APPEND SOURCE_FILES ${ARMSIMD_SOURCES})
+        set_property(SOURCE ${ARMSIMD_SOURCES} APPEND PROPERTY COMPILE_OPTIONS -x assembler-with-cpp)
         set(WARN_ABOUT_ARM_SIMD_ASM_MIT TRUE)
       endif()
     endif()
@@ -938,6 +943,7 @@ if(SDL_ASSEMBLY)
     if(SDL_ARMNEON)
       cmake_push_check_state()
       set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -x assembler-with-cpp")
+      list(APPEND CMAKE_REQUIRED_LINK_OPTIONS -x none)
       check_c_source_compiles("
         .text
         .fpu neon
@@ -949,6 +955,8 @@ if(SDL_ASSEMBLY)
         #ifndef __ARM_EABI__
         #error EABI is required (to be sure that calling conventions are compatible)
         #endif
+        main:
+        .global main
         pld [r0]
         vmovn.u16 d0, q0
       " ARMNEON_FOUND)
@@ -957,8 +965,10 @@ if(SDL_ASSEMBLY)
       if(ARMNEON_FOUND)
         set(HAVE_ARMNEON TRUE)
         set(SDL_ARM_NEON_BLITTERS 1)
+        enable_language(ASM)
         file(GLOB ARMNEON_SOURCES ${SDL3_SOURCE_DIR}/src/video/arm/pixman-arm-neon*.S)
         list(APPEND SOURCE_FILES ${ARMNEON_SOURCES})
+        set_property(SOURCE ${ARMNEON_SOURCES} APPEND PROPERTY COMPILE_OPTIONS -x assembler-with-cpp)
         set(WARN_ABOUT_ARM_NEON_ASM_MIT TRUE)
       endif()
     endif()
@@ -1028,6 +1038,10 @@ if(NOT HAVE_LASX)
   set(SDL_DISABLE_LASX 1)
 endif()
 
+if(NOT HAVE_ARMNEON)
+  set(SDL_DISABLE_NEON 1)
+endif()
+
 # TODO: Can't deactivate on FreeBSD? w/o LIBC, SDL_stdinc.h can't define
 # anything.
 if(SDL_LIBC)
@@ -3161,21 +3175,21 @@ if(UNIX)
 endif()
 
 if(WARN_ABOUT_ARM_SIMD_ASM_MIT)
-  message(STATUS "")
   message(STATUS "SDL is being built with ARM SIMD optimizations, which")
   message(STATUS "uses code licensed under the MIT license. If this is a")
   message(STATUS "problem, please disable that code by rerunning CMake with:")
   message(STATUS "")
   message(STATUS "    -DSDL_ARMSIMD=OFF")
+  message(STATUS "")
 endif()
 
 if(WARN_ABOUT_ARM_NEON_ASM_MIT)
-  message(STATUS "")
   message(STATUS "SDL is being built with ARM NEON optimizations, which")
   message(STATUS "uses code licensed under the MIT license. If this is a")
   message(STATUS "problem, please disable that code by rerunning CMake with:")
   message(STATUS "")
   message(STATUS "    -DSDL_ARMNEON=OFF")
+  message(STATUS "")
 endif()
 
 if(ANDROID)
diff --git a/include/SDL3/SDL_intrin.h b/include/SDL3/SDL_intrin.h
index 9ca942c3de1d..80be5a562f5f 100644
--- a/include/SDL3/SDL_intrin.h
+++ b/include/SDL3/SDL_intrin.h
@@ -65,7 +65,7 @@ _m_prefetch(void *__P)
 #endif
 #elif defined(__MINGW64_VERSION_MAJOR)
 #include <intrin.h>
-#if !defined(SDL_DISABLE_ARM_NEON_H) && defined(__ARM_NEON)
+#if defined(__ARM_NEON) && !defined(SDL_DISABLE_NEON)
 #  include <arm_neon.h>
 #endif
 #else
@@ -73,7 +73,7 @@ _m_prefetch(void *__P)
 #if defined(HAVE_ALTIVEC_H) && defined(__ALTIVEC__) && !defined(__APPLE_ALTIVEC__) && defined(SDL_ENABLE_ALTIVEC_H)
 #include <altivec.h>
 #endif
-#if !defined(SDL_DISABLE_ARM_NEON_H)
+#if !defined(SDL_DISABLE_NEON)
 #  if defined(__ARM_NEON)
 #    include <arm_neon.h>
 #  elif defined(__WINDOWS__) || defined(__WINRT__) || defined(__GDK__)
diff --git a/include/build_config/SDL_build_config.h.cmake b/include/build_config/SDL_build_config.h.cmake
index a35650f9e2f3..433b99225f1a 100644
--- a/include/build_config/SDL_build_config.h.cmake
+++ b/include/build_config/SDL_build_config.h.cmake
@@ -594,5 +594,6 @@ typedef unsigned int uintptr_t;
 #cmakedefine SDL_DISABLE_MMX 1
 #cmakedefine SDL_DISABLE_LSX 1
 #cmakedefine SDL_DISABLE_LASX 1
+#cmakedefine SDL_DISABLE_NEON 1
 
 #endif /* SDL_build_config_h_ */
diff --git a/src/SDL_internal.h b/src/SDL_internal.h
index 208b92943b78..cea752ae6dde 100644
--- a/src/SDL_internal.h
+++ b/src/SDL_internal.h
@@ -186,8 +186,7 @@
 #include <SDL3/SDL.h>
 #include <SDL3/SDL_intrin.h>
 
-
-#ifdef __ARM_NEON
+#if defined(__ARM_NEON) && !defined(SDL_DISABLE_NEON)
 #define HAVE_NEON_INTRINSICS 1
 #endif
 
diff --git a/src/audio/SDL_audiotypecvt.c b/src/audio/SDL_audiotypecvt.c
index 22595ec41b57..86ab19507f29 100644
--- a/src/audio/SDL_audiotypecvt.c
+++ b/src/audio/SDL_audiotypecvt.c
@@ -23,10 +23,6 @@
 #include "SDL_audio_c.h"
 #include "SDL_audiocvt_c.h"
 
-#ifdef __ARM_NEON
-#define HAVE_NEON_INTRINSICS 1
-#endif
-
 #if defined(__x86_64__) && HAVE_SSE2_INTRINSICS
 #define NEED_SCALAR_CONVERTER_FALLBACKS 0 /* x86_64 guarantees SSE2. */
 #elif __MACOS__ && HAVE_SSE2_INTRINSICS
diff --git a/src/video/SDL_stretch.c b/src/video/SDL_stretch.c
index ce3eae9a15ef..36eb391a0c03 100644
--- a/src/video/SDL_stretch.c
+++ b/src/video/SDL_stretch.c
@@ -332,14 +332,13 @@ static int scale_mat(const Uint32 *src, int src_w, int src_h, int src_pitch,
     return 0;
 }
 
-#if defined(__ARM_NEON)
-#define HAVE_NEON_INTRINSICS 1
+#if HAVE_NEON_INTRINSICS
 #define CAST_uint8x8_t       (uint8x8_t)
 #define CAST_uint32x2_t      (uint32x2_t)
 #endif
 
 #if defined(__WINRT__) || defined(_MSC_VER)
-#if defined(HAVE_NEON_INTRINSICS)
+#if HAVE_NEON_INTRINSICS
 #undef CAST_uint8x8_t
 #undef CAST_uint32x2_t
 #define CAST_uint8x8_t