Skip to content

Commit 7715528

Browse files
committed
refactor(lib): lua is external dependency
1 parent 51e2b65 commit 7715528

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+214
-30408
lines changed

.clang-format

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ IncludeCategories:
200200
Priority: 0
201201
- Regex: '^".*"' # quoted includes come next
202202
Priority: 1
203-
- Regex: '^<lib/.*>' # internal project headers
203+
- Regex: '^<lib/.*>' # internal project headers
204204
Priority: 2
205205
- Regex: '^<mrdocs/.*>' # internal project headers
206206
Priority: 3
@@ -214,12 +214,15 @@ IncludeCategories:
214214
Priority: 7
215215
- Regex: '^<duktape/.*>' # duktape headers
216216
Priority: 8
217-
- Regex: '^<(?!mrdocs/|clang/|llvm/|test_suite/|fmt/|duktape/)[^/]+/.*>' # other angle-bracket includes
217+
# Lua headers
218+
- Regex: '^<lua.*>' # Lua headers
218219
Priority: 9
219-
- Regex: '^<[^/]+>' # C++ standard headers like <vector>
220+
- Regex: '^<(?!mrdocs/|clang/|llvm/|test_suite/|fmt/|duktape/)[^/]+/.*>' # other angle-bracket includes
220221
Priority: 10
221-
- Regex: '.*' # fallback
222+
- Regex: '^<[^/]+>' # C++ standard headers like <vector>
222223
Priority: 11
224+
- Regex: '.*' # fallback
225+
Priority: 12
223226

224227
# Comments
225228
FixNamespaceComments: true

.github/workflows/ci.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,26 @@ jobs:
402402
run-tests: false
403403
trace-commands: true
404404

405+
- name: Install Lua
406+
uses: alandefreitas/cpp-actions/cmake-workflow@v1.8.12
407+
with:
408+
source-dir: ../third-party/lua
409+
url: https://github.com/lua/lua/archive/refs/tags/v5.4.8.tar.gz
410+
patches: |
411+
./third-party/lua/CMakeLists.txt
412+
./third-party/lua/LuaConfig.cmake.in
413+
build-dir: ${sourceDir}/build
414+
cc: ${{ steps.setup-cpp.outputs.cc }}
415+
cxx: ${{ steps.setup-cpp.outputs.cxx }}
416+
ccflags: ${{ matrix.common-ccflags }}
417+
cxxflags: ${{ steps.rmatrix.outputs.common-cxxflags }}
418+
build-type: ${{ matrix.build-type }}
419+
shared: false
420+
install: true
421+
install-prefix: ${sourceDir}/install
422+
run-tests: false
423+
trace-commands: true
424+
405425
- name: Install Libxml2
406426
uses: alandefreitas/cpp-actions/cmake-workflow@v1.8.12
407427
if: matrix.compiler == 'msvc'
@@ -479,6 +499,9 @@ jobs:
479499
-D CMAKE_EXE_LINKER_FLAGS="${{ steps.rmatrix.outputs.common-ldflags }}"
480500
-D LLVM_ROOT="${{ steps.rmatrix.outputs.llvm-path }}"
481501
-D duktape_ROOT="${{ steps.rmatrix.outputs.third-party-dir }}/duktape/install"
502+
-D LUA_ROOT="${{ steps.rmatrix.outputs.third-party-dir }}/lua/install"
503+
-D Lua_ROOT="${{ steps.rmatrix.outputs.third-party-dir }}/lua/install"
504+
-D lua_ROOT="${{ steps.rmatrix.outputs.third-party-dir }}/lua/install"
482505
${{ runner.os == 'Windows' && '-D LibXml2_ROOT=../third-party/libxml2/install' || '' }}
483506
export-compile-commands: true
484507
run-tests: true

CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,9 @@ if (NOT DUKTAPE_FOUND)
284284
find_package(Duktape REQUIRED CONFIG)
285285
endif()
286286

287+
# Lua
288+
find_package(Lua CONFIG REQUIRED)
289+
287290
unset(CMAKE_FOLDER)
288291

289292
#-------------------------------------------------
@@ -322,7 +325,6 @@ target_include_directories(mrdocs-core
322325
PRIVATE
323326
"${PROJECT_SOURCE_DIR}/src"
324327
"${PROJECT_BINARY_DIR}/src"
325-
"${PROJECT_SOURCE_DIR}/third-party/lua/src"
326328
)
327329
target_compile_definitions(
328330
mrdocs-core
@@ -345,6 +347,7 @@ target_include_directories(mrdocs-core
345347
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
346348
)
347349
target_link_libraries(mrdocs-core PRIVATE ${DUKTAPE_LIBRARY})
350+
target_link_libraries(mrdocs-core PRIVATE Lua::lua)
348351

349352
# Clang
350353
if (CLANG_SIMPLE_LIBS)

bootstrap.py

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,13 @@ class InstallOptions:
9191
duktape_build_dir: str = "<duktape-src-dir>/build/<duktape-build-type:lower><\"-\":if(cc)><cc:basename><\"-\":if(sanitizer)><sanitizer:lower>"
9292
duktape_install_dir: str = "<duktape-src-dir>/install/<duktape-build-type:lower><\"-\":if(cc)><cc:basename><\"-\":if(sanitizer)><sanitizer:lower>"
9393

94+
# Lua
95+
lua_src_dir: str = "<third-party-src-dir>/lua"
96+
lua_url: str = "https://github.com/lua/lua/archive/refs/tags/v5.4.8.tar.gz"
97+
lua_build_type: str = "<mrdocs-build-type>"
98+
lua_build_dir: str = "<lua-src-dir>/build/<lua-build-type:lower><\"-\":if(cc)><cc:basename><\"-\":if(sanitizer)><sanitizer:lower>"
99+
lua_install_dir: str = "<lua-src-dir>/install/<lua-build-type:lower><\"-\":if(cc)><cc:basename><\"-\":if(sanitizer)><sanitizer:lower>"
100+
94101
# LLVM
95102
llvm_src_dir: str = "<third-party-src-dir>/llvm-project"
96103
llvm_build_type: str = "<mrdocs-build-type>"
@@ -152,6 +159,11 @@ class InstallOptions:
152159
"duktape_build_type": "CMake build type for Duktape. (Release, Debug, RelWithDebInfo, MilRelSize)",
153160
"duktape_build_dir": "Directory where Duktape will be built.",
154161
"duktape_install_dir": "Directory where Duktape will be installed.",
162+
"lua_src_dir": "Directory for the Lua source code.",
163+
"lua_url": "Download URL for the Lua source archive.",
164+
"lua_build_type": "Build type for Lua. (Release, Debug)",
165+
"lua_build_dir": "Directory where Lua will be built.",
166+
"lua_install_dir": "Directory where Lua will be installed.",
155167
"libxml2_src_dir": "Directory for the libxml2 source code.",
156168
"libxml2_build_type": "CMake build type for libxml2: Release always recommended. (Release, Debug, RelWithDebInfo, MilRelSize)",
157169
"libxml2_build_dir": "Directory where libxml2 will be built.",
@@ -1162,6 +1174,63 @@ def install_duktape(self):
11621174
self.options.duktape_install_dir,
11631175
extra_args)
11641176

1177+
def install_lua(self):
1178+
# Resolve paths/values
1179+
self.prompt_dependency_path_option("lua_src_dir")
1180+
if not os.path.exists(self.options.lua_src_dir):
1181+
self.prompt_option("lua_url")
1182+
os.makedirs(self.options.lua_src_dir, exist_ok=True)
1183+
archive_filename = os.path.basename(self.options.lua_url)
1184+
archive_path = os.path.join(self.options.third_party_src_dir, archive_filename)
1185+
self.download_file(self.options.lua_url, archive_path)
1186+
1187+
# Extract lua-5.4.8.tar.gz, flatten top-level dir into lua_src_dir
1188+
mode = "r:gz" if archive_filename.endswith(".gz") else "r:*"
1189+
with tarfile.open(archive_path, mode) as tar:
1190+
top_level = tar.getmembers()[0].name.split('/')[0]
1191+
for member in tar.getmembers():
1192+
rel = os.path.relpath(member.name, top_level)
1193+
if rel == '.' or rel.startswith('..'):
1194+
continue
1195+
member.name = rel
1196+
tar.extract(member, path=self.options.lua_src_dir)
1197+
os.remove(archive_path)
1198+
1199+
# Copy our tiny CMake patch files (like we do for Duktape)
1200+
lua_patches = os.path.join(self.options.mrdocs_src_dir, 'third-party', 'lua')
1201+
if os.path.exists(lua_patches):
1202+
for fname in os.listdir(lua_patches):
1203+
src = os.path.join(lua_patches, fname)
1204+
dst = os.path.join(self.options.lua_src_dir, fname)
1205+
shutil.copy(src, dst)
1206+
1207+
# Lua’s own tree puts sources under src/; our CMakeLists handles that.
1208+
self.prompt_build_type_option("lua_build_type")
1209+
# align ABI expectations like we do for Duktape:
1210+
if not self.is_abi_compatible(self.options.mrdocs_build_type, self.options.lua_build_type):
1211+
if self.options.mrdocs_build_type.lower() == "debug":
1212+
self.options.lua_build_type = "OptimizedDebug"
1213+
else:
1214+
self.options.lua_build_type = self.options.mrdocs_build_type
1215+
1216+
self.prompt_dependency_path_option("lua_build_dir")
1217+
self.prompt_dependency_path_option("lua_install_dir")
1218+
1219+
extra_args = []
1220+
if self.options.sanitizer:
1221+
flag = self.sanitizer_flag_name(self.options.sanitizer)
1222+
for arg in ("CMAKE_C_FLAGS", "CMAKE_CXX_FLAGS"):
1223+
extra_args.append(f"-D{arg}=-fsanitize={flag} -fno-sanitize-recover={flag} -fno-omit-frame-pointer")
1224+
1225+
# Standard cmake_workflow like Duktape
1226+
self.cmake_workflow(
1227+
self.options.lua_src_dir,
1228+
self.options.lua_build_type,
1229+
self.options.lua_build_dir,
1230+
self.options.lua_install_dir,
1231+
extra_args
1232+
)
1233+
11651234
def install_libxml2(self):
11661235
self.prompt_dependency_path_option("libxml2_src_dir")
11671236
if not os.path.exists(self.options.libxml2_src_dir):
@@ -1338,7 +1407,9 @@ def create_cmake_presets(self):
13381407
"Duktape_ROOT": self.options.duktape_install_dir,
13391408
"libxml2_ROOT": self.options.libxml2_install_dir,
13401409
"LibXml2_ROOT": self.options.libxml2_install_dir,
1341-
"MRDOCS_BUILD_TESTS": self.options.mrdocs_build_tests,
1410+
"LUA_ROOT": self.options.lua_install_dir,
1411+
"Lua_ROOT": self.options.lua_install_dir,
1412+
"lua_ROOT": self.options.lua_install_dir,
13421413
"MRDOCS_BUILD_DOCS": False,
13431414
"MRDOCS_GENERATE_REFERENCE": False,
13441415
"MRDOCS_GENERATE_ANTORA_REFERENCE": False
@@ -2580,6 +2651,7 @@ def install_all(self):
25802651
self.probe_compilers()
25812652
self.install_ninja()
25822653
self.install_duktape()
2654+
self.install_lua()
25832655
self.install_llvm()
25842656
if self.prompt_option("mrdocs_build_tests"):
25852657
self.install_libxml2()

src/lib/Support/Lua.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@
1313
#include <mrdocs/Support/Path.hpp>
1414
#include <mrdocs/Support/Report.hpp>
1515
#include <llvm/Support/raw_ostream.h>
16+
#include <lua.h>
17+
#include <lualib.h>
1618
#include <format>
17-
#include <lua.hpp>
19+
#include <lauxlib.h>
1820
#include <print>
1921

20-
namespace clang {
2122
namespace mrdocs {
2223
namespace lua {
2324

@@ -1205,4 +1206,3 @@ lua_main()
12051206

12061207
} // lua
12071208
} // mrdocs
1208-
} // clang

src/lib/Support/LuaHandlebars.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#include "LuaHandlebars.hpp"
1212
#include <mrdocs/Support/Lua.hpp>
1313

14-
namespace clang {
1514
namespace mrdocs {
1615

1716
// VFALCO:
@@ -67,4 +66,3 @@ tryLoadHandlebars(
6766
}
6867

6968
} // mrdocs
70-
} // clang

src/lib/Support/LuaHandlebars.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#include <mrdocs/Platform.hpp>
1515
#include <mrdocs/Support/Lua.hpp>
1616

17-
namespace clang {
1817
namespace mrdocs {
1918

2019
/** Add the Handlebars Lua instance as a global
@@ -25,6 +24,5 @@ tryLoadHandlebars(
2524
lua::Context const& ctx);
2625

2726
} // mrdocs
28-
} // clang
2927

3028
#endif // MRDOCS_LIB_SUPPORT_LUAHANDLEBARS_HPP

src/lib/lua.cpp

Lines changed: 0 additions & 59 deletions
This file was deleted.

third-party/lua/CMakeLists.txt

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#
2+
# Licensed under the Apache License v2.0 with LLVM Exceptions.
3+
# See https://llvm.org/LICENSE.txt for license information.
4+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5+
#
6+
# Copyright (c) 2025 Alan de Freitas (alandefreitas@gmail.com)
7+
#
8+
# Official repository: https://github.com/cppalliance/mrdocs
9+
#
10+
#
11+
12+
cmake_minimum_required(VERSION 3.13)
13+
14+
set(lua_MAJOR_VERSION 5)
15+
set(lua_MINOR_VERSION 4)
16+
set(lua_PATCH_VERSION 8)
17+
set(lua_VERSION ${lua_MAJOR_VERSION}.${lua_MINOR_VERSION}.${lua_PATCH_VERSION})
18+
19+
option(CMAKE_VERBOSE_MAKEFILE "Create verbose makefile" OFF)
20+
option(BUILD_SHARED_LIBS "Create lua as a shared library" OFF)
21+
22+
project(lua VERSION ${lua_VERSION} LANGUAGES C)
23+
24+
# Check if ./src exists
25+
if (EXISTS "${CMAKE_CURRENT_LIST_DIR}/src")
26+
# Lua sources live under ./src
27+
file(GLOB LUA_SOURCES "${CMAKE_CURRENT_LIST_DIR}/src/*.c")
28+
list(REMOVE_ITEM LUA_SOURCES
29+
"${CMAKE_CURRENT_LIST_DIR}/src/lua.c"
30+
"${CMAKE_CURRENT_LIST_DIR}/src/luac.c"
31+
)
32+
file(GLOB LUA_HEADERS "${CMAKE_CURRENT_LIST_DIR}/src/*.h")
33+
else()
34+
# Lua sources live under ./ (github release)
35+
file(GLOB LUA_SOURCES "${CMAKE_CURRENT_LIST_DIR}/*.c")
36+
list(REMOVE_ITEM LUA_SOURCES
37+
"${CMAKE_CURRENT_LIST_DIR}/lua.c"
38+
"${CMAKE_CURRENT_LIST_DIR}/luac.c"
39+
)
40+
file(GLOB LUA_HEADERS "${CMAKE_CURRENT_LIST_DIR}/*.h")
41+
endif()
42+
43+
add_library(lua ${LUA_SOURCES} ${LUA_HEADERS})
44+
target_include_directories(lua
45+
PUBLIC
46+
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/src>
47+
$<INSTALL_INTERFACE:include>
48+
)
49+
50+
set_target_properties(lua PROPERTIES
51+
PUBLIC_HEADER "${LUA_HEADERS}"
52+
VERSION ${lua_VERSION}
53+
SOVERSION ${lua_MAJOR_VERSION}
54+
)
55+
56+
# Optional compatibility defines (uncomment if you need them)
57+
# target_compile_definitions(lua PRIVATE LUA_COMPAT_5_3)
58+
59+
install(TARGETS lua
60+
EXPORT LuaTargets
61+
ARCHIVE DESTINATION "lib"
62+
LIBRARY DESTINATION "lib"
63+
RUNTIME DESTINATION "bin"
64+
PUBLIC_HEADER DESTINATION "include"
65+
COMPONENT dev
66+
)
67+
68+
install(EXPORT LuaTargets
69+
FILE LuaTargets.cmake
70+
NAMESPACE Lua::
71+
DESTINATION "share/Lua"
72+
)
73+
74+
export(PACKAGE lua)
75+
76+
include(CMakePackageConfigHelpers)
77+
write_basic_package_version_file(
78+
"${PROJECT_BINARY_DIR}/LuaConfigVersion.cmake"
79+
COMPATIBILITY SameMajorVersion
80+
)
81+
82+
configure_file(LuaConfig.cmake.in
83+
"${PROJECT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/LuaConfig.cmake" @ONLY)
84+
85+
install(FILES
86+
"${PROJECT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/LuaConfig.cmake"
87+
"${PROJECT_BINARY_DIR}/LuaConfigVersion.cmake"
88+
DESTINATION "share/Lua"
89+
)

0 commit comments

Comments
 (0)