Skip to content

Commit

Permalink
feat: Byte-compile and embed builtin Lua modules
Browse files Browse the repository at this point in the history
FOr now this is only c_libs, but in future more builtin modules will be
written in Lua.
  • Loading branch information
mcb2003 committed Feb 27, 2024
1 parent 47367fe commit febba79
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ install(FILES
${CMAKE_CURRENT_BINARY_DIR}/lege_export.hpp
DESTINATION include/lege
)

add_subdirectory(modules)
5 changes: 5 additions & 0 deletions lib/builtins.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
#include "builtins.hpp"

// Byte-compiled Lua modules
#include "modules/lmod_c_libs.h"

namespace lege::modules {

void register_builtins(EngineImpl &e) {
e.load((const char *)luaJIT_BC_c_libs, luaJIT_BC_c_libs_SIZE, "b",
"lege.c_libs");
e.load(luaopen_lege_log, "lege.log");
e.load(luaopen_lege_enum, "lege.enum");
e.load(luaopen_lege_readonly, "lege.readonly");
Expand Down
33 changes: 33 additions & 0 deletions lib/modules/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Lua modules:
# These are byte-compiled and embedded in liblege, then preloaded by the library
function(add_lua_module MODULE_NAME SOURCE_FILE)
find_program(LUAJIT luajit)
if(NOT LUAJIT)
message(FATAL_ERROR "LuaJIT not found")
endif()

set(luajit_compile_opts "-b" "-t" "h")

if(CMAKE_BUILD_TYPE MATCHES Debug)
list(APPEND luajit_compile_opts "-g")
else()
list(APPEND luajit_compile_opts "-s")
endif()

set(OUTPUT_HEADER "lmod_${MODULE_NAME}.h")

add_custom_command(
OUTPUT ${OUTPUT_HEADER}
COMMAND ${LUAJIT}
ARGS ${luajit_compile_opts} "${CMAKE_CURRENT_SOURCE_DIR}/${SOURCE_FILE}" "${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT_HEADER}"
DEPENDS ${SOURCE_FILE}
COMMENT "Byte-compiling Lua module to ${OUTPUT_HEADER}"
)

add_custom_target(lmod_${MODULE_NAME} DEPENDS ${OUTPUT_HEADER})
add_dependencies(lege lmod_${MODULE_NAME})
endfunction()

foreach(module c_libs)
add_lua_module(${module} ${module}.lua)
endforeach()

0 comments on commit febba79

Please sign in to comment.