Skip to content

Commit

Permalink
Merge branch 'ps/upgrade-clar'
Browse files Browse the repository at this point in the history
Buildfix and upgrade of Clar to a newer version.

* ps/upgrade-clar:
  cmake: set up proper dependencies for generated clar headers
  cmake: fix compilation of clar-based unit tests
  Makefile: extract script to generate clar declarations
  Makefile: adjust sed command for generating "clar-decls.h"
  t/unit-tests: update clar to 206accb
  • Loading branch information
gitster committed Nov 8, 2024
2 parents 31fe139 + 30bf9f0 commit 1ee7dbd
Show file tree
Hide file tree
Showing 14 changed files with 219 additions and 166 deletions.
4 changes: 1 addition & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3905,9 +3905,7 @@ GIT-TEST-SUITES: FORCE
fi

$(UNIT_TEST_DIR)/clar-decls.h: $(patsubst %,$(UNIT_TEST_DIR)/%.c,$(CLAR_TEST_SUITES)) GIT-TEST-SUITES
$(QUIET_GEN)for suite in $(CLAR_TEST_SUITES); do \
sed -ne "s/^\(void test_$${suite}__[a-zA-Z_0-9][a-zA-Z_0-9]*(void)$$\)/extern \1;/p" $(UNIT_TEST_DIR)/$$suite.c; \
done >$@
$(QUIET_GEN)$(SHELL_PATH) $(UNIT_TEST_DIR)/generate-clar-decls.sh "$@" $(filter %.c,$^)
$(UNIT_TEST_DIR)/clar.suite: $(UNIT_TEST_DIR)/clar-decls.h
$(QUIET_GEN)awk -f $(UNIT_TEST_DIR)/clar-generate.awk $< >$(UNIT_TEST_DIR)/clar.suite
$(UNIT_TEST_DIR)/clar/clar.o: $(UNIT_TEST_DIR)/clar.suite
Expand Down
52 changes: 13 additions & 39 deletions contrib/buildsystems/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1002,47 +1002,21 @@ foreach(unit_test ${unit_test_PROGRAMS})
endforeach()

parse_makefile_for_scripts(clar_test_SUITES "CLAR_TEST_SUITES" "")

set(clar_decls "")
set(clar_cbs "")
set(clar_cbs_count 0)
set(clar_suites "static struct clar_suite _clar_suites[] = {\n")
list(LENGTH clar_test_SUITES clar_suites_count)
foreach(suite ${clar_test_SUITES})
file(STRINGS "${CMAKE_SOURCE_DIR}/t/unit-tests/${suite}.c" decls
REGEX "^void test_${suite}__[a-zA-Z_0-9][a-zA-Z_0-9]*\\(void\\)$")

list(LENGTH decls decls_count)
string(REGEX REPLACE "void (test_${suite}__([a-zA-Z_0-9]*))\\(void\\)" " { \"\\2\", &\\1 },\n" cbs ${decls})
string(JOIN "" cbs ${cbs})
list(TRANSFORM decls PREPEND "extern ")
string(JOIN ";\n" decls ${decls})

string(APPEND clar_decls "${decls};\n")
string(APPEND clar_cbs
"static const struct clar_func _clar_cb_${suite}[] = {\n"
${cbs}
"};\n")
string(APPEND clar_suites
" {\n"
" \"${suite}\",\n"
" { NULL, NULL },\n"
" { NULL, NULL },\n"
" _clar_cb_${suite}, ${decls_count}, 1\n"
" },\n")
math(EXPR clar_cbs_count "${clar_cbs_count}+${decls_count}")
endforeach()
string(APPEND clar_suites
"};\n"
"static const size_t _clar_suite_count = ${clar_suites_count};\n"
"static const size_t _clar_callback_count = ${clar_cbs_count};\n")
file(WRITE "${CMAKE_BINARY_DIR}/t/unit-tests/clar-decls.h" "${clar_decls}")
file(WRITE "${CMAKE_BINARY_DIR}/t/unit-tests/clar.suite" "${clar_decls}" "${clar_cbs}" "${clar_suites}")

list(TRANSFORM clar_test_SUITES PREPEND "${CMAKE_SOURCE_DIR}/t/unit-tests/")
list(TRANSFORM clar_test_SUITES APPEND ".c")
add_library(unit-tests-lib ${clar_test_SUITES} "${CMAKE_SOURCE_DIR}/t/unit-tests/clar/clar.c")
target_include_directories(unit-tests-lib PRIVATE "${CMAKE_SOURCE_DIR}/t/unit-tests")
add_custom_command(OUTPUT "${CMAKE_BINARY_DIR}/t/unit-tests/clar-decls.h"
COMMAND ${CMAKE_SOURCE_DIR}/t/unit-tests/generate-clar-decls.sh "${CMAKE_BINARY_DIR}/t/unit-tests/clar-decls.h" ${clar_test_SUITES}
DEPENDS ${CMAKE_SOURCE_DIR}/t/unit-tests/generate-clar-decls.sh ${clar_test_SUITES})
add_custom_command(OUTPUT "${CMAKE_BINARY_DIR}/t/unit-tests/clar.suite"
COMMAND awk -f "${CMAKE_SOURCE_DIR}/t/unit-tests/clar-generate.awk" "${CMAKE_BINARY_DIR}/t/unit-tests/clar-decls.h" > "${CMAKE_BINARY_DIR}/t/unit-tests/clar.suite"
DEPENDS "${CMAKE_SOURCE_DIR}/t/unit-tests/clar-generate.awk" "${CMAKE_BINARY_DIR}/t/unit-tests/clar-decls.h")

add_library(unit-tests-lib ${clar_test_SUITES}
"${CMAKE_SOURCE_DIR}/t/unit-tests/clar/clar.c"
"${CMAKE_BINARY_DIR}/t/unit-tests/clar-decls.h"
"${CMAKE_BINARY_DIR}/t/unit-tests/clar.suite"
)
target_include_directories(unit-tests-lib PUBLIC "${CMAKE_BINARY_DIR}/t/unit-tests")
add_executable(unit-tests "${CMAKE_SOURCE_DIR}/t/unit-tests/unit-test.c")
target_link_libraries(unit-tests unit-tests-lib common-main)
set_target_properties(unit-tests
Expand Down
13 changes: 13 additions & 0 deletions t/unit-tests/clar/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
root = true

[*]
charset = utf-8
insert_final_newline = true

[*.{c,h}]
indent_style = tab
tab_width = 8

[CMakeLists.txt]
indent_style = tab
tab_width = 8
20 changes: 16 additions & 4 deletions t/unit-tests/clar/.github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,26 @@ jobs:
build:
strategy:
matrix:
os: [ ubuntu-latest, macos-latest ]
platform:
- os: ubuntu-latest
generator: Unix Makefiles
- os: macos-latest
generator: Unix Makefiles
- os: windows-latest
generator: Visual Studio 17 2022
- os: windows-latest
generator: MSYS Makefiles
- os: windows-latest
generator: MinGW Makefiles

runs-on: ${{ matrix.os }}
runs-on: ${{ matrix.platform.os }}

steps:
- name: Check out
uses: actions/checkout@v2
- name: Build
run: |
cd test
make
mkdir build
cd build
cmake .. -G "${{matrix.platform.generator}}"
cmake --build .
1 change: 1 addition & 0 deletions t/unit-tests/clar/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build/
28 changes: 28 additions & 0 deletions t/unit-tests/clar/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
cmake_minimum_required(VERSION 3.16..3.29)

project(clar LANGUAGES C)

option(BUILD_TESTS "Build test executable" ON)

add_library(clar INTERFACE)
target_sources(clar INTERFACE
clar.c
clar.h
clar/fixtures.h
clar/fs.h
clar/print.h
clar/sandbox.h
clar/summary.h
)
set_target_properties(clar PROPERTIES
C_STANDARD 90
C_STANDARD_REQUIRED ON
C_EXTENSIONS OFF
)

if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
include(CTest)
if(BUILD_TESTING)
add_subdirectory(test)
endif()
endif()
Loading

0 comments on commit 1ee7dbd

Please sign in to comment.