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>
7#                             <skipped_entrypoints_var>
8#                             <target0> [<target1> ...])
9#
10#   The list of object files is collected in <result_var>.
11#   If skipped entrypoints were found, then <skipped_entrypoints_var> is
12#   set to a true value.
13#   targetN is either an "add_entrypoint_target" target or an
14#   "add_object_library" target.
15function(get_object_files_for_test result skipped_entrypoints_list)
16  set(object_files "")
17  set(skipped_list "")
18  foreach(dep IN LISTS ARGN)
19    get_target_property(dep_type ${dep} "TARGET_TYPE")
20    if(NOT dep_type)
21      # Target for which TARGET_TYPE property is not set do not
22      # provide any object files.
23      continue()
24    endif()
25
26    if(${dep_type} STREQUAL ${OBJECT_LIBRARY_TARGET_TYPE})
27      get_target_property(dep_object_files ${dep} "OBJECT_FILES")
28      if(dep_object_files)
29        list(APPEND object_files ${dep_object_files})
30      endif()
31    elseif(${dep_type} STREQUAL ${ENTRYPOINT_OBJ_TARGET_TYPE})
32      get_target_property(is_skipped ${dep} "SKIPPED")
33      if(is_skipped)
34        list(APPEND skipped_list ${dep})
35        continue()
36      endif()
37      get_target_property(object_file_raw ${dep} "OBJECT_FILE_RAW")
38      if(object_file_raw)
39        list(APPEND object_files ${object_file_raw})
40      endif()
41    endif()
42
43    get_target_property(indirect_deps ${dep} "DEPS")
44    get_object_files_for_test(
45        indirect_objfiles indirect_skipped_list ${indirect_deps})
46    list(APPEND object_files ${indirect_objfiles})
47    if(indirect_skipped_list)
48      list(APPEND skipped_list ${indirect_skipped_list})
49    endif()
50  endforeach(dep)
51  list(REMOVE_DUPLICATES object_files)
52  set(${result} ${object_files} PARENT_SCOPE)
53  list(REMOVE_DUPLICATES skipped_list)
54  set(${skipped_entrypoints_list} ${skipped_list} PARENT_SCOPE)
55endfunction(get_object_files_for_test)
56
57# Rule to add a libc unittest.
58# Usage
59#    add_libc_unittest(
60#      <target name>
61#      SUITE <name of the suite this test belongs to>
62#      SRCS  <list of .cpp files for the test>
63#      HDRS  <list of .h files for the test>
64#      DEPENDS <list of dependencies>
65#      COMPILE_OPTIONS <list of special compile options for this target>
66#      LINK_OPTIONS <list of special linking options for this target>
67#    )
68function(add_libc_unittest target_name)
69  if(NOT LLVM_INCLUDE_TESTS)
70    return()
71  endif()
72
73  cmake_parse_arguments(
74    "LIBC_UNITTEST"
75    "NO_RUN_POSTBUILD" # Optional arguments
76    "SUITE;CXX_STANDARD" # Single value arguments
77    "SRCS;HDRS;DEPENDS;COMPILE_OPTIONS;LINK_OPTIONS" # Multi-value arguments
78    "NO_LIBC_UNITTEST_TEST_MAIN"
79    ${ARGN}
80  )
81  if(NOT LIBC_UNITTEST_SRCS)
82    message(FATAL_ERROR "'add_libc_unittest' target requires a SRCS list of .cpp "
83                        "files.")
84  endif()
85  if(NOT LIBC_UNITTEST_DEPENDS)
86    message(FATAL_ERROR "'add_libc_unittest' target requires a DEPENDS list of "
87                        "'add_entrypoint_object' targets.")
88  endif()
89
90  get_fq_target_name(${target_name} fq_target_name)
91  get_fq_deps_list(fq_deps_list ${LIBC_UNITTEST_DEPENDS})
92  get_object_files_for_test(
93      link_object_files skipped_entrypoints_list ${fq_deps_list})
94  if(skipped_entrypoints_list)
95    # If a test is OS/target machine independent, it has to be skipped if the
96    # OS/target machine combination does not provide any dependent entrypoints.
97    # If a test is OS/target machine specific, then such a test will live is a
98    # OS/target machine specific directory and will be skipped at the directory
99    # level if required.
100    #
101    # There can potentially be a setup like this: A unittest is setup for a
102    # OS/target machine independent object library, which in turn depends on a
103    # machine specific object library. Such a test would be testing internals of
104    # the libc and it is assumed that they will be rare in practice. So, they
105    # can be skipped in the corresponding CMake files using platform specific
106    # logic. This pattern is followed in the loader tests for example.
107    #
108    # Another pattern that is present currently is to detect machine
109    # capabilities and add entrypoints and tests accordingly. That approach is
110    # much lower level approach and is independent of the kind of skipping that
111    # is happening here at the entrypoint level.
112
113    set(msg "Skipping unittest ${fq_target_name} as it has missing deps: "
114            "${skipped_entrypoints_list}.")
115    message(STATUS ${msg})
116    return()
117  endif()
118
119  add_executable(
120    ${fq_target_name}
121    EXCLUDE_FROM_ALL
122    ${LIBC_UNITTEST_SRCS}
123    ${LIBC_UNITTEST_HDRS}
124  )
125  target_include_directories(
126    ${fq_target_name}
127    PRIVATE
128      ${LIBC_SOURCE_DIR}
129      ${LIBC_BUILD_DIR}
130      ${LIBC_BUILD_DIR}/include
131  )
132  target_compile_options(
133    ${fq_target_name}
134    PRIVATE ${LIBC_COMPILE_OPTIONS_DEFAULT}
135  )
136  if(LIBC_UNITTEST_COMPILE_OPTIONS)
137    target_compile_options(
138      ${fq_target_name}
139      PRIVATE ${LIBC_UNITTEST_COMPILE_OPTIONS}
140    )
141  endif()
142  if(LIBC_UNITTEST_CXX_STANDARD)
143    set_target_properties(
144      ${fq_target_name}
145      PROPERTIES
146        CXX_STANDARD ${LIBC_UNITTEST_CXX_STANDARD}
147    )
148  endif()
149
150  target_link_libraries(${fq_target_name} PRIVATE ${link_object_files})
151  if(LIBC_UNITTEST_LINK_OPTIONS)
152    target_link_options(
153      ${fq_target_name}
154      PRIVATE ${LIBC_UNITTEST_LINK_OPTIONS}
155    )
156  endif()
157
158  set_target_properties(${fq_target_name}
159    PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
160
161  add_dependencies(
162    ${fq_target_name}
163    ${fq_deps_list}
164  )
165
166  if(NO_LIBC_UNITTEST_TEST_MAIN)
167    target_link_libraries(${fq_target_name} PRIVATE LibcUnitTest libc_test_utils)
168  else()
169    target_link_libraries(${fq_target_name} PRIVATE LibcUnitTest LibcUnitTestMain libc_test_utils)
170  endif()
171
172  if(NOT LIBC_UNITTEST_NO_RUN_POSTBUILD)
173    add_custom_command(
174      TARGET ${fq_target_name}
175      POST_BUILD
176      COMMAND $<TARGET_FILE:${fq_target_name}>
177    )
178  endif()
179
180  if(LIBC_UNITTEST_SUITE)
181    add_dependencies(
182      ${LIBC_UNITTEST_SUITE}
183      ${fq_target_name}
184    )
185  endif()
186endfunction(add_libc_unittest)
187
188function(add_libc_testsuite suite_name)
189  add_custom_target(${suite_name})
190  add_dependencies(check-llvmlibc ${suite_name})
191endfunction(add_libc_testsuite)
192
193function(add_libc_exhaustive_testsuite suite_name)
194  add_custom_target(${suite_name})
195  add_dependencies(exhaustive-check-libc ${suite_name})
196endfunction(add_libc_exhaustive_testsuite)
197
198function(add_libc_long_running_testsuite suite_name)
199  add_custom_target(${suite_name})
200  add_dependencies(libc-long-running-tests ${suite_name})
201endfunction(add_libc_long_running_testsuite)
202
203# Rule to add a fuzzer test.
204# Usage
205#    add_libc_fuzzer(
206#      <target name>
207#      SRCS  <list of .cpp files for the test>
208#      HDRS  <list of .h files for the test>
209#      DEPENDS <list of dependencies>
210#    )
211function(add_libc_fuzzer target_name)
212  cmake_parse_arguments(
213    "LIBC_FUZZER"
214    "" # No optional arguments
215    "" # Single value arguments
216    "SRCS;HDRS;DEPENDS" # Multi-value arguments
217    ${ARGN}
218  )
219  if(NOT LIBC_FUZZER_SRCS)
220    message(FATAL_ERROR "'add_libc_fuzzer' target requires a SRCS list of .cpp "
221                        "files.")
222  endif()
223  if(NOT LIBC_FUZZER_DEPENDS)
224    message(FATAL_ERROR "'add_libc_fuzzer' target requires a DEPENDS list of "
225                        "'add_entrypoint_object' targets.")
226  endif()
227
228  get_fq_target_name(${target_name} fq_target_name)
229  get_fq_deps_list(fq_deps_list ${LIBC_FUZZER_DEPENDS})
230  get_object_files_for_test(
231      link_object_files skipped_entrypoints_list ${fq_deps_list})
232  if(skipped_entrypoints_list)
233    set(msg "Skipping fuzzer target ${fq_target_name} as it has missing deps: "
234            "${skipped_entrypoints_list}.")
235    message(STATUS ${msg})
236    add_custom_target(${fq_target_name})
237
238    # A post build custom command is used to avoid running the command always.
239    add_custom_command(
240      TARGET ${fq_target_name}
241      POST_BUILD
242      COMMAND ${CMAKE_COMMAND} -E echo ${msg}
243    )
244    return()
245  endif()
246
247  add_executable(
248    ${fq_target_name}
249    EXCLUDE_FROM_ALL
250    ${LIBC_FUZZER_SRCS}
251    ${LIBC_FUZZER_HDRS}
252  )
253  target_include_directories(
254    ${fq_target_name}
255    PRIVATE
256      ${LIBC_SOURCE_DIR}
257      ${LIBC_BUILD_DIR}
258      ${LIBC_BUILD_DIR}/include
259  )
260
261  target_link_libraries(${fq_target_name} PRIVATE ${link_object_files})
262
263  set_target_properties(${fq_target_name}
264      PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
265
266  add_dependencies(
267    ${fq_target_name}
268    ${fq_deps_list}
269  )
270  add_dependencies(libc-fuzzer ${fq_target_name})
271endfunction(add_libc_fuzzer)
272
273# Rule to add an integration test. An integration test is like a unit test
274# but does not use the system libc. Not even the loader from the system libc
275# is linked to the final executable. The final exe is fully statically linked.
276# The libc that the final exe links to consists of only the object files of
277# the DEPENDS targets.
278#
279# Usage:
280#   add_integration_test(
281#     <target name>
282#     SUITE <the suite to which the test should belong>
283#     SRCS <src1.cpp> [src2.cpp ...]
284#     HDRS [hdr1.cpp ...]
285#     LOADER <fully qualified loader target name>
286#     DEPENDS <list of entrypoint or other object targets>
287#     ARGS <list of command line arguments to be passed to the test>
288#     ENV <list of environment variables to set before running the test>
289#   )
290#
291# The loader target should provide a property named LOADER_OBJECT which is
292# the full path to the object file produces when the loader is built.
293#
294# The DEPENDS list can be empty. If not empty, it should be a list of
295# targets added with add_entrypoint_object or add_object_library.
296function(add_integration_test test_name)
297  get_fq_target_name(${test_name} fq_target_name)
298  if(NOT (${LIBC_TARGET_OS} STREQUAL "linux"))
299    message(STATUS "Skipping ${fq_target_name} as it is not available on ${LIBC_TARGET_OS}.")
300    return()
301  endif()
302  cmake_parse_arguments(
303    "INTEGRATION_TEST"
304    "" # No optional arguments
305    "SUITE;LOADER" # Single value arguments
306    "SRCS;HDRS;DEPENDS;ARGS;ENV" # Multi-value arguments
307    ${ARGN}
308  )
309
310  if(NOT INTEGRATION_TEST_SUITE)
311    message(FATAL_ERROR "SUITE not specified for ${fq_target_name}")
312  endif()
313  if(NOT INTEGRATION_TEST_LOADER)
314    message(FATAL_ERROR "The LOADER to link to the integration test is missing.")
315  endif()
316  if(NOT INTEGRATION_TEST_SRCS)
317    message(FATAL_ERROR "The SRCS list for add_integration_test is missing.")
318  endif()
319
320  get_fq_target_name(${test_name}.libc fq_libc_target_name)
321
322  get_fq_deps_list(fq_deps_list ${INTEGRATION_TEST_DEPENDS})
323  # Add memory functions to which compilers can emit calls.
324  list(APPEND fq_deps_list
325          libc.src.string.bcmp
326          libc.src.string.bzero
327          libc.src.string.memcmp
328          libc.src.string.memcpy
329          libc.src.string.memset)
330  list(REMOVE_DUPLICATES fq_deps_list)
331  # TODO: Instead of gathering internal object files from entrypoints,
332  # collect the object files with public names of entrypoints.
333  get_object_files_for_test(
334      link_object_files skipped_entrypoints_list ${fq_deps_list})
335  if(skipped_entrypoints_list)
336    message(STATUS "Skipping ${fq_target_name} as it has skipped deps.")
337    return()
338  endif()
339
340  # Create a sysroot structure
341  set(sysroot ${CMAKE_CURRENT_BINARY_DIR}/${test_name}/sysroot)
342  file(MAKE_DIRECTORY ${sysroot})
343  file(MAKE_DIRECTORY ${sysroot}/include)
344  set(sysroot_lib ${sysroot}/lib)
345  file(MAKE_DIRECTORY ${sysroot_lib})
346  get_target_property(loader_object_file ${INTEGRATION_TEST_LOADER} LOADER_OBJECT)
347  get_target_property(crti_object_file libc.loader.linux.crti LOADER_OBJECT)
348  get_target_property(crtn_object_file libc.loader.linux.crtn LOADER_OBJECT)
349  set(dummy_archive $<TARGET_PROPERTY:libc_integration_test_dummy,ARCHIVE_OUTPUT_DIRECTORY>/lib$<TARGET_PROPERTY:libc_integration_test_dummy,ARCHIVE_OUTPUT_NAME>.a)
350  if(NOT loader_object_file)
351    message(FATAL_ERROR "Missing LOADER_OBJECT property of ${INTEGRATION_TEST_LOADER}.")
352  endif()
353  set(loader_dst ${sysroot_lib}/${LIBC_TARGET_ARCHITECTURE}-linux-gnu/crt1.o)
354  add_custom_command(
355    OUTPUT ${loader_dst} ${sysroot}/lib/crti.o ${sysroot}/lib/crtn.o ${sysroot}/lib/libm.a ${sysroot}/lib/libc++.a
356    COMMAND cmake -E copy ${loader_object_file} ${loader_dst}
357    COMMAND cmake -E copy ${crti_object_file} ${sysroot}/lib
358    COMMAND cmake -E copy ${crtn_object_file} ${sysroot}/lib
359    # We copy the dummy archive as libm.a and libc++.a as the compiler drivers expect them.
360    COMMAND cmake -E copy ${dummy_archive} ${sysroot}/lib/libm.a
361    COMMAND cmake -E copy ${dummy_archive} ${sysroot}/lib/libc++.a
362    DEPENDS ${INTEGRATION_TEST_LOADER} libc.loader.linux.crti libc.loader.linux.crtn libc_integration_test_dummy
363  )
364  add_custom_target(
365    ${fq_target_name}.__copy_loader__
366    DEPENDS ${loader_dst}
367  )
368
369  add_library(
370    ${fq_libc_target_name}
371    STATIC
372    ${link_object_files}
373  )
374  set_target_properties(${fq_libc_target_name} PROPERTIES ARCHIVE_OUTPUT_NAME c)
375  set_target_properties(${fq_libc_target_name} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${sysroot_lib})
376
377  add_executable(
378    ${fq_target_name}
379    EXCLUDE_FROM_ALL
380    ${INTEGRATION_TEST_SRCS}
381    ${INTEGRATION_TEST_HDRS}
382  )
383  set_target_properties(${fq_target_name}
384      PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
385  target_include_directories(
386    ${fq_target_name}
387    PRIVATE
388      ${LIBC_SOURCE_DIR}
389      ${LIBC_BUILD_DIR}
390      ${LIBC_BUILD_DIR}/include
391  )
392  # We set a number of link options to prevent picking up system libc binaries.
393  # Also, we restrict the integration tests to fully static executables. The
394  # rtlib is set to compiler-rt to make the compiler drivers pick up the compiler
395  # runtime binaries using full paths. Otherwise, files like crtbegin.o are passed
396  # as is (and not as paths like /usr/lib/.../crtbegin.o).
397  target_link_options(${fq_target_name} PRIVATE --sysroot=${sysroot} -static -stdlib=libc++ --rtlib=compiler-rt)
398  add_dependencies(${fq_target_name}
399                   ${fq_target_name}.__copy_loader__
400                   ${fq_libc_target_name}
401                   libc.utils.IntegrationTest.test)
402
403  add_custom_command(
404    TARGET ${fq_target_name}
405    POST_BUILD
406    COMMAND ${INTEGRATION_TEST_ENV} $<TARGET_FILE:${fq_target_name}> ${INTEGRATION_TEST_ARGS}
407  )
408
409  add_dependencies(${INTEGRATION_TEST_SUITE} ${fq_target_name})
410endfunction(add_integration_test)
411