CC      ?= cc
CFLAGS  ?= -std=c99 -Wall -Wextra -Wpedantic -O2 -g -D_POSIX_C_SOURCE=200809L
INCLUDE := -Iinclude -Isrc

SRCS    := src/vtree.c src/manager.c src/decomp.c src/apply.c \
           src/compile.c src/model_count.c src/vtree_ops.c \
           src/rebuild.c src/minimize.c src/canonical.c
OBJS    := $(SRCS:.c=.o)
LIB     := libmvsdd.a

TOOLS   := tools/mvsdd-compile

TESTS   := tests/test_smoke tests/test_apply tests/test_minimize
TEST_BINS := $(TESTS)

.PHONY: all test clean tools

all: $(LIB) tools

tools: $(TOOLS)

$(LIB): $(OBJS)
	ar rcs $@ $^

%.o: %.c
	$(CC) $(CFLAGS) $(INCLUDE) -c $< -o $@

tests/%: tests/%.c $(LIB)
	$(CC) $(CFLAGS) $(INCLUDE) $< $(LIB) -o $@

tools/%: tools/%.c $(LIB)
	$(CC) $(CFLAGS) $(INCLUDE) $< $(LIB) -o $@

test: $(TEST_BINS)
	@for t in $(TEST_BINS); do \
		echo "==> $$t"; \
		./$$t || exit 1; \
	done

clean:
	rm -f $(OBJS) $(LIB) $(TEST_BINS) $(TOOLS)
