xref: /llvm-project/libc/test/UnitTest/CMakeLists.txt (revision 80cf21dad150ae8fb82e4a17f3bc594d1486b210)
1function(add_unittest_framework_library name)
2  cmake_parse_arguments(
3    "TEST_LIB"
4    "" # No optional arguments
5    "" # No single value arguments
6    "SRCS;HDRS;DEPENDS" # Multi value arguments
7    ${ARGN}
8  )
9  if(NOT TEST_LIB_SRCS)
10    message(FATAL_ERROR "'add_unittest_framework_library' requires SRCS; for "
11                        "header only libraries, use 'add_header_library'")
12  endif()
13
14  foreach(lib IN ITEMS ${name}.unit ${name}.hermetic)
15    add_library(
16      ${lib}
17      STATIC
18      EXCLUDE_FROM_ALL
19      ${TEST_LIB_SRCS}
20      ${TEST_LIB_HDRS}
21    )
22    target_include_directories(${lib} PRIVATE ${LIBC_SOURCE_DIR})
23    if(TARGET libc.src.time.clock)
24      target_compile_definitions(${lib} PRIVATE TARGET_SUPPORTS_CLOCK)
25    endif()
26  endforeach()
27
28  if(LLVM_LIBC_FULL_BUILD)
29    # TODO: Build test framework with LIBC_FULL_BUILD in full build mode after
30    # making LibcFPExceptionHelpers and LibcDeathTestExecutors hermetic.
31    set(LLVM_LIBC_FULL_BUILD "")
32    _get_common_test_compile_options(compile_options "" "")
33    target_compile_options(${name}.unit PRIVATE ${compile_options})
34    set(LLVM_LIBC_FULL_BUILD ON)
35  else()
36    _get_common_test_compile_options(compile_options "" "")
37    target_compile_options(${name}.unit PRIVATE ${compile_options})
38endif()
39
40  _get_hermetic_test_compile_options(compile_options "")
41  target_include_directories(${name}.hermetic PRIVATE ${LIBC_INCLUDE_DIR})
42  target_compile_options(${name}.hermetic PRIVATE ${compile_options} -nostdinc++)
43
44  if(TEST_LIB_DEPENDS)
45    foreach(dep IN ITEMS ${TEST_LIB_DEPENDS})
46      if(TARGET ${dep}.unit)
47        add_dependencies(${name}.unit ${dep}.unit)
48      else()
49        add_dependencies(${name}.unit ${dep})
50      endif()
51      if(TARGET ${dep}.hermetic)
52        add_dependencies(${name}.hermetic ${dep}.hermetic)
53      else()
54        add_dependencies(${name}.hermetic ${dep})
55      endif()
56    endforeach()
57  endif()
58endfunction()
59
60add_unittest_framework_library(
61  LibcTest
62  SRCS
63    CmakeFilePath.cpp
64    LibcTest.cpp
65    LibcTestMain.cpp
66    TestLogger.cpp
67  HDRS
68    LibcTest.h
69    Test.h
70    TestLogger.h
71  DEPENDS
72    libc.src.__support.big_int
73    libc.src.__support.c_string
74    libc.src.__support.CPP.string
75    libc.src.__support.CPP.string_view
76    libc.src.__support.CPP.type_traits
77    libc.src.__support.fixed_point.fx_rep
78    libc.src.__support.macros.properties.types
79    libc.src.__support.OSUtil.osutil
80    libc.src.__support.uint128
81)
82
83set(libc_death_test_srcs LibcDeathTestExecutors.cpp)
84if(${LIBC_TARGET_OS} STREQUAL "linux")
85  list(APPEND libc_death_test_srcs ExecuteFunctionUnix.cpp)
86endif()
87
88add_unittest_framework_library(
89  LibcDeathTestExecutors
90  SRCS
91    ${libc_death_test_srcs}
92  HDRS
93    ExecuteFunction.h
94)
95
96add_unittest_framework_library(
97  LibcHermeticTestSupport
98  SRCS
99    HermeticTestUtils.cpp
100)
101
102add_header_library(
103  string_utils
104  HDRS
105    StringUtils.h
106  DEPENDS
107    libc.src.__support.big_int
108    libc.src.__support.CPP.string
109    libc.src.__support.CPP.type_traits
110)
111
112add_unittest_framework_library(
113  LibcFPTestHelpers
114  SRCS
115    FEnvSafeTest.cpp
116    RoundingModeUtils.cpp
117  HDRS
118    FEnvSafeTest.h
119    FPMatcher.h
120    RoundingModeUtils.h
121  DEPENDS
122    LibcTest
123    libc.test.UnitTest.string_utils
124    libc.src.__support.CPP.array
125    libc.src.__support.FPUtil.fp_bits
126    libc.src.__support.FPUtil.fpbits_str
127    libc.src.__support.FPUtil.fenv_impl
128    libc.src.__support.FPUtil.rounding_mode
129)
130
131add_unittest_framework_library(
132  LibcFPExceptionHelpers
133  SRCS
134    FPExceptMatcher.cpp
135  HDRS
136    FPExceptMatcher.h
137  DEPENDS
138    LibcTest
139    libc.src.__support.FPUtil.fp_bits
140    libc.src.__support.FPUtil.fenv_impl
141    libc.hdr.types.fenv_t
142)
143
144add_unittest_framework_library(
145  LibcMemoryHelpers
146  SRCS
147    MemoryMatcher.cpp
148  HDRS
149    MemoryMatcher.h
150  DEPENDS
151    LibcTest
152    libc.src.__support.CPP.span
153)
154
155add_unittest_framework_library(
156  LibcPrintfHelpers
157  SRCS
158    PrintfMatcher.cpp
159  HDRS
160    PrintfMatcher.h
161  DEPENDS
162    LibcTest
163    libc.src.__support.FPUtil.fp_bits
164    libc.src.stdio.printf_core.core_structs
165    libc.test.UnitTest.string_utils
166)
167
168add_unittest_framework_library(
169  LibcScanfHelpers
170  SRCS
171    ScanfMatcher.cpp
172  HDRS
173    ScanfMatcher.h
174  DEPENDS
175    LibcTest
176    libc.src.__support.FPUtil.fp_bits
177    libc.src.stdio.scanf_core.core_structs
178    libc.test.UnitTest.string_utils
179)
180
181add_header_library(
182  ErrnoSetterMatcher
183  HDRS
184    ErrnoSetterMatcher.h
185  DEPENDS
186    libc.src.__support.common
187    libc.src.__support.FPUtil.fp_bits
188    libc.src.__support.StringUtil.error_to_string
189    libc.src.errno.errno
190)
191