t/unit-tests: convert "clar-generate.awk" into a shell script

Convert "clar-generate.awk" into a shell script that invokes awk(1). This allows us to avoid the shell redirect in the build system, which may otherwise be a problem with build systems on platforms that use a different shell. While at it, wrap the overly long lines in the CMake build instructions. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Nov 15, 2024 at 08:32 UTC 9a91ab940012601247d6ef46e10e0c136b86ba69
4 files changed +69 -53
Makefile
+1 -1
@@ -3907,7 +3907,7 @@ GIT-TEST-SUITES: FORCE
3907 $(UNIT_TEST_DIR)/clar-decls.h: $(patsubst %,$(UNIT_TEST_DIR)/%.c,$(CLAR_TEST_SUITES)) GIT-TEST-SUITES
3908 $(QUIET_GEN)$(SHELL_PATH) $(UNIT_TEST_DIR)/generate-clar-decls.sh "$@" $(filter %.c,$^)
3909 $(UNIT_TEST_DIR)/clar.suite: $(UNIT_TEST_DIR)/clar-decls.h
3910 - $(QUIET_GEN)awk -f $(UNIT_TEST_DIR)/clar-generate.awk $< >$(UNIT_TEST_DIR)/clar.suite
3910 + $(QUIET_GEN)$(SHELL_PATH) $(UNIT_TEST_DIR)/generate-clar-suites.sh $< $(UNIT_TEST_DIR)/clar.suite
3911 $(UNIT_TEST_DIR)/clar/clar.o: $(UNIT_TEST_DIR)/clar.suite
3912 $(CLAR_TEST_OBJS): $(UNIT_TEST_DIR)/clar-decls.h
3913 $(CLAR_TEST_OBJS): EXTRA_CPPFLAGS = -I$(UNIT_TEST_DIR)
contrib/buildsystems/CMakeLists.txt
+5 -2
@@ -1008,8 +1008,11 @@ add_custom_command(OUTPUT "${CMAKE_BINARY_DIR}/t/unit-tests/clar-decls.h"
1008 COMMAND ${CMAKE_SOURCE_DIR}/t/unit-tests/generate-clar-decls.sh "${CMAKE_BINARY_DIR}/t/unit-tests/clar-decls.h" ${clar_test_SUITES}
1009 DEPENDS ${CMAKE_SOURCE_DIR}/t/unit-tests/generate-clar-decls.sh ${clar_test_SUITES})
1010 add_custom_command(OUTPUT "${CMAKE_BINARY_DIR}/t/unit-tests/clar.suite"
1011 - 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"
1012 - DEPENDS "${CMAKE_SOURCE_DIR}/t/unit-tests/clar-generate.awk" "${CMAKE_BINARY_DIR}/t/unit-tests/clar-decls.h")
1011 + COMMAND "${CMAKE_SOURCE_DIR}/t/unit-tests/generate-clar-suites.sh"
1012 + "${CMAKE_BINARY_DIR}/t/unit-tests/clar-decls.h"
1013 + "${CMAKE_BINARY_DIR}/t/unit-tests/clar.suite"
1014 + DEPENDS "${CMAKE_SOURCE_DIR}/t/unit-tests/generate-clar-suites.sh"
1015 + "${CMAKE_BINARY_DIR}/t/unit-tests/clar-decls.h")
1016
1017 add_library(unit-tests-lib ${clar_test_SUITES}
1018 "${CMAKE_SOURCE_DIR}/t/unit-tests/clar/clar.c"
t/unit-tests/clar-generate.awk deleted
-50
@@ -1,50 +0,0 @@
1 -function add_suite(suite, initialize, cleanup, count) {
2 - if (!suite) return
3 - suite_count++
4 - callback_count += count
5 - suites = suites " {\n"
6 - suites = suites " \"" suite "\",\n"
7 - suites = suites " " initialize ",\n"
8 - suites = suites " " cleanup ",\n"
9 - suites = suites " _clar_cb_" suite ", " count ", 1\n"
10 - suites = suites " },\n"
11 -}
12 -
13 -BEGIN {
14 - suites = "static struct clar_suite _clar_suites[] = {\n"
15 -}
16 -
17 -{
18 - print
19 - name = $3; sub(/\(.*$/, "", name)
20 - suite = name; sub(/^test_/, "", suite); sub(/__.*$/, "", suite)
21 - short_name = name; sub(/^.*__/, "", short_name)
22 - cb = "{ \"" short_name "\", &" name " }"
23 - if (suite != prev_suite) {
24 - add_suite(prev_suite, initialize, cleanup, count)
25 - if (callbacks) callbacks = callbacks "};\n"
26 - callbacks = callbacks "static const struct clar_func _clar_cb_" suite "[] = {\n"
27 - initialize = "{ NULL, NULL }"
28 - cleanup = "{ NULL, NULL }"
29 - count = 0
30 - prev_suite = suite
31 - }
32 - if (short_name == "initialize") {
33 - initialize = cb
34 - } else if (short_name == "cleanup") {
35 - cleanup = cb
36 - } else {
37 - callbacks = callbacks " " cb ",\n"
38 - count++
39 - }
40 -}
41 -
42 -END {
43 - add_suite(suite, initialize, cleanup, count)
44 - suites = suites "};"
45 - if (callbacks) callbacks = callbacks "};"
46 - print callbacks
47 - print suites
48 - print "static const size_t _clar_suite_count = " suite_count ";"
49 - print "static const size_t _clar_callback_count = " callback_count ";"
50 -}
t/unit-tests/generate-clar-suites.sh new
+63
@@ -0,0 +1,63 @@
1 +#!/bin/sh
2 +
3 +if test $# -lt 2
4 +then
5 + echo "USAGE: $0 <CLAR_DECLS_H> <OUTPUT>" 2>&1
6 + exit 1
7 +fi
8 +
9 +CLAR_DECLS_H="$1"
10 +OUTPUT="$2"
11 +
12 +awk '
13 + function add_suite(suite, initialize, cleanup, count) {
14 + if (!suite) return
15 + suite_count++
16 + callback_count += count
17 + suites = suites " {\n"
18 + suites = suites " \"" suite "\",\n"
19 + suites = suites " " initialize ",\n"
20 + suites = suites " " cleanup ",\n"
21 + suites = suites " _clar_cb_" suite ", " count ", 1\n"
22 + suites = suites " },\n"
23 + }
24 +
25 + BEGIN {
26 + suites = "static struct clar_suite _clar_suites[] = {\n"
27 + }
28 +
29 + {
30 + print
31 + name = $3; sub(/\(.*$/, "", name)
32 + suite = name; sub(/^test_/, "", suite); sub(/__.*$/, "", suite)
33 + short_name = name; sub(/^.*__/, "", short_name)
34 + cb = "{ \"" short_name "\", &" name " }"
35 + if (suite != prev_suite) {
36 + add_suite(prev_suite, initialize, cleanup, count)
37 + if (callbacks) callbacks = callbacks "};\n"
38 + callbacks = callbacks "static const struct clar_func _clar_cb_" suite "[] = {\n"
39 + initialize = "{ NULL, NULL }"
40 + cleanup = "{ NULL, NULL }"
41 + count = 0
42 + prev_suite = suite
43 + }
44 + if (short_name == "initialize") {
45 + initialize = cb
46 + } else if (short_name == "cleanup") {
47 + cleanup = cb
48 + } else {
49 + callbacks = callbacks " " cb ",\n"
50 + count++
51 + }
52 + }
53 +
54 + END {
55 + add_suite(suite, initialize, cleanup, count)
56 + suites = suites "};"
57 + if (callbacks) callbacks = callbacks "};"
58 + print callbacks
59 + print suites
60 + print "static const size_t _clar_suite_count = " suite_count ";"
61 + print "static const size_t _clar_callback_count = " callback_count ";"
62 + }
63 +' "$CLAR_DECLS_H" >"$OUTPUT"