Skip to content

Commit 894deb7

Browse files
dschogitster
authored andcommitted
clar: add CMake support
Now that we're using `clar` as powerful test framework, we have to adjust the Visual C build (read: the CMake definition) to be able to handle that, too. Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c976368 commit 894deb7

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

contrib/buildsystems/CMakeLists.txt

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,59 @@ foreach(unit_test ${unit_test_PROGRAMS})
10041004
endif()
10051005
endforeach()
10061006

1007+
parse_makefile_for_scripts(unit_tests_SUITES "UNIT_TESTS_SUITES" "")
1008+
1009+
set(clar_decls "")
1010+
set(clar_cbs "")
1011+
set(clar_cbs_count 0)
1012+
set(clar_suites "static struct clar_suite _clar_suites[] = {\n")
1013+
list(LENGTH unit_tests_SUITES clar_suites_count)
1014+
foreach(suite ${unit_tests_SUITES})
1015+
file(STRINGS "${CMAKE_SOURCE_DIR}/t/unit-tests/${suite}.c" decls
1016+
REGEX "^void test_${suite}__[a-zA-Z_0-9][a-zA-Z_0-9]*\\(void\\)$")
1017+
1018+
list(LENGTH decls decls_count)
1019+
string(REGEX REPLACE "void (test_${suite}__([a-zA-Z_0-9]*))\\(void\\)" " { \"\\2\", &\\1 },\n" cbs ${decls})
1020+
string(JOIN "" cbs ${cbs})
1021+
list(TRANSFORM decls PREPEND "extern ")
1022+
string(JOIN ";\n" decls ${decls})
1023+
1024+
string(APPEND clar_decls "${decls};\n")
1025+
string(APPEND clar_cbs
1026+
"static const struct clar_func _clar_cb_${suite}[] = {\n"
1027+
${cbs}
1028+
"};\n")
1029+
string(APPEND clar_suites
1030+
" {\n"
1031+
" \"${suite}\",\n"
1032+
" { NULL, NULL },\n"
1033+
" { NULL, NULL },\n"
1034+
" _clar_cb_${suite}, ${decls_count}, 1\n"
1035+
" },\n")
1036+
math(EXPR clar_cbs_count "${clar_cbs_count}+${decls_count}")
1037+
endforeach()
1038+
string(APPEND clar_suites
1039+
"};\n"
1040+
"static const size_t _clar_suite_count = ${clar_suites_count};\n"
1041+
"static const size_t _clar_callback_count = ${clar_cbs_count};\n")
1042+
file(WRITE "${CMAKE_BINARY_DIR}/t/unit-tests/clar-decls.h" "${clar_decls}")
1043+
file(WRITE "${CMAKE_BINARY_DIR}/t/unit-tests/clar.suite" "${clar_decls}" "${clar_cbs}" "${clar_suites}")
1044+
1045+
list(TRANSFORM unit_tests_SUITES PREPEND "${CMAKE_SOURCE_DIR}/t/unit-tests/")
1046+
list(TRANSFORM unit_tests_SUITES APPEND ".c")
1047+
add_library(unit-tests-lib ${unit_tests_SUITES} "${CMAKE_SOURCE_DIR}/t/unit-tests/clar/clar.c")
1048+
target_include_directories(unit-tests-lib PRIVATE "${CMAKE_SOURCE_DIR}/t/unit-tests")
1049+
add_executable(unit-tests "${CMAKE_SOURCE_DIR}/t/unit-tests/unit-test.c")
1050+
target_link_libraries(unit-tests unit-tests-lib common-main)
1051+
set_target_properties(unit-tests
1052+
PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/t/unit-tests/bin)
1053+
if(MSVC)
1054+
set_target_properties(unit-tests
1055+
PROPERTIES RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}/t/unit-tests/bin)
1056+
set_target_properties(unit-tests
1057+
PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR}/t/unit-tests/bin)
1058+
endif()
1059+
10071060
#test-tool
10081061
parse_makefile_for_sources(test-tool_SOURCES "TEST_BUILTINS_OBJS")
10091062
add_library(test-lib OBJECT ${CMAKE_SOURCE_DIR}/t/unit-tests/test-lib.c)

0 commit comments

Comments
 (0)