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

Petr Hosek via libc-commits libc-commits at lists.llvm.org
Thu Jan 28 12:54:50 PST 2021


Author: Petr Hosek
Date: 2021-01-28T12:54:37-08:00
New Revision: c4819eec1a2aea4758cc1ed38aefb0f1c9dec94a

URL: https://github.com/llvm/llvm-project/commit/c4819eec1a2aea4758cc1ed38aefb0f1c9dec94a
DIFF: https://github.com/llvm/llvm-project/commit/c4819eec1a2aea4758cc1ed38aefb0f1c9dec94a.diff

LOG: [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

Added: 
    

Modified: 
    libc/cmake/modules/LLVMLibCCheckCpuFeatures.cmake
    libc/test/src/string/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/libc/cmake/modules/LLVMLibCCheckCpuFeatures.cmake b/libc/cmake/modules/LLVMLibCCheckCpuFeatures.cmake
index 57dfbd9731d6..1b92b481d540 100644
--- a/libc/cmake/modules/LLVMLibCCheckCpuFeatures.cmake
+++ b/libc/cmake/modules/LLVMLibCCheckCpuFeatures.cmake
@@ -7,15 +7,15 @@ if(${LIBC_TARGET_MACHINE} MATCHES "x86|x86_64")
   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 @@ function(_check_defined_cpu_feature output_var)
   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/libc/test/src/string/CMakeLists.txt b/libc/test/src/string/CMakeLists.txt
index dbdad3ee6aef..511575f9a7f4 100644
--- a/libc/test/src/string/CMakeLists.txt
+++ b/libc/test/src/string/CMakeLists.txt
@@ -183,12 +183,12 @@ add_libc_unittest(
     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


        


More information about the libc-commits mailing list