xref: /llvm-project/compiler-rt/test/builtins/CMakeLists.txt (revision a35ac42fac88e82748a7e035821a1c6226be9ac0)
1set(BUILTINS_LIT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
2
3set(BUILTINS_TEST_DEPS ${SANITIZER_COMMON_LIT_TEST_DEPS} builtins)
4set(BUILTINS_TESTSUITES ${CMAKE_CURRENT_BINARY_DIR}/TestCases)
5
6# Test cases.
7configure_lit_site_cfg(
8  ${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.py.in
9  ${CMAKE_CURRENT_BINARY_DIR}/lit.site.cfg.py
10)
11
12#Unit tests.
13
14include(builtin-config-ix)
15
16if (COMPILER_RT_BUILD_CRT)
17  list(APPEND BUILTINS_TEST_DEPS crt)
18endif()
19pythonize_bool(COMPILER_RT_BUILD_CRT)
20
21# Indicate if this is an MSVC environment.
22pythonize_bool(MSVC)
23
24# Indicate if the compiler for the builtins library was MSVC. If the builtins
25# compiler was clang-cl, we will enable some features that the host compiler
26# will not, like C99 _Complex and int128.
27set(BUILTINS_IS_MSVC OFF)
28if (MSVC AND NOT "${CMAKE_C_COMPILER_ID}" MATCHES "Clang")
29  set(BUILTINS_IS_MSVC ON)
30endif()
31pythonize_bool(BUILTINS_IS_MSVC)
32
33set(BUILTIN_TEST_ARCH ${BUILTIN_SUPPORTED_ARCH})
34if(APPLE)
35  darwin_filter_host_archs(BUILTIN_SUPPORTED_ARCH BUILTIN_TEST_ARCH)
36endif()
37
38foreach(arch ${BUILTIN_TEST_ARCH})
39  set(BUILTINS_TEST_TARGET_ARCH ${arch})
40  string(TOLOWER "-${arch}-${OS_NAME}" BUILTINS_TEST_CONFIG_SUFFIX)
41  get_test_cc_for_arch(${arch} BUILTINS_TEST_TARGET_CC BUILTINS_TEST_TARGET_CFLAGS)
42  if (${arch} STREQUAL "armhf")
43    list(APPEND BUILTINS_TEST_TARGET_CFLAGS -fomit-frame-pointer -DCOMPILER_RT_ARMHF_TARGET)
44    string(REPLACE ";" " " BUILTINS_TEST_TARGET_CFLAGS "${BUILTINS_TEST_TARGET_CFLAGS}")
45  endif()
46
47  if (COMPILER_RT_ENABLE_SOFTWARE_INT128 OR ${arch} STREQUAL "riscv32")
48    list(APPEND BUILTINS_TEST_TARGET_CFLAGS -fforce-enable-int128)
49    string(REPLACE ";" " " BUILTINS_TEST_TARGET_CFLAGS "${BUILTINS_TEST_TARGET_CFLAGS}")
50  endif()
51
52  if(APPLE)
53    # TODO: Support the new ABI on Apple platforms.
54    if (${arch} MATCHES "arm|armhf|aarch64|arm64" AND COMPILER_RT_HAS_${arch}_FLOAT16)
55      list(APPEND BUILTINS_TEST_TARGET_CFLAGS -DCOMPILER_RT_HAS_FLOAT16)
56      string(REPLACE ";" " " BUILTINS_TEST_TARGET_CFLAGS "${BUILTINS_TEST_TARGET_CFLAGS}")
57    endif()
58  else()
59    if (${arch} MATCHES "arm|armhf|aarch64|arm64|i?86|x86_64|AMD64|riscv32|riscv64" AND COMPILER_RT_HAS_${arch}_FLOAT16)
60      list(APPEND BUILTINS_TEST_TARGET_CFLAGS -DCOMPILER_RT_HAS_FLOAT16)
61      string(REPLACE ";" " " BUILTINS_TEST_TARGET_CFLAGS "${BUILTINS_TEST_TARGET_CFLAGS}")
62    endif()
63  endif()
64
65  if(COMPILER_RT_ENABLE_CET)
66    if(NOT arch MATCHES "i?86|x86_64|AMD64")
67      message(SEND_ERROR "${arch} does not support CET")
68    endif()
69    if(COMPILER_RT_HAS_FCF_PROTECTION_FLAG)
70      list(APPEND BUILTINS_TEST_TARGET_CFLAGS -fcf-protection=full)
71      string(REPLACE ";" " " BUILTINS_TEST_TARGET_CFLAGS "${BUILTINS_TEST_TARGET_CFLAGS}")
72    endif()
73  endif()
74
75  # Compute builtins available in library and add them as lit features.
76  if(APPLE)
77    # TODO: Support other Apple platforms.
78    set(BUILTIN_LIB_TARGET_NAME "clang_rt.builtins_${arch}_osx")
79  else()
80    set(BUILTIN_LIB_TARGET_NAME "clang_rt.builtins-${arch}")
81  endif()
82  if (NOT TARGET "${BUILTIN_LIB_TARGET_NAME}")
83    message(FATAL_ERROR "Target ${BUILTIN_LIB_TARGET_NAME} does not exist")
84  endif()
85  get_target_property(BUILTIN_LIB_SOURCES "${BUILTIN_LIB_TARGET_NAME}" SOURCES)
86  list(LENGTH BUILTIN_LIB_SOURCES BUILTIN_LIB_SOURCES_LENGTH)
87  if (BUILTIN_LIB_SOURCES_LENGTH EQUAL 0)
88    message(FATAL_ERROR "Failed to find source files for ${arch} builtin library")
89  endif()
90  set(BUILTINS_LIT_SOURCE_FEATURES "")
91  foreach (file_name ${BUILTIN_LIB_SOURCES})
92    # Strip off any directories and file extensions. This approach means we add
93    # add a single feature if there is a C source file or assembly override
94    # present in the builtin library.
95    # E.g.
96    # "hexagon/udivsi3.S" => "udivsi3"
97    # "udivsi3.c" => "udivsi3"
98    get_filename_component(FILE_NAME_FILTERED "${file_name}" NAME_WE)
99    list(APPEND BUILTINS_LIT_SOURCE_FEATURES "librt_has_${FILE_NAME_FILTERED}")
100  endforeach()
101
102  string(TOUPPER ${arch} ARCH_UPPER_CASE)
103  set(CONFIG_NAME ${ARCH_UPPER_CASE}${OS_NAME}Config)
104  configure_lit_site_cfg(
105    ${CMAKE_CURRENT_SOURCE_DIR}/Unit/lit.site.cfg.py.in
106    ${CMAKE_CURRENT_BINARY_DIR}/Unit/${CONFIG_NAME}/lit.site.cfg.py
107    )
108  list(APPEND BUILTINS_TESTSUITES ${CMAKE_CURRENT_BINARY_DIR}/Unit/${CONFIG_NAME})
109endforeach()
110
111# TODO: Add support for running tests on iOS and iOS simulator.
112
113add_lit_testsuite(check-builtins "Running the Builtins tests"
114  ${BUILTINS_TESTSUITES}
115  DEPENDS ${BUILTINS_TEST_DEPS})
116