ARCH      ?= x86
CC        := gcc
ifeq ($(ARCH),arm64)
CC        := aarch64-linux-gnu-gcc
endif

CFLAGS    += -Wall -Wextra -O2 -flto=auto -ffunction-sections -fdata-sections -Iinclude -MMD -MP

LDFLAGS   += -static -flto=auto -O2 -Wl,--gc-sections -s
LDLIBS    := -lreadline -ltinfo

B         := build/$(ARCH)
SRCDIRS   := src ant_bin
SRC       := $(shell find $(SRCDIRS) -name "*.c")
OBJ       := $(patsubst %.c,$(B)/%.o,$(SRC))

all: $(B)/ant_sh

$(B)/ant_sh: $(OBJ)
	$(CC) $(LDFLAGS) $^ -o $@ $(LDLIBS)

$(B)/%.o: %.c
	@mkdir -p $(@D)
	$(CC) $(CFLAGS) -c $< -o $@

run:
	./build/x86/ant_sh

clean:
	rm -rf build

.PHONY: all clean analyze
-include $(OBJ:.o=.d)
