+include(CMakePushCheckState)
include(CheckCXXCompilerFlag)
include(CheckLibraryExists)
include(CheckSymbolExists)
set(SIMPLE_SOURCE ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/simple.cc)
file(WRITE ${SIMPLE_SOURCE} "#include <stdlib.h>\n#include <limits>\nint main() {}\n")
-# test_target_arch(<arch> <target flags...>)
-# Sets the target flags for a given architecture and determines if this
-# architecture is supported by trying to build a simple file.
-macro(test_target_arch arch)
+function(check_compile_definition def argstring out_var)
+ if("${def}" STREQUAL "")
+ set(${out_var} TRUE PARENT_SCOPE)
+ return()
+ endif()
+ cmake_push_check_state()
+ set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${argstring}")
+ check_symbol_exists(${def} "" ${out_var})
+ cmake_pop_check_state()
+endfunction()
+
+# test_target_arch(<arch> <def> <target flags...>)
+# Checks if architecture is supported: runs host compiler with provided
+# flags to verify that:
+# 1) <def> is defined (if non-empty)
+# 2) simple file can be successfully built.
+# If successful, saves target flags for this architecture.
+macro(test_target_arch arch def)
set(TARGET_${arch}_CFLAGS ${ARGN})
- set(argstring "${CMAKE_EXE_LINKER_FLAGS}")
+ set(argstring "")
foreach(arg ${ARGN})
set(argstring "${argstring} ${arg}")
endforeach()
- try_compile(CAN_TARGET_${arch} ${CMAKE_BINARY_DIR} ${SIMPLE_SOURCE}
- COMPILE_DEFINITIONS "${TARGET_${arch}_CFLAGS}"
- OUTPUT_VARIABLE TARGET_${arch}_OUTPUT
- CMAKE_FLAGS "-DCMAKE_EXE_LINKER_FLAGS:STRING=${argstring}")
+ check_compile_definition("${def}" "${argstring}" HAS_${arch}_DEF)
+ if(NOT HAS_${arch}_DEF)
+ set(CAN_TARGET_${arch} FALSE)
+ else()
+ set(argstring "${CMAKE_EXE_LINKER_FLAGS} ${argstring}")
+ try_compile(CAN_TARGET_${arch} ${CMAKE_BINARY_DIR} ${SIMPLE_SOURCE}
+ COMPILE_DEFINITIONS "${TARGET_${arch}_CFLAGS}"
+ OUTPUT_VARIABLE TARGET_${arch}_OUTPUT
+ CMAKE_FLAGS "-DCMAKE_EXE_LINKER_FLAGS:STRING=${argstring}")
+ endif()
if(${CAN_TARGET_${arch}})
list(APPEND COMPILER_RT_SUPPORTED_ARCH ${arch})
elseif("${COMPILER_RT_TEST_TARGET_ARCH}" MATCHES "${arch}")
else()
if("${LLVM_NATIVE_ARCH}" STREQUAL "X86")
if(NOT MSVC)
- test_target_arch(x86_64 "-m64")
- test_target_arch(i386 "-m32")
+ test_target_arch(x86_64 "" "-m64")
+ test_target_arch(i386 __i386__ "-m32")
+ test_target_arch(i686 __i686__ "-m32")
else()
- test_target_arch(i386 "")
+ test_target_arch(i386 "" "")
endif()
elseif("${LLVM_NATIVE_ARCH}" STREQUAL "PowerPC")
TEST_BIG_ENDIAN(HOST_IS_BIG_ENDIAN)
if(HOST_IS_BIG_ENDIAN)
- test_target_arch(powerpc64 "-m64")
+ test_target_arch(powerpc64 "" "-m64")
else()
- test_target_arch(powerpc64le "-m64")
+ test_target_arch(powerpc64le "" "-m64")
endif()
elseif("${LLVM_NATIVE_ARCH}" STREQUAL "Mips")
if("${COMPILER_RT_TEST_TARGET_ARCH}" MATCHES "mipsel|mips64el")
# regex for mipsel, mips64el
- test_target_arch(mipsel "-m32")
- test_target_arch(mips64el "-m64")
+ test_target_arch(mipsel "" "-m32")
+ test_target_arch(mips64el "" "-m64")
else()
- test_target_arch(mips "-m32")
- test_target_arch(mips64 "-m64")
+ test_target_arch(mips "" "-m32")
+ test_target_arch(mips64 "" "-m64")
endif()
elseif("${COMPILER_RT_TEST_TARGET_ARCH}" MATCHES "arm")
- test_target_arch(arm "-march=armv7-a")
+ test_target_arch(arm "" "-march=armv7-a")
elseif("${COMPILER_RT_TEST_TARGET_ARCH}" MATCHES "aarch32")
- test_target_arch(aarch32 "-march=armv8-a")
+ test_target_arch(aarch32 "" "-march=armv8-a")
elseif("${COMPILER_RT_TEST_TARGET_ARCH}" MATCHES "aarch64")
- test_target_arch(aarch64 "-march=armv8-a")
+ test_target_arch(aarch64 "" "-march=armv8-a")
endif()
set(COMPILER_RT_OS_SUFFIX "")
endif()