-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
142 lines (118 loc) · 4.37 KB
/
Copy pathMakefile
File metadata and controls
142 lines (118 loc) · 4.37 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
# ShellLM Makefile
DAEMON_BINARY=shelllm-daemon
CLI_BINARY=slm
# OS and Architecture detection
OS=$(shell uname -s | tr '[:upper:]' '[:lower:]')
ARCH=$(shell uname -m)
ifeq ($(ARCH),x86_64)
GO_ARCH=amd64
ONNX_ARCH=x64
else ifeq ($(ARCH),arm64)
GO_ARCH=arm64
ONNX_ARCH=arm64
else
GO_ARCH=$(ARCH)
ONNX_ARCH=$(ARCH)
endif
# Library paths
LIB_DIR=$(CURDIR)/lib/$(OS)_$(GO_ARCH)
INC_DIR=$(CURDIR)/include
# Default ONNX path
ONNX_VERSION=1.24.3
ifeq ($(OS),darwin)
ONNX_OS=osx
else
ONNX_OS=$(OS)
endif
ONNX_DIR=$(CURDIR)/onnxruntime-$(ONNX_OS)-$(ONNX_ARCH)-$(ONNX_VERSION)
ifeq ($(OS),darwin)
LIB_EXT=dylib
else
LIB_EXT=so
endif
ONNX_LIB=$(ONNX_DIR)/lib/libonnxruntime.$(LIB_EXT)
# Download URLs
MODEL_PATH=src/internal/all_minilm/model.onnx
MODEL_URL=https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2/resolve/main/onnx/model.onnx
ifeq ($(OS),linux)
ONNX_URL=https://github.com/microsoft/onnxruntime/releases/download/v1.24.3/onnxruntime-linux-x64-1.24.3.tgz
else
ONNX_URL=https://github.com/microsoft/onnxruntime/releases/download/v1.24.3/onnxruntime-osx-arm64-1.24.3.tgz
endif
LANCED_VERSION=v0.1.2
LANCED_URL=https://github.com/lancedb/lancedb-go/releases/download/$(LANCED_VERSION)/liblancedb_go.a
LANCED_H_URL=https://github.com/lancedb/lancedb-go/releases/download/$(LANCED_VERSION)/lancedb.h
.PHONY: all build build-daemon build-cli run clean help fetch-model fetch-onnx fetch-lancedb confirm install
all: build
confirm:
@echo -n "This may download several hundred MBs of dependencies. Continue? [y/N] " && read ans && [ $${ans:-N} = y ]
## fetch-onnx: Download and extract ONNX Runtime if missing
fetch-onnx:
@if [ ! -d $(ONNX_DIR) ]; then \
$(MAKE) confirm; \
echo "Downloading ONNX Runtime from $(ONNX_URL)..."; \
curl -L -o onnxruntime.tgz $(ONNX_URL); \
tar -xzf onnxruntime.tgz; \
rm onnxruntime.tgz; \
fi
## fetch-lancedb: Download LanceDB native library if missing
fetch-lancedb:
@if [ ! -f $(LIB_DIR)/liblancedb_go.a ] || [ $$(stat -f%z $(LIB_DIR)/liblancedb_go.a 2>/dev/null || stat -c%s $(LIB_DIR)/liblancedb_go.a) -lt 100 ]; then \
$(MAKE) confirm; \
echo "Downloading LanceDB native library from $(LANCED_URL)..."; \
mkdir -p $(LIB_DIR); \
curl -L -o $(LIB_DIR)/liblancedb_go.a $(LANCED_URL); \
fi
@if [ ! -f $(INC_DIR)/lancedb.h ]; then \
echo "Downloading lancedb.h..."; \
mkdir -p $(INC_DIR); \
curl -L -o $(INC_DIR)/lancedb.h $(LANCED_H_URL); \
fi
## fetch-model: Download the ONNX model if it is missing
fetch-model:
@if [ ! -f $(MODEL_PATH) ]; then \
if [ ! -d $(ONNX_DIR) ]; then $(MAKE) confirm; fi; \
echo "Downloading $(MODEL_PATH) from Hugging Face..."; \
curl -L -o $(MODEL_PATH) $(MODEL_URL); \
fi
## build: Compile both daemon and cli binaries
build: build-daemon build-cli
## build-daemon: Compile the shelllm-daemon binary
build-daemon: fetch-onnx fetch-lancedb fetch-model
@echo "Building daemon for $(OS)_$(GO_ARCH)..."
CGO_CFLAGS="-I$(INC_DIR) -I$(ONNX_DIR)/include" \
CGO_LDFLAGS="-L$(LIB_DIR) -llancedb_go -L$(ONNX_DIR)/lib -Wl,-rpath,$(ONNX_DIR)/lib -framework Security -framework CoreFoundation" \
go build -o $(DAEMON_BINARY) src/daemon/daemon.go
## dev: Run the daemon with debug logging enabled
dev: fetch-onnx fetch-lancedb fetch-model
@echo "Building daemon with debug tags..."
CGO_CFLAGS="-I$(INC_DIR) -I$(ONNX_DIR)/include" \
CGO_LDFLAGS="-L$(LIB_DIR) -llancedb_go -L$(ONNX_DIR)/lib -Wl,-rpath,$(ONNX_DIR)/lib -framework Security -framework CoreFoundation" \
go build -tags debug -o $(DAEMON_BINARY) src/daemon/daemon.go src/daemon/log_dev.go src/daemon/log_prod.go
ONNXRUNTIME_LIB_PATH=$(ONNX_LIB) ./$(DAEMON_BINARY)
## build-cli: Compile the slm CLI binary
build-cli:
@echo "Building CLI..."
go build -o $(CLI_BINARY) src/cli/slm.go
## install: Install the slm binary to /usr/local/bin
install: build-cli
@echo "Installing $(CLI_BINARY) to /usr/local/bin..."
sudo cp $(CLI_BINARY) /usr/local/bin/$(CLI_BINARY)
## run: Build and run the daemon
run: build-daemon
@echo "Running $(DAEMON_BINARY)..."
ONNXRUNTIME_LIB_PATH=$(ONNX_LIB) \
./$(DAEMON_BINARY)
## clean: Remove binaries and temporary runtime files
clean:
@echo "Cleaning up..."
go clean
rm -f $(DAEMON_BINARY) $(CLI_BINARY)
rm -f /tmp/shelllm.history.socket
rm -f /tmp/shelllm.query.socket
rm -f /tmp/shelllm.socket
# Caution: removing database
# rm -rf /tmp/testdb
help:
@echo "Available targets:"
@sed -n 's/^##//p' Makefile