blob: 3f6aeceed2afa19eabb608b8ee48e09a5dd79f81 [file] [log] [blame]
# Rule to add a libc unittest.
# Usage
# add_libc_unittest(
# <target name>
# SUITE <name of the suite this test belongs to>
# SRCS <list of .cpp files for the test>
# HDRS <list of .h files for the test>
# DEPENDS <list of dependencies>
# COMPILE_OPTIONS <list of special compile options for this target>
# )
function(add_libc_unittest target_name)
if(NOT LLVM_INCLUDE_TESTS)
return()
endif()
cmake_parse_arguments(
"LIBC_UNITTEST"
"" # No optional arguments
"SUITE" # Single value arguments
"SRCS;HDRS;DEPENDS;COMPILE_OPTIONS" # Multi-value arguments
${ARGN}
)
if(NOT LIBC_UNITTEST_SRCS)
message(FATAL_ERROR "'add_libc_unittest' target requires a SRCS list of .cpp "
"files.")
endif()
if(NOT LIBC_UNITTEST_DEPENDS)
message(FATAL_ERROR "'add_libc_unittest' target requires a DEPENDS list of "
"'add_entrypoint_object' targets.")
endif()
set(library_deps "")
get_fq_deps_list(fq_deps_list ${LIBC_UNITTEST_DEPENDS})
foreach(dep IN LISTS fq_deps_list)
get_target_property(dep_type ${dep} "TARGET_TYPE")
if(${dep_type} STREQUAL ${ENTRYPOINT_OBJ_TARGET_TYPE})
get_target_property(obj_files ${dep} "OBJECT_FILES_RAW")
list(APPEND library_deps ${obj_files})
elseif(${dep_type} STREQUAL ${OBJECT_LIBRARY_TARGET_TYPE})
get_target_property(obj_files ${dep} "OBJECT_FILES")
list(APPEND library_deps ${obj_files})
endif()
# TODO: Check if the dep is a normal CMake library target. If yes, then add it
# to the list of library_deps.
endforeach(dep)
list(REMOVE_DUPLICATES library_deps)
get_fq_target_name(${target_name} fq_target_name)
add_executable(
${fq_target_name}
EXCLUDE_FROM_ALL
${LIBC_UNITTEST_SRCS}
${LIBC_UNITTEST_HDRS}
)
target_include_directories(
${fq_target_name}
PRIVATE
${LIBC_SOURCE_DIR}
${LIBC_BUILD_DIR}
${LIBC_BUILD_DIR}/include
)
if(LIBC_UNITTEST_COMPILE_OPTIONS)
target_compile_options(
${target_name}
PRIVATE ${LIBC_UNITTEST_COMPILE_OPTIONS}
)
endif()
if(library_deps)
target_link_libraries(${fq_target_name} PRIVATE ${library_deps})
endif()
set_target_properties(${fq_target_name}
PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
add_dependencies(
${fq_target_name}
${fq_deps_list}
)
target_link_libraries(${fq_target_name} PRIVATE LibcUnitTest libc_test_utils)
add_custom_command(
TARGET ${fq_target_name}
POST_BUILD
COMMAND $<TARGET_FILE:${fq_target_name}>
)
if(LIBC_UNITTEST_SUITE)
add_dependencies(
${LIBC_UNITTEST_SUITE}
${fq_target_name}
)
endif()
endfunction(add_libc_unittest)
function(add_libc_testsuite suite_name)
add_custom_target(${suite_name})
add_dependencies(check-libc ${suite_name})
endfunction(add_libc_testsuite)
# Rule to add a fuzzer test.
# Usage
# add_libc_fuzzer(
# <target name>
# SRCS <list of .cpp files for the test>
# HDRS <list of .h files for the test>
# DEPENDS <list of dependencies>
# )
function(add_libc_fuzzer target_name)
cmake_parse_arguments(
"LIBC_FUZZER"
"" # No optional arguments
"" # Single value arguments
"SRCS;HDRS;DEPENDS" # Multi-value arguments
${ARGN}
)
if(NOT LIBC_FUZZER_SRCS)
message(FATAL_ERROR "'add_libc_fuzzer' target requires a SRCS list of .cpp "
"files.")
endif()
if(NOT LIBC_FUZZER_DEPENDS)
message(FATAL_ERROR "'add_libc_fuzzer' target requires a DEPENDS list of "
"'add_entrypoint_object' targets.")
endif()
get_fq_deps_list(fq_deps_list ${LIBC_FUZZER_DEPENDS})
set(library_deps "")
foreach(dep IN LISTS fq_deps_list)
get_target_property(dep_type ${dep} "TARGET_TYPE")
if (dep_type)
string(COMPARE EQUAL ${dep_type} ${ENTRYPOINT_OBJ_TARGET_TYPE} dep_is_entrypoint)
if(dep_is_entrypoint)
get_target_property(obj_file ${dep} "OBJECT_FILES_RAW")
list(APPEND library_deps ${obj_file})
continue()
endif()
endif()
# TODO: Check if the dep is a normal CMake library target. If yes, then add it
# to the list of library_deps.
endforeach(dep)
get_fq_target_name(${target_name} fq_target_name)
add_executable(
${fq_target_name}
EXCLUDE_FROM_ALL
${LIBC_FUZZER_SRCS}
${LIBC_FUZZER_HDRS}
)
target_include_directories(
${fq_target_name}
PRIVATE
${LIBC_SOURCE_DIR}
${LIBC_BUILD_DIR}
${LIBC_BUILD_DIR}/include
)
if(library_deps)
target_link_libraries(${fq_target_name} PRIVATE ${library_deps})
endif()
set_target_properties(${fq_target_name}
PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
add_dependencies(
${fq_target_name}
${fq_deps_list}
)
add_dependencies(libc-fuzzer ${fq_target_name})
endfunction(add_libc_fuzzer)