[CMake][libc] Don't do CPU feature detection when cross-compiling

We won't be able to run the compiled program since it will be compiled
for different system. We instead allow passing the CPU features via
CMake option in that case.

Differential Revision: https://reviews.llvm.org/D95203

GitOrigin-RevId: c4819eec1a2aea4758cc1ed38aefb0f1c9dec94a
diff --git a/cmake/modules/LLVMLibCCheckCpuFeatures.cmake b/cmake/modules/LLVMLibCCheckCpuFeatures.cmake
index 57dfbd9..1b92b48 100644
--- a/cmake/modules/LLVMLibCCheckCpuFeatures.cmake
+++ b/cmake/modules/LLVMLibCCheckCpuFeatures.cmake
@@ -7,15 +7,15 @@
   list(SORT ALL_CPU_FEATURES)
 endif()
 
-# Function to check whether the host supports the provided set of features.
+# Function to check whether the target CPU supports the provided set of features.
 # Usage:
-# host_supports(
+# cpu_supports(
 #   <output variable>
 #   <list of cpu features>
 # )
-function(host_supports output_var features)
-  _intersection(a "${HOST_CPU_FEATURES}" "${features}")
-  if("${a}" STREQUAL "${features}")
+function(cpu_supports output_var features)
+  _intersection(var "${LIBC_CPU_FEATURES}" "${features}")
+  if("${var}" STREQUAL "${features}")
     set(${output_var} TRUE PARENT_SCOPE)
   else()
     unset(${output_var} PARENT_SCOPE)
@@ -126,12 +126,22 @@
   endif()
 endfunction()
 
-# Populates the HOST_CPU_FEATURES list.
-# Use -march=native only when the compiler supports it.
-include(CheckCXXCompilerFlag)
-CHECK_CXX_COMPILER_FLAG("-march=native" COMPILER_SUPPORTS_MARCH_NATIVE)
-if(COMPILER_SUPPORTS_MARCH_NATIVE)
-  _check_defined_cpu_feature(HOST_CPU_FEATURES MARCH native)
+set(LIBC_CPU_FEATURES "" CACHE PATH "supported CPU features")
+
+if(CMAKE_CROSSCOMPILING)
+  _intersection(cpu_features "${ALL_CPU_FEATURES}" "${LIBC_CPU_FEATURES}")
+  if(NOT "${cpu_features}" STREQUAL "${LIBC_CPU_FEATURES}")
+    message(FATAL_ERROR "Unsupported CPU features: ${cpu_features}")
+  endif()
+  set(LIBC_CPU_FEATURES "${cpu_features}")
 else()
-  _check_defined_cpu_feature(HOST_CPU_FEATURES)
+  # Populates the LIBC_CPU_FEATURES list.
+  # Use -march=native only when the compiler supports it.
+  include(CheckCXXCompilerFlag)
+  CHECK_CXX_COMPILER_FLAG("-march=native" COMPILER_SUPPORTS_MARCH_NATIVE)
+  if(COMPILER_SUPPORTS_MARCH_NATIVE)
+    _check_defined_cpu_feature(LIBC_CPU_FEATURES MARCH native)
+  else()
+    _check_defined_cpu_feature(LIBC_CPU_FEATURES)
+  endif()
 endif()
diff --git a/test/src/string/CMakeLists.txt b/test/src/string/CMakeLists.txt
index dbdad3e..511575f 100644
--- a/test/src/string/CMakeLists.txt
+++ b/test/src/string/CMakeLists.txt
@@ -183,12 +183,12 @@
     libc.src.string.strtok_r
 )
 
-# Tests all implementations that can run on the host.
+# Tests all implementations that can run on the target CPU.
 function(add_libc_multi_impl_test name)
   get_property(fq_implementations GLOBAL PROPERTY ${name}_implementations)
   foreach(fq_config_name IN LISTS fq_implementations)
     get_target_property(required_cpu_features ${fq_config_name} REQUIRE_CPU_FEATURES)
-    host_supports(can_run "${required_cpu_features}")
+    cpu_supports(can_run "${required_cpu_features}")
     if(can_run)
       add_libc_unittest(
         ${fq_config_name}_test