Skip to content

Commit b5f9c70

Browse files
author
ochafik
committed
build: generate hex dumps of server assets on the fly
1 parent 4bd0f93 commit b5f9c70

File tree

9 files changed

+42
-7186
lines changed

9 files changed

+42
-7186
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ lcov-report/
3434
gcovr-report/
3535

3636
build*
37+
!build.zig
3738
cmake-build-*
3839
out/
3940
tmp/
@@ -100,6 +101,9 @@ qnt-*.txt
100101
perf-*.txt
101102

102103
examples/jeopardy/results.txt
104+
examples/server/*.html.hpp
105+
examples/server/*.js.hpp
106+
examples/server/*.mjs.hpp
103107

104108
poetry.lock
105109
poetry.toml

Makefile

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
BUILD_TARGETS = \
33
main quantize quantize-stats perplexity imatrix embedding vdot q8dot train-text-from-scratch convert-llama2c-to-ggml \
44
simple batched batched-bench save-load-state server gguf gguf-split eval-callback llama-bench libllava.a llava-cli baby-llama beam-search \
5-
retrieval speculative infill tokenize benchmark-matmult parallel finetune export-lora lookahead lookup passkey gritlm tests/test-c.o
5+
retrieval speculative infill tokenize benchmark-matmult parallel finetune export-lora lookahead lookup passkey gritlm tests/test-c.o \
6+
examples/server/index.html.hpp examples/server/index.js.hpp examples/server/completion.js.hpp examples/server/json-schema-to-grammar.mjs.hpp
67

78
# Binaries only useful for tests
89
TEST_TARGETS = \
@@ -788,10 +789,13 @@ save-load-state: examples/save-load-state/save-load-state.cpp ggml.o llama.o $(C
788789
$(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
789790
$(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
790791

791-
server: examples/server/server.cpp examples/server/utils.hpp examples/server/httplib.h common/json.hpp examples/server/index.html.hpp examples/server/index.js.hpp examples/server/completion.js.hpp json-schema-to-grammar.o common/stb_image.h ggml.o llama.o $(COMMON_DEPS) grammar-parser.o $(OBJS)
792+
server: examples/server/server.cpp examples/server/utils.hpp examples/server/httplib.h common/json.hpp examples/server/index.html.hpp examples/server/index.js.hpp examples/server/completion.js.hpp examples/server/json-schema-to-grammar.mjs.hpp json-schema-to-grammar.o common/stb_image.h ggml.o llama.o $(COMMON_DEPS) grammar-parser.o $(OBJS)
792793
$(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
793794
$(CXX) $(CXXFLAGS) $(filter-out %.h %.hpp $<,$^) -Iexamples/server $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS) $(LWINSOCK2)
794795

796+
examples/server/%.hpp: examples/server/public/%
797+
xxd -n $(notdir $<) -i $< $@
798+
795799
gguf: examples/gguf/gguf.cpp ggml.o $(OBJS)
796800
$(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
797801
$(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)

build.zig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,13 @@ pub fn build(b: *std.build.Builder) !void {
139139
if (server.target.isWindows()) {
140140
server.linkSystemLibrary("ws2_32");
141141
}
142+
143+
const server_assets = [_][]const u8{ "index.html", "index.js", "completion.js", "json-schema-to-grammar.mjs" };
144+
for (server_assets) |asset| {
145+
// Note: code can be simplified once setCwd is standard.
146+
const gen_hpp = b.addSystemCommand(
147+
&.{ "xxd", "-n", asset, "-i", b.fmt("examples/server/public/{s}", .{asset}), b.fmt("examples/server/{s}.hpp", .{asset}) },
148+
);
149+
server.step.dependOn(&gen_hpp.step);
150+
}
142151
}

examples/server/CMakeLists.txt

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,34 @@
1+
set(SERVER_ASSETS
2+
index.html
3+
index.js
4+
completion.js
5+
json-schema-to-grammar.mjs
6+
)
7+
set(SERVER_ASSETS_HPP "")
8+
foreach(asset ${SERVER_ASSETS})
9+
list(APPEND SERVER_ASSETS_HPP "${CMAKE_CURRENT_BINARY_DIR}/${asset}.hpp")
10+
endforeach()
11+
foreach(asset ${SERVER_ASSETS})
12+
add_custom_command(
13+
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${asset}.hpp"
14+
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/public"
15+
COMMAND xxd -i "${asset}" "${CMAKE_CURRENT_BINARY_DIR}/${asset}.hpp"
16+
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/public/${asset}"
17+
VERBATIM
18+
)
19+
endforeach()
20+
add_custom_target(generate_server_assets ALL DEPENDS ${SERVER_ASSETS_HPP})
21+
122
set(TARGET server)
223
option(LLAMA_SERVER_VERBOSE "Build verbose logging option for Server" ON)
324
option(LLAMA_SERVER_SSL "Build SSL support for the server" OFF)
4-
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
25+
include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})
526
add_executable(${TARGET}
627
server.cpp
728
utils.hpp
829
httplib.h
930
)
31+
add_dependencies(${TARGET} generate_server_assets)
1032
install(TARGETS ${TARGET} RUNTIME)
1133
target_compile_definitions(${TARGET} PRIVATE
1234
SERVER_VERBOSE=$<BOOL:${LLAMA_SERVER_VERBOSE}>

examples/server/completion.js.hpp

Lines changed: 0 additions & 496 deletions
This file was deleted.

examples/server/deps.sh

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,3 @@ PUBLIC=$DIR/public
88
echo "download js bundle files"
99
curl https://npm.reversehttp.com/@preact/signals-core,@preact/signals,htm/preact,preact,preact/hooks > $PUBLIC/index.js
1010
echo >> $PUBLIC/index.js # add newline
11-
12-
FILES=$(ls $PUBLIC)
13-
14-
cd $PUBLIC
15-
for FILE in $FILES; do
16-
echo "generate $FILE.hpp"
17-
18-
# use simple flag for old version of xxd
19-
xxd -i $FILE > $DIR/$FILE.hpp
20-
done

0 commit comments

Comments
 (0)