Files
MontaukOS/programs/lib/libc/Makefile
T

115 lines
3.0 KiB
Makefile

# Makefile for shared libc static library on MontaukOS
# Copyright (c) 2025-2026 Daniel Hammer
MAKEFLAGS += -rR
.SUFFIXES:
# ---- Toolchain ----
include ../../mk/arch.mk
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$(ARCH_SUFFIX)
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 \
$(ARCH_CFLAGS) \
$(ARCH_FP_FLAGS) \
-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 \
$(ARCH_CFLAGS) \
-fno-tree-loop-distribute-patterns \
-isystem $(LIBC_INC) \
-isystem $(shell $(CC) -print-file-name=include)
# ---- Target ----
TARGET := $(ARCH_OUT)liblibc.a
SDK_TARGET := $(ARCH_OUT)liblibc-full.a
CRT_OBJS := $(ARCH_OUT)crt1.o $(ARCH_OUT)crti.o $(ARCH_OUT)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)
@mkdir -p $(dir $@)
$(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: arch/setjmp-$(ARCH).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)
@mkdir -p $(dir $@)
$(AR) rcs $@ $^
$(OBJDIR)/libc-merged.o: $(OBJDIR)/libc.o $(OBJDIR)/printf_float.o
$(LD) -r -o $@ $(OBJDIR)/libc.o $(OBJDIR)/printf_float.o
$(ARCH_OUT)crt1.o: $(CRTDIR)/crt1.c Makefile
@mkdir -p $(dir $@)
$(CC) $(CRT_CFLAGS) -c $< -o $@
$(ARCH_OUT)crti.o: $(CRTDIR)/crti.c Makefile
@mkdir -p $(dir $@)
$(CC) $(CRT_CFLAGS) -c $< -o $@
$(ARCH_OUT)crtn.o: $(CRTDIR)/crtn.c Makefile
@mkdir -p $(dir $@)
$(CC) $(CRT_CFLAGS) -c $< -o $@
clean:
rm -rf $(OBJDIR) $(TARGET) $(SDK_TARGET) $(CRT_OBJS)