-
-
Notifications
You must be signed in to change notification settings - Fork 801
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
Scripting: Fix Lua 5.1/LuaJIT and Lua 5.2 problems #3376
Open
nuive
wants to merge
1
commit into
mgba-emu:master
Choose a base branch
from
nuive:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,225 @@ | ||
# Distributed under the OSI-approved BSD 3-Clause License. See accompanying | ||
# file Copyright.txt or https://cmake.org/licensing for details. | ||
|
||
#[=======================================================================[.rst: | ||
FindLuaJIT | ||
------- | ||
|
||
Locate LuaJIT library. | ||
|
||
This module defines: | ||
|
||
``LUAJIT_FOUND`` | ||
if false, do not try to link to LuaJIT | ||
``LUAJIT_LIBRARIES`` | ||
both luajit and luajitlib | ||
``LUAJIT_INCLUDE_DIR`` | ||
where to find luajit.h | ||
``LUAJIT_VERSION_STRING`` | ||
the version of LuaJIT found | ||
``LUAJIT_VERSION_MAJOR`` | ||
the major version of LuaJIT | ||
``LUAJIT_VERSION_MINOR`` | ||
the minor version of LuaJIT | ||
``LUAJIT_VERSION_PATCH`` | ||
the patch version of LuaJIT | ||
#]=======================================================================] | ||
|
||
cmake_policy(PUSH) # Policies apply to functions at definition-time | ||
cmake_policy(SET CMP0012 NEW) # For while(TRUE) | ||
|
||
unset(_luajit_include_subdirs) | ||
unset(_luajit_library_names) | ||
unset(_luajit_append_versions) | ||
|
||
# this is a function only to have all the variables inside go away automatically | ||
function(_luajit_get_versions) | ||
set(LUAJIT_VERSIONS2 2.1 2.0) | ||
|
||
if (LuaJIT_FIND_VERSION_EXACT) | ||
if (LuaJIT_FIND_VERSION_COUNT GREATER 1) | ||
set(_luajit_append_versions ${LuaJIT_FIND_VERSION_MAJOR}.${LuaJIT_FIND_VERSION_MINOR}) | ||
endif () | ||
elseif (LuaJIT_FIND_VERSION) | ||
# once there is a different major version supported this should become a loop | ||
if (NOT LuaJIT_FIND_VERSION_MAJOR GREATER 2) | ||
if (LuaJIT_FIND_VERSION_COUNT EQUAL 1) | ||
set(_luajit_append_versions ${LUAJIT_VERSIONS2}) | ||
else () | ||
foreach (subver IN LISTS LUAJIT_VERSIONS2) | ||
if (NOT subver VERSION_LESS ${LuaJIT_FIND_VERSION}) | ||
list(APPEND _luajit_append_versions ${subver}) | ||
endif () | ||
endforeach () | ||
# New version -> Search for it (heuristic only! Defines in include might have changed) | ||
if (NOT _luajit_append_versions) | ||
set(_luajit_append_versions ${LuaJIT_FIND_VERSION_MAJOR}.${LuaJIT_FIND_VERSION_MINOR}) | ||
endif() | ||
endif () | ||
endif () | ||
else () | ||
# once there is a different major version supported this should become a loop | ||
set(_luajit_append_versions ${LUAJIT_VERSIONS2}) | ||
endif () | ||
|
||
if (LUAJIT_Debug) | ||
message(STATUS "Considering following LuaJIT versions: ${_luajit_append_versions}") | ||
endif() | ||
|
||
set(_luajit_append_versions "${_luajit_append_versions}" PARENT_SCOPE) | ||
endfunction() | ||
|
||
function(_luajit_set_version_vars) | ||
set(_luajit_include_subdirs_raw "luajit") | ||
|
||
foreach (ver IN LISTS _luajit_append_versions) | ||
string(REGEX MATCH "^([0-9]+)\\.([0-9]+)$" _ver "${ver}") | ||
if (_ver) | ||
string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)$" "\\1" _version_major "${ver}") | ||
string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)$" "\\2" _version_minor "${ver}") | ||
list(APPEND _luajit_include_subdirs_raw | ||
luajit${_version_major}${_version_minor} | ||
luajit${_version_major}.${_version_minor} | ||
luajit-${_version_major}.${_version_minor} | ||
) | ||
endif () | ||
endforeach () | ||
|
||
# Prepend "include/" to each path directly after the path | ||
set(_luajit_include_subdirs "include") | ||
foreach (dir IN LISTS _luajit_include_subdirs_raw) | ||
list(APPEND _luajit_include_subdirs "${dir}" "include/${dir}") | ||
endforeach () | ||
|
||
set(_luajit_include_subdirs "${_luajit_include_subdirs}" PARENT_SCOPE) | ||
endfunction(_luajit_set_version_vars) | ||
|
||
function(_luajit_get_header_version) | ||
unset(LUAJIT_VERSION_STRING PARENT_SCOPE) | ||
set(_hdr_file "${LUAJIT_INCLUDE_DIR}/luajit.h") | ||
|
||
if (NOT EXISTS "${_hdr_file}") | ||
return() | ||
endif () | ||
|
||
file(STRINGS "${_hdr_file}" luajit_version_strings | ||
REGEX "^#define[ \t]+LUAJIT_VERSION[ \t]+\"LuaJIT [0-9].*") | ||
|
||
string(REGEX REPLACE ".*;#define[ \t]+LUAJIT_VERSION_MAJOR[ \t]+\"([0-9])\"[ \t]*;.*" "\\1" LUAJIT_VERSION_MAJOR ";${luajit_version_strings};") | ||
if (LUAJIT_VERSION_MAJOR MATCHES "^[0-9]+$") | ||
string(REGEX REPLACE ".*;#define[ \t]+LUAJIT_VERSION_MINOR[ \t]+\"([0-9])\"[ \t]*;.*" "\\1" LUAJIT_VERSION_MINOR ";${luajit_version_strings};") | ||
string(REGEX REPLACE ".*;#define[ \t]+LUAJIT_VERSION_RELEASE[ \t]+\"([0-9])\"[ \t]*;.*" "\\1" LUAJIT_VERSION_PATCH ";${luajit_version_strings};") | ||
set(LUAJIT_VERSION_STRING "${LUAJIT_VERSION_MAJOR}.${LUAJIT_VERSION_MINOR}.${LUAJIT_VERSION_PATCH}") | ||
else () | ||
string(REGEX REPLACE ".*;#define[ \t]+LUAJIT_VERSION[ \t]+\"LuaJIT ([0-9.]+[^\"]*)\"[ \t]*;.*" "\\1" LUAJIT_VERSION_STRING ";${luajit_version_strings};") | ||
string(REGEX REPLACE "^([0-9]+)\\.[^\"]*$" "\\1" LUAJIT_VERSION_MAJOR "${LUAJIT_VERSION_STRING}") | ||
string(REGEX REPLACE "^[0-9]+\\.([0-9]+)[^\"]*$" "\\1" LUAJIT_VERSION_MINOR "${LUAJIT_VERSION_STRING}") | ||
string(REGEX REPLACE "^[0-9]+\\.[0-9]+\\.([0-9]).*" "\\1" LUAJIT_VERSION_PATCH "${LUAJIT_VERSION_STRING}") | ||
endif () | ||
foreach (ver IN LISTS _luajit_append_versions) | ||
if (ver STREQUAL "${LUAJIT_VERSION_MAJOR}.${LUAJIT_VERSION_MINOR}") | ||
set(LUAJIT_VERSION_MAJOR ${LUAJIT_VERSION_MAJOR} PARENT_SCOPE) | ||
set(LUAJIT_VERSION_MINOR ${LUAJIT_VERSION_MINOR} PARENT_SCOPE) | ||
set(LUAJIT_VERSION_PATCH ${LUAJIT_VERSION_PATCH} PARENT_SCOPE) | ||
set(LUAJIT_VERSION_STRING ${LUAJIT_VERSION_STRING} PARENT_SCOPE) | ||
return() | ||
endif () | ||
endforeach () | ||
endfunction(_luajit_get_header_version) | ||
|
||
function(_luajit_find_header) | ||
_luajit_set_version_vars() | ||
|
||
# Initialize as local variable | ||
set(CMAKE_IGNORE_PATH ${CMAKE_IGNORE_PATH}) | ||
while (TRUE) | ||
# Find the next header to test. Check each possible subdir in order | ||
# This prefers e.g. higher versions as they are earlier in the list | ||
# It is also consistent with previous versions of FindLua | ||
foreach (subdir IN LISTS _luajit_include_subdirs) | ||
find_path(LUAJIT_INCLUDE_DIR luajit.h | ||
HINTS ENV LUAJIT_DIR | ||
PATH_SUFFIXES ${subdir} | ||
) | ||
if (LUAJIT_INCLUDE_DIR) | ||
break() | ||
endif() | ||
endforeach() | ||
# Did not found header -> Fail | ||
if (NOT LUAJIT_INCLUDE_DIR) | ||
return() | ||
endif() | ||
_luajit_get_header_version() | ||
# Found accepted version -> Ok | ||
if (LUAJIT_VERSION_STRING) | ||
if (LUAJIT_Debug) | ||
message(STATUS "Found suitable version ${LUAJIT_VERSION_STRING} in ${LUAJIT_INCLUDE_DIR}/lua.h") | ||
endif() | ||
return() | ||
endif() | ||
# Found wrong version -> Ignore this path and retry | ||
if (LUAJIT_Debug) | ||
message(STATUS "Ignoring unsuitable version in ${LUAJIT_INCLUDE_DIR}") | ||
endif() | ||
list(APPEND CMAKE_IGNORE_PATH "${LUAJIT_INCLUDE_DIR}") | ||
unset(LUAJIT_INCLUDE_DIR CACHE) | ||
unset(LUAJIT_INCLUDE_DIR) | ||
unset(LUAJIT_INCLUDE_DIR PARENT_SCOPE) | ||
endwhile () | ||
endfunction() | ||
|
||
_luajit_get_versions() | ||
_luajit_find_header() | ||
_luajit_get_header_version() | ||
unset(_luajit_append_versions) | ||
|
||
if (LUAJIT_VERSION_STRING) | ||
set(_luajit_library_names | ||
luajit${LUAJIT_VERSION_MAJOR}${LUAJIT_VERSION_MINOR} | ||
luajit${LUAJIT_VERSION_MAJOR}.${LUAJIT_VERSION_MINOR} | ||
luajit-${LUAJIT_VERSION_MAJOR}.${LUAJIT_VERSION_MINOR} | ||
luajit.${LUAJIT_VERSION_MAJOR}.${LUAJIT_VERSION_MINOR} | ||
lua51 | ||
lua5.1 | ||
lua-5.1 | ||
lua.5.1 | ||
) | ||
endif () | ||
|
||
find_library(LUAJIT_LIBRARY | ||
NAMES ${_luajit_library_names} luajit | ||
NAMES_PER_DIR | ||
HINTS | ||
ENV LUAJIT_DIR | ||
PATH_SUFFIXES lib | ||
) | ||
unset(_luajit_library_names) | ||
|
||
if (LUAJIT_LIBRARY) | ||
# include the math library for Unix | ||
if (UNIX AND NOT APPLE AND NOT BEOS) | ||
find_library(LUAJIT_MATH_LIBRARY m) | ||
mark_as_advanced(LUAJIT_MATH_LIBRARY) | ||
set(LUAJIT_LIBRARIES "${LUAJIT_LIBRARY};${LUAJIT_MATH_LIBRARY}") | ||
|
||
# include dl library for statically-linked Lua library | ||
get_filename_component(LUAJIT_LIB_EXT ${LUAJIT_LIBRARY} EXT) | ||
if(LUAJIT_LIB_EXT STREQUAL CMAKE_STATIC_LIBRARY_SUFFIX) | ||
list(APPEND LUAJIT_LIBRARIES ${CMAKE_DL_LIBS}) | ||
endif() | ||
|
||
# For Windows and Mac, don't need to explicitly include the math library | ||
else () | ||
set(LUAJIT_LIBRARIES "${LUAJIT_LIBRARY}") | ||
endif () | ||
endif () | ||
|
||
# handle the QUIETLY and REQUIRED arguments and set LUAJIT_FOUND to TRUE if | ||
# all listed variables are TRUE | ||
FIND_PACKAGE_HANDLE_STANDARD_ARGS(LuaJIT | ||
REQUIRED_VARS LUAJIT_LIBRARIES LUAJIT_INCLUDE_DIR | ||
VERSION_VAR LUAJIT_VERSION_STRING) | ||
|
||
mark_as_advanced(LUAJIT_INCLUDE_DIR LUAJIT_LIBRARY) | ||
|
||
cmake_policy(POP) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the provenance of this file? There's no info in the file itself, and it looks like it comes from somewhere
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like it's coming mostly from CMake's FindLua.cmake file, but it's changed kind of weirdly for LuaJIT. It appears to be mostly just find and replace
lua
withluajit
and such, then some changes to handle LuaJIT versions (2.1 and 2.0?), and there's kind of weird remanents remaining with lua functions getting a luajit replacement, but then also leaving in the older functions. The license is removed for some reason, and even a comment mentioning FindLua was removed here for some reason.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, it's bassed on CMake FindLua.cmake. I forgot to restore/modify comments.
The old Lua functions are there because of library name (lua51). I left them thinking LuaJIT could be built using different Lua versions in the future, but nowadays they could be removed and replaced those calls with "lua51" or "lua5.1" constants.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LuaJIT will never be anything but 5.1, as directly stated by Mike Pall -- the fenv change is too compatibility-breaking for the JIT core to ever work with 5.2+.
It might be interesting to someday consider supporting Ravi as a 5.2+ JIT engine, but I don't know what kind of API compatibility Ravi has or if it's even worth it since Ravi is much heavier than LuaJIT.