From 11a2e1ba6aebae7b456b63707830533bb2f681cc Mon Sep 17 00:00:00 2001 From: BlackAngelTVdev Date: Wed, 18 Nov 2026 08:05:41 +0100 Subject: [PATCH] build: add build script and CMake helper for compiled packages Compiling the compiler, the example and the demo by hand is error prone, so a single script keeps the whole flow reproducible and ignores build outputs. --- .gitignore | 10 +++++++ build.sh | 61 +++++++++++++++++++++++++++++++++++++++++ examples/CMakeLists.txt | 7 +++++ examples/lua/greet.lua | 14 ++++++++++ examples/lua/mathx.lua | 29 ++++++++++++++++++++ examples/main.cpp | 40 +++++++++++++++++++++++++++ 6 files changed, 161 insertions(+) create mode 100755 build.sh create mode 100644 examples/CMakeLists.txt create mode 100644 examples/lua/greet.lua create mode 100644 examples/lua/mathx.lua create mode 100644 examples/main.cpp diff --git a/.gitignore b/.gitignore index e257658..3e6596c 100644 --- a/.gitignore +++ b/.gitignore @@ -32,3 +32,13 @@ *.out *.app +# Build directories +build/ +Demo/build/ + + + +# ia +.freebuff +.claude +.copilot diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..fba453f --- /dev/null +++ b/build.sh @@ -0,0 +1,61 @@ +#!/usr/bin/env bash +# Builds the luacpp compiler, compiles the Lua modules of the Demo project with +# it, then builds and runs the resulting C++ program (plus the test suite). +# +# Usage: +# ./build.sh # full build + demo + tests +# ./build.sh path/to/x.lua # just compile one module and show the C++ output +# +# Environment: +# BUILD_DIR build directory (default: build) +# JOBS parallel jobs (default: nproc) +set -euo pipefail +cd "$(dirname "$0")" + +BUILD_DIR="${BUILD_DIR:-build}" +JOBS="${JOBS:-$(nproc 2>/dev/null || echo 2)}" +LUACPP="$BUILD_DIR/luacpp" + +echo "==> configuring" +cmake -S . -B "$BUILD_DIR" -DCMAKE_BUILD_TYPE=Release + +echo "==> building the compiler" +cmake --build "$BUILD_DIR" -j "$JOBS" --target luacpp +"$LUACPP" --version + +# Compile a single module on demand. +if [[ $# -gt 0 ]]; then + out_dir="$BUILD_DIR/single" + rm -rf "$out_dir" + "$LUACPP" "$1" --root "$(dirname "$1")" --output "$out_dir" + echo + echo "==> generated header ($out_dir)" + find "$out_dir" -name '*.hpp' -exec sh -c 'echo "--- $1"; cat "$1"' _ {} \; + echo + echo "==> generated source ($out_dir)" + find "$out_dir" -name '*.cpp' -exec sh -c 'echo "--- $1"; cat "$1"' _ {} \; + exit 0 +fi + +echo "==> compiling Demo/lua/*.lua into C++ packages" +rm -rf "$BUILD_DIR/demo-generated" +"$LUACPP" Demo/lua/todo.lua Demo/lua/report.lua \ + --root Demo/lua --namespace demo --output "$BUILD_DIR/demo-generated" + +echo "==> building the rest of the repository (example + tests)" +cmake --build "$BUILD_DIR" -j "$JOBS" + +echo +echo "==> running the example" +"$BUILD_DIR/examples/luacpp_example" + +echo +echo "==> building and running the Demo project" +cmake -S Demo -B "$BUILD_DIR/demo" -DCMAKE_BUILD_TYPE=Release +cmake --build "$BUILD_DIR/demo" -j "$JOBS" +echo +"$BUILD_DIR/demo/luacpp_demo" + +echo +echo "==> conformance test (compiled modules vs. plain Lua 5.4)" +( cd "$BUILD_DIR" && ctest --output-on-failure ) diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt new file mode 100644 index 0000000..eec89cd --- /dev/null +++ b/examples/CMakeLists.txt @@ -0,0 +1,7 @@ +add_lua_package(luacpp_example_lua + ROOT "${CMAKE_CURRENT_SOURCE_DIR}/lua" + MODULES mathx.lua greet.lua + NAMESPACE demo) + +add_executable(luacpp_example main.cpp) +target_link_libraries(luacpp_example PRIVATE luacpp_example_lua) diff --git a/examples/lua/greet.lua b/examples/lua/greet.lua new file mode 100644 index 0000000..ccfc79e --- /dev/null +++ b/examples/lua/greet.lua @@ -0,0 +1,14 @@ +-- Example module that requires another compiled module. +local mathx = require("demo.mathx") + +local M = {} + +function M.hello(name) + return "Bonjour " .. name .. " !" +end + +function M.double_factorial(n) + return mathx.factorial(n) * 2 +end + +return M diff --git a/examples/lua/mathx.lua b/examples/lua/mathx.lua new file mode 100644 index 0000000..133a21a --- /dev/null +++ b/examples/lua/mathx.lua @@ -0,0 +1,29 @@ +-- Example Lua module compiled into the C++ package demo::mathx. +local M = {} + +function M.add(a, b) + return a + b +end + +function M.factorial(n) + local result = 1 + for i = 2, n do + result = result * i + end + return result +end + +function M.summarise(values) + local total = 0 + local count = #values + for _, v in ipairs(values) do + total = total + v + end + return { + sum = total, + count = count, + average = count > 0 and total / count or 0, + } +end + +return M diff --git a/examples/main.cpp b/examples/main.cpp new file mode 100644 index 0000000..e3890a3 --- /dev/null +++ b/examples/main.cpp @@ -0,0 +1,40 @@ +// Shows how compiled Lua modules are consumed as C++ packages. +#include +#include + +#include + +#include "greet.hpp" +#include "mathx.hpp" + +int main() { + luacpp::State state; + + // 1. Namespaced facade generated from the exported Lua functions. + auto mathx = demo::mathx::bind(state); + auto greet = demo::greet::bind(state); + + std::cout << "mathx.add(2, 3) = " << mathx.add(2, 3).to_string() << "\n"; + std::cout << "mathx.factorial(6) = " << mathx.factorial(6).to_string() << "\n"; + std::cout << "greet.hello(\"Buffy\") = " << greet.hello("Buffy").to_string() << "\n"; + std::cout << "greet.double_factorial(5) = " << greet.double_factorial(5).to_string() << "\n"; + + // 2. Dynamic module handle (fallback when no facade is generated). + luacpp::Module mathx_module = state.module("demo.mathx"); + std::vector values{3.0, 4.0, 5.0}; + luacpp::Value summary = mathx_module.get("summarise").call(values); + std::cout << "summarise({3,4,5}).sum = " << summary.get("sum").to_string() << "\n"; + std::cout << "summarise({3,4,5}).average = " << summary.get("average").to_string() << "\n"; + + // 3. Host values visible from Lua, and Lua errors turned into C++ exceptions. + state.set_global("cpp_value", luacpp::Value(42)); + state.do_string("print('lua sees cpp_value = ' .. cpp_value)"); + + try { + mathx.add("not a number", 1); + } catch (const luacpp::Error& error) { + std::cout << "caught Lua error: " << error.what() << "\n"; + } + + return 0; +}