feat(cli): find the runtime headers next to the binary

A downloaded release has no checkout to refer to, and the include directory was
baked in at configure time. `luacpp run` now looks for include/luacpp/rt.hpp
beside its own executable and honours LUACPP_INCLUDE before falling back to that
path. CMake gained install rules producing the bin/include/share tree the
tarball is built from.
This commit is contained in:
BlackAngelTV committed 2026-11-18 10:40:24 +01:00
1 parent 9587d1ed49
commit a8364972da
2 files changed
+35 -2

No files matched your search

+15 -1
View File
@@ -40,7 +40,8 @@ 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_RUNTIME_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/include" CACHE PATH
"Directory holding the luacpp runtime headers (include/luacpp/*.hpp)")
set(LUACPP_LUA_CFLAGS "${_lua_cflags}")
set(LUACPP_LUA_LIBS "${_lua_libs}")
@@ -73,6 +74,19 @@ endif()
include(cmake/lua-package.cmake)
# --- install ------------------------------------------------------------------
# `cmake --install build --prefix <dir>` produces a relocatable tree:
# <dir>/bin/luacpp, <dir>/include/luacpp/*.hpp, <dir>/share/luacpp/examples
# `luacpp run` looks for include/ next to its own binary, so the tree can be moved.
include(GNUInstallDirs)
install(TARGETS luacpp RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")
install(FILES include/luacpp/rt.hpp include/luacpp/gui.hpp
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/luacpp")
install(DIRECTORY Demo/
DESTINATION "${CMAKE_INSTALL_DATADIR}/luacpp/examples"
PATTERN "build" EXCLUDE)
install(FILES README.md DESTINATION "${CMAKE_INSTALL_DATADIR}/doc/luacpp")
if(LUACPP_BUILD_TESTS)
enable_testing()
add_subdirectory(tests)
+20 -1
View File
@@ -278,6 +278,25 @@ std::vector<fs::path> collect_lua_files(const fs::path& root) {
return files;
}
// A release tarball ships the runtime headers next to the binary
// (<prefix>/bin/luacpp + <prefix>/include/luacpp), while a local build keeps
// using the include directory configured by CMake. LUACPP_INCLUDE wins over both.
std::string runtime_include_dir() {
if (const char* env = std::getenv("LUACPP_INCLUDE")) {
if (*env != '\0') return env;
}
#if defined(__linux__)
std::error_code ec;
fs::path exe = fs::read_symlink("/proc/self/exe", ec);
if (!ec) {
fs::path beside = exe.parent_path().parent_path() / "include";
std::error_code missing;
if (fs::exists(beside / "luacpp" / "rt.hpp", missing)) return beside.string();
}
#endif
return LUACPP_RUNTIME_INCLUDE_DIR;
}
int run_script(const Options& opt) {
fs::path entry = opt.inputs.front();
if (!fs::exists(entry)) {
@@ -323,7 +342,7 @@ int run_script(const Options& opt) {
for (const auto& job : jobs) command += " " + quoted(job.cpp.string());
command += " " + quoted(driver.string());
command += " -I" + quoted(srcDir.string());
command += " -I" + quoted(LUACPP_RUNTIME_INCLUDE_DIR);
command += " -I" + quoted(runtime_include_dir());
command += " " + lua_link_flags();
std::string gui_flags = LUACPP_GUI_FLAGS;
if (!gui_flags.empty()) command += " " + gui_flags;