Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Lua]Switch to LuaJIT 2.1 #492

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ set(HTMLHELP_LIB_DIR ${HTMLHELP_DIR}/lib-${ARCH})
set(HTMLHELP_LIB ${HTMLHELP_LIB_DIR}/Htmlhelp.lib)

set(ORBITER_TOOL_MESHC "$<TARGET_FILE:meshc>")
set(ldoc $<TARGET_FILE:lua::exe> ${ORBITER_BINARY_ROOT_DIR}/packages/LDoc/ldoc.lua -v --multimodule)
set(ldoc $<TARGET_FILE:luajit> ${ORBITER_BINARY_ROOT_DIR}/packages/LDoc/ldoc.lua -v --multimodule)

find_package(OpenGL QUIET)
find_package(HTMLHelp)
Expand Down
23 changes: 12 additions & 11 deletions Extern/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,22 @@ add_subdirectory(zlib)

## LFS
add_library(lfs SHARED luafilesystem/src/lfs.c)
install(TARGETS lfs DESTINATION ${ORBITER_BINARY_ROOT_DIR})
install(TARGETS lfs DESTINATION ${ORBITER_INSTALL_SDK_DIR}/Utils/)
target_include_directories(lfs PUBLIC Lua)
target_link_libraries(lfs lua)
#install(TARGETS lfs DESTINATION ${ORBITER_BINARY_ROOT_DIR})
#install(TARGETS lfs DESTINATION ${ORBITER_INSTALL_SDK_DIR}/Utils/)
target_include_directories(lfs PUBLIC LUAJIT_DIR)
target_link_libraries(lfs libluajit)
SET_TARGET_PROPERTIES(lfs PROPERTIES LINK_FLAGS "/DEF:\"${CMAKE_CURRENT_SOURCE_DIR}/luafilesystem/src/lfs.def\"")

install(TARGETS lfs DESTINATION ${ORBITER_BINARY_ROOT_DIR})

set_target_properties(lfs PROPERTIES
ARCHIVE_OUTPUT_DIRECTORY ${ORBITER_BINARY_ROOT_DIR}
LIBRARY_OUTPUT_DIRECTORY ${ORBITER_BINARY_ROOT_DIR}
RUNTIME_OUTPUT_DIRECTORY ${ORBITER_BINARY_ROOT_DIR}
set_target_properties(lfs
PROPERTIES
FOLDER ${ORBITER_BINARY_ROOT_DIR}/Extern/Lua/
LIBRARY_OUTPUT_DIRECTORY ${ORBITER_BINARY_ROOT_DIR}/Extern/Lua/
ARCHIVE_OUTPUT_DIRECTORY ${ORBITER_BINARY_ROOT_DIR}/Extern/Lua/
RUNTIME_OUTPUT_DIRECTORY ${ORBITER_BINARY_ROOT_DIR}/Extern/Lua/
)
install(TARGETS lfs DESTINATION ${ORBITER_BINARY_ROOT_DIR}/Extern/Lua/)

add_custom_target(CopyLDoc ALL
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/ldoc/ ${ORBITER_BINARY_ROOT_DIR}/packages/ldoc
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/Penlight/lua/ ${ORBITER_BINARY_ROOT_DIR}/
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/Penlight/lua/ ${ORBITER_BINARY_ROOT_DIR}/Extern/Lua/lua
)
195 changes: 195 additions & 0 deletions Extern/Lua/Bundle.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
# Modfied from luajit.cmake Added LUA_ADD_EXECUTABLE Ryan Phillips <ryan at
# trolocsis.com> This CMakeLists.txt has been first taken from LuaDist Copyright
# (C) 2007-2011 LuaDist. Created by Peter Drahoš Redistribution and use of this
# file is allowed according to the terms of the MIT license. Debugged and (now
# seriously) modIFied by Ronan Collobert, for Torch7

if(NOT DEFINED BUNDLE_CMD)
set(BUNDLE_CMD luajit CACHE STRING "Use lua to do lua file bundle")
endif()
if(NOT DEFINED BUNDLE_CMD_ARGS)
set(BUNDLE_CMD_ARGS "" CACHE STRING "Bundle args for cross compile")
endif()
if(NOT DEFINED BUNDLE_USE_LUA2C)
set(BUNDLE_USE_LUA2C OFF CACHE BOOL "Use bin2c.lua do lua file bundle")
endif()

include (TestBigEndian)
TEST_BIG_ENDIAN(IS_BIG_ENDIAN)
if(IS_BIG_ENDIAN)
message(STATUS "BIG_ENDIAN")
else()
message(STATUS "LITTLE_ENDIAN")
endif()

if(BUNDLE_USE_LUA2C)
file(COPY ${CMAKE_CURRENT_LIST_DIR}/lua2c.lua DESTINATION ${LUA_TARGET_PATH})
endif()

macro(LUA_add_custom_commands luajit_target)
set(target_srcs "")
foreach(file ${ARGN})
if(${file} MATCHES ".*\\.lua$")
if(NOT IS_ABSOLUTE ${file})
set(file "${CMAKE_CURRENT_SOURCE_DIR}/${file}")
endif()
set(source_file ${file})
string(LENGTH ${CMAKE_SOURCE_DIR} _luajit_source_dir_length)
string(LENGTH ${file} _luajit_file_length)
math(EXPR _begin "${_luajit_source_dir_length} + 1")
math(EXPR _stripped_file_length
"${_luajit_file_length} - ${_luajit_source_dir_length} - 1")
string(SUBSTRING ${file}
${_begin}
${_stripped_file_length}
stripped_file)

set(
generated_file
"${CMAKE_BINARY_DIR}/luacode_tmp/${stripped_file}_${luajit_target}_generated.c"
)

add_custom_command(
OUTPUT ${generated_file}
MAIN_DEPENDENCY ${source_file}
DEPENDS ${LUA_TARGET}
COMMAND ${BUNDLE_CMD} ARGS
${BUNDLE_CMD_ARGS} lua2c.lua ${source_file} ${generated_file}
COMMENT "${BUNDLE_CMD} ${BUNDLE_CMD_ARGS} lua2c.lua ${source_file} ${generated_file}"
WORKING_DIRECTORY ${LUA_TARGET_PATH})

get_filename_component(basedir ${generated_file} PATH)
file(MAKE_DIRECTORY ${basedir})

set(target_srcs ${target_srcs} ${generated_file})
set_source_files_properties(${generated_file}
properties
generated
true # to say that "it is OK that the obj-
# files do not exist before build time"
)
else()
set(target_srcs ${target_srcs} ${file})
endif()
endforeach()
endmacro()

if(NOT LJ_TARGET_ARCH)
include(${CMAKE_CURRENT_LIST_DIR}/modules/DetectArchitecture.cmake)
detect_architecture(LJ_DETECTED_ARCH)
if("${LJ_DETECTED_ARCH}" STREQUAL "x86")
set(LJ_TARGET_ARCH "x86")
elseif("${LJ_DETECTED_ARCH}" STREQUAL "x86_64")
set(LJ_TARGET_ARCH "x64")
elseif("${LJ_DETECTED_ARCH}" STREQUAL "AArch64")
set(LJ_TARGET_ARCH "arm64")
elseif("${LJ_DETECTED_ARCH}" STREQUAL "ARM")
set(LJ_TARGET_ARCH "arm")
elseif("${LJ_DETECTED_ARCH}" STREQUAL "Mips64")
if(IS_BIG_ENDIAN)
set(LJ_TARGET_ARCH "mips64")
else()
set(LJ_TARGET_ARCH "mips64el")
endif()
elseif("${LJ_DETECTED_ARCH}" STREQUAL "Mips")
if(IS_BIG_ENDIAN)
set(LJ_TARGET_ARCH "mips")
else()
set(LJ_TARGET_ARCH "mipsel")
endif()
elseif("${LJ_DETECTED_ARCH}" STREQUAL "PowerPC")
if(LJ_64)
set(LJ_TARGET_ARCH "ppc64")
else()
set(LJ_TARGET_ARCH "ppc")
endif()
else()
message(FATAL_ERROR "Unsupported target architecture: '${LJ_DETECTED_ARCH}'")
endif()
endif()

macro(LUAJIT_add_custom_commands luajit_target)
set(target_srcs "")

if(WIN32)
set(LJDUMP_OPT -b -g -a ${LJ_TARGET_ARCH} -o windows)
elseif(APPLE)
set(LJDUMP_OPT -b -g -a ${LJ_TARGET_ARCH} -o osx)
elseif(ANDROID OR ${CMAKE_SYSTEM_NAME} STREQUAL Linux)
set(LJDUMP_OPT -b -g -a ${LJ_TARGET_ARCH} -o linux)
else()
set(LJDUMP_OPT -b -g -a ${LJ_TARGET_ARCH})
endif()

foreach(file ${ARGN})
if(${file} MATCHES ".*\\.lua$")
if(NOT IS_ABSOLUTE ${file})
set(file "${CMAKE_CURRENT_SOURCE_DIR}/${file}")
endif()
set(source_file ${file})
string(LENGTH ${CMAKE_SOURCE_DIR} _luajit_source_dir_length)
string(LENGTH ${file} _luajit_file_length)
math(EXPR _begin "${_luajit_source_dir_length} + 1")
math(EXPR _stripped_file_length
"${_luajit_file_length} - ${_luajit_source_dir_length} - 1")
string(SUBSTRING ${file}
${_begin}
${_stripped_file_length}
stripped_file)

set(
generated_file
"${CMAKE_CURRENT_BINARY_DIR}/jitted_tmp/${stripped_file}_${luajit_target}_generated${CMAKE_C_OUTPUT_EXTENSION}"
)
string(REPLACE ";" " " LJDUMP_OPT_STR "${LJDUMP_OPT}")

add_custom_command(
OUTPUT ${generated_file}
MAIN_DEPENDENCY ${source_file}
DEPENDS ${LUA_TARGET}
COMMAND ${BUNDLE_CMD} ARGS
${BUNDLE_CMD_ARGS}
${LJDUMP_OPT} ${source_file} ${generated_file}
COMMENT "${BUNDLE_CMD} ${BUNDLE_CMD_ARGS} ${LJDUMP_OPT_STR} ${source_file} ${generated_file}"
WORKING_DIRECTORY ${LUA_TARGET_PATH})
get_filename_component(basedir ${generated_file} PATH)
file(MAKE_DIRECTORY ${basedir})

set(target_srcs ${target_srcs} ${generated_file})
set_source_files_properties(${generated_file}
properties
external_object
true # this is an object file
generated
true # to say that "it is OK that the obj-
# files do not exist before build time"
)
else()
set(target_srcs ${target_srcs} ${file})
endif()
endforeach()
endmacro()

if(BUNDLE_USE_LUA2C)

macro(LUA_ADD_CUSTOM luajit_target)
lua_add_custom_commands(${luajit_target} ${ARGN})
endmacro()

macro(LUA_ADD_EXECUTABLE luajit_target)
lua_add_custom_commands(${luajit_target} ${ARGN})
add_executable(${luajit_target} ${target_srcs})
endmacro()

else()

macro(LUA_ADD_CUSTOM luajit_target)
luajit_add_custom_commands(${luajit_target} ${ARGN})
endmacro()

macro(LUA_ADD_EXECUTABLE luajit_target)
luajit_add_custom_commands(${luajit_target} ${ARGN})
add_executable(${luajit_target} ${target_srcs})
endmacro()

endif()
60 changes: 17 additions & 43 deletions Extern/Lua/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,50 +1,24 @@
project(lua)
cmake_minimum_required(VERSION 3.0)

Include(FetchContent)
include(FetchContent)

FetchContent_Declare(
lua
GIT_REPOSITORY https://github.com/lua/lua.git
GIT_TAG v5.1
luajitsrc
GIT_REPOSITORY https://github.com/LuaJIT/LuaJIT.git
GIT_TAG v2.1
)
FetchContent_MakeAvailable(lua)

file(GLOB LUA_SRC_FILES "${lua_SOURCE_DIR}/*.c")
file(GLOB LUA_HDR_FILES "${lua_SOURCE_DIR}/*.h")
FetchContent_MakeAvailable(luajitsrc)

list(REMOVE_ITEM LUA_SRC_FILES "${lua_SOURCE_DIR}/lua.c")
list(REMOVE_ITEM LUA_SRC_FILES "${lua_SOURCE_DIR}/luac.c")
list(REMOVE_ITEM LUA_SRC_FILES "${lua_SOURCE_DIR}/print.c")
set(LUAJIT_DIR ${luajitsrc_SOURCE_DIR})

# Executable - lua.exe
add_executable(lua-exe ${LUA_SRC_FILES} "${lua_SOURCE_DIR}/lua.c")
set_target_properties(lua-exe PROPERTIES OUTPUT_NAME lua)

set_target_properties(lua-exe PROPERTIES
ARCHIVE_OUTPUT_DIRECTORY ${ORBITER_BINARY_ROOT_DIR}
LIBRARY_OUTPUT_DIRECTORY ${ORBITER_BINARY_ROOT_DIR}
RUNTIME_OUTPUT_DIRECTORY ${ORBITER_BINARY_ROOT_DIR}
)

install(TARGETS lua-exe RUNTIME DESTINATION ${ORBITER_INSTALL_ROOT_DIR})
install(TARGETS lua-exe DESTINATION ${ORBITER_INSTALL_SDK_DIR}/lib/Lua)
install(TARGETS lua-exe DESTINATION ${ORBITER_INSTALL_SDK_DIR}/Utils/)

# Library - lua.dll
add_library(lua SHARED ${LUA_SRC_FILES})
target_compile_definitions(lua PUBLIC LUA_BUILD_AS_DLL)

set_target_properties(lua PROPERTIES
ARCHIVE_OUTPUT_DIRECTORY ${ORBITER_BINARY_ROOT_DIR}
LIBRARY_OUTPUT_DIRECTORY ${ORBITER_BINARY_ROOT_DIR}
RUNTIME_OUTPUT_DIRECTORY ${ORBITER_BINARY_ROOT_DIR}
)
install(TARGETS lua DESTINATION ${ORBITER_INSTALL_ROOT_DIR})

set(LUA_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/include)
file(COPY ${LUA_HDR_FILES} DESTINATION ${LUA_INCLUDE_DIR})
target_include_directories(lua INTERFACE ${LUA_INCLUDE_DIR})

# Aliases for easier reference
add_executable(lua::exe ALIAS lua-exe)
add_library(lua::lib ALIAS lua)
if(LUAJIT_DIR)
project(luajit)
include(LuaJIT.cmake)
if (PARENT_DIRECTORY)
set(LUA_TARGET luajit PARENT_SCOPE)
set(LUA_LIBRARIES libluajit PARENT_SCOPE)
endif()
else()
message(FATAL_ERROR "LUAJIT_DIR not set")
endif()
9 changes: 9 additions & 0 deletions Extern/Lua/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
The MIT License (MIT)
Copyright © 2021 <[email protected]>
Copyright © 2022 Lasse Lykke Jensen

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Loading
Loading