# Generated by `spin pack`. Builds campfire from C alone: a C compiler
# and make, no spinel and no spin. Parallel-safe (`make -j`).
#
# CFLAGS below has two halves. The defines and the -I paths are what the
# compiler REPORTED this program requires (`spinel --print-build`), and
# changing them changes what gets built: a threaded program compiles its
# runtime with -DSP_THREADS too, and the generated TU writes its own
# externs, so a mismatch there links and then misbehaves rather than
# failing. The -O2 is this Makefile's opinion and yours to change.
#
# CC is a default, not a decision. Cross-compiling is `make CC=<your cc>`
# and the ingredients above travel unchanged -- which is the point of
# them being ingredients rather than a command line. The compiler does
# have to be GCC-compatible: the runtime headers use __attribute__
# ((cleanup)), __COUNTER__, statement expressions and __typeof__, so
# gcc and clang (including cross builds of them) work and a strict ISO C
# compiler does not.
CC ?= cc
CFLAGS ?= -O2 -DSP_THREADS -ftls-model=initial-exec -L/opt/homebrew/lib -L/usr/local/lib -DSP_INT_OVERFLOW_MODE_RAISE -Ilib -Ilib/regexp
LIBS ?= -lm -ljemalloc -lpthread -lsqlite3 -lvips -lgobject-2.0 -lglib-2.0
# This program uses Thread, so its runtime needs pthread from $(CC)'s target.
# Asked of $(CC) itself: a cross compiler for a target without pthread must
# say so here, not forty files in.
SP_PTHREAD := $(shell printf '#include <pthread.h>\nstatic void *f(void *a){return a;}\nint main(void){pthread_t t;return pthread_create(&t,0,f,0)!=0;}\n' > .sp_pthread_probe.c && $(CC) -pthread .sp_pthread_probe.c -o .sp_pthread_probe >/dev/null 2>&1 && echo yes; rm -f .sp_pthread_probe.c .sp_pthread_probe)
ifneq ($(SP_PTHREAD),yes)
$(error campfire uses Thread (Thread/Mutex/Queue/...) and $(CC) has no pthread: it cannot be built for this target)
endif
# String#crypt is libc crypt(3): a separate library on glibc, inside
# libSystem on Darwin. The recipient's platform decides, not the packer's.
ifneq ($(shell uname -s),Darwin)
LIBS += -lcrypt
endif

RT := $(wildcard lib/*.c) $(wildcard lib/regexp/*.c)
NATIVE := native/crypt_blowfish.o native/sp_bcrypt.o native/sp_vips.o native/qrcodegen.o native/sp_rqrcode.o native/sp_json.o native/sp_stringio.o
OBJS := src/campfire.o $(RT:.c=.o) $(NATIVE)

campfire: $(OBJS)
	$(CC) $(CFLAGS) $(OBJS) $(LIBS) -o $@

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

clean:
	rm -f $(OBJS) campfire

.PHONY: clean
