From d32f73006ea2839752ba0fc7cfd69a5a4308b2df Mon Sep 17 00:00:00 2001 From: BlackAngelTVdev Date: Wed, 18 Nov 2026 08:16:46 +0100 Subject: [PATCH] build: build only the compiler and drop the example project The example duplicated what Demo already covers, and the run mode needs the runtime include path and Lua link flags at execution time, so they are baked in at configure time. --- CMakeLists.txt | 24 +++++++++++++----- Demo/CMakeLists.txt | 1 - build.sh | 50 ++++++++++++++++---------------------- cmake/luacpp-config.hpp.in | 7 ++++++ examples/CMakeLists.txt | 7 ------ examples/lua/greet.lua | 14 ----------- examples/lua/mathx.lua | 29 ---------------------- examples/main.cpp | 40 ------------------------------ 8 files changed, 46 insertions(+), 126 deletions(-) create mode 100644 cmake/luacpp-config.hpp.in delete mode 100644 examples/CMakeLists.txt delete mode 100644 examples/lua/greet.lua delete mode 100644 examples/lua/mathx.lua delete mode 100644 examples/main.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 8f73445..e43bb23 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,6 @@ set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_EXTENSIONS OFF) -option(LUACPP_BUILD_EXAMPLES "Build the example package and program" ON) option(LUACPP_BUILD_TESTS "Build the conformance test" ON) # --- Lua 5.4 ----------------------------------------------------------------- @@ -30,21 +29,34 @@ else() endif() # --- compiler ---------------------------------------------------------------- +# `luacpp run` needs to invoke the C++ compiler and link the Lua VM itself, so +# the include path and link flags are baked into the binary at configure time. +if(LUA54_FOUND) + execute_process(COMMAND ${PKG_CONFIG_EXECUTABLE} --cflags lua5.4 + OUTPUT_VARIABLE _lua_cflags OUTPUT_STRIP_TRAILING_WHITESPACE) + execute_process(COMMAND ${PKG_CONFIG_EXECUTABLE} --libs lua5.4 + OUTPUT_VARIABLE _lua_libs OUTPUT_STRIP_TRAILING_WHITESPACE) +else() + set(_lua_cflags "-I${LUA_INCLUDE_DIR}") + string(REPLACE ";" " " _lua_libs "${LUA_LIBRARIES}") +endif() +set(LUACPP_RUNTIME_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/include") +set(LUACPP_LUA_CFLAGS "${_lua_cflags}") +set(LUACPP_LUA_LIBS "${_lua_libs}") +configure_file(cmake/luacpp-config.hpp.in + "${CMAKE_CURRENT_BINARY_DIR}/generated/luacpp-config.hpp" @ONLY) + add_executable(luacpp src/main.cpp src/frontend.cpp src/codegen.cpp) -target_include_directories(luacpp PRIVATE src) +target_include_directories(luacpp PRIVATE src "${CMAKE_CURRENT_BINARY_DIR}/generated") if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang") target_compile_options(luacpp PRIVATE -Wall -Wextra -Wpedantic) endif() include(cmake/lua-package.cmake) -if(LUACPP_BUILD_EXAMPLES) - add_subdirectory(examples) -endif() - if(LUACPP_BUILD_TESTS) enable_testing() add_subdirectory(tests) diff --git a/Demo/CMakeLists.txt b/Demo/CMakeLists.txt index 9277331..fb37145 100644 --- a/Demo/CMakeLists.txt +++ b/Demo/CMakeLists.txt @@ -5,7 +5,6 @@ set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) # Reuse the compiler from the repository root as a subproject. -set(LUACPP_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE) set(LUACPP_BUILD_TESTS OFF CACHE BOOL "" FORCE) add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/.." "${CMAKE_CURRENT_BINARY_DIR}/luacpp") include("${CMAKE_CURRENT_SOURCE_DIR}/../cmake/lua-package.cmake") diff --git a/build.sh b/build.sh index fba453f..34bf565 100755 --- a/build.sh +++ b/build.sh @@ -1,10 +1,12 @@ #!/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). +# Builds the luacpp compiler (the C++ program), nothing else. +# Then use the compiler from anywhere, for example: # -# Usage: -# ./build.sh # full build + demo + tests -# ./build.sh path/to/x.lua # just compile one module and show the C++ output +# ./build/luacpp run mon_script.lua # compile le Lua en C++ puis l'exécute +# cd build && ./luacpp run ../script.lua # même chose depuis le dossier build +# +# Options: +# ./build.sh --full # + Demo et tests # # Environment: # BUILD_DIR build directory (default: build) @@ -14,43 +16,33 @@ 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"' _ {} \; +LUACPP="$BUILD_DIR/luacpp" +"$LUACPP" --version +echo +echo "compilateur prêt : $LUACPP" +echo +echo "Utilisation :" +echo " $LUACPP run [args...] # compile le Lua en C++ puis l'exécute" +echo " cd $BUILD_DIR && ./luacpp run ../Demo/hello.lua" +echo " $LUACPP -n demo -o gen # génère seulement .hpp/.cpp" +echo + +if [[ "${1:-}" != "--full" ]]; then 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)" +echo "==> building the Demo and the 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" +echo "==> Demo project" cmake -S Demo -B "$BUILD_DIR/demo" -DCMAKE_BUILD_TYPE=Release cmake --build "$BUILD_DIR/demo" -j "$JOBS" echo diff --git a/cmake/luacpp-config.hpp.in b/cmake/luacpp-config.hpp.in new file mode 100644 index 0000000..a15c4c3 --- /dev/null +++ b/cmake/luacpp-config.hpp.in @@ -0,0 +1,7 @@ +// Generated by CMake: paths and flags luacpp bakes in to compile and link the +// C++ it generates for `luacpp run`. +#pragma once + +#define LUACPP_RUNTIME_INCLUDE_DIR "@LUACPP_RUNTIME_INCLUDE_DIR@" +#define LUACPP_LUA_CFLAGS "@LUACPP_LUA_CFLAGS@" +#define LUACPP_LUA_LIBS "@LUACPP_LUA_LIBS@" diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt deleted file mode 100644 index eec89cd..0000000 --- a/examples/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -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 deleted file mode 100644 index ccfc79e..0000000 --- a/examples/lua/greet.lua +++ /dev/null @@ -1,14 +0,0 @@ --- 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 deleted file mode 100644 index 133a21a..0000000 --- a/examples/lua/mathx.lua +++ /dev/null @@ -1,29 +0,0 @@ --- 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 deleted file mode 100644 index e3890a3..0000000 --- a/examples/main.cpp +++ /dev/null @@ -1,40 +0,0 @@ -// 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; -}