Fix non-SIMD (MSan) builds

Previously, WITH_SIMD was always defined and we were simply switching
the implementation to jsimd_none.c for MSan.

The placeholder jsimd_none.c was removed in [1], so now we have to
control SIMD via defines.

This CL removes the hard-coded WITH_SIMD defines and consolidates all
SIMD configuration in the "simd" library.

[1] https://github.com/libjpeg-turbo/libjpeg-turbo/commit/e8b40f3c2ba187ba95c13c3e8ce21c8534256df7

Bug: 398911117
Change-Id: Ia8108469c312a1b9b5c9db1868556214e6db7599
diff --git a/BUILD.gn b/BUILD.gn
index 8001eb0..036b358 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -168,12 +168,30 @@
   }
 }
 
+config("libjpeg_simd_config") {
+  if (is_msan && (current_cpu == "x86" || current_cpu == "x64")) {
+    # MemorySanitizer doesn't support assembly code, so keep it disabled in x86
+    # and x64 MSan builds for now.
+  } else if (current_cpu == "x86" || current_cpu == "x64") {
+    defines = [ "WITH_SIMD" ]
+  } else if ((current_cpu == "arm" || current_cpu == "arm64") && arm_use_neon) {
+    defines = [
+      "NEON_INTRINSICS",
+      "WITH_SIMD",
+    ]
+  }
+}
+
 static_library("simd") {
   include_dirs = [ "." ]
   deps = [ ":libjpeg_headers" ]
   defines = [ "MANGLE_JPEG_NAMES" ]
+  configs += [ ":libjpeg_simd_config" ]
 
-  if (current_cpu == "x86") {
+  if (is_msan && (current_cpu == "x86" || current_cpu == "x64")) {
+    # MemorySanitizer doesn't support assembly code, so keep it disabled in x86
+    # and x64 MSan builds for now.
+  } else if (current_cpu == "x86") {
     deps += [ ":simd_asm" ]
     sources = [ "simd/i386/jsimd.c" ]
   } else if (current_cpu == "x64") {
@@ -209,12 +227,8 @@
       ]
     }
 
-    defines += [ "NEON_INTRINSICS" ]
-
     configs -= [ "//build/config/compiler:default_optimization" ]
     configs += [ "//build/config/compiler:optimize_speed" ]
-  } else {
-    sources = [ "jsimd_none.c" ]
   }
 
   if (is_win) {
@@ -304,7 +318,6 @@
   sources = libjpeg16_sources
 
   defines = [
-    "WITH_SIMD",
     "NO_GETENV",
     "NO_PUTENV",
     "BITS_IN_JSAMPLE=16",
@@ -320,7 +333,6 @@
   sources = libjpeg12_sources
 
   defines = [
-    "WITH_SIMD",
     "NO_GETENV",
     "NO_PUTENV",
     "BITS_IN_JSAMPLE=12",
@@ -336,18 +348,24 @@
   sources = libjpeg_sources
 
   defines = [
-    "WITH_SIMD",
     "NO_GETENV",
     "NO_PUTENV",
   ]
 
-  configs += [ ":libjpeg_config" ]
+  configs += [
+    ":libjpeg_config",
+    ":libjpeg_simd_config",
+  ]
 
   public_configs = [ ":libjpeg_config" ]
   public_deps = [
     ":libjpeg_headers",
+  ]
+
+  deps = [
     ":libjpeg12",
     ":libjpeg16",
+    ":simd",
   ]
 
   if (current_cpu == "x86" || current_cpu == "x64") {
@@ -355,18 +373,6 @@
     # Note: USE_CLZ_INTRINSIC is enabled by default for Arm (jpeg_nbits.h)
     defines += [ "USE_CLZ_INTRINSIC" ]
   }
-
-  # MemorySanitizer doesn't support assembly code, so keep it disabled in x86
-  # and x64 MSan builds for now.
-  if (is_msan && (current_cpu == "x86" || current_cpu == "x64")) {
-    sources += [ "jsimd_none.c" ]
-  } else {
-    public_deps += [ ":simd" ]
-
-    if ((current_cpu == "arm" || current_cpu == "arm64") && arm_use_neon) {
-      defines += [ "NEON_INTRINSICS" ]
-    }
-  }
 }
 
 # PPM also require multiple precision-dependent versions.
@@ -379,7 +385,6 @@
 static_library("turbojpeg_ppm16") {
   sources = turbojpeg_ppm_sources
   defines = [
-    "WITH_SIMD",
     "BMP_SUPPORTED",
     "PPM_SUPPORTED",
     "BITS_IN_JSAMPLE=16",
@@ -394,7 +399,6 @@
 static_library("turbojpeg_ppm12") {
   sources = turbojpeg_ppm_sources
   defines = [
-    "WITH_SIMD",
     "BMP_SUPPORTED",
     "PPM_SUPPORTED",
     "BITS_IN_JSAMPLE=12",
@@ -417,16 +421,20 @@
   ]
 
   defines = [
-    "WITH_SIMD",
     "BMP_SUPPORTED",
     "PPM_SUPPORTED",
   ]
 
-  configs += [ ":libjpeg_config" ]
+  configs += [
+    ":libjpeg_config",
+    ":libjpeg_simd_config",
+  ]
 
   public_configs = [ ":libjpeg_config" ]
   public_deps = [
     ":libjpeg",
+  ]
+  deps = [
     ":turbojpeg_ppm12",
     ":turbojpeg_ppm16",
   ]
@@ -446,7 +454,6 @@
 
     defines = [
       "GTEST",
-      "WITH_SIMD",
       "BMP_SUPPORTED",
       "PPM_SUPPORTED",
       "BITS_IN_JSAMPLE=12",
@@ -489,7 +496,6 @@
 
     defines = [
       "GTEST",
-      "WITH_SIMD",
       "BMP_SUPPORTED",
       "PPM_SUPPORTED",
     ]
@@ -500,7 +506,10 @@
     ]
 
     configs -= [ "//build/config/compiler:chromium_code" ]
-    configs += [ "//build/config/compiler:no_chromium_code" ]
+    configs += [
+      ":libjpeg_simd_config",
+      "//build/config/compiler:no_chromium_code",
+    ]
 
     if (is_win) {
       cflags = [ "-U_CRT_SECURE_NO_DEPRECATE" ]
diff --git a/src/jconfig.h b/src/jconfig.h
index 39f20fc..22c32ee 100644
--- a/src/jconfig.h
+++ b/src/jconfig.h
@@ -19,7 +19,8 @@
 #define MEM_SRCDST_SUPPORTED  1
 
 /* Use accelerated SIMD routines when using 8-bit samples */
-#define WITH_SIMD 1
+/* Chromium note: this is defined in BUILD.gn */
+/* #define WITH_SIMD 1 */
 
 /* This version of libjpeg-turbo supports run-time selection of data precision,
  * so BITS_IN_JSAMPLE is no longer used to specify the data precision at build
diff --git a/src/jconfigint.h b/src/jconfigint.h
index 58273c3..815fc21 100644
--- a/src/jconfigint.h
+++ b/src/jconfigint.h
@@ -85,19 +85,3 @@
 #define BITS_IN_JSAMPLE  8      /* use 8 or 12 */
 #endif
 
-#undef C_ARITH_CODING_SUPPORTED
-#undef D_ARITH_CODING_SUPPORTED
-#undef WITH_SIMD
-
-#if BITS_IN_JSAMPLE == 8
-
-/* Support arithmetic encoding */
-/* #define C_ARITH_CODING_SUPPORTED 1 */
-
-/* Support arithmetic decoding */
-/* #define D_ARITH_CODING_SUPPORTED 1 */
-
-/* Use accelerated SIMD routines. */
-#define WITH_SIMD 1
-
-#endif