1function(add_fp_unittest name)
2  cmake_parse_arguments(
3    "MATH_UNITTEST"
4    "NEED_MPFR" # Optional arguments
5    "" # Single value arguments
6    "LINK_LIBRARIES" # Multi-value arguments
7    ${ARGN}
8  )
9
10  if(MATH_UNITTEST_NEED_MPFR)
11    if(NOT LIBC_TESTS_CAN_USE_MPFR)
12      message("WARNING: Math test ${name} will be skipped as MPFR library is not available.")
13      return()
14    endif()
15  endif()
16
17  if(MATH_UNITTEST_NEED_MPFR)
18    list(APPEND MATH_UNITTEST_LINK_LIBRARIES libcMPFRWrapper -lmpfr -lgmp)
19  endif()
20  list(APPEND MATH_UNITTEST_LINK_LIBRARIES LibcFPTestHelpers)
21
22  add_libc_unittest(
23    ${name}
24    LINK_LIBRARIES "${MATH_UNITTEST_LINK_LIBRARIES}"
25    "${MATH_UNITTEST_UNPARSED_ARGUMENTS}"
26  )
27endfunction(add_fp_unittest)
28
29add_subdirectory(__support)
30add_subdirectory(ctype)
31add_subdirectory(errno)
32add_subdirectory(fenv)
33add_subdirectory(inttypes)
34add_subdirectory(math)
35add_subdirectory(string)
36add_subdirectory(stdlib)
37
38if(${LIBC_TARGET_OS} STREQUAL "linux")
39  add_subdirectory(fcntl)
40  add_subdirectory(sys)
41  add_subdirectory(unistd)
42endif()
43
44if(NOT LLVM_LIBC_FULL_BUILD)
45  return()
46endif()
47
48# The signal API is currently disabled as signal.h is incorrect.
49# since assert uses the signal API, we disable assert also.
50# add_subdirectory(assert)
51# add_subdirectory(signal)
52add_subdirectory(stdio)
53add_subdirectory(time)
54
55if(${LIBC_TARGET_OS} STREQUAL "linux")
56  add_subdirectory(pthread)
57endif()
58
59set(public_test ${CMAKE_CURRENT_BINARY_DIR}/public_api_test.cpp)
60
61set(entrypoints_name_list "")
62foreach(entry IN LISTS TARGET_LLVMLIBC_ENTRYPOINTS)
63  get_target_property(entry_name ${entry} "ENTRYPOINT_NAME")
64  list(APPEND entrypoints_name_list ${entry_name})
65endforeach()
66
67# TODO: Remove these when they are added to the TableGen.
68list(REMOVE_ITEM entrypoints_name_list "__assert_fail" "__errno_location")
69list(TRANSFORM entrypoints_name_list PREPEND "-e=")
70
71file(GLOB spec_files ${LIBC_SOURCE_DIR}/spec/*.td)
72
73# Generate api test souce code.
74add_custom_command(
75  OUTPUT ${public_test}
76  COMMAND $<TARGET_FILE:libc-prototype-testgen> -o ${public_test}
77          ${entrypoints_name_list}
78          -I ${LIBC_SOURCE_DIR}
79          ${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/api.td
80
81  DEPENDS ${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/api.td ${spec_files}
82          libc-prototype-testgen ${TARGET_PUBLIC_HEADERS}
83          llvmlibc
84)
85
86add_executable(
87  libc-api-test
88  EXCLUDE_FROM_ALL
89  ${public_test}
90)
91# Blank out default include directories to prevent accidentally including
92# system headers or our own internal headers.
93set_target_properties(
94  libc-api-test
95  PROPERTIES
96  INCLUDE_DIRECTORIES ""
97)
98target_link_libraries(libc-api-test llvmlibc)
99
100# Only include we need is the include for cpp::IsSame and our generated
101# public headers.
102target_include_directories(
103  libc-api-test BEFORE
104  PRIVATE
105    "${LIBC_SOURCE_DIR}/src/__support/CPP"
106    "${LIBC_BUILD_DIR}/include"
107)
108target_compile_options(
109  libc-api-test
110  PRIVATE
111  -ffreestanding
112)
113target_link_options(
114  libc-api-test
115  PRIVATE "-nostdlib"
116)
117set(library_files)
118foreach(library_name IN LISTS "llvmlibc")
119  get_target_property(library_file ${library_name} "LIBRARY_FILE")
120  list(APPEND library_files ${library_file})
121endforeach()
122
123if(COMPILER_RESOURCE_DIR AND LLVM_LIBC_ENABLE_LINTING)
124  add_custom_target(
125    libc-api-test-tidy
126    VERBATIM
127    COMMAND $<TARGET_FILE:clang-tidy> --system-headers
128      --checks=-*,llvmlibc-restrict-system-libc-headers
129      "--extra-arg=-resource-dir=${COMPILER_RESOURCE_DIR}"
130      --header-filter=.*
131      --warnings-as-errors=llvmlibc-*
132      "-config={CheckOptions: [{key: llvmlibc-restrict-system-libc-headers.Includes, value: '-*, linux/*, asm/*.h, asm-generic/*.h'}]}"
133      --quiet
134      -p ${PROJECT_BINARY_DIR}
135      ${public_test}
136    DEPENDS
137      clang-tidy ${public_test}
138  )
139  add_dependencies(libc-api-test libc-api-test-tidy)
140endif()
141
142target_link_libraries(libc-api-test
143  PRIVATE
144  ${library_files}
145)
146