build: hex dump assets at cmake build time (not config time)
This commit is contained in:
parent
b9286a4d7b
commit
5c4a94803d
2 changed files with 34 additions and 20 deletions
|
@ -1,31 +1,29 @@
|
||||||
set(SERVER_ASSETS
|
set(TARGET server)
|
||||||
|
option(LLAMA_SERVER_VERBOSE "Build verbose logging option for Server" ON)
|
||||||
|
option(LLAMA_SERVER_SSL "Build SSL support for the server" OFF)
|
||||||
|
include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})
|
||||||
|
set(TARGET_SRCS
|
||||||
|
server.cpp
|
||||||
|
utils.hpp
|
||||||
|
httplib.h
|
||||||
|
)
|
||||||
|
set(PUBLIC_ASSETS
|
||||||
index.html
|
index.html
|
||||||
index.js
|
index.js
|
||||||
completion.js
|
completion.js
|
||||||
json-schema-to-grammar.mjs
|
json-schema-to-grammar.mjs
|
||||||
)
|
)
|
||||||
foreach(asset ${SERVER_ASSETS})
|
foreach(asset ${PUBLIC_ASSETS})
|
||||||
# Portable equivalent of `cd public && xxd -i ${asset} ../${asset}.hpp`
|
|
||||||
set(input "${CMAKE_CURRENT_SOURCE_DIR}/public/${asset}")
|
set(input "${CMAKE_CURRENT_SOURCE_DIR}/public/${asset}")
|
||||||
set(output "${CMAKE_CURRENT_BINARY_DIR}/${asset}.hpp")
|
set(output "${CMAKE_CURRENT_BINARY_DIR}/${asset}.hpp")
|
||||||
|
list(APPEND TARGET_SRCS ${output})
|
||||||
string(REGEX REPLACE "\\.|-" "_" name "${asset}")
|
add_custom_command(
|
||||||
file(READ "${input}" data HEX)
|
DEPENDS "${input}"
|
||||||
string(LENGTH ${data} hex_len)
|
OUTPUT "${output}"
|
||||||
math(EXPR len "${hex_len} / 2")
|
COMMAND "${CMAKE_COMMAND}" "-DINPUT=${input}" "-DOUTPUT=${output}" -P "${PROJECT_SOURCE_DIR}/scripts/xxd.cmake"
|
||||||
string(REGEX REPLACE "([0-9a-f][0-9a-f])" "0x\\1," data ${data})
|
|
||||||
file(WRITE "${output}" "unsigned char ${name}[] = {${data}};\nunsigned int ${name}_len = ${len};\n")
|
|
||||||
endforeach()
|
|
||||||
|
|
||||||
set(TARGET server)
|
|
||||||
option(LLAMA_SERVER_VERBOSE "Build verbose logging option for Server" ON)
|
|
||||||
option(LLAMA_SERVER_SSL "Build SSL support for the server" OFF)
|
|
||||||
include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})
|
|
||||||
add_executable(${TARGET}
|
|
||||||
server.cpp
|
|
||||||
utils.hpp
|
|
||||||
httplib.h
|
|
||||||
)
|
)
|
||||||
|
endforeach()
|
||||||
|
add_executable(${TARGET} ${TARGET_SRCS})
|
||||||
install(TARGETS ${TARGET} RUNTIME)
|
install(TARGETS ${TARGET} RUNTIME)
|
||||||
target_compile_definitions(${TARGET} PRIVATE
|
target_compile_definitions(${TARGET} PRIVATE
|
||||||
SERVER_VERBOSE=$<BOOL:${LLAMA_SERVER_VERBOSE}>
|
SERVER_VERBOSE=$<BOOL:${LLAMA_SERVER_VERBOSE}>
|
||||||
|
|
16
scripts/xxd.cmake
Normal file
16
scripts/xxd.cmake
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
# CMake equivalent of `xxd -i ${INPUT} ${OUTPUT}`
|
||||||
|
# Usage: cmake -DINPUT=examples/server/public/index.html -DOUTPUT=examples/server/index.html.hpp -P scripts/xxd.cmake
|
||||||
|
|
||||||
|
SET(INPUT "" CACHE STRING "Input File")
|
||||||
|
SET(OUTPUT "" CACHE STRING "Output File")
|
||||||
|
|
||||||
|
get_filename_component(filename "${INPUT}" NAME)
|
||||||
|
string(REGEX REPLACE "\\.|-" "_" name "${filename}")
|
||||||
|
|
||||||
|
file(READ "${INPUT}" hex_data HEX)
|
||||||
|
string(REGEX REPLACE "([0-9a-f][0-9a-f])" "0x\\1," hex_sequence "${hex_data}")
|
||||||
|
|
||||||
|
string(LENGTH ${hex_data} hex_len)
|
||||||
|
math(EXPR len "${hex_len} / 2")
|
||||||
|
|
||||||
|
file(WRITE "${OUTPUT}" "unsigned char ${name}[] = {${hex_sequence}};\nunsigned int ${name}_len = ${len};\n")
|
Loading…
Add table
Add a link
Reference in a new issue