diff mbox series

[v3,1/4] t/unit-tests: convert "clar-generate.awk" into a shell script

Message ID 20241115-pks-clar-build-improvements-v3-1-29672bf65ec6@pks.im (mailing list archive)
State New
Headers show
Series Build improvements for clar | expand

Commit Message

Patrick Steinhardt Nov. 15, 2024, 7:32 a.m. UTC
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>
---
 Makefile                             |  2 +-
 contrib/buildsystems/CMakeLists.txt  |  7 ++--
 t/unit-tests/clar-generate.awk       | 50 ----------------------------
 t/unit-tests/generate-clar-suites.sh | 63 ++++++++++++++++++++++++++++++++++++
 4 files changed, 69 insertions(+), 53 deletions(-)
diff mbox series

Patch

diff --git a/Makefile b/Makefile
index d06c9a8ffa7b637050c9619a367fbe61e7243a74..5232b913fd20f01a7e5f41d46178e93d52c9f534 100644
--- a/Makefile
+++ b/Makefile
@@ -3907,7 +3907,7 @@  GIT-TEST-SUITES: FORCE
 $(UNIT_TEST_DIR)/clar-decls.h: $(patsubst %,$(UNIT_TEST_DIR)/%.c,$(CLAR_TEST_SUITES)) GIT-TEST-SUITES
 	$(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
+	$(QUIET_GEN)$(SHELL_PATH) $(UNIT_TEST_DIR)/generate-clar-suites.sh $< $(UNIT_TEST_DIR)/clar.suite
 $(UNIT_TEST_DIR)/clar/clar.o: $(UNIT_TEST_DIR)/clar.suite
 $(CLAR_TEST_OBJS): $(UNIT_TEST_DIR)/clar-decls.h
 $(CLAR_TEST_OBJS): EXTRA_CPPFLAGS = -I$(UNIT_TEST_DIR)
diff --git a/contrib/buildsystems/CMakeLists.txt b/contrib/buildsystems/CMakeLists.txt
index 8974bb9fa202a0556fd9b16d105836d8cb66f543..da99dc3087a218d30e0fd1044567d7148d0d80a9 100644
--- a/contrib/buildsystems/CMakeLists.txt
+++ b/contrib/buildsystems/CMakeLists.txt
@@ -1008,8 +1008,11 @@  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")
+	COMMAND "${CMAKE_SOURCE_DIR}/t/unit-tests/generate-clar-suites.sh"
+		"${CMAKE_BINARY_DIR}/t/unit-tests/clar-decls.h"
+		"${CMAKE_BINARY_DIR}/t/unit-tests/clar.suite"
+	DEPENDS "${CMAKE_SOURCE_DIR}/t/unit-tests/generate-clar-suites.sh"
+		"${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"
diff --git a/t/unit-tests/clar-generate.awk b/t/unit-tests/clar-generate.awk
deleted file mode 100644
index ab71ce6c9fc2c3d49d826f3550a95be893114508..0000000000000000000000000000000000000000
--- a/t/unit-tests/clar-generate.awk
+++ /dev/null
@@ -1,50 +0,0 @@ 
-function add_suite(suite, initialize, cleanup, count) {
-       if (!suite) return
-       suite_count++
-       callback_count += count
-       suites = suites "    {\n"
-       suites = suites "        \"" suite "\",\n"
-       suites = suites "        " initialize ",\n"
-       suites = suites "        " cleanup ",\n"
-       suites = suites "        _clar_cb_" suite ", " count ", 1\n"
-       suites = suites "    },\n"
-}
-
-BEGIN {
-       suites = "static struct clar_suite _clar_suites[] = {\n"
-}
-
-{
-       print
-       name = $3; sub(/\(.*$/, "", name)
-       suite = name; sub(/^test_/, "", suite); sub(/__.*$/, "", suite)
-       short_name = name; sub(/^.*__/, "", short_name)
-       cb = "{ \"" short_name "\", &" name " }"
-       if (suite != prev_suite) {
-               add_suite(prev_suite, initialize, cleanup, count)
-               if (callbacks) callbacks = callbacks "};\n"
-               callbacks = callbacks "static const struct clar_func _clar_cb_" suite "[] = {\n"
-               initialize = "{ NULL, NULL }"
-               cleanup = "{ NULL, NULL }"
-               count = 0
-               prev_suite = suite
-       }
-       if (short_name == "initialize") {
-               initialize = cb
-       } else if (short_name == "cleanup") {
-               cleanup = cb
-       } else {
-               callbacks = callbacks "    " cb ",\n"
-               count++
-       }
-}
-
-END {
-       add_suite(suite, initialize, cleanup, count)
-       suites = suites "};"
-       if (callbacks) callbacks = callbacks "};"
-       print callbacks
-       print suites
-       print "static const size_t _clar_suite_count = " suite_count ";"
-       print "static const size_t _clar_callback_count = " callback_count ";"
-}
diff --git a/t/unit-tests/generate-clar-suites.sh b/t/unit-tests/generate-clar-suites.sh
new file mode 100755
index 0000000000000000000000000000000000000000..d5c712221e46a2eaa288fff5262438e5f04d6f72
--- /dev/null
+++ b/t/unit-tests/generate-clar-suites.sh
@@ -0,0 +1,63 @@ 
+#!/bin/sh
+
+if test $# -lt 2
+then
+	echo "USAGE: $0 <CLAR_DECLS_H> <OUTPUT>" 2>&1
+	exit 1
+fi
+
+CLAR_DECLS_H="$1"
+OUTPUT="$2"
+
+awk '
+	function add_suite(suite, initialize, cleanup, count) {
+		if (!suite) return
+		suite_count++
+		callback_count += count
+		suites = suites "    {\n"
+		suites = suites "        \"" suite "\",\n"
+		suites = suites "        " initialize ",\n"
+		suites = suites "        " cleanup ",\n"
+		suites = suites "        _clar_cb_" suite ", " count ", 1\n"
+		suites = suites "    },\n"
+	}
+
+	BEGIN {
+		suites = "static struct clar_suite _clar_suites[] = {\n"
+	}
+
+	{
+		print
+		name = $3; sub(/\(.*$/, "", name)
+		suite = name; sub(/^test_/, "", suite); sub(/__.*$/, "", suite)
+		short_name = name; sub(/^.*__/, "", short_name)
+		cb = "{ \"" short_name "\", &" name " }"
+		if (suite != prev_suite) {
+			add_suite(prev_suite, initialize, cleanup, count)
+			if (callbacks) callbacks = callbacks "};\n"
+			callbacks = callbacks "static const struct clar_func _clar_cb_" suite "[] = {\n"
+			initialize = "{ NULL, NULL }"
+			cleanup = "{ NULL, NULL }"
+			count = 0
+			prev_suite = suite
+		}
+		if (short_name == "initialize") {
+			initialize = cb
+		} else if (short_name == "cleanup") {
+			cleanup = cb
+		} else {
+			callbacks = callbacks "    " cb ",\n"
+			count++
+		}
+	}
+
+	END {
+		add_suite(suite, initialize, cleanup, count)
+		suites = suites "};"
+		if (callbacks) callbacks = callbacks "};"
+		print callbacks
+		print suites
+		print "static const size_t _clar_suite_count = " suite_count ";"
+		print "static const size_t _clar_callback_count = " callback_count ";"
+	}
+' "$CLAR_DECLS_H" >"$OUTPUT"