diff --git a/CMakeLists.txt b/CMakeLists.txt
index 91ccc99..93ff150 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -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
` produces a relocatable tree:
+# /bin/luacpp, /include/luacpp/*.hpp, /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)
diff --git a/src/main.cpp b/src/main.cpp
index c9ca470..f0c5a8c 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -278,6 +278,25 @@ std::vector collect_lua_files(const fs::path& root) {
return files;
}
+// A release tarball ships the runtime headers next to the binary
+// (/bin/luacpp + /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;