New EMSCRIPTEN_SYSTEM_PROCESSOR property in CMake toolchain (#15297)

This allows user to specify the value of CMAKE_SYSTEM_PROCESSOR set by
the toolchain file.

Fixes #15284.
diff --git a/ChangeLog.md b/ChangeLog.md
index bbcf40e..a46840b 100644
--- a/ChangeLog.md
+++ b/ChangeLog.md
@@ -44,6 +44,9 @@
   EM_WORKAROUND_PYTHON_BUG_34780 and EM_WORKAROUND_WIN7_BAD_ERRORLEVEL_BUG that
   can be enabled to work around a Windows Python issue
   https://bugs.python.org/issue34780 , and a Windows 7 exit code issue (#15146)
+- Support a new CMake propert `EMSCRIPTEN_SYSTEM_PROCESSOR` which can be used
+  to override the default value of `CMAKE_SYSTEM_PROCESSOR` set by the
+  toolchain file.
 
 2.0.31 - 10/01/2021
 -------------------
diff --git a/cmake/Modules/Platform/Emscripten.cmake b/cmake/Modules/Platform/Emscripten.cmake
index 922d26e..cf22990 100644
--- a/cmake/Modules/Platform/Emscripten.cmake
+++ b/cmake/Modules/Platform/Emscripten.cmake
@@ -23,7 +23,11 @@
 # Advertise Emscripten as a 32-bit platform (as opposed to
 # CMAKE_SYSTEM_PROCESSOR=x86_64 for 64-bit platform), since some projects (e.g.
 # OpenCV) use this to detect bitness.
-set(CMAKE_SYSTEM_PROCESSOR x86)
+# Allow users to ovewrite this on the command line with -DEMSCRIPTEN_SYSTEM_PROCESSOR=arm.
+if ("${EMSCRIPTEN_SYSTEM_PROCESSOR}" STREQUAL "")
+  set(EMSCRIPTEN_SYSTEM_PROCESSOR x86)
+endif()
+set(CMAKE_SYSTEM_PROCESSOR ${EMSCRIPTEN_SYSTEM_PROCESSOR})
 
 # Tell CMake how it should instruct the compiler to generate multiple versions
 # of an outputted .so library: e.g. "libfoo.so, libfoo.so.1, libfoo.so.1.4" etc.
diff --git a/tests/cmake/emscripten_system_processor/CMakeLists.txt b/tests/cmake/emscripten_system_processor/CMakeLists.txt
new file mode 100644
index 0000000..f5354e4
--- /dev/null
+++ b/tests/cmake/emscripten_system_processor/CMakeLists.txt
@@ -0,0 +1,6 @@
+project(emscripten_system_processor)
+
+# Test that EMSCRIPTEN_SYSTEM_PROCESSOR can be overridden, and that
+# CMAKE_SYSTEM_PROCESSOR is set to its value.
+
+message(STATUS "CMAKE_SYSTEM_PROCESSOR is ${CMAKE_SYSTEM_PROCESSOR}")
diff --git a/tests/test_other.py b/tests/test_other.py
index a18841f..4847125 100644
--- a/tests/test_other.py
+++ b/tests/test_other.py
@@ -699,6 +699,17 @@
   def test_cmake_emscripten_version(self):
     self.run_process([EMCMAKE, 'cmake', test_file('cmake/emscripten_version')])
 
+  def test_cmake_emscripten_system_processor(self):
+    cmake_dir = test_file('cmake/emscripten_system_processor')
+    # The default CMAKE_SYSTEM_PROCESSOR is x86.
+    out = self.run_process([EMCMAKE, 'cmake', cmake_dir], stdout=PIPE).stdout
+    self.assertContained('CMAKE_SYSTEM_PROCESSOR is x86', out)
+
+    # It can be overridden by setting EMSCRIPTEN_SYSTEM_PROCESSOR.
+    out = self.run_process(
+      [EMCMAKE, 'cmake', cmake_dir, '-DEMSCRIPTEN_SYSTEM_PROCESSOR=arm'], stdout=PIPE).stdout
+    self.assertContained('CMAKE_SYSTEM_PROCESSOR is arm', out)
+
   def test_cmake_find_stuff(self):
     # Ensure that zlib exists in the sysroot
     self.run_process([EMCC, test_file('hello_world.c'), '-sUSE_ZLIB'])