1# This is a helper function and not a build rule. It is to be used by the
2# various test rules to generate the full list of object files
3# recursively produced by "add_entrypoint_object" and "add_object_library"
4# targets.
5# Usage:
6#   get_object_files_for_test(<result var> <target0> [<target1> ...])
7#
8#   targetN is either an "add_entrypoint_target" target or an
9#   "add_object_library" target.
10function(get_object_files_for_test result)
11  set(object_files "")
12  foreach(dep IN LISTS ARGN)
13    get_target_property(dep_type ${dep} "TARGET_TYPE")
14    if(NOT dep_type)
15      # Target for which TARGET_TYPE property is not set do not
16      # provide any object files.
17      continue()
18    endif()
19
20    if(${dep_type} STREQUAL ${OBJECT_LIBRARY_TARGET_TYPE})
21      get_target_property(dep_object_files ${dep} "OBJECT_FILES")
22      if(dep_object_files)
23        list(APPEND object_files ${dep_object_files})
24      endif()
25    elseif(${dep_type} STREQUAL ${ENTRYPOINT_OBJ_TARGET_TYPE})
26      get_target_property(object_file_raw ${dep} "OBJECT_FILE_RAW")
27      if(object_file_raw)
28        list(APPEND object_files ${object_file_raw})
29      endif()
30    endif()
31
32    get_target_property(indirect_deps ${dep} "DEPS")
33    get_object_files_for_test(indirect_objfiles ${indirect_deps})
34    list(APPEND object_files ${indirect_objfiles})
35  endforeach(dep)
36  list(REMOVE_DUPLICATES object_files)
37  set(${result} ${object_files} PARENT_SCOPE)
38endfunction(get_object_files_for_test)
39
40# Rule to add a libc unittest.
41# Usage
42#    add_libc_unittest(
43#      <target name>
44#      SUITE <name of the suite this test belongs to>
45#      SRCS  <list of .cpp files for the test>
46#      HDRS  <list of .h files for the test>
47#      DEPENDS <list of dependencies>
48#      COMPILE_OPTIONS <list of special compile options for this target>
49#    )
50function(add_libc_unittest target_name)
51  if(NOT LLVM_INCLUDE_TESTS)
52    return()
53  endif()
54
55  cmake_parse_arguments(
56    "LIBC_UNITTEST"
57    "" # No optional arguments
58    "SUITE" # Single value arguments
59    "SRCS;HDRS;DEPENDS;COMPILE_OPTIONS" # Multi-value arguments
60    ${ARGN}
61  )
62  if(NOT LIBC_UNITTEST_SRCS)
63    message(FATAL_ERROR "'add_libc_unittest' target requires a SRCS list of .cpp "
64                        "files.")
65  endif()
66  if(NOT LIBC_UNITTEST_DEPENDS)
67    message(FATAL_ERROR "'add_libc_unittest' target requires a DEPENDS list of "
68                        "'add_entrypoint_object' targets.")
69  endif()
70
71
72  get_fq_target_name(${target_name} fq_target_name)
73  add_executable(
74    ${fq_target_name}
75    EXCLUDE_FROM_ALL
76    ${LIBC_UNITTEST_SRCS}
77    ${LIBC_UNITTEST_HDRS}
78  )
79  target_include_directories(
80    ${fq_target_name}
81    PRIVATE
82      ${LIBC_SOURCE_DIR}
83      ${LIBC_BUILD_DIR}
84      ${LIBC_BUILD_DIR}/include
85  )
86  if(LIBC_UNITTEST_COMPILE_OPTIONS)
87    target_compile_options(
88      ${target_name}
89      PRIVATE ${LIBC_UNITTEST_COMPILE_OPTIONS}
90    )
91  endif()
92
93  get_fq_deps_list(fq_deps_list ${LIBC_UNITTEST_DEPENDS})
94  get_object_files_for_test(link_object_files ${fq_deps_list})
95  target_link_libraries(${fq_target_name} PRIVATE ${link_object_files})
96
97  set_target_properties(${fq_target_name}
98    PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
99
100  add_dependencies(
101    ${fq_target_name}
102    ${fq_deps_list}
103  )
104
105  target_link_libraries(${fq_target_name} PRIVATE LibcUnitTest libc_test_utils)
106
107  add_custom_command(
108    TARGET ${fq_target_name}
109    POST_BUILD
110    COMMAND $<TARGET_FILE:${fq_target_name}>
111  )
112  if(LIBC_UNITTEST_SUITE)
113    add_dependencies(
114      ${LIBC_UNITTEST_SUITE}
115      ${fq_target_name}
116    )
117  endif()
118endfunction(add_libc_unittest)
119
120function(add_libc_testsuite suite_name)
121  add_custom_target(${suite_name})
122  add_dependencies(check-libc ${suite_name})
123endfunction(add_libc_testsuite)
124
125# Rule to add a fuzzer test.
126# Usage
127#    add_libc_fuzzer(
128#      <target name>
129#      SRCS  <list of .cpp files for the test>
130#      HDRS  <list of .h files for the test>
131#      DEPENDS <list of dependencies>
132#    )
133function(add_libc_fuzzer target_name)
134  cmake_parse_arguments(
135    "LIBC_FUZZER"
136    "" # No optional arguments
137    "" # Single value arguments
138    "SRCS;HDRS;DEPENDS" # Multi-value arguments
139    ${ARGN}
140  )
141  if(NOT LIBC_FUZZER_SRCS)
142    message(FATAL_ERROR "'add_libc_fuzzer' target requires a SRCS list of .cpp "
143                        "files.")
144  endif()
145  if(NOT LIBC_FUZZER_DEPENDS)
146    message(FATAL_ERROR "'add_libc_fuzzer' target requires a DEPENDS list of "
147                        "'add_entrypoint_object' targets.")
148  endif()
149
150  get_fq_target_name(${target_name} fq_target_name)
151  add_executable(
152    ${fq_target_name}
153    EXCLUDE_FROM_ALL
154    ${LIBC_FUZZER_SRCS}
155    ${LIBC_FUZZER_HDRS}
156  )
157  target_include_directories(
158    ${fq_target_name}
159    PRIVATE
160      ${LIBC_SOURCE_DIR}
161      ${LIBC_BUILD_DIR}
162      ${LIBC_BUILD_DIR}/include
163  )
164
165  get_fq_deps_list(fq_deps_list ${LIBC_FUZZER_DEPENDS})
166  get_object_files_for_test(link_object_files ${fq_deps_list})
167  target_link_libraries(${fq_target_name} PRIVATE ${link_object_files})
168
169  set_target_properties(${fq_target_name}
170      PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
171
172  add_dependencies(
173    ${fq_target_name}
174    ${fq_deps_list}
175  )
176  add_dependencies(libc-fuzzer ${fq_target_name})
177endfunction(add_libc_fuzzer)
178