Skip to content

Distribute and run LLMs with a single file.

4.0 KB
Raw Blame
#-*-mode:makefile-gmake;indent-tabs-mode:t;tab-width:8;coding:utf-8-*-┐
#── vi: set noet ft=make ts=8 sw=8 fenc=utf-8 :vi ────────────────────┘

# ==============================================================================
# Compiler Commands
# ==============================================================================

LINK.o = $(CXX) $(CCFLAGS) $(LDFLAGS)
COMPILE.c = $(CC) $(CCFLAGS) $(CFLAGS) $(CPPFLAGS_) $(CPPFLAGS) $(TARGET_ARCH) -c
COMPILE.cc = $(CXX) $(CCFLAGS) $(CXXFLAGS) $(CPPFLAGS_) $(CPPFLAGS) $(TARGET_ARCH) -c

# ==============================================================================
# Standard Compilation Rules
# ==============================================================================

o/$(MODE)/%.o: %.c $(COSMOCC)
	@mkdir -p $(@D)
	$(COMPILE.c) -o $@ $<

o/$(MODE)/%.o: o/$(MODE)/%.c $(COSMOCC)
	@mkdir -p $(@D)
	$(COMPILE.c) -o $@ $<

o/$(MODE)/%.o: %.cc $(COSMOCC)
	@mkdir -p $(@D)
	$(COMPILE.cc) -o $@ $<

o/$(MODE)/%.o: %.cpp $(COSMOCC)
	@mkdir -p $(@D)
	$(COMPILE.cc) -o $@ $<

# ==============================================================================
# Extension-preserving compilation rules
# ==============================================================================

# These rules produce foo.c.o from foo.c (instead of foo.o).
# Required when both foo.c and foo.cpp exist in the same directory,
# otherwise both would produce foo.o and collide.
# Example: ggml/src/ggml.c and ggml/src/ggml.cpp both exist in llama.cpp.
o/$(MODE)/%.c.o: %.c $(COSMOCC)
	@mkdir -p $(@D)
	$(COMPILE.c) -o $@ $<

o/$(MODE)/%.cpp.o: %.cpp $(COSMOCC)
	@mkdir -p $(@D)
	$(COMPILE.cc) -o $@ $<

o/$(MODE)/%.c.o: o/$(MODE)/%.c $(COSMOCC)
	@mkdir -p $(@D)
	$(COMPILE.c) -o $@ $<

o/$(MODE)/%.cpp.o: o/$(MODE)/%.cpp $(COSMOCC)
	@mkdir -p $(@D)
	$(COMPILE.cc) -o $@ $<

# ==============================================================================
# Hash functions generated by gperf
# ==============================================================================

o/$(MODE)/%.c: %.gperf
	@mkdir -p $(@D)
	build/gperf --output-file=$@ $<

# ==============================================================================
# Archive Creation
# ==============================================================================

o/$(MODE)/%.a:
	@mkdir -p $(dir $@)/.aarch64
	$(AR) $(ARFLAGS) $@ $^
	$(AR) $(ARFLAGS) $(dir $@)/.aarch64/$(notdir $@) $(foreach x,$^,$(dir $(x)).aarch64/$(notdir $(x)))

# ==============================================================================
# Linking Rules
# ==============================================================================

o/$(MODE)/%: o/$(MODE)/%.o
	$(LINK.o) $^ $(LOADLIBES) $(LDLIBS) -o $@

o/$(MODE)/%.com: o/$(MODE)/%.o
	$(LINK.o) $^ $(LOADLIBES) $(LDLIBS) -o $@

# ==============================================================================
# Test Execution
# ==============================================================================

%.runs: %
	$<
	@touch $@

# ==============================================================================
# Man Page Generation
# ==============================================================================

.PRECIOUS: %.1.asc
%.1.asc: %.1
	-MANWIDTH=80 MAN_KEEP_FORMATTING=1 man $< >[email protected] && mv -f [email protected] $@
	@rm -f [email protected]

# ==============================================================================
# Zip Object Creation via Cosmpolitan Zip File Compiler (zipobj)
# ==============================================================================

o/$(MODE)/%.zip.o: % $(COSMOCC)
	@mkdir -p $(dir $@)/.aarch64
	$(ZIPOBJ) $(ZIPOBJ_FLAGS) -a x86_64 -o $@ $<
	$(ZIPOBJ) $(ZIPOBJ_FLAGS) -a aarch64 -o $(dir $@)/.aarch64/$(notdir $@) $<

# ==============================================================================
# APE Setup
# ==============================================================================

$(PREFIX)/bin/ape: $(COSMOCC)
	$(INSTALL) $(COSMOCC)/bin/ape-$(ARCH).elf $(PREFIX)/bin/ape
	echo ':APE:M::MZqFpD::/usr/bin/ape:' > /proc/sys/fs/binfmt_misc/register