11cmake_minimum_required (VERSION 3.20 )
22
3- project (Caffeine VERSION 0.1.0 LANGUAGES CXX )
3+ project (Caffeine VERSION 0.1.0 LANGUAGES CXX C )
44
55set (CMAKE_CXX_STANDARD 20)
66set (CMAKE_CXX_STANDARD_REQUIRED ON )
@@ -88,4 +88,92 @@ target_link_libraries(caf-encode PRIVATE Caffeine)
8888target_include_directories (caf-encode PRIVATE ${CMAKE_CURRENT_SOURCE_DIR } /src )
8989target_compile_features (caf-encode PRIVATE cxx_std_20 )
9090
91- add_subdirectory (tests )
91+ add_subdirectory (tests )
92+
93+ # ── Scripting (Lua + sol2) ────────────────────────────────────
94+ option (CAFFEINE_ENABLE_SCRIPTING "Enable Lua scripting support" OFF )
95+
96+ if (CAFFEINE_ENABLE_SCRIPTING)
97+ include (FetchContent )
98+
99+ # Build Lua 5.4 from source (sol2 v3.3.0 needs exact 5.4 API)
100+ FetchContent_Declare (lua54
101+ GIT_REPOSITORY https://github.com/lua/lua.git
102+ GIT_TAG v5.4.7
103+ GIT_SHALLOW TRUE
104+ )
105+ FetchContent_GetProperties (lua54)
106+ if (NOT lua54_POPULATED)
107+ FetchContent_Populate (lua54)
108+ set (LUA54_SRC ${lua54_SOURCE_DIR } )
109+ add_library (lua54 STATIC
110+ ${LUA54_SRC} /lapi.c
111+ ${LUA54_SRC} /lauxlib.c
112+ ${LUA54_SRC} /lbaselib.c
113+ ${LUA54_SRC} /lcode.c
114+ ${LUA54_SRC} /lcorolib.c
115+ ${LUA54_SRC} /lctype.c
116+ ${LUA54_SRC} /ldblib.c
117+ ${LUA54_SRC} /ldebug.c
118+ ${LUA54_SRC} /ldo.c
119+ ${LUA54_SRC} /ldump.c
120+ ${LUA54_SRC} /lfunc.c
121+ ${LUA54_SRC} /lgc.c
122+ ${LUA54_SRC} /linit.c
123+ ${LUA54_SRC} /liolib.c
124+ ${LUA54_SRC} /llex.c
125+ ${LUA54_SRC} /lmathlib.c
126+ ${LUA54_SRC} /lmem.c
127+ ${LUA54_SRC} /loadlib.c
128+ ${LUA54_SRC} /lobject.c
129+ ${LUA54_SRC} /lopcodes.c
130+ ${LUA54_SRC} /loslib.c
131+ ${LUA54_SRC} /lparser.c
132+ ${LUA54_SRC} /lstate.c
133+ ${LUA54_SRC} /lstring.c
134+ ${LUA54_SRC} /lstrlib.c
135+ ${LUA54_SRC} /ltable.c
136+ ${LUA54_SRC} /ltablib.c
137+ ${LUA54_SRC} /ltm.c
138+ ${LUA54_SRC} /lundump.c
139+ ${LUA54_SRC} /lutf8lib.c
140+ ${LUA54_SRC} /lvm.c
141+ ${LUA54_SRC} /lzio.c
142+ )
143+ target_include_directories (lua54 BEFORE PUBLIC ${LUA54_SRC} )
144+ # Create lua.hpp C++ wrapper so sol2's #include <lua.hpp> finds our 5.4 headers
145+ file (WRITE ${LUA54_SRC} /lua.hpp
146+ "extern \" C\" {\n "
147+ "#include \" lua.h\"\n "
148+ "#include \" lauxlib.h\"\n "
149+ "#include \" lualib.h\"\n "
150+ "}\n " )
151+ endif ()
152+
153+ # sol2 (header-only C++/Lua binding library)
154+ set (SOL2_BUILD_LUA OFF )
155+ FetchContent_Declare (sol2
156+ GIT_REPOSITORY https://github.com/ThePhD/sol2.git
157+ GIT_TAG v3.3.0
158+ GIT_SHALLOW TRUE
159+ )
160+ FetchContent_MakeAvailable (sol2)
161+
162+ # Patch sol2 optional_implementation.hpp for GCC 16+ compatibility
163+ execute_process (COMMAND python3 "${CMAKE_CURRENT_SOURCE_DIR } /cmake/patch_sol2_optional.py"
164+ "${sol2_SOURCE_DIR } /include/sol/optional_implementation.hpp"
165+ RESULT_VARIABLE PATCH_RESULT )
166+ if (NOT PATCH_RESULT EQUAL 0)
167+ message (FATAL_ERROR "Failed to patch sol2 for GCC 16 compatibility" )
168+ endif ()
169+
170+ target_link_libraries (Caffeine PRIVATE sol2::sol2 lua54 )
171+
172+ target_compile_definitions (Caffeine PUBLIC CF_HAS_SCRIPTING=1 )
173+
174+ target_sources (Caffeine PRIVATE
175+ src/script/ScriptEngine.cpp
176+ src/script/ScriptSystem.cpp
177+ src/script/ScriptWatcher.cpp
178+ )
179+ endif ()
0 commit comments