# Makefile for shared libc static library on MontaukOS
# Copyright (c) 2025-2026 Daniel Hammer

MAKEFLAGS += -rR
.SUFFIXES:

# ---- Toolchain ----

TOOLCHAIN_PREFIX := $(shell cd ../../.. && pwd)/toolchain/local/bin/x86_64-montauk-
ifneq ($(wildcard $(TOOLCHAIN_PREFIX)gcc),)
    CC := $(TOOLCHAIN_PREFIX)gcc
    AR := $(TOOLCHAIN_PREFIX)ar
    LD := $(TOOLCHAIN_PREFIX)ld
else
    CC := gcc
    AR := ar
    LD := ld
endif

# ---- Paths ----

LIBC_INC := ../../include/libc
OBJDIR   := obj
CRTDIR   := crt
ASM_OBJS := $(OBJDIR)/setjmp.o

# ---- Compiler flags ----

CFLAGS := \
    -std=gnu11 \
    -g -O2 -pipe \
    -Wall \
    -Wno-unused-parameter \
    -nostdinc \
    -ffreestanding \
    -fno-stack-protector \
    -fno-stack-check \
    -fno-PIC \
    -ffunction-sections \
    -fdata-sections \
    -m64 \
    -march=x86-64 \
    -msse \
    -msse2 \
    -mno-red-zone \
    -mcmodel=small \
    -isystem $(LIBC_INC) \
    -isystem $(shell $(CC) -print-file-name=include)

CRT_CFLAGS := \
    -std=gnu11 \
    -O2 \
    -nostdinc \
    -ffreestanding \
    -fno-stack-protector \
    -fno-stack-check \
    -fno-PIC \
    -m64 \
    -march=x86-64 \
    -mno-red-zone \
    -fno-tree-loop-distribute-patterns \
    -isystem $(LIBC_INC) \
    -isystem $(shell $(CC) -print-file-name=include)

# ---- Target ----

TARGET := liblibc.a
SDK_TARGET := liblibc-full.a
CRT_OBJS := crt1.o crti.o crtn.o

.PHONY: all clean

all: $(TARGET) $(SDK_TARGET) $(CRT_OBJS)

# printf_float.o is deliberately a SEPARATE archive member: a weak undefined
# reference does not extract an archive member, so programs that never format
# a float leave the decimal-conversion and pow/exp/log code behind. Programs
# that do need %e/%f/%g must link with -Wl,-u,_pf_putfloat to force it in.
$(TARGET): $(OBJDIR)/libc.o $(OBJDIR)/printf_float.o $(ASM_OBJS)
	$(AR) rcs $@ $^

$(OBJDIR)/libc.o: libc.c printf_internal.h Makefile
	@mkdir -p $(OBJDIR)
	$(CC) $(CFLAGS) -c $< -o $@

$(OBJDIR)/printf_float.o: printf_float.c printf_internal.h Makefile
	@mkdir -p $(OBJDIR)
	$(CC) $(CFLAGS) -c $< -o $@

$(OBJDIR)/setjmp.o: setjmp.S Makefile
	@mkdir -p $(OBJDIR)
	$(CC) $(CFLAGS) -c $< -o $@

# Variant shipped in the on-OS sysroots (0:/sdk/lib/libc.a and tcc's), where
# nobody can pass -Wl,-u,_pf_putfloat by hand. `ld -r` binds vsnprintf's weak
# reference to the float module at merge time, so %e/%f/%g just work for code
# compiled ON MontaukOS. Function-level --gc-sections still applies to the
# rest; only the float path is pinned. Image size is not a concern here.
$(SDK_TARGET): $(OBJDIR)/libc-merged.o $(ASM_OBJS)
	$(AR) rcs $@ $^

$(OBJDIR)/libc-merged.o: $(OBJDIR)/libc.o $(OBJDIR)/printf_float.o
	$(LD) -r -o $@ $(OBJDIR)/libc.o $(OBJDIR)/printf_float.o

crt1.o: $(CRTDIR)/crt1.c Makefile
	$(CC) $(CRT_CFLAGS) -c $< -o $@

crti.o: $(CRTDIR)/crti.c Makefile
	$(CC) $(CRT_CFLAGS) -c $< -o $@

crtn.o: $(CRTDIR)/crtn.c Makefile
	$(CC) $(CRT_CFLAGS) -c $< -o $@

clean:
	rm -rf $(OBJDIR) $(TARGET) $(SDK_TARGET) $(CRT_OBJS)
