summaryrefslogtreecommitdiffstats
path: root/Makefile
blob: 5fca7e17afa52dcafc3d32ccf9c5f17f6834d0bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
CC            = cc
CXX           = c++
CFLAGS        = -O2 -std=c11 -Wall -Wextra
CXXFLAGS      = -O2 -Wall -Wextra
INCLUDES      = -Ideps/include

LLAMA_TAG     = b10107
LLAMA_URL     = https://github.com/ggml-org/llama.cpp/archive/refs/tags/$(LLAMA_TAG).tar.gz
LLAMA_TAR     = deps/llama-$(LLAMA_TAG).tar.gz
LLAMA_SRC_DIR = deps/llama.cpp-src
LLAMA_BUILD   = $(LLAMA_SRC_DIR)/build

STATIC_LIBS   = deps/lib/libllama.a \
                deps/lib/libggml.a \
                deps/lib/libggml-cpu.a \

# System flags required for static linking llama on OpenBSD
SYS_LIBS      = -static -pthread -lm -lc++ -lc++abi

TARGET        = wordbook.cgi
SRC           = main.c
OBJS          = main.o

.PHONY: all clean distclean

all: $(TARGET)

# Explicit link order: libllama -> libggml-cpu -> libggml -> libggml-base
$(TARGET): deps/lib/libllama.a $(OBJS)
	$(CXX) $(CXXFLAGS) $(OBJS) \
		-Wl,--start-group \
		deps/lib/libllama.a \
		deps/lib/libggml-cpu.a \
		deps/lib/libggml.a \
		deps/lib/libggml-base.a \
		-Wl,--end-group \
		$(SYS_LIBS) -o $@

# BSD suffix rule: compiles any .c file into .o
.c.o:
	$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

# Download llama source and build statically
deps/lib/libllama.a deps/lib/libggml.a deps/lib/libggml-cpu.a:
	@mkdir -p deps/lib deps/include
	@echo "===> Downloading llama.cpp $(LLAMA_TAG) source..."
	ftp -o $(LLAMA_TAR) $(LLAMA_URL)

	@echo "===> Extracting source archive..."
	tar -xzf $(LLAMA_TAR) -C deps
	rm -rf $(LLAMA_SRC_DIR)
	mv deps/llama.cpp-$(LLAMA_TAG) $(LLAMA_SRC_DIR)
	rm -f $(LLAMA_TAR)
	
	@echo "===> Compiling llama static archives..."
	cmake -S $(LLAMA_SRC_DIR) -B $(LLAMA_BUILD) \
		-DCMAKE_BUILD_TYPE=Release \
		-DBUILD_SHARED_LIBS=OFF \
		-DGGML_OPENMP=OFF \       # Disable OpenMP; use native OpenBSD pthreads
		-DGGML_VULKAN=OFF \       # Disable Vulkan GPU backend for CPU-only build
		-DLLAMA_BUILD_APP=OFF \
		-DLLAMA_BUILD_COMMON=OFF \
		-DLLAMA_BUILD_TESTS=OFF \
		-DLLAMA_BUILD_EXAMPLES=OFF \
		-DLLAMA_BUILD_SERVER=OFF \
		-DLLAMA_BUILD_TOOLS=OFF

	cmake --build $(LLAMA_BUILD) --config Release -j$$(sysctl -n hw.ncpu)

	@echo "===> Installing .a files and headers into deps/..."
	find $(LLAMA_BUILD) -type f -name "*.a" -exec cp {} deps/lib/ \;
	cp -R $(LLAMA_SRC_DIR)/include/* deps/include/
	cp -R $(LLAMA_SRC_DIR)/ggml/include/* deps/include/

	@echo "===> Cleaning up build directory..."
	rm -rf $(LLAMA_SRC_DIR)

clean:
	rm -f $(TARGET)
	
distclean: clean
	rm -rf deps