diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 2a80533a..03f03942 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -25,6 +25,9 @@ jobs: brew install openssl sqlite3 brew install libzip json-c + # Install Luau via Homebrew + brew install luau + # Install iOS development dependencies brew install llvm || true brew install libomp || true @@ -55,19 +58,41 @@ jobs: fi fi - # Note: Using Luau headers that are already in the project - echo "Using Luau headers from source/cpp/luau" + # Set up Luau paths + LUAU_PREFIX=$(brew --prefix luau) + echo "Using Homebrew Luau from $LUAU_PREFIX" + + # Check actual library and include structure + echo "Checking Luau installation structure..." + ls -la $LUAU_PREFIX/include/ || echo "Include directory not found" + ls -la $LUAU_PREFIX/lib/ || echo "Lib directory not found" + + # Find the actual library file + LUAU_LIBRARY=$(find $LUAU_PREFIX/lib -name "*.dylib" | head -1) + if [ -z "$LUAU_LIBRARY" ]; then + LUAU_LIBRARY=$(find $LUAU_PREFIX/lib -name "*.a" | head -1) + fi + + if [ -z "$LUAU_LIBRARY" ]; then + echo "Warning: Could not find Luau library, using default path assumption" + LUAU_LIBRARY="$LUAU_PREFIX/lib/liblua.dylib" + fi - # No need to install Lua or create custom headers - # The compatibility layer is added directly in source/lfs.c + echo "Found Luau library: $LUAU_LIBRARY" - # Create directories for compiler to find headers - mkdir -p build/include + # Set environment variables for Luau + echo "LUAU_INCLUDE_DIR=$LUAU_PREFIX/include" >> $GITHUB_ENV + echo "LUAU_LIB_DIR=$LUAU_PREFIX/lib" >> $GITHUB_ENV + echo "LUA_INCLUDE_DIR=$LUAU_PREFIX/include" >> $GITHUB_ENV + echo "LUA_LIBRARIES=$LUAU_LIBRARY" >> $GITHUB_ENV - # Set environment variables to use internal Luau headers - echo "LUA_INCLUDE_DIR=$GITHUB_WORKSPACE/source/cpp/luau" >> $GITHUB_ENV - echo "CFLAGS=-I$GITHUB_WORKSPACE/source/cpp/luau -I$GITHUB_WORKSPACE/source" >> $GITHUB_ENV - echo "CXXFLAGS=-I$GITHUB_WORKSPACE/source/cpp/luau -I$GITHUB_WORKSPACE/source" >> $GITHUB_ENV + # Set compiler flags to include Luau headers - include both Homebrew and project headers + echo "CFLAGS=-I$LUAU_PREFIX/include -I$GITHUB_WORKSPACE -I$GITHUB_WORKSPACE/source" >> $GITHUB_ENV + echo "CXXFLAGS=-I$LUAU_PREFIX/include -I$GITHUB_WORKSPACE -I$GITHUB_WORKSPACE/source" >> $GITHUB_ENV + + # Also pass these to subsequent commands - use project headers as fallback + export LUA_INCLUDE_DIR="$GITHUB_WORKSPACE/source/cpp/luau" + export LUA_LIBRARIES=$LUAU_LIBRARY # Create directories for project resources (only once) mkdir -p Resources/AIData/LocalModels @@ -83,46 +108,71 @@ jobs: # Create simplified Find*.cmake files for building # First, create FindLuaFileSystem.cmake - echo "# FindLuaFileSystem.cmake - Using internal Luau headers" > cmake/FindLuaFileSystem.cmake + echo "# FindLuaFileSystem.cmake - Using Homebrew Luau" > cmake/FindLuaFileSystem.cmake echo "# Create a target for lfs.c that ensures it can find the Luau headers" >> cmake/FindLuaFileSystem.cmake echo "function(add_lfs_target)" >> cmake/FindLuaFileSystem.cmake echo " if(TARGET lfs_obj)" >> cmake/FindLuaFileSystem.cmake echo " return()" >> cmake/FindLuaFileSystem.cmake echo " endif()" >> cmake/FindLuaFileSystem.cmake echo "" >> cmake/FindLuaFileSystem.cmake - echo " message(STATUS \"Setting up LuaFileSystem with native Luau headers\")" >> cmake/FindLuaFileSystem.cmake + echo " message(STATUS \"Setting up LuaFileSystem with Homebrew Luau headers\")" >> cmake/FindLuaFileSystem.cmake echo "" >> cmake/FindLuaFileSystem.cmake echo " add_library(lfs_obj OBJECT \${CMAKE_SOURCE_DIR}/source/lfs.c)" >> cmake/FindLuaFileSystem.cmake echo "" >> cmake/FindLuaFileSystem.cmake + echo " # Get Luau include directory from environment or find it" >> cmake/FindLuaFileSystem.cmake + echo " if(DEFINED ENV{LUAU_INCLUDE_DIR})" >> cmake/FindLuaFileSystem.cmake + echo " set(LUAU_INCLUDE_DIR \$ENV{LUAU_INCLUDE_DIR})" >> cmake/FindLuaFileSystem.cmake + echo " else()" >> cmake/FindLuaFileSystem.cmake + echo " # Try to find it using brew" >> cmake/FindLuaFileSystem.cmake + echo " execute_process(" >> cmake/FindLuaFileSystem.cmake + echo " COMMAND brew --prefix luau" >> cmake/FindLuaFileSystem.cmake + echo " OUTPUT_VARIABLE LUAU_PREFIX" >> cmake/FindLuaFileSystem.cmake + echo " OUTPUT_STRIP_TRAILING_WHITESPACE" >> cmake/FindLuaFileSystem.cmake + echo " )" >> cmake/FindLuaFileSystem.cmake + echo " set(LUAU_INCLUDE_DIR \"\${LUAU_PREFIX}/include\")" >> cmake/FindLuaFileSystem.cmake + echo " endif()" >> cmake/FindLuaFileSystem.cmake + echo "" >> cmake/FindLuaFileSystem.cmake echo " target_include_directories(lfs_obj PRIVATE" >> cmake/FindLuaFileSystem.cmake + echo " \${LUAU_INCLUDE_DIR}" >> cmake/FindLuaFileSystem.cmake echo " \${CMAKE_SOURCE_DIR}/source" >> cmake/FindLuaFileSystem.cmake - echo " \${CMAKE_SOURCE_DIR}/source/cpp" >> cmake/FindLuaFileSystem.cmake - echo " \${CMAKE_SOURCE_DIR}/source/cpp/luau" >> cmake/FindLuaFileSystem.cmake - echo " \${CMAKE_SOURCE_DIR}" >> cmake/FindLuaFileSystem.cmake echo " )" >> cmake/FindLuaFileSystem.cmake echo "" >> cmake/FindLuaFileSystem.cmake - echo " target_compile_definitions(lfs_obj PRIVATE " >> cmake/FindLuaFileSystem.cmake - echo " LFS_USE_INTERNAL_LUAU=1" >> cmake/FindLuaFileSystem.cmake - echo " LUAU_FASTFLAG_LUAERROR=1" >> cmake/FindLuaFileSystem.cmake - echo " )" >> cmake/FindLuaFileSystem.cmake + echo " # No need for special compile definitions with Homebrew Luau" >> cmake/FindLuaFileSystem.cmake echo "" >> cmake/FindLuaFileSystem.cmake echo " set_target_properties(lfs_obj PROPERTIES" >> cmake/FindLuaFileSystem.cmake echo " C_STANDARD 99" >> cmake/FindLuaFileSystem.cmake echo " POSITION_INDEPENDENT_CODE ON" >> cmake/FindLuaFileSystem.cmake echo " )" >> cmake/FindLuaFileSystem.cmake echo "" >> cmake/FindLuaFileSystem.cmake - echo " message(STATUS \"Using internal Luau headers from \${CMAKE_SOURCE_DIR}/source/cpp/luau\")" >> cmake/FindLuaFileSystem.cmake + echo " message(STATUS \"Using Homebrew Luau headers from \${LUAU_INCLUDE_DIR}\")" >> cmake/FindLuaFileSystem.cmake echo "endfunction()" >> cmake/FindLuaFileSystem.cmake # Then create FindLua.cmake - echo "# FindLua.cmake - Using internal Luau headers" > cmake/FindLua.cmake - echo "# This is a minimal finder that points to internal Luau headers" >> cmake/FindLua.cmake + echo "# FindLua.cmake - Using Homebrew Luau" > cmake/FindLua.cmake + echo "# This is a minimal finder that points to Homebrew Luau" >> cmake/FindLua.cmake + echo "" >> cmake/FindLua.cmake + echo "# Try to get from environment first" >> cmake/FindLua.cmake + echo "if(DEFINED ENV{LUAU_INCLUDE_DIR} AND DEFINED ENV{LUA_LIBRARIES})" >> cmake/FindLua.cmake + echo " set(LUA_INCLUDE_DIR \$ENV{LUAU_INCLUDE_DIR})" >> cmake/FindLua.cmake + echo " set(LUA_LIBRARIES \$ENV{LUA_LIBRARIES})" >> cmake/FindLua.cmake + echo " set(LUA_FOUND TRUE)" >> cmake/FindLua.cmake + echo "else()" >> cmake/FindLua.cmake + echo " # Try to find it using brew" >> cmake/FindLua.cmake + echo " execute_process(" >> cmake/FindLua.cmake + echo " COMMAND brew --prefix luau" >> cmake/FindLua.cmake + echo " OUTPUT_VARIABLE LUAU_PREFIX" >> cmake/FindLua.cmake + echo " OUTPUT_STRIP_TRAILING_WHITESPACE" >> cmake/FindLua.cmake + echo " )" >> cmake/FindLua.cmake echo "" >> cmake/FindLua.cmake - echo "set(LUA_INCLUDE_DIR \"\${CMAKE_SOURCE_DIR}/source/cpp/luau\")" >> cmake/FindLua.cmake - echo "set(LUA_LIBRARIES \"\")" >> cmake/FindLua.cmake - echo "set(LUA_FOUND TRUE)" >> cmake/FindLua.cmake + echo " if(LUAU_PREFIX)" >> cmake/FindLua.cmake + echo " set(LUA_INCLUDE_DIR \"\${LUAU_PREFIX}/include\")" >> cmake/FindLua.cmake + echo " set(LUA_LIBRARIES \"\${LUAU_PREFIX}/lib/libluau.dylib\")" >> cmake/FindLua.cmake + echo " set(LUA_FOUND TRUE)" >> cmake/FindLua.cmake + echo " endif()" >> cmake/FindLua.cmake + echo "endif()" >> cmake/FindLua.cmake echo "" >> cmake/FindLua.cmake - echo "message(STATUS \"Using internal Luau headers from \${LUA_INCLUDE_DIR}\")" >> cmake/FindLua.cmake + echo "message(STATUS \"Using Homebrew Luau headers from \${LUA_INCLUDE_DIR}\")" >> cmake/FindLua.cmake + echo "message(STATUS \"Using Homebrew Luau libraries: \${LUA_LIBRARIES}\")" >> cmake/FindLua.cmake echo "Created CMake module files" @@ -130,12 +180,11 @@ jobs: echo "CMake directory contents:" ls -la cmake/ - # Show current environment for debugging - echo "DYLD_LIBRARY_PATH: $DYLD_LIBRARY_PATH" - echo "Lua location in PATH (may not exist, that's okay):" - which lua || echo "Lua not found in PATH (this is expected)" - echo "Lua version (may not exist, that's okay):" - lua -v || echo "Lua version command failed (this is expected)" + # Show Luau version and paths + echo "Luau location in PATH:" + which luau || echo "Luau not found in PATH (this is unexpected)" + echo "Luau include directory: $LUAU_INCLUDE_DIR" + echo "Luau library directory: $LUAU_LIB_DIR" - name: Install Dobby (Optional) id: install-dobby diff --git a/CMakeLists.txt b/CMakeLists.txt index 673fb4af..eb3dd25d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,76 +19,53 @@ set(CMAKE_OSX_ARCHITECTURES "arm64" CACHE STRING "Build architectures for iOS") # Find Lua - try multiple approaches set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake") -# Handle Lua finding on macOS +# Handle Luau finding on macOS if(APPLE) # First check for environment variables set by the workflow - if(DEFINED ENV{LUA_DIR} AND DEFINED ENV{LUA_INCLUDE_DIR} AND DEFINED ENV{LUA_LIBRARIES}) - message(STATUS "Using Lua from environment variables") - set(LUA_INCLUDE_DIR "$ENV{LUA_INCLUDE_DIR}") + if(DEFINED ENV{LUAU_INCLUDE_DIR} AND DEFINED ENV{LUA_LIBRARIES}) + message(STATUS "Using Luau from environment variables") + set(LUA_INCLUDE_DIR "$ENV{LUAU_INCLUDE_DIR}") set(LUA_LIBRARIES "$ENV{LUA_LIBRARIES}") set(LUA_FOUND TRUE) else() - # Check Homebrew Lua location as fallback + # Check Homebrew Luau location execute_process( - COMMAND brew --prefix lua - OUTPUT_VARIABLE BREW_LUA_PREFIX + COMMAND brew --prefix luau + OUTPUT_VARIABLE BREW_LUAU_PREFIX OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET ) - if(BREW_LUA_PREFIX) - message(STATUS "Found Homebrew Lua at: ${BREW_LUA_PREFIX}") - set(LUA_INCLUDE_DIR "${BREW_LUA_PREFIX}/include") + if(BREW_LUAU_PREFIX) + message(STATUS "Found Homebrew Luau at: ${BREW_LUAU_PREFIX}") + set(LUA_INCLUDE_DIR "${BREW_LUAU_PREFIX}/include") - # Look for the lua library file + # Look for the luau library file find_library(LUA_LIBRARIES - NAMES lua lua5.4 lua5.3 liblua liblua5.4 liblua5.3 - PATHS "${BREW_LUA_PREFIX}/lib" NO_DEFAULT_PATH) + NAMES luau libluau + PATHS "${BREW_LUAU_PREFIX}/lib" NO_DEFAULT_PATH) if(LUA_LIBRARIES) - message(STATUS "Found Lua library: ${LUA_LIBRARIES}") + message(STATUS "Found Luau library: ${LUA_LIBRARIES}") set(LUA_FOUND TRUE) else() # Hardcode as a last resort - set(LUA_LIBRARIES "${BREW_LUA_PREFIX}/lib/liblua.dylib") - message(STATUS "Using hardcoded Lua library path: ${LUA_LIBRARIES}") + set(LUA_LIBRARIES "${BREW_LUAU_PREFIX}/lib/libluau.dylib") + message(STATUS "Using hardcoded Luau library path: ${LUA_LIBRARIES}") set(LUA_FOUND TRUE) endif() else() - message(STATUS "Homebrew Lua not found via brew --prefix lua") + message(STATUS "Homebrew Luau not found. Please install with: brew install luau") endif() endif() endif() -# Try standard find_package +# Try standard find_package with our custom finder module find_package(Lua QUIET) -# If not found, try FindLua51 or FindLua (both come with CMake) -if(NOT LUA_FOUND) - find_package(Lua51 QUIET) - if(LUA51_FOUND) - set(LUA_INCLUDE_DIR ${LUA_INCLUDE_DIR}) - set(LUA_LIBRARIES ${LUA_LIBRARIES}) - set(LUA_FOUND TRUE) - endif() -endif() - -# Last attempt - look for lua manually -if(NOT LUA_FOUND AND NOT DEFINED LUA_LIBRARIES) - # Try to find the libraries manually - find_library(LUA_LIBRARIES NAMES lua lua54 lua5.4 lua53 lua5.3 liblua) - find_path(LUA_INCLUDE_DIR NAMES lua.h - PATH_SUFFIXES lua lua5.4 lua5.3 include/lua) - - if(LUA_LIBRARIES AND LUA_INCLUDE_DIR) - message(STATUS "Found Lua manually: ${LUA_LIBRARIES}") - set(LUA_FOUND TRUE) - endif() -endif() - -# Check if Lua was found +# Check if Luau was found if(NOT LUA_FOUND OR NOT DEFINED LUA_LIBRARIES) - message(FATAL_ERROR "Could not find Lua. Please install Lua and its development files.") + message(FATAL_ERROR "Could not find Luau. Please install Luau with: brew install luau") endif() message(STATUS "Using Lua include dir: ${LUA_INCLUDE_DIR}") diff --git a/cmake/FindLua.cmake b/cmake/FindLua.cmake index d0dc173b..80644e34 100644 --- a/cmake/FindLua.cmake +++ b/cmake/FindLua.cmake @@ -1,104 +1,74 @@ -# FindLua.cmake -# This module finds Lua libraries and includes needed for our project -# This is used if the standard FindLua provided by CMake doesn't work +# FindLua.cmake for Homebrew Luau +# This module finds Luau libraries and includes installed via Homebrew # Variables this module defines: -# LUA_FOUND - True if Lua was found -# LUA_INCLUDE_DIR - Directory containing Lua headers -# LUA_LIBRARIES - Libraries needed to use Lua +# LUA_FOUND - True if Luau was found +# LUA_INCLUDE_DIR - Directory containing Luau headers +# LUA_LIBRARIES - Libraries needed to use Luau -# Try to find Lua in standard locations -find_path(LUA_INCLUDE_DIR lua.h - PATHS - /usr/include - /usr/local/include - /opt/local/include - /opt/homebrew/include - /usr/local/opt/lua/include - /usr/local/Cellar/lua/*/include - /opt/homebrew/Cellar/lua/*/include - PATH_SUFFIXES - lua - lua5.4 - lua5.3 - lua5.2 - lua5.1 - include/lua - include/lua5.4 - include/lua5.3 - include/lua5.2 - include/lua5.1 -) - -# Try to find the lua library -find_library(LUA_LIBRARIES - NAMES - lua - liblua - lua5.4 - liblua5.4 - lua-5.4 - lua5.3 - liblua5.3 - lua-5.3 - lua5.2 - liblua5.2 - lua-5.2 - lua5.1 - liblua5.1 - lua-5.1 - PATHS - /usr/lib - /usr/local/lib - /opt/local/lib - /opt/homebrew/lib - /usr/local/opt/lua/lib - /usr/local/Cellar/lua/*/lib - /opt/homebrew/Cellar/lua/*/lib -) - -# Handle Homebrew on macOS -if(APPLE) +# Try to get from environment variables first +if(DEFINED ENV{LUAU_INCLUDE_DIR} AND DEFINED ENV{LUA_LIBRARIES}) + set(LUA_INCLUDE_DIR $ENV{LUAU_INCLUDE_DIR}) + set(LUA_LIBRARIES $ENV{LUA_LIBRARIES}) + set(LUA_FOUND TRUE) + message(STATUS "Using Luau from environment variables") + message(STATUS "Luau include dir: ${LUA_INCLUDE_DIR}") + message(STATUS "Luau libraries: ${LUA_LIBRARIES}") +else() + # Try to find using Homebrew + message(STATUS "Looking for Homebrew Luau installation") + execute_process( - COMMAND brew --prefix lua - OUTPUT_VARIABLE BREW_LUA_PREFIX + COMMAND brew --prefix luau + OUTPUT_VARIABLE LUAU_PREFIX OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET ) - if(BREW_LUA_PREFIX) - message(STATUS "Found Homebrew Lua at: ${BREW_LUA_PREFIX}") + if(LUAU_PREFIX) + message(STATUS "Found Homebrew Luau at: ${LUAU_PREFIX}") - if(NOT LUA_INCLUDE_DIR) - set(LUA_INCLUDE_DIR "${BREW_LUA_PREFIX}/include") - endif() + # Set include directory + set(LUA_INCLUDE_DIR "${LUAU_PREFIX}/include") + # Find the library - try multiple variations of names + find_library(LUA_LIBRARIES + NAMES luau lua liblua libluau + PATHS "${LUAU_PREFIX}/lib" + NO_DEFAULT_PATH + ) + + # If library not found directly, try with the default name or search for any .dylib if(NOT LUA_LIBRARIES) - find_library(LUA_LIBRARIES NAMES lua lua54 lua5.4 lua-5.4 liblua - PATHS "${BREW_LUA_PREFIX}/lib" NO_DEFAULT_PATH) + file(GLOB LUAU_LIBS "${LUAU_PREFIX}/lib/*.dylib") + if(LUAU_LIBS) + list(GET LUAU_LIBS 0 FIRST_LIB) + set(LUA_LIBRARIES "${FIRST_LIB}") + message(STATUS "Using found Luau library: ${LUA_LIBRARIES}") + else() + file(GLOB LUAU_STATIC_LIBS "${LUAU_PREFIX}/lib/*.a") + if(LUAU_STATIC_LIBS) + list(GET LUAU_STATIC_LIBS 0 FIRST_STATIC_LIB) + set(LUA_LIBRARIES "${FIRST_STATIC_LIB}") + message(STATUS "Using found Luau static library: ${LUA_LIBRARIES}") + else() + # Default fallback options + if(EXISTS "${LUAU_PREFIX}/lib/liblua.dylib") + set(LUA_LIBRARIES "${LUAU_PREFIX}/lib/liblua.dylib") + elseif(EXISTS "${LUAU_PREFIX}/lib/libluau.dylib") + set(LUA_LIBRARIES "${LUAU_PREFIX}/lib/libluau.dylib") + else() + message(WARNING "Could not find any Luau library in ${LUAU_PREFIX}/lib") + endif() + message(STATUS "Using hardcoded Luau library path: ${LUA_LIBRARIES}") + endif() + endif() endif() - endif() -endif() - -# For iOS cross-compilation -if(CMAKE_SYSTEM_NAME STREQUAL "iOS") - # Check if lua was built for iOS (custom build is usually needed) - # Try the standard Homebrew prefix as lua might be there - execute_process( - COMMAND brew --prefix - OUTPUT_VARIABLE BREW_PREFIX - OUTPUT_STRIP_TRAILING_WHITESPACE - ERROR_QUIET - ) - - if(BREW_PREFIX) - set(IOS_LUA_INCLUDE_DIR "${BREW_PREFIX}/include") - set(IOS_LUA_LIBRARY "${BREW_PREFIX}/lib/liblua.a") - if(EXISTS "${IOS_LUA_INCLUDE_DIR}/lua.h" AND EXISTS "${IOS_LUA_LIBRARY}") - set(LUA_INCLUDE_DIR "${IOS_LUA_INCLUDE_DIR}") - set(LUA_LIBRARIES "${IOS_LUA_LIBRARY}") - endif() + set(LUA_FOUND TRUE) + else() + message(WARNING "Homebrew Luau not found. Please install with: brew install luau") + set(LUA_FOUND FALSE) endif() endif() diff --git a/cmake/FindLuaFileSystem.cmake b/cmake/FindLuaFileSystem.cmake index e73a1a35..7e845b81 100644 --- a/cmake/FindLuaFileSystem.cmake +++ b/cmake/FindLuaFileSystem.cmake @@ -1,38 +1,60 @@ -# FindLuaFileSystem.cmake -# This module allows compilation of lfs.c specifically by finding external Lua +# FindLuaFileSystem.cmake for Homebrew Luau +# This module allows compilation of lfs.c using Homebrew Luau -# Create a target for lfs.c with external Lua +# Create a target for lfs.c with Homebrew Luau function(add_lfs_target) # Don't add it twice if(TARGET lfs_obj) return() endif() - message(STATUS "Setting up LuaFileSystem with external Lua headers") + message(STATUS "Setting up LuaFileSystem with Homebrew Luau headers") # Create an object library for lfs.c add_library(lfs_obj OBJECT ${CMAKE_SOURCE_DIR}/source/lfs.c) - # Look for lua in standard paths - find_path(LUA_INCLUDE_DIR lua.h - PATHS - /opt/homebrew/opt/lua/include - /opt/homebrew/include - /usr/local/include - /usr/include - PATH_SUFFIXES lua lua5.4 lua5.3 lua5.2 lua5.1 - ) + # First try the internal Luau headers from the project + set(INTERNAL_LUAU_DIR "${CMAKE_SOURCE_DIR}/source/cpp/luau") + + if(EXISTS "${INTERNAL_LUAU_DIR}/lua.h") + set(LUAU_INCLUDE_DIR ${INTERNAL_LUAU_DIR}) + message(STATUS "Using internal Luau headers from: ${LUAU_INCLUDE_DIR}") + else() + # Try to get from environment variables + if(DEFINED ENV{LUAU_INCLUDE_DIR}) + set(LUAU_INCLUDE_DIR $ENV{LUAU_INCLUDE_DIR}) + message(STATUS "Using Luau include dir from environment: ${LUAU_INCLUDE_DIR}") + else() + # Try to find it using Homebrew + execute_process( + COMMAND brew --prefix luau + OUTPUT_VARIABLE LUAU_PREFIX + OUTPUT_STRIP_TRAILING_WHITESPACE + ERROR_QUIET + ) + + if(LUAU_PREFIX) + set(LUAU_INCLUDE_DIR "${LUAU_PREFIX}/include") + message(STATUS "Found Homebrew Luau include directory: ${LUAU_INCLUDE_DIR}") + else() + message(WARNING "Luau include directory not found. Using internal headers.") + set(LUAU_INCLUDE_DIR "${INTERNAL_LUAU_DIR}") + endif() + endif() + endif() - # Add include directories + # Add include directories - include both the project root and source dir to help with relative includes target_include_directories(lfs_obj PRIVATE - ${LUA_INCLUDE_DIR} + ${LUAU_INCLUDE_DIR} + ${CMAKE_SOURCE_DIR} + ${CMAKE_SOURCE_DIR}/source + ${CMAKE_SOURCE_DIR}/source/cpp ) - # Enable standard paths with quotes if needed - if(LUA_INCLUDE_DIR) - message(STATUS "Found Lua include directory: ${LUA_INCLUDE_DIR}") - target_compile_definitions(lfs_obj PRIVATE LFS_USE_INCLUDE_QUOTES) - endif() + # Add compile definitions to help with compatibility + target_compile_definitions(lfs_obj PRIVATE + LUA_COMPAT_5_1=1 + ) # Ensure the compiler knows this is C set_target_properties(lfs_obj PROPERTIES @@ -40,5 +62,5 @@ function(add_lfs_target) POSITION_INDEPENDENT_CODE ON ) - message(STATUS "LFS using external Lua headers from: ${LUA_INCLUDE_DIR}") + message(STATUS "LFS using Homebrew Luau headers from: ${LUAU_INCLUDE_DIR}") endfunction() diff --git a/source/lfs.c b/source/lfs.c index 10a66454..ed299a40 100644 --- a/source/lfs.c +++ b/source/lfs.c @@ -81,126 +81,22 @@ #endif -#ifdef LFS_USE_INCLUDE_QUOTES -#include "lua.h" -#include "lauxlib.h" -#include "lualib.h" -#else -// Use standard C headers first +// Use standard C headers #include #include #include -#include -#include - -// Define LUA_VERSION_NUM before including any Lua headers -#define LUA_VERSION_NUM 501 // Pretend we're using Lua 5.1 -// Use the Luau headers directly from the project +// Include Luau headers - use the ones included in the project +// since we're having issues with finding the Homebrew ones #include "cpp/luau/lua.h" #include "cpp/luau/lualib.h" -// Define missing structure for luaL_Reg +// Define missing structure for luaL_Reg if needed +#ifndef LUAAPI typedef struct luaL_Reg { const char* name; lua_CFunction func; } luaL_Reg; - -// We need to handle luaL_error which returns void in Luau but int in standard Lua -static int luaL_error_compat(lua_State* L, const char* fmt, ...) { - va_list args; - va_start(args, fmt); - const char* msg = lua_pushvfstring(L, fmt, args); - va_end(args); - lua_error(L); - return 0; // Unreachable, just to satisfy the compiler -} -#define luaL_error luaL_error_compat - -// Function to simulate lua_pushcfunction for Luau -static void lua_pushcfunction_compat(lua_State* L, lua_CFunction f) { - lua_pushcclosurek(L, f, "lfs_func", 0, NULL); -} -#define lua_pushcfunction(L,f) lua_pushcclosurek(L, (f), "lfs_func", 0, NULL) - -// Custom implementation of lua_pushglobaltable for Luau -static void lua_pushglobaltable_compat(lua_State* L) { - lua_pushvalue(L, LUA_GLOBALSINDEX); -} -#define lua_pushglobaltable(L) lua_pushvalue(L, LUA_GLOBALSINDEX) - -// Implementation of luaL_register for Luau -static void luaL_register_compat(lua_State* L, const char* libname, const luaL_Reg* l) { - if (libname) { - lua_getglobal(L, libname); // get table - if (lua_type(L, -1) != LUA_TTABLE) { // check if exists - lua_pop(L, 1); // remove previous result - lua_newtable(L); // create a new table - lua_pushvalue(L, -1); // copy table - lua_setglobal(L, libname); // set global variable - } - } else { - lua_pushvalue(L, LUA_GLOBALSINDEX); // Use global table directly - } - - // Register all functions - if (l) { - for (; l->name; l++) { - lua_pushcclosurek(L, l->func, l->name, 0, NULL); - lua_setfield(L, -2, l->name); - } - } - - if (libname) - lua_pop(L, 1); // remove table from stack -} -#define luaL_register luaL_register_compat - -// Define some other functions that might be needed -#ifndef luaL_checktype -#define luaL_checktype(L,n,t) \ - ((lua_type(L, n) == t) || (lua_error(L), 0)) -#endif - -// Define compatibility macros for version checking in the code -#define LUA_VERSION "Lua 5.1" -#define LUA_RELEASE "Lua 5.1.5" - -// Forward declarations for any missing functions -static void* luaL_checkudata_compat(lua_State* L, int ud, const char* tname) { - void* p = lua_touserdata(L, ud); - if (p != NULL) { - if (lua_getmetatable(L, ud)) { - lua_getfield(L, LUA_REGISTRYINDEX, tname); - if (lua_rawequal(L, -1, -2)) { - lua_pop(L, 2); - return p; - } - lua_pop(L, 2); - } - } - luaL_error(L, "%s expected", tname); - return NULL; // unreachable -} -#define luaL_checkudata luaL_checkudata_compat - -// Define any other missing functions for compatability with lfs.c -#ifndef luaL_getmetafield -#define luaL_getmetafield(L,obj,e) \ - (lua_getmetatable(L, obj) ? ((lua_getfield(L, -1, e), lua_type(L, -1) != LUA_TNIL) ? 1 : (lua_pop(L, 2), 0)) : 0) -#endif - -#ifndef luaL_callmeta -#define luaL_callmeta(L,obj,e) \ - (luaL_getmetafield(L, obj, e) ? (lua_pushvalue(L, obj), lua_call(L, 1, 1), 1) : 0) -#endif - -#ifndef luaL_newmetatable -#define luaL_newmetatable(L,tname) \ - (lua_getfield(L, LUA_REGISTRYINDEX, tname) ? 0 : (lua_pop(L, 1), lua_newtable(L), lua_pushvalue(L, -1), lua_setfield(L, LUA_REGISTRYINDEX, tname), 1)) -#endif - -// End of compatibility layer #endif #include "lfs.h"