dtob-sys 0.1.0

Raw FFI bindings to the dtob C library (encoder + decoder).
Documentation
CC      = gcc
CFLAGS  = -Wall -Wextra -Werror -pedantic -std=c11 -D_POSIX_C_SOURCE=200809L -I lib -I src
AR      = ar

SRCS    = src/trit.c src/lexer.c src/ast.c src/parser.c src/encoder.c src/encode_json.c src/encode_xml.c src/html_rules.c
OBJS    = $(SRCS:.c=.o)

.PHONY: all clean test

UNAME := $(shell uname)
ifeq ($(UNAME),Darwin)
  SHARED_LIB = libdtob.dylib
  SHARED_FLAGS = -dynamiclib
else
  SHARED_LIB = libdtob.so
  SHARED_FLAGS = -shared
endif

BIN     = bin
DTOB_BINS = $(BIN)/dtob $(BIN)/dtob-encode $(BIN)/dtob-represent

all: libdtob.a $(BIN) $(DTOB_BINS)

$(BIN):
	mkdir -p $(BIN)

$(SHARED_LIB): $(SRCS)
	$(CC) $(CFLAGS) $(SHARED_FLAGS) -fPIC -o $@ $^ -lm

libdtob.a: $(OBJS)
	$(AR) rcs $@ $^

src/%.o: src/%.c src/dtob_internal.h lib/dtob.h
	$(CC) $(CFLAGS) -c -o $@ $<

IO_SRCS = cli/io.c

$(BIN)/dtob: cli/main.c cli/memcache.c $(IO_SRCS) cli/cli.h libdtob.a
	$(CC) $(CFLAGS) -I cli -o $@ cli/main.c cli/memcache.c $(IO_SRCS) -L. -ldtob -lm

$(BIN)/dtob-encode: cli/main_encode.c cli/typed_encode.c cli/greedy.c cli/http.c $(IO_SRCS) cli/cli.h libdtob.a
	$(CC) $(CFLAGS) -I cli -o $@ cli/main_encode.c cli/typed_encode.c cli/greedy.c cli/http.c $(IO_SRCS) -L. -ldtob -lm -lcurl

$(BIN)/dtob-represent: cli/main_represent.c cli/represent.c cli/typed_encode.c $(IO_SRCS) cli/cli.h libdtob.a
	$(CC) $(CFLAGS) -I cli -o $@ cli/main_represent.c cli/represent.c cli/typed_encode.c $(IO_SRCS) -L. -ldtob -lm

# Tests
test: test_roundtrip test_raw test_types test_nesting
	@echo "=== roundtrip ===" && ./test_roundtrip
	@echo "=== raw ===" && ./test_raw
	@echo "=== types ===" && ./test_types
	@echo "=== nesting ===" && ./test_nesting
	@echo "ALL TESTS PASSED"

test_roundtrip: test/test_roundtrip.c libdtob.a
	$(CC) $(CFLAGS) -o $@ $< -L. -ldtob -lm

test_raw: test/test_raw.c libdtob.a
	$(CC) $(CFLAGS) -o $@ $< -L. -ldtob -lm

test_types: test/test_types.c libdtob.a
	$(CC) $(CFLAGS) -o $@ $< -L. -ldtob -lm

test_nesting: test/test_nesting.c libdtob.a
	$(CC) $(CFLAGS) -o $@ $< -L. -ldtob -lm

clean:
	rm -f src/*.o libdtob.a $(SHARED_LIB) test_roundtrip test_raw test_types test_nesting
	rm -rf $(BIN)