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.
This commit is contained in:
BlackAngelTV committed 2026-11-18 08:48:05 +01:00
1 parent 42e95d08b9
commit d32f73006e
8 files changed
+46 -126

No files matched your search

+18 -6
View File
@@ -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)
-1
View File
@@ -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")
+21 -29
View File
@@ -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 <script.lua> [args...] # compile le Lua en C++ puis l'exécute"
echo " cd $BUILD_DIR && ./luacpp run ../Demo/hello.lua"
echo " $LUACPP <module.lua> -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
+7
View File
@@ -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@"
-7
View File
@@ -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)
-14
View File
@@ -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
-29
View File
@@ -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
-40
View File
@@ -1,40 +0,0 @@
// Shows how compiled Lua modules are consumed as C++ packages.
#include <iostream>
#include <vector>
#include <luacpp/rt.hpp>
#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<luacpp::Value> 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;
}