# qfwpack -- portable Makefile (Linux / macOS / MinGW; native + cross)
#
#   make                       native build            -> build/qfwpack
#   make STATIC=1              fully static binary
#   make CROSS=aarch64-linux-gnu- STATIC=1             cross for arm64
#   make CROSS=arm-linux-gnueabihf- STATIC=1           cross for armv7 (armhf)
#   make clean
#
# The tool has NO external dependencies: zlib is replaced by the vendored miniz
# and LZMA by the vendored LZMA SDK decoder, both ISO C. This builds cleanly on
# glibc 2.19 (Ubuntu 14.04 / QTS 5) through glibc 2.39 (Ubuntu 24.04 / QTS 6).

CROSS   ?=
CC      := $(CROSS)gcc
# gnu99 (not c99): glibc hides S_IFMT/fseeko/strcasecmp under strict ANSI.
CFLAGS  ?= -O2 -std=gnu99 -Wall -Wextra -Wno-unused-parameter -Wno-format-truncation
CPPFLAGS := -D_FILE_OFFSET_BITS=64 -Ithird_party/miniz -Ithird_party/lzma
LDFLAGS ?=
ifeq ($(STATIC),1)
  LDFLAGS += -static
endif

OUT := build
BIN := $(OUT)/qfwpack

SRC := \
  src/main.c src/qfw.c src/pc1.c src/cksum.c src/util.c \
  src/tario.c src/gz.c src/lzma_dec.c src/sniff.c src/unpack.c \
  third_party/miniz/miniz.c third_party/lzma/LzmaDec.c

.PHONY: all clean
all: $(BIN)

$(BIN): $(SRC) | $(OUT)
	$(CC) $(CFLAGS) $(CPPFLAGS) $(SRC) -o $(BIN) $(LDFLAGS)
	@echo "[OK] $(BIN)  ($$($(CC) -dumpmachine))"

$(OUT):
	@mkdir -p $(OUT)

clean:
	rm -rf $(OUT)
