@@ -44,44 +44,32 @@ function(_intersection output_var list1 list2)
4444 set (${output_var} ${tmp} PARENT_SCOPE)
4545endfunction ()
4646
47- # Generates a cpp file to introspect the compiler defined flags.
48- function (_generate_check_code)
47+ set (AVAILABLE_CPU_FEATURES "" )
48+ if (LIBC_CROSSBUILD)
49+ # If we are doing a cross build, we will just assume that all CPU features
50+ # are available.
51+ set (AVAILABLE_CPU_FEATURES ${ALL_CPU_FEATURES} )
52+ else ()
53+ # Try compile a C file to check if flag is supported.
54+ set (CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
4955 foreach (feature IN LISTS ALL_CPU_FEATURES)
50- set (DEFINITIONS
51- "${DEFINITIONS}
52- #ifdef __${feature} __
53- \" ${feature} \" ,
54- #endif" )
56+ try_compile (
57+ has_feature
58+ ${CMAKE_CURRENT_BINARY_DIR} /cpu_features
59+ SOURCES ${LIBC_SOURCE_DIR} /cmake/modules/cpu_features/check_${feature} .cpp
60+ COMPILE_DEFINITIONS -I${LIBC_SOURCE_DIR} ${LIBC_COMPILE_OPTIONS_NATIVE}
61+ )
62+ if (has_feature)
63+ list (APPEND AVAILABLE_CPU_FEATURES ${feature} )
64+ endif ()
5565 endforeach ()
56- configure_file (
57- "${LIBC_SOURCE_DIR} /cmake/modules/cpu_features/check_cpu_features.cpp.in"
58- "cpu_features/check_cpu_features.cpp" @ONLY)
59- endfunction ()
60- _generate_check_code()
66+ endif ()
6167
62- set (LIBC_CPU_FEATURES "" CACHE PATH "Host supported CPU features" )
68+ set (LIBC_CPU_FEATURES ${AVAILABLE_CPU_FEATURES} CACHE STRING "Host supported CPU features" )
6369
64- if (LIBC_CROSSBUILD)
65- _intersection(cpu_features "${ALL_CPU_FEATURES} " "${LIBC_CPU_FEATURES} " )
66- if (NOT "${cpu_features} " STREQUAL "${LIBC_CPU_FEATURES} " )
67- message (FATAL_ERROR "Unsupported CPU features: ${cpu_features} " )
68- endif ()
69- message (STATUS "Set CPU features: ${cpu_features} " )
70- set (LIBC_CPU_FEATURES "${cpu_features} " )
70+ _intersection(cpu_features "${AVAILABLE_CPU_FEATURES} " "${LIBC_CPU_FEATURES} " )
71+ if (NOT "${cpu_features} " STREQUAL "${LIBC_CPU_FEATURES} " )
72+ message (FATAL_ERROR "Unsupported CPU features: ${cpu_features} " )
7173else ()
72- # Populates the LIBC_CPU_FEATURES list from host.
73- try_run (
74- run_result compile_result "${CMAKE_CURRENT_BINARY_DIR} /check_${feature} "
75- "${CMAKE_CURRENT_BINARY_DIR} /cpu_features/check_cpu_features.cpp"
76- COMPILE_DEFINITIONS ${LIBC_COMPILE_OPTIONS_NATIVE}
77- COMPILE_OUTPUT_VARIABLE compile_output
78- RUN_OUTPUT_VARIABLE run_output)
79- if ("${run_result} " EQUAL 0)
80- message (STATUS "Set CPU features: ${run_output} " )
81- set (LIBC_CPU_FEATURES "${run_output} " )
82- elseif (NOT ${compile_result} )
83- message (FATAL_ERROR "Failed to compile: ${compile_output} " )
84- else ()
85- message (FATAL_ERROR "Failed to run: ${run_output} " )
86- endif ()
74+ message (STATUS "Set CPU features: ${cpu_features} " )
8775endif ()
0 commit comments