-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
187 lines (151 loc) · 6.63 KB
/
Copy pathMakefile
File metadata and controls
187 lines (151 loc) · 6.63 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
LLAMA_DIR := llama
LIM_LLAMA_BUILD_DIR ?= $(LLAMA_DIR)/build
# Decide whether LIM_LLAMA_BUILD_DIR is the in-tree ./llama/build or a separate,
# pre-built EXTERNAL directory. The default (and the readlink-unavailable
# fallback) is a textual match on the in-tree spelling; the canonical match
# additionally catches absolute paths, ../, trailing slashes, and symlinks that
# resolve to the same directory. readlink -fm makes a path absolute, resolves ..
# and symlinks, and tolerates the build dir not existing yet (fresh checkout).
CANON_BUILD_DIR := $(shell readlink -fm $(LIM_LLAMA_BUILD_DIR) 2>/dev/null)
CANON_LLAMA_BUILD := $(shell readlink -fm $(LLAMA_DIR)/build 2>/dev/null)
IN_TREE_BUILD := 0
ifeq ($(LIM_LLAMA_BUILD_DIR),$(LLAMA_DIR)/build)
IN_TREE_BUILD := 1
else ifneq ($(CANON_BUILD_DIR),)
ifeq ($(CANON_BUILD_DIR),$(CANON_LLAMA_BUILD))
IN_TREE_BUILD := 1
endif
endif
# In-tree: `make` builds llama from the subrepo and `llama-clean`/`distclean`
# remove the build dir. External: `make` never builds llama (links the pre-built
# libs) and `distclean` never touches the directory.
CXX = g++
# Auto-detect CUDA architecture if not overridden by environment
CUDA_ARCH_FLAGS ?=
ifeq ($(CUDA_ARCH_FLAGS),)
# Try to detect GPU compute capability from nvidia-smi
NV_CAPS := $(shell nvidia-smi --query-gpu=compute_cap --format=csv,noheader 2>/dev/null | tr -d ' ')
ifneq ($(NV_CAPS),)
# Build architecture list for all unique GPUs
CUDA_ARCH_FLAGS := $(shell echo "$(NV_CAPS)" | while read cap; do \
major=$$(echo $$cap | cut -d. -f1); \
minor=$$(echo $$cap | cut -d. -f2); \
arch=$${major}$${minor}; \
if { [ "$$major" = "12" ] || [ "$$major" = "9" ]; } && [ "$$minor" = "0" ]; then echo "$${arch}a"; else echo "$$arch"; fi; \
done | sort -u | paste -sd';')
endif
endif
ifeq ($(CUDA_ARCH_FLAGS),)
# No GPU detected and no override -- will be checked at build time if needed
endif
# Determine GGML backend flags
GGML_CUDA_FLAG ?=
GGML_HIPBLAS_FLAG ?=
ifeq ($(GGML_CUDA),off)
GGML_CUDA_FLAG := -DGGML_CUDA=OFF
else ifneq ($(NV_CAPS),)
GGML_CUDA_FLAG := -DGGML_CUDA=ON
endif
ifeq ($(GGML_HIPBLAS),on)
GGML_HIPBLAS_FLAG := -DGGML_HIPBLAS=ON
endif
CXXFLAGS = -std=c++17 -O3 -DLIM_VERSION=\"$(VERSION)\" \
-I$(LLAMA_DIR)/include \
-I$(LLAMA_DIR)/common \
-I$(LLAMA_DIR)/ggml/include \
-I$(LLAMA_DIR)/vendor \
-I/usr/include/libxml2
CUDA_LIB_DIR ?= $(shell N=$$(readlink -f "$$(which nvcc 2>/dev/null)" 2>/dev/null) && dirname "$$(dirname "$$N")" || echo /usr/local/cuda-13.0)/targets/x86_64-linux/lib
LDFLAGS = -L$(LIM_LLAMA_BUILD_DIR)/bin \
-L$(CUDA_LIB_DIR) \
-Wl,-rpath,$(shell readlink -f $(LIM_LLAMA_BUILD_DIR)/bin) \
-Wl,-rpath,$(CUDA_LIB_DIR) \
-lllama -lggml-base -lggml -lggml-cpu -lggml-cuda \
-lcudart -l:libllama-common.so -lreadline -lcurl -lxml2 -lcrypto
MAKEDEPEND = $(CXXFLAGS) -O0 -M -MG -DDEPEND
FILES = main output server tools filesystem network parsers signals model session token_generator tool_executor session_utils mtp
TARGET = lim
# Default target: build llama.cpp first, then lim
all: $(TARGET) vscode
.PHONY: all clean llama-clean install install-vscode uninstall uninstall-vscode install-all uninstall-all FORCE
$(LIM_LLAMA_BUILD_DIR):
mkdir -p $@
ifeq ($(IN_TREE_BUILD),1)
# In-tree: auto-build llama.cpp from the subrepo (LIM_LLAMA_BUILD_DIR is ./llama/build)
$(LIM_LLAMA_BUILD_DIR)/bin/libllama.so $(LIM_LLAMA_BUILD_DIR)/bin/libllama-common.so: $(LLAMA_DIR)/CMakeLists.txt | $(LIM_LLAMA_BUILD_DIR)
@if [ -z "$(CUDA_ARCH_FLAGS)" ]; then echo "Error: No GPU detected and CUDA_ARCH_FLAGS not set. Set it manually or build with GGML_CUDA=off for CPU-only." >&2; exit 1; fi
@echo "[llama.cpp] Configuring with CUDA architectures: $(CUDA_ARCH_FLAGS)"
cd $(LLAMA_DIR) && cmake -B $(abspath $(LIM_LLAMA_BUILD_DIR)) \
-DCMAKE_CUDA_ARCHITECTURES="$(CUDA_ARCH_FLAGS)" \
$(GGML_CUDA_FLAG) \
$(GGML_HIPBLAS_FLAG) \
$(LIM_LLAMA_CMAKE_FLAGS)
@echo "[llama.cpp] Building..."
cd $(LLAMA_DIR) && cmake --build $(abspath $(LIM_LLAMA_BUILD_DIR)) --target llama --target llama-common
$(TARGET): $(FILES:=.o) | $(LIM_LLAMA_BUILD_DIR)/bin/libllama.so $(LIM_LLAMA_BUILD_DIR)/bin/libllama-common.so
$(CXX) $(CXXFLAGS) $(FILES:=.o) -o $(TARGET) $(LDFLAGS)
else
# Use pre-built llama.cpp from external directory
$(TARGET): $(FILES:=.o)
$(CXX) $(CXXFLAGS) $(FILES:=.o) -o $(TARGET) $(LDFLAGS)
endif
VERSION := $(shell cat VERSION 2>/dev/null || echo 0.1.0)
VSIX = vscode-extension/vscode-extension-$(VERSION).vsix
vscode: $(VSIX)
$(VSIX): vscode-extension/src/extension.ts \
vscode-extension/package.json \
vscode-extension/tsconfig.json \
vscode-extension/resources/lim.png
sed -i 's/"version": "[^"]*"/"version": "$(VERSION)"/' vscode-extension/package.json
cd vscode-extension && NODE_NO_WARNINGS=1 npm install --no-bin-links && node_modules/typescript/bin/tsc -p ./ && NODE_NO_WARNINGS=1 npx @vscode/vsce package
LIM_CONFIG_DIR ?= $(HOME)/.config/lim
CONFIG_FILES = prompt reincarnate limServer.py viewer.html
LIBS_FILES = auto-render.min.js katex-standalone.css katex.min.css katex.min.js marked.min.js
# Install the binary and config files, then build and install the
# VS Code extension.
install: $(TARGET) vscode
mkdir -p ~/bin
cp $(TARGET) ~/bin/
mkdir -p $(LIM_CONFIG_DIR)
cp $(CONFIG_FILES) $(LIM_CONFIG_DIR)/
mkdir -p $(LIM_CONFIG_DIR)/searchCache
mkdir -p $(LIM_CONFIG_DIR)/libs
cd libs && cp $(LIBS_FILES) $(LIM_CONFIG_DIR)/libs
cp -r libs/fonts $(LIM_CONFIG_DIR)/libs/
NODE_NO_WARNINGS=1 code --install-extension $(VSIX) --force
install-vscode: vscode
NODE_NO_WARNINGS=1 code --install-extension $(VSIX) --force
uninstall-vscode: FORCE
-NODE_NO_WARNINGS=1 code --uninstall-extension statefullm.vscode-extension
# Alias: install now includes the VS Code extension.
install-all: install
uninstall: FORCE
-cd $(LIM_CONFIG_DIR) && rm -f $(CONFIG_FILES) userprompt && rmdir searchCache 2>/dev/null || true
-cd $(LIM_CONFIG_DIR)/libs && rm -f $(LIBS_FILES) && rm -rf fonts
rmdir $(LIM_CONFIG_DIR)/libs 2>/dev/null || true
rmdir $(LIM_CONFIG_DIR) 2>/dev/null || true
rm -f ~/bin/lim
uninstall-all: uninstall uninstall-vscode
clean: FORCE
rm -f $(TARGET) *.o *.d
rm -rf vscode-extension/out vscode-extension/node_modules vscode-extension/*.vsix
llama-clean:
ifeq ($(IN_TREE_BUILD),1)
rm -rf $(LIM_LLAMA_BUILD_DIR)
endif
distclean: clean llama-clean
.SUFFIXES: .cc .o .d
%.o: %.cc %.d $(FILES:=.cc)
$(CXX) $(CXXFLAGS) -o $@ -c $<
%.d: %.cc
@echo Creating $@; \
rm -f $@; \
${CXX} $(MAKEDEPEND) $< > $@.$$$$ 2>/dev/null && \
sed 's,\($*\)\.o[ :]*,\1.o \1.pic.o $@ : ,g' < $@.$$$$ > $@; \
rm -f $@.$$$$
ifneq ($(filter clean install,${MAKECMDGOALS}),)
SKIP_DEPS = 1
endif
ifndef SKIP_DEPS
-include $(FILES:=.d)
endif