@@ -38,13 +38,20 @@ set(ARCH_OPTIONS
3838 ENABLE_ARMV8.5A ENABLE_ARMV8.6A
3939)
4040
41- set (AUTO_DETECT_ARCH ON )
41+ option (AUTO_DETECT_ARCH
42+ "Auto-detect architecture flags when no explicit ENABLE_* arch option is set"
43+ ON )
44+ set (_EXPLICIT_ARCH_SELECTED OFF )
4245foreach (opt IN LISTS ARCH_OPTIONS)
4346 if (${opt} )
44- set (AUTO_DETECT_ARCH OFF )
47+ set (_EXPLICIT_ARCH_SELECTED ON )
4548 break ()
4649 endif ()
4750endforeach ()
51+ if (_EXPLICIT_ARCH_SELECTED)
52+ # Explicit architecture options always win over auto-detection.
53+ set (AUTO_DETECT_ARCH OFF )
54+ endif ()
4855
4956include (CheckCCompilerFlag )
5057
@@ -89,11 +96,23 @@ function(_detect_armv8_best)
8996endfunction ()
9097
9198function (_detect_x86_best )
99+ if (NOT CMAKE_CROSSCOMPILING )
100+ # For native builds, match the actual host CPU capabilities.
101+ check_c_compiler_flag ("-march=native" _COMP_SUPP_native )
102+ if (_COMP_SUPP_native)
103+ _AppendFlags (CMAKE_C_FLAGS "-march=native" )
104+ _AppendFlags (CMAKE_CXX_FLAGS "-march=native" )
105+ set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} " PARENT_SCOPE )
106+ set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} " PARENT_SCOPE )
107+ return ()
108+ endif ()
109+ endif ()
110+
92111 set (_x86_flags
93- "graniterapids" "emeraldrapids" "sapphirerapids"
94- "skylake-avx512" "skylake"
95- "broadwell" "haswell" "sandybridge" "nehalem"
112+ "haswell" "broadwell" "skylake"
113+ "sandybridge" "nehalem"
96114 "znver3" "znver2" "znver1"
115+ "x86-64-v2" "x86-64"
97116 )
98117 foreach (_arch IN LISTS _x86_flags)
99118 check_c_compiler_flag ("-march=${_arch} " _COMP_SUPP_${_arch} )
0 commit comments