CC = gcc
CFLAGS = -Wall -Wextra -O2 -std=c99 -march=native $(shell pkg-config --cflags libcurl json-c readline)
LDLIBS = $(shell pkg-config --libs libcurl json-c readline)

# Fully static binary.
#
# The distro libcurl.a cannot be linked statically on Alpine: it pulls in
# libpsl, libbrotli, libzstd and libnghttp2, whose static archives hold LTO
# bytecode from GCC 14 that the installed GCC 15 cannot read ("bytecode
# stream ... generated with LTO version 14.0 instead of the expected 15.1"),
# so psl_*/Brotli*/ZSTD* never resolve. -fno-lto does not help; the objects
# are bytecode, not C.
#
# static-deps/libcurl.a is curl built without those four libraries, leaving
# only LTO-free static deps. Regenerate with: make static-deps
#
# c-ares is built from source by the same script, for two reasons: a minimal
# system often has no libcares.a at all (Void's c-ares-devel ships the shared
# library only), and where an archive does exist it can carry the same LTO
# bytecode as the distro libcurl.a. Building it also pins the version.
#
# Archive order matters: libcurl.a pulls in ares_* symbols, so libcares.a has to
# come after it on the link line.
CURL_STATIC ?= static-deps/libcurl.a
CARES_STATIC ?= static-deps/libcares.a
STATIC_LIBS = $(CURL_STATIC) $(CARES_STATIC) -lssl -lcrypto -lz -lidn2 -lunistring \
	-lreadline -lncursesw -ljson-c -lm -ldl -pthread

solo: main.c
	$(CC) $(CFLAGS) -o solo main.c $(LDLIBS)
	strip solo

static: main.c
	@for f in $(CURL_STATIC) $(CARES_STATIC); do \
		test -f $$f || { echo "missing $$f - run: make static-deps"; exit 1; }; \
	done
	$(CC) $(CFLAGS) -fno-lto -static -o solo-static main.c $(STATIC_LIBS)
	strip solo-static

static-deps:
	sh ./mkstaticdeps.sh

# Rebuild both archives from scratch, ignoring what is already there.
static-deps-force:
	FORCE=1 sh ./mkstaticdeps.sh

PREFIX ?= /usr/local
BINDIR = $(PREFIX)/bin
INSTALL ?= install

install: solo
	$(INSTALL) -d $(DESTDIR)$(BINDIR)
	$(INSTALL) -m 755 solo $(DESTDIR)$(BINDIR)/solo

uninstall:
	rm -f $(DESTDIR)$(BINDIR)/solo $(DESTDIR)$(BINDIR)/solo-static

clean:
	rm -f solo solo-static

.PHONY: clean static static-deps static-deps-force install uninstall
