Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ prefix?=/usr/local
termbox_cflags:=-std=c99 -Wall -Wextra -pedantic -Wno-unused-result -g -O0 -D_XOPEN_SOURCE -D_DEFAULT_SOURCE $(CFLAGS)
termbox_demos:=demo/keyboard
termbox_h:=termbox2.h
termbox_h_in:=termbox2.h.in
termbox_h_lib:=termbox2.h.lib
termbox_ffi_h:=termbox2.ffi.h
termbox_ffi_macro:=termbox2.ffi.macro
Expand All @@ -24,7 +25,10 @@ ifeq ($(shell $(CC) -dumpmachine | grep -q apple && echo 1), 1)
termbox_ld_soname:=install_name
endif

all: $(termbox_demos) $(termbox_so) $(termbox_so_x) $(termbox_a)
all: $(termbox_h) $(termbox_demos) $(termbox_so) $(termbox_so_x) $(termbox_a)

$(termbox_h): $(termbox_h_in) tb_api.h tb_platform.h termbox2.c tb_posix.c tb_wasm.c assemble.awk
awk -f assemble.awk $(termbox_h_in) > $@

$(termbox_demos): %: %.c
$(CC) -DTB_IMPL -DTB_LIB_OPTS -I. $(termbox_cflags) $^ -o $@
Expand Down Expand Up @@ -54,8 +58,8 @@ $(termbox_h_lib): $(termbox_h)
sed 's|0 // __tb_lib_opts|1 // __tb_lib_opts|' $(termbox_h) >$@

terminfo:
awk -vg=0 'g==0{print} /BEGIN codegen h/{g=1; system("./codegen.sh h")} /END codegen h/{g=0; print} g==1{next}' termbox2.h >termbox2.h.tmp && mv -vf termbox2.h.tmp termbox2.h
awk -vg=0 'g==0{print} /BEGIN codegen c/{g=1; system("./codegen.sh c")} /END codegen c/{g=0; print} g==1{next}' termbox2.h >termbox2.h.tmp && mv -vf termbox2.h.tmp termbox2.h
awk -vg=0 'g==0{print} /BEGIN codegen h/{g=1; system("./codegen.sh h")} /END codegen h/{g=0; print} g==1{next}' tb_api.h >tb_api.h.tmp && mv -vf tb_api.h.tmp tb_api.h
awk -vg=0 'g==0{print} /BEGIN codegen c/{g=1; system("./codegen.sh c")} /END codegen c/{g=0; print} g==1{next}' tb_api.h >tb_api.h.tmp && mv -vf tb_api.h.tmp tb_api.h

format:
clang-format -i termbox2.h
Expand Down Expand Up @@ -98,6 +102,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_ffi_macro) $(termbox_h_lib) tests/**/observed.ansi
rm -f $(termbox_h) $(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
122 changes: 122 additions & 0 deletions assemble.awk
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
# assemble.awk - Assembles termbox2.h from template and source files
#
# Usage: awk -f assemble.awk termbox2.h.in > termbox2.h
#
# Processes the template by:
# 1. Inlining #include "foo.h" (stripping include guards)
# 2. Injecting .c file contents at BEGIN/END markers
# (stripping #include "..." and #define TB_IMPL from .c files)

function extract_include(line, i, j) {
i = index(line, "\"")
if (i == 0) return ""
j = index(substr(line, i + 1), "\"")
if (j == 0) return ""
return substr(line, i + 1, j - 1)
}

# Inject a .c file, stripping the preamble (everything up to and including
# the last #include "..." line). System #include <...> lines after the
# preamble are kept.
function strip_inject(file, n, i, last_local, line, lines) {
n = 0
while ((getline line < file) > 0) {
lines[++n] = line
}
close(file)

# Find the last #include "..." line
last_local = 0
for (i = 1; i <= n; i++) {
if (lines[i] ~ /^#include "/) last_local = i
}

# Print everything after the preamble
for (i = last_local + 1; i <= n; i++) {
print lines[i]
}
}

# Read a header file, strip its include guard, inline nested #include "..."
# Uses a unique prefix per call depth to avoid array clobbering
function inline_header(file, n, i, start, end, last, line, f, lines) {
n = 0
while ((getline line < file) > 0) {
n++
lines[file, n] = line
}
close(file)

start = 1
end = n

# Detect and strip include guard
if (n >= 2 && lines[file, 1] ~ /^#ifndef [A-Z_]+_H/ && lines[file, 2] ~ /^#define [A-Z_]+_H/) {
start = 3
for (last = n; last >= 1; last--) {
if (lines[file, last] ~ /^#endif/) {
end = last - 1
break
}
}
}

for (i = start; i <= end; i++) {
line = lines[file, i]
if (line ~ /^#include "/) {
f = extract_include(line)
if (f != "") {
inline_header(f)
continue
}
}
print line
}
}

{
# Inline headers at BEGIN/END header markers
if ($0 ~ /\/\* BEGIN header .+ \*\//) {
print
f = $0
sub(/.*BEGIN header /, "", f)
sub(/ \*\/.*/, "", f)
inline_header(f)
next
}
if ($0 ~ /\/\* END header .+ \*\//) {
print
next
}

# Inject .c files at markers
if ($0 ~ /\/\* BEGIN core \*\//) {
print
strip_inject("termbox2.c")
next
}
if ($0 ~ /\/\* END core \*\//) {
print
next
}
if ($0 ~ /\/\* BEGIN platform posix \*\//) {
print
strip_inject("tb_posix.c")
next
}
if ($0 ~ /\/\* END platform posix \*\//) {
print
next
}
if ($0 ~ /\/\* BEGIN platform wasm \*\//) {
print
strip_inject("tb_wasm.c")
next
}
if ($0 ~ /\/\* END platform wasm \*\//) {
print
next
}

print
}
Loading
Loading