From 9f018e5c850a1d4572f5e601ccc16c4231e8ccbf Mon Sep 17 00:00:00 2001 From: Adam Saponara Date: Thu, 13 Jul 2023 04:23:41 -0400 Subject: [PATCH] parse test macros from `gcc -E -dM` output instead of the raw header previously we had no way intelligently parsing out conditional defines such as: ```c ``` we would parse out TB_XYZ twice and overwrite the first value. this was breaking tests as we now, for example, have TB_BOLD defined conditionally. we are now parsing the output of `gcc -E -dM` instead which give us a simple list of macro key-vals after the preprocessor has executed. --- .gitignore | 1 + Makefile | 10 +++++++--- tests/test_64bit/test.php | 2 +- tests/test_ffi.php | 26 ++++++++++++-------------- 4 files changed, 21 insertions(+), 18 deletions(-) diff --git a/.gitignore b/.gitignore index 9b734a6..e635ccf 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ termbox2.o libtermbox2.a libtermbox2.so* termbox2.ffi.h +termbox2.ffi.macro termbox2.h.lib demo/keyboard tests/**/observed.ansi diff --git a/Makefile b/Makefile index 4311877..5ce7b4b 100644 --- a/Makefile +++ b/Makefile @@ -5,6 +5,7 @@ termbox_demos:=$(patsubst demo/%.c,demo/%,$(wildcard demo/*.c)) termbox_h:=termbox2.h termbox_h_lib:=termbox2.h.lib termbox_ffi_h:=termbox2.ffi.h +termbox_ffi_macro:=termbox2.ffi.macro termbox_o:=termbox2.o termbox_so_version_abi:=2 termbox_so_version_minor_patch:=0.0 @@ -44,6 +45,9 @@ $(termbox_a): $(termbox_o) $(termbox_ffi_h): $(termbox_h) awk '/__ffi_start/{p=1} p==1 || /__TERMBOX_H/{print}' $^ | $(CC) -DTB_LIB_OPTS $(termbox_cflags) -P -E - >$@ +$(termbox_ffi_macro): $(termbox_h) + awk '/__ffi_start/{p=1} p==1 || /__TERMBOX_H/{print}' $^ | $(CC) -DTB_LIB_OPTS $(termbox_cflags) -P -E -dM - >$@ + $(termbox_h_lib): $(termbox_h) sed 's|0 // __tb_lib_opts|1 // __tb_lib_opts|' $(termbox_h) >$@ @@ -54,10 +58,10 @@ terminfo: format: clang-format -i termbox2.h -test: $(termbox_so) $(termbox_ffi_h) +test: $(termbox_so) $(termbox_ffi_h) $(termbox_ffi_macro) docker build -f tests/Dockerfile --build-arg=cflags="$(termbox_cflags)" . -test_local: $(termbox_so) $(termbox_ffi_h) +test_local: $(termbox_so) $(termbox_ffi_h) $(termbox_ffi_macro) ./tests/run.sh install: @@ -92,6 +96,6 @@ install_so: $(termbox_so_x_y_z) ln -sf $(termbox_so_x_y_z) $(DESTDIR)$(prefix)/lib/$(termbox_so) clean: - rm -f $(termbox_demos) $(termbox_o) $(termbox_a) $(termbox_so) $(termbox_so_x) $(termbox_so_x_y_z) $(termbox_ffi_h) $(termbox_h_lib) tests/**/observed.ansi + rm -f $(termbox_demos) $(termbox_o) $(termbox_a) $(termbox_so) $(termbox_so_x) $(termbox_so_x_y_z) $(termbox_ffi_h) $(termbox_ffi_macro) $(termbox_h_lib) tests/**/observed.ansi .PHONY: all lib terminfo format test test_local install install_lib install_h install_h_lib install_a install_so clean diff --git a/tests/test_64bit/test.php b/tests/test_64bit/test.php index 1a7f997..141c537 100755 --- a/tests/test_64bit/test.php +++ b/tests/test_64bit/test.php @@ -1,7 +1,7 @@ ffi->tb_attr_width() >= 64) { +if ($test->ffi->tb_attr_width() !== 64) { // This will only work with 64-bit attrs $test->skip(); } diff --git a/tests/test_ffi.php b/tests/test_ffi.php index fb14bbd..bd0dfac 100644 --- a/tests/test_ffi.php +++ b/tests/test_ffi.php @@ -6,40 +6,38 @@ return (function() { // preprocessor directives, so we feed it a `gcc -E` version of // termbox2.h (termbox2.ffi.h, created by the Makefile). On the other // hand, it's useful to have `#define` constants for tests, so we parse - // those out from the raw `termbox2.h`. + // those out from a `gcc -E -dM` version of termbox2.h + // (termbox2.ffi.macro, also created by the Makefile). $repo_dir = dirname(__DIR__); - $termbox_h = "$repo_dir/termbox2.h"; $termbox_ffi_h = "$repo_dir/termbox2.ffi.h"; + $termbox_ffi_macro = "$repo_dir/termbox2.ffi.macro"; $libtermbox_so = "$repo_dir/libtermbox2.so"; - $termbox_h_data = file_get_contents($termbox_h); - - // Look at only the content in between `__TERMBOX_H` - $matches = []; - preg_match( - '@#define __TERMBOX_H\n(.*?)#endif /\* __TERMBOX_H \*/@sm', - $termbox_h_data, - $matches - ); - $termbox_h_data = $matches[1] ?? ''; // Extract #define values $defines = []; $matches = []; - preg_match_all('/^#define\s+(TB_\S+)\s+(.+)$/m', $termbox_h_data, $matches, PREG_SET_ORDER); + $termbox_ffi_macro_data = file_get_contents($termbox_ffi_macro); + preg_match_all('/^#define\s+(TB_\S+)\s+(.+)$/m', $termbox_ffi_macro_data, $matches, PREG_SET_ORDER); foreach ($matches as $match) { $define_name = $match[1]; $define_value = $match[2]; // Remove comments $define_value = trim(preg_replace('|/\*.*$|', '', $define_value)); + $define_value = trim(preg_replace('|//.*$|', '', $define_value)); - // Special case for evaluating `(0xFFFF - ...)` values $match2 = []; if (preg_match('/^\(0xffff - (\d+)\)$/i', $define_value, $match2)) { + // Special case for evaluating `(0xFFFF - ...)` values $define_value = 0xffff - (int)$match2[1]; } else if (substr($define_value, 0, 2) === '0x') { + // Special case for evaluating hex $define_value = hexdec(substr($define_value, 2)); } + if (isset($defines[$define_name])) { + // Warn if overwriting + fwrite(STDERR, "Overwriting macro $define_name\n"); + } $defines[$define_name] = (int)$define_value; } -- 2.39.5