Skip to content

Commit

Permalink
Add support for Fennel.
Browse files Browse the repository at this point in the history
  • Loading branch information
rblank committed Nov 6, 2023
1 parent 87999e6 commit b9dfa24
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
8 changes: 8 additions & 0 deletions docs/core.md
Expand Up @@ -97,6 +97,14 @@ target_link_libraries(mod_example INTERFACE
)
```

### Fennel

MicroLua supports writing modules in [Fennel](https://fennel-lang.org/), by
transpiling Fennel sources to Lua. Simply use `mlua_add_fnl_modules()` instead
of `mlua_add_lua_modules()`. There are a few examples in the
[MicroLua-examples](https://github.com/MicroLua/MicroLua-examples/blob/master/fennel)
repository.

## Function metatable

The MicroLua runtime sets a metatable on the `function` type with a `__close`
Expand Down
52 changes: 52 additions & 0 deletions rules.cmake
Expand Up @@ -19,6 +19,39 @@ function(mlua_want_lua)
endif()
endfunction()

if(NOT FENNEL)
if(DEFINED ENV{FENNEL})
set(FENNEL "$ENV{FENNEL}")
message("Using FENNEL from environment ('${FENNEL}')")
else()
set(FENNEL "fennel")
endif()
endif()
set(FENNEL "${FENNEL}" CACHE PATH "Path to the fennel compiler" FORCE)

function(mlua_fennel_version VERSION)
execute_process(
COMMAND "${FENNEL}" "--version"
OUTPUT_VARIABLE version
RESULT_VARIABLE result
)
if("${result}" EQUAL 0)
string(STRIP "${version}" version)
set("${VERSION}" "${version}" PARENT_SCOPE)
endif()
endfunction()

function(mlua_want_fennel)
if(NOT Fennel_FOUND)
mlua_fennel_version(version)
if(version STREQUAL "")
message(FATAL_ERROR "Fennel compiler not found: ${FENNEL}")
endif()
message("Fennel compiler is ${FENNEL} (${version})")
set(Fennel_FOUND 1)
endif()
endfunction()

function(mlua_num_target VAR PREFIX)
set(prop "${PREFIX}_id")
get_property(set GLOBAL PROPERTY "${prop}" SET)
Expand Down Expand Up @@ -169,6 +202,25 @@ function(mlua_add_lua_modules TARGET)
target_link_libraries("${TARGET}" INTERFACE mlua_core mlua_core_main)
endfunction()

function(mlua_add_fnl_modules TARGET)
mlua_want_fennel()
foreach(src IN LISTS ARGN)
cmake_path(ABSOLUTE_PATH src)
cmake_path(GET src STEM LAST_ONLY mod)
set(output "${CMAKE_CURRENT_BINARY_DIR}/${mod}.lua")
add_custom_command(
COMMENT "Generating $<PATH:RELATIVE_PATH,${output},${CMAKE_BINARY_DIR}>"
DEPENDS "${src}"
OUTPUT "${output}"
COMMAND "${FENNEL}"
"--compile" "--correlate" "${src}" > "${output}"
VERBATIM
)
list(APPEND srcs "${output}")
endforeach()
mlua_add_lua_modules("${TARGET}" "${srcs}")
endfunction()

# TARGET must be a binary target.
function(mlua_add_config_module TARGET)
mlua_want_lua()
Expand Down

0 comments on commit b9dfa24

Please sign in to comment.