Compiled programs could only write to the terminal, so the demo editor had no real window. When CMake finds SDL2 the runtime now exposes a `gui` table and Demo/guipaint.lua builds a 960x640 editor on top of it. Frames are collected in Lua, merged into horizontal runs and sent with a single `gui.rects` call: a full scene goes from 15071 draw calls and 42.9 ms per frame to 1522 calls and 2.1 ms, and the rendered screenshot stays pixel-identical.
80 lines
3.0 KiB
CMake
80 lines
3.0 KiB
CMake
cmake_minimum_required(VERSION 3.20)
|
|
project(luacpp VERSION 0.2.0 LANGUAGES CXX)
|
|
|
|
set(CMAKE_CXX_STANDARD 17)
|
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
|
set(CMAKE_CXX_EXTENSIONS OFF)
|
|
|
|
option(LUACPP_BUILD_TESTS "Build the conformance test" ON)
|
|
|
|
# --- Lua 5.4 -----------------------------------------------------------------
|
|
find_package(PkgConfig QUIET)
|
|
if(PkgConfig_FOUND)
|
|
pkg_check_modules(LUA54 QUIET IMPORTED_TARGET lua5.4)
|
|
endif()
|
|
if(NOT LUA54_FOUND)
|
|
find_package(Lua 5.4 REQUIRED)
|
|
endif()
|
|
|
|
add_library(luacpp_rt INTERFACE)
|
|
add_library(luacpp::rt ALIAS luacpp_rt)
|
|
target_include_directories(luacpp_rt INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/include")
|
|
if(LUA54_FOUND)
|
|
target_link_libraries(luacpp_rt INTERFACE PkgConfig::LUA54)
|
|
message(STATUS "luacpp: using Lua 5.4 from pkg-config")
|
|
else()
|
|
target_include_directories(luacpp_rt INTERFACE ${LUA_INCLUDE_DIR})
|
|
target_link_libraries(luacpp_rt INTERFACE ${LUA_LIBRARIES})
|
|
message(STATUS "luacpp: using Lua 5.4 from FindLua")
|
|
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}")
|
|
|
|
# --- optional GUI backend -----------------------------------------------------
|
|
# Programs compiled with `luacpp run` get a global `gui` table (window, drawing
|
|
# primitives, events) when SDL2 is available. Without SDL2 nothing changes.
|
|
pkg_check_modules(SDL2 QUIET sdl2)
|
|
if(SDL2_FOUND)
|
|
execute_process(COMMAND ${PKG_CONFIG_EXECUTABLE} --cflags sdl2
|
|
OUTPUT_VARIABLE _sdl_cflags OUTPUT_STRIP_TRAILING_WHITESPACE)
|
|
execute_process(COMMAND ${PKG_CONFIG_EXECUTABLE} --libs sdl2
|
|
OUTPUT_VARIABLE _sdl_libs OUTPUT_STRIP_TRAILING_WHITESPACE)
|
|
set(LUACPP_GUI_FLAGS "-DLUACPP_HAS_GUI=1 ${_sdl_cflags} ${_sdl_libs}")
|
|
message(STATUS "luacpp: SDL2 found, compiled programs get the 'gui' API")
|
|
else()
|
|
set(LUACPP_GUI_FLAGS "")
|
|
message(STATUS "luacpp: SDL2 not found, the 'gui' API is disabled")
|
|
endif()
|
|
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 "${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_TESTS)
|
|
enable_testing()
|
|
add_subdirectory(tests)
|
|
endif()
|